From 1f099a07dc9dcbb0954f713fc0fc98115fd7b827 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 1 Oct 2014 02:49:25 +1000 Subject: [PATCH 01/11] Changed the command line argument's requirements --- common/utilities/config_utility.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/common/utilities/config_utility.cpp b/common/utilities/config_utility.cpp index e6f9f39..fedf7e0 100644 --- a/common/utilities/config_utility.cpp +++ b/common/utilities/config_utility.cpp @@ -53,15 +53,15 @@ void ConfigUtility::Load(std::string fname, int argc, char* argv[]) { } //set some specific values - if (!strncmp(argv[i], "-C", 2)) { + if (!strncmp(argv[i], "-", 1)) { //wipe the variables memset(key, 0, 256); memset(key, 0, 256); //read the key-value pair - if (sscanf(argv[i], "-C%[^=]=%[^\0]", key, val) != 2) { + if (sscanf(argv[i], "-%[^=]=%[^\0]", key, val) != 2) { std::ostringstream os; - os << "Failed to read a command line config argument (expected -C%s=%s):" << std::endl; + os << "Failed to read a command line config argument (expected -%s=%s):" << std::endl; os << "\targv[" << i << "]: " << argv[i] << std::endl; os << "\tkey: " << key << std::endl; os << "\tval: " << val << std::endl; From 76206a11465a387f2c4995041426e3567831d614 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 4 Oct 2014 19:09:37 +1000 Subject: [PATCH 02/11] Minor fix --- client/client_utilities/makefile | 2 +- todo.txt | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/client/client_utilities/makefile b/client/client_utilities/makefile index 8d12afe..4a01dcd 100644 --- a/client/client_utilities/makefile +++ b/client/client_utilities/makefile @@ -12,7 +12,7 @@ OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) #output OUTDIR=.. -OUT=$(addprefix $(OUTDIR)/,server.a) +OUT=$(addprefix $(OUTDIR)/,client.a) #targets all: $(OBJ) $(OUT) diff --git a/todo.txt b/todo.txt index e4c83ea..4cc86ff 100644 --- a/todo.txt +++ b/todo.txt @@ -10,3 +10,4 @@ TODO: Authentication TODO: Time delay for requesting region packets TODO: A proper logging system +TODO: Update Codebase with the improvements from Tortuga \ No newline at end of file From 5a42a7e36c44dee61252fa62f3beddfcffda9c67 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Tue, 7 Oct 2014 00:55:55 +1100 Subject: [PATCH 03/11] Wrote ManagerInterface, implemented it in RoomManager ManagerInterface is a server-side pure abstract class; all of it's methods are defined as pure virtual methods, and as such should be defined in the derived classes. It works correctly along side the Singleton class, but does not implement it directly as originally planned. It should also support variadic parameters, which still need testing. I've implemented ManagerInterface in RoomManager, but I've also disabled a number of the RoomManager's features, including the lua interface. The project as a whole should build, but it won't run correctly. The variadic parameters will be tested using the other managers. It feels good just playing around instead of pushing forward all the time. --- server/rooms/makefile | 2 +- server/rooms/room_manager.cpp | 76 ++++++++++++------- server/rooms/room_manager.hpp | 49 ++++++------ server/rooms/room_manager_api.cpp | 10 +-- server/server_methods.cpp | 2 +- server/server_utilities/manager_interface.hpp | 55 ++++++++++++++ 6 files changed, 136 insertions(+), 58 deletions(-) create mode 100644 server/server_utilities/manager_interface.hpp diff --git a/server/rooms/makefile b/server/rooms/makefile index ea66828..4b00d80 100644 --- a/server/rooms/makefile +++ b/server/rooms/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. ../../common/map ../../common/utilities +INCLUDES+=. ../server_utilities ../../common/map ../../common/utilities LIBS+= CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) diff --git a/server/rooms/room_manager.cpp b/server/rooms/room_manager.cpp index 4560fe9..bccfd8d 100644 --- a/server/rooms/room_manager.cpp +++ b/server/rooms/room_manager.cpp @@ -29,9 +29,9 @@ //public access methods //------------------------- -int RoomManager::CreateRoom() { - //create the room - RoomData* newRoom = new RoomData(); +int RoomManager::Create() { +/* //create the room + RoomData* newRoom = newRoom->pager.SetLuaState(luaState); //register the room @@ -47,13 +47,21 @@ int RoomManager::CreateRoom() { lua_pop(luaState, 1); //finish the routine - return counter++; +*/ return counter++; } -void RoomManager::UnloadRoom(int uid) { - //find the room - RoomData* room = FindRoom(uid); - if (!room) { +int RoomManager::Load() { + //TODO: RoomManageR::Load() +} + +int RoomManager::Save(int uid) { + //TODO: RoomManageR::Save(uid) +} + +void RoomManager::Unload(int uid) { +/* //find the room + RoomData* room = elementMap.find(uid); + if (room == elementMap.end()) { return; } @@ -69,28 +77,14 @@ void RoomManager::UnloadRoom(int uid) { //free the memory delete room; roomMap.erase(uid); -} +*/} -RoomData* RoomManager::GetRoom(int uid) { - return FindRoom(uid); - //TODO: expand this to auto-create the room -} - -RoomData* RoomManager::FindRoom(int uid) { - std::map::iterator it = roomMap.find(uid); - if (it == roomMap.end()) { - return nullptr; - } - return it->second; -} - -int RoomManager::PushRoom(RoomData* room) { - roomMap[counter++] = room; - return counter; +void RoomManager::Delete(int uid) { + // } void RoomManager::UnloadAll() { - lua_getglobal(luaState, TORTUGA_ROOM_NAME); +/* lua_getglobal(luaState, TORTUGA_ROOM_NAME); for (auto& it : roomMap) { //API hook @@ -103,4 +97,32 @@ void RoomManager::UnloadAll() { lua_pop(luaState, 1); roomMap.clear(); -} \ No newline at end of file +*/} + +void RoomManager::UnloadIf(std::function)> fn) { + std::map::iterator it = elementMap.begin(); + while (it != elementMap.end()) { + if (fn(*it)) { + it = elementMap.erase(it); + } + else { + ++it; + } + } +} + +RoomData* RoomManager::Get(int uid) { + return &elementMap[uid]; +} + +int RoomManager::GetLoadedCount() { + return elementMap.size(); +} + +int RoomManager::GetTotalCount() { + return elementMap.size(); +} + +std::map* RoomManager::GetContainer() { + return &elementMap; +} diff --git a/server/rooms/room_manager.hpp b/server/rooms/room_manager.hpp index 2f13893..7527147 100644 --- a/server/rooms/room_manager.hpp +++ b/server/rooms/room_manager.hpp @@ -23,38 +23,39 @@ #define ROOMMANAGER_HPP_ #include "room_data.hpp" + #include "singleton.hpp" +#include "manager_interface.hpp" #include "lua/lua.hpp" -#include - -class RoomManager : public Singleton { +class RoomManager: public Singleton, public ManagerInterface { public: - //public access methods - int CreateRoom(); - void UnloadRoom(int uid); - - RoomData* GetRoom(int uid); - RoomData* FindRoom(int uid); - int PushRoom(RoomData*); - - void UnloadAll(); - - //accessors and mutators - std::map* GetContainer() { return &roomMap; } - - lua_State* SetLuaState(lua_State* L) { return luaState = L; } - lua_State* GetLuaState() { return luaState; } - -private: - friend Singleton; - RoomManager() = default; ~RoomManager() = default; - std::map roomMap; - lua_State* luaState = nullptr; + //common public methods + int Create() override; + int Load() override; + int Save(int uid) override; + void Unload(int uid) override; + void Delete(int uid) override; + + void UnloadAll() override; + void UnloadIf(std::function)> fn) override; + + //accessors and mutators + RoomData* Get(int uid) override; + int GetLoadedCount() override; + int GetTotalCount() override; + std::map* GetContainer() override; + + //hooks + lua_State* SetLuaState(lua_State* L) { return lua = L; } + lua_State* GetLuaState() { return lua; } + +private: + lua_State* lua = nullptr; int counter = 0; }; diff --git a/server/rooms/room_manager_api.cpp b/server/rooms/room_manager_api.cpp index 02565b9..2c0c516 100644 --- a/server/rooms/room_manager_api.cpp +++ b/server/rooms/room_manager_api.cpp @@ -24,7 +24,7 @@ #include "room_manager.hpp" #include - +/* static int getRoom(lua_State* L) { //find, push and return the room RoomData* room = RoomManager::GetSingleton().GetRoom(lua_tointeger(L, -2)); @@ -46,11 +46,11 @@ static int unloadRoom(lua_State* L) { RoomManager::GetSingleton().UnloadRoom(lua_tointeger(L, -2)); return 0; } - +*/ static const luaL_Reg roomManagerLib[] = { - {"GetRoom",getRoom}, - {"CreateRoom",createRoom}, - {"UnloadRoom",unloadRoom}, +// {"GetRoom",getRoom}, +// {"CreateRoom",createRoom}, +// {"UnloadRoom",unloadRoom}, {nullptr, nullptr} }; diff --git a/server/server_methods.cpp b/server/server_methods.cpp index b211d8f..79f8d46 100644 --- a/server/server_methods.cpp +++ b/server/server_methods.cpp @@ -161,7 +161,7 @@ void ServerApplication::HandleRegionRequest(RegionPacket* const argPacket) { newPacket.y = argPacket->y; //BUG: possibly related to #35 - newPacket.region = roomMgr.GetRoom(argPacket->roomIndex)->GetPager()->GetRegion(argPacket->x, argPacket->y); + newPacket.region = roomMgr.Get(argPacket->roomIndex)->GetPager()->GetRegion(argPacket->x, argPacket->y); //send the content network.SendTo(argPacket->srcAddress, static_cast(&newPacket)); diff --git a/server/server_utilities/manager_interface.hpp b/server/server_utilities/manager_interface.hpp new file mode 100644 index 0000000..8a5161f --- /dev/null +++ b/server/server_utilities/manager_interface.hpp @@ -0,0 +1,55 @@ +/* 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 MANAGERINTERFACE_HPP_ +#define MANAGERINTERFACE_HPP_ + +#include +#include + +template +class ManagerInterface { +public: + //common public methods + virtual int Create(Arguments... parameters) = 0; + virtual int Load(Arguments... parameters) = 0; + virtual int Save(int uid) = 0; + virtual void Unload(int uid) = 0; + virtual void Delete(int uid) = 0; + + virtual void UnloadAll() = 0; + virtual void UnloadIf(std::function)> fn) = 0; + + //accessors & mutators + virtual T* Get(int uid) = 0; + virtual int GetLoadedCount() = 0; + virtual int GetTotalCount() = 0; //can be an alias of GetLoadedCount() + virtual std::map* GetContainer() = 0; + +protected: + ManagerInterface() = default; + ~ManagerInterface() = default; + + //members + std::map elementMap; +}; + +#endif \ No newline at end of file From 254b97aa80dc322970d74ec93c7d1432ce070c66 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Tue, 7 Oct 2014 01:23:55 +1100 Subject: [PATCH 04/11] Implemented ManagerInterface in AccountManager and CharacterManager ManagerInterface was already designed for those two anyway. The only thing left to do is to rewrite the room API, and the whole thing should work fine again. I still think map data should be saved and loaded by lua code, so the rooms will still implement lua hooks. There may be other things that can be loaded from SQL, but I don't know what. --- server/accounts/account_manager.cpp | 36 ++++++++++++------------ server/accounts/account_manager.hpp | 31 +++++++++++---------- server/accounts/makefile | 2 +- server/characters/character_manager.cpp | 37 +++++++++++++------------ server/characters/character_manager.hpp | 31 +++++++++++---------- server/characters/makefile | 2 +- server/rooms/room_manager.cpp | 8 +++++- server/rooms/room_manager.hpp | 14 ++++++---- 8 files changed, 90 insertions(+), 71 deletions(-) diff --git a/server/accounts/account_manager.cpp b/server/accounts/account_manager.cpp index e16ad3c..fccf530 100644 --- a/server/accounts/account_manager.cpp +++ b/server/accounts/account_manager.cpp @@ -87,13 +87,13 @@ int AccountManager::Load(std::string username, int clientIndex) { int uid = sqlite3_column_int(statement, 0); //check to see if this account is already loaded - if (accountMap.find(uid) != accountMap.end()) { + if (elementMap.find(uid) != elementMap.end()) { sqlite3_finalize(statement); return -1; } //extract the data into memory - AccountData& newAccount = accountMap[uid]; + AccountData& newAccount = elementMap[uid]; newAccount.username = reinterpret_cast(sqlite3_column_text(statement, 1)); newAccount.blackListed = sqlite3_column_int(statement, 2); newAccount.whiteListed = sqlite3_column_int(statement, 3); @@ -121,11 +121,11 @@ int AccountManager::Save(int uid) { //DOCS: To use this method, change the in-memory copy, and then call this function using that object's UID. //this method fails if this account is not loaded - if (accountMap.find(uid) == accountMap.end()) { + if (elementMap.find(uid) == elementMap.end()) { return -1; } - AccountData& account = accountMap[uid]; + AccountData& account = elementMap[uid]; sqlite3_stmt* statement = nullptr; //prep @@ -163,7 +163,7 @@ void AccountManager::Unload(int uid) { //save this user account, and then unload it //NOTE: the associated characters are unloaded externally Save(uid); - accountMap.erase(uid); + elementMap.erase(uid); } void AccountManager::Delete(int uid) { @@ -190,25 +190,27 @@ void AccountManager::Delete(int uid) { //finish the routine sqlite3_finalize(statement); - accountMap.erase(uid); + elementMap.erase(uid); } void AccountManager::UnloadAll() { - for (auto& it : accountMap) { + for (auto& it : elementMap) { Save(it.first); } - accountMap.clear(); + elementMap.clear(); } -void AccountManager::UnloadIf(std::function)> fn) { +void AccountManager::UnloadIf(std::function)> fn) { //replicate std::remove_if, using custom code - for (std::map::iterator it = accountMap.begin(); it != accountMap.end(); /* empty */) { + std::map::iterator it = elementMap.begin(); + while (it != elementMap.end()) { if (fn(*it)) { Save(it->first); - it = accountMap.erase(it); - continue; + it = elementMap.erase(it); + } + else { + ++it; } - ++it; } } @@ -218,9 +220,9 @@ void AccountManager::UnloadIf(std::function)> f AccountData* AccountManager::Get(int uid) { //TODO: could this load an account first? - std::map::iterator it = accountMap.find(uid); + std::map::iterator it = elementMap.find(uid); - if (it == accountMap.end()) { + if (it == elementMap.end()) { return nullptr; } @@ -228,7 +230,7 @@ AccountData* AccountManager::Get(int uid) { } int AccountManager::GetLoadedCount() { - return accountMap.size(); + return elementMap.size(); } int AccountManager::GetTotalCount() { @@ -250,7 +252,7 @@ int AccountManager::GetTotalCount() { } std::map* AccountManager::GetContainer() { - return &accountMap; + return &elementMap; } sqlite3* AccountManager::SetDatabase(sqlite3* db) { diff --git a/server/accounts/account_manager.hpp b/server/accounts/account_manager.hpp index 327eb3a..e1bb576 100644 --- a/server/accounts/account_manager.hpp +++ b/server/accounts/account_manager.hpp @@ -24,29 +24,33 @@ #include "account_data.hpp" #include "singleton.hpp" +#include "manager_interface.hpp" #include "sqlite3/sqlite3.h" #include #include -class AccountManager : public Singleton { +class AccountManager: + public Singleton, + public ManagerInterface +{ public: - //public access methods - int Create(std::string username, int clientIndex); - int Load(std::string username, int clientIndex); - int Save(int uid); - void Unload(int uid); - void Delete(int uid); + //common public methods + int Create(std::string username, int clientIndex) override; + int Load(std::string username, int clientIndex) override; + int Save(int uid) override; + void Unload(int uid) override; + void Delete(int uid) override; - void UnloadAll(); - void UnloadIf(std::function)> fn); + void UnloadAll() override; + void UnloadIf(std::function)> fn) override; //accessors and mutators - AccountData* Get(int uid); - int GetLoadedCount(); - int GetTotalCount(); - std::map* GetContainer(); + AccountData* Get(int uid) override; + int GetLoadedCount() override; + int GetTotalCount() override; + std::map* GetContainer() override; sqlite3* SetDatabase(sqlite3* db); sqlite3* GetDatabase(); @@ -57,7 +61,6 @@ private: AccountManager() = default; ~AccountManager() = default; - std::map accountMap; sqlite3* database = nullptr; }; diff --git a/server/accounts/makefile b/server/accounts/makefile index f24e1ba..2a5c671 100644 --- a/server/accounts/makefile +++ b/server/accounts/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. ../../common/utilities +INCLUDES+=. ../server_utilities ../../common/utilities LIBS+= CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) diff --git a/server/characters/character_manager.cpp b/server/characters/character_manager.cpp index 080b0ea..a9079b3 100644 --- a/server/characters/character_manager.cpp +++ b/server/characters/character_manager.cpp @@ -98,7 +98,7 @@ int CharacterManager::Load(int owner, std::string handle, std::string avatar) { int uid = sqlite3_column_int(statement, 0); //check to see if this character is already loaded - if (characterMap.find(uid) != characterMap.end()) { + if (elementMap.find(uid) != elementMap.end()) { sqlite3_finalize(statement); return -1; } @@ -110,7 +110,7 @@ int CharacterManager::Load(int owner, std::string handle, std::string avatar) { } //extract the data into memory - CharacterData& newChar = characterMap[uid]; + CharacterData& newChar = elementMap[uid]; //metadata newChar.owner = owner; @@ -145,11 +145,11 @@ int CharacterManager::Save(int uid) { //DOCS: To use this method, change the in-memory copy, and then call this function using that object's UID. //this method fails if this character is not loaded - if (characterMap.find(uid) == characterMap.end()) { + if (elementMap.find(uid) == elementMap.end()) { return -1; } - CharacterData& character = characterMap[uid]; + CharacterData& character = elementMap[uid]; sqlite3_stmt* statement = nullptr; //prep @@ -187,7 +187,7 @@ int CharacterManager::Save(int uid) { void CharacterManager::Unload(int uid) { //save this character, then unload it Save(uid); - characterMap.erase(uid); + elementMap.erase(uid); } void CharacterManager::Delete(int uid) { @@ -213,25 +213,26 @@ void CharacterManager::Delete(int uid) { //finish the routine sqlite3_finalize(statement); - characterMap.erase(uid); + elementMap.erase(uid); } void CharacterManager::UnloadAll() { - for (auto& it : characterMap) { + for (auto& it : elementMap) { Save(it.first); } - characterMap.clear(); + elementMap.clear(); } -void CharacterManager::UnloadIf(std::function)> fn) { - //replicate std::remove_if, using custom code - for (std::map::iterator it = characterMap.begin(); it != characterMap.end(); /* empty */) { +void CharacterManager::UnloadIf(std::function)> fn) { + std::map::iterator it = elementMap.begin(); + while (it != elementMap.end()) { if (fn(*it)) { Save(it->first); - it = characterMap.erase(it); - continue; + it = elementMap.erase(it); + } + else { + ++it; } - ++it; } } @@ -240,9 +241,9 @@ void CharacterManager::UnloadIf(std::function //------------------------- CharacterData* CharacterManager::Get(int uid) { - std::map::iterator it = characterMap.find(uid); + std::map::iterator it = elementMap.find(uid); - if (it == characterMap.end()) { + if (it == elementMap.end()) { return nullptr; } @@ -250,7 +251,7 @@ CharacterData* CharacterManager::Get(int uid) { } int CharacterManager::GetLoadedCount() { - return characterMap.size(); + return elementMap.size(); } int CharacterManager::GetTotalCount() { @@ -272,7 +273,7 @@ int CharacterManager::GetTotalCount() { } std::map* CharacterManager::GetContainer() { - return &characterMap; + return &elementMap; } sqlite3* CharacterManager::SetDatabase(sqlite3* db) { diff --git a/server/characters/character_manager.hpp b/server/characters/character_manager.hpp index 68b58aa..d73cd42 100644 --- a/server/characters/character_manager.hpp +++ b/server/characters/character_manager.hpp @@ -24,29 +24,33 @@ #include "character_data.hpp" #include "singleton.hpp" +#include "manager_interface.hpp" #include "sqlite3/sqlite3.h" #include #include -class CharacterManager : public Singleton { +class CharacterManager: + public Singleton, + public ManagerInterface +{ public: - //public access methods - int Create(int owner, std::string handle, std::string avatar); - int Load(int owner, std::string handle, std::string avatar); - int Save(int uid); - void Unload(int uid); - void Delete(int uid); + //common public methods + int Create(int owner, std::string handle, std::string avatar) override; + int Load(int owner, std::string handle, std::string avatar) override; + int Save(int uid) override; + void Unload(int uid) override; + void Delete(int uid) override; - void UnloadAll(); - void UnloadIf(std::function)> fn); + void UnloadAll() override; + void UnloadIf(std::function)> fn) override; //accessors and mutators - CharacterData* Get(int uid); - int GetLoadedCount(); - int GetTotalCount(); - std::map* GetContainer(); + CharacterData* Get(int uid) override; + int GetLoadedCount() override; + int GetTotalCount() override; + std::map* GetContainer() override; sqlite3* SetDatabase(sqlite3* db); sqlite3* GetDatabase(); @@ -57,7 +61,6 @@ private: CharacterManager() = default; ~CharacterManager() = default; - std::map characterMap; sqlite3* database = nullptr; }; diff --git a/server/characters/makefile b/server/characters/makefile index 68368e7..7133d20 100644 --- a/server/characters/makefile +++ b/server/characters/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. ../../common/gameplay ../../common/utilities +INCLUDES+=. ../server_utilities ../../common/gameplay ../../common/utilities LIBS+= CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) diff --git a/server/rooms/room_manager.cpp b/server/rooms/room_manager.cpp index bccfd8d..5d0329e 100644 --- a/server/rooms/room_manager.cpp +++ b/server/rooms/room_manager.cpp @@ -112,7 +112,13 @@ void RoomManager::UnloadIf(std::function)> fn } RoomData* RoomManager::Get(int uid) { - return &elementMap[uid]; + std::map::iterator it = elementMap.find(uid); + + if (it == elementMap.end()) { + return nullptr; + } + + return &it->second; } int RoomManager::GetLoadedCount() { diff --git a/server/rooms/room_manager.hpp b/server/rooms/room_manager.hpp index 7527147..5245d9c 100644 --- a/server/rooms/room_manager.hpp +++ b/server/rooms/room_manager.hpp @@ -23,17 +23,16 @@ #define ROOMMANAGER_HPP_ #include "room_data.hpp" - #include "singleton.hpp" #include "manager_interface.hpp" #include "lua/lua.hpp" -class RoomManager: public Singleton, public ManagerInterface { +class RoomManager: + public Singleton, + public ManagerInterface +{ public: - RoomManager() = default; - ~RoomManager() = default; - //common public methods int Create() override; int Load() override; @@ -55,6 +54,11 @@ public: lua_State* GetLuaState() { return lua; } private: + friend Singleton; + + RoomManager() = default; + ~RoomManager() = default; + lua_State* lua = nullptr; int counter = 0; }; From 869780589f7f2f41d002783eb793ec4443559713 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Tue, 7 Oct 2014 02:08:00 +1100 Subject: [PATCH 05/11] finished rewriting the room API --- server/rooms/room_api.cpp | 29 ++++++++++---- server/rooms/room_data.hpp | 6 +++ server/rooms/room_manager.cpp | 66 +++++++++---------------------- server/rooms/room_manager_api.cpp | 46 ++++++++++----------- 4 files changed, 69 insertions(+), 78 deletions(-) diff --git a/server/rooms/room_api.cpp b/server/rooms/room_api.cpp index 636d206..bc8b1a1 100644 --- a/server/rooms/room_api.cpp +++ b/server/rooms/room_api.cpp @@ -29,21 +29,36 @@ static int getPager(lua_State* L) { return 1; } -static int create(lua_State* L) { - //EMPTY - //NOTE: This can be used to set defaults for the pager +static int setRoomName(lua_State* L) { + RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); + room->SetRoomName(lua_tostring(L, 2)); return 0; } -static int unload(lua_State* L) { - //EMPTY +static int getRoomName(lua_State* L) { + RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); + lua_pushstring(L, room->GetRoomName().c_str()); + return 1; +} + +static int setTilesetName(lua_State* L) { + RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); + room->SetTilesetName(lua_tostring(L, 2)); return 0; } +static int getTilesetName(lua_State* L) { + RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); + lua_pushstring(L, room->GetTilesetName().c_str()); + return 1; +} + static const luaL_Reg roomLib[] = { {"GetPager",getPager}, - {"Create", create}, - {"Unload", unload}, + {"SetRoomName", setRoomName}, + {"GetRoomName", getRoomName}, + {"SetTileset", setTilesetName}, + {"GetTileset", getTilesetName}, {nullptr, nullptr} }; diff --git a/server/rooms/room_data.hpp b/server/rooms/room_data.hpp index 1ced1be..046b119 100644 --- a/server/rooms/room_data.hpp +++ b/server/rooms/room_data.hpp @@ -35,6 +35,9 @@ public: //accessors and mutators RegionPagerLua* GetPager() { return &pager; } + std::string SetRoomName(std::string s) { return roomName = s; } + std::string GetRoomName() { return roomName; } + std::string SetTilesetName(std::string s) { return tilesetName = s; } std::string GetTilesetName() { return tilesetName; } @@ -43,7 +46,10 @@ private: //members RegionPagerLua pager; + std::string roomName; std::string tilesetName; + //TODO: pass the room name & tileset name to the clients + //TODO: lua references i.e. create, unload, etc. }; #endif diff --git a/server/rooms/room_manager.cpp b/server/rooms/room_manager.cpp index 5d0329e..a697201 100644 --- a/server/rooms/room_manager.cpp +++ b/server/rooms/room_manager.cpp @@ -30,74 +30,44 @@ //------------------------- int RoomManager::Create() { -/* //create the room - RoomData* newRoom = - newRoom->pager.SetLuaState(luaState); - - //register the room - roomMap[counter] = newRoom; - - //API hook - lua_getglobal(luaState, TORTUGA_ROOM_NAME); - lua_getfield(luaState, -1, "Create"); - lua_pushlightuserdata(luaState, newRoom); - if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) { - throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) )); - } - lua_pop(luaState, 1); + //create the room + RoomData* newRoom = &elementMap[counter]; //implicitly constructs the element + newRoom->pager.SetLuaState(lua); //finish the routine -*/ return counter++; + return counter++; } int RoomManager::Load() { - //TODO: RoomManageR::Load() + //TODO: RoomManager::Load() + return -1; } int RoomManager::Save(int uid) { - //TODO: RoomManageR::Save(uid) + //TODO: RoomManager::Save(uid) + return -1; } void RoomManager::Unload(int uid) { -/* //find the room - RoomData* room = elementMap.find(uid); - if (room == elementMap.end()) { + //find the room + std::map::iterator it = elementMap.find(uid); + if (it == elementMap.end()) { return; } - //API hook - lua_getglobal(luaState, TORTUGA_ROOM_NAME); - lua_getfield(luaState, -1, "Unload"); - lua_pushlightuserdata(luaState, room); - if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) { - throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) )); - } - lua_pop(luaState, 1); - //free the memory - delete room; - roomMap.erase(uid); -*/} + elementMap.erase(uid); +} void RoomManager::Delete(int uid) { - // + //TODO: RoomManager::Delete(int uid) + //NOTE: aliased to RoomManager::Unload(int uid) + Unload(uid); } void RoomManager::UnloadAll() { -/* lua_getglobal(luaState, TORTUGA_ROOM_NAME); - - for (auto& it : roomMap) { - //API hook - lua_getfield(luaState, -1, "Unload"); - lua_pushlightuserdata(luaState, it.second); - if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) { - throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) )); - } - } - - lua_pop(luaState, 1); - roomMap.clear(); -*/} + elementMap.clear(); +} void RoomManager::UnloadIf(std::function)> fn) { std::map::iterator it = elementMap.begin(); diff --git a/server/rooms/room_manager_api.cpp b/server/rooms/room_manager_api.cpp index 2c0c516..ab4d6d9 100644 --- a/server/rooms/room_manager_api.cpp +++ b/server/rooms/room_manager_api.cpp @@ -23,34 +23,34 @@ #include "room_manager.hpp" -#include -/* -static int getRoom(lua_State* L) { - //find, push and return the room - RoomData* room = RoomManager::GetSingleton().GetRoom(lua_tointeger(L, -2)); - lua_pushlightuserdata(L, reinterpret_cast(room)); - return 1; +int createRoom(lua_State* L) { + //create & get the room + RoomManager& roomMgr = RoomManager::GetSingleton(); + int uid = roomMgr.Create(); + RoomData* room = roomMgr.Get(uid); + + //setup the room + //TODO: room parameters only set via lua, fix this + room->SetRoomName(lua_tostring(L, 1)); + room->SetTilesetName(lua_tostring(L, 2)); + + //return room, uid + lua_pushlightuserdata(L, static_cast(room)); + lua_pushinteger(L, uid); + + return 2; } -static int createRoom(lua_State* L) { - //TODO: check parameter count for the glue functions - - //create, find and return the room - int uid = RoomManager::GetSingleton().CreateRoom(); - lua_pushlightuserdata(L, RoomManager::GetSingleton().FindRoom(uid)); - return 1; -} - -static int unloadRoom(lua_State* L) { - //unload the specified room - RoomManager::GetSingleton().UnloadRoom(lua_tointeger(L, -2)); +int unloadRoom(lua_State* L) { + //TODO: check authorization for room deletion + RoomManager& roomMgr = RoomManager::GetSingleton(); + roomMgr.Unload(lua_tointeger(L, 1)); return 0; } -*/ + static const luaL_Reg roomManagerLib[] = { -// {"GetRoom",getRoom}, -// {"CreateRoom",createRoom}, -// {"UnloadRoom",unloadRoom}, + {"CreateRoom", createRoom}, + {"UnloadRoom", unloadRoom}, {nullptr, nullptr} }; From 4d6186021fc81a54bab05efc1fb222e8f11bfe12 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Tue, 7 Oct 2014 22:22:08 +1100 Subject: [PATCH 06/11] Added 'skume' sprites --- rsc/graphics/sprites/skume1.bmp | Bin 0 -> 1041 bytes rsc/graphics/sprites/skume2.bmp | Bin 0 -> 784 bytes 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 rsc/graphics/sprites/skume1.bmp create mode 100644 rsc/graphics/sprites/skume2.bmp diff --git a/rsc/graphics/sprites/skume1.bmp b/rsc/graphics/sprites/skume1.bmp new file mode 100644 index 0000000000000000000000000000000000000000..ae31c9a93d317d48da9e4db084b65cf505efef27 GIT binary patch literal 1041 zcmV+s1n&EZP)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!Tzd z+%OP?&n_cCUeZ9GQbhu!l2nsQ0>oA1DGlT$K-#!?gd2lLOCtA=q)dIrK_}}h?d%L! zXFXgm*X!kES{`rPlSv7k01wYUYdl|b+46Y4pTpxP{)7bpWysqFrSPV0-kEl@N8Y63vK#7hGeJq1du-`jMF zevS6(2~aX0r0>0^fwZALr+~NlAmW@UrvC!=isLbl5R5|21nj9-6aK0Il*V34+6-%B zw13xQ3Z#ocQ}Y&{=USuq9iWu@Em(Nzb2*-ZJ--js_93rds(%e>Pb9i`1E4jZ)P%s( z@HIR_Anik{JJlx*gQYjMy2SzT7O{3d&rnzc@YW~bW&o_KUyGyxs0E+rb_;C?W|)Ah z0cdHywgIJD^7Cp!FvA4w8h|MCfi(cLOu$zGXdzr{Jdo6(4Z%zk@a{RlmT(VH5MLS~ zSZYB2d1!;iQHU~Mg6Tew2Gdp)q$ z(rXdq*UhyTCjc%ixi~I3viYs?=!Zuywpb>BHTm9+Zef1D5@wx%rT};w4_cTH?wT<1 z0#F4Y!g!z&uC2u*01f^o&Z2n)Ck7!8CN?2h_=2F`mly50A#PBWE8rjwd_{(SwpY-e5o#Row(NCZb44{FU{o`e~-lTl13wLNeN zKo6u1&tU+-%=(uA;1?q{!76f#)O<3l{+9rN7EOo}Rm+3Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!TuM~%UPOti)y8?Iwz%@U9Z>LxDJl_0Fuk5|dA=T`n0GLFwi$4e0B1@H@(7FgTI2NFG0cc)L`@#kE7{Kep*V)m6=lsXFfi(ug;H698 zZ%LM30I=4l{lxaNmiagP2&la~Y2c;HDV=}s(LMl8Xj|YJ0M~APG}I!Q9|??B1%Q#% z66iLhS;Eo)v~Iz%W#_`TdIU)4H!DDi5sdP+5vzP4=LRe zO`=AdqdC8N0l2x+76Q)zz<UO2+shB&_hVf z6W0o<)wZ7hq5y<;5hOw60;r)h00JIBs}JY`srFLyM+;y{Nf@31aL5NI=83d8t-99p zp9%n$C_zd1oVfrb)CRzT2b_n0pCkMkE$tR^9?l8?S_N}xEai$8!|(?ayKlwY1Qh20 O0000 Date: Fri, 10 Oct 2014 06:00:58 +1100 Subject: [PATCH 07/11] Separated some character code into Renderable --- client/character.cpp | 4 -- client/character.hpp | 28 ++------------ client/renderable.cpp | 31 +++++++++++++++ client/renderable.hpp | 56 +++++++++++++++++++++++++++ common/gameplay/character_defines.hpp | 1 + 5 files changed, 92 insertions(+), 28 deletions(-) create mode 100644 client/renderable.cpp create mode 100644 client/renderable.hpp diff --git a/client/character.cpp b/client/character.cpp index 70ee4a9..5970adc 100644 --- a/client/character.cpp +++ b/client/character.cpp @@ -31,10 +31,6 @@ void Character::Update() { sprite.Update(0.016); } -void Character::DrawTo(SDL_Surface* const dest, int camX, int camY) { - sprite.DrawTo(dest, origin.x - camX, origin.y - camY); -} - void Character::CorrectSprite() { //NOTE: These must correspond to the sprite sheet in use if (motion.y > 0) { diff --git a/client/character.hpp b/client/character.hpp index 6be3cc6..1178495 100644 --- a/client/character.hpp +++ b/client/character.hpp @@ -24,28 +24,22 @@ //components #include "character_defines.hpp" -#include "vector2.hpp" -#include "bounding_box.hpp" +#include "renderable.hpp" #include "statistics.hpp" -//graphics -#include "sprite_sheet.hpp" - //std namespace #include #include -class Character { +class Character : public Renderable { public: Character() = default; ~Character() = default; - void Update(); + void Update() override; //graphics - void DrawTo(SDL_Surface* const, int camX, int camY); void CorrectSprite(); - SpriteSheet* GetSprite() { return &sprite; } //gameplay Statistics* GetStats() { return &stats; } @@ -61,17 +55,8 @@ public: std::string GetAvatar() const { return avatar; } //position - Vector2 SetOrigin(Vector2 v) { return origin = v; } - Vector2 GetOrigin() const { return origin; } - Vector2 SetMotion(Vector2 v) { return motion = v; } - Vector2 GetMotion() const { return motion; } - BoundingBox SetBounds(BoundingBox b) { return bounds = b; } - BoundingBox GetBounds() { return bounds; } - + private: - //graphics - SpriteSheet sprite; - //base statistics Statistics stats; @@ -81,11 +66,6 @@ private: int owner; std::string handle; std::string avatar; - - //position - Vector2 origin = {0.0,0.0}; - Vector2 motion = {0.0,0.0}; - BoundingBox bounds; }; //tmp diff --git a/client/renderable.cpp b/client/renderable.cpp new file mode 100644 index 0000000..46d319d --- /dev/null +++ b/client/renderable.cpp @@ -0,0 +1,31 @@ +/* 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 "renderable.hpp" + +void Renderable::Update() { + origin += motion; + sprite.Update(0.016); +} + +void Renderable::DrawTo(SDL_Surface* const dest, int camX, int camY) { + sprite.DrawTo(dest, origin.x - camX, origin.y - camY); +} \ No newline at end of file diff --git a/client/renderable.hpp b/client/renderable.hpp new file mode 100644 index 0000000..d78cde2 --- /dev/null +++ b/client/renderable.hpp @@ -0,0 +1,56 @@ +/* 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 RENDERABLE_HPP_ +#define RENDERABLE_HPP_ + +#include "bounding_box.hpp" +#include "sprite_sheet.hpp" +#include "vector2.hpp" + +class Renderable { +public: + Renderable() = default; + virtual ~Renderable() = default; + + virtual void Update(); + virtual void DrawTo(SDL_Surface* const, int camX, int camY); + + SpriteSheet* GetSprite() { return &sprite; } + + //position + Vector2 SetOrigin(Vector2 v) { return origin = v; } + Vector2 GetOrigin() const { return origin; } + Vector2 SetMotion(Vector2 v) { return motion = v; } + Vector2 GetMotion() const { return motion; } + + //collision + BoundingBox SetBounds(BoundingBox b) { return bounds = b; } + BoundingBox GetBounds() { return bounds; } + +protected: //TODO: should be private + SpriteSheet sprite; + Vector2 origin = {0, 0}; + Vector2 motion = {0, 0}; + BoundingBox bounds; +}; + +#endif \ No newline at end of file diff --git a/common/gameplay/character_defines.hpp b/common/gameplay/character_defines.hpp index 484fcfa..d5ca3b6 100644 --- a/common/gameplay/character_defines.hpp +++ b/common/gameplay/character_defines.hpp @@ -27,6 +27,7 @@ //the speeds that the characters move constexpr double CHARACTER_WALKING_SPEED = 2.24; constexpr double CHARACTER_WALKING_MOD = 1.0/sqrt(2.0); +constexpr double CHARACTER_WALKING_NEGATIVE_MOD = 1.0 - CHARACTER_WALKING_MOD; //the bounds for the character objects, mapped to the default sprites constexpr int CHARACTER_BOUNDS_X = 0; From 7c4762852bfaefd26f7ee3b882b179f7a0d3179a Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 10 Oct 2014 06:16:31 +1100 Subject: [PATCH 08/11] Moved Renderable and Character into Renderable/ --- .../base_character.cpp} | 10 ----- .../base_character.hpp} | 6 --- client/renderable/makefile | 37 +++++++++++++++++++ client/{ => renderable}/renderable.cpp | 0 client/{ => renderable}/renderable.hpp | 0 common/gameplay/combat_defines.hpp | 34 ----------------- 6 files changed, 37 insertions(+), 50 deletions(-) rename client/{character.cpp => renderable/base_character.cpp} (88%) rename client/{character.hpp => renderable/base_character.hpp} (96%) create mode 100644 client/renderable/makefile rename client/{ => renderable}/renderable.cpp (100%) rename client/{ => renderable}/renderable.hpp (100%) delete mode 100644 common/gameplay/combat_defines.hpp diff --git a/client/character.cpp b/client/renderable/base_character.cpp similarity index 88% rename from client/character.cpp rename to client/renderable/base_character.cpp index 5970adc..71cc13a 100644 --- a/client/character.cpp +++ b/client/renderable/base_character.cpp @@ -21,16 +21,6 @@ */ #include "character.hpp" -void Character::Update() { - if (motion.x && motion.y) { - origin += motion * CHARACTER_WALKING_MOD; - } - else if (motion != 0) { - origin += motion; - } - sprite.Update(0.016); -} - void Character::CorrectSprite() { //NOTE: These must correspond to the sprite sheet in use if (motion.y > 0) { diff --git a/client/character.hpp b/client/renderable/base_character.hpp similarity index 96% rename from client/character.hpp rename to client/renderable/base_character.hpp index 1178495..cd9fc43 100644 --- a/client/character.hpp +++ b/client/renderable/base_character.hpp @@ -36,16 +36,12 @@ public: Character() = default; ~Character() = default; - void Update() override; - //graphics void CorrectSprite(); //gameplay Statistics* GetStats() { return &stats; } - //accessors and mutators - //metadata int SetOwner(int i) { return owner = i; } int GetOwner() { return owner; } @@ -54,8 +50,6 @@ public: std::string SetAvatar(std::string s) { return avatar = s; } std::string GetAvatar() const { return avatar; } - //position - private: //base statistics Statistics stats; diff --git a/client/renderable/makefile b/client/renderable/makefile new file mode 100644 index 0000000..f8d8358 --- /dev/null +++ b/client/renderable/makefile @@ -0,0 +1,37 @@ +#config +INCLUDES+=. .. ../../common/gameplay ../../common/graphics ../../common/map ../../common/network ../../common/network/packet_types ../../common/ui ../../common/utilities +LIBS+= +CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) + +#source +CXXSRC=$(wildcard *.cpp) + +#objects +OBJDIR=obj +OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) + +#output +OUTDIR=.. +OUT=$(addprefix $(OUTDIR)/,client.a) + +#targets +all: $(OBJ) $(OUT) + ar -crs $(OUT) $(OBJ) + +$(OBJ): | $(OBJDIR) + +$(OUT): | $(OUTDIR) + +$(OBJDIR): + mkdir $(OBJDIR) + +$(OUTDIR): + mkdir $(OUTDIR) + +$(OBJDIR)/%.o: %.cpp + $(CXX) $(CXXFLAGS) -c -o $@ $< + +clean: + $(RM) *.o *.a *.exe + +rebuild: clean all diff --git a/client/renderable.cpp b/client/renderable/renderable.cpp similarity index 100% rename from client/renderable.cpp rename to client/renderable/renderable.cpp diff --git a/client/renderable.hpp b/client/renderable/renderable.hpp similarity index 100% rename from client/renderable.hpp rename to client/renderable/renderable.hpp diff --git a/common/gameplay/combat_defines.hpp b/common/gameplay/combat_defines.hpp deleted file mode 100644 index db6421c..0000000 --- a/common/gameplay/combat_defines.hpp +++ /dev/null @@ -1,34 +0,0 @@ -/* 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 COMBATDEFINES_HPP_ -#define COMBATDEFINES_HPP_ - -#define COMBAT_MAX_CHARACTERS 16 -#define COMBAT_MAX_ENEMIES 16 - -enum class TerrainType { - NONE = 0, - GRASSLANDS, - //etc. -}; - -#endif From 17611348392672fae02c68c7b04b820f3d06aeb6 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 10 Oct 2014 06:32:53 +1100 Subject: [PATCH 09/11] Added LocalCharacter & BaseMonster --- client/renderable/base_character.cpp | 4 +-- client/renderable/base_character.hpp | 24 ++++------------ client/renderable/base_monster.cpp | 23 +++++++++++++++ client/renderable/base_monster.hpp | 36 ++++++++++++++++++++++++ client/renderable/local_character.cpp | 23 +++++++++++++++ client/renderable/local_character.hpp | 40 +++++++++++++++++++++++++++ client/renderable/makefile | 2 +- 7 files changed, 130 insertions(+), 22 deletions(-) create mode 100644 client/renderable/base_monster.cpp create mode 100644 client/renderable/base_monster.hpp create mode 100644 client/renderable/local_character.cpp create mode 100644 client/renderable/local_character.hpp diff --git a/client/renderable/base_character.cpp b/client/renderable/base_character.cpp index 71cc13a..ad31e92 100644 --- a/client/renderable/base_character.cpp +++ b/client/renderable/base_character.cpp @@ -19,9 +19,9 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#include "character.hpp" +#include "base_character.hpp" -void Character::CorrectSprite() { +void BaseCharacter::CorrectSprite() { //NOTE: These must correspond to the sprite sheet in use if (motion.y > 0) { sprite.SetYIndex(0); diff --git a/client/renderable/base_character.hpp b/client/renderable/base_character.hpp index cd9fc43..1531ba3 100644 --- a/client/renderable/base_character.hpp +++ b/client/renderable/base_character.hpp @@ -19,29 +19,24 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#ifndef CHARACTER_HPP_ -#define CHARACTER_HPP_ +#ifndef BASECHARACTER_HPP_ +#define BASECHARACTER_HPP_ //components #include "character_defines.hpp" #include "renderable.hpp" -#include "statistics.hpp" //std namespace #include -#include -class Character : public Renderable { +class BaseCharacter : public Renderable { public: - Character() = default; - ~Character() = default; + BaseCharacter() = default; + virtual ~BaseCharacter() = default; //graphics void CorrectSprite(); - //gameplay - Statistics* GetStats() { return &stats; } - //metadata int SetOwner(int i) { return owner = i; } int GetOwner() { return owner; } @@ -51,19 +46,10 @@ public: std::string GetAvatar() const { return avatar; } private: - //base statistics - Statistics stats; - - //gameplay components: equipment, items, buffs, debuffs... - //metadata int owner; std::string handle; std::string avatar; }; -//tmp -#include -typedef std::map CharacterMap; - #endif diff --git a/client/renderable/base_monster.cpp b/client/renderable/base_monster.cpp new file mode 100644 index 0000000..e6350c4 --- /dev/null +++ b/client/renderable/base_monster.cpp @@ -0,0 +1,23 @@ +/* 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 "base_monster.hpp" + diff --git a/client/renderable/base_monster.hpp b/client/renderable/base_monster.hpp new file mode 100644 index 0000000..58d8f49 --- /dev/null +++ b/client/renderable/base_monster.hpp @@ -0,0 +1,36 @@ +/* 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 BASEMONSTER_HPP_ +#define BASEMONSTER_HPP_ + +#include "renderable.hpp" + +class BaseMonster { +public: + BaseMonster(); + virtual ~BaseMonster(); + +private: + // +}; + +#endif \ No newline at end of file diff --git a/client/renderable/local_character.cpp b/client/renderable/local_character.cpp new file mode 100644 index 0000000..211a6bc --- /dev/null +++ b/client/renderable/local_character.cpp @@ -0,0 +1,23 @@ +/* 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 "local_character.hpp" + diff --git a/client/renderable/local_character.hpp b/client/renderable/local_character.hpp new file mode 100644 index 0000000..8407fef --- /dev/null +++ b/client/renderable/local_character.hpp @@ -0,0 +1,40 @@ +/* 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 LOCALCHARACTER_HPP_ +#define LOCALCHARACTER_HPP_ + +#include "base_character.hpp" +#include "statistics.hpp" + +class LocalCharacter : public BaseCharacter { +public: + LocalCharacter() = default; + ~LocalCharacter() = default; + + Statistics* GetBaseStats() { return &baseStats; } + +private: + Statistics baseStats; + //TODO: weapons, armour, buffs, debuffs, etc. +}; + +#endif diff --git a/client/renderable/makefile b/client/renderable/makefile index f8d8358..a426eda 100644 --- a/client/renderable/makefile +++ b/client/renderable/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. .. ../../common/gameplay ../../common/graphics ../../common/map ../../common/network ../../common/network/packet_types ../../common/ui ../../common/utilities +INCLUDES+=. .. ../../common/gameplay ../../common/graphics ../../common/utilities LIBS+= CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) From f3073efa39cf81c991d7f115d8326efb8d5023cd Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 10 Oct 2014 06:35:50 +1100 Subject: [PATCH 10/11] Renamed clean_up.*pp to disconnected_screen.*pp --- client/scenes/{clean_up.cpp => disconnected_screen.cpp} | 0 client/scenes/{clean_up.hpp => disconnected_screen.hpp} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename client/scenes/{clean_up.cpp => disconnected_screen.cpp} (100%) rename client/scenes/{clean_up.hpp => disconnected_screen.hpp} (100%) diff --git a/client/scenes/clean_up.cpp b/client/scenes/disconnected_screen.cpp similarity index 100% rename from client/scenes/clean_up.cpp rename to client/scenes/disconnected_screen.cpp diff --git a/client/scenes/clean_up.hpp b/client/scenes/disconnected_screen.hpp similarity index 100% rename from client/scenes/clean_up.hpp rename to client/scenes/disconnected_screen.hpp From 974effd95e03a5d3897a94b8dc219795fd8f674d Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 10 Oct 2014 06:54:48 +1100 Subject: [PATCH 11/11] Implemented DisconnectedScreen, and removed CharacterMap Project also builds, since I went through and adjusted it. It's hacky right now, but I just want to make sure it worked. I'll give it another pass before merging into develop. --- client/client_application.cpp | 14 ++++----- client/client_application.hpp | 4 --- client/makefile | 3 +- client/scene_list.hpp | 3 +- client/scenes/disconnected_screen.cpp | 43 +++++++++------------------ client/scenes/disconnected_screen.hpp | 26 ++++------------ client/scenes/in_world.cpp | 25 ++++++---------- client/scenes/in_world.hpp | 16 ++++------ client/scenes/lobby_menu.cpp | 4 +++ client/scenes/makefile | 2 +- 10 files changed, 47 insertions(+), 93 deletions(-) diff --git a/client/client_application.cpp b/client/client_application.cpp index 4ae481b..3d287fc 100644 --- a/client/client_application.cpp +++ b/client/client_application.cpp @@ -38,8 +38,7 @@ #include "options_menu.hpp" #include "lobby_menu.hpp" #include "in_world.hpp" -//#include "in_combat.hpp" -#include "clean_up.hpp" +#include "disconnected_screen.hpp" //------------------------- //Public access members @@ -179,16 +178,13 @@ void ClientApplication::LoadScene(SceneList sceneIndex) { activeScene = new OptionsMenu(); break; case SceneList::LOBBYMENU: - activeScene = new LobbyMenu(&clientIndex, &accountIndex); + activeScene = new LobbyMenu(&clientIndex, &accountIndex); //TODO: can I use the ConfigUtility for these parameters? break; case SceneList::INWORLD: - activeScene = new InWorld(&clientIndex, &accountIndex, &characterIndex, &characterMap); + activeScene = new InWorld(&clientIndex, &accountIndex); break; -// case SceneList::INCOMBAT: -// activeScene = new InCombat(&clientIndex, &accountIndex, &characterIndex, &characterMap); -// break; - case SceneList::CLEANUP: - activeScene = new CleanUp(&clientIndex, &accountIndex, &characterIndex, &characterMap); + case SceneList::DISCONNECTEDSCREEN: + activeScene = new DisconnectedScreen(); break; default: throw(std::logic_error("Failed to recognize the scene index")); diff --git a/client/client_application.hpp b/client/client_application.hpp index dbbc785..7e99458 100644 --- a/client/client_application.hpp +++ b/client/client_application.hpp @@ -26,7 +26,6 @@ #include "base_scene.hpp" #include "udp_network_utility.hpp" -#include "character.hpp" #include "singleton.hpp" @@ -54,9 +53,6 @@ private: //shared parameters int clientIndex = -1; int accountIndex = -1; - int characterIndex = -1; - - CharacterMap characterMap; }; #endif diff --git a/client/makefile b/client/makefile index c46fbcb..ade60d1 100644 --- a/client/makefile +++ b/client/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. client_utilities scenes ../common/debugging ../common/gameplay ../common/graphics ../common/map ../common/network ../common/network/packet_types ../common/ui ../common/utilities +INCLUDES+=. client_utilities renderable scenes ../common/debugging ../common/gameplay ../common/graphics ../common/map ../common/network ../common/network/packet_types ../common/ui ../common/utilities LIBS+=client.a ../libcommon.a -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL -llua CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) @@ -18,6 +18,7 @@ OUT=$(addprefix $(OUTDIR)/,client) all: $(OBJ) $(OUT) $(MAKE) -C client_utilities $(MAKE) -C scenes + $(MAKE) -C renderable $(CXX) $(CXXFLAGS) -o $(OUT) $(OBJ) $(LIBS) $(OBJ): | $(OBJDIR) diff --git a/client/scene_list.hpp b/client/scene_list.hpp index 804b9c9..82895cd 100644 --- a/client/scene_list.hpp +++ b/client/scene_list.hpp @@ -34,8 +34,7 @@ enum class SceneList { OPTIONSMENU, LOBBYMENU, INWORLD, - INCOMBAT, - CLEANUP, + DISCONNECTEDSCREEN, }; #endif diff --git a/client/scenes/disconnected_screen.cpp b/client/scenes/disconnected_screen.cpp index 54ced25..7d95293 100644 --- a/client/scenes/disconnected_screen.cpp +++ b/client/scenes/disconnected_screen.cpp @@ -19,10 +19,11 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#include "clean_up.hpp" +#include "disconnected_screen.hpp" #include "channels.hpp" #include "config_utility.hpp" +#include "udp_network_utility.hpp" #include @@ -30,17 +31,7 @@ //Public access members //------------------------- -CleanUp::CleanUp( - int* const argClientIndex, - int* const argAccountIndex, - int* const argCharacterIndex, - CharacterMap* argCharacterMap - ): - clientIndex(*argClientIndex), - accountIndex(*argAccountIndex), - characterIndex(*argCharacterIndex), - characterMap(*argCharacterMap) -{ +DisconnectedScreen::DisconnectedScreen() { ConfigUtility& config = ConfigUtility::GetSingleton(); //setup the utility objects @@ -60,19 +51,13 @@ CleanUp::CleanUp( backButton.SetText("Back"); //full reset - network.Unbind(Channels::SERVER); - clientIndex = -1; - accountIndex = -1; - characterIndex = -1; -// combatMap.clear(); - characterMap.clear(); -// enemyMap.clear(); + UDPNetworkUtility::GetSingleton().Unbind(Channels::SERVER); //auto return startTick = std::chrono::steady_clock::now(); } -CleanUp::~CleanUp() { +DisconnectedScreen::~DisconnectedScreen() { // } @@ -80,16 +65,16 @@ CleanUp::~CleanUp() { //Frame loop //------------------------- -void CleanUp::Update() { +void DisconnectedScreen::Update() { if (std::chrono::steady_clock::now() - startTick > std::chrono::duration(10)) { SetNextScene(SceneList::MAINMENU); } //Eat incoming packets - while(network.Receive()); + while(UDPNetworkUtility::GetSingleton().Receive()); } -void CleanUp::Render(SDL_Surface* const screen) { +void DisconnectedScreen::Render(SDL_Surface* const screen) { ConfigUtility& config = ConfigUtility::GetSingleton(); backButton.DrawTo(screen); @@ -100,25 +85,25 @@ void CleanUp::Render(SDL_Surface* const screen) { //Event handlers //------------------------- -void CleanUp::QuitEvent() { +void DisconnectedScreen::QuitEvent() { SetNextScene(SceneList::QUIT); } -void CleanUp::MouseMotion(SDL_MouseMotionEvent const& motion) { +void DisconnectedScreen::MouseMotion(SDL_MouseMotionEvent const& motion) { backButton.MouseMotion(motion); } -void CleanUp::MouseButtonDown(SDL_MouseButtonEvent const& button) { +void DisconnectedScreen::MouseButtonDown(SDL_MouseButtonEvent const& button) { backButton.MouseButtonDown(button); } -void CleanUp::MouseButtonUp(SDL_MouseButtonEvent const& button) { +void DisconnectedScreen::MouseButtonUp(SDL_MouseButtonEvent const& button) { if (backButton.MouseButtonUp(button) == Button::State::HOVER) { SetNextScene(SceneList::MAINMENU); } } -void CleanUp::KeyDown(SDL_KeyboardEvent const& key) { +void DisconnectedScreen::KeyDown(SDL_KeyboardEvent const& key) { switch(key.keysym.sym) { case SDLK_ESCAPE: SetNextScene(SceneList::MAINMENU); @@ -126,6 +111,6 @@ void CleanUp::KeyDown(SDL_KeyboardEvent const& key) { } } -void CleanUp::KeyUp(SDL_KeyboardEvent const& key) { +void DisconnectedScreen::KeyUp(SDL_KeyboardEvent const& key) { // } diff --git a/client/scenes/disconnected_screen.hpp b/client/scenes/disconnected_screen.hpp index a408d3d..453dad8 100644 --- a/client/scenes/disconnected_screen.hpp +++ b/client/scenes/disconnected_screen.hpp @@ -19,11 +19,8 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#ifndef CLEANUP_HPP_ -#define CLEANUP_HPP_ - -//network -#include "udp_network_utility.hpp" +#ifndef DISCONNECTEDSCREEN_HPP_ +#define DISCONNECTEDSCREEN_HPP_ //graphics #include "image.hpp" @@ -31,22 +28,16 @@ #include "button.hpp" //client -#include "character.hpp" #include "base_scene.hpp" //std namespace #include -class CleanUp : public BaseScene { +class DisconnectedScreen : public BaseScene { public: //Public access members - CleanUp( - int* const argClientIndex, - int* const argAccountIndex, - int* const argCharacterIndex, - CharacterMap* argCharacterMap - ); - ~CleanUp(); + DisconnectedScreen(); + ~DisconnectedScreen(); protected: //Frame loop @@ -61,13 +52,6 @@ protected: void KeyDown(SDL_KeyboardEvent const&); void KeyUp(SDL_KeyboardEvent const&); - //shared parameters - UDPNetworkUtility& network = UDPNetworkUtility::GetSingleton(); - int& clientIndex; - int& accountIndex; - int& characterIndex; - CharacterMap& characterMap; - //graphics Image image; RasterFont font; diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index 028d5bd..4581b1a 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -34,16 +34,9 @@ //Public access members //------------------------- -InWorld::InWorld( - int* const argClientIndex, - int* const argAccountIndex, - int* const argCharacterIndex, - CharacterMap* argCharacterMap - ): +InWorld::InWorld(int* const argClientIndex, int* const argAccountIndex): clientIndex(*argClientIndex), - accountIndex(*argAccountIndex), - characterIndex(*argCharacterIndex), - characterMap(*argCharacterMap) + accountIndex(*argAccountIndex) { ConfigUtility& config = ConfigUtility::GetSingleton(); @@ -153,7 +146,7 @@ void InWorld::Update() { if (Clock::now() - lastBeat > std::chrono::seconds(3)) { if (attemptedBeats > 2) { RequestDisconnect(); - SetNextScene(SceneList::CLEANUP); + SetNextScene(SceneList::DISCONNECTEDSCREEN); ConfigUtility::GetSingleton()["client.disconnectMessage"] = "Error: Lost connection to the server"; } @@ -342,7 +335,7 @@ void InWorld::HandlePong(ServerPacket* const argPacket) { void InWorld::HandleDisconnect(ClientPacket* const argPacket) { //TODO: More needed in the disconnection - SetNextScene(SceneList::CLEANUP); + SetNextScene(SceneList::DISCONNECTEDSCREEN); ConfigUtility::GetSingleton()["client.disconnectMessage"] = "You have been disconnected"; } @@ -352,7 +345,7 @@ void InWorld::HandleCharacterNew(CharacterPacket* const argPacket) { } //create the character object - Character& newCharacter = characterMap[argPacket->characterIndex]; + BaseCharacter& newCharacter = characterMap[argPacket->characterIndex]; //fill out the character's members newCharacter.SetHandle(argPacket->handle); @@ -369,7 +362,7 @@ void InWorld::HandleCharacterNew(CharacterPacket* const argPacket) { CHARACTER_BOUNDS_HEIGHT }); - (*newCharacter.GetStats()) = argPacket->stats; +// (*newCharacter.GetBaseStats()) = argPacket->stats; //bookkeeping code newCharacter.CorrectSprite(); @@ -407,7 +400,7 @@ void InWorld::HandleCharacterUpdate(CharacterPacket* const argPacket) { return; } - Character& character = characterMap[argPacket->characterIndex]; + BaseCharacter& character = characterMap[argPacket->characterIndex]; //other characters moving if (argPacket->characterIndex != characterIndex) { @@ -419,7 +412,7 @@ void InWorld::HandleCharacterUpdate(CharacterPacket* const argPacket) { void InWorld::HandleCharacterRejection(TextPacket* const argPacket) { RequestDisconnect(); - SetNextScene(SceneList::CLEANUP); + SetNextScene(SceneList::DISCONNECTEDSCREEN); ConfigUtility& config = ConfigUtility::GetSingleton(); config["client.disconnectMessage"] = "Error: "; config["client.disconnectMessage"] += argPacket->text; @@ -464,7 +457,7 @@ void InWorld::SendPlayerUpdate() { newPacket.roomIndex = 0; //TODO: room index newPacket.origin = localCharacter->GetOrigin(); newPacket.motion = localCharacter->GetMotion(); - newPacket.stats = *localCharacter->GetStats(); +// newPacket.stats = *localCharacter->GetBaseStats(); //TODO: gameplay components: equipment, items, buffs, debuffs diff --git a/client/scenes/in_world.hpp b/client/scenes/in_world.hpp index d80659d..62904d9 100644 --- a/client/scenes/in_world.hpp +++ b/client/scenes/in_world.hpp @@ -38,7 +38,8 @@ //common #include "frame_rate.hpp" -#include "character.hpp" +#include "base_character.hpp" +#include "local_character.hpp" //client #include "base_scene.hpp" @@ -51,12 +52,7 @@ class InWorld : public BaseScene { public: //Public access members - InWorld( - int* const argClientIndex, - int* const argAccountIndex, - int* const argCharacterIndex, - CharacterMap* argCharacterMap - ); + InWorld(int* const argClientIndex, int* const argAccountIndex); ~InWorld(); protected: @@ -100,8 +96,8 @@ protected: UDPNetworkUtility& network = UDPNetworkUtility::GetSingleton(); int& clientIndex; int& accountIndex; - int& characterIndex; - CharacterMap& characterMap; + int characterIndex = -1; + std::map characterMap; //graphics Image buttonImage; @@ -124,7 +120,7 @@ protected: FrameRate fps; //game - Character* localCharacter = nullptr; + BaseCharacter* localCharacter = nullptr; //connections //TODO: This needs it's own utility, for both InWorld and InCombat diff --git a/client/scenes/lobby_menu.cpp b/client/scenes/lobby_menu.cpp index 7346f45..c679f96 100644 --- a/client/scenes/lobby_menu.cpp +++ b/client/scenes/lobby_menu.cpp @@ -34,6 +34,10 @@ LobbyMenu::LobbyMenu(int* const argClientIndex, int* const argAccountIndex): clientIndex(*argClientIndex), accountIndex(*argAccountIndex) { + //preemptive reset + clientIndex = -1; + accountIndex = -1; + //setup the utility objects image.LoadSurface(config["dir.interface"] + "button_menu.bmp"); image.SetClipH(image.GetClipH()/3); diff --git a/client/scenes/makefile b/client/scenes/makefile index f8d8358..7a59128 100644 --- a/client/scenes/makefile +++ b/client/scenes/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. .. ../../common/gameplay ../../common/graphics ../../common/map ../../common/network ../../common/network/packet_types ../../common/ui ../../common/utilities +INCLUDES+=. .. ../renderable ../../common/gameplay ../../common/graphics ../../common/map ../../common/network ../../common/network/packet_types ../../common/ui ../../common/utilities LIBS+= CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))