Filled out some barrier stuff
This commit is contained in:
@@ -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<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 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;
|
||||
}
|
||||
@@ -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);
|
||||
@@ -21,10 +21,63 @@
|
||||
*/
|
||||
#include "barrier_data.hpp"
|
||||
|
||||
BarrierData::BarrierData(int i): Entity::Entity("barrier") {
|
||||
instanceIndex = 0;
|
||||
#include <sstream>
|
||||
|
||||
BarrierData::BarrierData(int i):
|
||||
Entity::Entity("barrier")
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
BarrierData::~BarrierData() {
|
||||
//
|
||||
}
|
||||
|
||||
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<void*>(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;
|
||||
}
|
||||
|
||||
@@ -23,14 +23,30 @@
|
||||
|
||||
#include "entity.hpp"
|
||||
|
||||
#include "lua.hpp"
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
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<std::string, std::string> tags;
|
||||
|
||||
int instanceIndex = -1;
|
||||
};
|
||||
@@ -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<std::pair<const int, BarrierData*>>* barrierList) {
|
||||
int ret;
|
||||
for (auto& it : elementMap) {
|
||||
ret = it.second.Update(lua);
|
||||
if (ret) {
|
||||
barrierList->push_back(std::pair<const int, BarrierData*>(it.first, &it.second));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int BarrierManager::Create() {
|
||||
int BarrierManager::Create(int instanceIndex) {
|
||||
//implicitly create the new object
|
||||
elementMap.emplace( std::pair<int, BarrierData>(counter, BarrierData(-1)) );
|
||||
elementMap.emplace( std::pair<int, BarrierData>(counter, BarrierData(instanceIndex)) );
|
||||
|
||||
//TODO: do various things like saving to the database
|
||||
return counter++;
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
#include "sqlite3.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <list>
|
||||
#include <map>
|
||||
|
||||
class BarrierManager {
|
||||
@@ -35,9 +36,9 @@ public:
|
||||
~BarrierManager();
|
||||
|
||||
//common public methods
|
||||
void Update();
|
||||
void Update(std::list<std::pair<const int, BarrierData*>>* barrierList);
|
||||
|
||||
int Create();
|
||||
int Create(int instanceIndex);
|
||||
void Unload(int uid);
|
||||
|
||||
void UnloadAll();
|
||||
|
||||
@@ -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},
|
||||
|
||||
Reference in New Issue
Block a user