From 803073213e103c3c32e203ed2301288adc23d1e8 Mon Sep 17 00:00:00 2001 From: dongresource Date: Wed, 20 Oct 2021 00:40:40 +0200 Subject: [PATCH] [refactor] Replace a few uses of magic numbers with enums --- src/BuiltinCommands.cpp | 12 ++++++------ src/Email.hpp | 2 +- src/Missions.cpp | 4 ++-- src/MobAI.cpp | 8 ++++---- src/PlayerManager.cpp | 31 +++++++++++++++--------------- src/core/Defines.hpp | 42 +++++++++++++++++++++++++++++++---------- 6 files changed, 61 insertions(+), 38 deletions(-) diff --git a/src/BuiltinCommands.cpp b/src/BuiltinCommands.cpp index 6f60430..356992a 100644 --- a/src/BuiltinCommands.cpp +++ b/src/BuiltinCommands.cpp @@ -247,17 +247,17 @@ static void teleportPlayer(CNSocket *sock, CNPacketData *data) { uint64_t instance = plr->instanceID; const int unstickRange = 400; - switch (req->eTeleportType) { - case eCN_GM_TeleportMapType__MyLocation: + switch ((eCN_GM_TeleportType)req->eTeleportType) { + case eCN_GM_TeleportType::MyLocation: PlayerManager::sendPlayerTo(targetSock, plr->x, plr->y, plr->z, instance); break; - case eCN_GM_TeleportMapType__MapXYZ: + case eCN_GM_TeleportType::MapXYZ: instance = req->iToMap; // fallthrough - case eCN_GM_TeleportMapType__XYZ: + case eCN_GM_TeleportType::XYZ: PlayerManager::sendPlayerTo(targetSock, req->iToX, req->iToY, req->iToZ, instance); break; - case eCN_GM_TeleportMapType__SomeoneLocation: + case eCN_GM_TeleportType::SomeoneLocation: // player to teleport to goalSock = PlayerManager::getSockFromAny(req->eGoalPCSearchBy, req->iGoalPC_ID, req->iGoalPC_UID, AUTOU16TOU8(req->szGoalPC_FirstName), AUTOU16TOU8(req->szGoalPC_LastName)); @@ -269,7 +269,7 @@ static void teleportPlayer(CNSocket *sock, CNPacketData *data) { PlayerManager::sendPlayerTo(targetSock, goalPlr->x, goalPlr->y, goalPlr->z, goalPlr->instanceID); break; - case eCN_GM_TeleportMapType__Unstick: + case eCN_GM_TeleportType::Unstick: targetPlr = PlayerManager::getPlayer(targetSock); PlayerManager::sendPlayerTo(targetSock, targetPlr->x - unstickRange/2 + Rand::rand(unstickRange), diff --git a/src/Email.hpp b/src/Email.hpp index 3e5c557..55e4302 100644 --- a/src/Email.hpp +++ b/src/Email.hpp @@ -5,6 +5,6 @@ namespace Email { extern std::vector dump; - + void init(); } diff --git a/src/Missions.cpp b/src/Missions.cpp index bd6891e..5a33354 100644 --- a/src/Missions.cpp +++ b/src/Missions.cpp @@ -367,7 +367,7 @@ static void taskStart(CNSocket* sock, CNPacketData* data) { sock->sendPacket((void*)&response, P_FE2CL_REP_PC_TASK_START_SUCC, sizeof(sP_FE2CL_REP_PC_TASK_START_SUCC)); // if escort task, assign matching paths to all nearby NPCs - if (task["m_iHTaskType"] == 6) { + if (task["m_iHTaskType"] == (int)eTaskTypeProperty::EscortDefence) { for (ChunkPos& chunkPos : Chunking::getChunksInMap(plr->instanceID)) { // check all NPCs in the instance Chunk* chunk = Chunking::chunks[chunkPos]; for (EntityRef ref : chunk->entities) { @@ -399,7 +399,7 @@ static void taskEnd(CNSocket* sock, CNPacketData* data) { * once we comb over mission logic more throughly */ bool mobsAreKilled = false; - if (task->task["m_iHTaskType"] == 5) { + if (task->task["m_iHTaskType"] == (int)eTaskTypeProperty::Defeat) { mobsAreKilled = true; for (int i = 0; i < ACTIVE_MISSION_COUNT; i++) { if (plr->tasks[i] == missionData->iTaskNum) { diff --git a/src/MobAI.cpp b/src/MobAI.cpp index 4808156..3f4c020 100644 --- a/src/MobAI.cpp +++ b/src/MobAI.cpp @@ -234,14 +234,14 @@ static void dealCorruption(Mob *mob, std::vector targetData, int skillID, i int style2 = Nanos::nanoStyle(plr->activeNano); if (style2 == -1) { // no nano - respdata[i].iHitFlag = 8; + respdata[i].iHitFlag = HF_BIT_STYLE_TIE; respdata[i].iDamage = Abilities::SkillTable[skillID].powerIntensity[0] * PC_MAXHEALTH((int)mob->data["m_iNpcLevel"]) / 1500; } else if (style == style2) { - respdata[i].iHitFlag = 8; // tie + respdata[i].iHitFlag = HF_BIT_STYLE_TIE; respdata[i].iDamage = 0; respdata[i].iNanoStamina = plr->Nanos[plr->activeNano].iStamina; } else if (style - style2 == 1 || style2 - style == 2) { - respdata[i].iHitFlag = 4; // win + respdata[i].iHitFlag = HF_BIT_STYLE_WIN; respdata[i].iDamage = 0; respdata[i].iNanoStamina = plr->Nanos[plr->activeNano].iStamina += 45; if (plr->Nanos[plr->activeNano].iStamina > 150) @@ -252,7 +252,7 @@ static void dealCorruption(Mob *mob, std::vector targetData, int skillID, i if (pwr.skillType == EST_DAMAGE) pwr.handle(sock, targetData2, plr->activeNano, skillID, 0, 200); } else { - respdata[i].iHitFlag = 16; // lose + respdata[i].iHitFlag = HF_BIT_STYLE_LOSE; respdata[i].iDamage = Abilities::SkillTable[skillID].powerIntensity[0] * PC_MAXHEALTH((int)mob->data["m_iNpcLevel"]) / 1500; respdata[i].iNanoStamina = plr->Nanos[plr->activeNano].iStamina -= 90; if (plr->Nanos[plr->activeNano].iStamina < 0) { diff --git a/src/PlayerManager.cpp b/src/PlayerManager.cpp index 5785db2..98f351b 100644 --- a/src/PlayerManager.cpp +++ b/src/PlayerManager.cpp @@ -406,21 +406,22 @@ static void revivePlayer(CNSocket* sock, CNPacketData* data) { int activeSlot = -1; bool move = false; - if (reviveData->iRegenType == 3 && plr->iConditionBitFlag & CSB_BIT_PHOENIX) { - // nano revive + switch ((ePCRegenType)reviveData->iRegenType) { + case ePCRegenType::HereByPhoenix: // nano revive + if (!(plr->iConditionBitFlag & CSB_BIT_PHOENIX)) + return; // sanity check plr->Nanos[plr->activeNano].iStamina = 0; - plr->HP = PC_MAXHEALTH(plr->level) / 2; Abilities::applyBuff(sock, plr->Nanos[plr->activeNano].iSkillID, 2, 1, 0); - } else if (reviveData->iRegenType == 4) { - // revived by group member's nano + // fallthrough + case ePCRegenType::HereByPhoenixGroup: // revived by group member's nano plr->HP = PC_MAXHEALTH(plr->level) / 2; - } else if (reviveData->iRegenType == 5) { - // warp away - move = true; - } else { - // plain respawn - move = true; + break; + default: // plain respawn plr->HP = PC_MAXHEALTH(plr->level) / 2; + // fallthrough + case ePCRegenType::Unstick: // warp away + move = true; + break; } for (int i = 0; i < 3; i++) { @@ -667,16 +668,16 @@ CNSocket *PlayerManager::getSockFromName(std::string firstname, std::string last } CNSocket *PlayerManager::getSockFromAny(int by, int id, int uid, std::string firstname, std::string lastname) { - switch (by) { - case eCN_GM_TargetSearchBy__PC_ID: + switch ((eCN_GM_TargetSearchBy)by) { + case eCN_GM_TargetSearchBy::PC_ID: assert(id != 0); return getSockFromID(id); - case eCN_GM_TargetSearchBy__PC_UID: // account id; not player id + case eCN_GM_TargetSearchBy::PC_UID: // account id; not player id assert(uid != 0); for (auto& pair : players) if (pair.second->accountId == uid) return pair.first; - case eCN_GM_TargetSearchBy__PC_Name: + case eCN_GM_TargetSearchBy::PC_Name: assert(firstname != "" && lastname != ""); // XXX: remove this if we start messing around with edited names? return getSockFromName(firstname, lastname); } diff --git a/src/core/Defines.hpp b/src/core/Defines.hpp index 3cb0e05..185b5c5 100644 --- a/src/core/Defines.hpp +++ b/src/core/Defines.hpp @@ -10,18 +10,40 @@ const float CN_EP_RANK_4 = 0.3f; const float CN_EP_RANK_5 = 0.29f; // methods of finding players for GM commands -enum eCN_GM_TargetSearchBy { - eCN_GM_TargetSearchBy__PC_ID, // player id - eCN_GM_TargetSearchBy__PC_Name, // firstname, lastname - eCN_GM_TargetSearchBy__PC_UID // account id +enum class eCN_GM_TargetSearchBy { + PC_ID, // player id + PC_Name, // firstname, lastname + PC_UID // account id }; -enum eCN_GM_TeleportType { - eCN_GM_TeleportMapType__XYZ, - eCN_GM_TeleportMapType__MapXYZ, - eCN_GM_TeleportMapType__MyLocation, - eCN_GM_TeleportMapType__SomeoneLocation, - eCN_GM_TeleportMapType__Unstick +enum class eCN_GM_TeleportType { + XYZ, + MapXYZ, + MyLocation, + SomeoneLocation, + Unstick +}; + +enum class eTaskTypeProperty { + None = -1, + Talk = 1, + GotoLocation = 2, + UseItems = 3, + Delivery = 4, + Defeat = 5, + EscortDefence = 6, + Max = 7 +}; + +enum class ePCRegenType { + None, + Xcom, + Here, + HereByPhoenix, + HereByPhoenixGroup, + Unstick, + HereByPhoenixItem, + End }; // nano powers