Make /npcr use the nearest NPC

This commit is contained in:
Gent 2020-10-06 18:34:11 -04:00
parent 42fc018097
commit 5ed332d836

View File

@ -4,9 +4,11 @@
#include "PlayerManager.hpp" #include "PlayerManager.hpp"
#include "TransportManager.hpp" #include "TransportManager.hpp"
#include "TableData.hpp" #include "TableData.hpp"
#include "limits.h"
#include <sstream> #include <sstream>
#include <iterator> #include <iterator>
#include <cmath>
std::map<std::string, ChatCommand> ChatManager::commands; std::map<std::string, ChatCommand> ChatManager::commands;
@ -187,40 +189,39 @@ void mssCommand(std::string full, std::vector<std::string>& args, CNSocket* sock
void npcRotateCommand(std::string full, std::vector<std::string>& args, CNSocket* sock) { void npcRotateCommand(std::string full, std::vector<std::string>& args, CNSocket* sock) {
if (args.size() < 2) { PlayerView& plrv = PlayerManager::players[sock];
ChatManager::sendServerMessage(sock, "[NPCR] Too few arguments"); Player* plr = plrv.plr;
ChatManager::sendServerMessage(sock, "[NPCR] Usage: /npcr <NPC ID>");
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;
}
}
}
if (npc == nullptr) {
ChatManager::sendServerMessage(sock, "[NPCR] No NPCs found nearby");
return; return;
} }
// Validate NPC ID number int angle = (plr->angle + 180) % 360;
char* npcIDC; NPCManager::updateNPCPosition(npc->appearanceData.iNPC_ID, npc->appearanceData.iX, npc->appearanceData.iY, npc->appearanceData.iZ, angle);
int npcID = std::strtol(args[1].c_str(), &npcIDC, 10); TableData::RunningNPCRotations[npc->appearanceData.iNPC_ID] = angle;
if (*npcIDC) {
// not an integer
ChatManager::sendServerMessage(sock, "[NPCR] Invalid NPC ID '" + args[1] + "'");
return;
}
// Ensure NPC exists // update rotation clientside
if (NPCManager::NPCs.find(npcID) == NPCManager::NPCs.end()) { INITSTRUCT(sP_FE2CL_NPC_ENTER, pkt);
// doesn't exist pkt.NPCAppearanceData = npc->appearanceData;
ChatManager::sendServerMessage(sock, "[NPCR] Can't find NPC with ID " + args[1]); sock->sendPacket((void*)&pkt, P_FE2CL_NPC_ENTER, sizeof(sP_FE2CL_NPC_ENTER));
return;
}
Player* plr = PlayerManager::getPlayer(sock); ChatManager::sendServerMessage(sock, "[NPCR] Successfully set angle to " + std::to_string(angle) + " for NPC " + std::to_string(npc->appearanceData.iNPC_ID));
BaseNPC* npc = NPCManager::NPCs[npcID];
int angle = plr->angle + 180;
// normalize angle
while (angle >= 360)
angle -= 360;
while (angle < 0)
angle += 360;
NPCManager::updateNPCPosition(npcID, npc->appearanceData.iX, npc->appearanceData.iY, npc->appearanceData.iZ, angle);
TableData::RunningNPCRotations[npcID] = angle;
PlayerManager::sendPlayerTo(sock, plr->x, plr->y, plr->z);
ChatManager::sendServerMessage(sock, "[NPCR] Successfully set angle to " + std::to_string(angle) + " for NPC " + args[1]);
} }
void refreshCommand(std::string full, std::vector<std::string>& args, CNSocket* sock) { void refreshCommand(std::string full, std::vector<std::string>& args, CNSocket* sock) {