diff --git a/common/map/region_pager.cpp b/common/map/region_pager.cpp index 547b51f..098a720 100644 --- a/common/map/region_pager.cpp +++ b/common/map/region_pager.cpp @@ -70,10 +70,14 @@ Region* RegionPager::LoadRegion(int x, int y) { x = snapToBase(REGION_WIDTH, x); y = snapToBase(REGION_HEIGHT, y); - Region* ptr = new Region(x, y); + //overwrite? + Region* ptr = FindRegion(x, y); + if (!ptr) { + ptr = new Region(x, y); + } //API hook - lua_getglobal(luaState, "map"); + lua_getglobal(luaState, "region"); lua_getfield(luaState, -1, "load"); lua_pushlightuserdata(luaState, ptr); lua_pushstring(luaState, directory.c_str()); @@ -101,7 +105,7 @@ Region* RegionPager::SaveRegion(int x, int y) { Region* ptr = FindRegion(x, y); if (ptr) { //API hook - lua_getglobal(luaState, "map"); + lua_getglobal(luaState, "region"); lua_getfield(luaState, -1, "save"); lua_pushlightuserdata(luaState, ptr); lua_pushstring(luaState, directory.c_str()); @@ -118,13 +122,17 @@ Region* RegionPager::CreateRegion(int x, int y) { x = snapToBase(REGION_WIDTH, x); y = snapToBase(REGION_HEIGHT, y); - //create and push the object - Region* ptr = new Region(x, y); + //overwrite? + Region* ptr = FindRegion(x, y); + if (!ptr) { + ptr = new Region(x, y); + } //API hook - lua_getglobal(luaState, "map"); + lua_getglobal(luaState, "region"); lua_getfield(luaState, -1, "create"); lua_pushlightuserdata(luaState, ptr); + //TODO: parameters if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) { throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) )); } @@ -139,7 +147,7 @@ void RegionPager::UnloadRegion(int x, int y) { x = snapToBase(REGION_WIDTH, x); y = snapToBase(REGION_HEIGHT, y); - lua_getglobal(luaState, "map"); + lua_getglobal(luaState, "region"); //custom loop, not FindRegion() for (std::list::iterator it = regionList.begin(); it != regionList.end(); /* EMPTY */) { @@ -148,7 +156,8 @@ void RegionPager::UnloadRegion(int x, int y) { //API hook lua_getfield(luaState, -1, "unload"); lua_pushlightuserdata(luaState, *it); - if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) { + lua_pushstring(luaState, directory.c_str()); + if (lua_pcall(luaState, 2, 0, 0) != LUA_OK) { throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) )); } diff --git a/common/map/region_pager.hpp b/common/map/region_pager.hpp index e53bc2c..ab119dc 100644 --- a/common/map/region_pager.hpp +++ b/common/map/region_pager.hpp @@ -46,7 +46,6 @@ public: Region* SaveRegion(int x, int y); Region* CreateRegion(int x, int y); void UnloadRegion(int x, int y); - void DeleteRegion(int x, int y); //accessors & mutators std::list* GetContainer() { return ®ionList; } diff --git a/common/script/pager_api.cpp b/common/script/pager_api.cpp index 5e88260..16c10dd 100644 --- a/common/script/pager_api.cpp +++ b/common/script/pager_api.cpp @@ -21,12 +21,81 @@ */ #include "pager_api.hpp" -static int func(lua_State* L) { +#include "region_pager.hpp" +#include "region.hpp" + +#include + +static int setTile(lua_State* L) { + RegionPager* pager = reinterpret_cast(lua_touserdata(L, 1)); + int ret = pager->SetTile(lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5)); + lua_pop(L, 5); + lua_pushinteger(L, ret); + return 1; +} + +static int getTile(lua_State* L) { + RegionPager* pager = reinterpret_cast(lua_touserdata(L, 1)); + int ret = pager->GetTile(lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4)); + lua_pop(L, 4); + lua_pushinteger(L, ret); + return 1; +} + +static int getRegion(lua_State* L) { + RegionPager* pager = reinterpret_cast(lua_touserdata(L, 1)); + Region* region = pager->GetRegion(lua_tointeger(L, 2), lua_tointeger(L, 3)); + lua_pop(L, 3); + lua_pushlightuserdata(L, region); + return 1; +} + +static int setDirectory(lua_State* L) { + RegionPager* pager = reinterpret_cast(lua_touserdata(L, 1)); + std::string s = pager->SetDirectory(lua_tostring(L, 2)); + lua_pop(L, 2); + lua_pushstring(L, s.c_str()); + return 1; +} + +static int getDirectory(lua_State* L) { + RegionPager* pager = reinterpret_cast(lua_touserdata(L, 1)); + std::string s = pager->GetDirectory(); + lua_pop(L, 1); + lua_pushstring(L, s.c_str()); + return 1; +} + +static int loadRegion(lua_State* L) { + //TODO: fill this + return 0; +} + +static int saveRegion(lua_State* L) { + //TODO: fill this + return 0; +} + +static int createRegion(lua_State* L) { + //TODO: fill this + return 0; +} + +static int unloadRegion(lua_State* L) { + //TODO: fill this return 0; } static const luaL_Reg pagerlib[] = { - {"name", func}, + {"settile", setTile}, + {"gettile", getTile}, + {"getregion", getRegion}, + {"setdirectory", setDirectory}, + {"getdirectory", getDirectory}, + {"loadregion", loadRegion}, + {"saveregion", saveRegion}, + {"createregion", createRegion}, + {"unloadregion", unloadRegion}, {nullptr, nullptr} }; diff --git a/common/script/region_api.cpp b/common/script/region_api.cpp index c94691b..9e16d45 100644 --- a/common/script/region_api.cpp +++ b/common/script/region_api.cpp @@ -24,55 +24,84 @@ #include "region.hpp" static int setTile(lua_State* L) { - //operating on a region - Region* ptr = (Region*)lua_touserdata(L, 1); - ptr->SetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1, lua_tointeger(L, 5)); - return 0; + Region* region = reinterpret_cast(lua_touserdata(L, 1)); + int ret = region->SetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1, lua_tointeger(L, 5)); + lua_pop(L, 5); + lua_pushinteger(L, ret); + return 1; } static int getTile(lua_State* L) { - //operating on a region - Region* ptr = (Region*)lua_touserdata(L, 1); - int ret = ptr->GetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1); - lua_pushnumber(L, ret); + Region* region = reinterpret_cast(lua_touserdata(L, 1)); + int ret = region->GetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1); + lua_pop(L, 4); + lua_pushinteger(L, ret); return 1; } static int getX(lua_State* L) { - Region* ptr = (Region*)lua_touserdata(L, 1); - lua_pushinteger(L, ptr->GetX()); + Region* region = reinterpret_cast(lua_touserdata(L, 1)); + int ret = region->GetX(); + lua_pop(L, 1); + lua_pushinteger(L, ret); return 1; } static int getY(lua_State* L) { - Region* ptr = (Region*)lua_touserdata(L, 1); - lua_pushinteger(L, ptr->GetY()); + Region* region = reinterpret_cast(lua_touserdata(L, 1)); + int ret = region->GetY(); + lua_pop(L, 1); + lua_pushinteger(L, ret); return 1; } -static int getRegionWidth(lua_State* L) { +static int getWidth(lua_State* L) { lua_pushinteger(L, REGION_WIDTH); return 1; } -static int getRegionHeight(lua_State* L) { +static int getHeight(lua_State* L) { lua_pushinteger(L, REGION_HEIGHT); return 1; } -static int getRegionDepth(lua_State* L) { +static int getDepth(lua_State* L) { lua_pushinteger(L, REGION_DEPTH); return 1; } +static int load(lua_State* L) { + //TODO: fill this + return 0; +} + +static int save(lua_State* L) { + //TODO: fill this + return 0; +} + +static int create(lua_State* L) { + //TODO: fill this + return 0; +} + +static int unload(lua_State* L) { + //TODO: fill this + return 0; +} + static const luaL_Reg regionlib[] = { {"settile",setTile}, {"gettile",getTile}, {"getx",getX}, {"gety",getY}, - {"getregionwidth",getRegionWidth}, - {"getregionheight",getRegionHeight}, - {"getregiondepth",getRegionDepth}, + {"getwidth",getWidth}, + {"getheight",getHeight}, + {"getdepth",getDepth}, + {"load",load}, + {"save",save}, + {"create",create}, + {"unload",unload}, {nullptr, nullptr} };