2021-01-06 15:10:21 -05:00
|
|
|
|
|
|
|
|
#include "world.hpp"
|
|
|
|
|
#include "idalloc.hpp"
|
2021-01-16 01:24:33 -05:00
|
|
|
#include <iostream>
|
2021-01-06 15:10:21 -05:00
|
|
|
|
|
|
|
|
LuaDefineType(World);
|
|
|
|
|
|
2021-01-17 16:23:10 -05:00
|
|
|
Tangible::Tangible() : world_(nullptr) {
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-06 15:10:21 -05:00
|
|
|
World::~World() {
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-16 01:24:33 -05:00
|
|
|
World::World() {
|
|
|
|
|
// Create the lua state.
|
|
|
|
|
lua_state_ = lua_open();
|
|
|
|
|
if (lua_state_ == nullptr) {
|
|
|
|
|
std::cerr << "Cannot create lua state." << std::endl;
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialize the userdata metatables.
|
|
|
|
|
LuaStack::register_all_userdata(lua_state_);
|
|
|
|
|
|
2021-01-17 16:23:10 -05:00
|
|
|
// Initialize the ID allocator in master mode.
|
|
|
|
|
id_global_pool_.init_master(10);
|
|
|
|
|
|
2021-01-16 01:24:33 -05:00
|
|
|
// Prepare to manipulate the lua state.
|
2021-01-06 15:10:21 -05:00
|
|
|
LuaVar world;
|
2021-01-16 01:24:33 -05:00
|
|
|
LuaStack LS(lua_state_, world);
|
|
|
|
|
|
|
|
|
|
// Put the world pointer into the lua registry.
|
|
|
|
|
LS.newpointer(world, this, false);
|
2021-01-06 15:10:21 -05:00
|
|
|
LS.setfield(LuaRegistry, "world", world);
|
2021-01-16 01:24:33 -05:00
|
|
|
|
2021-01-17 16:23:10 -05:00
|
|
|
// Create the tangibles table in the registry.
|
|
|
|
|
LS.setfield(LuaRegistry, "tangibles", LuaNewTable);
|
|
|
|
|
|
2021-01-16 01:24:33 -05:00
|
|
|
// Initialize the SourceDB
|
|
|
|
|
source_db_.initialize(lua_state_);
|
|
|
|
|
|
|
|
|
|
// Do an initial lua source update and then environment rebuild.
|
|
|
|
|
source_db_.update();
|
|
|
|
|
source_db_.rebuild();
|
|
|
|
|
|
|
|
|
|
// Run unit tests.
|
|
|
|
|
source_db_.run_unittests();
|
2021-01-06 15:10:21 -05:00
|
|
|
}
|
|
|
|
|
|
2021-01-17 16:23:10 -05:00
|
|
|
Tangible *World::tangible_make(lua_State *L) {
|
|
|
|
|
LuaVar tangibles, metatab;
|
|
|
|
|
LuaRet database;
|
|
|
|
|
LuaStack LS(L, tangibles, database, metatab);
|
|
|
|
|
|
|
|
|
|
// Get a fresh ID.
|
|
|
|
|
int64_t id = id_global_pool_.alloc_id_for_thread(L);
|
|
|
|
|
|
|
|
|
|
// Create the C++ part of the structure.
|
|
|
|
|
Tangible *t = &tangibles_[id];
|
|
|
|
|
assert(t->world_ == nullptr);
|
|
|
|
|
t->world_ = this;
|
|
|
|
|
plane_map_.track(&t->plane_item_);
|
|
|
|
|
|
|
|
|
|
// Create the tangible's database and metatable.
|
|
|
|
|
LS.set(database, LuaNewTable);
|
|
|
|
|
LS.set(metatab, LuaNewTable);
|
|
|
|
|
LS.setmetatable(database, metatab);
|
|
|
|
|
|
|
|
|
|
// Store the database into the tangibles table.
|
|
|
|
|
LS.getfield(tangibles, LuaRegistry, "tangibles");
|
|
|
|
|
LS.rawset(tangibles, id, database);
|
|
|
|
|
|
|
|
|
|
// Populate the database and metatable with initial stuff.
|
|
|
|
|
LS.setfield(database, "inventory", LuaNewTable);
|
|
|
|
|
LS.setfield(database, "id", id);
|
|
|
|
|
LS.setfield(metatab, "id", id);
|
|
|
|
|
// LS.setfield(metatab, "__metatable", LuaNil);
|
|
|
|
|
|
|
|
|
|
LS.result();
|
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-06 15:10:21 -05:00
|
|
|
World *World::fetch(lua_State *L) {
|
|
|
|
|
LuaVar world;
|
|
|
|
|
LuaStack LS(L, world);
|
|
|
|
|
LS.getfield(world, LuaRegistry, "world");
|
|
|
|
|
World *w = LS.ckuserdata<World>(world);
|
|
|
|
|
LS.result();
|
|
|
|
|
return w;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 16:23:10 -05:00
|
|
|
LuaDefine(tangible_get, "c") {
|
|
|
|
|
LuaArg id;
|
|
|
|
|
LuaRet database;
|
|
|
|
|
LuaVar tangibles;
|
|
|
|
|
LuaStack LS(L, id, database, tangibles);
|
|
|
|
|
|
|
|
|
|
LS.getfield(tangibles, LuaRegistry, "tangibles");
|
|
|
|
|
LS.rawget(database, tangibles, id);
|
|
|
|
|
return LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaDefine(tangible_make, "c") {
|
|
|
|
|
World::fetch(L)->tangible_make(L);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|