mirror of
https://github.com/OpenFusionProject/OpenFusion.git
synced 2024-11-22 21:40:05 +00:00
Make proximity NPC search a helper function
This commit is contained in:
parent
2b9d0f6bab
commit
1564cc7724
@ -9,8 +9,6 @@
|
|||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <cmath>
|
|
||||||
#include <limits.h>
|
|
||||||
|
|
||||||
std::map<std::string, ChatCommand> ChatManager::commands;
|
std::map<std::string, ChatCommand> ChatManager::commands;
|
||||||
|
|
||||||
@ -241,21 +239,7 @@ void unsummonWCommand(std::string full, std::vector<std::string>& args, CNSocket
|
|||||||
PlayerView& plrv = PlayerManager::players[sock];
|
PlayerView& plrv = PlayerManager::players[sock];
|
||||||
Player* plr = plrv.plr;
|
Player* plr = plrv.plr;
|
||||||
|
|
||||||
// shamelessly stolen from npcRotateCommand()
|
BaseNPC* npc = NPCManager::getNearestNPC(plrv.currentChunks, plr->x, plr->y, plr->z);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (npc == nullptr) {
|
if (npc == nullptr) {
|
||||||
ChatManager::sendServerMessage(sock, "/unsummonW: No NPCs found nearby");
|
ChatManager::sendServerMessage(sock, "/unsummonW: No NPCs found nearby");
|
||||||
@ -292,20 +276,7 @@ void npcRotateCommand(std::string full, std::vector<std::string>& args, CNSocket
|
|||||||
PlayerView& plrv = PlayerManager::players[sock];
|
PlayerView& plrv = PlayerManager::players[sock];
|
||||||
Player* plr = plrv.plr;
|
Player* plr = plrv.plr;
|
||||||
|
|
||||||
BaseNPC* npc = nullptr;
|
BaseNPC* npc = NPCManager::getNearestNPC(plrv.currentChunks, plr->x, plr->y, plr->z);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (npc == nullptr) {
|
if (npc == nullptr) {
|
||||||
ChatManager::sendServerMessage(sock, "[NPCR] No NPCs found nearby");
|
ChatManager::sendServerMessage(sock, "[NPCR] No NPCs found nearby");
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
#include "contrib/JSON.hpp"
|
#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));
|
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<Chunk*> 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;
|
||||||
|
}
|
||||||
|
@ -47,4 +47,6 @@ namespace NPCManager {
|
|||||||
void npcCombineItems(CNSocket* sock, CNPacketData* data);
|
void npcCombineItems(CNSocket* sock, CNPacketData* data);
|
||||||
|
|
||||||
void handleWarp(CNSocket* sock, int32_t warpId);
|
void handleWarp(CNSocket* sock, int32_t warpId);
|
||||||
|
|
||||||
|
BaseNPC* getNearestNPC(std::vector<Chunk*> chunks, int X, int Y, int Z);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user