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);
}