diff --git a/server/combat/barrier_api.cpp b/server/combat/barrier_api.cpp new file mode 100644 index 0000000..069d6bf --- /dev/null +++ b/server/combat/barrier_api.cpp @@ -0,0 +1,102 @@ +/* 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_api.hpp" + +#include "barrier_data.hpp" + +#include "entity_api.hpp" + + +static int setScript(lua_State* L) { + BarrierData* barrier = static_cast(lua_touserdata(L, 1)); + luaL_unref(L, LUA_REGISTRYINDEX, barrier->GetScriptReference()); + barrier->SetScriptReference(luaL_ref(L, LUA_REGISTRYINDEX)); + return 0; +} + +static int getScript(lua_State* L) { + BarrierData* barrier = static_cast(lua_touserdata(L, 1)); + lua_pushinteger(L, barrier->GetScriptReference()); + lua_gettable(L, LUA_REGISTRYINDEX); + return 1; +} + +static int setTag(lua_State* L) { + BarrierData* barrier = static_cast(lua_touserdata(L, 1)); + barrier->SetTag(lua_tostring(L, 2), lua_tostring(L, 3)); + return 0; +} + +static int getTag(lua_State* L) { + BarrierData* barrier = static_cast(lua_touserdata(L, 1)); + lua_pushstring(L, barrier->GetTag(lua_tostring(L, 2)).c_str()); + return 1; +} + +static int setInstance(lua_State* L) { + BarrierData* barrier = static_cast(lua_touserdata(L, 1)); + barrier->SetInstanceIndex(lua_tointeger(L, 2)); + return 0; +} + +static int getInstance(lua_State* L) { + BarrierData* barrier = static_cast(lua_touserdata(L, 1)); + lua_pushinteger(L, barrier->GetInstanceIndex()); + return 1; +} + +static const luaL_Reg barrierLib[] = { + {"SetScript", setScript}, + {"GetScript", getScript}, + {"SetTag", setTag}, + {"GetTag", getTag}, + {"SetInstance", setInstance}, + {"GetInstance", getInstance}, + {nullptr, nullptr} +}; + +LUAMOD_API int openBarrierAPI(lua_State* L) { + //get the parent table + luaL_requiref(L, TORTUGA_ENTITY_API, openEntityAPI, false); + + //the local table + luaL_newlib(L, barrierLib); + + //merge the local table into the parent table + lua_pushnil(L); //first key + while(lua_next(L, -2)) { + //copy the key-value pair + lua_pushvalue(L, -2); + lua_pushvalue(L, -2); + + //push the copy to the parent table + lua_settable(L, -6); + + //pop the original value before continuing + lua_pop(L, 1); + } + + //remove the local table, leaving the expanded parent table + lua_pop(L, 1); + + return 1; +} \ No newline at end of file diff --git a/server/combat/barrier_api.hpp b/server/combat/barrier_api.hpp new file mode 100644 index 0000000..475e64f --- /dev/null +++ b/server/combat/barrier_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_API "barrier" +LUAMOD_API int openBarrierAPI(lua_State* L); diff --git a/server/combat/barrier_data.cpp b/server/combat/barrier_data.cpp index 9739b55..3282b0d 100644 --- a/server/combat/barrier_data.cpp +++ b/server/combat/barrier_data.cpp @@ -21,10 +21,63 @@ */ #include "barrier_data.hpp" -BarrierData::BarrierData(int i): Entity::Entity("barrier") { - instanceIndex = 0; +#include + +BarrierData::BarrierData(int i): + Entity::Entity("barrier") +{ + // } BarrierData::~BarrierData() { // -} \ No newline at end of file +} + +int BarrierData::Update(lua_State* L) { + int ret = 0; + + if (scriptRef != 0) { + //Call the script reference + lua_pushinteger(L, scriptRef); + lua_gettable(L, LUA_REGISTRYINDEX); + lua_pushlightuserdata(L, reinterpret_cast(this)); + + //check for errors + if(lua_pcall(L, 1, 1, 0) != LUA_OK) { + std::ostringstream msg; + msg << "Error running creature script: " << lua_tostring(L, -1); + lua_pop(L, 1); + throw(std::runtime_error(msg.str())); + } + + ret += lua_tonumber(L, -1); + } + + Entity::Update(); + + return ret; +} + +int BarrierData::SetScriptReference(int i) { + return scriptRef = i; +} + +int BarrierData::GetScriptReference() { + return scriptRef; +} + +std::string BarrierData::SetTag(std::string key, std::string value) { + return tags[key] = value; +} + +std::string BarrierData::GetTag(std::string key) { + return tags[key]; +} + +int BarrierData::SetInstanceIndex(int i) { + return instanceIndex = i; +} + +int BarrierData::GetInstanceIndex() const { + return instanceIndex; +} diff --git a/server/combat/barrier_data.hpp b/server/combat/barrier_data.hpp index f77e824..ab2b38a 100644 --- a/server/combat/barrier_data.hpp +++ b/server/combat/barrier_data.hpp @@ -23,14 +23,30 @@ #include "entity.hpp" +#include "lua.hpp" + +#include +#include + class BarrierData: public Entity { public: BarrierData(int instanceIndex); ~BarrierData(); -private: - int instanceIndex = -1; + int Update(lua_State*); - //for displaying the status of combatants: 0 empty, 1 green, 2 red - int slotData[8]; + int SetScriptReference(int); + int GetScriptReference(); + + std::string SetTag(std::string key, std::string value); + std::string GetTag(std::string key); + + int SetInstanceIndex(int i); + int GetInstanceIndex() const; + +private: + int scriptRef = LUA_NOREF; + std::map tags; + + int instanceIndex = -1; }; \ No newline at end of file diff --git a/server/combat/barrier_manager.cpp b/server/combat/barrier_manager.cpp index 6cf3ca3..3a328e3 100644 --- a/server/combat/barrier_manager.cpp +++ b/server/combat/barrier_manager.cpp @@ -29,13 +29,20 @@ BarrierManager::~BarrierManager() { UnloadAll(); } -void BarrierManager::Update() { - // +//arg: a list of barriers to be updated in the clients +void BarrierManager::Update(std::list>* barrierList) { + int ret; + for (auto& it : elementMap) { + ret = it.second.Update(lua); + if (ret) { + barrierList->push_back(std::pair(it.first, &it.second)); + } + } } -int BarrierManager::Create() { +int BarrierManager::Create(int instanceIndex) { //implicitly create the new object - elementMap.emplace( std::pair(counter, BarrierData(-1)) ); + elementMap.emplace( std::pair(counter, BarrierData(instanceIndex)) ); //TODO: do various things like saving to the database return counter++; diff --git a/server/combat/barrier_manager.hpp b/server/combat/barrier_manager.hpp index ce6cf85..83a1dc8 100644 --- a/server/combat/barrier_manager.hpp +++ b/server/combat/barrier_manager.hpp @@ -27,6 +27,7 @@ #include "sqlite3.h" #include +#include #include class BarrierManager { @@ -35,9 +36,9 @@ public: ~BarrierManager(); //common public methods - void Update(); + void Update(std::list>* barrierList); - int Create(); + int Create(int instanceIndex); void Unload(int uid); void UnloadAll(); diff --git a/server/linit.cpp b/server/linit.cpp index 947c7b6..4084a59 100644 --- a/server/linit.cpp +++ b/server/linit.cpp @@ -37,6 +37,7 @@ #include "lua.hpp" #include "entity_api.hpp" +#include "barrier_api.hpp" #include "character_api.hpp" #include "character_manager_api.hpp" #include "region_api.hpp" @@ -68,6 +69,7 @@ static const luaL_Reg loadedlibs[] = { //these libs are preloaded and must be required before used static const luaL_Reg preloadedlibs[] = { {TORTUGA_ENTITY_API, openEntityAPI}, //required by derived classes + {TORTUGA_BARRIER_API, openBarrierAPI}, {TORTUGA_CHARACTER_API, openCharacterAPI}, {TORTUGA_CHARACTER_MANAGER_API, openCharacterManagerAPI}, {TORTUGA_CREATURE_API, openCreatureAPI},