Expaneded the lua API for Regions
I've added lua hooks for both pager functor classes. Hopefully, I haven't missed any corner cases, because it took me a while to hunt everything down. One issue is that the map's save directory needs to be set in the Format class, but it'll do for now. I'll review this again when I've got more than one map running at one time. There should be enough here for a lua-driven map generator to be implemented, even if it's a bit rough. I think I'll test this out in the editor eventually, but getting the base branch's network map code going comes first. The current process is extremely convulted, so I need to document everything that I've done so far, including C++ and lua functions.
This commit is contained in:
@@ -21,7 +21,9 @@
|
|||||||
*/
|
*/
|
||||||
#include "map_file_format.hpp"
|
#include "map_file_format.hpp"
|
||||||
|
|
||||||
void DummyFormat::Load(Region** const ptr, int x, int y) {
|
#include <stdexcept>
|
||||||
|
|
||||||
|
void DummyFormat::Load(Region** const ptr, int width, int height, int depth, int x, int y) {
|
||||||
//TODO
|
//TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,3 +47,33 @@ void CompactFormat::Save(Region* const ptr) {
|
|||||||
//TODO
|
//TODO
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
void LuaFormat::Load(Region** const ptr, int width, int height, int depth, int x, int y) {
|
||||||
|
//something to load into
|
||||||
|
(*ptr) = new Region(width, height, depth, x, y);
|
||||||
|
|
||||||
|
//API hook
|
||||||
|
lua_getglobal(state, "Region");
|
||||||
|
lua_getfield(state, -1, "Load");
|
||||||
|
lua_pushlightuserdata(state, *ptr);
|
||||||
|
lua_pushstring(state, saveDir.c_str());
|
||||||
|
if (lua_pcall(state, 2, 1, 0) != LUA_OK) {
|
||||||
|
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) ));
|
||||||
|
}
|
||||||
|
if (lua_toboolean(state, -1) == false) {
|
||||||
|
delete (*ptr);
|
||||||
|
(*ptr) = nullptr;
|
||||||
|
}
|
||||||
|
lua_pop(state, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void LuaFormat::Save(Region* const ptr) {
|
||||||
|
//API hook
|
||||||
|
lua_getglobal(state, "Region");
|
||||||
|
lua_getfield(state, -1, "Save");
|
||||||
|
lua_pushlightuserdata(state, ptr);
|
||||||
|
lua_pushstring(state, saveDir.c_str());
|
||||||
|
if (lua_pcall(state, 2, 0, 0) != LUA_OK) {
|
||||||
|
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) ));
|
||||||
|
}
|
||||||
|
lua_pop(state, 1);
|
||||||
|
}
|
||||||
@@ -24,28 +24,56 @@
|
|||||||
|
|
||||||
#include "region.hpp"
|
#include "region.hpp"
|
||||||
|
|
||||||
|
#include "lua/lua.hpp"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
class DummyFormat {
|
class DummyFormat {
|
||||||
public:
|
public:
|
||||||
void Load(Region** const, int x, int y);
|
void Load(Region** const, int width, int height, int depth, int x, int y);
|
||||||
void Save(Region* const);
|
void Save(Region* const);
|
||||||
|
|
||||||
|
std::string SetSaveDir(std::string s) { return saveDir = s; }
|
||||||
|
std::string GetSaveDir() { return saveDir; }
|
||||||
private:
|
private:
|
||||||
//
|
std::string saveDir;
|
||||||
};
|
};
|
||||||
/*
|
/*
|
||||||
class VerboseFormat {
|
class VerboseFormat {
|
||||||
public:
|
public:
|
||||||
void Load(Region** const, int x, int y);
|
void Load(Region** const, int width, int height, int depth, int x, int y);
|
||||||
void Save(Region* const);
|
void Save(Region* const);
|
||||||
|
|
||||||
|
std::string SetSaveDir(std::string s) { return saveDir = s; }
|
||||||
|
std::string GetSaveDir() { return saveDir; }
|
||||||
private:
|
private:
|
||||||
//
|
std::string saveDir;
|
||||||
};
|
};
|
||||||
|
|
||||||
class CompactFormat {
|
class CompactFormat {
|
||||||
public:
|
public:
|
||||||
void Load(Region** const, int x, int y);
|
void Load(Region** const, int width, int height, int depth, int x, int y);
|
||||||
void Save(Region* const);
|
void Save(Region* const);
|
||||||
|
|
||||||
|
std::string SetSaveDir(std::string s) { return saveDir = s; }
|
||||||
|
std::string GetSaveDir() { return saveDir; }
|
||||||
private:
|
private:
|
||||||
//
|
std::string saveDir;
|
||||||
};
|
};
|
||||||
*/
|
*/
|
||||||
|
class LuaFormat {
|
||||||
|
public:
|
||||||
|
void Load(Region** const, int width, int height, int depth, int x, int y);
|
||||||
|
void Save(Region* const);
|
||||||
|
|
||||||
|
std::string SetSaveDir(std::string s) { return saveDir = s; }
|
||||||
|
std::string GetSaveDir() { return saveDir; }
|
||||||
|
|
||||||
|
lua_State* SetLuaState(lua_State* L) { return state = L; }
|
||||||
|
lua_State* GetLuaState() { return state; }
|
||||||
|
private:
|
||||||
|
std::string saveDir;
|
||||||
|
lua_State* state = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -21,6 +21,8 @@
|
|||||||
*/
|
*/
|
||||||
#include "map_generator.hpp"
|
#include "map_generator.hpp"
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
void BlankGenerator::Create(Region** const ptr, int width, int height, int depth, int x, int y) {
|
void BlankGenerator::Create(Region** const ptr, int width, int height, int depth, int x, int y) {
|
||||||
(*ptr) = new Region(width, height, depth, x, y);
|
(*ptr) = new Region(width, height, depth, x, y);
|
||||||
}
|
}
|
||||||
@@ -38,14 +40,29 @@ void PerlinGenerator::Unload(Region* const ptr) {
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
void LuaGenerator::Create(Region** const ptr, int width, int height, int depth, int x, int y) {
|
void LuaGenerator::Create(Region** const ptr, int width, int height, int depth, int x, int y) {
|
||||||
|
//something to work on
|
||||||
(*ptr) = new Region(width, height, depth, x, y);
|
(*ptr) = new Region(width, height, depth, x, y);
|
||||||
|
|
||||||
//generate the lua-driven maps
|
//API hook
|
||||||
lua_getglobal(state, "CreateRegion");
|
lua_getglobal(state, "Region");
|
||||||
|
lua_getfield(state, -1, "Create");
|
||||||
lua_pushlightuserdata(state, *ptr);
|
lua_pushlightuserdata(state, *ptr);
|
||||||
lua_pcall(state, 1, 0, 0);
|
if (lua_pcall(state, 1, 0, 0) != LUA_OK) {
|
||||||
|
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) ));
|
||||||
|
}
|
||||||
|
lua_pop(state, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LuaGenerator::Unload(Region* const ptr) {
|
void LuaGenerator::Unload(Region* const ptr) {
|
||||||
|
//API hook
|
||||||
|
lua_getglobal(state, "Region");
|
||||||
|
lua_getfield(state, -1, "Unload");
|
||||||
|
lua_pushlightuserdata(state, ptr);
|
||||||
|
if (lua_pcall(state, 1, 0, 0) != LUA_OK) {
|
||||||
|
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) ));
|
||||||
|
}
|
||||||
|
lua_pop(state, 1);
|
||||||
|
|
||||||
|
//clean up the memory
|
||||||
delete ptr;
|
delete ptr;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ public:
|
|||||||
virtual Region* SaveRegion(int x, int y) = 0;
|
virtual Region* SaveRegion(int x, int y) = 0;
|
||||||
virtual Region* CreateRegion(int x, int y) = 0;
|
virtual Region* CreateRegion(int x, int y) = 0;
|
||||||
virtual void UnloadRegion(int x, int y) = 0;
|
virtual void UnloadRegion(int x, int y) = 0;
|
||||||
|
virtual void UnloadAll() = 0;
|
||||||
|
|
||||||
//accessors
|
//accessors
|
||||||
//NOTE: don't change the sizes mid-program, it will cause issues
|
//NOTE: don't change the sizes mid-program, it will cause issues
|
||||||
@@ -69,7 +70,9 @@ public:
|
|||||||
{
|
{
|
||||||
//EMPTY
|
//EMPTY
|
||||||
}
|
}
|
||||||
~RegionPager() = default;
|
~RegionPager() {
|
||||||
|
UnloadAll();
|
||||||
|
}
|
||||||
|
|
||||||
Region* LoadRegion(int x, int y) {
|
Region* LoadRegion(int x, int y) {
|
||||||
//snap the coords
|
//snap the coords
|
||||||
@@ -78,7 +81,7 @@ public:
|
|||||||
|
|
||||||
//load the region if possible
|
//load the region if possible
|
||||||
Region* ptr = nullptr;
|
Region* ptr = nullptr;
|
||||||
format.Load(&ptr, x, y);
|
format.Load(&ptr, regionWidth, regionHeight, regionDepth, x, y);
|
||||||
if (ptr) {
|
if (ptr) {
|
||||||
regionList.push_back(ptr);
|
regionList.push_back(ptr);
|
||||||
return ptr;
|
return ptr;
|
||||||
@@ -130,6 +133,12 @@ public:
|
|||||||
++it;
|
++it;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
void UnloadAll() {
|
||||||
|
for (auto& it : regionList) {
|
||||||
|
generator.Unload(it);
|
||||||
|
}
|
||||||
|
regionList.clear();
|
||||||
|
}
|
||||||
|
|
||||||
//accessors
|
//accessors
|
||||||
MapGenerator* GetGenerator() { return &generator; }
|
MapGenerator* GetGenerator() { return &generator; }
|
||||||
|
|||||||
@@ -24,24 +24,34 @@
|
|||||||
#include "region.hpp"
|
#include "region.hpp"
|
||||||
|
|
||||||
static int setTile(lua_State* L) {
|
static int setTile(lua_State* L) {
|
||||||
// Region* ptr = lua_touserdata(L, 1);
|
Region* ptr = (Region*)lua_touserdata(L, 1);
|
||||||
// ptr->SetTile
|
ptr->SetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1, lua_tointeger(L, 5));
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int getTile(lua_State* L) {
|
static int getTile(lua_State* L) {
|
||||||
//TODO
|
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);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int getWidth(lua_State* L) {
|
static int getWidth(lua_State* L) {
|
||||||
//TODO
|
Region* ptr = (Region*)lua_touserdata(L, 1);
|
||||||
|
lua_pushinteger(L, ptr->GetWidth());
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int getHeight(lua_State* L) {
|
static int getHeight(lua_State* L) {
|
||||||
//TODO
|
Region* ptr = (Region*)lua_touserdata(L, 1);
|
||||||
|
lua_pushinteger(L, ptr->GetHeight());
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int getDepth(lua_State* L) {
|
static int getDepth(lua_State* L) {
|
||||||
//TODO
|
Region* ptr = (Region*)lua_touserdata(L, 1);
|
||||||
|
lua_pushinteger(L, ptr->GetDepth());
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int getX(lua_State* L) {
|
static int getX(lua_State* L) {
|
||||||
@@ -56,6 +66,10 @@ static int getY(lua_State* L) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int dummy(lua_State* L) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static const luaL_Reg regionlib[] = {
|
static const luaL_Reg regionlib[] = {
|
||||||
{"SetTile",setTile},
|
{"SetTile",setTile},
|
||||||
{"GetTile",getTile},
|
{"GetTile",getTile},
|
||||||
@@ -64,6 +78,10 @@ static const luaL_Reg regionlib[] = {
|
|||||||
{"GetDepth",getDepth},
|
{"GetDepth",getDepth},
|
||||||
{"GetX",getX},
|
{"GetX",getX},
|
||||||
{"GetY",getY},
|
{"GetY",getY},
|
||||||
|
{"Create", dummy},
|
||||||
|
{"Unload", dummy},
|
||||||
|
{"Load", dummy},
|
||||||
|
{"Save", dummy},
|
||||||
{nullptr, nullptr}
|
{nullptr, nullptr}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,19 @@
|
|||||||
print("Lua script check OK (./rsc)")
|
print("Lua script check OK (./rsc)")
|
||||||
|
|
||||||
function CreateRegion(r)
|
function Region.Create(r)
|
||||||
print(Region.GetX(r), Region.GetY(r))
|
print("Region:Create(r", Region.GetX(r), Region.GetY(r), ")")
|
||||||
|
end
|
||||||
|
|
||||||
|
function Region.Unload(r)
|
||||||
|
print("Region:Unload(r", Region.GetX(r), Region.GetY(r), ")")
|
||||||
|
end
|
||||||
|
|
||||||
|
--return true if file loaded, otherwise return false
|
||||||
|
function Region.Load(r, saveDir)
|
||||||
|
print("Region:Load(r,", saveDir, Region.GetX(r), Region.GetY(r), ")")
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
function Region.Save(r, saveDir)
|
||||||
|
print("Region:Save(r,", saveDir, Region.GetX(r), Region.GetY(r), ")")
|
||||||
end
|
end
|
||||||
@@ -101,6 +101,8 @@ void ServerApplication::Init(int argc, char** argv) {
|
|||||||
mapPager.SetRegionHeight(REGION_HEIGHT);
|
mapPager.SetRegionHeight(REGION_HEIGHT);
|
||||||
mapPager.SetRegionDepth(REGION_DEPTH);
|
mapPager.SetRegionDepth(REGION_DEPTH);
|
||||||
mapPager.GetGenerator()->SetLuaState(luaState);
|
mapPager.GetGenerator()->SetLuaState(luaState);
|
||||||
|
mapPager.GetFormat()->SetLuaState(luaState);
|
||||||
|
mapPager.GetFormat()->SetSaveDir("save/mapname/");
|
||||||
//TODO: pass args to the generator & format as needed
|
//TODO: pass args to the generator & format as needed
|
||||||
//NOTE: I might need to rearrange the init process so that lua & SQL can interact
|
//NOTE: I might need to rearrange the init process so that lua & SQL can interact
|
||||||
// with the map system as needed.
|
// with the map system as needed.
|
||||||
@@ -112,8 +114,7 @@ void ServerApplication::Init(int argc, char** argv) {
|
|||||||
cout << "Startup completed successfully" << endl;
|
cout << "Startup completed successfully" << endl;
|
||||||
|
|
||||||
//debugging
|
//debugging
|
||||||
mapPager.GetRegion(0,0);
|
//
|
||||||
mapPager.GetRegion(128,256);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerApplication::Loop() {
|
void ServerApplication::Loop() {
|
||||||
@@ -136,6 +137,7 @@ void ServerApplication::Loop() {
|
|||||||
void ServerApplication::Quit() {
|
void ServerApplication::Quit() {
|
||||||
cout << "Shutting down" << endl;
|
cout << "Shutting down" << endl;
|
||||||
//empty the members
|
//empty the members
|
||||||
|
mapPager.UnloadAll();
|
||||||
//TODO: player manager
|
//TODO: player manager
|
||||||
//TODO: client manager
|
//TODO: client manager
|
||||||
|
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ private:
|
|||||||
void PumpPacket(NetworkPacket);
|
void PumpPacket(NetworkPacket);
|
||||||
|
|
||||||
//maps
|
//maps
|
||||||
RegionPager<LuaGenerator, DummyFormat> mapPager;
|
RegionPager<LuaGenerator, LuaFormat> mapPager;
|
||||||
|
|
||||||
//networking
|
//networking
|
||||||
UDPNetworkUtility network;
|
UDPNetworkUtility network;
|
||||||
|
|||||||
Reference in New Issue
Block a user