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

530 lines
15 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>
void World::store_global_pointer(lua_State *L, World *v) {
lua_pushstring(L, "world");
lua_pushlightuserdata(L, v);
lua_rawset(L, LUA_REGISTRYINDEX);
}
World *World::fetch_global_pointer(lua_State *L) {
lua_pushstring(L, "world");
lua_rawget(L, LUA_REGISTRYINDEX);
World *result = (World *)lua_touserdata(L, -1);
if (result == nullptr) {
luaL_error(L, "No world pointer stored.");
}
lua_pop(L, 1);
return result;
2021-01-17 16:23:10 -05:00
}
World::~World() {
}
2021-02-25 14:09:16 -05:00
World::World() {
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.
2021-02-25 14:09:16 -05:00
World::store_global_pointer(state(), this);
// Clear the global GUI pointer.
Gui::store_global_pointer(state(), nullptr);
2021-01-16 01:24:33 -05:00
2021-01-17 16:23:10 -05:00
// Create the tangibles table in the registry.
2021-02-10 16:47:45 -05:00
LS.rawset(LuaRegistry, "tangibles", LuaNewTable);
2021-01-17 16:23:10 -05:00
2021-01-16 01:24:33 -05:00
// Initialize the SourceDB
2021-02-28 16:32:42 -05:00
source_db_.init(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
}
Tangible::Tangible(World *w, int64_t id) : world_(w), id_player_pool_(&w->id_global_pool_) {
plane_item_.set_id(id);
plane_item_.track(&w->plane_map_);
}
2021-02-25 15:43:05 -05:00
void Tangible::update_plane_item() {
util::XYZ xyz = anim_queue_.get_xyz();
std::string plane = anim_queue_.get_plane();
plane_item_.set_pos(plane, xyz.x, xyz.y, xyz.z);
}
void Tangible::serialize(StreamBuffer *sb) {
anim_queue_.serialize(sb);
id_player_pool_.serialize(sb);
}
void Tangible::deserialize(StreamBuffer *sb) {
anim_queue_.deserialize(sb);
id_player_pool_.deserialize(sb);
update_plane_item();
}
2021-01-22 17:35:23 -05:00
void Tangible::be_a_player() {
if (!id_player_pool_.fifo_enabled()) {
id_player_pool_.enable_fifo();
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);
2021-02-10 16:22:24 -05:00
LS.makeclass(classtab, "player");
2021-02-10 16:47:45 -05:00
LS.rawget(tangibles, LuaRegistry, "tangibles");
LS.rawget(place, tangibles, id());
2021-02-09 17:15:54 -05:00
LS.getmetatable(mt, place);
2021-02-10 16:47:45 -05:00
LS.rawset(mt, "__index", classtab);
2021-02-09 17:15:54 -05:00
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.get();
2021-01-22 17:35:23 -05:00
}
}
2021-02-25 15:43:05 -05:00
Tangible *World::tangible_get(lua_State *L, int idx) {
Tangible *result = nullptr;
int top = lua_gettop(L);
if (lua_istable(L, idx)) {
lua_getmetatable(L, idx);
if (lua_istable(L, -1)) {
lua_pushstring(L, "id");
lua_rawget(L, -2);
lua_Number id = lua_tonumber(L, -1);
result = tangible_get(int64_t(id));
}
}
lua_settop(L, top);
if (result == nullptr) {
luaL_error(L, "parameter is not a tangible");
}
return result;
}
void World::tangible_delete(lua_State *L, int64_t id) {
LuaVar tangibles, database;
LuaStack LS(L, tangibles, database);
// Fetch the C++ side of the tangible.
auto iter = tangibles_.find(id);
if (iter == tangibles_.end()) {
luaL_error(L, "Not a tangible: %lld", id);
}
// Fetch the lua side of the tangible.
LS.rawget(tangibles, LuaRegistry, "tangibles");
LS.rawget(database, tangibles, id);
assert(LS.istable(database));
// Clear out the database.
LS.clearmetatable(database);
LS.cleartable(database);
// Remove the lua portion from the table.
LS.rawset(tangibles, id, LuaNil);
// Remove the C++ portion from the table.
tangibles_.erase(iter);
LS.result();
}
std::vector<int64_t> World::get_near(int64_t player_id, float radius, bool exclude_nowhere) {
2021-02-07 13:38:29 -05:00
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();
if (exclude_nowhere && (plane == "nowhere")) {
return std::vector<int64_t>();
}
2021-02-07 13:38:29 -05:00
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.
std::unique_ptr<Tangible> &t = tangibles_[id];
assert (t == nullptr);
t.reset(new Tangible(this, id));
2021-01-17 16:23:10 -05:00
// 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.
2021-02-10 16:47:45 -05:00
LS.rawget(tangibles, LuaRegistry, "tangibles");
2021-01-17 16:23:10 -05:00
LS.rawset(tangibles, id, database);
// Populate the database and metatable with initial stuff.
2021-02-10 16:47:45 -05:00
LS.rawset(database, "inventory", LuaNewTable);
LS.rawset(metatab, "id", id);
2021-02-17 13:38:22 -05:00
LS.rawset(metatab, "threads", LuaNewTable);
2021-02-10 16:47:45 -05:00
// LS.rawset(metatab, "__metatable", LuaNil);
2021-01-17 16:23:10 -05:00
LS.result();
2021-01-22 17:35:23 -05:00
if (!pushdb) lua_pop(L, 1);
return t.get();
2021-01-17 16:23:10 -05:00
}
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
2021-02-16 13:31:34 -05:00
LuaVar actor, place, ugui, func, tangibles, mt, index;
LuaStack LS(L, actor, place, ugui, func, tangibles, mt, index);
2021-02-07 17:26:48 -05:00
// Get the actor and place.
2021-02-10 16:47:45 -05:00
LS.rawget(tangibles, LuaRegistry, "tangibles");
2021-02-07 17:26:48 -05:00
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.
2021-02-16 13:31:34 -05:00
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");
2021-02-07 17:26:48 -05:00
if (!LS.isfunction(func)) {
LS.result();
return;
}
// Call the interface function.
lua_pushvalue(L, func.index());
lua_pushvalue(L, actor.index());
lua_pushvalue(L, place.index());
2021-02-25 14:09:16 -05:00
Gui::store_global_pointer(L, gui);
int status = traceback_pcall(L, 2, 0);
Gui::store_global_pointer(L, nullptr);
2021-02-07 17:26:48 -05:00
if (status != 0) {
gui->clear();
std::cerr << lua_tostring(L, -1);
LS.result();
return;
}
// And we're done.
LS.result();
}
2021-02-16 13:31:34 -05:00
2021-02-25 14:09:16 -05:00
void World::invoke_plan(int64_t actor_id, int64_t place_id, const std::string &action, const GuiResult &gres) {
2021-02-16 14:15:40 -05:00
// Validate that the action is legal.
Gui validation_gui;
update_gui(actor_id, place_id, &validation_gui);
if (!validation_gui.has_action(action)) {
return;
}
2021-02-18 14:56:12 -05:00
// 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();
2021-02-18 14:56:12 -05:00
int64_t tid = id_batch++;
2021-02-16 13:31:34 -05:00
2021-02-18 14:56:12 -05:00
// Set up for Lua manipulation.
lua_State *L = state();
2021-02-25 14:09:16 -05:00
LuaVar actor, place, func, tangibles, mt, index, actions, thread, threads, message, guires;
LuaStack LS(L, actor, place, func, tangibles, mt, index, actions, thread, threads, message, guires);
2021-02-16 13:31:34 -05:00
// 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;
}
2021-02-25 14:09:16 -05:00
// Convert the GuiResult into a lua table.
LS.newtable(guires);
for (const auto &p : gres) {
LS.rawset(guires, p.first, p.second);
}
2021-02-17 13:38:22 -05:00
// Create a new thread, set up function and parameters.
lua_State *CO = LS.newthread(thread);
2021-02-16 13:31:34 -05:00
lua_pushvalue(L, func.index());
lua_pushvalue(L, actor.index());
lua_pushvalue(L, place.index());
2021-02-25 14:09:16 -05:00
lua_pushvalue(L, guires.index());
2021-02-17 13:38:22 -05:00
lua_xmove(L, CO, 4);
2021-02-18 14:56:12 -05:00
// Store the thread into place's thread table.
2021-02-18 16:19:43 -05:00
LS.rawget(threads, mt, "threads");
if (!LS.istable(threads)) {
2021-02-18 14:56:12 -05:00
LS.result();
return;
}
2021-02-18 16:19:43 -05:00
LS.rawset(threads, tid, thread);
2021-02-18 14:56:12 -05:00
LS.result();
2021-02-18 14:56:12 -05:00
// Push the thread's ID into the runnable thread queue,
// then run the thread queue.
2021-02-18 17:21:25 -05:00
thread_sched_.add(0, tid, place_id);
run_scheduled_threads(0);
2021-02-18 14:56:12 -05:00
}
2021-02-18 17:21:25 -05:00
void World::run_scheduled_threads(int64_t clk) {
2021-02-18 14:56:12 -05:00
lua_State *L = state();
2021-02-18 16:19:43 -05:00
LuaVar tangibles, place, mt, threads, thread;
LuaStack LS(L, tangibles, place, mt, threads, thread);
2021-02-18 14:56:12 -05:00
LS.rawget(tangibles, LuaRegistry, "tangibles");
2021-02-18 17:21:25 -05:00
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 top = lua_gettop(CO);
2021-02-28 15:05:45 -05:00
int status = lua_resume(CO, nullptr, (top > 0) ? (top - 1) : 0);
2021-02-18 17:21:25 -05:00
// Three possible outcomes: finished, yielded, or errored.
if (status == LUA_YIELD) {
// When the wait statement yields, it yields the desired timestamp.
if ((lua_gettop(CO) != 1) || (!lua_isnumber(CO, 1))) {
std::cerr << "Thread yielded incorrectly. Killing it." << std::endl;
LS.rawset(threads, sched.thread_id(), LuaNil);
} else {
lua_Number delay = lua_tonumber(CO, 1);
lua_settop(CO, 0);
std::cerr << "Thread wait = " << delay << std::endl;
thread_sched_.add(sched.clock() + int64_t(delay), sched.thread_id(), sched.place_id());
std::cerr << "Added to schedule." << std::endl;
}
2021-02-18 17:21:25 -05:00
} 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 {
// Generated an error. Add a traceback, print, and kill the coroutine.
traceback_coroutine(CO);
std::cerr << lua_tostring(CO, -1);
2021-02-18 17:21:25 -05:00
LS.rawset(threads, sched.thread_id(), LuaNil);
}
2021-02-16 13:31:34 -05:00
}
LS.result();
}
void World::serialize(StreamBuffer *sb) {
int64_t wc0 = sb->write_count();
lua_snap_.serialize(sb);
id_global_pool_.serialize(sb);
thread_sched_.serialize(sb);
sb->write_size(tangibles_.size());
for (const auto &p : tangibles_) {
sb->write_int64(p.first);
p.second->serialize(sb);
}
int64_t wc1 = sb->write_count();
std::cerr << "World serialized to " << wc1-wc0 << " bytes." << std::endl;
}
void World::deserialize(StreamBuffer *sb) {
lua_snap_.deserialize(sb);
id_global_pool_.deserialize(sb);
thread_sched_.deserialize(sb);
// Mark all tangibles for deletion by setting ID to zero.
for (const auto &p : tangibles_) {
p.second->plane_item_.set_id(0);
}
// Deserialize tangibles.
size_t ntan = sb->read_size();
for (size_t i = 0; i < ntan; i++) {
int64_t id = sb->read_int64();
std::unique_ptr<Tangible> &t = tangibles_[id];
if (t == nullptr) {
t.reset(new Tangible(this, id));
} else {
t->plane_item_.set_id(id);
}
t->deserialize(sb);
}
// Delete tangibles that didn't get deserialized.
for (auto iter = tangibles_.begin(); iter != tangibles_.end(); ) {
if (iter->second->plane_item_.id() == 0) {
tangibles_.erase(iter++);
} else {
++iter;
}
}
}
void World::snapshot() {
snapshot_.clear();
serialize(&snapshot_);
}
void World::rollback() {
assert(!snapshot_.at_eof());
deserialize(&snapshot_);
}
2021-02-25 15:43:05 -05:00
LuaDefine(tangible_xyz, "c") {
LuaArg tanobj;
LuaRet X, Y, Z;
LuaStack LS(L, tanobj, X, Y, Z);
World *w = World::fetch_global_pointer(L);
Tangible *tan = w->tangible_get(L, tanobj.index());
util::XYZ xyz = tan->anim_queue_.get_xyz();
LS.set(X, xyz.x);
LS.set(Y, xyz.y);
LS.set(Z, xyz.z);
return LS.result();
}
2021-01-17 16:23:10 -05:00
2021-02-25 16:32:48 -05:00
LuaDefine(tangible_plane, "c") {
LuaArg tanobj;
LuaRet plane;
LuaStack LS(L, tanobj, plane);
World *w = World::fetch_global_pointer(L);
Tangible *tan = w->tangible_get(L, tanobj.index());
LS.set(plane, tan->anim_queue_.get_plane());
return LS.result();
}
LuaDefine(tangible_graphic, "c") {
LuaArg tanobj;
LuaRet graphic;
LuaStack LS(L, tanobj, graphic);
World *w = World::fetch_global_pointer(L);
Tangible *tan = w->tangible_get(L, tanobj.index());
LS.set(graphic, tan->anim_queue_.get_graphic());
return LS.result();
}
2021-02-25 16:32:48 -05:00
LuaDefine(tangible_animate, "c") {
LuaArg tanobj, config;
LuaStack LS(L, tanobj, config);
2021-02-25 15:43:05 -05:00
World *w = World::fetch_global_pointer(L);
Tangible *tan = w->tangible_get(L, tanobj.index());
int64_t id = w->id_global_pool_.alloc_id_for_thread(L);
2021-02-25 16:32:48 -05:00
tan->anim_queue_.add(id, L, config.index());
2021-02-25 15:43:05 -05:00
tan->update_plane_item();
2021-01-17 16:23:10 -05:00
return LS.result();
}
LuaDefine(tangible_setclass, "c") {
LuaArg tanobj, classname;
LuaVar classtab, mt;
LuaStack LS(L, tanobj, classname, classtab, mt);
World *w = World::fetch_global_pointer(L);
w->tangible_get(L, tanobj.index());
LS.getclass(classtab, classname);
LS.getmetatable(mt, tanobj);
LS.rawset(mt, "__index", classtab);
return LS.result();
}
LuaDefine(tangible_delete, "c") {
LuaArg tanobj;
LuaStack LS(L, tanobj);
World *w = World::fetch_global_pointer(L);
Tangible *tan = w->tangible_get(L, tanobj.index());
w->tangible_delete(L, tan->id());
return LS.result();
}
2021-01-17 16:23:10 -05:00
LuaDefine(tangible_make, "c") {
2021-02-25 14:09:16 -05:00
World::fetch_global_pointer(L)->tangible_make(L, 0, true);
2021-01-17 16:23:10 -05:00
return 1;
}
LuaDefine(world_wait, "f") {
2021-02-25 14:09:16 -05:00
if ((lua_gettop(L) != 1) || (lua_type(L, -1) != LUA_TNUMBER)) {
luaL_error(L, "Argument to wait must be a number.");
}
return lua_yield(L, 1);
}
2021-02-25 14:09:16 -05:00
LuaDefine(world_getregistry, "f") {
lua_pushvalue(L, LUA_REGISTRYINDEX);
return 1;
}