diff --git a/luprex/cpp/core/luastack.cpp b/luprex/cpp/core/luastack.cpp index 3b6615a4..6169dc91 100644 --- a/luprex/cpp/core/luastack.cpp +++ b/luprex/cpp/core/luastack.cpp @@ -94,8 +94,8 @@ lua_State *LuaCoreStack::newstate (lua_Alloc allocf) { lua_pushstring(L, "tangibles"); lua_newtable(L); lua_rawset(L, LUA_REGISTRYINDEX); - lua_pushstring(L, "worldtype"); - lua_pushnumber(L, WORLD_TYPE_MASTER); + lua_pushstring(L, "is_authoritative"); + lua_pushboolean(L, 1); lua_rawset(L, LUA_REGISTRYINDEX); return L; @@ -687,24 +687,19 @@ void LuaCoreStack::setvisited(LuaSlot tab, bool visited) const { lua_modflagbits(L_, tab.index(), 0x0010, visited ? 0x0010 : 0); } -WorldType LuaCoreStack::get_world_type() const { - lua_pushstring(L_, "worldtype"); - lua_rawget(L_, LUA_REGISTRYINDEX); - lua_Integer n = lua_tointeger(L_, -1); - lua_pop(L_, 1); - assert(n != 0); - return (WorldType)n; -} - -void LuaCoreStack::set_world_type(WorldType t) const { - lua_pushstring(L_, "worldtype"); - lua_pushnumber(L_, (int)t); +void LuaCoreStack::set_authoritative(bool auth) const { + lua_pushstring(L_, "is_authoritative"); + lua_pushboolean(L_, auth ? 1 : 0); lua_rawset(L_, LUA_REGISTRYINDEX); } void LuaCoreStack::guard_nopredict(const char *fn) { if (lua_isyieldable(L_)) { - if (!is_authoritative()) { + lua_pushstring(L_, "is_authoritative"); + lua_rawget(L_, LUA_REGISTRYINDEX); + bool auth = lua_toboolean(L_, -1); + lua_pop(L_, 1); + if (!auth) { lua_yield(L_, 0); luaL_error(L_, "unexplained nopredict failure in %s", fn); } diff --git a/luprex/cpp/core/luastack.hpp b/luprex/cpp/core/luastack.hpp index 5910d640..eb163f19 100644 --- a/luprex/cpp/core/luastack.hpp +++ b/luprex/cpp/core/luastack.hpp @@ -989,25 +989,11 @@ public: // void setvisited(LuaSlot tab, bool visited) const; - // Store the world type in the registry. + // Store whether the world is authoritative in the + // registry. This is used to implement guard_nopredict. // - // This just stores the enum value in the registry key "worldtype". - // - void set_world_type(WorldType t) const; + void set_authoritative(bool auth) const; - // Return the world type from the registry. - // - // This just fetches the enum value from the registry key "worldtype". - // - WorldType get_world_type() const; - - // Return true if this world is authoritative. - // - // This fetches the enum value from the registry key "worldtype", - // then it checks if the world type is authoritative. - // - bool is_authoritative() { return util::is_authoritative(get_world_type()); } - // Yield this thread with zero if in a nonauth model, and not a probe. // // The function name is just used for generating better error messages. diff --git a/luprex/cpp/core/util.hpp b/luprex/cpp/core/util.hpp index e70629b7..3935a1a4 100644 --- a/luprex/cpp/core/util.hpp +++ b/luprex/cpp/core/util.hpp @@ -33,11 +33,6 @@ #include "spookyv2.hpp" -enum WorldType { - WORLD_TYPE_MASTER = 1, - WORLD_TYPE_PREDICTIVE = 2, -}; - namespace sv { @@ -355,9 +350,6 @@ double distance_squared(double x1, double y1, double x2, double y2); // Make a LuaSourceVec with one element, for unit testing. LuaSourcePtr make_lua_source(const eng::string &code); -// Return true if the worldtype is authoritative. -inline bool is_authoritative(WorldType t) { return (t == WORLD_TYPE_MASTER); } - // Remove items from a vector that are nullptr. template void remove_nullptrs(T &vec) { diff --git a/luprex/cpp/core/world-core.cpp b/luprex/cpp/core/world-core.cpp index 1a03231e..7ae96a43 100644 --- a/luprex/cpp/core/world-core.cpp +++ b/luprex/cpp/core/world-core.cpp @@ -131,8 +131,8 @@ World::World(WorldType wt) { LS.getglobaltable(globtab); LS.settabletype(globtab, LUA_TT_GLOBALENV); - // Store the world type in the registry. - LS.set_world_type(wt); + // Store whether the world is authoritative in the registry. + LS.set_authoritative(wt == WORLD_TYPE_MASTER); // Create the globaldb in the registry. LS.rawset(LuaRegistry, "globaldb", LuaNewTable); diff --git a/luprex/cpp/core/world.hpp b/luprex/cpp/core/world.hpp index b5094f9d..5cfe819e 100644 --- a/luprex/cpp/core/world.hpp +++ b/luprex/cpp/core/world.hpp @@ -22,6 +22,11 @@ #include "source.hpp" #include "luasnap.hpp" +enum WorldType { + WORLD_TYPE_MASTER = 1, + WORLD_TYPE_PREDICTIVE = 2, +}; + class World; class Tangible : public eng::opnew { @@ -313,7 +318,7 @@ public: // Check if the world is authoritative. // - bool is_authoritative() const { return util::is_authoritative(world_type_); } + bool is_authoritative() const { return world_type_ == WORLD_TYPE_MASTER; } // Get a table showing all outstanding HTTP requests. //