diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index c251851..7d19380 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -32,7 +32,7 @@ roomAPI.Initialize(overworld, mapSaver.Load, mapSaver.Save, mapMaker.DebugIsland local underworld, uidTwo = roomManagerAPI.CreateRoom("underworld", "overworld.bmp") roomAPI.Initialize(underworld, mapSaver.Load, mapSaver.Save, mapMaker.DebugGrassland, mapSaver.Save) ---debug: test the trigger system +--NOTE: test the trigger system regionPagerAPI = require("region_pager") triggerManagerAPI = require("trigger_manager") @@ -58,17 +58,10 @@ createTrigger("door 1", overworld, 128, -128, function(entity) return end - print("mark 1") local x, y = characterAPI.GetOrigin(entity) - print("mark 2") characterAPI.SetRoomIndex(entity, uidTwo) --TODO: (1) take exit coordinates as a parameter - print("mark 3") characterAPI.SetOrigin(entity, 0, 0) - print("mark 4") networkAPI.PumpCharacterUpdate(entity) - print("mark 5") - - return false end) createTrigger("door 1", underworld, 128, -128, function(entity) @@ -76,17 +69,10 @@ createTrigger("door 1", underworld, 128, -128, function(entity) return end - print("mark 6") local x, y = characterAPI.GetOrigin(entity) - print("mark 7") characterAPI.SetRoomIndex(entity, uidOne) - print("mark 8") characterAPI.SetOrigin(entity, 0, 0) - print("mark 9") networkAPI.PumpCharacterUpdate(entity) - print("mark 10") - - return false end) print("Finished the lua script") diff --git a/server/rooms/room_data.cpp b/server/rooms/room_data.cpp index 911e9e7..8b57466 100644 --- a/server/rooms/room_data.cpp +++ b/server/rooms/room_data.cpp @@ -22,6 +22,7 @@ #include "room_data.hpp" #include +#include #include void RoomData::RunFrame() { @@ -44,42 +45,40 @@ void RoomData::RunFrame() { it->Update(); } //TODO: (3) iterate through the monster map + //TODO: (3) trigger script for monsters + + //build a list of game entities + std::stack entityStack; + for (auto& it : characterList) { + entityStack.push(it); + } + //TODO: (3) push the monster entities //compare the triggers to the entities, using their real hitboxes - for (auto& it : *triggerMgr.GetContainer()) { - BoundingBox itBox = it.second.GetBoundingBox() + it.second.GetOrigin(); - for (auto& character : characterList) { - BoundingBox hitBox = character->GetBounds() + character->GetOrigin(); + //NOTE: this stack solution should prevent problems when modifying the various lists + while(entityStack.size()) { + //get the entity & hitbox + Entity* entity = entityStack.top(); + BoundingBox entityBox = entity->GetBounds() + entity->GetOrigin(); - if ( itBox.CheckOverlap(hitBox) ) { - //trigger script + //get the trigger & hitbox + for (auto& it : *triggerMgr.GetContainer()) { + BoundingBox triggerBox = it.second.GetBoundingBox() + it.second.GetOrigin(); + + if (entityBox.CheckOverlap(triggerBox)) { + //run the trigger script lua_rawgeti(lua, LUA_REGISTRYINDEX, it.second.GetScriptReference()); - lua_pushlightuserdata(lua, character); + lua_pushlightuserdata(lua, entity); - //BUG: (0) - - std::cout << "running scripts" << std::endl; - - //run the script - //BUGFIX: changing the character's room via lua invalidates the list, therefore, the script much signal for an early exit - //TODO: (2) fix this somehow (operate on a stack?) - if (lua_pcall(lua, 1, 1, 0) != LUA_OK) { + if (lua_pcall(lua, 1, 0, 0) != LUA_OK) { //error throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) )); } - - //true = safe to continue, false = exit early - if (!lua_toboolean(lua, -1)) { - std::cout << "Warning!: Early abort" << std::endl; - return; - } } } -// for (auto& monster : *monsterMgr.GetContainer()) { -// if (it.second.Compare(static_cast(&monster.second))) { -// //TODO: (1) trigger script -// } -// } + + //next + entityStack.pop(); } } diff --git a/server/server_utilities/server_utilities.cpp b/server/server_utilities/server_utilities.cpp index 0583ee5..5207d4f 100644 --- a/server/server_utilities/server_utilities.cpp +++ b/server/server_utilities/server_utilities.cpp @@ -156,8 +156,6 @@ void copyCharacterToPacket(CharacterPacket* const packet, CharacterData* const c } void pumpAndChangeRooms(int characterIndex, int newRoomIndex) { - //BUG: three redundant lookups - //get the character object CharacterData* character = CharacterManager::GetSingleton().Get(characterIndex);