Re-added the lua hooks
This commit is contained in:
@@ -23,6 +23,8 @@
|
|||||||
|
|
||||||
#include "utility.hpp"
|
#include "utility.hpp"
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
Region::type_t RegionPager::SetTile(int x, int y, int z, Region::type_t v) {
|
Region::type_t RegionPager::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);
|
||||||
@@ -62,13 +64,32 @@ Region* RegionPager::FindRegion(int x, int y) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Region* RegionPager::LoadRegion(int x, int y) {
|
Region* RegionPager::LoadRegion(int x, int y) {
|
||||||
|
//load the region if possible
|
||||||
|
|
||||||
//snap the coords
|
//snap the coords
|
||||||
x = snapToBase(REGION_WIDTH, x);
|
x = snapToBase(REGION_WIDTH, x);
|
||||||
y = snapToBase(REGION_HEIGHT, y);
|
y = snapToBase(REGION_HEIGHT, y);
|
||||||
|
|
||||||
//load the region if possible
|
Region* ptr = new Region(x, y);
|
||||||
//TODO: Load the region (lua)
|
|
||||||
return nullptr;
|
//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) {
|
Region* RegionPager::SaveRegion(int x, int y) {
|
||||||
@@ -79,7 +100,15 @@ Region* RegionPager::SaveRegion(int x, int y) {
|
|||||||
//find & save the region
|
//find & save the region
|
||||||
Region* ptr = FindRegion(x, y);
|
Region* ptr = FindRegion(x, y);
|
||||||
if (ptr) {
|
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;
|
return ptr;
|
||||||
}
|
}
|
||||||
@@ -91,7 +120,16 @@ Region* RegionPager::CreateRegion(int x, int y) {
|
|||||||
|
|
||||||
//create and push the object
|
//create and push the object
|
||||||
Region* ptr = new Region(x, y);
|
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);
|
regionList.push_front(ptr);
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
@@ -101,14 +139,25 @@ void RegionPager::UnloadRegion(int x, int y) {
|
|||||||
x = snapToBase(REGION_WIDTH, x);
|
x = snapToBase(REGION_WIDTH, x);
|
||||||
y = snapToBase(REGION_HEIGHT, y);
|
y = snapToBase(REGION_HEIGHT, y);
|
||||||
|
|
||||||
|
lua_getglobal(luaState, "map");
|
||||||
|
|
||||||
//custom loop, not FindRegion()
|
//custom loop, not FindRegion()
|
||||||
for (std::list<Region*>::iterator it = regionList.begin(); it != regionList.end(); /* EMPTY */) {
|
for (std::list<Region*>::iterator it = regionList.begin(); it != regionList.end(); /* EMPTY */) {
|
||||||
if ((*it)->GetX() == x && (*it)->GetY() == y) {
|
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);
|
delete (*it);
|
||||||
it = regionList.erase(it);
|
it = regionList.erase(it);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
++it;
|
++it;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lua_pop(luaState, 1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,9 +24,11 @@
|
|||||||
|
|
||||||
#include "region.hpp"
|
#include "region.hpp"
|
||||||
|
|
||||||
#include <list>
|
#include "lua/lua.hpp"
|
||||||
|
|
||||||
|
#include <list>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
//TODO: add lua interface?
|
|
||||||
class RegionPager {
|
class RegionPager {
|
||||||
public:
|
public:
|
||||||
RegionPager() = default;
|
RegionPager() = default;
|
||||||
@@ -48,8 +50,17 @@ public:
|
|||||||
|
|
||||||
//accessors & mutators
|
//accessors & mutators
|
||||||
std::list<Region*>* GetContainer() { return ®ionList; }
|
std::list<Region*>* 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<Region*> regionList;
|
std::list<Region*> regionList;
|
||||||
|
std::string directory;
|
||||||
|
lua_State* luaState = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user