Re-added the lua hooks

This commit is contained in:
Kayne Ruse
2014-06-08 04:20:07 +10:00
parent f034c32c38
commit b269ce5fb9
2 changed files with 69 additions and 9 deletions
+55 -6
View File
@@ -23,6 +23,8 @@
#include "utility.hpp"
#include <stdexcept>
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<Region*>::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);
}
+14 -3
View File
@@ -24,9 +24,11 @@
#include "region.hpp"
#include <list>
#include "lua/lua.hpp"
#include <list>
#include <string>
//TODO: add lua interface?
class RegionPager {
public:
RegionPager() = default;
@@ -48,8 +50,17 @@ public:
//accessors & mutators
std::list<Region*>* GetContainer() { return &regionList; }
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<Region*> regionList;
std::string directory;
lua_State* luaState = nullptr;
};
#endif