diff --git a/client/scenes/base_scene.cpp b/client/base_scene.cpp similarity index 98% rename from client/scenes/base_scene.cpp rename to client/base_scene.cpp index 19753a0..48512ec 100644 --- a/client/scenes/base_scene.cpp +++ b/client/base_scene.cpp @@ -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 diff --git a/client/scenes/base_scene.hpp b/client/base_scene.hpp similarity index 98% rename from client/scenes/base_scene.hpp rename to client/base_scene.hpp index 44dfab8..a4bfd88 100644 --- a/client/scenes/base_scene.hpp +++ b/client/base_scene.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 diff --git a/client/channels.hpp b/client/channels.hpp index 92695cd..2fe04a7 100644 --- a/client/channels.hpp +++ b/client/channels.hpp @@ -1,3 +1,24 @@ +/* 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 + * 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 CHANNELS_HPP_ #define CHANNELS_HPP_ diff --git a/client/client_application.cpp b/client/client_application.cpp index 297a903..ebc9f18 100644 --- a/client/client_application.cpp +++ b/client/client_application.cpp @@ -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 @@ -56,7 +56,7 @@ void ClientApplication::Init(int argc, char** argv) { if (SDLNet_Init()) { throw(std::runtime_error("Failed to initialize SDL_net")); } - network.Open(0, PACKET_BUFFER_SIZE); + network.Open(0); } void ClientApplication::Proc() { @@ -122,10 +122,10 @@ void ClientApplication::LoadScene(SceneList sceneIndex) { activeScene = new LobbyMenu(&config, &network, &clientIndex, &accountIndex, &characterIndex); break; case SceneList::INWORLD: - activeScene = new InWorld(&config, &network, &clientIndex, &accountIndex, &characterIndex); + activeScene = new InWorld(&config, &network, &clientIndex, &accountIndex, &characterIndex, &combatMap, &characterMap); break; case SceneList::INCOMBAT: - activeScene = new InCombat(); + activeScene = new InCombat(&config, &network, &clientIndex, &accountIndex, &characterIndex, &combatMap, &characterMap, &enemyMap); 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..54df4ff 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 @@ -27,6 +27,11 @@ #include "config_utility.hpp" #include "udp_network_utility.hpp" +#include "character_data.hpp" +#include "combat_data.hpp" +#include "enemy_data.hpp" + +#include class ClientApplication { public: @@ -50,6 +55,10 @@ private: int clientIndex = -1; int accountIndex = -1; int characterIndex = -1; + + std::map combatMap; + std::map characterMap; + std::map enemyMap; }; #endif diff --git a/client/scenes/in_combat.cpp b/client/in_combat.cpp similarity index 62% rename from client/scenes/in_combat.cpp rename to client/in_combat.cpp index 1d70713..f20f765 100644 --- a/client/scenes/in_combat.cpp +++ b/client/in_combat.cpp @@ -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,7 +25,25 @@ //Public access members //------------------------- -InCombat::InCombat() { +InCombat::InCombat( + ConfigUtility* const argConfig, + UDPNetworkUtility* const argNetwork, + int* const argClientIndex, + int* const argAccountIndex, + int* const argCharacterIndex, + std::map* argCombatMap, + std::map* argCharacterMap, + std::map* argEnemyMap + ): + config(*argConfig), + network(*argNetwork), + clientIndex(*argClientIndex), + accountIndex(*argAccountIndex), + characterIndex(*argCharacterIndex), + combatMap(*argCombatMap), + characterMap(*argCharacterMap), + enemyMap(*argEnemyMap) +{ // } @@ -49,6 +67,13 @@ void InCombat::FrameEnd() { // } +void InCombat::RenderFrame() { + SDL_FillRect(GetScreen(), 0, 0); + Render(GetScreen()); + SDL_Flip(GetScreen()); + fps.Calculate(); +} + void InCombat::Render(SDL_Surface* const screen) { // } @@ -57,6 +82,12 @@ void InCombat::Render(SDL_Surface* const screen) { //Event handlers //------------------------- +void InCombat::QuitEvent() { + //exit the game AND the server +// RequestDisconnect(); + SetNextScene(SceneList::MAINMENU); +} + void InCombat::MouseMotion(SDL_MouseMotionEvent const& motion) { // } @@ -80,3 +111,15 @@ void InCombat::KeyDown(SDL_KeyboardEvent const& key) { void InCombat::KeyUp(SDL_KeyboardEvent const& key) { // } + +//------------------------- +//Network handlers +//------------------------- + +//TODO: network handlers + +//------------------------- +//Server control +//------------------------- + +//TODO: server control diff --git a/client/in_combat.hpp b/client/in_combat.hpp new file mode 100644 index 0000000..15df345 --- /dev/null +++ b/client/in_combat.hpp @@ -0,0 +1,104 @@ +/* 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 + * 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 INCOMBAT_HPP_ +#define INCOMBAT_HPP_ + +//network +#include "udp_network_utility.hpp" + +//graphics +#include "image.hpp" +#include "raster_font.hpp" +#include "button.hpp" + +//common +#include "config_utility.hpp" +#include "frame_rate.hpp" + +#include "combat_data.hpp" +#include "character_data.hpp" +#include "enemy_data.hpp" + +//client +#include "base_scene.hpp" + +class InCombat : public BaseScene { +public: + //Public access members + InCombat( + ConfigUtility* const argConfig, + UDPNetworkUtility* const argNetwork, + int* const argClientIndex, + int* const argAccountIndex, + int* const argCharacterIndex, + std::map* argCombatMap, + std::map* argCharacterMap, + std::map* argEnemyMap + ); + ~InCombat(); + +protected: + //Frame loop + void FrameStart(); + void Update(double delta); + void FrameEnd(); + void RenderFrame(); + 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); + void HandleDisconnect(SerialPacket); + //TODO: more + + //Server control + void SendPlayerUpdate(); + void RequestDisconnect(); + void RequestShutdown(); + //TODO: more + + //shared parameters + ConfigUtility& config; + UDPNetworkUtility& network; + int& clientIndex; + int& accountIndex; + int& characterIndex; + std::map& combatMap; + std::map& characterMap; + std::map& enemyMap; + + //graphics + //TODO: graphics + + //UI + //TODO: UI + FrameRate fps; +}; + +#endif diff --git a/client/scenes/in_world.cpp b/client/in_world.cpp similarity index 72% rename from client/scenes/in_world.cpp rename to client/in_world.cpp index 28e7ce5..9822a2a 100644 --- a/client/scenes/in_world.cpp +++ b/client/in_world.cpp @@ -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 @@ -31,12 +31,22 @@ //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, + int* const argClientIndex, + int* const argAccountIndex, + int* const argCharacterIndex, + std::map* argCombatMap, + std::map* argCharacterMap + ): config(*argConfig), network(*argNetwork), clientIndex(*argClientIndex), accountIndex(*argAccountIndex), - characterIndex(*argCharacterIndex) + characterIndex(*argCharacterIndex), + combatMap(*argCombatMap), + characterMap(*argCharacterMap) { //setup the utility objects buttonImage.LoadSurface(config["dir.interface"] + "button_menu.bmp"); @@ -63,16 +73,8 @@ InWorld::InWorld(ConfigUtility* const argConfig, UDPNetworkUtility* const argNet //TODO: add the tilesheet to the map system? tileSheet.Load(config["dir.tilesets"] + "terrain.bmp", 12, 15); - //TODO: move this into it's own function //request a sync - 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; - serialize(&packet, buffer); - network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE); + RequestSynchronize(); //debug // RequestRegion(0, 0); @@ -94,22 +96,19 @@ void InWorld::Update(double delta) { SerialPacket packet; //suck in all waiting packets - while(network.Receive()) { - deserialize(&packet, network.GetInData()); - packet.meta.srcAddress = network.GetInPacket()->address; + while(network.Receive(&packet)) { HandlePacket(packet); } //update the characters - for (auto& it : playerCharacters) { + for (auto& it : characterMap) { it.second.Update(delta); } - //TODO: sort the players and entities by Y position //update the camera if(localCharacter) { - camera.x = localCharacter->GetPosition().x - camera.marginX; - camera.y = localCharacter->GetPosition().y - camera.marginY; + camera.x = localCharacter->position.x - camera.marginX; + camera.y = localCharacter->position.y - camera.marginY; } //check the map @@ -134,7 +133,8 @@ void InWorld::Render(SDL_Surface* const screen) { } //draw characters - for (auto& it : playerCharacters) { + for (auto& it : characterMap) { + //TODO: drawing order according to Y position it.second.DrawTo(screen, camera.x, camera.y); } @@ -151,6 +151,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) { @@ -182,28 +183,28 @@ void InWorld::KeyDown(SDL_KeyboardEvent const& key) { //player movement case SDLK_LEFT: if (localCharacter) { - localCharacter->AdjustDirection(PlayerCharacter::Direction::WEST); + localCharacter->motion.x -= CHARACTER_WALKING_SPEED; SendPlayerUpdate(); } break; case SDLK_RIGHT: if (localCharacter) { - localCharacter->AdjustDirection(PlayerCharacter::Direction::EAST); + localCharacter->motion.x += CHARACTER_WALKING_SPEED; SendPlayerUpdate(); } break; case SDLK_UP: if (localCharacter) { - localCharacter->AdjustDirection(PlayerCharacter::Direction::NORTH); + localCharacter->motion.y -= CHARACTER_WALKING_SPEED; SendPlayerUpdate(); } break; case SDLK_DOWN: if (localCharacter) { - localCharacter->AdjustDirection(PlayerCharacter::Direction::SOUTH); + localCharacter->motion.y += CHARACTER_WALKING_SPEED; SendPlayerUpdate(); } break; @@ -215,28 +216,28 @@ void InWorld::KeyUp(SDL_KeyboardEvent const& key) { //player movement case SDLK_LEFT: if (localCharacter) { - localCharacter->AdjustDirection(PlayerCharacter::Direction::EAST); + localCharacter->motion.x += CHARACTER_WALKING_SPEED; SendPlayerUpdate(); } break; case SDLK_RIGHT: if (localCharacter) { - localCharacter->AdjustDirection(PlayerCharacter::Direction::WEST); + localCharacter->motion.x -= CHARACTER_WALKING_SPEED; SendPlayerUpdate(); } break; case SDLK_UP: if (localCharacter) { - localCharacter->AdjustDirection(PlayerCharacter::Direction::SOUTH); + localCharacter->motion.y += CHARACTER_WALKING_SPEED; SendPlayerUpdate(); } break; case SDLK_DOWN: if (localCharacter) { - localCharacter->AdjustDirection(PlayerCharacter::Direction::NORTH); + localCharacter->motion.y -= CHARACTER_WALKING_SPEED; SendPlayerUpdate(); } break; @@ -281,123 +282,134 @@ void InWorld::HandleDisconnect(SerialPacket packet) { 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; } void InWorld::HandleCharacterUpdate(SerialPacket packet) { - if (playerCharacters.find(packet.characterInfo.characterIndex) == playerCharacters.end()) { + if (characterMap.find(packet.characterInfo.characterIndex) == characterMap.end()) { HandleCharacterNew(packet); return; } //update only if the message didn't originate from here if (packet.characterInfo.clientIndex != clientIndex) { - playerCharacters[packet.characterInfo.characterIndex].SetPosition(packet.characterInfo.position); - playerCharacters[packet.characterInfo.characterIndex].SetMotion(packet.characterInfo.motion); + characterMap[packet.characterInfo.characterIndex].position = packet.characterInfo.position; + characterMap[packet.characterInfo.characterIndex].motion = packet.characterInfo.motion; } - playerCharacters[packet.characterInfo.characterIndex].ResetDirection(); + characterMap[packet.characterInfo.characterIndex].CorrectSprite(); } void InWorld::HandleCharacterNew(SerialPacket packet) { - if (playerCharacters.find(packet.characterInfo.characterIndex) != playerCharacters.end()) { + if (characterMap.find(packet.characterInfo.characterIndex) != characterMap.end()) { throw(std::runtime_error("Cannot create duplicate characters")); } - //TODO: set the player's handle - 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(); + //create the character object + CharacterData& character = characterMap[packet.characterInfo.characterIndex]; + + //set the members + character.handle = packet.characterInfo.handle; + character.avatar = packet.characterInfo.avatar; + character.sprite.LoadSurface(config["dir.sprites"] + character.avatar, 4, 4); + character.mapIndex = packet.characterInfo.mapIndex; + character.position = packet.characterInfo.position; + character.motion = packet.characterInfo.motion; + character.stats = packet.characterInfo.stats; + + character.CorrectSprite(); //catch this client's player object if (packet.characterInfo.characterIndex == characterIndex && !localCharacter) { - localCharacter = &playerCharacters[characterIndex]; + localCharacter = &character; //setup the camera + //TODO: can't change the screen size? camera.width = GetScreen()->w; camera.height = GetScreen()->h; + //center on the player's character - camera.marginX = (GetScreen()->w / 2 - localCharacter->GetSprite()->GetImage()->GetClipW() / 2); - camera.marginY = (GetScreen()->h / 2 - localCharacter->GetSprite()->GetImage()->GetClipH() / 2); + camera.marginX = (GetScreen()->w / 2 - localCharacter->sprite.GetImage()->GetClipW() / 2); + camera.marginY = (GetScreen()->h / 2 - localCharacter->sprite.GetImage()->GetClipH() / 2); } } void InWorld::HandleCharacterDelete(SerialPacket packet) { - //TODO: authenticate - if (playerCharacters.find(packet.characterInfo.characterIndex) == playerCharacters.end()) { - throw(std::runtime_error("Cannot delete non-existant characters")); - } - - playerCharacters.erase(packet.characterInfo.characterIndex); - + //TODO: authenticate when own character is being deleted (linked to a TODO in the server) //catch this client's player object if (packet.characterInfo.characterIndex == characterIndex) { characterIndex = -1; localCharacter = nullptr; } + + characterMap.erase(packet.characterInfo.characterIndex); } //------------------------- //Server control //------------------------- +void InWorld::RequestSynchronize() { + SerialPacket packet; + + //request a sync + packet.meta.type = SerialPacket::Type::SYNCHRONIZE; + packet.clientInfo.clientIndex = clientIndex; + packet.clientInfo.accountIndex = accountIndex; + packet.clientInfo.characterIndex = characterIndex; + + network.SendTo(Channels::SERVER, &packet); +} + void InWorld::SendPlayerUpdate() { SerialPacket packet; - char buffer[PACKET_BUFFER_SIZE]; //pack the packet packet.meta.type = SerialPacket::Type::CHARACTER_UPDATE; packet.characterInfo.clientIndex = clientIndex; packet.characterInfo.accountIndex = accountIndex; packet.characterInfo.characterIndex = characterIndex; - packet.characterInfo.position = localCharacter->GetPosition(); - packet.characterInfo.motion = localCharacter->GetMotion(); + packet.characterInfo.position = localCharacter->position; + packet.characterInfo.motion = localCharacter->motion; - serialize(&packet, buffer); - network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE); + network.SendTo(Channels::SERVER, &packet); } void InWorld::RequestDisconnect() { SerialPacket packet; - char buffer[PACKET_BUFFER_SIZE]; //send a disconnect request packet.meta.type = SerialPacket::Type::DISCONNECT; packet.clientInfo.clientIndex = clientIndex; packet.clientInfo.accountIndex = accountIndex; packet.clientInfo.characterIndex = characterIndex; - serialize(&packet, buffer); - network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE); + + network.SendTo(Channels::SERVER, &packet); } void InWorld::RequestShutDown() { SerialPacket packet; - char buffer[PACKET_BUFFER_SIZE]; //send a shutdown request packet.meta.type = SerialPacket::Type::SHUTDOWN; packet.clientInfo.clientIndex = clientIndex; packet.clientInfo.accountIndex = accountIndex; packet.clientInfo.characterIndex = characterIndex; - serialize(&packet, buffer); - network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE); + + network.SendTo(Channels::SERVER, &packet); } void InWorld::RequestRegion(int mapIndex, int x, int y) { SerialPacket packet; - char buffer[PACKET_BUFFER_SIZE]; //pack the region's data packet.meta.type = SerialPacket::Type::REGION_REQUEST; packet.regionInfo.mapIndex = mapIndex; packet.regionInfo.x = x; packet.regionInfo.y = y; - serialize(&packet, buffer); - network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE); + + network.SendTo(Channels::SERVER, &packet); } //------------------------- diff --git a/client/scenes/in_world.hpp b/client/in_world.hpp similarity index 83% rename from client/scenes/in_world.hpp rename to client/in_world.hpp index 0b30f5f..038d22c 100644 --- a/client/scenes/in_world.hpp +++ b/client/in_world.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 @@ -29,8 +29,6 @@ //networking #include "udp_network_utility.hpp" -#include "serial_packet.hpp" -#include "serial.hpp" //graphics #include "image.hpp" @@ -42,9 +40,11 @@ #include "config_utility.hpp" #include "frame_rate.hpp" +#include "combat_data.hpp" +#include "character_data.hpp" + //client #include "base_scene.hpp" -#include "player_character.hpp" //STL #include @@ -52,7 +52,15 @@ class InWorld : public BaseScene { public: //Public access members - InWorld(ConfigUtility* const, UDPNetworkUtility* const, int* const, int* const, int* const); + InWorld( + ConfigUtility* const argConfig, + UDPNetworkUtility* const argNetwork, + int* const argClientIndex, + int* const argAccountIndex, + int* const argCharacterIndex, + std::map* argCombatMap, + std::map* argCharacterMap + ); ~InWorld(); protected: @@ -80,6 +88,7 @@ protected: void HandleRegionContent(SerialPacket); //Server control + void RequestSynchronize(); void SendPlayerUpdate(); void RequestDisconnect(); void RequestShutDown(); @@ -94,6 +103,8 @@ protected: int& clientIndex; int& accountIndex; int& characterIndex; + std::map& combatMap; + std::map& characterMap; //graphics Image buttonImage; @@ -106,7 +117,7 @@ protected: //UI Button disconnectButton; Button shutDownButton; - //TODO: Fix the camera + //TODO: Review the camera struct { int x = 0, y = 0; int width = 0, height = 0; @@ -115,8 +126,7 @@ protected: FrameRate fps; //game - std::map playerCharacters; - PlayerCharacter* localCharacter = nullptr; + CharacterData* localCharacter = nullptr; }; #endif diff --git a/client/scenes/lobby_menu.cpp b/client/lobby_menu.cpp similarity index 89% rename from client/scenes/lobby_menu.cpp rename to client/lobby_menu.cpp index cd5a50d..fa1925e 100644 --- a/client/scenes/lobby_menu.cpp +++ b/client/lobby_menu.cpp @@ -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 @@ -30,7 +30,13 @@ //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, + int* const argClientIndex, + int* const argAccountIndex, + int* const argCharacterIndex + ): config(*argConfig), network(*argNetwork), clientIndex(*argClientIndex), @@ -80,11 +86,9 @@ void LobbyMenu::FrameStart() { } void LobbyMenu::Update(double delta) { - //suck in all waiting packets + //suck in and process all waiting packets SerialPacket packet; - while(network.Receive()) { - deserialize(&packet, network.GetInData()); - packet.meta.srcAddress = network.GetInPacket()->address; + while(network.Receive(&packet)) { HandlePacket(packet); } } @@ -95,6 +99,7 @@ void LobbyMenu::FrameEnd() { void LobbyMenu::Render(SDL_Surface* const screen) { //TODO: I need a proper UI system for the entire client and the editor + //UI search.DrawTo(screen); join.DrawTo(screen); @@ -120,7 +125,7 @@ void LobbyMenu::Render(SDL_Surface* const screen) { font.DrawStringTo("?", screen, listBox.x - font.GetCharW(), listBox.y + i*listBox.h); } - //ping? + //TODO: ping/delay? } } @@ -142,14 +147,10 @@ void LobbyMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) { void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) { if (search.MouseButtonUp(button) == Button::State::HOVER) { - //the vars - SerialPacket packet; - char buffer[PACKET_BUFFER_SIZE]; - //broadcast to the network, or a specific server + SerialPacket packet; packet.meta.type = SerialPacket::Type::BROADCAST_REQUEST; - serialize(&packet, buffer); - network.Send(config["server.host"].c_str(), config.Int("server.port"), buffer, PACKET_BUFFER_SIZE); + network.SendTo(config["server.host"].c_str(), config.Int("server.port"), &packet); //reset the server list serverInfo.clear(); @@ -157,19 +158,15 @@ void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) { } else if (join.MouseButtonUp(button) == Button::State::HOVER && selection != nullptr && selection->compatible) { - //the vars - SerialPacket packet; - char buffer[PACKET_BUFFER_SIZE]; - //pack the packet + SerialPacket packet; packet.meta.type = SerialPacket::Type::JOIN_REQUEST; strncpy(packet.clientInfo.username, config["client.username"].c_str(), PACKET_STRING_SIZE); strncpy(packet.clientInfo.handle, config["client.handle"].c_str(), PACKET_STRING_SIZE); strncpy(packet.clientInfo.avatar, config["client.avatar"].c_str(), PACKET_STRING_SIZE); //join the selected server - serialize(&packet, buffer); - network.Send(&selection->address, buffer, PACKET_BUFFER_SIZE); + network.SendTo(&selection->address, &packet); selection = nullptr; } @@ -203,6 +200,10 @@ void LobbyMenu::KeyUp(SDL_KeyboardEvent const& key) { // } +//------------------------- +//Network handlers +//------------------------- + void LobbyMenu::HandlePacket(SerialPacket packet) { switch(packet.meta.type) { case SerialPacket::Type::BROADCAST_RESPONSE: { diff --git a/client/scenes/lobby_menu.hpp b/client/lobby_menu.hpp similarity index 90% rename from client/scenes/lobby_menu.hpp rename to client/lobby_menu.hpp index 7b6c8a9..2360705 100644 --- a/client/scenes/lobby_menu.hpp +++ b/client/lobby_menu.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 @@ -30,8 +30,6 @@ //network #include "udp_network_utility.hpp" -#include "serial_packet.hpp" -#include "serial.hpp" //client #include "base_scene.hpp" @@ -42,7 +40,13 @@ class LobbyMenu : public BaseScene { public: //Public access members - LobbyMenu(ConfigUtility* const, UDPNetworkUtility* const, int* const, int* const, int* const); + LobbyMenu( + ConfigUtility* const argConfig, + UDPNetworkUtility* const argNetwork, + int* const argClientIndex, + int* const argAccountIndex, + int* const argCharacterIndex + ); ~LobbyMenu(); protected: @@ -59,6 +63,7 @@ protected: void KeyDown(SDL_KeyboardEvent const&); void KeyUp(SDL_KeyboardEvent const&); + //Network handlers void HandlePacket(SerialPacket); //shared parameters diff --git a/client/main.cpp b/client/main.cpp index 18adf3f..9ec652c 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -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 diff --git a/client/scenes/main_menu.cpp b/client/main_menu.cpp similarity index 98% rename from client/scenes/main_menu.cpp rename to client/main_menu.cpp index d5b2064..b5d633e 100644 --- a/client/scenes/main_menu.cpp +++ b/client/main_menu.cpp @@ -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 diff --git a/client/scenes/main_menu.hpp b/client/main_menu.hpp similarity index 97% rename from client/scenes/main_menu.hpp rename to client/main_menu.hpp index 9c06a0d..3816482 100644 --- a/client/scenes/main_menu.hpp +++ b/client/main_menu.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 diff --git a/client/makefile b/client/makefile index 56a4184..bba3c67 100644 --- a/client/makefile +++ b/client/makefile @@ -1,17 +1,14 @@ #config -INCLUDES+=. scenes ../common ../common/graphics ../common/map ../common/network ../common/ui -LIBS+=libclient.a ../libcommon.a -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL -llua -lsqlite3 -CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES)) -CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES)) +INCLUDES+=. ../common/gameplay ../common/graphics ../common/map ../common/network ../common/ui ../common/utilities +LIBS+=../libcommon.a -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL -llua +CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) -DGRAPHICS #source CXXSRC=$(wildcard *.cpp) -CSRC=$(wildcard *.c) #objects OBJDIR=obj OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) -OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o)) #output OUTDIR=../out @@ -19,7 +16,6 @@ OUT=$(addprefix $(OUTDIR)/,client) #targets all: $(OBJ) $(OUT) - $(MAKE) -C scenes $(CXX) $(CXXFLAGS) -o $(OUT) $(OBJ) $(LIBS) $(OBJ): | $(OBJDIR) @@ -35,9 +31,6 @@ $(OUTDIR): $(OBJDIR)/%.o: %.cpp $(CXX) $(CXXFLAGS) -c -o $@ $< -$(OBJDIR)/%.o: %.c - $(CC) $(CFLAGS) -c -o $@ $< - clean: $(RM) *.o *.a *.exe diff --git a/client/scenes/options_menu.cpp b/client/options_menu.cpp similarity index 98% rename from client/scenes/options_menu.cpp rename to client/options_menu.cpp index 414d94c..bc8290c 100644 --- a/client/scenes/options_menu.cpp +++ b/client/options_menu.cpp @@ -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 diff --git a/client/scenes/options_menu.hpp b/client/options_menu.hpp similarity index 97% rename from client/scenes/options_menu.hpp rename to client/options_menu.hpp index bc05e1f..94008d3 100644 --- a/client/scenes/options_menu.hpp +++ b/client/options_menu.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 diff --git a/client/player_character.cpp b/client/player_character.cpp deleted file mode 100644 index 8e20c92..0000000 --- a/client/player_character.cpp +++ /dev/null @@ -1,117 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013 - * - * 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 "player_character.hpp" - -#define WALKING_SPEED 140 - -void PlayerCharacter::Update(double delta) { - if (diagonal) { - constexpr double d = 1.0/sqrt(2); - position += motion * delta * d; - } - else { - position += motion * delta; - } - sprite.Update(delta); -} - -void PlayerCharacter::AdjustDirection(Direction direction) { - //shift the movement in this direction - switch(direction) { - case Direction::NORTH: - if (motion.y >= 0) { - motion.y -= WALKING_SPEED; - } - break; - case Direction::SOUTH: - if (motion.y <= 0) { - motion.y += WALKING_SPEED; - } - break; - case Direction::WEST: - if (motion.x >= 0) { - motion.x -= WALKING_SPEED; - } - break; - case Direction::EAST: - if (motion.x <= 0) { - motion.x += WALKING_SPEED; - } - break; - } - //face the correct direction - ResetDirection(); -} - -void PlayerCharacter::FaceDirection(Direction direction) { - //this function depends on the format of the sprite sheets - switch(direction) { - case Direction::NORTH: - sprite.SetYIndex(1); - break; - case Direction::SOUTH: - sprite.SetYIndex(0); - break; - case Direction::WEST: - sprite.SetYIndex(2); - break; - case Direction::EAST: - sprite.SetYIndex(3); - break; - } -} - -void PlayerCharacter::ResetDirection() { - //base the direction on the character's movement - if (motion.y > 0) { - FaceDirection(Direction::SOUTH); - } - else if (motion.y < 0) { - FaceDirection(Direction::NORTH); - } - else if (motion.x > 0) { - FaceDirection(Direction::EAST); - } - else if (motion.x < 0) { - FaceDirection(Direction::WEST); - } - ResetSpeed(); -} - -void PlayerCharacter::ResetSpeed() { - //diagonal - if (motion.x != 0 && motion.y != 0) { - sprite.SetDelay(0.1); - diagonal = true; - } - //cardinal - else if (motion != 0) { - sprite.SetDelay(0.1); - diagonal = false; - } - //not moving - else { - sprite.SetDelay(0); - sprite.SetXIndex(0); - diagonal = false; - } -} diff --git a/client/player_character.hpp b/client/player_character.hpp deleted file mode 100644 index 9dde742..0000000 --- a/client/player_character.hpp +++ /dev/null @@ -1,67 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013 - * - * 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 PLAYERCHARACTER_HPP_ -#define PLAYERCHARACTER_HPP_ - -#include "vector2.hpp" -#include "sprite_sheet.hpp" - -//TODO: correct the PlayerCharacter class and it's movement system -class PlayerCharacter { -public: - enum class Direction { - NORTH, SOUTH, EAST, WEST - }; - - PlayerCharacter() = default; - ~PlayerCharacter() = default; - - void Update(double delta); - - void DrawTo(SDL_Surface* const dest, int camX, int camY) { sprite.DrawTo(dest, position.x - camX, position.y - camY); } - - //clunky code results in smooth movement and controls - void AdjustDirection(Direction); - void FaceDirection(Direction); - void ResetDirection(); - void ResetSpeed(); - - //accessors and mutators - Vector2 SetPosition(Vector2 v) { return position = v; } - Vector2 ShiftPosition(Vector2 v) { return position += v; } - Vector2 GetPosition() { return position; } - - Vector2 SetMotion(Vector2 v) { return motion = v; } - Vector2 ShiftMotion(Vector2 v) { return motion += v; } - Vector2 GetMotion() { return motion; } - - SpriteSheet* GetSprite() { return &sprite; } -private: - Vector2 position; - Vector2 motion; - SpriteSheet sprite; - - //for moving diagonally - bool diagonal = false; -}; - -#endif diff --git a/client/scene_list.hpp b/client/scene_list.hpp index 2953289..a124237 100644 --- a/client/scene_list.hpp +++ b/client/scene_list.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 diff --git a/client/scenes/splash_screen.cpp b/client/splash_screen.cpp similarity index 97% rename from client/scenes/splash_screen.cpp rename to client/splash_screen.cpp index dd9697b..a363a2c 100644 --- a/client/scenes/splash_screen.cpp +++ b/client/splash_screen.cpp @@ -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 diff --git a/client/scenes/splash_screen.hpp b/client/splash_screen.hpp similarity index 97% rename from client/scenes/splash_screen.hpp rename to client/splash_screen.hpp index f24a1c6..2b2000b 100644 --- a/client/scenes/splash_screen.hpp +++ b/client/splash_screen.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 diff --git a/server/account_data.hpp b/common/gameplay/account_data.hpp similarity index 100% rename from server/account_data.hpp rename to common/gameplay/account_data.hpp diff --git a/common/gameplay/character_data.cpp b/common/gameplay/character_data.cpp new file mode 100644 index 0000000..e86706c --- /dev/null +++ b/common/gameplay/character_data.cpp @@ -0,0 +1,67 @@ +/* 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 "character_data.hpp" + +void CharacterData::Update(double delta) { + if (motion.x && motion.y) { + position += motion * delta * CHARACTER_WALKING_MOD; + } + else if (motion != 0) { + position += motion * delta; + } +#ifdef GRAPHICS + sprite.Update(delta); +#endif +} + +#ifdef GRAPHICS + +void CharacterData::DrawTo(SDL_Surface* const dest, int camX, int camY) { + sprite.DrawTo(dest, position.x - camX, position.y - camY); +} + +void CharacterData::CorrectSprite() { + //NOTE: These must correspond to the sprite sheet in use + if (motion.y > 0) { + sprite.SetYIndex(0); + } + else if (motion.y < 0) { + sprite.SetYIndex(1); + } + else if (motion.x > 0) { + sprite.SetYIndex(3); + } + else if (motion.x < 0) { + sprite.SetYIndex(2); + } + + //animation + if (motion != 0) { + sprite.SetDelay(0.1); + } + else { + sprite.SetDelay(0); + sprite.SetXIndex(0); + } +} + +#endif \ No newline at end of file diff --git a/server/character_data.hpp b/common/gameplay/character_data.hpp similarity index 75% rename from server/character_data.hpp rename to common/gameplay/character_data.hpp index 2db2bb0..128fe16 100644 --- a/server/character_data.hpp +++ b/common/gameplay/character_data.hpp @@ -25,8 +25,20 @@ //POD members #include "bbox.hpp" #include "vector2.hpp" +#include "statistics.hpp" +//graphics +#ifdef GRAPHICS + #include "sprite_sheet.hpp" +#endif + +//std namespace #include +#include + +//the speeds that the characters move +constexpr double CHARACTER_WALKING_SPEED = 140.0; +constexpr double CHARACTER_WALKING_MOD = 1.0/sqrt(2.0); struct CharacterData { //metadata @@ -40,28 +52,25 @@ struct CharacterData { Vector2 motion = {0.0,0.0}; //base statistics - int level = 0; - int exp = 0; - int maxHP = 0; - int health = 0; - int maxMP = 0; - int mana = 0; - int attack = 0; - int defence = 0; - int intelligence = 0; - int resistance = 0; - int speed = 0; - float accuracy = 0.0; - float evasion = 0.0; - float luck = 0.0; + Statistics stats; //TODO: equipment //TODO: items //TODO: buffs //TODO: debuffs + //methods + void Update(double delta); +#ifdef GRAPHICS + void DrawTo(SDL_Surface* const, int camX, int camY); + void CorrectSprite(); +#endif + //active gameplay members //NOTE: these are lost when unloaded +#ifdef GRAPHICS + SpriteSheet sprite; +#endif BBox bbox = {0,0,0,0}; bool inCombat = false; int atbGauge = 0; diff --git a/server/client_data.hpp b/common/gameplay/client_data.hpp similarity index 97% rename from server/client_data.hpp rename to common/gameplay/client_data.hpp index d67a1ea..7410b08 100644 --- a/server/client_data.hpp +++ b/common/gameplay/client_data.hpp @@ -26,7 +26,6 @@ struct ClientData { IPaddress address = {0,0}; - static int uidCounter; }; #endif diff --git a/server/combat_data.hpp b/common/gameplay/combat_data.hpp similarity index 78% rename from server/combat_data.hpp rename to common/gameplay/combat_data.hpp index df75325..a013577 100644 --- a/server/combat_data.hpp +++ b/common/gameplay/combat_data.hpp @@ -22,21 +22,30 @@ #ifndef COMBATDATA_HPP_ #define COMBATDATA_HPP_ +//POD members #include "vector2.hpp" #include "bbox.hpp" +//gameplay members #include "character_data.hpp" #include "enemy_data.hpp" +//graphics +#ifdef GRAPHICS + #include "sprite_sheet.hpp" +#endif + +//std namespace #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; @@ -46,7 +55,10 @@ struct CombatData { //time interval Clock::time_point lastTick = Clock::now(); - static int uidCounter; + //graphics +#ifdef GRAPHICS + SpriteSheet sprite; +#endif }; #endif diff --git a/server/enemy_data.hpp b/common/gameplay/enemy_data.hpp similarity index 82% rename from server/enemy_data.hpp rename to common/gameplay/enemy_data.hpp index 4d562fb..d0edd0a 100644 --- a/server/enemy_data.hpp +++ b/common/gameplay/enemy_data.hpp @@ -22,6 +22,15 @@ #ifndef ENEMYDATA_HPP_ #define ENEMYDATA_HPP_ +//gameplay +#include "statistics.hpp" + +//graphics +#ifdef GRAPHICS + #include "sprite_sheet.hpp" +#endif + +//std namespace #include struct EnemyData { @@ -29,21 +38,8 @@ struct EnemyData { std::string handle; std::string avatar; - //statistics - int level = 0; - int exp = 0; - int maxHP = 0; - int health = 0; - int maxMP = 0; - int mana = 0; - int attack = 0; - int defence = 0; - int intelligence = 0; - int resistance = 0; - int speed = 0; - float accuracy = 0.0; - float evasion = 0.0; - float luck = 0.0; + //gameplay + Statistics stats; //TODO: equipment //TODO: items @@ -52,6 +48,9 @@ struct EnemyData { //active gameplay members //NOTE: these are lost when unloaded +#ifdef GRAPHICS + SpriteSheet sprite; +#endif int tableIndex; int atbGauge = 0; }; diff --git a/client/scenes/makefile b/common/gameplay/makefile similarity index 50% rename from client/scenes/makefile rename to common/gameplay/makefile index c0c815b..3be52be 100644 --- a/client/scenes/makefile +++ b/common/gameplay/makefile @@ -1,21 +1,18 @@ #config -INCLUDES+=. .. ../../common ../../common/graphics ../../common/map ../../common/network ../../common/ui +INCLUDES+=. ../utilities ../graphics LIBS+= -CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES)) -CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES)) +CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) -DGRAPHICS #source CXXSRC=$(wildcard *.cpp) -CSRC=$(wildcard *.c) #objects OBJDIR=obj OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) -OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o)) #output -OUTDIR=.. -OUT=$(addprefix $(OUTDIR)/,libclient.a) +OUTDIR=../.. +OUT=$(addprefix $(OUTDIR)/,libcommon.a) #targets all: $(OBJ) $(OUT) @@ -34,9 +31,6 @@ $(OUTDIR): $(OBJDIR)/%.o: %.cpp $(CXX) $(CXXFLAGS) -c -o $@ $< -$(OBJDIR)/%.o: %.c - $(CC) $(CFLAGS) -c -o $@ $< - clean: $(RM) *.o *.a *.exe diff --git a/common/gameplay/sanity_check.cpp b/common/gameplay/sanity_check.cpp new file mode 100644 index 0000000..529cd0f --- /dev/null +++ b/common/gameplay/sanity_check.cpp @@ -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. +*/ +#include "account_data.hpp" +#include "character_data.hpp" +#include "client_data.hpp" +#include "combat_data.hpp" +#include "enemy_data.hpp" +#include "statistics.hpp" + +/* DOCS: Sanity check, read more + * Since most/all of the files in this directory are header files, I've created + * this source file as a "sanity check", to ensure that the above header files + * are written correctly via make. + * + * Oddly enough, I'm pretty sure this is the first directory compiled in a + * clean build. +*/ \ No newline at end of file diff --git a/client/scenes/in_combat.hpp b/common/gameplay/statistics.hpp similarity index 59% rename from client/scenes/in_combat.hpp rename to common/gameplay/statistics.hpp index b08a53d..71020ae 100644 --- a/client/scenes/in_combat.hpp +++ b/common/gameplay/statistics.hpp @@ -1,4 +1,4 @@ -/* Copyright: (c) Kayne Ruse 2013 +/* 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 @@ -19,30 +19,24 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#ifndef INCOMBAT_HPP_ -#define INCOMBAT_HPP_ +#ifndef STATISTICS_HPP_ +#define STATISTICS_HPP_ -#include "base_scene.hpp" - -class InCombat : public BaseScene { -public: - //Public access members - InCombat(); - ~InCombat(); - -protected: - //Frame loop - void FrameStart(); - void Update(double delta); - void FrameEnd(); - void Render(SDL_Surface* const); - - //Event handlers - 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&); +struct Statistics { + int level = 0; + int exp = 0; + int maxHP = 0; + int health = 0; + int maxMP = 0; + int mana = 0; + int attack = 0; + int defence = 0; + int intelligence = 0; + int resistance = 0; + int speed = 0; + float accuracy = 0.0; + float evasion = 0.0; + float luck = 0.0; }; -#endif +#endif \ No newline at end of file diff --git a/common/graphics/image.cpp b/common/graphics/image.cpp index 474b76d..3b4f99b 100644 --- a/common/graphics/image.cpp +++ b/common/graphics/image.cpp @@ -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 diff --git a/common/graphics/image.hpp b/common/graphics/image.hpp index eb684bb..462e565 100644 --- a/common/graphics/image.hpp +++ b/common/graphics/image.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 diff --git a/common/graphics/makefile b/common/graphics/makefile index c1b2927..afcde11 100644 --- a/common/graphics/makefile +++ b/common/graphics/makefile @@ -1,17 +1,14 @@ #config -INCLUDES+=. .. ../map +INCLUDES+=. ../map LIBS+= -CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES)) -CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES)) +CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) #source CXXSRC=$(wildcard *.cpp) -CSRC=$(wildcard *.c) #objects OBJDIR=obj OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) -OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o)) #output OUTDIR=../.. @@ -34,9 +31,6 @@ $(OUTDIR): $(OBJDIR)/%.o: %.cpp $(CXX) $(CXXFLAGS) -c -o $@ $< -$(OBJDIR)/%.o: %.c - $(CC) $(CFLAGS) -c -o $@ $< - clean: $(RM) *.o *.a *.exe diff --git a/common/graphics/sprite_sheet.cpp b/common/graphics/sprite_sheet.cpp index 39a669c..7726484 100644 --- a/common/graphics/sprite_sheet.cpp +++ b/common/graphics/sprite_sheet.cpp @@ -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 diff --git a/common/graphics/sprite_sheet.hpp b/common/graphics/sprite_sheet.hpp index a82148b..8ecaa5b 100644 --- a/common/graphics/sprite_sheet.hpp +++ b/common/graphics/sprite_sheet.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 diff --git a/common/makefile b/common/makefile index 645ac13..0e48688 100644 --- a/common/makefile +++ b/common/makefile @@ -1,46 +1,11 @@ -#config -INCLUDES+=. -LIBS+= -CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES)) -CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES)) - -#source -CXXSRC=$(wildcard *.cpp) -CSRC=$(wildcard *.c) - -#objects -OBJDIR=obj -OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) -OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o)) - -#output -OUTDIR=.. -OUT=$(addprefix $(OUTDIR)/,libcommon.a) - -#targets -all: $(OBJ) $(OUT) - ar -crs $(OUT) $(OBJ) +all: + $(MAKE) -C gameplay $(MAKE) -C graphics $(MAKE) -C map - $(MAKE) -C script $(MAKE) -C network + $(MAKE) -C script $(MAKE) -C ui - -$(OBJ): | $(OBJDIR) - -$(OUT): | $(OUTDIR) - -$(OBJDIR): - mkdir $(OBJDIR) - -$(OUTDIR): - mkdir $(OUTDIR) - -$(OBJDIR)/%.o: %.cpp - $(CXX) $(CXXFLAGS) -c -o $@ $< - -$(OBJDIR)/%.o: %.c - $(CC) $(CFLAGS) -c -o $@ $< + $(MAKE) -C utilities clean: $(RM) *.o *.a *.exe diff --git a/common/map/makefile b/common/map/makefile index c2cdfb3..769709e 100644 --- a/common/map/makefile +++ b/common/map/makefile @@ -1,17 +1,14 @@ #config -INCLUDES+=. .. ../graphics +INCLUDES+=. ../utilities LIBS+= -CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES)) -CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES)) +CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) #source CXXSRC=$(wildcard *.cpp) -CSRC=$(wildcard *.c) #objects OBJDIR=obj OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) -OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o)) #output OUTDIR=../.. @@ -34,9 +31,6 @@ $(OUTDIR): $(OBJDIR)/%.o: %.cpp $(CXX) $(CXXFLAGS) -c -o $@ $< -$(OBJDIR)/%.o: %.c - $(CC) $(CFLAGS) -c -o $@ $< - clean: $(RM) *.o *.a *.exe diff --git a/common/network/makefile b/common/network/makefile index c1b2927..7053c0f 100644 --- a/common/network/makefile +++ b/common/network/makefile @@ -1,17 +1,14 @@ #config -INCLUDES+=. .. ../map +INCLUDES+=. ../gameplay ../map ../utilities LIBS+= -CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES)) -CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES)) +CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) #source CXXSRC=$(wildcard *.cpp) -CSRC=$(wildcard *.c) #objects OBJDIR=obj OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) -OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o)) #output OUTDIR=../.. @@ -34,9 +31,6 @@ $(OUTDIR): $(OBJDIR)/%.o: %.cpp $(CXX) $(CXXFLAGS) -c -o $@ $< -$(OBJDIR)/%.o: %.c - $(CC) $(CFLAGS) -c -o $@ $< - clean: $(RM) *.o *.a *.exe diff --git a/common/network/serial.cpp b/common/network/serial.cpp index af527ef..81aecea 100644 --- a/common/network/serial.cpp +++ b/common/network/serial.cpp @@ -22,6 +22,7 @@ #include "serial.hpp" #include "map_allocator.hpp" +#include "statistics.hpp" #include @@ -59,6 +60,7 @@ void serializeClient(SerialPacket* packet, char* buffer) { //texts SERIALIZE(buffer, packet->clientInfo.username, PACKET_STRING_SIZE); + //TODO: password SERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE); SERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE); } @@ -91,6 +93,35 @@ void serializeRegionContent(SerialPacket* packet, char* buffer) { } } +void serializeCombat(SerialPacket* packet, char* buffer) { + SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); + + //integers + SERIALIZE(buffer, &packet->combatInfo.combatIndex, sizeof(int)); + SERIALIZE(buffer, &packet->combatInfo.difficulty, sizeof(int)); + //TODO: more comabat info +} + +void serializeStatistics(Statistics* stats, char* buffer) { + //integers + SERIALIZE(buffer, &stats->level, sizeof(int)); + SERIALIZE(buffer, &stats->exp, sizeof(int)); + SERIALIZE(buffer, &stats->maxHP, sizeof(int)); + SERIALIZE(buffer, &stats->health, sizeof(int)); + SERIALIZE(buffer, &stats->maxMP, sizeof(int)); + SERIALIZE(buffer, &stats->mana, sizeof(int)); + SERIALIZE(buffer, &stats->attack, sizeof(int)); + SERIALIZE(buffer, &stats->defence, sizeof(int)); + SERIALIZE(buffer, &stats->intelligence, sizeof(int)); + SERIALIZE(buffer, &stats->resistance, sizeof(int)); + SERIALIZE(buffer, &stats->speed, sizeof(int)); + + //floats + SERIALIZE(buffer, &stats->accuracy, sizeof(float)); + SERIALIZE(buffer, &stats->evasion, sizeof(float)); + SERIALIZE(buffer, &stats->luck, sizeof(float)); +} + void serializeCharacter(SerialPacket* packet, char* buffer) { SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); @@ -108,6 +139,22 @@ void serializeCharacter(SerialPacket* packet, char* buffer) { SERIALIZE(buffer, &packet->characterInfo.position.y, sizeof(double)); SERIALIZE(buffer, &packet->characterInfo.motion.x, sizeof(double)); SERIALIZE(buffer, &packet->characterInfo.motion.y, sizeof(double)); + + //stats structure + serializeStatistics(&packet->characterInfo.stats, buffer); + buffer += sizeof(Statistics); +} + +void serializeEnemy(SerialPacket* packet, char* buffer) { + SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); + + //texts + SERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE); + SERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE); + + //stats structure + serializeStatistics(&packet->characterInfo.stats, buffer); + buffer += sizeof(Statistics); } //------------------------- @@ -137,6 +184,7 @@ void deserializeClient(SerialPacket* packet, char* buffer) { //texts DESERIALIZE(buffer, packet->clientInfo.username, PACKET_STRING_SIZE); + //TODO: password DESERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE); DESERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE); } @@ -176,6 +224,37 @@ void deserializeRegionContent(SerialPacket* packet, char* buffer) { } } + +void deserializeCombat(SerialPacket* packet, char* buffer) { + DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); + + //integers + DESERIALIZE(buffer, &packet->combatInfo.combatIndex, sizeof(int)); + DESERIALIZE(buffer, &packet->combatInfo.difficulty, sizeof(int)); + //TODO: more comabat info +} + + +void deserializeStatistics(Statistics* stats, char* buffer) { + //integers + DESERIALIZE(buffer, &stats->level, sizeof(int)); + DESERIALIZE(buffer, &stats->exp, sizeof(int)); + DESERIALIZE(buffer, &stats->maxHP, sizeof(int)); + DESERIALIZE(buffer, &stats->health, sizeof(int)); + DESERIALIZE(buffer, &stats->maxMP, sizeof(int)); + DESERIALIZE(buffer, &stats->mana, sizeof(int)); + DESERIALIZE(buffer, &stats->attack, sizeof(int)); + DESERIALIZE(buffer, &stats->defence, sizeof(int)); + DESERIALIZE(buffer, &stats->intelligence, sizeof(int)); + DESERIALIZE(buffer, &stats->resistance, sizeof(int)); + DESERIALIZE(buffer, &stats->speed, sizeof(int)); + + //floats + DESERIALIZE(buffer, &stats->accuracy, sizeof(float)); + DESERIALIZE(buffer, &stats->evasion, sizeof(float)); + DESERIALIZE(buffer, &stats->luck, sizeof(float)); +} + void deserializeCharacter(SerialPacket* packet, char* buffer) { DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); @@ -193,6 +272,22 @@ void deserializeCharacter(SerialPacket* packet, char* buffer) { DESERIALIZE(buffer, &packet->characterInfo.position.y, sizeof(double)); DESERIALIZE(buffer, &packet->characterInfo.motion.x, sizeof(double)); DESERIALIZE(buffer, &packet->characterInfo.motion.y, sizeof(double)); + + //stats structure + deserializeStatistics(&packet->characterInfo.stats, buffer); + buffer += sizeof(Statistics); +} + +void deserializeEnemy(SerialPacket* packet, char* buffer) { + DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); + + //texts + DESERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE); + DESERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE); + + //stats structure + deserializeStatistics(&packet->characterInfo.stats, buffer); + buffer += sizeof(Statistics); } //------------------------- @@ -201,20 +296,29 @@ void deserializeCharacter(SerialPacket* packet, char* buffer) { void serialize(SerialPacket* packet, void* buffer) { switch(packet->meta.type) { - //No extra data + //no extra data case SerialPacket::Type::NONE: case SerialPacket::Type::PING: case SerialPacket::Type::PONG: case SerialPacket::Type::BROADCAST_REQUEST: + + //all rejections + case SerialPacket::Type::BROADCAST_REJECTION: + case SerialPacket::Type::JOIN_REJECTION: + case SerialPacket::Type::REGION_REJECTION: + case SerialPacket::Type::CHARACTER_REJECTION: + case SerialPacket::Type::ENEMY_REJECTION: + case SerialPacket::Type::COMBAT_REJECTION: + serializeType(packet, reinterpret_cast(buffer)); break; - //Server info + //server info case SerialPacket::Type::BROADCAST_RESPONSE: serializeServer(packet, reinterpret_cast(buffer)); break; - //Client info + //client info case SerialPacket::Type::JOIN_REQUEST: case SerialPacket::Type::JOIN_RESPONSE: case SerialPacket::Type::SYNCHRONIZE: @@ -232,12 +336,29 @@ void serialize(SerialPacket* packet, void* buffer) { serializeRegionContent(packet, reinterpret_cast(buffer)); break; - //Character info + //combat info + case SerialPacket::Type::COMBAT_ENTER: + case SerialPacket::Type::COMBAT_EXIT: + serializeCombat(packet, reinterpret_cast(buffer)); + break; + + //character info case SerialPacket::Type::CHARACTER_NEW: case SerialPacket::Type::CHARACTER_DELETE: case SerialPacket::Type::CHARACTER_UPDATE: + case SerialPacket::Type::CHARACTER_STATS_REQUEST: + case SerialPacket::Type::CHARACTER_STATS_RESPONSE: serializeCharacter(packet, reinterpret_cast(buffer)); break; + + //enemy info + case SerialPacket::Type::ENEMY_NEW: + case SerialPacket::Type::ENEMY_DELETE: + case SerialPacket::Type::ENEMY_UPDATE: + case SerialPacket::Type::ENEMY_STATS_REQUEST: + case SerialPacket::Type::ENEMY_STATS_RESPONSE: + serializeEnemy(packet, reinterpret_cast(buffer)); + break; } } @@ -245,20 +366,29 @@ void deserialize(SerialPacket* packet, void* buffer) { //find the type, so that you can actually deserialize the packet! deserializeType(packet, reinterpret_cast(buffer)); switch(packet->meta.type) { - //No extra data + //no extra data case SerialPacket::Type::NONE: case SerialPacket::Type::PING: case SerialPacket::Type::PONG: case SerialPacket::Type::BROADCAST_REQUEST: + + //all rejections + case SerialPacket::Type::BROADCAST_REJECTION: + case SerialPacket::Type::JOIN_REJECTION: + case SerialPacket::Type::REGION_REJECTION: + case SerialPacket::Type::CHARACTER_REJECTION: + case SerialPacket::Type::ENEMY_REJECTION: + case SerialPacket::Type::COMBAT_REJECTION: + //NOTHING break; - //Server info + //server info case SerialPacket::Type::BROADCAST_RESPONSE: deserializeServer(packet, reinterpret_cast(buffer)); break; - //Client info + //client info case SerialPacket::Type::JOIN_REQUEST: case SerialPacket::Type::JOIN_RESPONSE: case SerialPacket::Type::SYNCHRONIZE: @@ -276,11 +406,28 @@ void deserialize(SerialPacket* packet, void* buffer) { deserializeRegionContent(packet, reinterpret_cast(buffer)); break; - //Character info + //combat info + case SerialPacket::Type::COMBAT_ENTER: + case SerialPacket::Type::COMBAT_EXIT: + serializeCombat(packet, reinterpret_cast(buffer)); + break; + + //character info case SerialPacket::Type::CHARACTER_NEW: case SerialPacket::Type::CHARACTER_DELETE: case SerialPacket::Type::CHARACTER_UPDATE: + case SerialPacket::Type::CHARACTER_STATS_REQUEST: + case SerialPacket::Type::CHARACTER_STATS_RESPONSE: deserializeCharacter(packet, reinterpret_cast(buffer)); break; + + //enemy info + case SerialPacket::Type::ENEMY_NEW: + case SerialPacket::Type::ENEMY_DELETE: + case SerialPacket::Type::ENEMY_UPDATE: + case SerialPacket::Type::ENEMY_STATS_REQUEST: + case SerialPacket::Type::ENEMY_STATS_RESPONSE: + serializeEnemy(packet, reinterpret_cast(buffer)); + break; } } \ No newline at end of file diff --git a/common/network/serial.hpp b/common/network/serial.hpp index 609b1f3..29b9c2d 100644 --- a/common/network/serial.hpp +++ b/common/network/serial.hpp @@ -28,11 +28,11 @@ * NOTE: REGION_CONTENT is currently the largest type of packet * map content: REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizoeof(region::type_t) * map format: sizeof(int) * 3 - * metadata: sizeof(metadata) + * metadata: sizeof(SerialPacket::Type) */ -#define PACKET_BUFFER_SIZE REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizeof(Region::type_t) + sizeof(int) * 3 + sizeof(SerialPacket::Metadata) +#define PACKET_BUFFER_SIZE REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizeof(Region::type_t) + sizeof(int) * 3 + sizeof(SerialPacket::Type) -void serialize(SerialPacket* const, void*); -void deserialize(SerialPacket* const, void*); +void serialize(SerialPacket* const, void* dest); +void deserialize(SerialPacket* const, void* src); #endif diff --git a/common/network/serial_packet.hpp b/common/network/serial_packet.hpp index 3b8c4e5..27de13b 100644 --- a/common/network/serial_packet.hpp +++ b/common/network/serial_packet.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 @@ -24,53 +24,79 @@ #include "vector2.hpp" #include "region.hpp" +#include "statistics.hpp" #include "SDL/SDL_net.h" -#define NETWORK_VERSION 20140512 +#define NETWORK_VERSION 20140528 #define PACKET_STRING_SIZE 100 -#pragma pack(push, 0) - union SerialPacket { //types of packets enum class Type { //default: there is something wrong NONE = 0, - //not used + //keep alive PING = 1, PONG = 2, - //TODO: rejection message - - //Searching for a server to join + //searching for a server to join BROADCAST_REQUEST = 3, BROADCAST_RESPONSE = 4, + BROADCAST_REJECTION = 5, //try to join the server - JOIN_REQUEST = 5, - JOIN_RESPONSE = 6, + JOIN_REQUEST = 6, + JOIN_RESPONSE = 7, + JOIN_REJECTION = 8, //mass update - SYNCHRONIZE = 7, + SYNCHRONIZE = 9, //disconnect from the server - DISCONNECT = 8, + DISCONNECT = 10, //shut down the server - SHUTDOWN = 9, + SHUTDOWN = 11, //map data - REGION_REQUEST = 10, - REGION_CONTENT = 11, + REGION_REQUEST = 12, + REGION_CONTENT = 13, + REGION_REJECTION = 14, - //Character movement, etc. - CHARACTER_NEW = 12, - CHARACTER_DELETE = 13, - CHARACTER_UPDATE = 14, + //combat data + COMBAT_ENTER = 15, + COMBAT_EXIT = 16, - //TODO: combat packets + COMBAT_UPDATE = 17, + + COMBAT_REJECTION = 18, + + //character data + CHARACTER_NEW = 19, + CHARACTER_DELETE = 20, + CHARACTER_UPDATE = 21, + + CHARACTER_STATS_REQUEST = 22, + CHARACTER_STATS_RESPONSE = 23, + + CHARACTER_REJECTION = 24, + + //enemy data + ENEMY_NEW = 25, + ENEMY_DELETE = 26, + ENEMY_UPDATE = 27, + + ENEMY_STATS_REQUEST = 28, + ENEMY_STATS_RESPONSE = 29, + + ENEMY_REJECTION = 30, + + //more packet types go here + + //not used + LAST, }; //metadata on the packet itself @@ -79,7 +105,7 @@ union SerialPacket { IPaddress srcAddress; }meta; - //information about the server + //info about the server struct ServerInformation { Metadata meta; int networkVersion; @@ -87,18 +113,19 @@ union SerialPacket { int playerCount; }serverInfo; - //information about the client + //info about the client struct ClientInformation { Metadata meta; int clientIndex; int accountIndex; int characterIndex; char username[PACKET_STRING_SIZE]; + //TODO: password char handle[PACKET_STRING_SIZE]; char avatar[PACKET_STRING_SIZE]; }clientInfo; - //map data + //info about a region struct RegionInformation { Metadata meta; int mapIndex; @@ -106,7 +133,17 @@ union SerialPacket { Region* region; }regionInfo; - //information about a character + //info about a combat scenario + struct CombatInformation { + Metadata meta; + int combatIndex; + int difficulty; + //TODO: background image, based on terrain type + //TODO: array of combatants + //TODO: rewards + }combatInfo; + + //info about a character struct CharacterInformation { Metadata meta; int clientIndex; @@ -117,8 +154,17 @@ union SerialPacket { int mapIndex; Vector2 position; Vector2 motion; + Statistics stats; }characterInfo; + //info about an enemy + struct EnemyInformation { + Metadata meta; + char handle[PACKET_STRING_SIZE]; + char avatar[PACKET_STRING_SIZE]; + Statistics stats; + }enemyInfo; + //defaults SerialPacket() { meta.type = Type::NONE; @@ -126,6 +172,4 @@ union SerialPacket { } }; -#pragma pack(pop) - #endif diff --git a/common/network/udp_network_utility.cpp b/common/network/udp_network_utility.cpp index 5f828c7..5fb669e 100644 --- a/common/network/udp_network_utility.cpp +++ b/common/network/udp_network_utility.cpp @@ -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 @@ -21,34 +21,33 @@ */ #include "udp_network_utility.hpp" +#include "serial.hpp" + #include -void UDPNetworkUtility::Open(int port, int packSize) { - if (!(socket = SDLNet_UDP_Open(port))) { - Close(); - throw(std::runtime_error("Failed to open a UDP socket")); - } +//DOCS: memset() is used before sending a packet to remove old data; you don't want to send sensitive data over the network +//NOTE: don't confuse SerialPacket with UDPpacket - if (!(packOut = SDLNet_AllocPacket(packSize))) { +void UDPNetworkUtility::Open(int port) { + socket = SDLNet_UDP_Open(port); + packet = SDLNet_AllocPacket(PACKET_BUFFER_SIZE); + if (!socket || !packet) { Close(); - throw(std::runtime_error("Failed to allocate the out packet")); - } - - if (!(packIn = SDLNet_AllocPacket(packSize))) { - Close(); - throw(std::runtime_error("Failed to allocate the in packet")); + throw(std::runtime_error("Failed to open UDPNetworkUtility")); } } void UDPNetworkUtility::Close() { SDLNet_UDP_Close(socket); - SDLNet_FreePacket(packOut); - SDLNet_FreePacket(packIn); + SDLNet_FreePacket(packet); socket = nullptr; - packOut = nullptr; - packIn = nullptr; + packet = nullptr; } +//------------------------- +//bind to a channel +//------------------------- + int UDPNetworkUtility::Bind(const char* ip, int port, int channel) { IPaddress add; if (SDLNet_ResolveHost(&add, ip, port) == -1) { @@ -61,7 +60,7 @@ int UDPNetworkUtility::Bind(const char* ip, int port, int channel) { int UDPNetworkUtility::Bind(IPaddress* add, int channel) { int ret = SDLNet_UDP_Bind(socket, channel, add); - if (ret == -1) { + if (ret < 0) { throw(std::runtime_error("Failed to bind to a channel")); } @@ -72,25 +71,29 @@ void UDPNetworkUtility::Unbind(int channel) { SDLNet_UDP_Unbind(socket, channel); } -int UDPNetworkUtility::Send(const char* ip, int port, void* data, int len) { +//------------------------- +//send a buffer +//------------------------- + +int UDPNetworkUtility::SendTo(const char* ip, int port, void* data, int len) { IPaddress add; if (SDLNet_ResolveHost(&add, ip, port) == -1) { throw(std::runtime_error("Failed to resolve a host")); } - Send(&add, data, len); + SendTo(&add, data, len); } -int UDPNetworkUtility::Send(IPaddress* add, void* data, int len) { - if (len > packOut->maxlen) { - throw(std::runtime_error("Failed to copy the data into the packet")); +int UDPNetworkUtility::SendTo(IPaddress* add, void* data, int len) { + if (len > packet->maxlen) { + throw(std::runtime_error("The buffer is to large for the UDPpacket")); } - memset(packOut->data, 0, packOut->maxlen); - memcpy(packOut->data, data, len); - packOut->len = len; - packOut->address = *add; + memset(packet->data, 0, packet->maxlen); + memcpy(packet->data, data, len); + packet->len = len; + packet->address = *add; - int ret = SDLNet_UDP_Send(socket, -1, packOut); + int ret = SDLNet_UDP_Send(socket, -1, packet); if (ret <= 0) { throw(std::runtime_error("Failed to send a packet")); @@ -99,15 +102,15 @@ int UDPNetworkUtility::Send(IPaddress* add, void* data, int len) { return ret; } -int UDPNetworkUtility::Send(int channel, void* data, int len) { - if (len > packOut->maxlen) { - throw(std::runtime_error("Failed to copy the data into the packet")); +int UDPNetworkUtility::SendTo(int channel, void* data, int len) { + if (len > packet->maxlen) { + throw(std::runtime_error("The buffer is to large for the UDPpacket")); } - memset(packOut->data, 0, packOut->maxlen); - memcpy(packOut->data, data, len); - packOut->len = len; + memset(packet->data, 0, packet->maxlen); + memcpy(packet->data, data, len); + packet->len = len; - int ret = SDLNet_UDP_Send(socket, channel, packOut); + int ret = SDLNet_UDP_Send(socket, channel, packet); if (ret <= 0) { throw(std::runtime_error("Failed to send a packet")); @@ -116,29 +119,30 @@ int UDPNetworkUtility::Send(int channel, void* data, int len) { return ret; } -int UDPNetworkUtility::SendAll(void* data, int len) { - if (len > packOut->maxlen) { - throw(std::runtime_error("Failed to copy the data into the packet")); +int UDPNetworkUtility::SendToAllChannels(void* data, int len) { + if (len > packet->maxlen) { + throw(std::runtime_error("The buffer is to large for the UDPpacket")); } - memset(packOut->data, 0, packOut->maxlen); - memcpy(packOut->data, data, len); - packOut->len = len; + memset(packet->data, 0, packet->maxlen); + memcpy(packet->data, data, len); + packet->len = len; int sent = 0; //send to all bound channels for (int i = 0; i < SDLNET_MAX_UDPCHANNELS; i++) { if (SDLNet_UDP_GetPeerAddress(socket, i)) { - sent += SDLNet_UDP_Send(socket, i, packOut); + sent += SDLNet_UDP_Send(socket, i, packet); } } return sent; } +//TODO: put a void* and int* parameter list here int UDPNetworkUtility::Receive() { - memset(packIn->data, 0, packIn->maxlen); - int ret = SDLNet_UDP_Recv(socket, packIn); + memset(packet->data, 0, packet->maxlen); + int ret = SDLNet_UDP_Recv(socket, packet); if (ret < 0) { throw(std::runtime_error("Unknown network error occured")); @@ -146,3 +150,75 @@ int UDPNetworkUtility::Receive() { return ret; } + +//------------------------- +//send a SerialPacket +//------------------------- + +int UDPNetworkUtility::SendTo(const char* ip, int port, SerialPacket* serialPacket) { + IPaddress add; + if (SDLNet_ResolveHost(&add, ip, port) == -1) { + throw(std::runtime_error("Failed to resolve a host")); + } + + SendTo(&add, serialPacket); +} + +int UDPNetworkUtility::SendTo(IPaddress* add, SerialPacket* serialPacket) { + memset(packet->data, 0, packet->maxlen); + serialize(serialPacket, packet->data); + packet->len = PACKET_BUFFER_SIZE; + packet->address = *add; + + int ret = SDLNet_UDP_Send(socket, -1, packet); + + if (ret <= 0) { + throw(std::runtime_error("Failed to send a packet")); + } + + return ret; +} + +int UDPNetworkUtility::SendTo(int channel, SerialPacket* serialPacket) { + memset(packet->data, 0, packet->maxlen); + serialize(serialPacket, packet->data); + packet->len = PACKET_BUFFER_SIZE; + + int ret = SDLNet_UDP_Send(socket, channel, packet); + + if (ret <= 0) { + throw(std::runtime_error("Failed to send a packet")); + } + + return ret; +} + +int UDPNetworkUtility::SendToAllChannels(SerialPacket* serialPacket) { + memset(packet->data, 0, packet->maxlen); + serialize(serialPacket, packet->data); + packet->len = PACKET_BUFFER_SIZE; + + int sent = 0; + + //send to all bound channels + for (int i = 0; i < SDLNET_MAX_UDPCHANNELS; i++) { + if (SDLNet_UDP_GetPeerAddress(socket, i)) { + sent += SDLNet_UDP_Send(socket, i, packet); + } + } + + return sent; +} + +int UDPNetworkUtility::Receive(SerialPacket* serialPacket) { + memset(packet->data, 0, packet->maxlen); + int ret = SDLNet_UDP_Recv(socket, packet); + deserialize(serialPacket, packet->data); + serialPacket->meta.srcAddress = packet->address; + + if (ret < 0) { + throw(std::runtime_error("Unknown network error occured")); + } + + return ret; +} \ No newline at end of file diff --git a/common/network/udp_network_utility.hpp b/common/network/udp_network_utility.hpp index 9ba7842..4ebdd98 100644 --- a/common/network/udp_network_utility.hpp +++ b/common/network/udp_network_utility.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 @@ -24,12 +24,14 @@ #include "SDL/SDL_net.h" +#include "serial_packet.hpp" + class UDPNetworkUtility { public: UDPNetworkUtility() = default; ~UDPNetworkUtility() = default; - void Open(int port, int packSize); + void Open(int port); void Close(); //bind to a channel @@ -41,31 +43,30 @@ public: return SDLNet_UDP_GetPeerAddress(socket, channel); } - int Send(const char* ip, int port, void* data, int len); - int Send(IPaddress* add, void* data, int len); - int Send(int channel, void* data, int len); - int SendAll(void* data, int len); + //send a buffer + int SendTo(const char* ip, int port, void* data, int len); + int SendTo(IPaddress* add, void* data, int len); + int SendTo(int channel, void* data, int len); + int SendToAllChannels(void* data, int len); int Receive(); - void* GetOutData() const { - return reinterpret_cast(packOut->data); - }; - void* GetInData() const { - return reinterpret_cast(packIn->data); - }; - UDPpacket* GetOutPacket() const { - return packOut; - } - UDPpacket* GetInPacket() const { - return packIn; + //send a SerialPacket + int SendTo(const char* ip, int port, SerialPacket* serialPacket); + int SendTo(IPaddress* add, SerialPacket* serialPacket); + int SendTo(int channel, SerialPacket* serialPacket); + int SendToAllChannels(SerialPacket* serialPacket); + int Receive(SerialPacket* serialPacket); + + //accessors + UDPpacket* GetPacket() const { + return packet; } UDPsocket GetSocket() const { return socket; } private: UDPsocket socket = nullptr; - UDPpacket* packOut = nullptr; - UDPpacket* packIn = nullptr; + UDPpacket* packet = nullptr; }; #endif diff --git a/common/script/makefile b/common/script/makefile index c1b2927..7cab524 100644 --- a/common/script/makefile +++ b/common/script/makefile @@ -1,17 +1,14 @@ #config -INCLUDES+=. .. ../map +INCLUDES+=. ../map ../utilities LIBS+= -CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES)) -CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES)) +CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) #source CXXSRC=$(wildcard *.cpp) -CSRC=$(wildcard *.c) #objects OBJDIR=obj OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) -OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o)) #output OUTDIR=../.. @@ -34,9 +31,6 @@ $(OUTDIR): $(OBJDIR)/%.o: %.cpp $(CXX) $(CXXFLAGS) -c -o $@ $< -$(OBJDIR)/%.o: %.c - $(CC) $(CFLAGS) -c -o $@ $< - clean: $(RM) *.o *.a *.exe diff --git a/common/ui/button.cpp b/common/ui/button.cpp index 8d7eff9..59b0ae2 100644 --- a/common/ui/button.cpp +++ b/common/ui/button.cpp @@ -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 diff --git a/common/ui/button.hpp b/common/ui/button.hpp index ae69ec1..2fe2faa 100644 --- a/common/ui/button.hpp +++ b/common/ui/button.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 diff --git a/common/ui/makefile b/common/ui/makefile index c2cdfb3..bd9ffd1 100644 --- a/common/ui/makefile +++ b/common/ui/makefile @@ -1,17 +1,14 @@ #config -INCLUDES+=. .. ../graphics +INCLUDES+=. ../graphics LIBS+= -CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES)) -CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES)) +CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) #source CXXSRC=$(wildcard *.cpp) -CSRC=$(wildcard *.c) #objects OBJDIR=obj OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) -OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o)) #output OUTDIR=../.. @@ -34,9 +31,6 @@ $(OUTDIR): $(OBJDIR)/%.o: %.cpp $(CXX) $(CXXFLAGS) -c -o $@ $< -$(OBJDIR)/%.o: %.c - $(CC) $(CFLAGS) -c -o $@ $< - clean: $(RM) *.o *.a *.exe diff --git a/common/ui/menu_bar.cpp b/common/ui/menu_bar.cpp index a6af861..2670e32 100644 --- a/common/ui/menu_bar.cpp +++ b/common/ui/menu_bar.cpp @@ -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 diff --git a/common/ui/menu_bar.hpp b/common/ui/menu_bar.hpp index 81fbe4a..fdaf45c 100644 --- a/common/ui/menu_bar.hpp +++ b/common/ui/menu_bar.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 diff --git a/common/ui/raster_font.cpp b/common/ui/raster_font.cpp index bde074d..5005f7f 100644 --- a/common/ui/raster_font.cpp +++ b/common/ui/raster_font.cpp @@ -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 diff --git a/common/ui/raster_font.hpp b/common/ui/raster_font.hpp index 0c4cb77..c3c1ddd 100644 --- a/common/ui/raster_font.hpp +++ b/common/ui/raster_font.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 diff --git a/common/bbox.hpp b/common/utilities/bbox.hpp similarity index 100% rename from common/bbox.hpp rename to common/utilities/bbox.hpp diff --git a/common/config_utility.cpp b/common/utilities/config_utility.cpp similarity index 98% rename from common/config_utility.cpp rename to common/utilities/config_utility.cpp index a66c730..02970b1 100644 --- a/common/config_utility.cpp +++ b/common/utilities/config_utility.cpp @@ -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 diff --git a/common/config_utility.hpp b/common/utilities/config_utility.hpp similarity index 97% rename from common/config_utility.hpp rename to common/utilities/config_utility.hpp index 3de540f..6e944ef 100644 --- a/common/config_utility.hpp +++ b/common/utilities/config_utility.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 diff --git a/common/frame_rate.hpp b/common/utilities/frame_rate.hpp similarity index 100% rename from common/frame_rate.hpp rename to common/utilities/frame_rate.hpp diff --git a/common/utilities/makefile b/common/utilities/makefile new file mode 100644 index 0000000..9013447 --- /dev/null +++ b/common/utilities/makefile @@ -0,0 +1,37 @@ +#config +INCLUDES+=. +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)/,libcommon.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/common/sql_utility.cpp b/common/utilities/sql_utility.cpp similarity index 100% rename from common/sql_utility.cpp rename to common/utilities/sql_utility.cpp diff --git a/common/sql_utility.hpp b/common/utilities/sql_utility.hpp similarity index 100% rename from common/sql_utility.hpp rename to common/utilities/sql_utility.hpp diff --git a/common/utility.cpp b/common/utilities/utility.cpp similarity index 97% rename from common/utility.cpp rename to common/utilities/utility.cpp index 38c79ef..75871b4 100644 --- a/common/utility.cpp +++ b/common/utilities/utility.cpp @@ -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 diff --git a/common/utility.hpp b/common/utilities/utility.hpp similarity index 97% rename from common/utility.hpp rename to common/utilities/utility.hpp index 7e380b7..aea01ef 100644 --- a/common/utility.hpp +++ b/common/utilities/utility.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 diff --git a/common/vector2.hpp b/common/utilities/vector2.hpp similarity index 98% rename from common/vector2.hpp rename to common/utilities/vector2.hpp index c898c02..a165018 100644 --- a/common/vector2.hpp +++ b/common/utilities/vector2.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 diff --git a/makefile b/makefile index 3315bc2..27fd80e 100644 --- a/makefile +++ b/makefile @@ -1,4 +1,3 @@ -#TODO: The build process needs revising #for use on Windows: #MKDIR=mkdir diff --git a/rsc/scripts/setup_server.sql b/rsc/scripts/setup_server.sql index 1167c3c..9ea5498 100644 --- a/rsc/scripts/setup_server.sql +++ b/rsc/scripts/setup_server.sql @@ -1,56 +1,18 @@ -------------------------- ---Server -------------------------- - -CREATE TABLE IF NOT EXISTS UserAccounts ( +CREATE TABLE IF NOT EXISTS Accounts ( uid INTEGER PRIMARY KEY AUTOINCREMENT, username varchar(100) UNIQUE, --TODO: server-client security -- password varchar(100), blacklisted BIT DEFAULT 0, - whitelisted BIT DEFAULT 1 --- TODO: moderator + whitelisted BIT DEFAULT 1, + administrator BIT DEFAULT 0 ); -------------------------- ---Items -------------------------- - -CREATE TABLE IF NOT EXISTS MundaneItems ( - --metadata - uid INTEGER PRIMARY KEY AUTOINCREMENT, - itemID INTEGER, - stackSize INTEGER DEFAULT 0, - owner INTEGER REFERENCES PlayerCharacters(uid) -); - -CREATE TABLE IF NOT EXISTS Consumables ( - --metadata - uid INTEGER PRIMARY KEY AUTOINCREMENT, - itemID INTEGER, - stackSize INTEGER DEFAULT 0, - owner INTEGER REFERENCES PlayerCharacters(uid) - --holds all consumable items info (food, potions, etc.) -); - -CREATE TABLE IF NOT EXISTS Equipment ( - --metadata - uid INTEGER PRIMARY KEY AUTOINCREMENT, - itemID INTEGER, - owner INTEGER REFERENCES PlayerCharacters(uid) - --hold all equipment info - --stat mods, special effects, etc. -); - -------------------------- ---Players -------------------------- - -CREATE TABLE IF NOT EXISTS PlayerCharacters ( +CREATE TABLE IF NOT EXISTS Characters ( uid INTEGER PRIMARY KEY AUTOINCREMENT, --metadata - owner INTEGER REFERENCES UserAccounts(uid), + owner INTEGER REFERENCES Accounts(uid), handle varchar(100) UNIQUE, avatar varchar(100), birth timestamp NOT NULL DEFAULT (datetime()), @@ -77,8 +39,25 @@ CREATE TABLE IF NOT EXISTS PlayerCharacters ( luck REAL DEFAULT 0.0, --equipment - weapon INTEGER REFERENCES Equipment(uid), - helmet INTEGER REFERENCES Equipment(uid), - armour INTEGER REFERENCES Equipment(uid) + weapon INTEGER REFERENCES WornEquipment(uid), + helmet INTEGER REFERENCES WornEquipment(uid), + armour INTEGER REFERENCES WornEquipment(uid) --etc. ); + +CREATE TABLE IF NOT EXISTS InventoryItems ( + --metadata + uid INTEGER PRIMARY KEY AUTOINCREMENT, + itemID INTEGER, --type + stackSize INTEGER DEFAULT 0, + owner INTEGER REFERENCES Characters(uid) +); + +CREATE TABLE IF NOT EXISTS WornEquipment ( + --metadata + uid INTEGER PRIMARY KEY AUTOINCREMENT, + itemID INTEGER, --type + owner INTEGER REFERENCES Characters(uid) + --hold all equipment info + --stat mods, special effects, etc. +); \ No newline at end of file diff --git a/server/account_management.cpp b/server/account_management.cpp index 734244f..cca3630 100644 --- a/server/account_management.cpp +++ b/server/account_management.cpp @@ -29,10 +29,10 @@ //Define the queries //------------------------- -static const char* CREATE_USER_ACCOUNT = "INSERT INTO UserAccounts (username) VALUES (?);"; -static const char* LOAD_USER_ACCOUNT = "SELECT * FROM UserAccounts WHERE username = ?;"; -static const char* SAVE_USER_ACCOUNT = "UPDATE OR FAIL UserAccounts SET blacklisted = ?2, whitelisted = ?3 WHERE uid = ?1;"; -static const char* DELETE_USER_ACCOUNT = "DELETE FROM UserAccounts WHERE uid = ?;"; +static const char* CREATE_USER_ACCOUNT = "INSERT INTO Accounts (username) VALUES (?);"; +static const char* LOAD_USER_ACCOUNT = "SELECT * FROM Accounts WHERE username = ?;"; +static const char* SAVE_USER_ACCOUNT = "UPDATE OR FAIL Accounts SET blacklisted = ?2, whitelisted = ?3 WHERE uid = ?1;"; +static const char* DELETE_USER_ACCOUNT = "DELETE FROM Accounts WHERE uid = ?;"; //------------------------- //Define the methods diff --git a/server/character_management.cpp b/server/character_management.cpp index 58fb6b2..fe7de3e 100644 --- a/server/character_management.cpp +++ b/server/character_management.cpp @@ -29,15 +29,17 @@ //Define the queries //------------------------- -static const char* CREATE_CHARACTER = "INSERT INTO PlayerCharacters (owner, handle, avatar) VALUES (?, ?, ?);"; -static const char* LOAD_CHARACTER = "SELECT * FROM PlayerCharacters WHERE handle = ?;"; -static const char* SAVE_CHARACTER = "UPDATE OR FAIL PlayerCharacters SET mapIndex = ?2, positionX = ?3, positionY = ?4 WHERE uid = ?1;"; -static const char* DELETE_CHARACTER = "DELETE FROM PlayerCharacters WHERE uid = ?;"; +//TODO: save and load the statistics +static const char* CREATE_CHARACTER = "INSERT INTO Characters (owner, handle, avatar) VALUES (?, ?, ?);"; +static const char* LOAD_CHARACTER = "SELECT * FROM Characters WHERE handle = ?;"; +static const char* SAVE_CHARACTER = "UPDATE OR FAIL Characters SET mapIndex = ?2, positionX = ?3, positionY = ?4 WHERE uid = ?1;"; +static const char* DELETE_CHARACTER = "DELETE FROM Characters WHERE uid = ?;"; //------------------------- //Define the methods //------------------------- +//TODO: default stats as a parameter int ServerApplication::CreateCharacter(int owner, std::string handle, std::string avatar) { //Create the character, failing if it exists sqlite3_stmt* statement = nullptr; @@ -121,20 +123,20 @@ int ServerApplication::LoadCharacter(int owner, std::string handle, std::string newChar.position.y = (double)sqlite3_column_int(statement, 7); //statistics - newChar.level = sqlite3_column_int(statement, 8); - newChar.exp = sqlite3_column_int(statement, 9); - newChar.maxHP = sqlite3_column_int(statement, 10); - newChar.health = sqlite3_column_int(statement, 11); - newChar.maxMP = sqlite3_column_int(statement, 12); - newChar.mana = sqlite3_column_int(statement, 13); - newChar.attack = sqlite3_column_int(statement, 14); - newChar.defence = sqlite3_column_int(statement, 15); - newChar.intelligence = sqlite3_column_int(statement, 16); - newChar.resistance = sqlite3_column_int(statement, 17); - newChar.speed = sqlite3_column_int(statement, 18); - newChar.accuracy = sqlite3_column_double(statement, 19); - newChar.evasion = sqlite3_column_double(statement, 20); - newChar.luck = sqlite3_column_double(statement, 21); + newChar.stats.level = sqlite3_column_int(statement, 8); + newChar.stats.exp = sqlite3_column_int(statement, 9); + newChar.stats.maxHP = sqlite3_column_int(statement, 10); + newChar.stats.health = sqlite3_column_int(statement, 11); + newChar.stats.maxMP = sqlite3_column_int(statement, 12); + newChar.stats.mana = sqlite3_column_int(statement, 13); + newChar.stats.attack = sqlite3_column_int(statement, 14); + newChar.stats.defence = sqlite3_column_int(statement, 15); + newChar.stats.intelligence = sqlite3_column_int(statement, 16); + newChar.stats.resistance = sqlite3_column_int(statement, 17); + newChar.stats.speed = sqlite3_column_int(statement, 18); + newChar.stats.accuracy = sqlite3_column_double(statement, 19); + newChar.stats.evasion = sqlite3_column_double(statement, 20); + newChar.stats.luck = sqlite3_column_double(statement, 21); //TODO: equipment diff --git a/server/main.cpp b/server/main.cpp index 4da6edf..288e10f 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -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 diff --git a/server/makefile b/server/makefile index 9686a1c..901f8a6 100644 --- a/server/makefile +++ b/server/makefile @@ -1,17 +1,14 @@ #config -INCLUDES+=. ../common ../common/map ../common/script ../common/network +INCLUDES+=. ../common/gameplay ../common/map ../common/network ../common/script ../common/utilities LIBS+=../libcommon.a -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL -llua -lsqlite3 -CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES)) -CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES)) +CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) #source CXXSRC=$(wildcard *.cpp) -CSRC=$(wildcard *.c) #objects OBJDIR=obj OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) -OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o)) #output OUTDIR=../out @@ -34,9 +31,6 @@ $(OUTDIR): $(OBJDIR)/%.o: %.cpp $(CXX) $(CXXFLAGS) -c -o $@ $< -$(OBJDIR)/%.o: %.c - $(CC) $(CFLAGS) -c -o $@ $< - clean: $(RM) *.o *.a *.exe diff --git a/server/server_application.hpp b/server/server_application.hpp index 398c8b4..cbc7511 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -35,9 +35,7 @@ #include "region_pager.hpp" //networking -#include "serial_packet.hpp" #include "udp_network_utility.hpp" -#include "serial.hpp" //common #include "config_utility.hpp" @@ -106,6 +104,7 @@ private: std::map accountMap; std::map characterMap; std::map combatMap; + std::map enemyMap; //maps //TODO: I need to handle multiple map objects @@ -116,6 +115,9 @@ private: //misc bool running = true; ConfigUtility config; + int clientUID = 0; + int combatUID = 0; + int enemyUID = 0; }; #endif diff --git a/server/server_connections.cpp b/server/server_connections.cpp index 4b3bf0f..11e99ce 100644 --- a/server/server_connections.cpp +++ b/server/server_connections.cpp @@ -36,9 +36,7 @@ void ServerApplication::HandleBroadcastRequest(SerialPacket packet) { packet.serverInfo.playerCount = characterMap.size(); //bounce this packet - char buffer[PACKET_BUFFER_SIZE]; - serialize(&packet, buffer); - network.Send(&packet.meta.srcAddress, buffer, PACKET_BUFFER_SIZE); + network.SendTo(&packet.meta.srcAddress, &packet); } void ServerApplication::HandleJoinRequest(SerialPacket packet) { @@ -47,7 +45,7 @@ void ServerApplication::HandleJoinRequest(SerialPacket packet) { newClient.address = packet.meta.srcAddress; //load the user account - int accountIndex = LoadUserAccount(packet.clientInfo.username, ClientData::uidCounter); + int accountIndex = LoadUserAccount(packet.clientInfo.username, clientUID); if (accountIndex < 0) { //TODO: send rejection packet std::cerr << "Error: Account already loaded: " << accountIndex << std::endl; @@ -65,14 +63,12 @@ void ServerApplication::HandleJoinRequest(SerialPacket packet) { //send the client their info packet.meta.type = SerialPacket::Type::JOIN_RESPONSE; - packet.clientInfo.clientIndex = ClientData::uidCounter; + packet.clientInfo.clientIndex = clientUID; packet.clientInfo.accountIndex = accountIndex; packet.clientInfo.characterIndex = characterIndex; //bounce this packet - char buffer[PACKET_BUFFER_SIZE]; - serialize(&packet, buffer); - network.Send(&newClient.address, buffer, PACKET_BUFFER_SIZE); + network.SendTo(&newClient.address, &packet); //send the new character to all clients packet.meta.type = SerialPacket::Type::CHARACTER_NEW; @@ -85,7 +81,7 @@ void ServerApplication::HandleJoinRequest(SerialPacket packet) { //TODO: don't send anything to a certain client until they send the OK (the sync packet? or ignore client side?) //finished this routine - clientMap[ClientData::uidCounter++] = newClient; + clientMap[clientUID++] = newClient; std::cout << "Connect, total: " << clientMap.size() << std::endl; } @@ -94,7 +90,6 @@ void ServerApplication::HandleSynchronize(SerialPacket packet) { //send all the server's data to this client SerialPacket newPacket; - char buffer[PACKET_BUFFER_SIZE]; //characters newPacket.meta.type = SerialPacket::Type::CHARACTER_UPDATE; @@ -106,8 +101,9 @@ void ServerApplication::HandleSynchronize(SerialPacket packet) { newPacket.characterInfo.mapIndex = it.second.mapIndex; newPacket.characterInfo.position = it.second.position; newPacket.characterInfo.motion = it.second.motion; - serialize(&newPacket, buffer); - network.Send(&clientMap[packet.clientInfo.clientIndex].address, buffer, PACKET_BUFFER_SIZE); + newPacket.characterInfo.stats = it.second.stats; + + network.SendTo(&clientMap[packet.clientInfo.clientIndex].address, &newPacket); } } @@ -115,9 +111,7 @@ void ServerApplication::HandleDisconnect(SerialPacket packet) { //TODO: authenticate who is disconnecting/kicking //forward to the specified client - char buffer[PACKET_BUFFER_SIZE]; - serialize(&packet, buffer); - network.Send(&clientMap[accountMap[packet.clientInfo.accountIndex].clientIndex].address, buffer, PACKET_BUFFER_SIZE); + network.SendTo(&clientMap[accountMap[packet.clientInfo.accountIndex].clientIndex].address, &packet); //unload client and server-side characters for (std::map::iterator it = characterMap.begin(); it != characterMap.end(); /* EMPTY */ ) { @@ -173,17 +167,13 @@ void ServerApplication::HandleRegionRequest(SerialPacket packet) { packet.regionInfo.region = regionPager.GetRegion(packet.regionInfo.x, packet.regionInfo.y); //send the content - char buffer[PACKET_BUFFER_SIZE]; - serialize(&packet, buffer); - network.Send(&packet.meta.srcAddress, buffer, PACKET_BUFFER_SIZE); + network.SendTo(&packet.meta.srcAddress, &packet); } void ServerApplication::PumpPacket(SerialPacket packet) { //NOTE: I don't really like this, but it'll do for now - char buffer[PACKET_BUFFER_SIZE]; - serialize(&packet, buffer); for (auto& it : clientMap) { - network.Send(&it.second.address, buffer, PACKET_BUFFER_SIZE); + network.SendTo(&it.second.address, &packet); } } diff --git a/server/server_internals.cpp b/server/server_internals.cpp index abd16e8..1cfeb4c 100644 --- a/server/server_internals.cpp +++ b/server/server_internals.cpp @@ -22,18 +22,12 @@ #include "server_application.hpp" #include "sql_utility.hpp" +#include "serial.hpp" #include #include #include -//------------------------- -//Define the various UIDs -//------------------------- - -int ClientData::uidCounter = 0; -int CombatData::uidCounter = 0; - //------------------------- //Define the public members //------------------------- @@ -59,7 +53,7 @@ void ServerApplication::Init(int argc, char** argv) { if (SDLNet_Init()) { throw(std::runtime_error("Failed to initialize SDL_net")); } - network.Open(config.Int("server.port"), PACKET_BUFFER_SIZE); + network.Open(config.Int("server.port")); std::cout << "Initialized SDL_net" << std::endl; //Init SQL @@ -126,12 +120,7 @@ void ServerApplication::Proc() { SerialPacket packet; while(running) { //suck in the waiting packets & process them - while(network.Receive()) { - //get the packet - deserialize(&packet, network.GetInData()); - //cache the source address - packet.meta.srcAddress = network.GetInPacket()->address; - //we need to go deeper + while(network.Receive(&packet)) { HandlePacket(packet); } //update the internals