Files
integration/luprex/cpp/world.cpp

200 lines
5.3 KiB
C++
Raw Normal View History

#include "world.hpp"
#include "idalloc.hpp"
2021-01-22 19:10:47 -05:00
#include "animqueue.hpp"
2021-02-07 17:26:48 -05:00
#include "gui.hpp"
#include "traceback.hpp"
2021-01-16 01:24:33 -05:00
#include <iostream>
LuaDefineType(World);
2021-01-17 16:23:10 -05:00
Tangible::Tangible() : world_(nullptr) {
}
World::~World() {
}
2021-01-16 01:24:33 -05:00
World::World() {
// Initialize the userdata metatables.
2021-02-09 17:15:54 -05:00
LuaStack::register_all_userdata(state());
2021-01-16 01:24:33 -05:00
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.
LuaVar world;
2021-02-09 17:15:54 -05:00
LuaStack LS(state(), world);
2021-01-16 01:24:33 -05:00
// Put the world pointer into the lua registry.
LS.newpointer(world, this, false);
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
2021-02-09 17:15:54 -05:00
source_db_.initialize(state());
2021-01-22 17:35:23 -05:00
source_db_.rebuild();
2021-01-23 16:31:16 -05:00
LS.result();
2021-02-09 17:15:54 -05:00
assert (lua_gettop(state()) == 0);
2021-01-22 17:35:23 -05:00
}
void Tangible::be_a_player() {
if (id_player_pool_ == nullptr) {
id_player_pool_.reset(new IdPlayerPool(&world_->id_global_pool_));
2021-02-02 16:29:07 -05:00
anim_queue_.add(world_->id_global_pool_.get_one(), "");
anim_queue_.set_graphic("player");
2021-02-09 17:15:54 -05:00
LuaVar classtab, mt, place, tangibles;
LuaStack LS(world_->state(), classtab, mt, place, tangibles);
LS.call(classtab, source_makeclass, "player");
LS.getfield(tangibles, LuaRegistry, "tangibles");
LS.rawget(place, tangibles, anim_queue_.get_id());
LS.getmetatable(mt, place);
LS.setfield(mt, "__index", classtab);
LS.result();
2021-01-22 17:35:23 -05:00
}
}
2021-01-16 01:24:33 -05:00
2021-01-22 17:35:23 -05:00
void World::init_standalone() {
// Load the lua source from disk then rebuild the environment.
2021-01-16 01:24:33 -05:00
source_db_.update();
source_db_.rebuild();
// Run unit tests.
source_db_.run_unittests();
2021-01-22 17:35:23 -05:00
// Create the player tangible.
2021-02-09 17:15:54 -05:00
Tangible *player = tangible_make(state(), 1, false);
2021-01-22 17:35:23 -05:00
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;
}
}
2021-02-07 13:38:29 -05:00
std::vector<int64_t> World::get_near(int64_t player_id, float radius) {
Tangible *player = tangible_get(player_id);
2021-01-22 19:10:47 -05:00
2021-02-07 13:38:29 -05:00
// Find out where's the center of the world.
std::string plane = player->anim_queue_.get_plane();
util::XYZ xyz = player->anim_queue_.get_xyz();
return plane_map_.scan_radius(plane, xyz.x, xyz.y, radius, player_id);
2021-01-22 19:10:47 -05:00
}
2021-01-22 17:35:23 -05:00
Tangible *World::tangible_make(lua_State *L, int64_t id, bool pushdb) {
2021-01-17 16:23:10 -05:00
LuaVar tangibles, metatab;
LuaRet database;
LuaStack LS(L, tangibles, database, metatab);
2021-01-22 17:35:23 -05:00
// Allocate an ID if we don't already have one.
if (id == 0) id = id_global_pool_.alloc_id_for_thread(L);
2021-01-17 16:23:10 -05:00
// Create the C++ part of the structure.
Tangible *t = &tangibles_[id];
assert(t->world_ == nullptr);
t->world_ = this;
2021-01-22 17:35:23 -05:00
t->plane_item_.set_id(id);
2021-01-22 19:10:47 -05:00
t->anim_queue_.set_id(id);
2021-01-17 16:23:10 -05:00
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();
2021-01-22 17:35:23 -05:00
if (!pushdb) lua_pop(L, 1);
2021-01-17 16:23:10 -05:00
return t;
}
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-02-07 17:26:48 -05:00
void World::update_gui(int64_t actor_id, int64_t place_id, Gui *gui) {
gui->clear();
2021-02-09 17:15:54 -05:00
lua_State *L = state();
2021-02-07 17:26:48 -05:00
LuaVar actor, place, ugui, func, tangibles;
LuaStack LS(L, actor, place, ugui, func, tangibles);
// Get the actor and place.
LS.getfield(tangibles, LuaRegistry, "tangibles");
LS.rawget(actor, tangibles, actor_id);
LS.rawget(place, tangibles, place_id);
if (!LS.istable(actor) || !LS.istable(place)) {
LS.result();
return;
}
// Get the interface closure.
LS.getfield(func, place, "interface");
if (!LS.isfunction(func)) {
LS.result();
return;
}
// Construct the userdata with the GUI pointer.
LS.newpointer<Gui>(ugui, gui, false);
// Call the interface function.
lua_pushvalue(L, func.index());
lua_pushvalue(L, actor.index());
lua_pushvalue(L, place.index());
lua_pushvalue(L, ugui.index());
int status = traceback_pcall(L, 3, 0);
if (status != 0) {
gui->clear();
std::cerr << lua_tostring(L, -1);
LS.result();
return;
}
// Nuke the userdata, in case somebody saved a pointer to it.
LS.clearuserdata(ugui);
// And we're done.
LS.result();
}
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") {
2021-01-22 17:35:23 -05:00
World::fetch(L)->tangible_make(L, 0, true);
2021-01-17 16:23:10 -05:00
return 1;
}