diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index 9547f01..19caef0 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -501,7 +501,9 @@ void InWorld::SendRegionRequest(int roomIndex, int x, int y) { void InWorld::HandleRegionContent(RegionPacket* const argPacket) { //replace existing regions - regionPager.UnloadRegion(argPacket->x, argPacket->y); + regionPager.UnloadIf([&](Region const& region) -> bool { + return region.GetX() == argPacket->x && region.GetY() == argPacket->y; + }); regionPager.PushRegion(argPacket->region); //clean up after the serial code diff --git a/common/map/map_system_api.cpp b/common/map/map_system_api.cpp index 9e908ad..1ab6984 100644 --- a/common/map/map_system_api.cpp +++ b/common/map/map_system_api.cpp @@ -26,31 +26,11 @@ #include "region_pager_api.hpp" #include "tile_sheet_api.hpp" -//macros -#include "region.hpp" - //useful "globals" -static int getRegionWidth(lua_State* L) { - lua_pushinteger(L, REGION_WIDTH); - return 1; -} - -static int getRegionHeight(lua_State* L) { - lua_pushinteger(L, REGION_HEIGHT); - return 1; -} - -static int getRegionDepth(lua_State* L) { - lua_pushinteger(L, REGION_DEPTH); - return 1; -} +//... //This mimics linit.c to create a nested collection of all map modules. static const luaL_Reg funcs[] = { - //synonyms - {"GetRegionWidth", getRegionWidth}, - {"GetRegionHeight", getRegionHeight}, - {"GetRegionDepth", getRegionDepth}, {nullptr, nullptr} }; diff --git a/common/map/region.cpp b/common/map/region.cpp index 229c350..4546bdf 100644 --- a/common/map/region.cpp +++ b/common/map/region.cpp @@ -21,9 +21,9 @@ */ #include "region.hpp" -#include #include #include +#include int snapToBase(int base, int x) { return floor((double)x / base) * base; @@ -55,4 +55,16 @@ bool Region::SetSolid(int x, int y, bool b) { bool Region::GetSolid(int x, int y) { return solid[x * REGION_WIDTH + y]; +} + +int Region::GetX() const { + return x; +} + +int Region::GetY() const { + return y; +} + +std::bitset* Region::GetSolidBitset() { + return &solid; } \ No newline at end of file diff --git a/common/map/region.hpp b/common/map/region.hpp index 5e2f882..fd8c461 100644 --- a/common/map/region.hpp +++ b/common/map/region.hpp @@ -48,10 +48,10 @@ public: bool GetSolid(int x, int y); //accessors - int GetX() const { return x; } - int GetY() const { return y; } + int GetX() const; + int GetY() const; - std::bitset* GetSolidBitset() { return &solid; } + std::bitset* GetSolidBitset(); private: const int x; const int y; diff --git a/common/map/region_api.cpp b/common/map/region_api.cpp index 1749f49..1c0f848 100644 --- a/common/map/region_api.cpp +++ b/common/map/region_api.cpp @@ -85,6 +85,8 @@ static const luaL_Reg regionLib[] = { {"GetSolid",getSolid}, {"GetX",getX}, {"GetY",getY}, + + //the global macros {"GetWidth",getWidth}, {"GetHeight",getHeight}, {"GetDepth",getDepth}, diff --git a/common/map/region_pager_api.cpp b/common/map/region_pager_api.cpp index d7a38e9..73ad6e9 100644 --- a/common/map/region_pager_api.cpp +++ b/common/map/region_pager_api.cpp @@ -84,7 +84,22 @@ static int createRegion(lua_State* L) { static int unloadRegion(lua_State* L) { RegionPagerLua* pager = reinterpret_cast(lua_touserdata(L, 1)); - pager->UnloadRegion(lua_tointeger(L, 2), lua_tointeger(L, 3)); + + //two argument types: coords & the region itself + switch(lua_type(L, 2)) { + case LUA_TNUMBER: + pager->UnloadIf([&](Region const& region) -> bool { + int x = lua_tointeger(L, 2); + int y = lua_tointeger(L, 3); + return region.GetX() == x && region.GetY() == y; + }); + break; + case LUA_TLIGHTUSERDATA: + pager->UnloadIf([&](Region const& region) -> bool { + return (®ion) == lua_touserdata(L, 2); + }); + break; + } return 0; } @@ -116,6 +131,13 @@ static int setOnUnload(lua_State* L) { return 0; } +//debugging +static int containerSize(lua_State* L) { + RegionPagerLua* pager = static_cast(lua_touserdata(L, 1)); + lua_pushinteger(L, pager->GetContainer()->size()); + return 1; +} + static const luaL_Reg regionPagerLib[] = { //curry {"SetTile", setTile}, @@ -136,6 +158,9 @@ static const luaL_Reg regionPagerLib[] = { {"SetOnCreate",setOnCreate}, {"SetOnUnload",setOnUnload}, + //debugging + {"ContainerSize", containerSize}, + //sentinel {nullptr, nullptr} }; diff --git a/common/map/region_pager_base.cpp b/common/map/region_pager_base.cpp index d20b346..1df6864 100644 --- a/common/map/region_pager_base.cpp +++ b/common/map/region_pager_base.cpp @@ -24,6 +24,10 @@ #include #include +RegionPagerBase::~RegionPagerBase() { + UnloadAll(); +}; + Region::type_t RegionPagerBase::SetTile(int x, int y, int z, Region::type_t v) { Region* ptr = GetRegion(x, y); return ptr->SetTile(x - ptr->GetX(), y - ptr->GetY(), z, v); @@ -88,12 +92,14 @@ Region* RegionPagerBase::CreateRegion(int x, int y) { return ®ionList.front(); } -void RegionPagerBase::UnloadRegion(int x, int y) { - regionList.remove_if([x, y](Region& region) -> bool { - return region.GetX() == x && region.GetY() == y; - }); +void RegionPagerBase::UnloadIf(std::function fn) { + regionList.remove_if(fn); } void RegionPagerBase::UnloadAll() { regionList.clear(); +} + +std::list* RegionPagerBase::GetContainer() { + return ®ionList; } \ No newline at end of file diff --git a/common/map/region_pager_base.hpp b/common/map/region_pager_base.hpp index c6d6b3d..cdafa3f 100644 --- a/common/map/region_pager_base.hpp +++ b/common/map/region_pager_base.hpp @@ -24,12 +24,13 @@ #include "region.hpp" +#include #include class RegionPagerBase { public: RegionPagerBase() = default; - virtual ~RegionPagerBase() { UnloadAll(); }; + virtual ~RegionPagerBase(); //tile manipulation virtual Region::type_t SetTile(int x, int y, int z, Region::type_t v); @@ -47,12 +48,12 @@ public: virtual Region* LoadRegion(int x, int y); virtual Region* SaveRegion(int x, int y); virtual Region* CreateRegion(int x, int y); - virtual void UnloadRegion(int x, int y); + virtual void UnloadIf(std::function fn); virtual void UnloadAll(); //accessors & mutators - std::list* GetContainer() { return ®ionList; } + std::list* GetContainer(); protected: std::list regionList; }; diff --git a/common/map/region_pager_lua.cpp b/common/map/region_pager_lua.cpp index 8fb0dfc..4e0a973 100644 --- a/common/map/region_pager_lua.cpp +++ b/common/map/region_pager_lua.cpp @@ -23,6 +23,9 @@ #include +//DOCS: Load, Save and Create fail unless the lua function has been set +//DOCS: UnloadIf and UnloadAll will still continue without the function set + RegionPagerLua::~RegionPagerLua() { //unload all regions UnloadAll(); @@ -130,23 +133,25 @@ Region* RegionPagerLua::CreateRegion(int x, int y) { } //no return -void RegionPagerLua::UnloadRegion(int x, int y) { +void RegionPagerLua::UnloadIf(std::function fn) { //get the pager's function from the registry lua_rawgeti(lua, LUA_REGISTRYINDEX, unloadRef); //check if this function is available if (lua_isnil(lua, -1)) { lua_pop(lua, 1); + //remove the regions anyway + regionList.remove_if(fn); return; } //run each region through this lambda regionList.remove_if([&](Region& region) -> bool { - if (region.GetX() == x && region.GetY() == y) { + if (fn(region)) { //push a copy of the function onto the stack with the region lua_pushvalue(lua, -1); - lua_pushlightuserdata(lua, ®ion); + lua_pushlightuserdata(lua, static_cast(®ion)); //call the function, 1 arg, 0 return if (lua_pcall(lua, 1, 0, 0) != LUA_OK) { @@ -171,6 +176,8 @@ void RegionPagerLua::UnloadAll() { //check if this function is available if (lua_isnil(lua, -1)) { lua_pop(lua, 1); + //remove the regions anyway + regionList.clear(); return; } diff --git a/common/map/region_pager_lua.hpp b/common/map/region_pager_lua.hpp index 203715b..a4f5ba1 100644 --- a/common/map/region_pager_lua.hpp +++ b/common/map/region_pager_lua.hpp @@ -30,6 +30,7 @@ #include "lua.hpp" #endif +#include #include class RegionPagerLua : public RegionPagerBase { @@ -41,8 +42,8 @@ public: Region* LoadRegion(int x, int y) override; Region* SaveRegion(int x, int y) override; Region* CreateRegion(int x, int y) override; - void UnloadRegion(int x, int y) override; + void UnloadIf(std::function fn) override; void UnloadAll() override; //accessors & mutators diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 364c061..53c733f 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -23,3 +23,45 @@ mapSystem.RegionPager.SetOnCreate(roomSystem.Room.GetPager(overworld), mapMaker. mapSystem.RegionPager.SetOnUnload(roomSystem.Room.GetPager(overworld), mapSaver.Save) print("Finished the lua script") + +--[[ +debugging test + +Ideal output: + +------------------------- +pager: userdata: [memory location] +Size 0: 0 +[debug output from load] +Size 1: 1 +[debug output from save] +Size 2: 0 +[debug output from load] +Size 3: 1 +[debug output from save] +Size 4: 0 +------------------------- + +--]-] + +print("-------------------------") +local pager = roomSystem.Room.GetPager(overworld) + +print("pager:", pager) + +print("Size 0:", mapSystem.RegionPager.ContainerSize(pager)) + +local regionFoo = mapSystem.RegionPager.GetRegion(pager, 0, 0) +print("Size 1:", mapSystem.RegionPager.ContainerSize(pager)) + +mapSystem.RegionPager.UnloadRegion(pager, regionFoo) +print("Size 2:", mapSystem.RegionPager.ContainerSize(pager)) + +local regionFoo = mapSystem.RegionPager.GetRegion(pager, 0, 0) +print("Size 3:", mapSystem.RegionPager.ContainerSize(pager)) + +mapSystem.RegionPager.UnloadRegion(pager, 0, 0) +print("Size 4:", mapSystem.RegionPager.ContainerSize(pager)) + +print("-------------------------") +--]] \ No newline at end of file