From 8c149cf2ee16be8ac4e418e0b74841705932d701 Mon Sep 17 00:00:00 2001 From: Josh Yelon Date: Tue, 30 Mar 2021 19:30:58 -0400 Subject: [PATCH] More work on the login system, especially 'tangible.redirect' --- luprex/core/cpp/luasnap.cpp | 2 +- luprex/core/cpp/luastack.cpp | 4 ++++ luprex/core/cpp/luastack.hpp | 1 + luprex/core/cpp/textgame.cpp | 3 ++- luprex/core/cpp/world.cpp | 37 +++++++++++++++++++++++++++++------- luprex/core/cpp/world.hpp | 3 ++- 6 files changed, 40 insertions(+), 10 deletions(-) diff --git a/luprex/core/cpp/luasnap.cpp b/luprex/core/cpp/luasnap.cpp index c2e66d5b..9351404d 100644 --- a/luprex/core/cpp/luasnap.cpp +++ b/luprex/core/cpp/luasnap.cpp @@ -23,7 +23,7 @@ LuaSnap::LuaSnap() { } LuaSnap::~LuaSnap() { - std::cerr << "LuaSnap destructor not implemented yet" << std::endl; + lua_close(state_); } void LuaSnap::serialize(StreamBuffer *sb) { diff --git a/luprex/core/cpp/luastack.cpp b/luprex/core/cpp/luastack.cpp index c559796c..266cfca9 100644 --- a/luprex/core/cpp/luastack.cpp +++ b/luprex/core/cpp/luastack.cpp @@ -24,6 +24,10 @@ LuaFunctionReg::List LuaFunctionReg::all() { LuaFunctionReg *LuaFunctionReg::LuaFunctionRegistry; +bool LuaStack::ckboolean(LuaSlot s) const { + luaL_checktype(L_, s, LUA_TBOOLEAN); + return lua_toboolean(L_, s) ? true:false; +} lua_Integer LuaStack::ckinteger(LuaSlot s) const { luaL_checktype(L_, s, LUA_TNUMBER); diff --git a/luprex/core/cpp/luastack.hpp b/luprex/core/cpp/luastack.hpp index ab980462..f90abe08 100644 --- a/luprex/core/cpp/luastack.hpp +++ b/luprex/core/cpp/luastack.hpp @@ -380,6 +380,7 @@ public: void checkboolean(LuaSlot index) const { checktype(index, LUA_TBOOLEAN); } void checknil(LuaSlot index) const { checktype(index, LUA_TNIL); } + bool ckboolean(LuaSlot s) const; lua_Integer ckinteger(LuaSlot s) const; lua_Number cknumber(LuaSlot s) const; std::string ckstring(LuaSlot s) const; diff --git a/luprex/core/cpp/textgame.cpp b/luprex/core/cpp/textgame.cpp index f4d81513..5227673e 100644 --- a/luprex/core/cpp/textgame.cpp +++ b/luprex/core/cpp/textgame.cpp @@ -149,7 +149,7 @@ void TextGame::do_quit_command(const StringVec &cmd) { std::cerr << "q command (quit) takes no arguments" << std::endl; return; } - exit(0); + actor_id_ = 0; } void TextGame::do_command(const StringVec &words) { @@ -184,6 +184,7 @@ void TextGame::run() console_.clear(); while (true) { check_redirects(); + if (actor_id_ == 0) break; console_.add_stdin(); int action = console_.action(); if (action == LuaConsole::DO_LUA) { diff --git a/luprex/core/cpp/world.cpp b/luprex/core/cpp/world.cpp index 5039cb50..84cca650 100644 --- a/luprex/core/cpp/world.cpp +++ b/luprex/core/cpp/world.cpp @@ -142,6 +142,9 @@ void World::tangible_delete(lua_State *L, int64_t id) { std::vector World::get_near(int64_t player_id, float radius, bool exclude_nowhere) { Tangible *player = tangible_get(player_id); + if (player == nullptr) { + return std::vector(); + } // Find out where's the center of the world. const AnimStep &aqback = player->anim_queue_.back(); @@ -290,8 +293,14 @@ void World::invoke_plan(int64_t actor_id, int64_t place_id, const std::string &a return; } - // Get an ID batch for the thread, and take one for the thread itself. + // 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; + } + + // Get an ID batch for the thread, and take one for the thread itself. int64_t id_batch = tactor->id_player_pool_.get_batch(); int64_t tid = id_batch++; @@ -525,12 +534,14 @@ LuaDefine(tangible_setclass, "c") { } LuaDefine(tangible_delete, "c") { - // TODO: we need some sanity checks to make sure you're not deleting - // the current player, or anything like that. LuaArg tanobj; LuaStack LS(L, tanobj); World *w = World::fetch_global_pointer(L); Tangible *tan = w->tangible_get(L, tanobj.index()); + if (tan->is_an_actor()) { + luaL_error(L, "Cannot delete a player using tangible.delete, use tangible.redirect instead."); + return 0; + } w->tangible_delete(L, tan->id()); return LS.result(); } @@ -595,12 +606,24 @@ LuaDefine(tangible_get, "c") { } LuaDefine(tangible_redirect, "c") { - LuaArg actor1, actor2; - LuaStack LS(L, actor1, actor2); + LuaArg actor1, actor2, bldz; + LuaStack LS(L, actor1, actor2, bldz); World *w = World::fetch_global_pointer(L); + bool bulldoze = LS.ckboolean(bldz); Tangible *tan1 = w->tangible_get(L, actor1.index()); - Tangible *tan2 = w->tangible_get(L, actor2.index()); - w->redirects_[tan1->id()] = tan2->id(); + if (!tan1->is_an_actor()) { + luaL_error(L, "redirect source is not an actor"); + } + if (LS.isnil(actor2)) { + w->redirects_[tan1->id()] = 0; + } else { + Tangible *tan2 = w->tangible_get(L, actor2.index()); + tan2->be_an_actor(); + w->redirects_[tan1->id()] = tan2->id(); + } + if (bulldoze) { + w->tangible_delete(L, tan1->id()); + } return LS.result(); } diff --git a/luprex/core/cpp/world.hpp b/luprex/core/cpp/world.hpp index b4735bd1..641e0d1b 100644 --- a/luprex/core/cpp/world.hpp +++ b/luprex/core/cpp/world.hpp @@ -68,8 +68,9 @@ public: // int64_t id() { return plane_item_.id(); } - // void be_a_player(); void update_plane_item(); + bool is_an_actor() { return id_player_pool_.fifo_enabled(); } + void be_an_actor() { id_player_pool_.enable_fifo(); } }; class World {