2021-09-10 17:06:07 -04:00
|
|
|
|
|
|
|
|
#include "world.hpp"
|
|
|
|
|
#include "idalloc.hpp"
|
|
|
|
|
#include "animqueue.hpp"
|
|
|
|
|
#include "gui.hpp"
|
|
|
|
|
#include "traceback.hpp"
|
2021-10-21 13:15:04 -04:00
|
|
|
#include "pprint.hpp"
|
2021-09-10 17:06:07 -04: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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
World::~World() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
World::World(util::WorldType wt) {
|
|
|
|
|
// Master world model by default.
|
|
|
|
|
world_type_ = wt;
|
|
|
|
|
|
|
|
|
|
// Initialize the ID allocator in master mode.
|
|
|
|
|
if (wt == util::WORLD_TYPE_MASTER || wt == util::WORLD_TYPE_STANDALONE) {
|
|
|
|
|
id_global_pool_.init_master();
|
|
|
|
|
} else {
|
|
|
|
|
id_global_pool_.init_synch();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Prepare to manipulate the lua state.
|
|
|
|
|
LuaVar world, globtab;
|
|
|
|
|
LuaStack LS(state(), world, globtab);
|
|
|
|
|
|
|
|
|
|
// Put the world pointer into the lua registry.
|
|
|
|
|
World::store_global_pointer(state(), this);
|
|
|
|
|
|
|
|
|
|
// Clear the global GUI pointer.
|
|
|
|
|
Gui::store_global_pointer(state(), nullptr);
|
|
|
|
|
|
2021-10-14 11:43:16 -04:00
|
|
|
// Clear the lthread state.
|
2021-10-21 13:15:04 -04:00
|
|
|
clear_lthread_state();
|
2021-10-14 11:43:16 -04:00
|
|
|
|
2021-09-10 17:06:07 -04:00
|
|
|
// Set the tabletype of the registry.
|
|
|
|
|
LS.settabletype(LuaRegistry, LUA_TT_REGISTRY);
|
|
|
|
|
|
|
|
|
|
// Set the tabletype of the global environment.
|
|
|
|
|
LS.getglobaltable(globtab);
|
|
|
|
|
LS.settabletype(globtab, LUA_TT_GLOBALENV);
|
|
|
|
|
|
|
|
|
|
// Create the tangibles table in the registry.
|
|
|
|
|
LS.rawset(LuaRegistry, "tangibles", LuaNewTable);
|
|
|
|
|
|
2021-12-21 15:22:26 -05:00
|
|
|
// Create the globaldb and oncedb in the registry.
|
2021-12-21 13:00:11 -05:00
|
|
|
if (util::world_type_authoritative(wt)) {
|
|
|
|
|
LS.rawset(LuaRegistry, "globaldb", LuaNewTable);
|
2021-12-21 15:22:26 -05:00
|
|
|
LS.rawset(LuaRegistry, "oncedb", LuaNewTable);
|
2021-12-21 13:00:11 -05:00
|
|
|
}
|
2021-09-10 17:06:07 -04:00
|
|
|
|
|
|
|
|
// Initialize the SourceDB. At this stage, the sourcedb is
|
|
|
|
|
// empty, so it's just populating the lua builtins.
|
|
|
|
|
source_db_.init(state());
|
2022-02-24 02:17:41 -05:00
|
|
|
eng::string srcerrs = source_db_.rebuild();
|
2021-10-25 14:47:37 -04:00
|
|
|
|
2021-11-26 15:45:36 -05:00
|
|
|
// Clear the clock.
|
|
|
|
|
clock_ = 0;
|
|
|
|
|
|
2021-10-25 14:47:37 -04:00
|
|
|
// There shouldn't be any lua errors in the sourceDB at this
|
|
|
|
|
// point, since there's no lua code in the sourceDB.
|
|
|
|
|
assert(srcerrs == "");
|
2021-09-10 17:06:07 -04:00
|
|
|
|
|
|
|
|
LS.result();
|
|
|
|
|
assert (stack_is_clear());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Tangible::Tangible(World *w, int64_t id) : world_(w), anim_queue_(w->world_type_), id_player_pool_(&w->id_global_pool_) {
|
|
|
|
|
plane_item_.set_id(id);
|
|
|
|
|
plane_item_.track(&w->plane_map_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Tangible::update_plane_item() {
|
|
|
|
|
const AnimStep &aqback = anim_queue_.back();
|
|
|
|
|
plane_item_.set_pos(aqback.plane(), aqback.xyz().x, aqback.xyz().y, aqback.xyz().z);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Tangible::serialize(StreamBuffer *sb) {
|
|
|
|
|
anim_queue_.serialize(sb);
|
|
|
|
|
id_player_pool_.serialize(sb);
|
2021-11-14 15:57:18 -05:00
|
|
|
print_buffer_.serialize(sb);
|
2021-09-10 17:06:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Tangible::deserialize(StreamBuffer *sb) {
|
|
|
|
|
anim_queue_.deserialize(sb);
|
|
|
|
|
id_player_pool_.deserialize(sb);
|
2021-11-14 15:57:18 -05:00
|
|
|
print_buffer_.deserialize(sb);
|
2021-09-10 17:06:07 -04:00
|
|
|
update_plane_item();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Tangible *World::tangible_get(int64_t id) {
|
|
|
|
|
auto iter = tangibles_.find(id);
|
|
|
|
|
if (iter == tangibles_.end()) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
} else {
|
|
|
|
|
return iter->second.get();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Tangible *World::tangible_get(int64_t id) const {
|
|
|
|
|
auto iter = tangibles_.find(id);
|
|
|
|
|
if (iter == tangibles_.end()) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
} else {
|
|
|
|
|
return iter->second.get();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
World::TanVector World::tangible_get_all(const IdVector &ids) const {
|
|
|
|
|
TanVector result(ids.size());
|
|
|
|
|
for (int i = 0; i < int(ids.size()); i++) {
|
|
|
|
|
result[i] = tangible_get(ids[i]);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Tangible *World::tangible_get(const LuaStack &LS, LuaSlot tab) {
|
2021-11-26 12:28:59 -05:00
|
|
|
int64_t id = LS.tanid(tab);
|
2021-09-10 17:06:07 -04:00
|
|
|
if (id == 0) {
|
|
|
|
|
luaL_error(LS.state(), "parameter is not a tangible");
|
|
|
|
|
}
|
|
|
|
|
Tangible *result = tangible_get(id);
|
|
|
|
|
if (result == nullptr) {
|
|
|
|
|
luaL_error(LS.state(), "parameter is not a tangible");
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 02:17:41 -05:00
|
|
|
Tangible *World::tangible_make(lua_State *L, int64_t id, const eng::string &plane, bool pushdb) {
|
2021-09-10 17:06:07 -04:00
|
|
|
// Get a state if we don't already have one.
|
|
|
|
|
if (L == nullptr) {
|
|
|
|
|
L = state();
|
|
|
|
|
assert(!pushdb);
|
|
|
|
|
}
|
2021-10-14 11:43:16 -04:00
|
|
|
assert(id != 0);
|
2021-09-10 17:06:07 -04:00
|
|
|
|
|
|
|
|
LuaVar tangibles, metatab;
|
|
|
|
|
LuaRet database;
|
|
|
|
|
LuaStack LS(L, tangibles, database, metatab);
|
|
|
|
|
|
|
|
|
|
// Create the C++ part of the structure.
|
2021-10-15 13:51:32 -04:00
|
|
|
UniqueTangible &t = tangibles_[id];
|
2021-09-10 17:06:07 -04:00
|
|
|
assert (t == nullptr);
|
|
|
|
|
t.reset(new Tangible(this, id));
|
|
|
|
|
|
|
|
|
|
// Set up initial animation state.
|
|
|
|
|
t->anim_queue_.clear(plane);
|
|
|
|
|
t->update_plane_item();
|
|
|
|
|
|
|
|
|
|
// Create the tangible's database and metatable.
|
|
|
|
|
LS.set(database, LuaNewTable);
|
|
|
|
|
LS.set(metatab, LuaNewTable);
|
|
|
|
|
LS.setmetatable(database, metatab);
|
|
|
|
|
|
|
|
|
|
// Mark the tangible using the tabletype field.
|
|
|
|
|
LS.settabletype(database, LUA_TT_TANGIBLE);
|
|
|
|
|
LS.settabletype(metatab, LUA_TT_TANGIBLEMETA);
|
|
|
|
|
|
|
|
|
|
// 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(metatab, "id", id);
|
|
|
|
|
LS.rawset(metatab, "threads", LuaNewTable);
|
|
|
|
|
// LS.rawset(metatab, "__metatable", LuaNil);
|
|
|
|
|
|
|
|
|
|
LS.result();
|
|
|
|
|
if (!pushdb) lua_pop(L, 1);
|
|
|
|
|
return t.get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void World::tangible_delete(int64_t id) {
|
|
|
|
|
lua_State *L = state();
|
|
|
|
|
LuaVar tangibles, database;
|
|
|
|
|
LuaStack LS(L, tangibles, database);
|
|
|
|
|
|
|
|
|
|
// Fetch the C++ side of the tangible.
|
|
|
|
|
auto iter = tangibles_.find(id);
|
|
|
|
|
if (iter == tangibles_.end()) {
|
|
|
|
|
LS.result();
|
|
|
|
|
return; // Nothing to delete.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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.
|
2021-10-25 14:47:37 -04:00
|
|
|
LS.cleartable(database, true);
|
2021-09-10 17:06:07 -04:00
|
|
|
LS.settabletype(database, LUA_TT_DEADTANGIBLE);
|
|
|
|
|
|
|
|
|
|
// Remove the lua portion from the tangibles table.
|
|
|
|
|
LS.rawset(tangibles, id, LuaNil);
|
|
|
|
|
|
|
|
|
|
// Remove the C++ portion from the tangibles table.
|
|
|
|
|
tangibles_.erase(iter);
|
|
|
|
|
LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-23 16:10:48 -05:00
|
|
|
util::IdVector World::get_near_unsorted(int64_t player_id, float radius, bool exclude_nowhere, bool omit_player) const {
|
2021-09-10 17:06:07 -04:00
|
|
|
const Tangible *player = tangible_get(player_id);
|
|
|
|
|
if (player == nullptr) {
|
|
|
|
|
return IdVector();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find out where's the center of the world.
|
|
|
|
|
const AnimStep &aqback = player->anim_queue_.back();
|
2021-11-23 16:10:48 -05:00
|
|
|
return plane_map_.scan_radius_unsorted(aqback.plane(), aqback.xyz().x, aqback.xyz().y, radius, exclude_nowhere, player_id, omit_player);
|
2021-09-10 17:06:07 -04:00
|
|
|
}
|
|
|
|
|
|
2021-11-23 16:10:48 -05:00
|
|
|
util::IdVector World::get_near(int64_t player_id, float radius, bool exclude_nowhere, bool omit_player) const {
|
|
|
|
|
const Tangible *player = tangible_get(player_id);
|
|
|
|
|
if (player == nullptr) {
|
|
|
|
|
return IdVector();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find out where's the center of the world.
|
|
|
|
|
const AnimStep &aqback = player->anim_queue_.back();
|
|
|
|
|
return plane_map_.scan_radius(aqback.plane(), aqback.xyz().x, aqback.xyz().y, radius, exclude_nowhere, player_id, omit_player);
|
2021-09-10 17:06:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
World::Redirects World::fetch_redirects() {
|
|
|
|
|
World::Redirects result = std::move(redirects_);
|
|
|
|
|
redirects_.clear();
|
2021-11-23 14:38:08 -05:00
|
|
|
return result;
|
2021-09-10 17:06:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int64_t World::create_login_actor() {
|
2021-10-25 14:47:37 -04:00
|
|
|
assert(stack_is_clear());
|
2021-10-14 11:43:16 -04:00
|
|
|
int64_t id = id_global_pool_.get_one();
|
|
|
|
|
Tangible *tan = tangible_make(state(), id, "nowhere", true);
|
2021-09-10 17:06:07 -04:00
|
|
|
LuaArg database;
|
|
|
|
|
LuaVar classtab, mt;
|
|
|
|
|
LuaStack LS(state(), database, classtab, mt);
|
|
|
|
|
LS.makeclass(classtab, "login");
|
|
|
|
|
LS.getmetatable(mt, database);
|
|
|
|
|
LS.rawset(mt, "__index", classtab);
|
|
|
|
|
LS.result();
|
|
|
|
|
tan->configure_id_pool_for_actor();
|
2021-11-14 15:57:18 -05:00
|
|
|
tan->print_buffer_.clear();
|
2021-09-10 17:06:07 -04:00
|
|
|
assert(stack_is_clear());
|
|
|
|
|
return tan->id();
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 02:17:41 -05:00
|
|
|
eng::string World::probe_lua(int64_t actor_id, const eng::string &lua) {
|
2021-11-26 13:56:24 -05:00
|
|
|
assert(stack_is_clear());
|
|
|
|
|
lua_State *L = state();
|
|
|
|
|
|
|
|
|
|
Tangible *actor = tangible_get(actor_id);
|
|
|
|
|
if (actor == nullptr) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaVar closure;
|
|
|
|
|
LuaStack LS(L, closure);
|
|
|
|
|
|
|
|
|
|
// create the compiled closure.
|
|
|
|
|
int status = luaL_loadbuffer(L, lua.c_str(), lua.size(), "=probe");
|
|
|
|
|
lua_replace(L, closure.index());
|
|
|
|
|
if (status != LUA_OK) {
|
|
|
|
|
// The closure is actually an error message. Do nothing.
|
|
|
|
|
// This should normally not happen: LuaConsole should filter
|
|
|
|
|
// out syntax errors.
|
|
|
|
|
LS.result();
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Call the closure.
|
|
|
|
|
int top = lua_gettop(L);
|
|
|
|
|
lua_pushvalue(L, closure.index());
|
|
|
|
|
open_lthread_state(actor_id, actor_id, false, true);
|
|
|
|
|
status = traceback_pcall(L, 0, LUA_MULTRET);
|
|
|
|
|
|
|
|
|
|
// If there's an error message, print it.
|
|
|
|
|
// Otherwise, pretty-print the results.
|
2022-02-25 19:57:23 -05:00
|
|
|
std::ostream *ostream = lthread_print_stream();
|
2021-11-26 13:56:24 -05:00
|
|
|
if (status == LUA_OK) {
|
|
|
|
|
for (int i = top + 1; i <= lua_gettop(L); i++) {
|
|
|
|
|
LuaSpecial root(i);
|
|
|
|
|
pprint(LS, root, true, ostream);
|
|
|
|
|
(*ostream) << std::endl;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
const char *msg = lua_tostring(L, -1);
|
|
|
|
|
if (msg == NULL) {
|
|
|
|
|
msg = "(error object is not a string)";
|
|
|
|
|
}
|
|
|
|
|
(*ostream) << msg << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Collect the lthread_prints (and also make sure they
|
|
|
|
|
// don't go into the printbuffer).
|
2022-02-24 02:17:41 -05:00
|
|
|
eng::string result = lthread_prints_->str();
|
2021-11-26 13:56:24 -05:00
|
|
|
lthread_prints_.reset();
|
|
|
|
|
|
|
|
|
|
close_lthread_state();
|
|
|
|
|
|
|
|
|
|
// And we're done.
|
|
|
|
|
LS.result();
|
|
|
|
|
assert(stack_is_clear());
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-10 17:06:07 -04:00
|
|
|
void World::update_gui(int64_t actor_id, int64_t place_id, Gui *gui) {
|
|
|
|
|
assert(stack_is_clear());
|
2021-11-16 13:14:59 -05:00
|
|
|
gui->clear(place_id);
|
2021-09-10 17:06:07 -04:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Call the interface function.
|
|
|
|
|
lua_pushvalue(L, func.index());
|
|
|
|
|
lua_pushvalue(L, actor.index());
|
|
|
|
|
lua_pushvalue(L, place.index());
|
|
|
|
|
Gui::store_global_pointer(L, gui);
|
2021-10-21 13:15:04 -04:00
|
|
|
open_lthread_state(actor_id, place_id, false, false);
|
2021-09-10 17:06:07 -04:00
|
|
|
int status = traceback_pcall(L, 2, 0);
|
2021-10-21 13:15:04 -04:00
|
|
|
close_lthread_state();
|
2021-09-10 17:06:07 -04:00
|
|
|
Gui::store_global_pointer(L, nullptr);
|
|
|
|
|
if (status != 0) {
|
2021-11-16 13:14:59 -05:00
|
|
|
gui->clear(0);
|
2021-09-10 17:06:07 -04:00
|
|
|
std::cerr << lua_tostring(L, -1);
|
|
|
|
|
LS.result();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// And we're done.
|
|
|
|
|
LS.result();
|
|
|
|
|
assert(stack_is_clear());
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-30 12:39:09 -05:00
|
|
|
void World::update_source(const util::LuaSourcePtr &source) {
|
2021-10-15 14:47:12 -04:00
|
|
|
if (source != nullptr) {
|
2021-12-15 14:18:19 -05:00
|
|
|
update_source(*source);
|
2021-10-15 14:47:12 -04:00
|
|
|
}
|
2021-12-15 14:18:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void World::update_source(const util::LuaSourceVec &source) {
|
|
|
|
|
assert(stack_is_clear());
|
|
|
|
|
source_db_.update(source);
|
|
|
|
|
assert(stack_is_clear());
|
2022-02-24 02:17:41 -05:00
|
|
|
eng::string errs = source_db_.rebuild();
|
2021-12-15 14:18:19 -05:00
|
|
|
// I don't have a good place to send the error messages right
|
|
|
|
|
// now. The engine needs a catch-all place to send errors that
|
|
|
|
|
// occur at unexpected times.
|
|
|
|
|
std::cerr << errs;
|
2021-10-25 14:47:37 -04:00
|
|
|
assert(stack_is_clear());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void World::run_unittests() {
|
|
|
|
|
assert(stack_is_clear());
|
|
|
|
|
source_db_.run_unittests();
|
|
|
|
|
assert(stack_is_clear());
|
2021-10-15 14:47:12 -04:00
|
|
|
}
|
|
|
|
|
|
2021-09-10 17:06:07 -04:00
|
|
|
void World::invoke(const Invocation &inv) {
|
|
|
|
|
switch (inv.kind()) {
|
|
|
|
|
case Invocation::KIND_PLAN:
|
|
|
|
|
invoke_plan(inv.actor(), inv.place(), inv.action(), inv.data());
|
|
|
|
|
break;
|
2021-10-15 14:47:12 -04:00
|
|
|
case Invocation::KIND_LUA:
|
|
|
|
|
invoke_lua(inv.actor(), inv.place(), inv.action(), inv.data());
|
|
|
|
|
break;
|
2021-10-25 14:47:37 -04:00
|
|
|
case Invocation::KIND_FLUSH_PRINTS:
|
|
|
|
|
invoke_flush_prints(inv.actor(), inv.place(), inv.action(), inv.data());
|
|
|
|
|
break;
|
2021-11-26 15:45:36 -05:00
|
|
|
case Invocation::KIND_TICK:
|
|
|
|
|
invoke_tick(inv.actor(), inv.place(), inv.action(), inv.data());
|
2021-12-15 14:18:19 -05:00
|
|
|
break;
|
|
|
|
|
case Invocation::KIND_LUA_SOURCE:
|
|
|
|
|
invoke_lua_source(inv.actor(), inv.place(), inv.action(), inv.data());
|
|
|
|
|
break;
|
2021-09-10 17:06:07 -04:00
|
|
|
default:
|
|
|
|
|
// Do nothing. Standard behavior for any invalid command is to
|
|
|
|
|
// simply do nothing at all. Perhaps eventually we may add a flag
|
|
|
|
|
// to the world model to indicate that we've detected an invalid
|
|
|
|
|
// command, to allow us to close the connection to a client that
|
|
|
|
|
// is misbehaving.
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 02:17:41 -05:00
|
|
|
void World::invoke_flush_prints(int64_t actor_id, int64_t place_id, const eng::string &action, const InvocationData &data) {
|
2021-10-25 14:47:37 -04:00
|
|
|
assert(stack_is_clear());
|
|
|
|
|
// Check argument sanity.
|
|
|
|
|
if (actor_id != place_id) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
int line = util::strtoint(action, -1);
|
|
|
|
|
if (line < 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Tangible *tactor = tangible_get(actor_id);
|
|
|
|
|
if (tactor == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-11-14 15:57:18 -05:00
|
|
|
tactor->print_buffer_.discard_upto(line);
|
2021-10-25 14:47:37 -04:00
|
|
|
assert(stack_is_clear());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-11-30 12:39:09 -05:00
|
|
|
|
2022-02-24 02:17:41 -05:00
|
|
|
void World::invoke_lua(int64_t actor_id, int64_t place_id, const eng::string &action, const InvocationData &data) {
|
2021-10-15 14:47:12 -04:00
|
|
|
assert(stack_is_clear());
|
|
|
|
|
|
2021-11-30 12:39:09 -05:00
|
|
|
// Make sure that actor and place exist.
|
2021-10-15 14:47:12 -04:00
|
|
|
Tangible *tactor = tangible_get(actor_id);
|
2021-11-30 12:39:09 -05:00
|
|
|
Tangible *tplace = tangible_get(place_id);
|
|
|
|
|
if ((tactor == nullptr) || (tplace == nullptr)) {
|
2021-10-15 14:47:12 -04:00
|
|
|
return;
|
2021-10-05 12:54:37 -04:00
|
|
|
}
|
2021-10-15 14:47:12 -04:00
|
|
|
|
2021-11-30 12:39:09 -05:00
|
|
|
// Get a thread ID for the new thread.
|
|
|
|
|
int64_t tid = tplace->id_player_pool_.get_one();
|
|
|
|
|
|
|
|
|
|
// Set up for lua manipulation.
|
2021-10-15 14:47:12 -04:00
|
|
|
lua_State *L = state();
|
2021-11-30 12:39:09 -05:00
|
|
|
LuaVar func, tangibles, place, mt, thread, thinfo, threads;
|
|
|
|
|
LuaStack LS(L, func, tangibles, place, mt, thread, thinfo, threads);
|
2021-10-15 14:47:12 -04:00
|
|
|
|
|
|
|
|
// create the compiled closure.
|
|
|
|
|
int status = luaL_loadbuffer(L, action.c_str(), action.size(), "=invoke");
|
2021-11-30 12:39:09 -05:00
|
|
|
lua_replace(L, func.index());
|
2021-10-15 14:47:12 -04:00
|
|
|
if (status != LUA_OK) {
|
|
|
|
|
// The closure is actually an error message. Do nothing.
|
2021-11-26 13:56:24 -05:00
|
|
|
// This should normally not happen: LuaConsole should filter
|
|
|
|
|
// out syntax errors.
|
2021-10-15 14:47:12 -04:00
|
|
|
LS.result();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-30 12:39:09 -05:00
|
|
|
// Get the place.
|
|
|
|
|
LS.rawget(tangibles, LuaRegistry, "tangibles");
|
|
|
|
|
LS.rawget(place, tangibles, place_id);
|
|
|
|
|
if (!LS.istable(place)) {
|
|
|
|
|
LS.result();
|
|
|
|
|
return;
|
2021-10-15 14:47:12 -04:00
|
|
|
}
|
2021-10-21 13:15:04 -04:00
|
|
|
|
2021-11-30 12:39:09 -05:00
|
|
|
// Get the place's metatable.
|
|
|
|
|
LS.getmetatable(mt, place);
|
|
|
|
|
if (!LS.istable(mt)) {
|
|
|
|
|
LS.result();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a new thread, set up function and parameters.
|
|
|
|
|
lua_State *CO = LS.newthread(thread);
|
|
|
|
|
lua_pushvalue(L, func.index());
|
|
|
|
|
lua_xmove(L, CO, 1);
|
|
|
|
|
|
|
|
|
|
// Create the thread info table.
|
|
|
|
|
LS.newtable(thinfo);
|
|
|
|
|
LS.rawset(thinfo, "thread", thread);
|
|
|
|
|
LS.rawset(thinfo, "actorid", actor_id);
|
|
|
|
|
LS.rawset(thinfo, "nargs", 0);
|
|
|
|
|
LS.rawset(thinfo, "useppool", true);
|
2021-12-14 12:24:25 -05:00
|
|
|
LS.rawset(thinfo, "print", true);
|
2021-11-30 12:39:09 -05:00
|
|
|
|
|
|
|
|
// Store the thread into place's thread table.
|
|
|
|
|
LS.rawget(threads, mt, "threads");
|
|
|
|
|
if (!LS.istable(threads)) {
|
|
|
|
|
LS.result();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
LS.rawset(threads, tid, thinfo);
|
2021-10-15 14:47:12 -04:00
|
|
|
LS.result();
|
2021-11-30 12:39:09 -05:00
|
|
|
|
|
|
|
|
thread_sched_.add(0, tid, place_id);
|
|
|
|
|
run_scheduled_threads();
|
2021-10-15 14:47:12 -04:00
|
|
|
assert(stack_is_clear());
|
2021-10-05 12:54:37 -04:00
|
|
|
}
|
|
|
|
|
|
2022-02-24 02:17:41 -05:00
|
|
|
void World::invoke_plan(int64_t actor_id, int64_t place_id, const eng::string &action, const InvocationData &data) {
|
2021-09-10 17:06:07 -04:00
|
|
|
assert(stack_is_clear());
|
|
|
|
|
|
|
|
|
|
// 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-12-28 14:07:15 -05:00
|
|
|
// Make sure the action starts with "cb_"
|
|
|
|
|
if (!util::has_prefix(action, "cb_")) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-10 17:06:07 -04:00
|
|
|
// Get the actor and place. Make sure both exist.
|
|
|
|
|
Tangible *tactor = tangible_get(actor_id);
|
|
|
|
|
Tangible *tplace = tangible_get(place_id);
|
|
|
|
|
if ((tactor == nullptr) || (tplace == nullptr)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-14 11:43:16 -04:00
|
|
|
// Get an ID for the thread. We always use the player
|
|
|
|
|
// pool in this case.
|
|
|
|
|
int64_t tid = tactor->id_player_pool_.get_one();
|
2021-09-10 17:06:07 -04:00
|
|
|
|
|
|
|
|
// Set up for Lua manipulation.
|
|
|
|
|
lua_State *L = state();
|
2021-12-28 13:56:16 -05:00
|
|
|
LuaVar actor, place, func, tangibles, mt, index, thread, threads, thinfo, message, invdata;
|
|
|
|
|
LuaStack LS(L, actor, place, func, tangibles, mt, index, thread, threads, thinfo, message, invdata);
|
2021-09-10 17:06:07 -04: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;
|
|
|
|
|
}
|
2021-12-28 13:56:16 -05:00
|
|
|
LS.rawget(func, index, action);
|
2021-09-10 17:06:07 -04:00
|
|
|
if (!LS.isfunction(func)) {
|
|
|
|
|
LS.result();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convert the InvocationData into a lua table.
|
|
|
|
|
LS.newtable(invdata);
|
2021-10-15 14:47:12 -04:00
|
|
|
for (const auto &p : data) {
|
2021-09-10 17:06:07 -04:00
|
|
|
LS.rawset(invdata, p.first, p.second);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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_pushvalue(L, invdata.index());
|
|
|
|
|
lua_xmove(L, CO, 4);
|
|
|
|
|
|
2021-10-13 20:32:43 -04:00
|
|
|
// Create the thread info table.
|
|
|
|
|
LS.newtable(thinfo);
|
|
|
|
|
LS.rawset(thinfo, "thread", thread);
|
2021-10-14 11:43:16 -04:00
|
|
|
LS.rawset(thinfo, "actorid", actor_id);
|
2021-11-30 12:39:09 -05:00
|
|
|
LS.rawset(thinfo, "nargs", 3); // actor, place, invdata
|
2021-10-14 11:43:16 -04:00
|
|
|
LS.rawset(thinfo, "useppool", true);
|
2021-12-14 12:24:25 -05:00
|
|
|
LS.rawset(thinfo, "print", false);
|
2021-10-13 20:32:43 -04:00
|
|
|
|
2021-09-10 17:06:07 -04:00
|
|
|
// Store the thread into place's thread table.
|
|
|
|
|
LS.rawget(threads, mt, "threads");
|
|
|
|
|
if (!LS.istable(threads)) {
|
|
|
|
|
LS.result();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-10-13 20:32:43 -04:00
|
|
|
LS.rawset(threads, tid, thinfo);
|
2021-09-10 17:06:07 -04:00
|
|
|
LS.result();
|
|
|
|
|
|
|
|
|
|
// Push the thread's ID into the runnable thread queue,
|
|
|
|
|
// then run the thread queue.
|
|
|
|
|
thread_sched_.add(0, tid, place_id);
|
2021-11-26 15:45:36 -05:00
|
|
|
run_scheduled_threads();
|
2021-09-10 17:06:07 -04:00
|
|
|
assert(stack_is_clear());
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 02:17:41 -05:00
|
|
|
void World::invoke_tick(int64_t actor_id, int64_t place_id, const eng::string &action, const InvocationData &data) {
|
2021-12-15 14:18:19 -05:00
|
|
|
if (!util::world_type_authoritative(world_type_)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
clock_ += 1;
|
|
|
|
|
run_scheduled_threads();
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 02:17:41 -05:00
|
|
|
void World::invoke_lua_source(int64_t actor_id, int64_t place_id, const eng::string &action, const InvocationData &data) {
|
2021-12-15 14:18:19 -05:00
|
|
|
if (!util::world_type_authoritative(world_type_)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// We need some kind of authentication here.
|
|
|
|
|
try {
|
|
|
|
|
StreamBuffer sb(action);
|
|
|
|
|
util::LuaSourceVec sv;
|
|
|
|
|
SourceDB::deserialize_source(&sv, &sb);
|
|
|
|
|
update_source(sv);
|
|
|
|
|
} catch (const StreamException &ex) {
|
|
|
|
|
return;
|
2021-11-26 15:45:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void World::run_scheduled_threads() {
|
2021-09-10 17:06:07 -04:00
|
|
|
assert(stack_is_clear());
|
|
|
|
|
lua_State *L = state();
|
2021-12-14 12:24:25 -05:00
|
|
|
LuaVar tangibles, place, mt, threads, thinfo, actorid, nargs, useppool, thread, print;
|
|
|
|
|
LuaStack LS(L, tangibles, place, mt, threads, thinfo, actorid, nargs, useppool, thread, print);
|
2021-09-10 17:06:07 -04:00
|
|
|
|
2021-11-30 12:39:09 -05:00
|
|
|
LS.rawget(tangibles, LuaRegistry, "tangibles");
|
2021-11-26 15:45:36 -05:00
|
|
|
while (thread_sched_.ready(clock_)) {
|
2021-09-10 17:06:07 -04:00
|
|
|
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;
|
|
|
|
|
}
|
2021-10-13 20:32:43 -04:00
|
|
|
LS.rawget(thinfo, threads, sched.thread_id());
|
|
|
|
|
if (!LS.istable(thinfo)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-10-14 11:43:16 -04:00
|
|
|
LS.rawget(actorid, thinfo, "actorid");
|
|
|
|
|
if (!LS.isnumber(actorid)) {
|
2021-10-13 20:32:43 -04:00
|
|
|
continue;
|
|
|
|
|
}
|
2021-11-30 12:39:09 -05:00
|
|
|
LS.rawget(nargs, thinfo, "nargs");
|
|
|
|
|
if (!LS.isnumber(nargs)) {
|
2021-10-13 20:32:43 -04:00
|
|
|
continue;
|
|
|
|
|
}
|
2021-10-14 11:43:16 -04:00
|
|
|
LS.rawget(useppool, thinfo, "useppool");
|
|
|
|
|
if (!LS.isboolean(useppool)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-10-13 20:32:43 -04:00
|
|
|
LS.rawget(thread, thinfo, "thread");
|
2021-09-10 17:06:07 -04:00
|
|
|
if (!LS.isthread(thread)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Resume the coroutine.
|
|
|
|
|
lua_State *CO = LS.ckthread(thread);
|
2021-10-21 13:15:04 -04:00
|
|
|
open_lthread_state(LS.ckinteger(actorid), sched.place_id(), LS.ckboolean(useppool), true);
|
2021-11-30 12:39:09 -05:00
|
|
|
int status = lua_resume(CO, nullptr, LS.ckint(nargs));
|
2022-02-25 19:57:23 -05:00
|
|
|
std::ostream *ostream = lthread_print_stream();
|
2021-11-30 12:39:09 -05:00
|
|
|
|
2021-09-10 17:06:07 -04:00
|
|
|
// Three possible outcomes: finished, yielded, or errored.
|
2021-11-30 12:39:09 -05:00
|
|
|
if (!util::world_type_authoritative(world_type_)) {
|
|
|
|
|
LS.rawset(threads, sched.thread_id(), LuaNil);
|
|
|
|
|
} else if (status == LUA_YIELD) {
|
2021-11-21 13:35:39 -05:00
|
|
|
// If there's nothing on the stack, infer that tangible.nopredict yielded.
|
|
|
|
|
if (lua_gettop(CO) == 0) {
|
2021-09-10 17:06:07 -04:00
|
|
|
LS.rawset(threads, sched.thread_id(), LuaNil);
|
2021-11-21 13:35:39 -05:00
|
|
|
}
|
|
|
|
|
// If there's a single number on the stack, infer that 'wait' yielded.
|
|
|
|
|
else if ((lua_gettop(CO) == 1) && (lua_isnumber(CO, 1))) {
|
2021-09-10 17:06:07 -04:00
|
|
|
lua_Number delay = lua_tonumber(CO, 1);
|
|
|
|
|
lua_settop(CO, 0);
|
2021-11-30 12:39:09 -05:00
|
|
|
LS.rawset(thinfo, "nargs", 0);
|
2021-10-14 11:43:16 -04:00
|
|
|
LS.rawset(thinfo, "useppool", false);
|
2021-11-30 12:39:09 -05:00
|
|
|
thread_sched_.add(clock_ + int64_t(delay), sched.thread_id(), sched.place_id());
|
2021-09-10 17:06:07 -04:00
|
|
|
}
|
2021-11-21 13:35:39 -05:00
|
|
|
// In any other case, generate an error and kill the coroutine.
|
|
|
|
|
else {
|
|
|
|
|
std::cerr << "Thread yielded incorrectly. Killing it." << std::endl;
|
|
|
|
|
LS.rawset(threads, sched.thread_id(), LuaNil);
|
|
|
|
|
}
|
2021-09-10 17:06:07 -04:00
|
|
|
} else if (status == LUA_OK) {
|
2021-12-14 12:24:25 -05:00
|
|
|
// Successfully ran to completion. Print any return values.
|
|
|
|
|
// Remove from thread table.
|
|
|
|
|
LS.rawget(print, thinfo, "print");
|
|
|
|
|
LuaStack LSCO(CO);
|
|
|
|
|
if (LS.ckboolean(print)) {
|
|
|
|
|
for (int i = 1; i <= lua_gettop(CO); i++) {
|
|
|
|
|
pprint(LSCO, LuaSpecial(i), true, ostream);
|
2021-12-15 23:03:43 -05:00
|
|
|
(*ostream) << std::endl;
|
2021-12-14 12:24:25 -05:00
|
|
|
}
|
|
|
|
|
}
|
2021-09-10 17:06:07 -04:00
|
|
|
LS.rawset(threads, sched.thread_id(), LuaNil);
|
|
|
|
|
} else {
|
|
|
|
|
// Generated an error. Add a traceback, print, and kill the coroutine.
|
2021-11-30 12:39:09 -05:00
|
|
|
// Currently, the error is sent to the actor. That seems... not right in the long run.
|
2021-09-10 17:06:07 -04:00
|
|
|
traceback_coroutine(CO);
|
2021-11-30 12:39:09 -05:00
|
|
|
(*ostream) << lua_tostring(CO, -1);
|
2021-09-10 17:06:07 -04:00
|
|
|
LS.rawset(threads, sched.thread_id(), LuaNil);
|
|
|
|
|
}
|
2021-11-30 12:39:09 -05:00
|
|
|
close_lthread_state();
|
2021-09-10 17:06:07 -04:00
|
|
|
}
|
2021-11-30 12:39:09 -05:00
|
|
|
|
2021-09-10 17:06:07 -04:00
|
|
|
LS.result();
|
|
|
|
|
assert(stack_is_clear());
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-14 11:43:16 -04:00
|
|
|
int64_t World::alloc_id_predictable() {
|
|
|
|
|
if (!lthread_use_ppool_) {
|
|
|
|
|
return id_global_pool_.get_one();
|
|
|
|
|
}
|
|
|
|
|
Tangible *t = tangible_get(lthread_actor_id_);
|
|
|
|
|
if (t == nullptr) {
|
|
|
|
|
return id_global_pool_.get_one();
|
|
|
|
|
}
|
|
|
|
|
return t->id_player_pool_.get_one();
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-21 13:15:04 -04:00
|
|
|
const PrintBuffer *World::get_printbuffer(int64_t actor_id) {
|
|
|
|
|
Tangible *actor = tangible_get(actor_id);
|
|
|
|
|
if (actor != nullptr) {
|
2021-11-14 15:57:18 -05:00
|
|
|
return &actor->print_buffer_;
|
2021-10-21 13:15:04 -04:00
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void World::clear_lthread_state() {
|
|
|
|
|
lthread_prints_.reset();
|
|
|
|
|
lthread_actor_id_ = 0;
|
|
|
|
|
lthread_place_id_ = 0;
|
|
|
|
|
lthread_use_ppool_ = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void World::open_lthread_state(int64_t actor, int64_t place, bool ppool, bool prints) {
|
2021-10-14 11:43:16 -04:00
|
|
|
lthread_actor_id_ = actor;
|
|
|
|
|
lthread_place_id_ = place;
|
|
|
|
|
lthread_use_ppool_ = ppool;
|
2021-10-21 13:15:04 -04:00
|
|
|
if (prints) {
|
2022-02-24 02:17:41 -05:00
|
|
|
lthread_prints_.reset(new eng::ostringstream);
|
2021-10-21 13:15:04 -04:00
|
|
|
} else {
|
|
|
|
|
lthread_prints_.reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void World::close_lthread_state() {
|
|
|
|
|
// Copy prints from lthread_prints_ stringstream into
|
|
|
|
|
// the appropriate actor's PrintBuffer. If for some reason
|
|
|
|
|
// there isn't an actor, or if the actor doesn't have a PrintBuffer,
|
|
|
|
|
// send the output to std::cerr.
|
|
|
|
|
if (lthread_prints_ != nullptr) {
|
2022-02-24 02:17:41 -05:00
|
|
|
const eng::string &output = lthread_prints_->str();
|
2021-10-21 13:15:04 -04:00
|
|
|
Tangible *actor = tangible_get(lthread_actor_id_);
|
|
|
|
|
if (actor != nullptr) {
|
2021-11-14 15:57:18 -05:00
|
|
|
bool auth = util::world_type_authoritative(world_type_);
|
|
|
|
|
actor->print_buffer_.add_string(output, auth);
|
2021-10-21 13:15:04 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Now clean up everything.
|
|
|
|
|
clear_lthread_state();
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-25 19:57:23 -05:00
|
|
|
std::ostream *World::lthread_print_stream() const {
|
2021-10-21 13:15:04 -04:00
|
|
|
if (lthread_prints_ != nullptr) {
|
|
|
|
|
return lthread_prints_.get();
|
|
|
|
|
} else {
|
|
|
|
|
return &std::cerr;
|
|
|
|
|
}
|
2021-10-14 11:43:16 -04:00
|
|
|
}
|
|
|
|
|
|
2021-09-10 17:06:07 -04:00
|
|
|
void World::serialize(StreamBuffer *sb) {
|
|
|
|
|
assert(stack_is_clear());
|
|
|
|
|
assert(redirects_.empty());
|
|
|
|
|
// int64_t wc0 = sb->total_writes();
|
|
|
|
|
lua_snap_.serialize(sb);
|
|
|
|
|
id_global_pool_.serialize(sb);
|
2021-11-26 15:45:36 -05:00
|
|
|
sb->write_int64(clock_);
|
2021-09-10 17:06:07 -04:00
|
|
|
thread_sched_.serialize(sb);
|
|
|
|
|
sb->write_uint32(tangibles_.size());
|
|
|
|
|
for (const auto &p : tangibles_) {
|
|
|
|
|
sb->write_int64(p.first);
|
|
|
|
|
p.second->serialize(sb);
|
|
|
|
|
}
|
|
|
|
|
// int64_t wc1 = sb->total_writes();
|
|
|
|
|
// std::cerr << "World serialized to " << wc1-wc0 << " bytes." << std::endl;
|
|
|
|
|
assert(stack_is_clear());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void World::deserialize(StreamBuffer *sb) {
|
|
|
|
|
assert(stack_is_clear());
|
|
|
|
|
redirects_.clear();
|
|
|
|
|
lua_snap_.deserialize(sb);
|
|
|
|
|
id_global_pool_.deserialize(sb);
|
2021-11-26 15:45:36 -05:00
|
|
|
clock_ = sb->read_int64();
|
2021-09-10 17:06:07 -04:00
|
|
|
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_uint32();
|
|
|
|
|
for (size_t i = 0; i < ntan; i++) {
|
|
|
|
|
int64_t id = sb->read_int64();
|
2021-10-15 13:51:32 -04:00
|
|
|
UniqueTangible &t = tangibles_[id];
|
2021-09-10 17:06:07 -04:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
assert(stack_is_clear());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void World::snapshot() {
|
2021-11-09 16:27:39 -05:00
|
|
|
assert(snapshot_.empty());
|
2021-09-10 17:06:07 -04:00
|
|
|
serialize(&snapshot_);
|
2021-11-09 16:27:39 -05:00
|
|
|
assert(!snapshot_.empty());
|
2021-09-10 17:06:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void World::rollback() {
|
2021-10-07 14:58:20 -04:00
|
|
|
assert(!snapshot_.empty());
|
2021-09-10 17:06:07 -04:00
|
|
|
deserialize(&snapshot_);
|
2021-11-09 16:27:39 -05:00
|
|
|
assert(snapshot_.empty());
|
2021-09-10 17:06:07 -04:00
|
|
|
}
|
|
|
|
|
|