From 38b603fc8fc15dfe47f1f9946d54b908c399dcfd Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 28 Mar 2014 03:24:14 +1100 Subject: [PATCH 1/3] Began working on the lua API for the map The basic framework is done. --- client/scenes/in_world.hpp | 2 +- common/makefile | 1 + common/map/map_file_format.cpp | 21 +++++++++- common/map/map_file_format.hpp | 18 ++++++++- common/map/map_generator.cpp | 25 +++++++++++- common/map/map_generator.hpp | 23 ++++++++++- common/script/linit.cpp | 72 ++++++++++++++++++++++++++++++++++ common/script/makefile | 43 ++++++++++++++++++++ common/script/region_api.cpp | 27 +++++++++++++ common/script/region_api.hpp | 30 ++++++++++++++ editor/editor_scene.hpp | 2 +- editor/makefile | 2 +- server/makefile | 2 +- server/server_application.cpp | 7 ++-- server/server_application.hpp | 2 +- 15 files changed, 263 insertions(+), 14 deletions(-) create mode 100644 common/script/linit.cpp create mode 100644 common/script/makefile create mode 100644 common/script/region_api.cpp create mode 100644 common/script/region_api.hpp diff --git a/client/scenes/in_world.hpp b/client/scenes/in_world.hpp index 7fb14c4..c6ecd72 100644 --- a/client/scenes/in_world.hpp +++ b/client/scenes/in_world.hpp @@ -88,7 +88,7 @@ protected: RasterFont font; //map - RegionPager mapPager; + RegionPager mapPager; //UI Button disconnectButton; diff --git a/common/makefile b/common/makefile index c1e25c2..645ac13 100644 --- a/common/makefile +++ b/common/makefile @@ -22,6 +22,7 @@ all: $(OBJ) $(OUT) ar -crs $(OUT) $(OBJ) $(MAKE) -C graphics $(MAKE) -C map + $(MAKE) -C script $(MAKE) -C network $(MAKE) -C ui diff --git a/common/map/map_file_format.cpp b/common/map/map_file_format.cpp index ee7f58d..ac1b79e 100644 --- a/common/map/map_file_format.cpp +++ b/common/map/map_file_format.cpp @@ -21,10 +21,27 @@ */ #include "map_file_format.hpp" -void MapFileFormat::Load(Region** const ptr, int x, int y) { +void DummyFormat::Load(Region** const ptr, int x, int y) { //TODO } -void MapFileFormat::Save(Region* const ptr) { +void DummyFormat::Save(Region* const ptr) { //TODO } +/* +void VerboseFormat::Load(Region** const ptr, int x, int y) { + //TODO +} + +void VerboseFormat::Save(Region* const ptr) { + //TODO +} + +void CompactFormat::Load(Region** const ptr, int x, int y) { + //TODO +} + +void CompactFormat::Save(Region* const ptr) { + //TODO +} +*/ \ No newline at end of file diff --git a/common/map/map_file_format.hpp b/common/map/map_file_format.hpp index 546003d..576dc50 100644 --- a/common/map/map_file_format.hpp +++ b/common/map/map_file_format.hpp @@ -24,7 +24,15 @@ #include "region.hpp" -class MapFileFormat { +class DummyFormat { +public: + void Load(Region** const, int x, int y); + void Save(Region* const); +private: + // +}; +/* +class VerboseFormat { public: void Load(Region** const, int x, int y); void Save(Region* const); @@ -32,4 +40,12 @@ private: // }; +class CompactFormat { +public: + void Load(Region** const, int x, int y); + void Save(Region* const); +private: + // +}; +*/ #endif diff --git a/common/map/map_generator.cpp b/common/map/map_generator.cpp index 2668ca5..d0fbbca 100644 --- a/common/map/map_generator.cpp +++ b/common/map/map_generator.cpp @@ -21,10 +21,31 @@ */ #include "map_generator.hpp" -void MapGenerator::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); } -void MapGenerator::Unload(Region* const ptr) { +void BlankGenerator::Unload(Region* const ptr) { + delete ptr; +} +/* +void PerlinGenerator::Create(Region** const ptr, int width, int height, int depth, int x, int y) { + (*ptr) = new Region(width, height, depth, x, y); +} + +void PerlinGenerator::Unload(Region* const ptr) { + delete ptr; +} +*/ +void LuaGenerator::Create(Region** const ptr, int width, int height, int depth, int x, int y) { + (*ptr) = new Region(width, height, depth, x, y); + + //generate the lua-driven maps + lua_getglobal(state, "CreateRegion"); + lua_pushlightuserdata(state, ptr); + lua_pcall(state, 1, 0, 0); +} + +void LuaGenerator::Unload(Region* const ptr) { delete ptr; } diff --git a/common/map/map_generator.hpp b/common/map/map_generator.hpp index 85121b9..f3eba56 100644 --- a/common/map/map_generator.hpp +++ b/common/map/map_generator.hpp @@ -24,12 +24,33 @@ #include "region.hpp" -class MapGenerator { +#include "lua/lua.hpp" + +class BlankGenerator { public: void Create(Region** const, int width, int height, int depth, int x, int y); void Unload(Region* const); private: // }; +/* +class PerlinGenerator { +public: + void Create(Region** const, int width, int height, int depth, int x, int y); + void Unload(Region* const); +private: + // +}; +*/ +class LuaGenerator { +public: + void Create(Region** const, int width, int height, int depth, int x, int y); + void Unload(Region* const); + + lua_State* SetLuaState(lua_State* L) { return state = L; } + lua_State* GetLuaState() { return state; } +private: + lua_State* state = nullptr; +}; #endif diff --git a/common/script/linit.cpp b/common/script/linit.cpp new file mode 100644 index 0000000..c03a812 --- /dev/null +++ b/common/script/linit.cpp @@ -0,0 +1,72 @@ +/* +** $Id: linit.c,v 1.32 2011/04/08 19:17:36 roberto Exp $ +** Initialization of libraries for lua.c and other clients +** See Copyright Notice in lua.h +*/ + +/* Modified for use in Tortuga, renamed to linit.cpp +*/ + + +/* +** If you embed Lua in your program and need to open the standard +** libraries, call luaL_openlibs in your program. If you need a +** different set of libraries, copy this file to your project and edit +** it to suit your needs. +*/ + + +#define linit_c +#define LUA_LIB + +#include "lua/lua.hpp" +#include "region_api.hpp" + + +/* +** these libs are loaded by lua.c and are readily available to any Lua +** program +*/ +static const luaL_Reg loadedlibs[] = { + /* Standard libs */ + {"_G", luaopen_base}, + {LUA_LOADLIBNAME, luaopen_package}, + {LUA_COLIBNAME, luaopen_coroutine}, + {LUA_TABLIBNAME, luaopen_table}, + {LUA_IOLIBNAME, luaopen_io}, + {LUA_OSLIBNAME, luaopen_os}, + {LUA_STRLIBNAME, luaopen_string}, + {LUA_BITLIBNAME, luaopen_bit32}, + {LUA_MATHLIBNAME, luaopen_math}, + {LUA_DBLIBNAME, luaopen_debug}, + + /* custom libs */ + {LUA_REGIONLIBNAME, luaopen_regionapi}, + + {NULL, NULL} +}; + + +/* +** these libs are preloaded and must be required before used +*/ +static const luaL_Reg preloadedlibs[] = { + {NULL, NULL} +}; + + +LUALIB_API void luaL_openlibs (lua_State *L) { + const luaL_Reg *lib; + /* call open functions from 'loadedlibs' and set results to global table */ + for (lib = loadedlibs; lib->func; lib++) { + luaL_requiref(L, lib->name, lib->func, 1); + lua_pop(L, 1); /* remove lib */ + } + /* add open functions from 'preloadedlibs' into 'package.preload' table */ + luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); + for (lib = preloadedlibs; lib->func; lib++) { + lua_pushcfunction(L, lib->func); + lua_setfield(L, -2, lib->name); + } + lua_pop(L, 1); /* remove _PRELOAD table */ +} \ No newline at end of file diff --git a/common/script/makefile b/common/script/makefile new file mode 100644 index 0000000..c1b2927 --- /dev/null +++ b/common/script/makefile @@ -0,0 +1,43 @@ +#config +INCLUDES+=. .. ../map +LIBS+= +CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES)) +CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES)) + +#source +CXXSRC=$(wildcard *.cpp) +CSRC=$(wildcard *.c) + +#objects +OBJDIR=obj +OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) +OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o)) + +#output +OUTDIR=../.. +OUT=$(addprefix $(OUTDIR)/,libcommon.a) + +#targets +all: $(OBJ) $(OUT) + ar -crs $(OUT) $(OBJ) + +$(OBJ): | $(OBJDIR) + +$(OUT): | $(OUTDIR) + +$(OBJDIR): + mkdir $(OBJDIR) + +$(OUTDIR): + mkdir $(OUTDIR) + +$(OBJDIR)/%.o: %.cpp + $(CXX) $(CXXFLAGS) -c -o $@ $< + +$(OBJDIR)/%.o: %.c + $(CC) $(CFLAGS) -c -o $@ $< + +clean: + $(RM) *.o *.a *.exe + +rebuild: clean all diff --git a/common/script/region_api.cpp b/common/script/region_api.cpp new file mode 100644 index 0000000..d378835 --- /dev/null +++ b/common/script/region_api.cpp @@ -0,0 +1,27 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "region_api.hpp" + +LUAMOD_API int luaopen_regionapi(lua_State* L) { + //TODO: stuff + return 1; +} \ No newline at end of file diff --git a/common/script/region_api.hpp b/common/script/region_api.hpp new file mode 100644 index 0000000..310074d --- /dev/null +++ b/common/script/region_api.hpp @@ -0,0 +1,30 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef REGIONAPI_HPP_ +#define REGIONAPI_HPP_ + +#include "lua/lua.hpp" + +#define LUA_REGIONLIBNAME "region" +LUAMOD_API int luaopen_regionapi(lua_State* L); + +#endif diff --git a/editor/editor_scene.hpp b/editor/editor_scene.hpp index e86fa52..ecfc291 100644 --- a/editor/editor_scene.hpp +++ b/editor/editor_scene.hpp @@ -73,7 +73,7 @@ protected: int x = 0, y = 0; } camera; - RegionPager pager; + RegionPager pager; TileSheet tsheet; }; diff --git a/editor/makefile b/editor/makefile index 7343b2e..f785faa 100644 --- a/editor/makefile +++ b/editor/makefile @@ -1,6 +1,6 @@ #config INCLUDES+=../common ../common/graphics ../common/map ../common/ui -LIBS+=../libcommon.a -lmingw32 -lSDLmain -lSDL +LIBS+=../libcommon.a -lmingw32 -lSDLmain -lSDL -llua CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES)) CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES)) diff --git a/server/makefile b/server/makefile index 6af6c60..9686a1c 100644 --- a/server/makefile +++ b/server/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. ../common ../common/map ../common/network +INCLUDES+=. ../common ../common/map ../common/script ../common/network LIBS+=../libcommon.a -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL -llua -lsqlite3 CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES)) CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES)) diff --git a/server/server_application.cpp b/server/server_application.cpp index a126280..9aeed88 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -100,6 +100,7 @@ void ServerApplication::Init(int argc, char** argv) { mapPager.SetRegionWidth(REGION_WIDTH); mapPager.SetRegionHeight(REGION_HEIGHT); mapPager.SetRegionDepth(REGION_DEPTH); + mapPager.GetGenerator()->SetLuaState(luaState); //TODO: pass args to the generator & format as needed //NOTE: I might need to rearrange the init process so that lua & SQL can interact // with the map system as needed. @@ -212,7 +213,7 @@ void ServerApplication::HandleJoinRequest(NetworkPacket packet) { //finished this routine clientCounter++; - cout << "connect, total: " << clientMap.size() << endl; + cout << "Connect, total: " << clientMap.size() << endl; } void ServerApplication::HandleDisconnect(NetworkPacket packet) { @@ -239,7 +240,7 @@ void ServerApplication::HandleDisconnect(NetworkPacket packet) { }); //finished this routine - cout << "disconnect, total: " << clientMap.size() << endl; + cout << "Disconnect, total: " << clientMap.size() << endl; } void ServerApplication::HandleSynchronize(NetworkPacket packet) { @@ -270,7 +271,7 @@ void ServerApplication::HandleShutdown(NetworkPacket packet) { PumpPacket(packet); //finished this routine - cout << "shutting down" << endl; + cout << "Shutdown signal accepted" << endl; } void ServerApplication::HandlePlayerNew(NetworkPacket packet) { diff --git a/server/server_application.hpp b/server/server_application.hpp index 3d43ca4..4d969a0 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -85,7 +85,7 @@ private: void PumpPacket(NetworkPacket); //maps - RegionPager mapPager; + RegionPager mapPager; //networking UDPNetworkUtility network; From 4cff57fe71577c85adfd287a61008b09da9d2e1b Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 28 Mar 2014 04:11:34 +1100 Subject: [PATCH 2/3] Established a connection between the Region objects and lua --- common/map/map_generator.cpp | 2 +- common/script/region_api.cpp | 48 ++++++++++++++++++++++++++++++++++- common/script/region_api.hpp | 2 +- rsc/scripts/setup_server.lua | 6 ++++- server/server_application.cpp | 4 +++ 5 files changed, 58 insertions(+), 4 deletions(-) diff --git a/common/map/map_generator.cpp b/common/map/map_generator.cpp index d0fbbca..8f7a9ad 100644 --- a/common/map/map_generator.cpp +++ b/common/map/map_generator.cpp @@ -42,7 +42,7 @@ void LuaGenerator::Create(Region** const ptr, int width, int height, int depth, //generate the lua-driven maps lua_getglobal(state, "CreateRegion"); - lua_pushlightuserdata(state, ptr); + lua_pushlightuserdata(state, *ptr); lua_pcall(state, 1, 0, 0); } diff --git a/common/script/region_api.cpp b/common/script/region_api.cpp index d378835..2f5a3c1 100644 --- a/common/script/region_api.cpp +++ b/common/script/region_api.cpp @@ -21,7 +21,53 @@ */ #include "region_api.hpp" +#include "region.hpp" + +static int setTile(lua_State* L) { +// Region* ptr = lua_touserdata(L, 1); +// ptr->SetTile +} + +static int getTile(lua_State* L) { + //TODO +} + +static int getWidth(lua_State* L) { + //TODO +} + +static int getHeight(lua_State* L) { + //TODO +} + +static int getDepth(lua_State* L) { + //TODO +} + +static int getX(lua_State* L) { + Region* ptr = (Region*)lua_touserdata(L, 1); + lua_pushinteger(L, ptr->GetX()); + return 1; +} + +static int getY(lua_State* L) { + Region* ptr = (Region*)lua_touserdata(L, 1); + lua_pushinteger(L, ptr->GetY()); + return 1; +} + +static const luaL_Reg regionlib[] = { + {"SetTile",setTile}, + {"GetTile",getTile}, + {"GetWidth",getWidth}, + {"GetHeight",getHeight}, + {"GetDepth",getDepth}, + {"GetX",getX}, + {"GetY",getY}, + {nullptr, nullptr} +}; + LUAMOD_API int luaopen_regionapi(lua_State* L) { - //TODO: stuff + luaL_newlib(L, regionlib); return 1; } \ No newline at end of file diff --git a/common/script/region_api.hpp b/common/script/region_api.hpp index 310074d..f193950 100644 --- a/common/script/region_api.hpp +++ b/common/script/region_api.hpp @@ -24,7 +24,7 @@ #include "lua/lua.hpp" -#define LUA_REGIONLIBNAME "region" +#define LUA_REGIONLIBNAME "Region" LUAMOD_API int luaopen_regionapi(lua_State* L); #endif diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index a2cf14a..448b25f 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -1 +1,5 @@ -print("Lua script check OK") \ No newline at end of file +print("Lua script check OK (./rsc)") + +function CreateRegion(r) + print(Region.GetX(r), Region.GetY(r)) +end \ No newline at end of file diff --git a/server/server_application.cpp b/server/server_application.cpp index 9aeed88..d39223b 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -110,6 +110,10 @@ void ServerApplication::Init(int argc, char** argv) { //finalize the startup cout << "Startup completed successfully" << endl; + + //debugging + mapPager.GetRegion(0,0); + mapPager.GetRegion(128,256); } void ServerApplication::Loop() { From a5b68cf1fd092fabf03a4f0817cb9aeb4f333d4b Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 31 Mar 2014 01:26:45 +1100 Subject: [PATCH 3/3] 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. --- common/map/map_file_format.cpp | 36 ++++++++++++++++++++++++++++-- common/map/map_file_format.hpp | 40 +++++++++++++++++++++++++++++----- common/map/map_generator.cpp | 23 ++++++++++++++++--- common/map/region_pager.hpp | 13 +++++++++-- common/script/region_api.cpp | 30 ++++++++++++++++++++----- rsc/scripts/setup_server.lua | 20 ++++++++++++++--- server/server_application.cpp | 6 +++-- server/server_application.hpp | 2 +- 8 files changed, 145 insertions(+), 25 deletions(-) diff --git a/common/map/map_file_format.cpp b/common/map/map_file_format.cpp index ac1b79e..879d14e 100644 --- a/common/map/map_file_format.cpp +++ b/common/map/map_file_format.cpp @@ -21,7 +21,9 @@ */ #include "map_file_format.hpp" -void DummyFormat::Load(Region** const ptr, int x, int y) { +#include + +void DummyFormat::Load(Region** const ptr, int width, int height, int depth, int x, int y) { //TODO } @@ -44,4 +46,34 @@ void CompactFormat::Load(Region** const ptr, int x, int y) { void CompactFormat::Save(Region* const ptr) { //TODO } -*/ \ No newline at end of file +*/ +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); +} \ No newline at end of file diff --git a/common/map/map_file_format.hpp b/common/map/map_file_format.hpp index 576dc50..50345e5 100644 --- a/common/map/map_file_format.hpp +++ b/common/map/map_file_format.hpp @@ -24,28 +24,56 @@ #include "region.hpp" +#include "lua/lua.hpp" + +#include + class DummyFormat { 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); + + std::string SetSaveDir(std::string s) { return saveDir = s; } + std::string GetSaveDir() { return saveDir; } private: - // + std::string saveDir; }; /* class VerboseFormat { 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); + + std::string SetSaveDir(std::string s) { return saveDir = s; } + std::string GetSaveDir() { return saveDir; } private: - // + std::string saveDir; }; class CompactFormat { 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); + + std::string SetSaveDir(std::string s) { return saveDir = s; } + std::string GetSaveDir() { return saveDir; } 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 diff --git a/common/map/map_generator.cpp b/common/map/map_generator.cpp index 8f7a9ad..eef4b16 100644 --- a/common/map/map_generator.cpp +++ b/common/map/map_generator.cpp @@ -21,6 +21,8 @@ */ #include "map_generator.hpp" +#include + void BlankGenerator::Create(Region** const ptr, int width, int height, int depth, int x, int 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) { + //something to work on (*ptr) = new Region(width, height, depth, x, y); - //generate the lua-driven maps - lua_getglobal(state, "CreateRegion"); + //API hook + lua_getglobal(state, "Region"); + lua_getfield(state, -1, "Create"); 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) { + //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; } diff --git a/common/map/region_pager.hpp b/common/map/region_pager.hpp index 46fc612..bbf1d42 100644 --- a/common/map/region_pager.hpp +++ b/common/map/region_pager.hpp @@ -43,6 +43,7 @@ public: virtual Region* SaveRegion(int x, int y) = 0; virtual Region* CreateRegion(int x, int y) = 0; virtual void UnloadRegion(int x, int y) = 0; + virtual void UnloadAll() = 0; //accessors //NOTE: don't change the sizes mid-program, it will cause issues @@ -69,7 +70,9 @@ public: { //EMPTY } - ~RegionPager() = default; + ~RegionPager() { + UnloadAll(); + } Region* LoadRegion(int x, int y) { //snap the coords @@ -78,7 +81,7 @@ public: //load the region if possible Region* ptr = nullptr; - format.Load(&ptr, x, y); + format.Load(&ptr, regionWidth, regionHeight, regionDepth, x, y); if (ptr) { regionList.push_back(ptr); return ptr; @@ -130,6 +133,12 @@ public: ++it; } } + void UnloadAll() { + for (auto& it : regionList) { + generator.Unload(it); + } + regionList.clear(); + } //accessors MapGenerator* GetGenerator() { return &generator; } diff --git a/common/script/region_api.cpp b/common/script/region_api.cpp index 2f5a3c1..48380d3 100644 --- a/common/script/region_api.cpp +++ b/common/script/region_api.cpp @@ -24,24 +24,34 @@ #include "region.hpp" static int setTile(lua_State* L) { -// Region* ptr = lua_touserdata(L, 1); -// ptr->SetTile + Region* ptr = (Region*)lua_touserdata(L, 1); + 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) { - //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) { - //TODO + Region* ptr = (Region*)lua_touserdata(L, 1); + lua_pushinteger(L, ptr->GetWidth()); + return 1; } 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) { - //TODO + Region* ptr = (Region*)lua_touserdata(L, 1); + lua_pushinteger(L, ptr->GetDepth()); + return 1; } static int getX(lua_State* L) { @@ -56,6 +66,10 @@ static int getY(lua_State* L) { return 1; } +static int dummy(lua_State* L) { + return 0; +} + static const luaL_Reg regionlib[] = { {"SetTile",setTile}, {"GetTile",getTile}, @@ -64,6 +78,10 @@ static const luaL_Reg regionlib[] = { {"GetDepth",getDepth}, {"GetX",getX}, {"GetY",getY}, + {"Create", dummy}, + {"Unload", dummy}, + {"Load", dummy}, + {"Save", dummy}, {nullptr, nullptr} }; diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 448b25f..4daf1f4 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -1,5 +1,19 @@ print("Lua script check OK (./rsc)") -function CreateRegion(r) - print(Region.GetX(r), Region.GetY(r)) -end \ No newline at end of file +function Region.Create(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 diff --git a/server/server_application.cpp b/server/server_application.cpp index d39223b..9466ce8 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -101,6 +101,8 @@ void ServerApplication::Init(int argc, char** argv) { mapPager.SetRegionHeight(REGION_HEIGHT); mapPager.SetRegionDepth(REGION_DEPTH); mapPager.GetGenerator()->SetLuaState(luaState); + mapPager.GetFormat()->SetLuaState(luaState); + mapPager.GetFormat()->SetSaveDir("save/mapname/"); //TODO: pass args to the generator & format as needed //NOTE: I might need to rearrange the init process so that lua & SQL can interact // with the map system as needed. @@ -112,8 +114,7 @@ void ServerApplication::Init(int argc, char** argv) { cout << "Startup completed successfully" << endl; //debugging - mapPager.GetRegion(0,0); - mapPager.GetRegion(128,256); + // } void ServerApplication::Loop() { @@ -136,6 +137,7 @@ void ServerApplication::Loop() { void ServerApplication::Quit() { cout << "Shutting down" << endl; //empty the members + mapPager.UnloadAll(); //TODO: player manager //TODO: client manager diff --git a/server/server_application.hpp b/server/server_application.hpp index 4d969a0..d3d30fa 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -85,7 +85,7 @@ private: void PumpPacket(NetworkPacket); //maps - RegionPager mapPager; + RegionPager mapPager; //networking UDPNetworkUtility network;