diff --git a/server/characters/character_api.cpp b/server/characters/character_api.cpp index bf8b6f0..c6a0414 100644 --- a/server/characters/character_api.cpp +++ b/server/characters/character_api.cpp @@ -23,11 +23,56 @@ #include "character_data.hpp" +#include "entity_api.hpp" + +static int getOwner(lua_State* L) { + CharacterData* character = static_cast(lua_touserdata(L, 1)); + lua_pushinteger(L, character->GetOwner()); + return 1; +} + +static int getHandle(lua_State* L) { + CharacterData* character = static_cast(lua_touserdata(L, 1)); + lua_pushstring(L, character->GetHandle().c_str()); + return 1; +} + +static int getAvatar(lua_State* L) { + CharacterData* character = static_cast(lua_touserdata(L, 1)); + lua_pushstring(L, character->GetAvatar().c_str()); + return 1; +} + static const luaL_Reg characterLib[] = { +// {"GetOwner", getOwner}, //unusable without account API + {"GetHandle", getHandle}, + {"GetAvatar", getAvatar}, {nullptr, nullptr} }; LUAMOD_API int openCharacterAPI(lua_State* L) { + //the local table luaL_newlib(L, characterLib); + + //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/characters/character_data.hpp b/server/characters/character_data.hpp index 873bfa4..6c02134 100644 --- a/server/characters/character_data.hpp +++ b/server/characters/character_data.hpp @@ -35,9 +35,6 @@ public: CharacterData() = default; ~CharacterData() = default; - //accessors and mutators - //... - //database stuff int GetOwner(); std::string GetHandle(); diff --git a/server/characters/character_system_api.cpp b/server/characters/character_system_api.cpp new file mode 100644 index 0000000..bac7893 --- /dev/null +++ b/server/characters/character_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 "character_system_api.hpp" + +//all character API headers +#include "character_api.hpp" +#include "character_manager_api.hpp" + +//useful "globals" +//... + +//This mimics linit.c to create a nested collection of all character modules. +static const luaL_Reg funcs[] = { + {nullptr, nullptr} +}; + +static const luaL_Reg libs[] = { + {"Character", openCharacterAPI}, + {"CharacterManager", openCharacterManagerAPI}, + {nullptr, nullptr} +}; + +int openCharacterSystemAPI(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/characters/character_system_api.hpp b/server/characters/character_system_api.hpp new file mode 100644 index 0000000..67b47a7 --- /dev/null +++ b/server/characters/character_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 CHARACTERSYSTEMAPI_HPP_ +#define CHARACTERSYSTEMAPI_HPP_ + +#include "lua.hpp" + +#define TORTUGA_CHARACTER_SYSTEM_API "character_system" +LUAMOD_API int openCharacterSystemAPI(lua_State* L); + +#endif \ No newline at end of file diff --git a/server/linit.cpp b/server/linit.cpp index b460290..5390bfc 100644 --- a/server/linit.cpp +++ b/server/linit.cpp @@ -37,6 +37,7 @@ #include "lua.hpp" #include "entity_api.hpp" +#include "character_system_api.hpp" #include "map_system_api.hpp" #include "monster_system_api.hpp" #include "room_system_api.hpp" @@ -58,10 +59,10 @@ static const luaL_Reg loadedlibs[] = { {NULL, NULL} }; - //these libs are preloaded and must be required before used static const luaL_Reg preloadedlibs[] = { - {TORTUGA_ENTITY_API, openEntityAPI}, + {TORTUGA_ENTITY_API, openEntityAPI}, //required by derived classes + {TORTUGA_CHARACTER_SYSTEM_API, openCharacterSystemAPI}, {TORTUGA_MAP_SYSTEM_API, openMapSystemAPI}, {TORTUGA_MONSTER_SYSTEM_API, openMonsterSystemAPI}, {TORTUGA_ROOM_SYSTEM_API, openRoomSystemAPI},