From b269ce5fb97e0c375994bd9eb0bb9e13a3375dc8 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 8 Jun 2014 04:20:07 +1000 Subject: [PATCH] Re-added the lua hooks --- common/map/region_pager.cpp | 61 +++++++++++++++++++++++++++++++++---- common/map/region_pager.hpp | 17 +++++++++-- 2 files changed, 69 insertions(+), 9 deletions(-) diff --git a/common/map/region_pager.cpp b/common/map/region_pager.cpp index 0fafd8f..547b51f 100644 --- a/common/map/region_pager.cpp +++ b/common/map/region_pager.cpp @@ -23,6 +23,8 @@ #include "utility.hpp" +#include + Region::type_t RegionPager::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); @@ -62,13 +64,32 @@ Region* RegionPager::FindRegion(int x, int y) { } Region* RegionPager::LoadRegion(int x, int y) { + //load the region if possible + //snap the coords x = snapToBase(REGION_WIDTH, x); y = snapToBase(REGION_HEIGHT, y); - //load the region if possible - //TODO: Load the region (lua) - return nullptr; + Region* ptr = new Region(x, y); + + //API hook + lua_getglobal(luaState, "map"); + lua_getfield(luaState, -1, "load"); + lua_pushlightuserdata(luaState, ptr); + lua_pushstring(luaState, directory.c_str()); + if (lua_pcall(luaState, 2, 1, 0) != LUA_OK) { + throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) )); + } + if (lua_toboolean(luaState, -1)) { + regionList.push_front(ptr); + } + else { + delete ptr; + ptr = nullptr; + } + lua_pop(luaState, 2); + + return ptr; } Region* RegionPager::SaveRegion(int x, int y) { @@ -79,7 +100,15 @@ Region* RegionPager::SaveRegion(int x, int y) { //find & save the region Region* ptr = FindRegion(x, y); if (ptr) { - //TODO: save the region (lua) + //API hook + lua_getglobal(luaState, "map"); + lua_getfield(luaState, -1, "save"); + lua_pushlightuserdata(luaState, ptr); + 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) )); + } + lua_pop(luaState, 1); } return ptr; } @@ -91,7 +120,16 @@ Region* RegionPager::CreateRegion(int x, int y) { //create and push the object Region* ptr = new Region(x, y); - //TODO: create the region (lua) + + //API hook + lua_getglobal(luaState, "map"); + lua_getfield(luaState, -1, "create"); + lua_pushlightuserdata(luaState, ptr); + if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) { + throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) )); + } + lua_pop(luaState, 1); + regionList.push_front(ptr); return ptr; } @@ -101,14 +139,25 @@ void RegionPager::UnloadRegion(int x, int y) { x = snapToBase(REGION_WIDTH, x); y = snapToBase(REGION_HEIGHT, y); + lua_getglobal(luaState, "map"); + //custom loop, not FindRegion() for (std::list::iterator it = regionList.begin(); it != regionList.end(); /* EMPTY */) { if ((*it)->GetX() == x && (*it)->GetY() == y) { - //TODO: unload the region (lua) + + //API hook + lua_getfield(luaState, -1, "unload"); + lua_pushlightuserdata(luaState, *it); + if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) { + throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) )); + } + delete (*it); it = regionList.erase(it); continue; } ++it; } + + lua_pop(luaState, 1); } diff --git a/common/map/region_pager.hpp b/common/map/region_pager.hpp index 6a1d091..e53bc2c 100644 --- a/common/map/region_pager.hpp +++ b/common/map/region_pager.hpp @@ -24,9 +24,11 @@ #include "region.hpp" -#include +#include "lua/lua.hpp" + +#include +#include -//TODO: add lua interface? class RegionPager { public: RegionPager() = default; @@ -48,8 +50,17 @@ public: //accessors & mutators std::list* GetContainer() { return ®ionList; } -protected: + + std::string SetDirectory(std::string s) { return directory = s; } + std::string GetDirectory() { return directory; } + + lua_State* SetLuaState(lua_State* L) { return luaState = L; } + lua_State* GetLuaState() { return luaState; } + +private: std::list regionList; + std::string directory; + lua_State* luaState = nullptr; }; #endif