diff --git a/server/combat_instance.hpp b/server/combat_data.hpp similarity index 87% rename from server/combat_instance.hpp rename to server/combat_data.hpp index 30257ca..df75325 100644 --- a/server/combat_instance.hpp +++ b/server/combat_data.hpp @@ -19,8 +19,8 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#ifndef COMBATINSTANCE_HPP_ -#define COMBATINSTACNE_HPP_ +#ifndef COMBATDATA_HPP_ +#define COMBATDATA_HPP_ #include "vector2.hpp" #include "bbox.hpp" @@ -31,7 +31,9 @@ #include #include -struct CombatInstance { +struct CombatData { + typedef std::chrono::steady_clock Clock; + //combatants std::list characterList; std::list enemyList; @@ -42,9 +44,9 @@ struct CombatInstance { BBox bbox = {0,0,0,0}; //time interval - std::chrono::time_point lastTick; + Clock::time_point lastTick = Clock::now(); - void Update(); + static int uidCounter; }; #endif diff --git a/server/combat_management.cpp b/server/combat_management.cpp index 2c822a0..7376f5a 100644 --- a/server/combat_management.cpp +++ b/server/combat_management.cpp @@ -21,4 +21,81 @@ */ #include "server_application.hpp" -//TODO: method definitions \ No newline at end of file +#include "utility.hpp" + +int ServerApplication::CreateCombatInstance(int mapIndex, int x, int y) { + CombatData& combat = combatMap[CombatData::uidCounter]; + + combat.mapIndex = mapIndex; + combat.position.x = x; + combat.position.y = y; + + //explicitly postfix + return CombatData::uidCounter++; +} + +void ServerApplication::UnloadCombatInstance(int uid) { + for (auto& it : combatMap[uid].characterList) { + it->inCombat = false; + } + combatMap.erase(uid); +} + +void ServerApplication::UpdateCombat() { + for (auto& combat : combatMap) { + //prune characters that have left + erase_if(combat.second.characterList, [](CharacterData* it) -> bool { + return !it->inCombat; + }); + + //TODO: prune dead enemies + + //update the instance once per second + if (CombatData::Clock::now() - combat.second.lastTick > std::chrono::duration(1)) { + //increase the ATB gauges + for (auto& it : combat.second.characterList) { + it->atbGauge += it->speed; + } + for (auto& it : combat.second.enemyList) { + it.atbGauge += it.speed; + } + combat.second.lastTick = CombatData::Clock::now(); + + //execute the combat commands + for (auto& it : combat.second.characterList) { + if (it->atbGauge >= 100 && /* TODO: Check that there is something stored... */ true ) { + it->atbGauge = 0; + //TODO: EXECUTE STORED COMMAND + } + } + for (auto& it : combat.second.enemyList) { + if (it.atbGauge >= 100) { + it.atbGauge = 0; + //TODO: EXECUTE AI SCRIPT + } + } + } + } + + //Erase instances with no enemies left + erase_if(combatMap, [](std::pair& combat) -> bool { + if (combat.second.enemyList.size() == 0) { + //kick the characters out + for (auto& it : combat.second.characterList) { + it->inCombat = false; + } + return true; + } + return false; + }); + + //TODO: reset instances with no players? +} + +void ServerApplication::PushCharacterToCombat() { + //TODO +} + +void ServerApplication::PopCharacterFromCombat() { + //TODO +} \ No newline at end of file diff --git a/server/server_application.hpp b/server/server_application.hpp index 3953a30..4dd50ea 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -26,6 +26,7 @@ #include "client_data.hpp" #include "account_data.hpp" #include "character_data.hpp" +#include "combat_data.hpp" //maps #include "map_allocator.hpp" @@ -85,14 +86,19 @@ private: void UnloadUserAccount(int uid); void DeleteUserAccount(int uid); - //TODO: character management + //character management int CreateCharacter(int owner, std::string handle, std::string avatar); int LoadCharacter(int owner, std::string handle, std::string avatar); int SaveCharacter(int uid); void UnloadCharacter(int uid); void DeleteCharacter(int uid); - //TODO: combat systems + //TODO: combat management + int CreateCombatInstance(int mapIndex, int x, int y); + void UnloadCombatInstance(int uid); + void UpdateCombat(); + void PushCharacterToCombat(); + void PopCharacterFromCombat(); //APIs UDPNetworkUtility network; @@ -103,6 +109,7 @@ private: std::map clientMap; std::map accountMap; std::map characterMap; + std::map combatMap; //maps //TODO: I need to handle multiple map objects diff --git a/server/server_internals.cpp b/server/server_internals.cpp index c2b83c1..abd16e8 100644 --- a/server/server_internals.cpp +++ b/server/server_internals.cpp @@ -32,6 +32,7 @@ //------------------------- int ClientData::uidCounter = 0; +int CombatData::uidCounter = 0; //------------------------- //Define the public members