diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 1ee6324..99f9ebf 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -130,14 +130,14 @@ roomManagerAPI.SetOnCreate(function(room, index) -- end) - creatureManagerAPI.Create(roomAPI.GetCreatureMgr(room), "anibunny.png", bunnySquare) - --creatureManager with SetOnCreate, SetOnUnload & create & unload barrierManagerAPI.SetOnCreate(roomAPI.GetBarrierMgr(room), function(barrier) barrierAPI.SetScript(barrier, barrierTick) end) roomAPI.SetOnTick(room, function(room) + ret = 0 + --placeholders roomAPI.ForEachCharacter(room, function(character) -- end) @@ -146,7 +146,19 @@ roomManagerAPI.SetOnCreate(function(room, index) -- end) - --TODO: for each barrier + roomAPI.ForEachBarrier(room, function(creature) + -- + end) + + --respawn a new rabbit when needed + if creatureManagerAPI.GetLoadedCount(roomAPI.GetCreatureMgr(room)) < 1 and + barrierManagerAPI.GetLoadedCount(roomAPI.GetCreatureMgr(room)) < 2 + then + --make a new creature + creatureManagerAPI.Create(roomAPI.GetCreatureMgr(room), "anibunny.png", bunnySquare) + ret = 1 + end + return ret end) end) diff --git a/server/combat/barrier_manager.cpp b/server/combat/barrier_manager.cpp index a174459..ece6b19 100644 --- a/server/combat/barrier_manager.cpp +++ b/server/combat/barrier_manager.cpp @@ -32,11 +32,11 @@ BarrierManager::~BarrierManager() { } //arg: a list of barriers to be updated in the clients -void BarrierManager::Update(std::list>* barrierList) { +void BarrierManager::Update(std::list>* barrierList, bool updateAll) { int ret; for (auto& it : elementMap) { ret = it.second.Update(lua); - if (ret) { + if (ret || updateAll) { barrierList->push_back(std::pair(it.first, &it.second)); } } diff --git a/server/combat/barrier_manager.hpp b/server/combat/barrier_manager.hpp index c1d443e..497dfdf 100644 --- a/server/combat/barrier_manager.hpp +++ b/server/combat/barrier_manager.hpp @@ -36,7 +36,7 @@ public: ~BarrierManager(); //common public methods - void Update(std::list>* barrierList); + void Update(std::list>* barrierList, bool updateAll); int Create(int instanceIndex); void Unload(int uid); diff --git a/server/creatures/creature_manager.cpp b/server/creatures/creature_manager.cpp index 4b82eb3..985e7a8 100644 --- a/server/creatures/creature_manager.cpp +++ b/server/creatures/creature_manager.cpp @@ -32,11 +32,11 @@ CreatureManager::~CreatureManager() { } //arg: a list of creatures to be updated in the clients -void CreatureManager::Update(std::list>* creatureList) { +void CreatureManager::Update(std::list>* creatureList, bool updateAll) { int ret; for (auto& it : elementMap) { ret = it.second.Update(lua); - if (ret) { + if (ret || updateAll) { creatureList->push_back(std::pair(it.first, &it.second)); } } diff --git a/server/creatures/creature_manager.hpp b/server/creatures/creature_manager.hpp index 353abc1..64229bd 100644 --- a/server/creatures/creature_manager.hpp +++ b/server/creatures/creature_manager.hpp @@ -37,7 +37,7 @@ public: ~CreatureManager(); //common public methods - void Update(std::list>* creatureList); + void Update(std::list>* creatureList, bool updateAll); int Create(std::string avatar, int scriptRef); void Unload(int uid); diff --git a/server/rooms/room_api.cpp b/server/rooms/room_api.cpp index 8e70448..7128b8d 100644 --- a/server/rooms/room_api.cpp +++ b/server/rooms/room_api.cpp @@ -109,6 +109,24 @@ static int forEachCreature(lua_State* L) { return 0; } +static int forEachBarrier(lua_State* L) { + RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); + BarrierManager* barrierMgr = room->GetBarrierMgr(); + //pass each barrier to the given function + for (auto& it : *barrierMgr->GetContainer()) { + lua_pushvalue(L, -1); + lua_pushlightuserdata(L, static_cast(&it.second)); + //call each iteration, throwing an exception if something happened + if (lua_pcall(L, 1, 0, 0) != LUA_OK) { + std::ostringstream os; + os << "Lua error: "; + os << lua_tostring(L, -1); + throw(std::runtime_error(os.str())); + } + } + return 0; +} + static int setOnTick(lua_State* L) { RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); luaL_unref(L, LUA_REGISTRYINDEX, room->GetTickReference()); @@ -122,6 +140,19 @@ static int getOnTick(lua_State* L) { return 1; } +//TODO: autogen docs +static int setTag(lua_State* L) { + RoomData* room = static_cast(lua_touserdata(L, 1)); + room->SetTag(lua_tostring(L, 2), lua_tostring(L, 3)); + return 0; +} + +static int getTag(lua_State* L) { + RoomData* room = static_cast(lua_touserdata(L, 1)); + lua_pushstring(L, room->GetTag(lua_tostring(L, 2)).c_str()); + return 1; +} + static int initialize(lua_State* L) { RoomData* room = static_cast(lua_touserdata(L, 1)); @@ -148,10 +179,14 @@ static const luaL_Reg roomLib[] = { {"ForEachCharacter", forEachCharacter}, {"ForEachCreature", forEachCreature}, + {"ForEachBarrier", forEachBarrier}, {"SetOnTick", setOnTick}, {"GetOnTick", getOnTick}, + {"SetTag", setTag}, + {"GetTag", getTag}, + {"Initialize", initialize}, {nullptr, nullptr} }; diff --git a/server/rooms/room_data.cpp b/server/rooms/room_data.cpp index b8feee5..3f23bb5 100644 --- a/server/rooms/room_data.cpp +++ b/server/rooms/room_data.cpp @@ -36,12 +36,21 @@ void RoomData::RunFrame() { //get the hook lua_rawgeti(lua, LUA_REGISTRYINDEX, tickRef); + //returning 1 means pump all + bool updateAll = false; if (!lua_isnil(lua, -1)) { //call the tick function, with this as a parameter lua_pushlightuserdata(lua, this); - if (lua_pcall(lua, 1, 0, 0) != LUA_OK) { + if (lua_pcall(lua, 1, 1, 0) != LUA_OK) { throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) )); } + + //pump creatures & barriers + if (lua_tonumber(lua, -1)) { + updateAll = true; + } + + lua_pop(lua, 1); } else { lua_pop(lua, 1); @@ -55,8 +64,8 @@ void RoomData::RunFrame() { for (auto& it : characterList) { it->Update(); } - creatureMgr.Update(&creatureList); - barrierMgr.Update(&barrierList); + creatureMgr.Update(&creatureList, updateAll); + barrierMgr.Update(&barrierList, updateAll); //build a list of entities for use with the triggers std::stack entityStack; @@ -202,4 +211,12 @@ int RoomData::SetTickReference(int i) { int RoomData::GetTickReference() { return tickRef; +} + +std::string RoomData::SetTag(std::string key, std::string value) { + return tags[key] = value; +} + +std::string RoomData::GetTag(std::string key) { + return tags[key]; } \ No newline at end of file diff --git a/server/rooms/room_data.hpp b/server/rooms/room_data.hpp index cd7cb23..8cb31c2 100644 --- a/server/rooms/room_data.hpp +++ b/server/rooms/room_data.hpp @@ -70,6 +70,9 @@ public: int GetTickReference(); //TODO: other triggers like player entry & exit, etc. + std::string SetTag(std::string key, std::string value); + std::string GetTag(std::string key); + private: //metadata std::string roomName; @@ -90,4 +93,5 @@ private: //hooks int tickRef = LUA_NOREF; + std::map tags; };