From 051ed0f14c8b98e8d906e404f19ae529ccaee0bb Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 11 Jan 2015 19:08:31 +1100 Subject: [PATCH] Monster API clones from Entity API, read more This is my solution for handling inheritance via lua. The Entity class is only a base class, so the entity API is designed to be copied from, rather than used directly. linit.c: It should be noted that the Entity API must always be placed before the utilizing child APIs. I don't know about how lua handles things internally, but I'm assuming that this is the case. There's no real meat in the API code yet, since that's just busy-work. Right now I feel beter about writing the connective tissue. This case could aslo extend to the waypoint and monster APIs. The waypoint system had some API and class methods removed for brevity. --- rsc/scripts/setup_server.lua | 15 ++++++ server/linit.cpp | 5 ++ server/monsters/monster_api.cpp | 44 ++++++++++++++++ server/monsters/monster_data.cpp | 8 +-- server/monsters/monster_data.hpp | 4 +- server/monsters/monster_manager.cpp | 16 ------ server/monsters/monster_manager.hpp | 4 -- server/monsters/monster_system_api.cpp | 55 ++++++++++++++++++++ server/monsters/monster_system_api.hpp | 30 +++++++++++ server/waypoints/waypoint_api.cpp | 70 ++++---------------------- 10 files changed, 165 insertions(+), 86 deletions(-) create mode 100644 server/monsters/monster_system_api.cpp create mode 100644 server/monsters/monster_system_api.hpp diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 53c733f..605dcff 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -22,6 +22,21 @@ mapSystem.RegionPager.SetOnSave(roomSystem.Room.GetPager(overworld), mapSaver.Sa mapSystem.RegionPager.SetOnCreate(roomSystem.Room.GetPager(overworld), mapMaker.debugIsland) mapSystem.RegionPager.SetOnUnload(roomSystem.Room.GetPager(overworld), mapSaver.Save) +--check the entity-monster cloning... +print("Checking monster-entity stuff") + +monsterSystem = require "monster_system" +entities = require "entity" + +dumpTable(entities) +dumpTable(monsterSystem) +dumpTable(monsterSystem.Monster) + +monsterSystem.Monster.GetRoomIndex = -1 + +dumpTable(entities) +dumpTable(monsterSystem.Monster) + print("Finished the lua script") --[[ diff --git a/server/linit.cpp b/server/linit.cpp index aaa449d..fe803b9 100644 --- a/server/linit.cpp +++ b/server/linit.cpp @@ -36,6 +36,9 @@ #include "lua.hpp" +#include "entity_api.hpp" +#include "monster_system_api.hpp" + #include "map_system_api.hpp" #include "room_system_api.hpp" #include "waypoint_system_api.hpp" @@ -59,6 +62,8 @@ static const luaL_Reg loadedlibs[] = { //these libs are preloaded and must be required before used static const luaL_Reg preloadedlibs[] = { + {TORTUGA_ENTITY_API, openEntityAPI}, + {TORTUGA_MONSTER_SYSTEM_API, openMonsterSystemAPI}, {TORTUGA_MAP_SYSTEM_API, openMapSystemAPI}, {TORTUGA_ROOM_SYSTEM_API, openRoomSystemAPI}, {TORTUGA_WAYPOINT_SYSTEM_API, openWaypointSystemAPI}, diff --git a/server/monsters/monster_api.cpp b/server/monsters/monster_api.cpp index 3baec56..473e7b4 100644 --- a/server/monsters/monster_api.cpp +++ b/server/monsters/monster_api.cpp @@ -23,11 +23,55 @@ #include "monster_data.hpp" +#include "entity_api.hpp" + +static int setAvatar(lua_State* L) { + //TODO +} + +static int getAvatar(lua_State* L) { + //TODO +} + +static int setScript(lua_State* L) { + //TODO +} + +static int getScript(lua_State* L) { + //TODO +} + static const luaL_Reg monsterLib[] = { + {"SetAvatar", setAvatar}, + {"GetAvatar", getAvatar}, + {"SetScript", setScript}, + {"GetScript", getScript}, {nullptr, nullptr} }; LUAMOD_API int openMonsterAPI(lua_State* L) { + //the local table luaL_newlib(L, monsterLib); + + //get the parent table + luaL_requiref(L, TORTUGA_ENTITY_API, openEntityAPI, false); + + //clone 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 child table + lua_pop(L, 1); + return 1; } \ No newline at end of file diff --git a/server/monsters/monster_data.cpp b/server/monsters/monster_data.cpp index 7fda714..22f5252 100644 --- a/server/monsters/monster_data.cpp +++ b/server/monsters/monster_data.cpp @@ -25,14 +25,14 @@ std::string MonsterData::SetAvatar(std::string s) { return avatar = s; } -int MonsterData::SetScriptReference(int i) { - return scriptRef = i; -} - std::string MonsterData::GetAvatar() { return avatar; } +int MonsterData::SetScriptReference(int i) { + return scriptRef = i; +} + int MonsterData::GetScriptReference() { return scriptRef; } \ No newline at end of file diff --git a/server/monsters/monster_data.hpp b/server/monsters/monster_data.hpp index 034cebb..f02723b 100644 --- a/server/monsters/monster_data.hpp +++ b/server/monsters/monster_data.hpp @@ -32,9 +32,9 @@ public: ~MonsterData() = default; std::string SetAvatar(std::string); - int SetScriptReference(int); - std::string GetAvatar(); + + int SetScriptReference(int); int GetScriptReference(); private: diff --git a/server/monsters/monster_manager.cpp b/server/monsters/monster_manager.cpp index 0cc78ce..2031fe2 100644 --- a/server/monsters/monster_manager.cpp +++ b/server/monsters/monster_manager.cpp @@ -25,22 +25,10 @@ int MonsterManager::Create(std::string) { //TODO } -int MonsterManager::Load(std::string) { - //TODO -} - -int MonsterManager::Save(int uid) { - //TODO -} - void MonsterManager::Unload(int uid) { //TODO } -void MonsterManager::Delete(int uid) { - //TODO -} - void MonsterManager::UnloadAll() { //TODO } @@ -57,10 +45,6 @@ int MonsterManager::GetLoadedCount() { //TODO } -int MonsterManager::GetTotalCount() { - //TODO -} - std::map* MonsterManager::GetContainer() { //TODO } diff --git a/server/monsters/monster_manager.hpp b/server/monsters/monster_manager.hpp index 83e799a..07d9602 100644 --- a/server/monsters/monster_manager.hpp +++ b/server/monsters/monster_manager.hpp @@ -36,10 +36,7 @@ class MonsterManager: public Singleton { public: //common public methods int Create(std::string); - int Load(std::string); - int Save(int uid); void Unload(int uid); - void Delete(int uid); void UnloadAll(); void UnloadIf(std::function)> fn); @@ -47,7 +44,6 @@ public: //accessors & mutators MonsterData* Get(int uid); int GetLoadedCount(); - int GetTotalCount(); std::map* GetContainer(); //hooks diff --git a/server/monsters/monster_system_api.cpp b/server/monsters/monster_system_api.cpp new file mode 100644 index 0000000..2ecbf71 --- /dev/null +++ b/server/monsters/monster_system_api.cpp @@ -0,0 +1,55 @@ +/* Copyright: (c) Kayne Ruse 2013-2015 + * + * 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 "monster_system_api.hpp" + +//all monster API headers +#include "monster_api.hpp" +#include "monster_manager_api.hpp" + +//useful "globals" +//... + +//This mimics linit.c to create a nested collection of all monster modules. +static const luaL_Reg funcs[] = { + {nullptr, nullptr} +}; + +static const luaL_Reg libs[] = { + {"Monster", openMonsterAPI}, + {"MonsterManager", openMonsterManagerAPI}, + {nullptr, nullptr} +}; + +int openMonsterSystemAPI(lua_State* L) { + //create the table + luaL_newlibtable(L, libs); + + //push the "global" functions + luaL_setfuncs(L, funcs, 0); + + //push the substable + for (const luaL_Reg* lib = libs; lib->func; lib++) { + lib->func(L); + lua_setfield(L, -2, lib->name); + } + return 1; +} diff --git a/server/monsters/monster_system_api.hpp b/server/monsters/monster_system_api.hpp new file mode 100644 index 0000000..843a5e7 --- /dev/null +++ b/server/monsters/monster_system_api.hpp @@ -0,0 +1,30 @@ +/* Copyright: (c) Kayne Ruse 2013-2015 + * + * 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. +*/ +#ifndef MONSTERSYSTEMAPI_HPP_ +#define MONSTERSYSTEMAPI_HPP_ + +#include "lua.hpp" + +#define TORTUGA_MONSTER_SYSTEM_API "monster_system" +LUAMOD_API int openMonsterSystemAPI(lua_State* L); + +#endif \ No newline at end of file diff --git a/server/waypoints/waypoint_api.cpp b/server/waypoints/waypoint_api.cpp index 34a6274..f769ddb 100644 --- a/server/waypoints/waypoint_api.cpp +++ b/server/waypoints/waypoint_api.cpp @@ -23,64 +23,26 @@ #include "waypoint_data.hpp" +//TODO: return multiple values, dummy + //origin -static int setOriginX(lua_State* L) { +static int setOrigin(lua_State* L) { //TODO return 0; } -static int setOriginY(lua_State* L) { - //TODO - return 0; -} - -static int getOriginX(lua_State* L) { - //TODO - return 0; -} - -static int getOriginY(lua_State* L) { +static int getOrigin(lua_State* L) { //TODO return 0; } //bounds -static int setBoundingBoxX(lua_State* L) { +static int setBoundingBox(lua_State* L) { //TODO return 0; } -static int setBoundingBoxY(lua_State* L) { - //TODO - return 0; -} - -static int setBoundingBoxW(lua_State* L) { - //TODO - return 0; -} - -static int setBoundingBoxH(lua_State* L) { - //TODO - return 0; -} - -static int getBoundingBoxX(lua_State* L) { - //TODO - return 0; -} - -static int getBoundingBoxY(lua_State* L) { - //TODO - return 0; -} - -static int getBoundingBoxW(lua_State* L) { - //TODO - return 0; -} - -static int getBoundingBoxH(lua_State* L) { +static int getBoundingBox(lua_State* L) { //TODO return 0; } @@ -97,24 +59,12 @@ static int getTriggerReference(lua_State* L) { } static const luaL_Reg waypointLib[] = { - //origin - {"SetOriginX",setOriginX}, - {"SetOriginY",setOriginY}, - {"GetOriginX",getOriginX}, - {"GetOriginY",getOriginY}, + {"SetOrigin",setOrigin}, + {"GetOrigin",getOrigin}, - //bounds - {"SetBoundsX",setBoundingBoxX}, - {"SetBoundsY",setBoundingBoxY}, - {"SetBoundsW",setBoundingBoxW}, - {"SetBoundsH",setBoundingBoxH}, + {"SetBounds",setBoundingBox}, + {"GetBounds",getBoundingBox}, - {"GetBoundsX",getBoundingBoxX}, - {"GetBoundsY",getBoundingBoxY}, - {"GetBoundsW",getBoundingBoxW}, - {"GetBoundsH",getBoundingBoxH}, - - //triggers {"SetTrigger",setTriggerReference}, {"GetTrigger",getTriggerReference}, {nullptr, nullptr}