Updated the map system & APIs
This commit is contained in:
@@ -501,7 +501,9 @@ void InWorld::SendRegionRequest(int roomIndex, int x, int y) {
|
|||||||
|
|
||||||
void InWorld::HandleRegionContent(RegionPacket* const argPacket) {
|
void InWorld::HandleRegionContent(RegionPacket* const argPacket) {
|
||||||
//replace existing regions
|
//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);
|
regionPager.PushRegion(argPacket->region);
|
||||||
|
|
||||||
//clean up after the serial code
|
//clean up after the serial code
|
||||||
|
|||||||
@@ -26,31 +26,11 @@
|
|||||||
#include "region_pager_api.hpp"
|
#include "region_pager_api.hpp"
|
||||||
#include "tile_sheet_api.hpp"
|
#include "tile_sheet_api.hpp"
|
||||||
|
|
||||||
//macros
|
|
||||||
#include "region.hpp"
|
|
||||||
|
|
||||||
//useful "globals"
|
//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.
|
//This mimics linit.c to create a nested collection of all map modules.
|
||||||
static const luaL_Reg funcs[] = {
|
static const luaL_Reg funcs[] = {
|
||||||
//synonyms
|
|
||||||
{"GetRegionWidth", getRegionWidth},
|
|
||||||
{"GetRegionHeight", getRegionHeight},
|
|
||||||
{"GetRegionDepth", getRegionDepth},
|
|
||||||
{nullptr, nullptr}
|
{nullptr, nullptr}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+13
-1
@@ -21,9 +21,9 @@
|
|||||||
*/
|
*/
|
||||||
#include "region.hpp"
|
#include "region.hpp"
|
||||||
|
|
||||||
#include <stdexcept>
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
int snapToBase(int base, int x) {
|
int snapToBase(int base, int x) {
|
||||||
return floor((double)x / base) * base;
|
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) {
|
bool Region::GetSolid(int x, int y) {
|
||||||
return solid[x * REGION_WIDTH + y];
|
return solid[x * REGION_WIDTH + y];
|
||||||
|
}
|
||||||
|
|
||||||
|
int Region::GetX() const {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Region::GetY() const {
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::bitset<REGION_WIDTH*REGION_HEIGHT>* Region::GetSolidBitset() {
|
||||||
|
return &solid;
|
||||||
}
|
}
|
||||||
@@ -48,10 +48,10 @@ public:
|
|||||||
bool GetSolid(int x, int y);
|
bool GetSolid(int x, int y);
|
||||||
|
|
||||||
//accessors
|
//accessors
|
||||||
int GetX() const { return x; }
|
int GetX() const;
|
||||||
int GetY() const { return y; }
|
int GetY() const;
|
||||||
|
|
||||||
std::bitset<REGION_WIDTH*REGION_HEIGHT>* GetSolidBitset() { return &solid; }
|
std::bitset<REGION_WIDTH*REGION_HEIGHT>* GetSolidBitset();
|
||||||
private:
|
private:
|
||||||
const int x;
|
const int x;
|
||||||
const int y;
|
const int y;
|
||||||
|
|||||||
@@ -85,6 +85,8 @@ static const luaL_Reg regionLib[] = {
|
|||||||
{"GetSolid",getSolid},
|
{"GetSolid",getSolid},
|
||||||
{"GetX",getX},
|
{"GetX",getX},
|
||||||
{"GetY",getY},
|
{"GetY",getY},
|
||||||
|
|
||||||
|
//the global macros
|
||||||
{"GetWidth",getWidth},
|
{"GetWidth",getWidth},
|
||||||
{"GetHeight",getHeight},
|
{"GetHeight",getHeight},
|
||||||
{"GetDepth",getDepth},
|
{"GetDepth",getDepth},
|
||||||
|
|||||||
@@ -84,7 +84,22 @@ static int createRegion(lua_State* L) {
|
|||||||
|
|
||||||
static int unloadRegion(lua_State* L) {
|
static int unloadRegion(lua_State* L) {
|
||||||
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
|
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,6 +131,13 @@ static int setOnUnload(lua_State* L) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//debugging
|
||||||
|
static int containerSize(lua_State* L) {
|
||||||
|
RegionPagerLua* pager = static_cast<RegionPagerLua*>(lua_touserdata(L, 1));
|
||||||
|
lua_pushinteger(L, pager->GetContainer()->size());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
static const luaL_Reg regionPagerLib[] = {
|
static const luaL_Reg regionPagerLib[] = {
|
||||||
//curry
|
//curry
|
||||||
{"SetTile", setTile},
|
{"SetTile", setTile},
|
||||||
@@ -136,6 +158,9 @@ static const luaL_Reg regionPagerLib[] = {
|
|||||||
{"SetOnCreate",setOnCreate},
|
{"SetOnCreate",setOnCreate},
|
||||||
{"SetOnUnload",setOnUnload},
|
{"SetOnUnload",setOnUnload},
|
||||||
|
|
||||||
|
//debugging
|
||||||
|
{"ContainerSize", containerSize},
|
||||||
|
|
||||||
//sentinel
|
//sentinel
|
||||||
{nullptr, nullptr}
|
{nullptr, nullptr}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -24,6 +24,10 @@
|
|||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
|
RegionPagerBase::~RegionPagerBase() {
|
||||||
|
UnloadAll();
|
||||||
|
};
|
||||||
|
|
||||||
Region::type_t RegionPagerBase::SetTile(int x, int y, int z, Region::type_t v) {
|
Region::type_t RegionPagerBase::SetTile(int x, int y, int z, Region::type_t v) {
|
||||||
Region* ptr = GetRegion(x, y);
|
Region* ptr = GetRegion(x, y);
|
||||||
return ptr->SetTile(x - ptr->GetX(), y - ptr->GetY(), z, v);
|
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();
|
return ®ionList.front();
|
||||||
}
|
}
|
||||||
|
|
||||||
void RegionPagerBase::UnloadRegion(int x, int y) {
|
void RegionPagerBase::UnloadIf(std::function<bool(Region const&)> fn) {
|
||||||
regionList.remove_if([x, y](Region& region) -> bool {
|
regionList.remove_if(fn);
|
||||||
return region.GetX() == x && region.GetY() == y;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void RegionPagerBase::UnloadAll() {
|
void RegionPagerBase::UnloadAll() {
|
||||||
regionList.clear();
|
regionList.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::list<Region>* RegionPagerBase::GetContainer() {
|
||||||
|
return ®ionList;
|
||||||
}
|
}
|
||||||
@@ -24,12 +24,13 @@
|
|||||||
|
|
||||||
#include "region.hpp"
|
#include "region.hpp"
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
#include <list>
|
#include <list>
|
||||||
|
|
||||||
class RegionPagerBase {
|
class RegionPagerBase {
|
||||||
public:
|
public:
|
||||||
RegionPagerBase() = default;
|
RegionPagerBase() = default;
|
||||||
virtual ~RegionPagerBase() { UnloadAll(); };
|
virtual ~RegionPagerBase();
|
||||||
|
|
||||||
//tile manipulation
|
//tile manipulation
|
||||||
virtual Region::type_t SetTile(int x, int y, int z, Region::type_t v);
|
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* LoadRegion(int x, int y);
|
||||||
virtual Region* SaveRegion(int x, int y);
|
virtual Region* SaveRegion(int x, int y);
|
||||||
virtual Region* CreateRegion(int x, int y);
|
virtual Region* CreateRegion(int x, int y);
|
||||||
virtual void UnloadRegion(int x, int y);
|
|
||||||
|
|
||||||
|
virtual void UnloadIf(std::function<bool(Region const&)> fn);
|
||||||
virtual void UnloadAll();
|
virtual void UnloadAll();
|
||||||
|
|
||||||
//accessors & mutators
|
//accessors & mutators
|
||||||
std::list<Region>* GetContainer() { return ®ionList; }
|
std::list<Region>* GetContainer();
|
||||||
protected:
|
protected:
|
||||||
std::list<Region> regionList;
|
std::list<Region> regionList;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -23,6 +23,9 @@
|
|||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
|
//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() {
|
RegionPagerLua::~RegionPagerLua() {
|
||||||
//unload all regions
|
//unload all regions
|
||||||
UnloadAll();
|
UnloadAll();
|
||||||
@@ -130,23 +133,25 @@ Region* RegionPagerLua::CreateRegion(int x, int y) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//no return
|
//no return
|
||||||
void RegionPagerLua::UnloadRegion(int x, int y) {
|
void RegionPagerLua::UnloadIf(std::function<bool(Region const&)> fn) {
|
||||||
//get the pager's function from the registry
|
//get the pager's function from the registry
|
||||||
lua_rawgeti(lua, LUA_REGISTRYINDEX, unloadRef);
|
lua_rawgeti(lua, LUA_REGISTRYINDEX, unloadRef);
|
||||||
|
|
||||||
//check if this function is available
|
//check if this function is available
|
||||||
if (lua_isnil(lua, -1)) {
|
if (lua_isnil(lua, -1)) {
|
||||||
lua_pop(lua, 1);
|
lua_pop(lua, 1);
|
||||||
|
//remove the regions anyway
|
||||||
|
regionList.remove_if(fn);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//run each region through this lambda
|
//run each region through this lambda
|
||||||
regionList.remove_if([&](Region& region) -> bool {
|
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
|
//push a copy of the function onto the stack with the region
|
||||||
lua_pushvalue(lua, -1);
|
lua_pushvalue(lua, -1);
|
||||||
lua_pushlightuserdata(lua, ®ion);
|
lua_pushlightuserdata(lua, static_cast<void*>(®ion));
|
||||||
|
|
||||||
//call the function, 1 arg, 0 return
|
//call the function, 1 arg, 0 return
|
||||||
if (lua_pcall(lua, 1, 0, 0) != LUA_OK) {
|
if (lua_pcall(lua, 1, 0, 0) != LUA_OK) {
|
||||||
@@ -171,6 +176,8 @@ void RegionPagerLua::UnloadAll() {
|
|||||||
//check if this function is available
|
//check if this function is available
|
||||||
if (lua_isnil(lua, -1)) {
|
if (lua_isnil(lua, -1)) {
|
||||||
lua_pop(lua, 1);
|
lua_pop(lua, 1);
|
||||||
|
//remove the regions anyway
|
||||||
|
regionList.clear();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30,6 +30,7 @@
|
|||||||
#include "lua.hpp"
|
#include "lua.hpp"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class RegionPagerLua : public RegionPagerBase {
|
class RegionPagerLua : public RegionPagerBase {
|
||||||
@@ -41,8 +42,8 @@ public:
|
|||||||
Region* LoadRegion(int x, int y) override;
|
Region* LoadRegion(int x, int y) override;
|
||||||
Region* SaveRegion(int x, int y) override;
|
Region* SaveRegion(int x, int y) override;
|
||||||
Region* CreateRegion(int x, int y) override;
|
Region* CreateRegion(int x, int y) override;
|
||||||
void UnloadRegion(int x, int y) override;
|
|
||||||
|
|
||||||
|
void UnloadIf(std::function<bool(Region const&)> fn) override;
|
||||||
void UnloadAll() override;
|
void UnloadAll() override;
|
||||||
|
|
||||||
//accessors & mutators
|
//accessors & mutators
|
||||||
|
|||||||
@@ -23,3 +23,45 @@ mapSystem.RegionPager.SetOnCreate(roomSystem.Room.GetPager(overworld), mapMaker.
|
|||||||
mapSystem.RegionPager.SetOnUnload(roomSystem.Room.GetPager(overworld), mapSaver.Save)
|
mapSystem.RegionPager.SetOnUnload(roomSystem.Room.GetPager(overworld), mapSaver.Save)
|
||||||
|
|
||||||
print("Finished the lua script")
|
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("-------------------------")
|
||||||
|
--]]
|
||||||
Reference in New Issue
Block a user