#include "world.hpp" #include "idalloc.hpp" #include "animqueue.hpp" #include LuaDefineType(World); Tangible::Tangible() : world_(nullptr) { } World::~World() { } 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_); // Initialize the ID allocator in master mode. id_global_pool_.init_master(10); // Prepare to manipulate the lua state. LuaVar world; LuaStack LS(lua_state_, world); // Put the world pointer into the lua registry. LS.newpointer(world, this, false); LS.setfield(LuaRegistry, "world", world); // Create the tangibles table in the registry. LS.setfield(LuaRegistry, "tangibles", LuaNewTable); // Initialize the SourceDB source_db_.initialize(lua_state_); source_db_.rebuild(); LS.result(); assert (lua_gettop(lua_state_) == 0); } void Tangible::be_a_player() { if (id_player_pool_ == nullptr) { id_player_pool_.reset(new IdPlayerPool(&world_->id_global_pool_)); anim_queue_.add(world_->id_global_pool_.get_one(), ""); anim_queue_.set_graphic("player"); } } void World::init_standalone() { // Load the lua source from disk then rebuild the environment. source_db_.update(); source_db_.rebuild(); // Run unit tests. source_db_.run_unittests(); // Create the player tangible. Tangible *player = tangible_make(lua_state_, 1, false); player->be_a_player(); } Tangible *World::tangible_get(int64_t id) { auto iter = tangibles_.find(id); if (iter == tangibles_.end()) { return nullptr; } else { return &iter->second; } } void World::update_view_map(AnimViewMap *vm, int64_t player_id, float radius) { vm->clear_updated_bits(); // Update the player's AnimView first. vm->update_one(tangible_get(player_id)->anim_queue_); const AnimView *player_view = vm->get_one(player_id); // Find out where's the center of the world. std::string plane = player_view->get_plane(); util::XYZ xyz = player_view->get_xyz(); // Update AnimViews for every tangible near the player. for (int64_t id : plane_map_.scan_radius(plane, xyz.x, xyz.y, 100.0)) { vm->update_one(tangible_get(id)->anim_queue_); } vm->delete_non_updated(); } Tangible *World::tangible_make(lua_State *L, int64_t id, bool pushdb) { LuaVar tangibles, metatab; LuaRet database; LuaStack LS(L, tangibles, database, metatab); // Allocate an ID if we don't already have one. if (id == 0) 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; t->plane_item_.set_id(id); t->anim_queue_.set_id(id); 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(); if (!pushdb) lua_pop(L, 1); return t; } World *World::fetch(lua_State *L) { LuaVar world; LuaStack LS(L, world); LS.getfield(world, LuaRegistry, "world"); World *w = LS.ckuserdata(world); LS.result(); return w; } 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, 0, true); return 1; }