diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 5289087..6cd1c1e 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -20,7 +20,7 @@ roomSystem.RoomManager.SetOnCreate(function(room, index) --called ~60 times per second roomSystem.Room.SetOnTick(room, function(room) - characterSystem.CharacterManager.ForEach(function(character) + roomSystem.Room.ForEachCharacter(room, function(character) print(characterSystem.Character.GetHandle(character)) end) --[[ diff --git a/server/rooms/room_api.cpp b/server/rooms/room_api.cpp index 4b7db80..8328f99 100644 --- a/server/rooms/room_api.cpp +++ b/server/rooms/room_api.cpp @@ -23,6 +23,9 @@ #include "room_data.hpp" +#include +#include + static int setRoomName(lua_State* L) { RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); room->SetName(lua_tostring(L, 2)); @@ -67,7 +70,22 @@ static int getTriggerMgr(lua_State* L) { //TODO: character list -//TODO: (1) forEachCharacter +static int forEachCharacter(lua_State* L) { + RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); + //pass each character to the given function + for (auto& it : *room->GetCharacterList()) { + lua_pushvalue(L, -1); + lua_pushlightuserdata(L, static_cast(it)); + //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)); @@ -76,6 +94,12 @@ static int setOnTick(lua_State* L) { return 0; } +static int getOnTick(lua_State* L) { + RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); + lua_rawgeti(lua, LUA_REGISTRYINDEX, room->GetTickReference()); + return 1; +} + static int initialize(lua_State* L) { RoomData* room = static_cast(lua_touserdata(L, 1)); @@ -99,7 +123,10 @@ static const luaL_Reg roomLib[] = { {"GetMonsterMgr",getMonsterMgr}, {"GetTriggerMgr",getTriggerMgr}, + {"ForEachCharacter", forEachCharacter}, + {"SetOnTick", setOnTick}, + {"GetOnTick", getOnTick}, {"Initialize", initialize}, {nullptr, nullptr}