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
+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}
};