Added new tangible ops, including tangible.delete

This commit is contained in:
2021-03-19 13:41:41 -04:00
parent 05052f846e
commit e06bae63f7
5 changed files with 138 additions and 41 deletions

View File

@@ -6,9 +6,21 @@
#include "traceback.hpp"
#include <iostream>
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_);
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() {
@@ -39,6 +51,11 @@ World::World() {
assert (lua_gettop(state()) == 0);
}
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_);
}
void Tangible::update_plane_item() {
util::XYZ xyz = anim_queue_.get_xyz();
std::string plane = anim_queue_.get_plane();
@@ -116,6 +133,33 @@ Tangible *World::tangible_get(lua_State *L, int idx) {
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) {
Tangible *player = tangible_get(player_id);
@@ -159,23 +203,6 @@ Tangible *World::tangible_make(lua_State *L, int64_t id, bool pushdb) {
return t.get();
}
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;
}
void World::update_gui(int64_t actor_id, int64_t place_id, Gui *gui) {
gui->clear();
lua_State *L = state();
@@ -439,6 +466,16 @@ LuaDefine(tangible_plane, "c") {
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();
}
LuaDefine(tangible_animate, "c") {
LuaArg tanobj, config;
LuaStack LS(L, tanobj, config);
@@ -450,6 +487,27 @@ LuaDefine(tangible_animate, "c") {
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();
}
LuaDefine(tangible_make, "c") {
World::fetch_global_pointer(L)->tangible_make(L, 0, true);
return 1;