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
+1 -1
View File
@@ -947,6 +947,7 @@ void World::hCreatureMovement(CreaturePacket* const argPacket) {
//------------------------- //-------------------------
void World::hBarrierUpdate(BarrierPacket* const argPacket) { void World::hBarrierUpdate(BarrierPacket* const argPacket) {
std::cout << "Barrier Update" << std::endl;
//Cull barriers that are too far away //Cull barriers that are too far away
if ( (localCharacter->GetOrigin() - argPacket->origin).Length() > INFLUENCE_RADIUS) { if ( (localCharacter->GetOrigin() - argPacket->origin).Length() > INFLUENCE_RADIUS) {
//ignore beyond 1000 units //ignore beyond 1000 units
@@ -967,7 +968,6 @@ void World::hBarrierUpdate(BarrierPacket* const argPacket) {
} }
barrier->SetStatusArray(argPacket->status); barrier->SetStatusArray(argPacket->status);
barrier->CorrectSprite(); barrier->CorrectSprite();
} }
void World::hBarrierCreate(BarrierPacket* const argPacket) { void World::hBarrierCreate(BarrierPacket* const argPacket) {
+41 -2
View File
@@ -43,6 +43,8 @@ doorUtility = require("door_utility")
creatureAPI = require("creature") creatureAPI = require("creature")
creatureManagerAPI = require("creature_manager") creatureManagerAPI = require("creature_manager")
barrierAPI = require("barrier")
barrierManagerAPI = require("barrier_manager")
--testing creature tags --testing creature tags
local function bunnySquare(creature) local function bunnySquare(creature)
@@ -60,8 +62,6 @@ local function bunnySquare(creature)
creatureAPI.SetMotion(creature, 0, 1) creatureAPI.SetMotion(creature, 0, 1)
end end
--is it time to change direction? --is it time to change direction?
if os.time() - tonumber(timestamp) >= 4 then if os.time() - tonumber(timestamp) >= 4 then
-- print("changing directions") -- print("changing directions")
@@ -90,12 +90,51 @@ local function bunnySquare(creature)
return ret return ret
end end
local function barrierTick(barrier)
--load from the object
local statusTable = {}
for i = 1, 8 do
statusTable[i] = barrierAPI.GetStatus(barrier, i)
end
--binary tick
for i = 1, 8, 0 do
if status[i] == 0 then
status[i] = 1
break
else
status[i] = 0
i = i + 1
end
end
--save to the object
for i = 1, 8 do
barrierAPI.GetStatus(barrier, i, statusTable[i])
end
end
--test the room hooks --test the room hooks
roomManagerAPI.SetOnCreate(function(room, index) roomManagerAPI.SetOnCreate(function(room, index)
print("", "Creating room: ", roomAPI.GetName(room), index) print("", "Creating room: ", roomAPI.GetName(room), index)
--TODO: (0) creatureManager with SetOnCreate
creatureManagerAPI.SetOnCreate(roomAPI.GetCreatureMgr(room), function(creature, index)
print ("making creature: ", index)
end)
print("Here it comes...")
creatureManagerAPI.Create(roomAPI.GetCreatureMgr(room), "anibunny.png", bunnySquare) creatureManagerAPI.Create(roomAPI.GetCreatureMgr(room), "anibunny.png", bunnySquare)
creatureManagerAPI.SetOnUnload(roomAPI.GetCreatureMgr(room), function(creature)
print("unloading creature...")
end)
--TODO: (0) barrierManager with Create
barrierManagerAPI.SetOnCreate(roomAPI.GetBarrierMgr(room), function(barrier)
barrierAPI.SetScript(barrier, barrierTick)
end)
roomAPI.SetOnTick(room, function(room) roomAPI.SetOnTick(room, function(room)
roomAPI.ForEachCharacter(room, function(character) roomAPI.ForEachCharacter(room, function(character)
-- --
+8 -3
View File
@@ -21,6 +21,8 @@
*/ */
#include "barrier_manager.hpp" #include "barrier_manager.hpp"
#include "lua_utilities.hpp"
BarrierManager::BarrierManager() { BarrierManager::BarrierManager() {
//EMPTY //EMPTY
} }
@@ -44,8 +46,7 @@ int BarrierManager::Create(int instanceIndex) {
//implicitly create the new object //implicitly create the new object
elementMap.emplace( std::pair<int, BarrierData>(counter, BarrierData(instanceIndex)) ); elementMap.emplace( std::pair<int, BarrierData>(counter, BarrierData(instanceIndex)) );
//set the script runHook(lua, createRef, &elementMap.find(counter)->second, counter);
//TODO: (0)
//TODO: do various things like saving to the database //TODO: do various things like saving to the database
return counter++; return counter++;
@@ -54,10 +55,14 @@ int BarrierManager::Create(int instanceIndex) {
//TODO: (1) combat load, save //TODO: (1) combat load, save
void BarrierManager::Unload(int uid) { void BarrierManager::Unload(int uid) {
runHook(lua, unloadRef, &elementMap.find(uid)->second, uid);
elementMap.erase(uid); elementMap.erase(uid);
} }
void BarrierManager::UnloadAll() { 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(); 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(); std::map<int, BarrierData>::iterator it = elementMap.begin();
while (it != elementMap.end()) { while (it != elementMap.end()) {
if (fn(*it)) { if (fn(*it)) {
runHook(lua, unloadRef, &it->second, it->first);
it = elementMap.erase(it); it = elementMap.erase(it);
} }
else { else {
@@ -122,4 +128,3 @@ int BarrierManager::GetCreateReference() {
int BarrierManager::GetUnloadReference() { int BarrierManager::GetUnloadReference() {
return unloadRef; return unloadRef;
} }
+81 -4
View File
@@ -23,19 +23,96 @@
#include "barrier_manager.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) { static int setOnCreate(lua_State* L) {
BarrierManager* barrierMgr = static_cast<BarrierManager*>(lua_touserdata(L, 1)); BarrierManager* mgr = static_cast<BarrierManager*>(lua_touserdata(L, 1));
barrierMgr->SetCreateReference(luaL_ref(L, LUA_REGISTRYINDEX)); mgr->SetCreateReference(luaL_ref(L, LUA_REGISTRYINDEX));
return 0; return 0;
} }
static int setOnUnload(lua_State* L) { static int setOnUnload(lua_State* L) {
BarrierManager* barrierMgr = static_cast<BarrierManager*>(lua_touserdata(L, 1)); BarrierManager* mgr = static_cast<BarrierManager*>(lua_touserdata(L, 1));
barrierMgr->SetUnloadReference(luaL_ref(L, LUA_REGISTRYINDEX)); mgr->SetUnloadReference(luaL_ref(L, LUA_REGISTRYINDEX));
return 0; return 0;
} }
static const luaL_Reg barrierManagerLib[] = { static const luaL_Reg barrierManagerLib[] = {
{"Create", create},
{"Unload", unload},
{"UnloadAll", unloadAll},
{"UnloadIf", unloadIf},
{"Find", find},
{"GetLoadedCount", getLoadedCount},
{"SetOnCreate", setOnCreate}, {"SetOnCreate", setOnCreate},
{"SetOnUnload", setOnUnload}, {"SetOnUnload", setOnUnload},
{nullptr, nullptr} {nullptr, nullptr}
+25
View File
@@ -21,6 +21,8 @@
*/ */
#include "creature_manager.hpp" #include "creature_manager.hpp"
#include "lua_utilities.hpp"
CreatureManager::CreatureManager() { CreatureManager::CreatureManager() {
//EMPTY //EMPTY
} }
@@ -44,6 +46,8 @@ int CreatureManager::Create(std::string avatar, int scriptRef) {
//implicitly create the new object //implicitly create the new object
elementMap.emplace( std::pair<int, CreatureData>(counter, CreatureData(avatar, scriptRef)) ); 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 //TODO: do various things like saving to the database
return counter++; return counter++;
} }
@@ -51,10 +55,14 @@ int CreatureManager::Create(std::string avatar, int scriptRef) {
//TODO: (1) creature load, save //TODO: (1) creature load, save
void CreatureManager::Unload(int uid) { void CreatureManager::Unload(int uid) {
runHook(lua, unloadRef, &elementMap.find(uid)->second, uid);
elementMap.erase(uid); elementMap.erase(uid);
} }
void CreatureManager::UnloadAll() { 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(); 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(); std::map<int, CreatureData>::iterator it = elementMap.begin();
while (it != elementMap.end()) { while (it != elementMap.end()) {
if (fn(*it)) { if (fn(*it)) {
runHook(lua, unloadRef, &it->second, it->first);
it = elementMap.erase(it); it = elementMap.erase(it);
} }
else { else {
@@ -103,3 +112,19 @@ sqlite3* CreatureManager::SetDatabase(sqlite3* db) {
sqlite3* CreatureManager::GetDatabase() { sqlite3* CreatureManager::GetDatabase() {
return database; 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* SetDatabase(sqlite3* db);
sqlite3* GetDatabase(); sqlite3* GetDatabase();
int SetCreateReference(int i);
int SetUnloadReference(int i);
int GetCreateReference();
int GetUnloadReference();
private: private:
//members //members
std::map<int, CreatureData> elementMap; std::map<int, CreatureData> elementMap;
int counter = 0; int counter = 0;
lua_State* lua = nullptr; lua_State* lua = nullptr;
sqlite3* database = 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) { static int unloadIf(lua_State* L) {
CreatureManager* mgr = static_cast<CreatureManager* const>(lua_touserdata(L, 1)); 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; return 0;
} }
@@ -69,13 +94,27 @@ static int getLoadedCount(lua_State* L) {
return 1; 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[] = { static const luaL_Reg creatureManagerLib[] = {
{"Create", create}, {"Create", create},
{"Unload", unload}, {"Unload", unload},
{"UnloadAll", unloadAll}, {"UnloadAll", unloadAll},
// {"UnloadIf", unloadIf}, {"UnloadIf", unloadIf},
{"Find", find}, {"Find", find},
{"GetLoadedCount", getLoadedCount}, {"GetLoadedCount", getLoadedCount},
{"SetOnCreate", setOnCreate},
{"SetOnUnload", setOnUnload},
{nullptr, nullptr} {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_State* RoomData::SetLuaState(lua_State* L) {
lua = L; lua = L;
barrierMgr.SetLuaState(lua);
creatureMgr.SetLuaState(lua); creatureMgr.SetLuaState(lua);
pager.SetLuaState(lua); pager.SetLuaState(lua);
triggerMgr.SetLuaState(lua); triggerMgr.SetLuaState(lua);
@@ -168,6 +169,7 @@ lua_State* RoomData::GetLuaState() {
} }
sqlite3* RoomData::SetDatabase(sqlite3* db) { sqlite3* RoomData::SetDatabase(sqlite3* db) {
//TODO: (1) set database here
database = db; database = db;
return database; 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(int characterIndex, int newRoomIndex);
void pumpAndChangeRooms(CharacterData* const characterData, int newRoomIndex, int characterIndex); void pumpAndChangeRooms(CharacterData* const characterData, int newRoomIndex, int characterIndex);
double distance(Vector2 lhs, Vector2 rhs); double distance(Vector2 lhs, Vector2 rhs);