Expanded creature & barrier API

This commit is contained in:
2016-04-11 02:18:13 +10:00
parent f32b8a9b4f
commit 20b121766a
11 changed files with 281 additions and 13 deletions
+8 -3
View File
@@ -21,6 +21,8 @@
*/
#include "barrier_manager.hpp"
#include "lua_utilities.hpp"
BarrierManager::BarrierManager() {
//EMPTY
}
@@ -44,8 +46,7 @@ int BarrierManager::Create(int instanceIndex) {
//implicitly create the new object
elementMap.emplace( std::pair<int, BarrierData>(counter, BarrierData(instanceIndex)) );
//set the script
//TODO: (0)
runHook(lua, createRef, &elementMap.find(counter)->second, counter);
//TODO: do various things like saving to the database
return counter++;
@@ -54,10 +55,14 @@ int BarrierManager::Create(int instanceIndex) {
//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();
}
@@ -65,6 +70,7 @@ void BarrierManager::UnloadIf(std::function<bool(std::pair<const int, BarrierDat
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 {
@@ -122,4 +128,3 @@ int BarrierManager::GetCreateReference() {
int BarrierManager::GetUnloadReference() {
return unloadRef;
}
+81 -4
View File
@@ -23,19 +23,96 @@
#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* barrierMgr = static_cast<BarrierManager*>(lua_touserdata(L, 1));
barrierMgr->SetCreateReference(luaL_ref(L, LUA_REGISTRYINDEX));
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* barrierMgr = static_cast<BarrierManager*>(lua_touserdata(L, 1));
barrierMgr->SetUnloadReference(luaL_ref(L, LUA_REGISTRYINDEX));
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}
+25
View File
@@ -21,6 +21,8 @@
*/
#include "creature_manager.hpp"
#include "lua_utilities.hpp"
CreatureManager::CreatureManager() {
//EMPTY
}
@@ -44,6 +46,8 @@ int CreatureManager::Create(std::string avatar, int scriptRef) {
//implicitly create the new object
elementMap.emplace( std::pair<int, CreatureData>(counter, CreatureData(avatar, scriptRef)) );
runHook(lua, createRef, &elementMap.find(counter)->second, counter);
//TODO: do various things like saving to the database
return counter++;
}
@@ -51,10 +55,14 @@ int CreatureManager::Create(std::string avatar, int scriptRef) {
//TODO: (1) creature load, save
void CreatureManager::Unload(int uid) {
runHook(lua, unloadRef, &elementMap.find(uid)->second, uid);
elementMap.erase(uid);
}
void CreatureManager::UnloadAll() {
for (std::map<int, CreatureData>::iterator it = elementMap.begin(); it != elementMap.end(); it++) {
runHook(lua, unloadRef, &it->second, it->first);
}
elementMap.clear();
}
@@ -62,6 +70,7 @@ void CreatureManager::UnloadIf(std::function<bool(std::pair<const int, CreatureD
std::map<int, CreatureData>::iterator it = elementMap.begin();
while (it != elementMap.end()) {
if (fn(*it)) {
runHook(lua, unloadRef, &it->second, it->first);
it = elementMap.erase(it);
}
else {
@@ -103,3 +112,19 @@ sqlite3* CreatureManager::SetDatabase(sqlite3* db) {
sqlite3* CreatureManager::GetDatabase() {
return database;
}
int CreatureManager::SetCreateReference(int i) {
return createRef = i;
}
int CreatureManager::SetUnloadReference(int i) {
return unloadRef = i;
}
int CreatureManager::GetCreateReference() {
return createRef;
}
int CreatureManager::GetUnloadReference() {
return unloadRef;
}
+8
View File
@@ -56,10 +56,18 @@ public:
sqlite3* SetDatabase(sqlite3* db);
sqlite3* GetDatabase();
int SetCreateReference(int i);
int SetUnloadReference(int i);
int GetCreateReference();
int GetUnloadReference();
private:
//members
std::map<int, CreatureData> elementMap;
int counter = 0;
lua_State* lua = nullptr;
sqlite3* database = nullptr;
int createRef = LUA_NOREF;
int unloadRef = LUA_NOREF;
};
+41 -2
View File
@@ -52,7 +52,32 @@ static int unloadAll(lua_State* L) {
static int unloadIf(lua_State* L) {
CreatureManager* mgr = static_cast<CreatureManager* const>(lua_touserdata(L, 1));
//TODO: unloadIf
//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;
}
@@ -69,13 +94,27 @@ static int getLoadedCount(lua_State* L) {
return 1;
}
static int setOnCreate(lua_State* L) {
CreatureManager* mgr = static_cast<CreatureManager*>(lua_touserdata(L, 1));
mgr->SetCreateReference(luaL_ref(L, LUA_REGISTRYINDEX));
return 0;
}
static int setOnUnload(lua_State* L) {
CreatureManager* mgr = static_cast<CreatureManager*>(lua_touserdata(L, 1));
mgr->SetUnloadReference(luaL_ref(L, LUA_REGISTRYINDEX));
return 0;
}
static const luaL_Reg creatureManagerLib[] = {
{"Create", create},
{"Unload", unload},
{"UnloadAll", unloadAll},
// {"UnloadIf", unloadIf},
{"UnloadIf", unloadIf},
{"Find", find},
{"GetLoadedCount", getLoadedCount},
{"SetOnCreate", setOnCreate},
{"SetOnUnload", setOnUnload},
{nullptr, nullptr}
};
+47
View File
@@ -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 "lua_utilities.hpp"
#include <stdexcept>
#include <string>
//this is for functions passed to SetOnCreate & SetOnUnload
int runHook(lua_State* L, int scriptRef, void* userdata, int index) {
//get the hook
lua_rawgeti(L, LUA_REGISTRYINDEX, scriptRef);
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
return -1;
}
//push args
lua_pushlightuserdata(L, userdata);
lua_pushinteger(L, index);
//call the function, 2 arg, 0 return
if (lua_pcall(L, 2, 0, 0) != LUA_OK) {
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(L, -1) ));
}
return 0;
}
+26
View File
@@ -0,0 +1,26 @@
/* 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"
int runHook(lua_State* L, int scriptRef, void* userdata, int index);
+2
View File
@@ -157,6 +157,7 @@ TriggerManager* RoomData::GetTriggerMgr() {
lua_State* RoomData::SetLuaState(lua_State* L) {
lua = L;
barrierMgr.SetLuaState(lua);
creatureMgr.SetLuaState(lua);
pager.SetLuaState(lua);
triggerMgr.SetLuaState(lua);
@@ -168,6 +169,7 @@ lua_State* RoomData::GetLuaState() {
}
sqlite3* RoomData::SetDatabase(sqlite3* db) {
//TODO: (1) set database here
database = db;
return database;
}
+1 -1
View File
@@ -44,4 +44,4 @@ void copyBarrierToPacket(BarrierPacket* const packet, BarrierData* const barrier
void pumpAndChangeRooms(int characterIndex, int newRoomIndex);
void pumpAndChangeRooms(CharacterData* const characterData, int newRoomIndex, int characterIndex);
double distance(Vector2 lhs, Vector2 rhs);
double distance(Vector2 lhs, Vector2 rhs);