From a07e7418a6c36ed085f34238b5664353f3a4b98f Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Tue, 10 Jun 2014 01:46:42 +1000 Subject: [PATCH] Implemented a basic API for the server's rooms --- common/map/region_api.cpp | 3 +- rsc/scripts/setup_server.lua | 34 +++++-------------- server/makefile | 2 +- server/room_api.cpp | 63 +++++++++++++++++++++++++++++++++++ server/room_api.hpp | 30 +++++++++++++++++ server/room_manager.hpp | 4 ++- server/server_application.cpp | 22 ++++++------ 7 files changed, 119 insertions(+), 39 deletions(-) create mode 100644 server/room_api.cpp create mode 100644 server/room_api.hpp diff --git a/common/map/region_api.cpp b/common/map/region_api.cpp index df37532..b30f04f 100644 --- a/common/map/region_api.cpp +++ b/common/map/region_api.cpp @@ -66,7 +66,8 @@ static int getDepth(lua_State* L) { static int load(lua_State* L) { //TODO: fill this - return 0; + lua_pushboolean(L, false); + return 1; } static int save(lua_State* L) { diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 8629320..bae4a74 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -4,33 +4,17 @@ print("Lua script check (./rsc)") --Map API overrides ------------------------- -function map.create(region) - for i = 1, map.getregionwidth() do - for j = 1, map.getregionheight() do - if math.abs(map.getx(region) + i -1) == math.abs(map.gety(region) + j -1) then - map.settile(region, i, j, 1, 50) +function region.create(r) + for i = 1, region.getwidth() do + for j = 1, region.getheight() do + if math.abs(region.getx(r) + i -1) == math.abs(region.gety(r) + j -1) then + region.settile(r, i, j, 1, 50) else - map.settile(region, i, j, 1, 14) + region.settile(r, i, j, 1, 14) end end end + + --signal + region.settile(r, 4, 5, 2, 86) end - -function map.unload(region) - -- -end - -function map.load(region, dir) - --return true if file loaded, otherwise return false - return false -end - -function map.save(region, dir) - -- -end - -------------------------- ---Enemy API -------------------------- - ---TODO \ No newline at end of file diff --git a/server/makefile b/server/makefile index 7c733ea..1d8df31 100644 --- a/server/makefile +++ b/server/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. ../common/gameplay ../common/map ../common/network ../common/network/packet ../common/network/serial ../common/script ../common/utilities +INCLUDES+=. ../common/gameplay ../common/map ../common/network ../common/network/packet ../common/network/serial ../common/utilities LIBS+=../libcommon.a -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL -llua -lsqlite3 CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) diff --git a/server/room_api.cpp b/server/room_api.cpp new file mode 100644 index 0000000..ea7ea52 --- /dev/null +++ b/server/room_api.cpp @@ -0,0 +1,63 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * 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 "room_api.hpp" + +#include "room_manager.hpp" +#include "room_data.hpp" + +static int getType(lua_State* L) { + RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); + lua_pushinteger(L, static_cast(room->type)); + return 1; +} + +//TODO: parameters + +static int getRegionPager(lua_State* L) { + RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); + lua_pushlightuserdata(L, reinterpret_cast(&room->pager)); + return 1; +} + +//RoomManager only +static int getRoom(lua_State* L) { + //get the room manager + lua_pushstring(L, ROOM_MANAGER_PSEUDOINDEX); + lua_gettable(L, LUA_REGISTRYINDEX); + RoomManager* roomMgr = reinterpret_cast(lua_touserdata(L, -1)); + + //push the room and return it + lua_pushlightuserdata(L, reinterpret_cast( roomMgr->GetRoom(lua_tointeger(L, -2)) )); + return 1; +} + +static const luaL_Reg roomlib[] = { + {"gettype",getType}, + {"getregionpager",getRegionPager}, + {"getroom",getRoom}, + {nullptr, nullptr} +}; + +LUAMOD_API int luaopen_roomapi(lua_State* L) { + luaL_newlib(L, roomlib); + return 1; +} \ No newline at end of file diff --git a/server/room_api.hpp b/server/room_api.hpp new file mode 100644 index 0000000..a9c31a8 --- /dev/null +++ b/server/room_api.hpp @@ -0,0 +1,30 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * 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 ROOMAPI_HPP_ +#define ROOMAPI_HPP_ + +#include "lua/lua.hpp" + +#define LUA_ROOMLIBNAME "room" +LUAMOD_API int luaopen_roomapi(lua_State* L); + +#endif diff --git a/server/room_manager.hpp b/server/room_manager.hpp index 9b1dfc6..655c960 100644 --- a/server/room_manager.hpp +++ b/server/room_manager.hpp @@ -28,13 +28,15 @@ #include +#define ROOM_MANAGER_PSEUDOINDEX "RoomManager" + class RoomManager { public: RoomManager() = default; ~RoomManager() = default; //public access methods - //TODO + //TODO: Fill this out //accessors and mutators RoomData* GetRoom(int uid); diff --git a/server/server_application.cpp b/server/server_application.cpp index 0cb011d..6fd85d9 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -76,6 +76,7 @@ void ServerApplication::Init(int argc, char** argv) { //Setup the objects //------------------------- + //set the hooks accountMgr.SetDatabase(database); characterMgr.SetDatabase(database); @@ -83,7 +84,14 @@ void ServerApplication::Init(int argc, char** argv) { roomMgr.SetLuaState(luaState); enemyMgr.SetLuaState(luaState); - std::cout << "Internal managers ready" << std::endl; + std::cout << "Internal managers set" << std::endl; + + //register the "globals" + lua_pushstring(luaState, ROOM_MANAGER_PSEUDOINDEX); + lua_pushlightuserdata(luaState, &roomMgr); + lua_settable(luaState, LUA_REGISTRYINDEX); + + std::cout << "Internal managers registered with lua" << std::endl; //------------------------- //Run the startup scripts @@ -110,6 +118,7 @@ void ServerApplication::Init(int argc, char** argv) { std::cout << "\tRegion Format: " << REGION_WIDTH << ", " << REGION_HEIGHT << ", " << REGION_DEPTH << std::endl; std::cout << "\tRegion Content Footprint: " << REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizeof(Region::type_t) << std::endl; std::cout << "\tPACKET_BUFFER_SIZE (max size): " << PACKET_BUFFER_SIZE << std::endl; + std::cout << "\tMAX_PACKET_SIZE: " << MAX_PACKET_SIZE << std::endl; //------------------------- //finalize the startup @@ -121,16 +130,7 @@ void ServerApplication::Init(int argc, char** argv) { //debugging //------------------------- - std::cout << "Debugging dump:" << std::endl; - std::cout << "\tMAX_PACKET_SIZE:\t\t" << MAX_PACKET_SIZE << std::endl; - std::cout << "\tsizeof(SerialPacket):\t\t" << sizeof(SerialPacket) << std::endl; - std::cout << "\tsizeof(CharacterPacket):\t" << sizeof(CharacterPacket) << std::endl; - std::cout << "\t\tsizeof(Statistics):\t" << sizeof(Statistics) << std::endl; - std::cout << "\tsizeof(ClientPacket):\t\t" << sizeof(ClientPacket) << std::endl; - std::cout << "\tsizeof(CombatPacket):\t\t" << sizeof(CombatPacket) << std::endl; - std::cout << "\tsizeof(EnemyPacket):\t\t" << sizeof(EnemyPacket) << std::endl; - std::cout << "\tsizeof(RegionPacket):\t\t" << sizeof(RegionPacket) << std::endl; - std::cout << "\tsizeof(ServerPacket):\t\t" << sizeof(ServerPacket) << std::endl; + //... } void ServerApplication::Proc() {