From 1564cc77243d74f0637d69461f3e7cd2207a3e7f Mon Sep 17 00:00:00 2001 From: Gent Date: Sat, 10 Oct 2020 23:12:12 -0400 Subject: [PATCH] Make proximity NPC search a helper function --- src/ChatManager.cpp | 33 ++------------------------------- src/NPCManager.cpp | 22 ++++++++++++++++++++++ src/NPCManager.hpp | 2 ++ 3 files changed, 26 insertions(+), 31 deletions(-) diff --git a/src/ChatManager.cpp b/src/ChatManager.cpp index f584e76..7642cd7 100644 --- a/src/ChatManager.cpp +++ b/src/ChatManager.cpp @@ -9,8 +9,6 @@ #include #include -#include -#include std::map ChatManager::commands; @@ -241,21 +239,7 @@ void unsummonWCommand(std::string full, std::vector& args, CNSocket PlayerView& plrv = PlayerManager::players[sock]; Player* plr = plrv.plr; - // shamelessly stolen from npcRotateCommand() - BaseNPC* npc = nullptr; - int lastDist = INT_MAX; - for (auto c = plrv.currentChunks.begin(); c != plrv.currentChunks.end(); c++) { - Chunk* chunk = *c; - for (auto _npc = chunk->NPCs.begin(); _npc != chunk->NPCs.end(); _npc++) { - BaseNPC* npcTemp = NPCManager::NPCs[*_npc]; - int distXY = std::hypot(plr->x - npcTemp->appearanceData.iX, plr->y - npcTemp->appearanceData.iY); - int dist = std::hypot(distXY, plr->z - npcTemp->appearanceData.iZ); - if (dist < lastDist) { - npc = npcTemp; - lastDist = dist; - } - } - } + BaseNPC* npc = NPCManager::getNearestNPC(plrv.currentChunks, plr->x, plr->y, plr->z); if (npc == nullptr) { ChatManager::sendServerMessage(sock, "/unsummonW: No NPCs found nearby"); @@ -292,20 +276,7 @@ void npcRotateCommand(std::string full, std::vector& args, CNSocket PlayerView& plrv = PlayerManager::players[sock]; Player* plr = plrv.plr; - BaseNPC* npc = nullptr; - int lastDist = INT_MAX; - for (auto c = plrv.currentChunks.begin(); c != plrv.currentChunks.end(); c++) { // haha get it - Chunk* chunk = *c; - for (auto _npc = chunk->NPCs.begin(); _npc != chunk->NPCs.end(); _npc++) { - BaseNPC* npcTemp = NPCManager::NPCs[*_npc]; - int distXY = std::hypot(plr->x - npcTemp->appearanceData.iX, plr->y - npcTemp->appearanceData.iY); - int dist = std::hypot(distXY, plr->z - npcTemp->appearanceData.iZ); - if (dist < lastDist) { - npc = npcTemp; - lastDist = dist; - } - } - } + BaseNPC* npc = NPCManager::getNearestNPC(plrv.currentChunks, plr->x, plr->y, plr->z); if (npc == nullptr) { ChatManager::sendServerMessage(sock, "[NPCR] No NPCs found nearby"); diff --git a/src/NPCManager.cpp b/src/NPCManager.cpp index 225fe47..18ad04f 100644 --- a/src/NPCManager.cpp +++ b/src/NPCManager.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include "contrib/JSON.hpp" @@ -588,3 +589,24 @@ void NPCManager::handleWarp(CNSocket* sock, int32_t warpId) { sock->sendPacket((void*)&resp, P_FE2CL_REP_PC_WARP_USE_NPC_SUCC, sizeof(sP_FE2CL_REP_PC_WARP_USE_NPC_SUCC)); } + +/* + * Helper function to get NPC closest to coordinates in specified chunks + */ +BaseNPC* NPCManager::getNearestNPC(std::vector chunks, int X, int Y, int Z) { + BaseNPC* npc = nullptr; + int lastDist = INT_MAX; + for (auto c = chunks.begin(); c != chunks.end(); c++) { // haha get it + Chunk* chunk = *c; + for (auto _npc = chunk->NPCs.begin(); _npc != chunk->NPCs.end(); _npc++) { + BaseNPC* npcTemp = NPCs[*_npc]; + int distXY = std::hypot(X - npcTemp->appearanceData.iX, Y - npcTemp->appearanceData.iY); + int dist = std::hypot(distXY, Z - npcTemp->appearanceData.iZ); + if (dist < lastDist) { + npc = npcTemp; + lastDist = dist; + } + } + } + return npc; +} diff --git a/src/NPCManager.hpp b/src/NPCManager.hpp index abf0712..20ef45f 100644 --- a/src/NPCManager.hpp +++ b/src/NPCManager.hpp @@ -47,4 +47,6 @@ namespace NPCManager { void npcCombineItems(CNSocket* sock, CNPacketData* data); void handleWarp(CNSocket* sock, int32_t warpId); + + BaseNPC* getNearestNPC(std::vector chunks, int X, int Y, int Z); }