diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 9f30182..94ed374 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -14,6 +14,12 @@ end --test the room hooks roomSystem.RoomManager.SetOnCreate(function(room, index) print("", "Creating room: ", roomSystem.Room.GetName(room), index) + + --called ~60 times per second + --TODO: test this +-- roomSystem.Room.SetOnTick(room, function(room) +-- print("", "","tick") +-- end) end) roomSystem.RoomManager.SetOnUnload(function(room, index) print("", "Unloading room: ", roomSystem.Room.GetName(room), index) diff --git a/server/rooms/room_api.cpp b/server/rooms/room_api.cpp index ffb9c8a..cac2651 100644 --- a/server/rooms/room_api.cpp +++ b/server/rooms/room_api.cpp @@ -65,6 +65,15 @@ static int getWaypointMgr(lua_State* L) { return 1; } +//TODO: character list + +static int setOnTick(lua_State* L) { + RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); + luaL_unref(L, LUA_REGISTRYINDEX, room->GetTickReference()); + room->SetTickReference(luaL_ref(L, LUA_REGISTRYINDEX)); + return 0; +} + static int initialize(lua_State* L) { RoomData* room = static_cast(lua_touserdata(L, 1)); @@ -88,6 +97,8 @@ static const luaL_Reg roomLib[] = { {"GetMonsterMgr",getMonsterMgr}, {"GetWaypointMgr",getWaypointMgr}, + {"SetOnTick", setOnTick}, + {"Initialize", initialize}, {nullptr, nullptr} }; diff --git a/server/rooms/room_data.cpp b/server/rooms/room_data.cpp index 5c3dfad..ea72ddd 100644 --- a/server/rooms/room_data.cpp +++ b/server/rooms/room_data.cpp @@ -21,6 +21,22 @@ */ #include "room_data.hpp" +void RoomData::RunFrame() { + //get the hook + lua_rawgeti(lua, LUA_REGISTRYINDEX, tickRef); + + if (lua_isnil(lua, -1)) { + lua_pop(lua, 1); + return; + } + + //call the tick function, with this as a parameter + lua_pushlightuserdata(lua, this); + if (lua_pcall(lua, 1, 0, 0) != LUA_OK) { + throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) )); + } +} + std::string RoomData::SetName(std::string s) { return roomName = s; } @@ -53,6 +69,26 @@ std::list* RoomData::GetCharacterList() { return &characterList; } -void RoomData::RunFrame() { - //TODO: (1) EMPTY +lua_State* RoomData::SetLuaState(lua_State* L) { + return lua = L; +} + +lua_State* RoomData::GetLuaState() { + return lua; +} + +sqlite3* RoomData::SetDatabase(sqlite3* db) { + return database = db; +} + +sqlite3* RoomData::GetDatabase() { + return database; +} + +int RoomData::SetTickReference(int i) { + return tickRef = i; +} + +int RoomData::GetTickReference() { + return tickRef; } \ No newline at end of file diff --git a/server/rooms/room_data.hpp b/server/rooms/room_data.hpp index 78cbc4f..1933e68 100644 --- a/server/rooms/room_data.hpp +++ b/server/rooms/room_data.hpp @@ -37,6 +37,8 @@ public: RoomData() = default; ~RoomData() = default; + void RunFrame(); + //accessors and mutators std::string SetName(std::string); std::string GetName(); @@ -49,21 +51,34 @@ public: WaypointManager* GetWaypointMgr(); std::list* GetCharacterList(); - void RunFrame(); + //API interfaces + lua_State* SetLuaState(lua_State* L); + lua_State* GetLuaState(); + sqlite3* SetDatabase(sqlite3* db); + sqlite3* GetDatabase(); - //TODO: triggers for unload, save, per-second, player enter, player exit, etc. + //hooks + int SetTickReference(int i); + int GetTickReference(); + //TODO: other triggers like player entry & exit, etc. private: - friend class RoomManager; - - //members + //metadata std::string roomName; std::string tilesetName; + //members RegionPagerLua pager; MonsterManager monsterMgr; WaypointManager waypointMgr; std::list characterList; + + //API + lua_State* lua = nullptr; + sqlite3* database = nullptr; + + //hooks + int tickRef = LUA_NOREF; }; #endif diff --git a/server/rooms/room_manager.cpp b/server/rooms/room_manager.cpp index 78916a8..ee3c6aa 100644 --- a/server/rooms/room_manager.cpp +++ b/server/rooms/room_manager.cpp @@ -35,10 +35,13 @@ int RoomManager::Create(std::string roomName, std::string tileset) { newRoom->SetName(roomName); newRoom->SetTileset(tileset); - newRoom->pager.SetLuaState(lua); - newRoom->monsterMgr.SetLuaState(lua); - newRoom->monsterMgr.SetDatabase(database); - newRoom->waypointMgr.SetLuaState(lua); + newRoom->GetPager()->SetLuaState(lua); + newRoom->GetMonsterMgr()->SetLuaState(lua); + newRoom->GetMonsterMgr()->SetDatabase(database); + newRoom->GetWaypointMgr()->SetLuaState(lua); + + newRoom->SetLuaState(lua); + newRoom->SetDatabase(database); //get the hook lua_rawgeti(lua, LUA_REGISTRYINDEX, createRef); @@ -123,7 +126,7 @@ void RoomManager::PushCharacter(CharacterData* character) { throw(std::runtime_error("Failed to push an character to a non-existant room")); } - room->characterList.push_back(character); + room->GetCharacterList()->push_back(character); } void RoomManager::PopCharacter(CharacterData const* character) { @@ -138,7 +141,7 @@ void RoomManager::PopCharacter(CharacterData const* character) { throw(std::runtime_error("Failed to pop an character to a non-existant room")); } - room->characterList.remove_if([character](CharacterData* ptr) { + room->GetCharacterList()->remove_if([character](CharacterData* ptr) { return character == ptr; }); } diff --git a/server/rooms/room_manager.hpp b/server/rooms/room_manager.hpp index 00b44de..90e7a49 100644 --- a/server/rooms/room_manager.hpp +++ b/server/rooms/room_manager.hpp @@ -70,9 +70,11 @@ private: //members std::map elementMap; + int counter = 0; + + //API lua_State* lua = nullptr; sqlite3* database = nullptr; - int counter = 0; //hooks int createRef = LUA_NOREF; diff --git a/server/rooms/room_manager_api.cpp b/server/rooms/room_manager_api.cpp index 9269de4..c0e88a6 100644 --- a/server/rooms/room_manager_api.cpp +++ b/server/rooms/room_manager_api.cpp @@ -110,7 +110,6 @@ static int setOnUnload(lua_State* L) { return 0; } - static const luaL_Reg roomManagerLib[] = { {"CreateRoom", createRoom}, {"UnloadRoom", unloadRoom},