Files
integration/luprex/core/cpp/world.cpp

333 lines
9.4 KiB
C++

#include "world.hpp"
#include "idalloc.hpp"
#include "animqueue.hpp"
#include "gui.hpp"
#include "traceback.hpp"
#include <iostream>
LuaDefineType(World);
Tangible::Tangible() : world_(nullptr) {
}
World::~World() {
}
World::World() {
// Initialize the userdata metatables.
LuaStack::register_all_userdata(state());
// Initialize the ID allocator in master mode.
id_global_pool_.init_master(10);
// Prepare to manipulate the lua state.
LuaVar world;
LuaStack LS(state(), world);
// Put the world pointer into the lua registry.
LS.newpointer(world, this, false);
LS.rawset(LuaRegistry, "world", world);
// Create the tangibles table in the registry.
LS.rawset(LuaRegistry, "tangibles", LuaNewTable);
// Initialize the SourceDB
source_db_.initialize(state());
source_db_.rebuild();
LS.result();
assert (lua_gettop(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");
LuaVar classtab, mt, place, tangibles;
LuaStack LS(world_->state(), classtab, mt, place, tangibles);
LS.makeclass(classtab, "player");
LS.rawget(tangibles, LuaRegistry, "tangibles");
LS.rawget(place, tangibles, anim_queue_.get_id());
LS.getmetatable(mt, place);
LS.rawset(mt, "__index", classtab);
LS.result();
}
}
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(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;
}
}
std::vector<int64_t> World::get_near(int64_t player_id, float radius) {
Tangible *player = tangible_get(player_id);
// 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);
}
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.rawget(tangibles, LuaRegistry, "tangibles");
LS.rawset(tangibles, id, database);
// Populate the database and metatable with initial stuff.
LS.rawset(database, "inventory", LuaNewTable);
LS.rawset(database, "id", id);
LS.rawset(metatab, "id", id);
LS.rawset(metatab, "threads", LuaNewTable);
// LS.rawset(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.rawget(world, LuaRegistry, "world");
World *w = LS.ckuserdata<World>(world);
LS.result();
return w;
}
void World::update_gui(int64_t actor_id, int64_t place_id, Gui *gui) {
gui->clear();
lua_State *L = state();
LuaVar actor, place, ugui, func, tangibles, mt, index;
LuaStack LS(L, actor, place, ugui, func, tangibles, mt, index);
// Get the actor and place.
LS.rawget(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.getmetatable(mt, place);
if (!LS.istable(mt)) {
LS.result();
return;
}
LS.rawget(index, mt, "__index");
if (!LS.istable(index)) {
LS.result();
return;
}
LS.rawget(func, index, "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();
}
void World::invoke_plan(int64_t actor_id, int64_t place_id, const std::string &action) {
// Validate that the action is legal.
Gui validation_gui;
update_gui(actor_id, place_id, &validation_gui);
if (!validation_gui.has_action(action)) {
return;
}
// Get an ID batch for the thread, and take one for the thread itself.
Tangible *tactor = tangible_get(actor_id);
int64_t id_batch = tactor->id_player_pool_->get_batch();
int64_t tid = id_batch++;
// Set up for Lua manipulation.
lua_State *L = state();
LuaVar actor, place, func, tangibles, mt, index, actions, thread, threads, message;
LuaStack LS(L, actor, place, func, tangibles, mt, index, actions, thread, threads, message);
// Get the actor and place.
LS.rawget(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 action closure.
LS.getmetatable(mt, place);
if (!LS.istable(mt)) {
LS.result();
return;
}
LS.rawget(index, mt, "__index");
if (!LS.istable(index)) {
LS.result();
return;
}
LS.rawget(actions, index, "action");
if (!LS.istable(actions)) {
LS.result();
return;
}
LS.rawget(func, actions, action);
if (!LS.isfunction(func)) {
LS.result();
return;
}
// Create a new thread, set up function and parameters.
lua_State *CO = LS.newthread(thread);
lua_pushvalue(L, func.index());
lua_pushvalue(L, actor.index());
lua_pushvalue(L, place.index());
lua_pushnil(L); // Gui state not implemented yet.
lua_xmove(L, CO, 4);
// Store the thread into place's thread table.
LS.rawget(threads, mt, "threads");
if (!LS.istable(threads)) {
LS.result();
return;
}
LS.rawset(threads, tid, thread);
LS.result();
// Push the thread's ID into the runnable thread queue,
// then run the thread queue.
thread_sched_.add(0, tid, place_id);
run_scheduled_threads(0);
}
void World::run_scheduled_threads(int64_t clk) {
lua_State *L = state();
LuaVar tangibles, place, mt, threads, thread;
LuaStack LS(L, tangibles, place, mt, threads, thread);
LS.rawget(tangibles, LuaRegistry, "tangibles");
while (thread_sched_.ready(clk)) {
SchedEntry sched = thread_sched_.pop();
LS.rawget(place, tangibles, sched.place_id());
if (!LS.istable(place)) {
continue;
}
LS.getmetatable(mt, place);
if (!LS.istable(mt)) {
continue;
}
LS.rawget(threads, mt, "threads");
if (!LS.istable(threads)) {
continue;
}
LS.rawget(thread, threads, sched.thread_id());
if (!LS.isthread(thread)) {
continue;
}
// Resume the coroutine.
lua_State *CO = LS.ckthread(thread);
int status = lua_resume(CO, 3);
// Three possible outcomes: finished, yielded, or errored.
if (status == LUA_YIELD) {
// When the wait statement yields, it yields the desired timestamp.
std::cerr << "Thread yield top = " << lua_gettop(CO) << std::endl;
// TODO: Insert the thread back into thread_sched_.
LS.rawset(threads, sched.thread_id(), LuaNil);
} else if (status == 0) {
// Successfully ran to completion. Remove from thread table.
std::cerr << "Thread ran to completion." << std::endl;
LS.rawset(threads, sched.thread_id(), LuaNil);
} else {
// Transfer the error message from CO to L, and add a traceback.
LS.rawset(threads, sched.thread_id(), LuaNil);
traceback_coroutine(L, CO);
std::cerr << lua_tostring(L, -1);
}
}
LS.result();
}
LuaDefine(tangible_get, "c") {
LuaArg id;
LuaRet database;
LuaVar tangibles;
LuaStack LS(L, id, database, tangibles);
LS.rawget(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;
}