Tangible serialization and design improvements

This commit is contained in:
2021-03-14 18:17:34 -04:00
parent 4426fa157a
commit 7c9fd1e46b
7 changed files with 108 additions and 44 deletions

View File

@@ -6,7 +6,9 @@
#include "traceback.hpp"
#include <iostream>
Tangible::Tangible() : world_(nullptr) {
Tangible::Tangible(World *w, int64_t id) : world_(w), id_player_pool_(&w->id_global_pool_) {
plane_item_.set_id(id);
w->plane_map_.track(&plane_item_);
}
World::~World() {
@@ -43,9 +45,23 @@ void Tangible::update_plane_item() {
plane_item_.set_pos(plane, xyz.x, xyz.y, xyz.z);
}
void Tangible::serialize(StreamBuffer *sb) {
sb->write_int64(id());
anim_queue_.serialize(sb);
id_player_pool_.serialize(sb);
}
void Tangible::deserialize(StreamBuffer *sb) {
int64_t id = sb->read_int64();
plane_item_.set_id(id);
anim_queue_.deserialize(sb);
id_player_pool_.deserialize(sb);
update_plane_item();
}
void Tangible::be_a_player() {
if (id_player_pool_ == nullptr) {
id_player_pool_.reset(new IdPlayerPool(&world_->id_global_pool_));
if (!id_player_pool_.fifo_enabled()) {
id_player_pool_.enable_fifo();
anim_queue_.add(world_->id_global_pool_.get_one(), "");
anim_queue_.set_graphic("player");
@@ -55,7 +71,7 @@ void Tangible::be_a_player() {
LS.makeclass(classtab, "player");
LS.rawget(tangibles, LuaRegistry, "tangibles");
LS.rawget(place, tangibles, anim_queue_.get_id());
LS.rawget(place, tangibles, id());
LS.getmetatable(mt, place);
LS.rawset(mt, "__index", classtab);
LS.result();
@@ -80,7 +96,7 @@ Tangible *World::tangible_get(int64_t id) {
if (iter == tangibles_.end()) {
return nullptr;
} else {
return &iter->second;
return iter->second.get();
}
}
@@ -122,12 +138,9 @@ Tangible *World::tangible_make(lua_State *L, int64_t id, bool pushdb) {
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_);
std::unique_ptr<Tangible> &t = tangibles_[id];
assert (t == nullptr);
t.reset(new Tangible(this, id));
// Create the tangible's database and metatable.
LS.set(database, LuaNewTable);
@@ -146,7 +159,7 @@ Tangible *World::tangible_make(lua_State *L, int64_t id, bool pushdb) {
LS.result();
if (!pushdb) lua_pop(L, 1);
return t;
return t.get();
}
void World::store_global_pointer(lua_State *L, World *v) {
@@ -227,7 +240,7 @@ void World::invoke_plan(int64_t actor_id, int64_t place_id, const std::string &a
// 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 id_batch = tactor->id_player_pool_.get_batch();
int64_t tid = id_batch++;
// Set up for Lua manipulation.