diff --git a/client/client_application.cpp b/client/client_application.cpp index 297a903..9f3d395 100644 --- a/client/client_application.cpp +++ b/client/client_application.cpp @@ -119,13 +119,13 @@ void ClientApplication::LoadScene(SceneList sceneIndex) { activeScene = new OptionsMenu(&config); break; case SceneList::LOBBYMENU: - activeScene = new LobbyMenu(&config, &network, &clientIndex, &accountIndex, &characterIndex); + activeScene = new LobbyMenu(&config, &network, ¶ms); break; case SceneList::INWORLD: - activeScene = new InWorld(&config, &network, &clientIndex, &accountIndex, &characterIndex); + activeScene = new InWorld(&config, &network, ¶ms); break; case SceneList::INCOMBAT: - activeScene = new InCombat(); + activeScene = new InCombat(&config, &network, ¶ms); 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 b6b3b31..07155ae 100644 --- a/client/client_application.hpp +++ b/client/client_application.hpp @@ -1,4 +1,4 @@ -/* Copyright: (c) Kayne Ruse 2013 +/* Copyright: (c) Kayne Ruse 2013, 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 @@ -25,6 +25,7 @@ #include "scene_list.hpp" #include "base_scene.hpp" +#include "shared_parameters.hpp" #include "config_utility.hpp" #include "udp_network_utility.hpp" @@ -47,9 +48,7 @@ private: //shared parameters ConfigUtility config; UDPNetworkUtility network; - int clientIndex = -1; - int accountIndex = -1; - int characterIndex = -1; + SharedParameters params; }; #endif diff --git a/client/scenes/in_combat.cpp b/client/scenes/in_combat.cpp index 1d70713..b0d69ec 100644 --- a/client/scenes/in_combat.cpp +++ b/client/scenes/in_combat.cpp @@ -25,7 +25,11 @@ //Public access members //------------------------- -InCombat::InCombat() { +InCombat::InCombat(ConfigUtility* const argConfig, UDPNetworkUtility* const argNetwork, SharedParameters* const argParams): + config(*argConfig), + network(*argNetwork), + params(*argParams) +{ // } @@ -57,6 +61,10 @@ void InCombat::Render(SDL_Surface* const screen) { //Event handlers //------------------------- +void InCombat::QuitEvent() { + // +} + void InCombat::MouseMotion(SDL_MouseMotionEvent const& motion) { // } @@ -80,3 +88,7 @@ void InCombat::KeyDown(SDL_KeyboardEvent const& key) { void InCombat::KeyUp(SDL_KeyboardEvent const& key) { // } + +void InCombat::HandlePacket(SerialPacket& packet) { + // +} \ No newline at end of file diff --git a/client/scenes/in_combat.hpp b/client/scenes/in_combat.hpp index b08a53d..c9d58f3 100644 --- a/client/scenes/in_combat.hpp +++ b/client/scenes/in_combat.hpp @@ -22,12 +22,25 @@ #ifndef INCOMBAT_HPP_ #define INCOMBAT_HPP_ +//graphics & utilities +#include "image.hpp" +#include "raster_font.hpp" +#include "button.hpp" +#include "config_utility.hpp" +#include "shared_parameters.hpp" + +//network +#include "udp_network_utility.hpp" +#include "serial_packet.hpp" +#include "serial.hpp" + +//client #include "base_scene.hpp" class InCombat : public BaseScene { public: //Public access members - InCombat(); + InCombat(ConfigUtility* const, UDPNetworkUtility* const, SharedParameters* const); ~InCombat(); protected: @@ -38,11 +51,20 @@ protected: void Render(SDL_Surface* const); //Event handlers + void QuitEvent(); void MouseMotion(SDL_MouseMotionEvent const&); void MouseButtonDown(SDL_MouseButtonEvent const&); void MouseButtonUp(SDL_MouseButtonEvent const&); void KeyDown(SDL_KeyboardEvent const&); void KeyUp(SDL_KeyboardEvent const&); + + //Network handlers + void HandlePacket(SerialPacket&); + + //shared parameters + ConfigUtility& config; + UDPNetworkUtility& network; + SharedParameters& params; }; #endif diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index 28e7ce5..86619ff 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -31,12 +31,10 @@ //Public access members //------------------------- -InWorld::InWorld(ConfigUtility* const argConfig, UDPNetworkUtility* const argNetwork, int* const argClientIndex, int* const argAccountIndex, int* const argCharacterIndex): +InWorld::InWorld(ConfigUtility* const argConfig, UDPNetworkUtility* const argNetwork, SharedParameters* const argParams): config(*argConfig), network(*argNetwork), - clientIndex(*argClientIndex), - accountIndex(*argAccountIndex), - characterIndex(*argCharacterIndex) + params(*argParams) { //setup the utility objects buttonImage.LoadSurface(config["dir.interface"] + "button_menu.bmp"); @@ -68,9 +66,9 @@ InWorld::InWorld(ConfigUtility* const argConfig, UDPNetworkUtility* const argNet SerialPacket packet; char buffer[PACKET_STRING_SIZE]; packet.meta.type = SerialPacket::Type::SYNCHRONIZE; - packet.clientInfo.clientIndex = clientIndex; - packet.clientInfo.accountIndex = accountIndex; - packet.clientInfo.characterIndex = characterIndex; + packet.clientInfo.clientIndex = params.clientIndex; + packet.clientInfo.accountIndex = params.accountIndex; + packet.clientInfo.characterIndex = params.characterIndex; serialize(&packet, buffer); network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE); @@ -151,6 +149,7 @@ void InWorld::Render(SDL_Surface* const screen) { void InWorld::QuitEvent() { //exit the game AND the server RequestDisconnect(); + SetNextScene(SceneList::MAINMENU); } void InWorld::MouseMotion(SDL_MouseMotionEvent const& motion) { @@ -273,17 +272,15 @@ void InWorld::HandlePacket(SerialPacket packet) { void InWorld::HandleDisconnect(SerialPacket packet) { network.Unbind(Channels::SERVER); - clientIndex = -1; - accountIndex = -1; - characterIndex = -1; + params.clientIndex = -1; + params.accountIndex = -1; + params.characterIndex = -1; SetNextScene(SceneList::MAINMENU); } void InWorld::HandleRegionContent(SerialPacket packet) { //replace existing regions - if (regionPager.FindRegion(packet.regionInfo.x, packet.regionInfo.y)) { - regionPager.UnloadRegion(packet.regionInfo.x, packet.regionInfo.y); - } + regionPager.UnloadRegion(packet.regionInfo.x, packet.regionInfo.y); regionPager.PushRegion(packet.regionInfo.region); packet.regionInfo.region = nullptr; } @@ -295,7 +292,7 @@ void InWorld::HandleCharacterUpdate(SerialPacket packet) { } //update only if the message didn't originate from here - if (packet.characterInfo.clientIndex != clientIndex) { + if (packet.characterInfo.clientIndex != params.clientIndex) { playerCharacters[packet.characterInfo.characterIndex].SetPosition(packet.characterInfo.position); playerCharacters[packet.characterInfo.characterIndex].SetMotion(packet.characterInfo.motion); } @@ -308,14 +305,15 @@ void InWorld::HandleCharacterNew(SerialPacket packet) { } //TODO: set the player's handle + //TODO: use a reference, don't use a lookup for every call playerCharacters[packet.characterInfo.characterIndex].GetSprite()->LoadSurface(config["dir.sprites"] + packet.characterInfo.avatar, 4, 4); playerCharacters[packet.characterInfo.characterIndex].SetPosition(packet.characterInfo.position); playerCharacters[packet.characterInfo.characterIndex].SetMotion(packet.characterInfo.motion); playerCharacters[packet.characterInfo.characterIndex].ResetDirection(); //catch this client's player object - if (packet.characterInfo.characterIndex == characterIndex && !localCharacter) { - localCharacter = &playerCharacters[characterIndex]; + if (packet.characterInfo.characterIndex == params.characterIndex && !localCharacter) { + localCharacter = &playerCharacters[params.characterIndex]; //setup the camera camera.width = GetScreen()->w; @@ -327,16 +325,13 @@ void InWorld::HandleCharacterNew(SerialPacket packet) { } void InWorld::HandleCharacterDelete(SerialPacket packet) { - //TODO: authenticate - if (playerCharacters.find(packet.characterInfo.characterIndex) == playerCharacters.end()) { - throw(std::runtime_error("Cannot delete non-existant characters")); - } + //TODO: authenticate when own character is being deleted playerCharacters.erase(packet.characterInfo.characterIndex); //catch this client's player object - if (packet.characterInfo.characterIndex == characterIndex) { - characterIndex = -1; + if (packet.characterInfo.characterIndex == params.characterIndex) { + params.characterIndex = -1; localCharacter = nullptr; } } @@ -351,9 +346,9 @@ void InWorld::SendPlayerUpdate() { //pack the packet packet.meta.type = SerialPacket::Type::CHARACTER_UPDATE; - packet.characterInfo.clientIndex = clientIndex; - packet.characterInfo.accountIndex = accountIndex; - packet.characterInfo.characterIndex = characterIndex; + packet.characterInfo.clientIndex = params.clientIndex; + packet.characterInfo.accountIndex = params.accountIndex; + packet.characterInfo.characterIndex = params.characterIndex; packet.characterInfo.position = localCharacter->GetPosition(); packet.characterInfo.motion = localCharacter->GetMotion(); @@ -367,9 +362,9 @@ void InWorld::RequestDisconnect() { //send a disconnect request packet.meta.type = SerialPacket::Type::DISCONNECT; - packet.clientInfo.clientIndex = clientIndex; - packet.clientInfo.accountIndex = accountIndex; - packet.clientInfo.characterIndex = characterIndex; + packet.clientInfo.clientIndex = params.clientIndex; + packet.clientInfo.accountIndex = params.accountIndex; + packet.clientInfo.characterIndex = params.characterIndex; serialize(&packet, buffer); network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE); } @@ -380,9 +375,9 @@ void InWorld::RequestShutDown() { //send a shutdown request packet.meta.type = SerialPacket::Type::SHUTDOWN; - packet.clientInfo.clientIndex = clientIndex; - packet.clientInfo.accountIndex = accountIndex; - packet.clientInfo.characterIndex = characterIndex; + packet.clientInfo.clientIndex = params.clientIndex; + packet.clientInfo.accountIndex = params.accountIndex; + packet.clientInfo.characterIndex = params.characterIndex; serialize(&packet, buffer); network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE); } diff --git a/client/scenes/in_world.hpp b/client/scenes/in_world.hpp index 0b30f5f..74fb233 100644 --- a/client/scenes/in_world.hpp +++ b/client/scenes/in_world.hpp @@ -45,6 +45,7 @@ //client #include "base_scene.hpp" #include "player_character.hpp" +#include "shared_parameters.hpp" //STL #include @@ -52,7 +53,7 @@ class InWorld : public BaseScene { public: //Public access members - InWorld(ConfigUtility* const, UDPNetworkUtility* const, int* const, int* const, int* const); + InWorld(ConfigUtility* const, UDPNetworkUtility* const, SharedParameters* const); ~InWorld(); protected: @@ -91,9 +92,7 @@ protected: //shared parameters ConfigUtility& config; UDPNetworkUtility& network; - int& clientIndex; - int& accountIndex; - int& characterIndex; + SharedParameters& params; //graphics Image buttonImage; diff --git a/client/scenes/lobby_menu.cpp b/client/scenes/lobby_menu.cpp index cd5a50d..9cd68b5 100644 --- a/client/scenes/lobby_menu.cpp +++ b/client/scenes/lobby_menu.cpp @@ -30,12 +30,10 @@ //Public access members //------------------------- -LobbyMenu::LobbyMenu(ConfigUtility* const argConfig, UDPNetworkUtility* const argNetwork, int* const argClientIndex, int* const argAccountIndex, int* const argCharacterIndex): +LobbyMenu::LobbyMenu(ConfigUtility* const argConfig, UDPNetworkUtility* const argNetwork, SharedParameters* const argParams): config(*argConfig), network(*argNetwork), - clientIndex(*argClientIndex), - accountIndex(*argAccountIndex), - characterIndex(*argCharacterIndex) + params(*argParams) { //setup the utility objects image.LoadSurface(config["dir.interface"] + "button_menu.bmp"); @@ -94,6 +92,7 @@ void LobbyMenu::FrameEnd() { } void LobbyMenu::Render(SDL_Surface* const screen) { + //TODO: this needs rewriting //TODO: I need a proper UI system for the entire client and the editor //UI search.DrawTo(screen); @@ -221,9 +220,9 @@ void LobbyMenu::HandlePacket(SerialPacket packet) { } break; case SerialPacket::Type::JOIN_RESPONSE: - clientIndex = packet.clientInfo.clientIndex; - accountIndex = packet.clientInfo.accountIndex; - characterIndex = packet.clientInfo.characterIndex; + params.clientIndex = packet.clientInfo.clientIndex; + params.accountIndex = packet.clientInfo.accountIndex; + params.characterIndex = packet.clientInfo.characterIndex; network.Bind(&packet.meta.srcAddress, Channels::SERVER); SetNextScene(SceneList::INWORLD); break; diff --git a/client/scenes/lobby_menu.hpp b/client/scenes/lobby_menu.hpp index 7b6c8a9..efd7b0b 100644 --- a/client/scenes/lobby_menu.hpp +++ b/client/scenes/lobby_menu.hpp @@ -27,6 +27,7 @@ #include "raster_font.hpp" #include "button.hpp" #include "config_utility.hpp" +#include "shared_parameters.hpp" //network #include "udp_network_utility.hpp" @@ -42,7 +43,7 @@ class LobbyMenu : public BaseScene { public: //Public access members - LobbyMenu(ConfigUtility* const, UDPNetworkUtility* const, int* const, int* const, int* const); + LobbyMenu(ConfigUtility* const, UDPNetworkUtility* const, SharedParameters* const); ~LobbyMenu(); protected: @@ -64,9 +65,7 @@ protected: //shared parameters ConfigUtility& config; UDPNetworkUtility& network; - int& clientIndex; - int& accountIndex; - int& characterIndex; + SharedParameters& params; //members Image image; diff --git a/client/shared_parameters.hpp b/client/shared_parameters.hpp new file mode 100644 index 0000000..5963789 --- /dev/null +++ b/client/shared_parameters.hpp @@ -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. +*/ +#ifndef SHAREDPARAMETERS_HPP_ +#define SHAREDPARAMETERS_HPP_ + +struct SharedParameters { + int clientIndex = -1; + int accountIndex = -1; + int characterIndex = -1; +}; + +#endif diff --git a/server/combat_data.hpp b/server/combat_data.hpp index df75325..4fe73d2 100644 --- a/server/combat_data.hpp +++ b/server/combat_data.hpp @@ -30,13 +30,14 @@ #include #include +#include struct CombatData { typedef std::chrono::steady_clock Clock; - //combatants - std::list characterList; - std::list enemyList; + //combatants, point to the std::map's internal pairs + std::list*> characterList; + std::list*> enemyList; //world interaction int mapIndex = 0; diff --git a/server/enemy_data.hpp b/server/enemy_data.hpp index 48a5fa4..16f3f21 100644 --- a/server/enemy_data.hpp +++ b/server/enemy_data.hpp @@ -43,6 +43,8 @@ struct EnemyData { //NOTE: these are lost when unloaded int tableIndex; int atbGauge = 0; + + static int uidCounter; }; #endif diff --git a/server/server_application.hpp b/server/server_application.hpp index 398c8b4..c8453ac 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -106,6 +106,7 @@ private: std::map accountMap; std::map characterMap; std::map combatMap; + std::map enemyMap; //maps //TODO: I need to handle multiple map objects diff --git a/server/server_internals.cpp b/server/server_internals.cpp index abd16e8..f8624eb 100644 --- a/server/server_internals.cpp +++ b/server/server_internals.cpp @@ -33,6 +33,7 @@ int ClientData::uidCounter = 0; int CombatData::uidCounter = 0; +int EnemyData::uidCounter = 0; //------------------------- //Define the public members