From f32b8a9b4f3202f3f17970703dd7e23c6a302d79 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 11 Apr 2016 01:03:02 +1000 Subject: [PATCH] Exposed BarrierManager to the API* --- server/combat/barrier_api.cpp | 14 ++++++++ server/combat/barrier_data.cpp | 14 ++++++++ server/combat/barrier_data.hpp | 3 ++ server/combat/barrier_manager.cpp | 20 ++++++++++++ server/combat/barrier_manager.hpp | 8 +++++ server/combat/barrier_manager_api.cpp | 47 +++++++++++++++++++++++++++ server/combat/barrier_manager_api.hpp | 27 +++++++++++++++ server/linit.cpp | 2 ++ server/rooms/room_api.cpp | 7 ++++ 9 files changed, 142 insertions(+) create mode 100644 server/combat/barrier_manager_api.cpp create mode 100644 server/combat/barrier_manager_api.hpp diff --git a/server/combat/barrier_api.cpp b/server/combat/barrier_api.cpp index 207f35e..3940e66 100644 --- a/server/combat/barrier_api.cpp +++ b/server/combat/barrier_api.cpp @@ -64,6 +64,18 @@ static int getInstance(lua_State* L) { return 1; } +static int setStatus(lua_State* L) { + BarrierData* barrier = static_cast(lua_touserdata(L, 1)); + barrier->SetStatus(lua_tointeger(L, 2), lua_tointeger(L, 3)); + return 0; +} + +static int getStatus(lua_State* L) { + BarrierData* barrier = static_cast(lua_touserdata(L, 1)); + lua_pushinteger(L, barrier->GetStatus(lua_tointeger(L, 2)) ); + return 1; +} + static const luaL_Reg barrierLib[] = { {"SetScript", setScript}, {"GetScript", getScript}, @@ -71,6 +83,8 @@ static const luaL_Reg barrierLib[] = { {"GetTag", getTag}, {"SetInstance", setInstance}, {"GetInstance", getInstance}, + {"SetStatus", setStatus}, + {"GetStatus", getStatus}, {nullptr, nullptr} }; diff --git a/server/combat/barrier_data.cpp b/server/combat/barrier_data.cpp index 63d5578..bf65cd9 100644 --- a/server/combat/barrier_data.cpp +++ b/server/combat/barrier_data.cpp @@ -84,6 +84,20 @@ int BarrierData::GetInstanceIndex() const { return instanceIndex; } +int BarrierData::SetStatus(int k, int v) { + if (k < 0 || k >= 8) { + return -1; + } + return status[k] = v; +} + +int BarrierData::GetStatus(int k) { + if (k < 0 || k >= 8) { + return -1; + } + return status[k]; +} + int* BarrierData::GetStatusArray() { return status; } \ No newline at end of file diff --git a/server/combat/barrier_data.hpp b/server/combat/barrier_data.hpp index c91cd9f..bb9de50 100644 --- a/server/combat/barrier_data.hpp +++ b/server/combat/barrier_data.hpp @@ -44,6 +44,9 @@ public: int SetInstanceIndex(int i); int GetInstanceIndex() const; + int SetStatus(int k, int v); + int GetStatus(int k); + int* GetStatusArray(); private: diff --git a/server/combat/barrier_manager.cpp b/server/combat/barrier_manager.cpp index 3a328e3..899d6fe 100644 --- a/server/combat/barrier_manager.cpp +++ b/server/combat/barrier_manager.cpp @@ -44,6 +44,9 @@ int BarrierManager::Create(int instanceIndex) { //implicitly create the new object elementMap.emplace( std::pair(counter, BarrierData(instanceIndex)) ); + //set the script + //TODO: (0) + //TODO: do various things like saving to the database return counter++; } @@ -103,3 +106,20 @@ sqlite3* BarrierManager::SetDatabase(sqlite3* db) { sqlite3* BarrierManager::GetDatabase() { return database; } + +int BarrierManager::SetCreateReference(int i) { + return createRef = i; +} + +int BarrierManager::SetUnloadReference(int i) { + return unloadRef = i; +} + +int BarrierManager::GetCreateReference() { + return createRef; +} + +int BarrierManager::GetUnloadReference() { + return unloadRef; +} + diff --git a/server/combat/barrier_manager.hpp b/server/combat/barrier_manager.hpp index 83a1dc8..c1d443e 100644 --- a/server/combat/barrier_manager.hpp +++ b/server/combat/barrier_manager.hpp @@ -55,10 +55,18 @@ public: sqlite3* SetDatabase(sqlite3* db); sqlite3* GetDatabase(); + int SetCreateReference(int i); + int SetUnloadReference(int i); + + int GetCreateReference(); + int GetUnloadReference(); + private: //members std::map elementMap; int counter = 0; lua_State* lua = nullptr; sqlite3* database = nullptr; + int createRef = LUA_NOREF; + int unloadRef = LUA_NOREF; }; \ No newline at end of file diff --git a/server/combat/barrier_manager_api.cpp b/server/combat/barrier_manager_api.cpp new file mode 100644 index 0000000..e926722 --- /dev/null +++ b/server/combat/barrier_manager_api.cpp @@ -0,0 +1,47 @@ +/* Copyright: (c) Kayne Ruse 2013-2016 + * + * 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 "barrier_manager_api.hpp" + +#include "barrier_manager.hpp" + +static int setOnCreate(lua_State* L) { + BarrierManager* barrierMgr = static_cast(lua_touserdata(L, 1)); + barrierMgr->SetCreateReference(luaL_ref(L, LUA_REGISTRYINDEX)); + return 0; +} + +static int setOnUnload(lua_State* L) { + BarrierManager* barrierMgr = static_cast(lua_touserdata(L, 1)); + barrierMgr->SetUnloadReference(luaL_ref(L, LUA_REGISTRYINDEX)); + return 0; +} + +static const luaL_Reg barrierManagerLib[] = { + {"SetOnCreate", setOnCreate}, + {"SetOnUnload", setOnUnload}, + {nullptr, nullptr} +}; + +LUAMOD_API int openBarrierManagerAPI(lua_State* L) { + luaL_newlib(L, barrierManagerLib); + return 1; +} \ No newline at end of file diff --git a/server/combat/barrier_manager_api.hpp b/server/combat/barrier_manager_api.hpp new file mode 100644 index 0000000..a5015e2 --- /dev/null +++ b/server/combat/barrier_manager_api.hpp @@ -0,0 +1,27 @@ +/* Copyright: (c) Kayne Ruse 2013-2016 + * + * 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. +*/ +#pragma once + +#include "lua.hpp" + +#define TORTUGA_BARRIER_MANAGER_API "barrier_manager" +LUAMOD_API int openBarrierManagerAPI(lua_State* L); diff --git a/server/linit.cpp b/server/linit.cpp index 4084a59..572f902 100644 --- a/server/linit.cpp +++ b/server/linit.cpp @@ -38,6 +38,7 @@ #include "entity_api.hpp" #include "barrier_api.hpp" +#include "barrier_manager_api.hpp" #include "character_api.hpp" #include "character_manager_api.hpp" #include "region_api.hpp" @@ -70,6 +71,7 @@ static const luaL_Reg loadedlibs[] = { static const luaL_Reg preloadedlibs[] = { {TORTUGA_ENTITY_API, openEntityAPI}, //required by derived classes {TORTUGA_BARRIER_API, openBarrierAPI}, + {TORTUGA_BARRIER_MANAGER_API, openBarrierManagerAPI}, {TORTUGA_CHARACTER_API, openCharacterAPI}, {TORTUGA_CHARACTER_MANAGER_API, openCharacterManagerAPI}, {TORTUGA_CREATURE_API, openCreatureAPI}, diff --git a/server/rooms/room_api.cpp b/server/rooms/room_api.cpp index 455ce02..8e70448 100644 --- a/server/rooms/room_api.cpp +++ b/server/rooms/room_api.cpp @@ -50,6 +50,12 @@ static int getTilesetName(lua_State* L) { return 1; } +static int getBarrierMgr(lua_State* L) { + RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); + lua_pushlightuserdata(L, reinterpret_cast(room->GetBarrierMgr()) ); + return 1; +} + static int getCreatureMgr(lua_State* L) { RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); lua_pushlightuserdata(L, reinterpret_cast(room->GetCreatureMgr()) ); @@ -135,6 +141,7 @@ static const luaL_Reg roomLib[] = { {"SetTileset", setTilesetName}, {"GetTileset", getTilesetName}, + {"GetBarrierMgr", getBarrierMgr}, {"GetCreatureMgr",getCreatureMgr}, {"GetPager",getPager}, {"GetTriggerMgr",getTriggerMgr},