From f034c32c384483f437156d2c343d7a0579951588 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 8 Jun 2014 04:01:09 +1000 Subject: [PATCH 1/8] Collapsed the pager into a single file, removing lua hooks I need to re-add the lua hooks, but it'll be easy. --- common/map/map_allocator.cpp | 60 ---------------- common/map/map_allocator.hpp | 48 ------------- common/map/map_file_format.cpp | 66 ----------------- common/map/map_file_format.hpp | 62 ---------------- common/map/region_pager.cpp | 61 ++++++++++++++-- common/map/region_pager.hpp | 96 +++---------------------- common/network/serial/serial_region.cpp | 8 +-- common/script/map_api.cpp | 6 +- server/room_data.hpp | 4 +- server/room_manager.hpp | 1 - server/server_application.cpp | 1 + todo.txt | 1 - 12 files changed, 68 insertions(+), 346 deletions(-) delete mode 100644 common/map/map_allocator.cpp delete mode 100644 common/map/map_allocator.hpp delete mode 100644 common/map/map_file_format.cpp delete mode 100644 common/map/map_file_format.hpp diff --git a/common/map/map_allocator.cpp b/common/map/map_allocator.cpp deleted file mode 100644 index d57351c..0000000 --- a/common/map/map_allocator.cpp +++ /dev/null @@ -1,60 +0,0 @@ -/* 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 "map_allocator.hpp" - -#include - -void BlankAllocator::Create(Region** const ptr, int x, int y) { - (*ptr) = new Region(x, y); -} - -void BlankAllocator::Unload(Region* const ptr) { - delete ptr; -} - -void LuaAllocator::Create(Region** const ptr, int x, int y) { - //something to work on - (*ptr) = new Region(x, y); - - //API hook - lua_getglobal(state, "map"); - lua_getfield(state, -1, "create"); - 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); -} - -void LuaAllocator::Unload(Region* const ptr) { - //API hook - lua_getglobal(state, "map"); - 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/map_allocator.hpp b/common/map/map_allocator.hpp deleted file mode 100644 index 1efeaf8..0000000 --- a/common/map/map_allocator.hpp +++ /dev/null @@ -1,48 +0,0 @@ -/* 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 MAPALLOCATOR_HPP_ -#define MAPALLOCATOR_HPP_ - -#include "region.hpp" - -#include "lua/lua.hpp" - -class BlankAllocator { -public: - void Create(Region** const, int x, int y); - void Unload(Region* const); -private: - // -}; - -class LuaAllocator { -public: - void Create(Region** const, 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/map/map_file_format.cpp b/common/map/map_file_format.cpp deleted file mode 100644 index b4366e4..0000000 --- a/common/map/map_file_format.cpp +++ /dev/null @@ -1,66 +0,0 @@ -/* 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 "map_file_format.hpp" - -#include - -void DummyFormat::Load(Region** const ptr, int x, int y) { - //EMPTY -} - -void DummyFormat::Save(Region* const ptr) { - //EMPTY -} - -void LuaFormat::Load(Region** const ptr, int x, int y) { - //something to load into - - if (!(*ptr)) { - (*ptr) = new Region(x, y); - } - - //API hook - lua_getglobal(state, "map"); - 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, "map"); - 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 deleted file mode 100644 index 75f5204..0000000 --- a/common/map/map_file_format.hpp +++ /dev/null @@ -1,62 +0,0 @@ -/* 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 MAPFILEFORMAT_HPP_ -#define MAPFILEFORMAT_HPP_ - -#include "region.hpp" - -#include "lua/lua.hpp" - -#include - -//TODO: I'm unhappy with using this system, there needs to be a way to handle saving/loading better - -class DummyFormat { -public: - void Load(Region** const, 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; -}; - -//TODO: verbose save file format -//TODO: compact save file format - -class LuaFormat { -public: - void Load(Region** const, 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/region_pager.cpp b/common/map/region_pager.cpp index 90664dd..0fafd8f 100644 --- a/common/map/region_pager.cpp +++ b/common/map/region_pager.cpp @@ -23,17 +23,17 @@ #include "utility.hpp" -Region::type_t RegionPagerBase::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); return ptr->SetTile(x - ptr->GetX(), y - ptr->GetY(), z, v); } -Region::type_t RegionPagerBase::GetTile(int x, int y, int z) { +Region::type_t RegionPager::GetTile(int x, int y, int z) { Region* ptr = GetRegion(x, y); return ptr->GetTile(x - ptr->GetX(), y - ptr->GetY(), z); } -Region* RegionPagerBase::GetRegion(int x, int y) { +Region* RegionPager::GetRegion(int x, int y) { //snap the coords x = snapToBase(REGION_WIDTH, x); y = snapToBase(REGION_HEIGHT, y); @@ -47,7 +47,7 @@ Region* RegionPagerBase::GetRegion(int x, int y) { return CreateRegion(x, y); } -Region* RegionPagerBase::FindRegion(int x, int y) { +Region* RegionPager::FindRegion(int x, int y) { //snap the coords x = snapToBase(REGION_WIDTH, x); y = snapToBase(REGION_HEIGHT, y); @@ -61,7 +61,54 @@ Region* RegionPagerBase::FindRegion(int x, int y) { return nullptr; } -Region* RegionPagerBase::PushRegion(Region* ptr) { +Region* RegionPager::LoadRegion(int x, int y) { + //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* RegionPager::SaveRegion(int x, int y) { + //snap the coords + x = snapToBase(REGION_WIDTH, x); + y = snapToBase(REGION_HEIGHT, y); + + //find & save the region + Region* ptr = FindRegion(x, y); + if (ptr) { + //TODO: save the region (lua) + } + return ptr; +} + +Region* RegionPager::CreateRegion(int x, int y) { + //snap the coords + x = snapToBase(REGION_WIDTH, x); + y = snapToBase(REGION_HEIGHT, y); + + //create and push the object + Region* ptr = new Region(x, y); + //TODO: create the region (lua) regionList.push_front(ptr); - return regionList.front(); -} \ No newline at end of file + return ptr; +} + +void RegionPager::UnloadRegion(int x, int y) { + //snap the coords + x = snapToBase(REGION_WIDTH, x); + y = snapToBase(REGION_HEIGHT, y); + + //custom loop, not FindRegion() + for (std::list::iterator it = regionList.begin(); it != regionList.end(); /* EMPTY */) { + if ((*it)->GetX() == x && (*it)->GetY() == y) { + //TODO: unload the region (lua) + delete (*it); + it = regionList.erase(it); + continue; + } + ++it; + } +} diff --git a/common/map/region_pager.hpp b/common/map/region_pager.hpp index 95864f0..6a1d091 100644 --- a/common/map/region_pager.hpp +++ b/common/map/region_pager.hpp @@ -23,14 +23,14 @@ #define REGIONPAGER_HPP_ #include "region.hpp" -#include "utility.hpp" #include -class RegionPagerBase { +//TODO: add lua interface? +class RegionPager { public: - RegionPagerBase() {}; - virtual ~RegionPagerBase() {}; + RegionPager() = default; + ~RegionPager() = default; //tile manipulation Region::type_t SetTile(int x, int y, int z, Region::type_t v); @@ -39,14 +39,12 @@ public: //region manipulation Region* GetRegion(int x, int y); Region* FindRegion(int x, int y); - Region* PushRegion(Region*); - //interface - virtual Region* LoadRegion(int x, int y) = 0; - virtual Region* SaveRegion(int x, int y) = 0; - virtual Region* CreateRegion(int x, int y) = 0; - virtual void UnloadRegion(int x, int y) = 0; - //TODO: delete existing regions + Region* LoadRegion(int x, int y); + Region* SaveRegion(int x, int y); + Region* CreateRegion(int x, int y); + void UnloadRegion(int x, int y); + void DeleteRegion(int x, int y); //accessors & mutators std::list* GetContainer() { return ®ionList; } @@ -54,80 +52,4 @@ protected: std::list regionList; }; -template -class RegionPager : public RegionPagerBase { -public: - RegionPager() {}; - ~RegionPager() { - UnloadAll(); - } - - Region* LoadRegion(int x, int y) { - //snap the coords - x = snapToBase(REGION_WIDTH, x); - y = snapToBase(REGION_HEIGHT, y); - - //load the region if possible - Region* ptr = nullptr; - format.Load(&ptr, x, y); - if (ptr) { - return PushRegion(ptr); - } - return nullptr; - } - - Region* SaveRegion(int x, int y) { - //snap the coords - x = snapToBase(REGION_WIDTH, x); - y = snapToBase(REGION_HEIGHT, y); - - //find & save the region - Region* ptr = FindRegion(x, y); - if (ptr) { - format.Save(ptr); - } - return ptr; - } - - Region* CreateRegion(int x, int y) { - //snap the coords - x = snapToBase(REGION_WIDTH, x); - y = snapToBase(REGION_HEIGHT, y); - - //create and push the object - Region* ptr = nullptr; - allocator.Create(&ptr, x, y); - return PushRegion(ptr); - } - - void UnloadRegion(int x, int y) { - //snap the coords - x = snapToBase(REGION_WIDTH, x); - y = snapToBase(REGION_HEIGHT, y); - - //custom loop - for (std::list::iterator it = regionList.begin(); it != regionList.end(); /* EMPTY */) { - if ((*it)->GetX() == x && (*it)->GetY() == y) { - allocator.Unload(*it); - it = regionList.erase(it); - continue; - } - ++it; - } - } - void UnloadAll() { - for (auto& it : regionList) { - allocator.Unload(it); - } - regionList.clear(); - } - - //accessors - Allocator* GetAllocator() { return &allocator; } - FileFormat* GetFormat() { return &format; } -protected: - Allocator allocator; - FileFormat format; -}; - #endif diff --git a/common/network/serial/serial_region.cpp b/common/network/serial/serial_region.cpp index e0860ac..31145bd 100644 --- a/common/network/serial/serial_region.cpp +++ b/common/network/serial/serial_region.cpp @@ -23,8 +23,6 @@ #include "serial_util.hpp" -#include "map_allocator.hpp" - void serializeRegionFormat(RegionPacket* packet, void* buffer) { SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); @@ -71,11 +69,7 @@ void deserializeRegionContent(RegionPacket* packet, void* buffer) { DESERIALIZE(buffer, &packet->y, sizeof(int)); //an object to work on - BlankAllocator().Create( - &packet->region, - packet->x, - packet->y - ); + packet->region = new Region(packet->x, packet->y); //content for (register int i = 0; i < REGION_WIDTH; i++) { diff --git a/common/script/map_api.cpp b/common/script/map_api.cpp index 0bcaf80..20aa681 100644 --- a/common/script/map_api.cpp +++ b/common/script/map_api.cpp @@ -22,8 +22,6 @@ #include "map_api.hpp" //map headers -#include "map_allocator.hpp" -#include "map_file_format.hpp" #include "region_pager.hpp" //NOTE: When operating on a region, setTile() & getTile() *are not* zero indexed, but when operating on the entire map they *are* zero indexed. @@ -42,7 +40,7 @@ static int setTile(lua_State* L) { lua_gettable(L, LUA_REGISTRYINDEX); //assume the pager is using lua - RegionPager* pager = reinterpret_cast*>(lua_touserdata(L, -1)); + RegionPager* pager = reinterpret_cast(lua_touserdata(L, -1)); //balance the stack lua_pop(L, 1); @@ -65,7 +63,7 @@ static int getTile(lua_State* L) { lua_gettable(L, LUA_REGISTRYINDEX); //assume the pager is using lua - RegionPager* pager = reinterpret_cast*>(lua_touserdata(L, -1)); + RegionPager* pager = reinterpret_cast(lua_touserdata(L, -1)); //balance the stack lua_pop(L, 1); diff --git a/server/room_data.hpp b/server/room_data.hpp index c436d54..0787425 100644 --- a/server/room_data.hpp +++ b/server/room_data.hpp @@ -23,8 +23,6 @@ #define ROOMDATA_HPP_ //map system -#include "map_allocator.hpp" -#include "map_file_format.hpp" #include "region_pager.hpp" struct RoomData { @@ -37,7 +35,7 @@ struct RoomData { }; //members - RegionPager pager; + RegionPager pager; RoomType type; //TODO: collision map diff --git a/server/room_manager.hpp b/server/room_manager.hpp index 2caadaa..9b1dfc6 100644 --- a/server/room_manager.hpp +++ b/server/room_manager.hpp @@ -35,7 +35,6 @@ public: //public access methods //TODO - //TODO: setup the pagers and functors of each room object //accessors and mutators RoomData* GetRoom(int uid); diff --git a/server/server_application.cpp b/server/server_application.cpp index 44467a0..0cb011d 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -23,6 +23,7 @@ #include "sql_utility.hpp" #include "serial.hpp" +#include "utility.hpp" #include #include diff --git a/todo.txt b/todo.txt index 605e60a..20fbe42 100644 --- a/todo.txt +++ b/todo.txt @@ -1,4 +1,3 @@ -TODO: MapLoader, in place of FileFormat TODO: Get the rooms working TODO: update the map API to handle multiple rooms From b269ce5fb97e0c375994bd9eb0bb9e13a3375dc8 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 8 Jun 2014 04:20:07 +1000 Subject: [PATCH 2/8] Re-added the lua hooks --- common/map/region_pager.cpp | 61 +++++++++++++++++++++++++++++++++---- common/map/region_pager.hpp | 17 +++++++++-- 2 files changed, 69 insertions(+), 9 deletions(-) diff --git a/common/map/region_pager.cpp b/common/map/region_pager.cpp index 0fafd8f..547b51f 100644 --- a/common/map/region_pager.cpp +++ b/common/map/region_pager.cpp @@ -23,6 +23,8 @@ #include "utility.hpp" +#include + 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::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); } diff --git a/common/map/region_pager.hpp b/common/map/region_pager.hpp index 6a1d091..e53bc2c 100644 --- a/common/map/region_pager.hpp +++ b/common/map/region_pager.hpp @@ -24,9 +24,11 @@ #include "region.hpp" -#include +#include "lua/lua.hpp" + +#include +#include -//TODO: add lua interface? class RegionPager { public: RegionPager() = default; @@ -48,8 +50,17 @@ public: //accessors & mutators std::list* 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 regionList; + std::string directory; + lua_State* luaState = nullptr; }; #endif From ba83fac29f1150bbcdb4514d189e99496a766dd8 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 8 Jun 2014 19:30:03 +1000 Subject: [PATCH 3/8] Changed map API to region API --- common/script/linit.cpp | 4 +- common/script/{map_api.cpp => region_api.cpp} | 64 +++---------------- common/script/{map_api.hpp => region_api.hpp} | 8 +-- 3 files changed, 16 insertions(+), 60 deletions(-) rename common/script/{map_api.cpp => region_api.cpp} (52%) rename common/script/{map_api.hpp => region_api.hpp} (87%) diff --git a/common/script/linit.cpp b/common/script/linit.cpp index 4f9eb81..c03a812 100644 --- a/common/script/linit.cpp +++ b/common/script/linit.cpp @@ -20,7 +20,7 @@ #define LUA_LIB #include "lua/lua.hpp" -#include "map_api.hpp" +#include "region_api.hpp" /* @@ -41,7 +41,7 @@ static const luaL_Reg loadedlibs[] = { {LUA_DBLIBNAME, luaopen_debug}, /* custom libs */ - {LUA_MAPLIBNAME, luaopen_mapapi}, + {LUA_REGIONLIBNAME, luaopen_regionapi}, {NULL, NULL} }; diff --git a/common/script/map_api.cpp b/common/script/region_api.cpp similarity index 52% rename from common/script/map_api.cpp rename to common/script/region_api.cpp index 20aa681..c94691b 100644 --- a/common/script/map_api.cpp +++ b/common/script/region_api.cpp @@ -19,58 +19,22 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#include "map_api.hpp" +#include "region_api.hpp" -//map headers -#include "region_pager.hpp" - -//NOTE: When operating on a region, setTile() & getTile() *are not* zero indexed, but when operating on the entire map they *are* zero indexed. -//TODO: enforce all possible parameter counts -//TODO: update the map API to handle multiple rooms +#include "region.hpp" static int setTile(lua_State* L) { - if (lua_gettop(L) == 5) { - //operating on a region - 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)); - } - else { - //operating on the whole map - lua_pushstring(L, "pager"); - lua_gettable(L, LUA_REGISTRYINDEX); - - //assume the pager is using lua - RegionPager* pager = reinterpret_cast(lua_touserdata(L, -1)); - - //balance the stack - lua_pop(L, 1); - - pager->SetTile(lua_tointeger(L, 1), lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4)); - } + //operating on a region + 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) { - if (lua_gettop(L) == 4) { - //operating on a region - 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); - } - else { - //operating on the whole map - lua_pushstring(L, "pager"); - lua_gettable(L, LUA_REGISTRYINDEX); - - //assume the pager is using lua - RegionPager* pager = reinterpret_cast(lua_touserdata(L, -1)); - - //balance the stack - lua_pop(L, 1); - - int ret = pager->GetTile(lua_tointeger(L, 1), lua_tointeger(L, 2), lua_tointeger(L, 3)); - lua_pushnumber(L, ret); - } + //operating on a region + 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; } @@ -101,15 +65,7 @@ static int getRegionDepth(lua_State* L) { return 1; } -static int dummy(lua_State* L) { - return 0; -} - static const luaL_Reg regionlib[] = { - {"create", dummy}, - {"unload", dummy}, - {"load", dummy}, - {"save", dummy}, {"settile",setTile}, {"gettile",getTile}, {"getx",getX}, @@ -120,7 +76,7 @@ static const luaL_Reg regionlib[] = { {nullptr, nullptr} }; -LUAMOD_API int luaopen_mapapi(lua_State* L) { +LUAMOD_API int luaopen_regionapi(lua_State* L) { luaL_newlib(L, regionlib); return 1; } \ No newline at end of file diff --git a/common/script/map_api.hpp b/common/script/region_api.hpp similarity index 87% rename from common/script/map_api.hpp rename to common/script/region_api.hpp index efa2abd..310074d 100644 --- a/common/script/map_api.hpp +++ b/common/script/region_api.hpp @@ -19,12 +19,12 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#ifndef MAPAPI_HPP_ -#define MAPAPI_HPP_ +#ifndef REGIONAPI_HPP_ +#define REGIONAPI_HPP_ #include "lua/lua.hpp" -#define LUA_MAPLIBNAME "map" -LUAMOD_API int luaopen_mapapi(lua_State* L); +#define LUA_REGIONLIBNAME "region" +LUAMOD_API int luaopen_regionapi(lua_State* L); #endif From 2d27399fd1080c6da4d82d022231e1af6b7d11a8 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 8 Jun 2014 19:43:24 +1000 Subject: [PATCH 4/8] Added the pager API placeholder files --- common/script/linit.cpp | 119 ++++++++++++++++++++---------------- common/script/pager_api.cpp | 36 +++++++++++ common/script/pager_api.hpp | 30 +++++++++ 3 files changed, 131 insertions(+), 54 deletions(-) create mode 100644 common/script/pager_api.cpp create mode 100644 common/script/pager_api.hpp diff --git a/common/script/linit.cpp b/common/script/linit.cpp index c03a812..13541f2 100644 --- a/common/script/linit.cpp +++ b/common/script/linit.cpp @@ -1,72 +1,83 @@ -/* -** $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 +/* $Id: linit.c,v 1.32, modified + * Initialization of libraries for lua.c and other clients + * See Copyright Notice in lua.h + * + * 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. + * + * Modified for use in Tortuga, renamed to linit.cpp + * Modifications are released under the zlib license: + * + * 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. */ - -/* 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" +#include "pager_api.hpp" - -/* -** these libs are loaded by lua.c and are readily available to any Lua -** program -*/ +//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}, + //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}, + //custom libs + {LUA_REGIONLIBNAME, luaopen_regionapi}, + {LUA_PAGERLIBNAME, luaopen_pagerapi}, - {NULL, NULL} + {NULL, NULL} }; -/* -** these libs are preloaded and must be required before used -*/ +//these libs are preloaded and must be required before used static const luaL_Reg preloadedlibs[] = { - {NULL, NULL} + {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 */ + 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/pager_api.cpp b/common/script/pager_api.cpp new file mode 100644 index 0000000..5e88260 --- /dev/null +++ b/common/script/pager_api.cpp @@ -0,0 +1,36 @@ +/* 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 "pager_api.hpp" + +static int func(lua_State* L) { + return 0; +} + +static const luaL_Reg pagerlib[] = { + {"name", func}, + {nullptr, nullptr} +}; + +LUAMOD_API int luaopen_pagerapi(lua_State* L) { + luaL_newlib(L, pagerlib); + return 1; +} \ No newline at end of file diff --git a/common/script/pager_api.hpp b/common/script/pager_api.hpp new file mode 100644 index 0000000..2531e94 --- /dev/null +++ b/common/script/pager_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 PAGERAPI_HPP_ +#define PAGERAPI_HPP_ + +#include "lua/lua.hpp" + +#define LUA_PAGERLIBNAME "pager" +LUAMOD_API int luaopen_pagerapi(lua_State* L); + +#endif From b7c12ba1061963e71a2bc25d8939404ef2bd5ddb Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Tue, 10 Jun 2014 00:20:45 +1000 Subject: [PATCH 5/8] Fleshed out the pager's API --- common/map/region_pager.cpp | 25 ++++++++---- common/map/region_pager.hpp | 1 - common/script/pager_api.cpp | 73 +++++++++++++++++++++++++++++++++++- common/script/region_api.cpp | 65 +++++++++++++++++++++++--------- 4 files changed, 135 insertions(+), 29 deletions(-) diff --git a/common/map/region_pager.cpp b/common/map/region_pager.cpp index 547b51f..098a720 100644 --- a/common/map/region_pager.cpp +++ b/common/map/region_pager.cpp @@ -70,10 +70,14 @@ Region* RegionPager::LoadRegion(int x, int y) { x = snapToBase(REGION_WIDTH, x); y = snapToBase(REGION_HEIGHT, y); - Region* ptr = new Region(x, y); + //overwrite? + Region* ptr = FindRegion(x, y); + if (!ptr) { + ptr = new Region(x, y); + } //API hook - lua_getglobal(luaState, "map"); + lua_getglobal(luaState, "region"); lua_getfield(luaState, -1, "load"); lua_pushlightuserdata(luaState, ptr); lua_pushstring(luaState, directory.c_str()); @@ -101,7 +105,7 @@ Region* RegionPager::SaveRegion(int x, int y) { Region* ptr = FindRegion(x, y); if (ptr) { //API hook - lua_getglobal(luaState, "map"); + lua_getglobal(luaState, "region"); lua_getfield(luaState, -1, "save"); lua_pushlightuserdata(luaState, ptr); lua_pushstring(luaState, directory.c_str()); @@ -118,13 +122,17 @@ Region* RegionPager::CreateRegion(int x, int y) { x = snapToBase(REGION_WIDTH, x); y = snapToBase(REGION_HEIGHT, y); - //create and push the object - Region* ptr = new Region(x, y); + //overwrite? + Region* ptr = FindRegion(x, y); + if (!ptr) { + ptr = new Region(x, y); + } //API hook - lua_getglobal(luaState, "map"); + lua_getglobal(luaState, "region"); lua_getfield(luaState, -1, "create"); lua_pushlightuserdata(luaState, ptr); + //TODO: parameters if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) { throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) )); } @@ -139,7 +147,7 @@ void RegionPager::UnloadRegion(int x, int y) { x = snapToBase(REGION_WIDTH, x); y = snapToBase(REGION_HEIGHT, y); - lua_getglobal(luaState, "map"); + lua_getglobal(luaState, "region"); //custom loop, not FindRegion() for (std::list::iterator it = regionList.begin(); it != regionList.end(); /* EMPTY */) { @@ -148,7 +156,8 @@ void RegionPager::UnloadRegion(int x, int y) { //API hook lua_getfield(luaState, -1, "unload"); lua_pushlightuserdata(luaState, *it); - if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) { + 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) )); } diff --git a/common/map/region_pager.hpp b/common/map/region_pager.hpp index e53bc2c..ab119dc 100644 --- a/common/map/region_pager.hpp +++ b/common/map/region_pager.hpp @@ -46,7 +46,6 @@ public: Region* SaveRegion(int x, int y); Region* CreateRegion(int x, int y); void UnloadRegion(int x, int y); - void DeleteRegion(int x, int y); //accessors & mutators std::list* GetContainer() { return ®ionList; } diff --git a/common/script/pager_api.cpp b/common/script/pager_api.cpp index 5e88260..16c10dd 100644 --- a/common/script/pager_api.cpp +++ b/common/script/pager_api.cpp @@ -21,12 +21,81 @@ */ #include "pager_api.hpp" -static int func(lua_State* L) { +#include "region_pager.hpp" +#include "region.hpp" + +#include + +static int setTile(lua_State* L) { + RegionPager* pager = reinterpret_cast(lua_touserdata(L, 1)); + int ret = pager->SetTile(lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5)); + lua_pop(L, 5); + lua_pushinteger(L, ret); + return 1; +} + +static int getTile(lua_State* L) { + RegionPager* pager = reinterpret_cast(lua_touserdata(L, 1)); + int ret = pager->GetTile(lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4)); + lua_pop(L, 4); + lua_pushinteger(L, ret); + return 1; +} + +static int getRegion(lua_State* L) { + RegionPager* pager = reinterpret_cast(lua_touserdata(L, 1)); + Region* region = pager->GetRegion(lua_tointeger(L, 2), lua_tointeger(L, 3)); + lua_pop(L, 3); + lua_pushlightuserdata(L, region); + return 1; +} + +static int setDirectory(lua_State* L) { + RegionPager* pager = reinterpret_cast(lua_touserdata(L, 1)); + std::string s = pager->SetDirectory(lua_tostring(L, 2)); + lua_pop(L, 2); + lua_pushstring(L, s.c_str()); + return 1; +} + +static int getDirectory(lua_State* L) { + RegionPager* pager = reinterpret_cast(lua_touserdata(L, 1)); + std::string s = pager->GetDirectory(); + lua_pop(L, 1); + lua_pushstring(L, s.c_str()); + return 1; +} + +static int loadRegion(lua_State* L) { + //TODO: fill this + return 0; +} + +static int saveRegion(lua_State* L) { + //TODO: fill this + return 0; +} + +static int createRegion(lua_State* L) { + //TODO: fill this + return 0; +} + +static int unloadRegion(lua_State* L) { + //TODO: fill this return 0; } static const luaL_Reg pagerlib[] = { - {"name", func}, + {"settile", setTile}, + {"gettile", getTile}, + {"getregion", getRegion}, + {"setdirectory", setDirectory}, + {"getdirectory", getDirectory}, + {"loadregion", loadRegion}, + {"saveregion", saveRegion}, + {"createregion", createRegion}, + {"unloadregion", unloadRegion}, {nullptr, nullptr} }; diff --git a/common/script/region_api.cpp b/common/script/region_api.cpp index c94691b..9e16d45 100644 --- a/common/script/region_api.cpp +++ b/common/script/region_api.cpp @@ -24,55 +24,84 @@ #include "region.hpp" static int setTile(lua_State* L) { - //operating on a region - 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; + Region* region = reinterpret_cast(lua_touserdata(L, 1)); + int ret = region->SetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1, lua_tointeger(L, 5)); + lua_pop(L, 5); + lua_pushinteger(L, ret); + return 1; } static int getTile(lua_State* L) { - //operating on a region - 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); + Region* region = reinterpret_cast(lua_touserdata(L, 1)); + int ret = region->GetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1); + lua_pop(L, 4); + lua_pushinteger(L, ret); return 1; } static int getX(lua_State* L) { - Region* ptr = (Region*)lua_touserdata(L, 1); - lua_pushinteger(L, ptr->GetX()); + Region* region = reinterpret_cast(lua_touserdata(L, 1)); + int ret = region->GetX(); + lua_pop(L, 1); + lua_pushinteger(L, ret); return 1; } static int getY(lua_State* L) { - Region* ptr = (Region*)lua_touserdata(L, 1); - lua_pushinteger(L, ptr->GetY()); + Region* region = reinterpret_cast(lua_touserdata(L, 1)); + int ret = region->GetY(); + lua_pop(L, 1); + lua_pushinteger(L, ret); return 1; } -static int getRegionWidth(lua_State* L) { +static int getWidth(lua_State* L) { lua_pushinteger(L, REGION_WIDTH); return 1; } -static int getRegionHeight(lua_State* L) { +static int getHeight(lua_State* L) { lua_pushinteger(L, REGION_HEIGHT); return 1; } -static int getRegionDepth(lua_State* L) { +static int getDepth(lua_State* L) { lua_pushinteger(L, REGION_DEPTH); return 1; } +static int load(lua_State* L) { + //TODO: fill this + return 0; +} + +static int save(lua_State* L) { + //TODO: fill this + return 0; +} + +static int create(lua_State* L) { + //TODO: fill this + return 0; +} + +static int unload(lua_State* L) { + //TODO: fill this + return 0; +} + static const luaL_Reg regionlib[] = { {"settile",setTile}, {"gettile",getTile}, {"getx",getX}, {"gety",getY}, - {"getregionwidth",getRegionWidth}, - {"getregionheight",getRegionHeight}, - {"getregiondepth",getRegionDepth}, + {"getwidth",getWidth}, + {"getheight",getHeight}, + {"getdepth",getDepth}, + {"load",load}, + {"save",save}, + {"create",create}, + {"unload",unload}, {nullptr, nullptr} }; From 135e650ec8f7a79069af929f9a0d38753f026f96 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Tue, 10 Jun 2014 00:38:00 +1000 Subject: [PATCH 6/8] Moved the map API to the map directory --- common/makefile | 1 - common/{script => map}/pager_api.cpp | 0 common/{script => map}/pager_api.hpp | 0 common/{script => map}/region_api.cpp | 0 common/{script => map}/region_api.hpp | 0 common/script/makefile | 37 --------------------------- {common/script => server}/linit.cpp | 0 7 files changed, 38 deletions(-) rename common/{script => map}/pager_api.cpp (100%) rename common/{script => map}/pager_api.hpp (100%) rename common/{script => map}/region_api.cpp (100%) rename common/{script => map}/region_api.hpp (100%) delete mode 100644 common/script/makefile rename {common/script => server}/linit.cpp (100%) diff --git a/common/makefile b/common/makefile index 0e48688..b670b7d 100644 --- a/common/makefile +++ b/common/makefile @@ -3,7 +3,6 @@ all: $(MAKE) -C graphics $(MAKE) -C map $(MAKE) -C network - $(MAKE) -C script $(MAKE) -C ui $(MAKE) -C utilities diff --git a/common/script/pager_api.cpp b/common/map/pager_api.cpp similarity index 100% rename from common/script/pager_api.cpp rename to common/map/pager_api.cpp diff --git a/common/script/pager_api.hpp b/common/map/pager_api.hpp similarity index 100% rename from common/script/pager_api.hpp rename to common/map/pager_api.hpp diff --git a/common/script/region_api.cpp b/common/map/region_api.cpp similarity index 100% rename from common/script/region_api.cpp rename to common/map/region_api.cpp diff --git a/common/script/region_api.hpp b/common/map/region_api.hpp similarity index 100% rename from common/script/region_api.hpp rename to common/map/region_api.hpp diff --git a/common/script/makefile b/common/script/makefile deleted file mode 100644 index 7cab524..0000000 --- a/common/script/makefile +++ /dev/null @@ -1,37 +0,0 @@ -#config -INCLUDES+=. ../map ../utilities -LIBS+= -CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) - -#source -CXXSRC=$(wildcard *.cpp) - -#objects -OBJDIR=obj -OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.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 $@ $< - -clean: - $(RM) *.o *.a *.exe - -rebuild: clean all diff --git a/common/script/linit.cpp b/server/linit.cpp similarity index 100% rename from common/script/linit.cpp rename to server/linit.cpp From 1ef5eb7a0f31f9e3324d17f65f1e0c062f90d033 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Tue, 10 Jun 2014 00:52:19 +1000 Subject: [PATCH 7/8] Pager's heavyweight API methods call the Region's counterparts --- common/map/pager_api.cpp | 71 +++++++++++++++++++++++++++++++++------ common/map/region_api.cpp | 10 ++---- 2 files changed, 63 insertions(+), 18 deletions(-) diff --git a/common/map/pager_api.cpp b/common/map/pager_api.cpp index 16c10dd..332cc5e 100644 --- a/common/map/pager_api.cpp +++ b/common/map/pager_api.cpp @@ -24,12 +24,12 @@ #include "region_pager.hpp" #include "region.hpp" +#include #include static int setTile(lua_State* L) { RegionPager* pager = reinterpret_cast(lua_touserdata(L, 1)); int ret = pager->SetTile(lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5)); - lua_pop(L, 5); lua_pushinteger(L, ret); return 1; } @@ -37,7 +37,6 @@ static int setTile(lua_State* L) { static int getTile(lua_State* L) { RegionPager* pager = reinterpret_cast(lua_touserdata(L, 1)); int ret = pager->GetTile(lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4)); - lua_pop(L, 4); lua_pushinteger(L, ret); return 1; } @@ -45,7 +44,6 @@ static int getTile(lua_State* L) { static int getRegion(lua_State* L) { RegionPager* pager = reinterpret_cast(lua_touserdata(L, 1)); Region* region = pager->GetRegion(lua_tointeger(L, 2), lua_tointeger(L, 3)); - lua_pop(L, 3); lua_pushlightuserdata(L, region); return 1; } @@ -53,7 +51,6 @@ static int getRegion(lua_State* L) { static int setDirectory(lua_State* L) { RegionPager* pager = reinterpret_cast(lua_touserdata(L, 1)); std::string s = pager->SetDirectory(lua_tostring(L, 2)); - lua_pop(L, 2); lua_pushstring(L, s.c_str()); return 1; } @@ -61,28 +58,82 @@ static int setDirectory(lua_State* L) { static int getDirectory(lua_State* L) { RegionPager* pager = reinterpret_cast(lua_touserdata(L, 1)); std::string s = pager->GetDirectory(); - lua_pop(L, 1); lua_pushstring(L, s.c_str()); return 1; } static int loadRegion(lua_State* L) { - //TODO: fill this - return 0; + //get the parameters + RegionPager* pager = reinterpret_cast(lua_touserdata(L, 1)); + Region* region = pager->GetRegion(lua_tointeger(L, 2), lua_tointeger(L, 3)); + std::string s = pager->GetDirectory(); + + //push the parameters + lua_getglobal(L, "region"); + lua_getfield(L, -1, "load"); + lua_pushlightuserdata(L, region); + lua_pushstring(L, s.c_str()); + + //call the method + if (lua_pcall(L, 2, 1, 0) != LUA_OK) { + throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(L, -1) )); + } + return 1; } static int saveRegion(lua_State* L) { - //TODO: fill this + //get the parameters + RegionPager* pager = reinterpret_cast(lua_touserdata(L, 1)); + Region* region = pager->GetRegion(lua_tointeger(L, 2), lua_tointeger(L, 3)); + std::string s = pager->GetDirectory(); + + //push the parameters + lua_getglobal(L, "region"); + lua_getfield(L, -1, "save"); + lua_pushlightuserdata(L, region); + lua_pushstring(L, s.c_str()); + + //call the method + if (lua_pcall(L, 2, 0, 0) != LUA_OK) { + throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(L, -1) )); + } return 0; } static int createRegion(lua_State* L) { - //TODO: fill this + //get the parameters + RegionPager* pager = reinterpret_cast(lua_touserdata(L, 1)); + Region* region = pager->GetRegion(lua_tointeger(L, 2), lua_tointeger(L, 3)); + + //push the parameters + lua_getglobal(L, "region"); + lua_getfield(L, -1, "create"); + lua_pushlightuserdata(L, region); + //TODO: parameters + + //call the method + if (lua_pcall(L, 1, 0, 0) != LUA_OK) { + throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(L, -1) )); + } return 0; } static int unloadRegion(lua_State* L) { - //TODO: fill this + //get the parameters + RegionPager* pager = reinterpret_cast(lua_touserdata(L, 1)); + Region* region = pager->GetRegion(lua_tointeger(L, 2), lua_tointeger(L, 3)); + std::string s = pager->GetDirectory(); + + //push the parameters + lua_getglobal(L, "region"); + lua_getfield(L, -1, "unload"); + lua_pushlightuserdata(L, region); + lua_pushstring(L, s.c_str()); + + //call the method + if (lua_pcall(L, 2, 0, 0) != LUA_OK) { + throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(L, -1) )); + } return 0; } diff --git a/common/map/region_api.cpp b/common/map/region_api.cpp index 9e16d45..df37532 100644 --- a/common/map/region_api.cpp +++ b/common/map/region_api.cpp @@ -26,7 +26,6 @@ static int setTile(lua_State* L) { Region* region = reinterpret_cast(lua_touserdata(L, 1)); int ret = region->SetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1, lua_tointeger(L, 5)); - lua_pop(L, 5); lua_pushinteger(L, ret); return 1; } @@ -34,24 +33,19 @@ static int setTile(lua_State* L) { static int getTile(lua_State* L) { Region* region = reinterpret_cast(lua_touserdata(L, 1)); int ret = region->GetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1); - lua_pop(L, 4); lua_pushinteger(L, ret); return 1; } static int getX(lua_State* L) { Region* region = reinterpret_cast(lua_touserdata(L, 1)); - int ret = region->GetX(); - lua_pop(L, 1); - lua_pushinteger(L, ret); + lua_pushinteger(L, region->GetX()); return 1; } static int getY(lua_State* L) { Region* region = reinterpret_cast(lua_touserdata(L, 1)); - int ret = region->GetY(); - lua_pop(L, 1); - lua_pushinteger(L, ret); + lua_pushinteger(L, region->GetY()); return 1; } From a07e7418a6c36ed085f34238b5664353f3a4b98f Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Tue, 10 Jun 2014 01:46:42 +1000 Subject: [PATCH 8/8] Implemented a basic API for the server's rooms --- common/map/region_api.cpp | 3 +- rsc/scripts/setup_server.lua | 34 +++++-------------- server/makefile | 2 +- server/room_api.cpp | 63 +++++++++++++++++++++++++++++++++++ server/room_api.hpp | 30 +++++++++++++++++ server/room_manager.hpp | 4 ++- server/server_application.cpp | 22 ++++++------ 7 files changed, 119 insertions(+), 39 deletions(-) create mode 100644 server/room_api.cpp create mode 100644 server/room_api.hpp diff --git a/common/map/region_api.cpp b/common/map/region_api.cpp index df37532..b30f04f 100644 --- a/common/map/region_api.cpp +++ b/common/map/region_api.cpp @@ -66,7 +66,8 @@ static int getDepth(lua_State* L) { static int load(lua_State* L) { //TODO: fill this - return 0; + lua_pushboolean(L, false); + return 1; } static int save(lua_State* L) { diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 8629320..bae4a74 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -4,33 +4,17 @@ print("Lua script check (./rsc)") --Map API overrides ------------------------- -function map.create(region) - for i = 1, map.getregionwidth() do - for j = 1, map.getregionheight() do - if math.abs(map.getx(region) + i -1) == math.abs(map.gety(region) + j -1) then - map.settile(region, i, j, 1, 50) +function region.create(r) + for i = 1, region.getwidth() do + for j = 1, region.getheight() do + if math.abs(region.getx(r) + i -1) == math.abs(region.gety(r) + j -1) then + region.settile(r, i, j, 1, 50) else - map.settile(region, i, j, 1, 14) + region.settile(r, i, j, 1, 14) end end end + + --signal + region.settile(r, 4, 5, 2, 86) end - -function map.unload(region) - -- -end - -function map.load(region, dir) - --return true if file loaded, otherwise return false - return false -end - -function map.save(region, dir) - -- -end - -------------------------- ---Enemy API -------------------------- - ---TODO \ No newline at end of file diff --git a/server/makefile b/server/makefile index 7c733ea..1d8df31 100644 --- a/server/makefile +++ b/server/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. ../common/gameplay ../common/map ../common/network ../common/network/packet ../common/network/serial ../common/script ../common/utilities +INCLUDES+=. ../common/gameplay ../common/map ../common/network ../common/network/packet ../common/network/serial ../common/utilities LIBS+=../libcommon.a -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL -llua -lsqlite3 CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) diff --git a/server/room_api.cpp b/server/room_api.cpp new file mode 100644 index 0000000..ea7ea52 --- /dev/null +++ b/server/room_api.cpp @@ -0,0 +1,63 @@ +/* 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 "room_api.hpp" + +#include "room_manager.hpp" +#include "room_data.hpp" + +static int getType(lua_State* L) { + RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); + lua_pushinteger(L, static_cast(room->type)); + return 1; +} + +//TODO: parameters + +static int getRegionPager(lua_State* L) { + RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); + lua_pushlightuserdata(L, reinterpret_cast(&room->pager)); + return 1; +} + +//RoomManager only +static int getRoom(lua_State* L) { + //get the room manager + lua_pushstring(L, ROOM_MANAGER_PSEUDOINDEX); + lua_gettable(L, LUA_REGISTRYINDEX); + RoomManager* roomMgr = reinterpret_cast(lua_touserdata(L, -1)); + + //push the room and return it + lua_pushlightuserdata(L, reinterpret_cast( roomMgr->GetRoom(lua_tointeger(L, -2)) )); + return 1; +} + +static const luaL_Reg roomlib[] = { + {"gettype",getType}, + {"getregionpager",getRegionPager}, + {"getroom",getRoom}, + {nullptr, nullptr} +}; + +LUAMOD_API int luaopen_roomapi(lua_State* L) { + luaL_newlib(L, roomlib); + return 1; +} \ No newline at end of file diff --git a/server/room_api.hpp b/server/room_api.hpp new file mode 100644 index 0000000..a9c31a8 --- /dev/null +++ b/server/room_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 ROOMAPI_HPP_ +#define ROOMAPI_HPP_ + +#include "lua/lua.hpp" + +#define LUA_ROOMLIBNAME "room" +LUAMOD_API int luaopen_roomapi(lua_State* L); + +#endif diff --git a/server/room_manager.hpp b/server/room_manager.hpp index 9b1dfc6..655c960 100644 --- a/server/room_manager.hpp +++ b/server/room_manager.hpp @@ -28,13 +28,15 @@ #include +#define ROOM_MANAGER_PSEUDOINDEX "RoomManager" + class RoomManager { public: RoomManager() = default; ~RoomManager() = default; //public access methods - //TODO + //TODO: Fill this out //accessors and mutators RoomData* GetRoom(int uid); diff --git a/server/server_application.cpp b/server/server_application.cpp index 0cb011d..6fd85d9 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -76,6 +76,7 @@ void ServerApplication::Init(int argc, char** argv) { //Setup the objects //------------------------- + //set the hooks accountMgr.SetDatabase(database); characterMgr.SetDatabase(database); @@ -83,7 +84,14 @@ void ServerApplication::Init(int argc, char** argv) { roomMgr.SetLuaState(luaState); enemyMgr.SetLuaState(luaState); - std::cout << "Internal managers ready" << std::endl; + std::cout << "Internal managers set" << std::endl; + + //register the "globals" + lua_pushstring(luaState, ROOM_MANAGER_PSEUDOINDEX); + lua_pushlightuserdata(luaState, &roomMgr); + lua_settable(luaState, LUA_REGISTRYINDEX); + + std::cout << "Internal managers registered with lua" << std::endl; //------------------------- //Run the startup scripts @@ -110,6 +118,7 @@ void ServerApplication::Init(int argc, char** argv) { std::cout << "\tRegion Format: " << REGION_WIDTH << ", " << REGION_HEIGHT << ", " << REGION_DEPTH << std::endl; std::cout << "\tRegion Content Footprint: " << REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizeof(Region::type_t) << std::endl; std::cout << "\tPACKET_BUFFER_SIZE (max size): " << PACKET_BUFFER_SIZE << std::endl; + std::cout << "\tMAX_PACKET_SIZE: " << MAX_PACKET_SIZE << std::endl; //------------------------- //finalize the startup @@ -121,16 +130,7 @@ void ServerApplication::Init(int argc, char** argv) { //debugging //------------------------- - std::cout << "Debugging dump:" << std::endl; - std::cout << "\tMAX_PACKET_SIZE:\t\t" << MAX_PACKET_SIZE << std::endl; - std::cout << "\tsizeof(SerialPacket):\t\t" << sizeof(SerialPacket) << std::endl; - std::cout << "\tsizeof(CharacterPacket):\t" << sizeof(CharacterPacket) << std::endl; - std::cout << "\t\tsizeof(Statistics):\t" << sizeof(Statistics) << std::endl; - std::cout << "\tsizeof(ClientPacket):\t\t" << sizeof(ClientPacket) << std::endl; - std::cout << "\tsizeof(CombatPacket):\t\t" << sizeof(CombatPacket) << std::endl; - std::cout << "\tsizeof(EnemyPacket):\t\t" << sizeof(EnemyPacket) << std::endl; - std::cout << "\tsizeof(RegionPacket):\t\t" << sizeof(RegionPacket) << std::endl; - std::cout << "\tsizeof(ServerPacket):\t\t" << sizeof(ServerPacket) << std::endl; + //... } void ServerApplication::Proc() {