Overhaul tangible_make to get rid of LuaOldStack

This commit is contained in:
2023-04-14 15:33:58 -04:00
parent 167dfe909f
commit 69f2fb6226
5 changed files with 35 additions and 46 deletions

View File

@@ -167,8 +167,7 @@ LuaDefine(tangible_build, "config",
World *w = World::fetch_global_pointer(L); World *w = World::fetch_global_pointer(L);
int64_t new_id = w->alloc_id_predictable(); int64_t new_id = w->alloc_id_predictable();
Tangible *tan = w->tangible_make(L, new_id, "nowhere", true); Tangible *tan = w->tangible_make(LS, database, new_id, "nowhere");
lua_replace(L, database.index());
// Update the class of the new tangible. // Update the class of the new tangible.
LS.getmetatable(mt, database); LS.getmetatable(mt, database);

View File

@@ -43,7 +43,7 @@ World::World(WorldType wt) {
// Prepare to manipulate the lua state. // Prepare to manipulate the lua state.
LuaVar world, globtab; LuaVar world, globtab;
LuaOldStack LS(state(), world, globtab); LuaExtStack LS(state(), world, globtab);
// Put the world pointer into the lua registry. // Put the world pointer into the lua registry.
World::store_global_pointer(state(), this); World::store_global_pointer(state(), this);
@@ -75,9 +75,6 @@ World::World(WorldType wt) {
// Initialize global variable state. // Initialize global variable state.
assign_seqno_ = 1; assign_seqno_ = 1;
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_) { Tangible::Tangible(World *w, int64_t id) : world_(w), anim_queue_(w->world_type_), id_player_pool_(&w->id_global_pool_) {
@@ -143,17 +140,10 @@ Tangible *World::tangible_get(const LuaCoreStack &LS, LuaSlot tab, bool allowdel
return result; return result;
} }
Tangible *World::tangible_make(lua_State *L, int64_t id, const eng::string &plane, bool pushdb) { Tangible *World::tangible_make(const LuaCoreStack &LS0, LuaSlot database, int64_t id, const eng::string &plane) {
// Get a state if we don't already have one.
if (L == nullptr) {
L = state();
assert(!pushdb);
}
assert(id != 0); assert(id != 0);
LuaVar metatab; LuaVar metatab;
LuaRet database; LuaExtStack LS(LS0.state(), metatab);
LuaOldStack LS(L, database, metatab);
// Create the C++ part of the structure. // Create the C++ part of the structure.
UniqueTangible &t = tangibles_[id]; UniqueTangible &t = tangibles_[id];
@@ -171,21 +161,24 @@ Tangible *World::tangible_make(lua_State *L, int64_t id, const eng::string &plan
// Set up the inventory and thread table. // Set up the inventory and thread table.
LS.rawset(database, "inventory", LuaNewTable); LS.rawset(database, "inventory", LuaNewTable);
LS.rawset(metatab, "threads", LuaNewTable); LS.rawset(metatab, "threads", LuaNewTable);
LS.result();
if (!pushdb) lua_pop(L, 1);
return t.get(); return t.get();
} }
Tangible *World::tangible_make(int64_t id, const eng::string &plane) {
LuaVar database;
LuaExtStack LS(state(), database);
return tangible_make(LS, database, id, plane);
}
void World::tangible_delete(int64_t id) { void World::tangible_delete(int64_t id) {
lua_State *L = state(); lua_State *L = state();
LuaVar tangibles, database, metatab; LuaVar tangibles, database, metatab;
LuaOldStack LS(L, tangibles, database, metatab); LuaExtStack LS(L, tangibles, database, metatab);
// Fetch the C++ side of the tangible. // Fetch the C++ side of the tangible.
auto iter = tangibles_.find(id); auto iter = tangibles_.find(id);
if (iter == tangibles_.end()) { if (iter == tangibles_.end()) {
LS.result();
return; // Nothing to delete. return; // Nothing to delete.
} }
@@ -204,7 +197,6 @@ void World::tangible_delete(int64_t id) {
// Remove the C++ portion from the tangibles table. // Remove the C++ portion from the tangibles table.
tangibles_.erase(iter); tangibles_.erase(iter);
LS.result();
} }
util::IdVector World::get_near(int64_t player_id, float radius, bool exclude_nowhere, bool omit_player, bool sorted) const { util::IdVector World::get_near(int64_t player_id, float radius, bool exclude_nowhere, bool omit_player, bool sorted) const {
@@ -235,17 +227,14 @@ World::Redirects World::fetch_redirects() {
int64_t World::create_login_actor() { int64_t World::create_login_actor() {
assert(stack_is_clear()); assert(stack_is_clear());
int64_t id = id_global_pool_.get_one(); int64_t id = id_global_pool_.get_one();
Tangible *tan = tangible_make(state(), id, "nowhere", true); LuaVar database, classtab, mt;
LuaArg database; LuaExtStack LS(state(), database, classtab, mt);
LuaVar classtab, mt; Tangible *tan = tangible_make(LS, database, id, "nowhere");
LuaOldStack LS(state(), database, classtab, mt);
LS.makeclass(classtab, "login"); LS.makeclass(classtab, "login");
LS.getmetatable(mt, database); LS.getmetatable(mt, database);
LS.rawset(mt, "__index", classtab); LS.rawset(mt, "__index", classtab);
LS.result();
tan->configure_id_pool_for_actor(); tan->configure_id_pool_for_actor();
tan->print_buffer_.clear(); tan->print_buffer_.clear();
assert(stack_is_clear());
return tan->id(); return tan->id();
} }

View File

@@ -14,7 +14,7 @@ int64_t World::patch_actor(StreamBuffer *sb, DebugCollector *dbc) {
Tangible *s_actor = tangible_get(actor_id); Tangible *s_actor = tangible_get(actor_id);
if (s_actor == nullptr) { if (s_actor == nullptr) {
DebugLine(dbc) << "create new actor " << actor_id; DebugLine(dbc) << "create new actor " << actor_id;
s_actor = tangible_make(nullptr, actor_id, "", false); s_actor = tangible_make(actor_id, "");
s_actor->id_player_pool_.deserialize(sb); s_actor->id_player_pool_.deserialize(sb);
s_actor->anim_queue_.deserialize(sb); s_actor->anim_queue_.deserialize(sb);
s_actor->print_buffer_.deserialize(sb); s_actor->print_buffer_.deserialize(sb);
@@ -61,7 +61,7 @@ void World::patch_visible(StreamBuffer *sb, DebugCollector *dbc) {
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
int64_t id = sb->read_int64(); int64_t id = sb->read_int64();
DebugLine(dbc) << "patch_visible create tan " << id; DebugLine(dbc) << "patch_visible create tan " << id;
Tangible *t = tangible_make(state(), id, "", false); Tangible *t = tangible_make(id, "");
t->anim_queue_.deserialize(sb); t->anim_queue_.deserialize(sb);
t->update_plane_item(); t->update_plane_item();
} }

View File

@@ -267,8 +267,8 @@ LuaDefine(unittests_world1animdiff, "", "some unit tests") {
util::IdVector ids = util::id_vector_create(123, 345); util::IdVector ids = util::id_vector_create(123, 345);
// Create some tangibles, and add some animations. // Create some tangibles, and add some animations.
m->tangible_make(0, 123, "somewhere", false); m->tangible_make(123, "somewhere");
m->tangible_make(0, 345, "somewhere", false); m->tangible_make(345, "somewhere");
m->tangible_walkto(123, 770, 3, 4); m->tangible_walkto(123, 770, 3, 4);
m->tangible_walkto(345, 771, 6, 2); m->tangible_walkto(345, 771, 6, 2);
LuaAssertStrEq(L, m->tangible_ids_debug_string(), "123,345"); LuaAssertStrEq(L, m->tangible_ids_debug_string(), "123,345");
@@ -337,7 +337,7 @@ LuaDefine(unittests_world2pairtab, "", "some unit tests") {
// Create a master model containing some general tables, and // Create a master model containing some general tables, and
// some specialty tables (not numberable). // some specialty tables (not numberable).
m->tangible_make(0, 123, "somewhere", false); m->tangible_make(123, "somewhere");
m->tangible_set_string(123, "inventory.TID", "inventory"); m->tangible_set_string(123, "inventory.TID", "inventory");
m->tangible_set_string(123, "transactions.TID", "transactions"); m->tangible_set_string(123, "transactions.TID", "transactions");
m->tangible_set_string(123, "skills.TID", "skills"); m->tangible_set_string(123, "skills.TID", "skills");
@@ -348,7 +348,7 @@ LuaDefine(unittests_world2pairtab, "", "some unit tests") {
// Now we're going to create a synchronous model that's similar to, but not // Now we're going to create a synchronous model that's similar to, but not
// exactly the same as that master model. // exactly the same as that master model.
ss->tangible_make(0, 123, "somewhere", false); ss->tangible_make(123, "somewhere");
ss->tangible_set_string(123, "inventory.TID", "inventory"); ss->tangible_set_string(123, "inventory.TID", "inventory");
ss->tangible_set_string(123, "skills.TID", "skills"); ss->tangible_set_string(123, "skills.TID", "skills");
ss->tangible_set_string(123, "skills.crap.TID", "skills.crap"); ss->tangible_set_string(123, "skills.crap.TID", "skills.crap");
@@ -384,12 +384,12 @@ LuaDefine(unittests_world3diffluatab, "", "some unit tests") {
StreamBuffer sb; StreamBuffer sb;
// Initialize all three models so that a tangible exists. // Initialize all three models so that a tangible exists.
m->tangible_make(0, 123, "somewhere", false); m->tangible_make(123, "somewhere");
ss->tangible_make(0, 123, "somewhere", false); ss->tangible_make(123, "somewhere");
cs->tangible_make(0, 123, "somewhere", false); cs->tangible_make(123, "somewhere");
m->tangible_make(0, 345, "somewhere", false); m->tangible_make(345, "somewhere");
ss->tangible_make(0, 345, "somewhere", false); ss->tangible_make(345, "somewhere");
cs->tangible_make(0, 345, "somewhere", false); cs->tangible_make(345, "somewhere");
// Put some data into the master model. // Put some data into the master model.
m->tangible_set_string(123, "bacon", "crispy"); m->tangible_set_string(123, "bacon", "crispy");
@@ -438,9 +438,9 @@ LuaDefine(unittests_world4difftanclass, "", "some unit tests") {
StreamBuffer sb; StreamBuffer sb;
// Initialize all three models so that a tangible exists. // Initialize all three models so that a tangible exists.
m->tangible_make(0, 123, "somewhere", false); m->tangible_make(123, "somewhere");
ss->tangible_make(0, 123, "somewhere", false); ss->tangible_make(123, "somewhere");
cs->tangible_make(0, 123, "somewhere", false); cs->tangible_make(123, "somewhere");
// Change the lua class of the tangible. // Change the lua class of the tangible.
m->tangible_set_class(123, "chicken"); m->tangible_set_class(123, "chicken");

View File

@@ -129,11 +129,12 @@ public:
// Make a tangible. // Make a tangible.
// //
// You must provide a valid previously-unused ID. If pushdb is true, pushes // You must provide a valid previously-unused ID. Otherwise, leaves the lua
// the tangible's database onto the lua stack. Otherwise, leaves the lua // stack untouched. Returns a pointer to the C++ part of the tangible, and
// stack untouched. // optionally stores the Lua part in a stack slot.
// //
Tangible *tangible_make(lua_State *L, int64_t id, const eng::string &plane, bool pushdb); Tangible *tangible_make(const LuaCoreStack &LS0, LuaSlot tan, int64_t id, const eng::string &plane);
Tangible *tangible_make(int64_t id, const eng::string &plane);
// Get a pointer to the specified tangible. // Get a pointer to the specified tangible.
// //