Moved barrier system into it's own subdirectory

This commit is contained in:
2016-07-09 16:56:35 +10:00
parent 9a1dcc2f53
commit 4894daa273
12 changed files with 36 additions and 3 deletions
-116
View File
@@ -1,116 +0,0 @@
/* 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<BarrierData*>(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<BarrierData*>(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<BarrierData*>(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<BarrierData*>(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<BarrierData*>(lua_touserdata(L, 1));
barrier->SetInstanceIndex(lua_tointeger(L, 2));
return 0;
}
static int getInstance(lua_State* L) {
BarrierData* barrier = static_cast<BarrierData*>(lua_touserdata(L, 1));
lua_pushinteger(L, barrier->GetInstanceIndex());
return 1;
}
static int setStatus(lua_State* L) {
BarrierData* barrier = static_cast<BarrierData*>(lua_touserdata(L, 1));
barrier->SetStatus(lua_tointeger(L, 2) - 1, lua_tointeger(L, 3));
return 0;
}
static int getStatus(lua_State* L) {
BarrierData* barrier = static_cast<BarrierData*>(lua_touserdata(L, 1));
lua_pushinteger(L, barrier->GetStatus(lua_tointeger(L, 2) - 1));
return 1;
}
static const luaL_Reg barrierLib[] = {
{"SetScript", setScript},
{"GetScript", getScript},
{"SetTag", setTag},
{"GetTag", getTag},
{"SetInstance", setInstance},
{"GetInstance", getInstance},
{"SetStatus", setStatus},
{"GetStatus", getStatus},
{nullptr, nullptr}
};
LUAMOD_API int openBarrierAPI(lua_State* L) {
//the local table
luaL_newlib(L, barrierLib);
//get the parent table
luaL_requiref(L, TORTUGA_ENTITY_API, openEntityAPI, false);
//merge the parent table into the local 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 local table
lua_settable(L, -6);
//pop the original value before continuing
lua_pop(L, 1);
}
//remove the parent table, leaving the expanded local table
lua_pop(L, 1);
return 1;
}
-27
View File
@@ -1,27 +0,0 @@
/* 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);
-112
View File
@@ -1,112 +0,0 @@
/* 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_data.hpp"
#include <cstring>
#include <sstream>
#include <stdexcept>
BarrierData::BarrierData(int i):
Entity::Entity("barrier")
{
instanceIndex = i;
memset(status, 0, sizeof(int) * 8);
SetBounds({
BARRIER_BOUNDS_X,
BARRIER_BOUNDS_Y,
BARRIER_BOUNDS_WIDTH,
BARRIER_BOUNDS_HEIGHT
});
}
BarrierData::~BarrierData() {
//
}
int BarrierData::Update(lua_State* L) {
int ret = 0;
//NOTE: this is here mostly for the "barrier tick" effect
if (scriptRef != LUA_NOREF) {
//Call the script reference
lua_pushinteger(L, scriptRef);
lua_gettable(L, LUA_REGISTRYINDEX);
lua_pushlightuserdata(L, reinterpret_cast<void*>(this));
//check for errors
if(lua_pcall(L, 1, 1, 0) != LUA_OK) {
std::ostringstream msg;
msg << "Error running barrier 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;
}
int BarrierData::SetStatus(int k, int v) {
if (k < 0 || k >= 8) {
throw(std::runtime_error("Failed to set status"));
}
return status[k] = v;
}
int BarrierData::GetStatus(int k) {
if (k < 0 || k >= 8) {
throw(std::runtime_error("Failed to get status"));
}
return status[k];
}
int* BarrierData::GetStatusArray() {
return status;
}
-60
View File
@@ -1,60 +0,0 @@
/* 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 "barrier_defines.hpp"
#include "entity.hpp"
#include "lua.hpp"
#include <map>
#include <string>
class BarrierData: public Entity {
public:
BarrierData(int instanceIndex);
~BarrierData();
int Update(lua_State*);
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;
int SetStatus(int k, int v);
int GetStatus(int k);
int* GetStatusArray();
private:
int scriptRef = LUA_NOREF;
std::map<std::string, std::string> tags;
int instanceIndex;
int status[8];
};
-130
View File
@@ -1,130 +0,0 @@
/* 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.hpp"
#include "lua_utilities.hpp"
BarrierManager::BarrierManager() {
//EMPTY
}
BarrierManager::~BarrierManager() {
UnloadAll();
}
//arg: a list of barriers to be updated in the clients
void BarrierManager::Update(std::list<std::pair<const int, BarrierData*>>* barrierList, bool updateAll) {
int ret;
for (auto& it : elementMap) {
ret = it.second.Update(lua);
if (ret || updateAll) {
barrierList->push_back(std::pair<const int, BarrierData*>(it.first, &it.second));
}
}
}
int BarrierManager::Create(int instanceIndex) {
//implicitly create the new object
elementMap.emplace( std::pair<int, BarrierData>(counter, BarrierData(instanceIndex)) );
runHook(lua, createRef, &elementMap.find(counter)->second, counter);
//TODO: do various things like saving to the database
return counter++;
}
//TODO: (1) combat load, save
void BarrierManager::Unload(int uid) {
runHook(lua, unloadRef, &elementMap.find(uid)->second, uid);
elementMap.erase(uid);
}
void BarrierManager::UnloadAll() {
for (std::map<int, BarrierData>::iterator it = elementMap.begin(); it != elementMap.end(); it++) {
runHook(lua, unloadRef, &it->second, it->first);
}
elementMap.clear();
}
void BarrierManager::UnloadIf(std::function<bool(std::pair<const int, BarrierData const&>)> fn) {
std::map<int, BarrierData>::iterator it = elementMap.begin();
while (it != elementMap.end()) {
if (fn(*it)) {
runHook(lua, unloadRef, &it->second, it->first);
it = elementMap.erase(it);
}
else {
++it;
}
}
}
BarrierData* BarrierManager::Find(int uid) {
std::map<int, BarrierData>::iterator it = elementMap.find(uid);
if (it == elementMap.end()) {
return nullptr;
}
return &it->second;
}
int BarrierManager::GetLoadedCount() {
return elementMap.size();
}
std::map<int, BarrierData>* BarrierManager::GetContainer() {
return &elementMap;
}
lua_State* BarrierManager::SetLuaState(lua_State* L) {
return lua = L;
}
lua_State* BarrierManager::GetLuaState() {
return lua;
}
sqlite3* BarrierManager::SetDatabase(sqlite3* db) {
return database = 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;
}
-73
View File
@@ -1,73 +0,0 @@
/* 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 "barrier_data.hpp"
#include "lua.hpp"
#include "sqlite3.h"
#include <algorithm>
#include <functional>
#include <list>
#include <map>
class BarrierManager {
public:
BarrierManager();
~BarrierManager();
//common public methods
void Update(std::list<std::pair<const int, BarrierData*>>* barrierList, bool updateAll);
int Create(int instanceIndex);
void Unload(int uid);
void UnloadAll();
void UnloadIf(std::function<bool(std::pair<const int, BarrierData const&>)> fn);
//accessors & mutators
BarrierData* Find(int uid);
int GetLoadedCount();
std::map<int, BarrierData>* GetContainer();
//hooks
lua_State* SetLuaState(lua_State* L);
lua_State* GetLuaState();
sqlite3* SetDatabase(sqlite3* db);
sqlite3* GetDatabase();
int SetCreateReference(int i);
int SetUnloadReference(int i);
int GetCreateReference();
int GetUnloadReference();
private:
//members
std::map<int, BarrierData> elementMap;
int counter = 0;
lua_State* lua = nullptr;
sqlite3* database = nullptr;
int createRef = LUA_NOREF;
int unloadRef = LUA_NOREF;
};
-124
View File
@@ -1,124 +0,0 @@
/* 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"
//args: mgr, avatar, script
static int create(lua_State* L) {
//register the function at the top of the stack
lua_pushinteger(L, luaL_ref(L, LUA_REGISTRYINDEX));
//create the actual barrier
BarrierManager* mgr = static_cast<BarrierManager* const>(lua_touserdata(L, 1));
int index = mgr->Create(-1);
BarrierData* barrier = mgr->Find(index);
lua_pushlightuserdata(L, static_cast<void*>(barrier));
lua_pushinteger(L, index);
return 2;
}
//TOOD: overload this to take the userdata as a parameter
static int unload(lua_State* L) {
BarrierManager* mgr = static_cast<BarrierManager* const>(lua_touserdata(L, 1));
mgr->Unload(lua_tointeger(L, 2));
return 0;
}
static int unloadAll(lua_State* L) {
BarrierManager* mgr = static_cast<BarrierManager* const>(lua_touserdata(L, 1));
mgr->UnloadAll();
return 0;
}
static int unloadIf(lua_State* L) {
BarrierManager* mgr = static_cast<BarrierManager* const>(lua_touserdata(L, 1));
//list of stuff to unload (don't invalidate iterators)
std::list<int> unloadList;
//unloadIf
for (auto it : *mgr->GetContainer()) {
//copy the function at the top
lua_pushvalue(L, -1);
//index & object as function parameters
lua_pushinteger(L, it.first);
lua_pushlightuserdata(L, &it.second);
//call
lua_pcall(L, 2, 1, 0);
//unload-ish
if (lua_toboolean(L, -1)) {
unloadList.push_back(it.first);
}
}
//actually unload
for (auto& it : unloadList) {
mgr->Unload(it);
}
return 0;
}
static int find(lua_State* L) {
BarrierManager* mgr = static_cast<BarrierManager* const>(lua_touserdata(L, 1));
BarrierData* barrier = mgr->Find(lua_tointeger(L, 2));
lua_pushlightuserdata(L, static_cast<void*>(barrier));
return 1;
}
static int getLoadedCount(lua_State* L) {
BarrierManager* mgr = static_cast<BarrierManager* const>(lua_touserdata(L, 1));
lua_pushinteger(L, mgr->GetLoadedCount());
return 1;
}
static int setOnCreate(lua_State* L) {
BarrierManager* mgr = static_cast<BarrierManager*>(lua_touserdata(L, 1));
mgr->SetCreateReference(luaL_ref(L, LUA_REGISTRYINDEX));
return 0;
}
static int setOnUnload(lua_State* L) {
BarrierManager* mgr = static_cast<BarrierManager*>(lua_touserdata(L, 1));
mgr->SetUnloadReference(luaL_ref(L, LUA_REGISTRYINDEX));
return 0;
}
static const luaL_Reg barrierManagerLib[] = {
{"Create", create},
{"Unload", unload},
{"UnloadAll", unloadAll},
{"UnloadIf", unloadIf},
{"Find", find},
{"GetLoadedCount", getLoadedCount},
{"SetOnCreate", setOnCreate},
{"SetOnUnload", setOnUnload},
{nullptr, nullptr}
};
LUAMOD_API int openBarrierManagerAPI(lua_State* L) {
luaL_newlib(L, barrierManagerLib);
return 1;
}
-27
View File
@@ -1,27 +0,0 @@
/* 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);