From 0b5c47d8c71223f815b9beabfa659a5da85b0803 Mon Sep 17 00:00:00 2001 From: jyelon Date: Tue, 9 Jun 2026 17:36:58 -0400 Subject: [PATCH] Some changes to make login.init more consistent. --- Content/Tangibles/TAN_Character.uasset | 4 ++-- luprex/cpp/core/luastack.hpp | 1 + luprex/cpp/core/world-accessor.cpp | 19 ++++++------------- luprex/cpp/core/world-core.cpp | 26 ++++++++++++++++++-------- luprex/lua/login.lua | 23 +++++++++++------------ 5 files changed, 38 insertions(+), 35 deletions(-) diff --git a/Content/Tangibles/TAN_Character.uasset b/Content/Tangibles/TAN_Character.uasset index 91450916..3f70275e 100644 --- a/Content/Tangibles/TAN_Character.uasset +++ b/Content/Tangibles/TAN_Character.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:da16ef153d93fb56cebf88d72831c57f4d7ec636dc610697b68acf0288848150 -size 357918 +oid sha256:6291e46a8b8c1502b1e5e007c7ef7440538ea5f58a81ae2a0ecbf78f16bb56a1 +size 357777 diff --git a/luprex/cpp/core/luastack.hpp b/luprex/cpp/core/luastack.hpp index d7a6007e..50e74670 100644 --- a/luprex/cpp/core/luastack.hpp +++ b/luprex/cpp/core/luastack.hpp @@ -171,6 +171,7 @@ public: LuaExtraArgs(int i, int s) : index_(i), size_(s) {} LuaSpecial operator[] (int n) const { return LuaSpecial(index_ + n); } + int index(int n) const { return index_ + n; } int size() const { return size_; } friend class LuaCoreStack; diff --git a/luprex/cpp/core/world-accessor.cpp b/luprex/cpp/core/world-accessor.cpp index 5b9de453..52709586 100644 --- a/luprex/cpp/core/world-accessor.cpp +++ b/luprex/cpp/core/world-accessor.cpp @@ -607,20 +607,13 @@ LuaDefine(tangible_start, "tangible,function,arg1,arg2...", "|to continue running until it blocks. But from that point" "|forward, the thread will never be resumed for any reason.") { - int top = lua_gettop(L); - if (top < 2) { - luaL_error(L, "Not enough arguments to tangible.start"); - return 0; - } - int varlen = top - 2; - World *w = World::fetch_global_pointer(L); w->guard_blockable(L, "tangible.start"); + LuaArg place, fname; + LuaExtraArgs extra; LuaVar mt, classtab, plthreads, thread, thinfo, func, tanlist; - LuaDefStack LS(L, mt, classtab, plthreads, thread, thinfo, func, tanlist); - LuaSpecial place(1); - LuaSpecial fname(2); + LuaDefStack LS(L, place, fname, extra, mt, classtab, plthreads, thread, thinfo, func, tanlist); // If they passed in a single tangible, convert it to a tangible list. int64_t place_id = LS.tanid(place); @@ -677,10 +670,10 @@ LuaDefine(tangible_start, "tangible,function,arg1,arg2...", // Create a new thread, set up function and arguments. lua_State *CO = LS.newthread(thread); lua_pushvalue(L, func.index()); - for (int i = 0; i < varlen; i++) { - lua_pushvalue(L, i + 3); + for (int i = 0; i < extra.size(); i++) { + lua_pushvalue(L, extra.index(i)); } - lua_xmove(L, CO, varlen + 1); + lua_xmove(L, CO, extra.size() + 1); // Create the thread info table. LS.newtable(thinfo); diff --git a/luprex/cpp/core/world-core.cpp b/luprex/cpp/core/world-core.cpp index a9a09549..a088edf7 100644 --- a/luprex/cpp/core/world-core.cpp +++ b/luprex/cpp/core/world-core.cpp @@ -310,10 +310,13 @@ int64_t World::connection_create() { assert(stack_is_clear()); int64_t id = id_global_pool_.get_one(); { - LuaVar database, classtab, mt, func; - LuaExtStack LS(state(), database, classtab, mt, func); + LuaVar database, classtab, config, mt, func; + LuaExtStack LS(state(), database, classtab, config, mt, func); Tangible *tan = tangible_make(LS, database, id); - + + // A blank config table to pass to 'init' + LS.newtable(config); + // Set the login flags. if (is_authoritative()) { tan->client_id_ = id; @@ -327,7 +330,9 @@ int64_t World::connection_create() { if (is_authoritative()) { LS.rawget(func, classtab, "init"); - spawn(LS, id, id, func, 0, false); + lua_pushvalue(state(), database.index()); + lua_pushvalue(state(), config.index()); + spawn(LS, id, id, func, 2, false); } } connections_.emplace(id, id); @@ -977,13 +982,18 @@ void World::invoke_lua_source(int64_t actor_id, int64_t place_id, std::string_vi if (errors.empty()) { { lua_State *L = state(); - LuaVar lclass, lfunc; - LuaExtStack LS(L, lclass, lfunc); - + LuaVar lclass, lfunc, tangibles, actor, config; + LuaExtStack LS(L, lclass, lfunc, tangibles, actor, config); + LS.rawget(tangibles, LuaRegistry, "tangibles"); + LS.rawget(actor, tangibles, actor_id); + LS.newtable(config); + LS.getclass(lclass, "world"); if (LS.classname(lclass) != "") { LS.rawget(lfunc, lclass, "init"); - spawn(LS, actor_id, place_id, lfunc, 0, false); + lua_pushvalue(L, actor.index()); + lua_pushvalue(L, config.index()); + spawn(LS, actor_id, place_id, lfunc, 2, false); } } run_scheduled_threads(); diff --git a/luprex/lua/login.lua b/luprex/lua/login.lua index aca89709..28478ba0 100644 --- a/luprex/lua/login.lua +++ b/luprex/lua/login.lua @@ -32,21 +32,20 @@ end -- tangible.delete(actor) -- If actor is a connected player it will disconnect -- This gets called on every login except the admin user. -function login.init() +function login.init(self, config) local player = global.get("nextplayer") global.set("nextplayer", player + 1) - dprint("login.init initializing player ", player) - actor.player = player - tangible.keepactor(actor) -- do not delete this login when the client disconnects - actor.color={0,0,0,0,0,0,0,0,0,0,0,0} - actor.kills=0 - actor.killed=0 - tangible.animinit{tan=actor, anim={bp="character", mesh="manny", plane="earth", xyz={player * 100, 0, 90}}} - dprint("About to start scanning") - actor.startscanning() + dprintf("login.init initializing %p, player %d", self, player) + self.player = player + self.color={0,0,0,0,0,0,0,0,0,0,0,0} + self.kills=0 + self.killed=0 + tangible.animinit{tan=self, anim={bp="character", mesh="manny", plane="earth", xyz={player * 100, 0, 90}}} + tangible.start(self, login.startscanning) end function login.startscanning() + dprintf("Scanning started. Actor=%p Place=%p", actor, place) while true do login.onescan() wait(1) @@ -75,13 +74,13 @@ function login.onescan() end -- This gets called on the admin user. You can call login.init in here if you want. -function world.init() +function world.init(self, config) dprint("world.init") global.set("nextplayer", 0) tangible.build{class=roster, anim={plane="earth", xyz={2000,0,0} } } tangible.build{class=cube, anim={plane="earth", xyz={500,-100,0}, mat_color={1,0,0}}} tangible.build{class=sphere, anim={plane="earth", xyz={500,100,0}, mat_color={0,0,1}}} - login.init() + login.init(self, config) end