Rewrite /lair command

This wasn't strictly necessary as this command has outlived its
usefulness, but I had gone ahead and rewritten it because it was (barring
taskStart()) the only place in the codebase that accesses Chunking::chunks
outside of Chunking.cpp. This became apparent during a (currently paused)
effort to improve the API that the Chunking namespace exposes to the
rest of the codebase.

I went ahead and rewrote the rest of this command as it was poorly
implemented anyway. This has been sitting in my working directory
uncommitted for a few months, so I may as well push it.
This commit is contained in:
dongresource 2021-09-19 04:46:28 +02:00
parent bab17eb23f
commit 25ce0f6d82

View File

@ -13,6 +13,7 @@
#include <sstream>
#include <iterator>
#include <math.h>
#include <limits.h>
typedef void (*CommandHandler)(std::string fullString, std::vector<std::string>& args, CNSocket* sock);
@ -690,39 +691,35 @@ static void whoisCommand(std::string full, std::vector<std::string>& args, CNSoc
static void lairUnlockCommand(std::string full, std::vector<std::string>& args, CNSocket* sock) {
Player* plr = PlayerManager::getPlayer(sock);
if (!Chunking::chunkExists(plr->chunkPos))
return;
Chunk* chnk = Chunking::chunks[plr->chunkPos];
int taskID = -1;
int missionID = -1;
int found = 0;
int lastDist = INT_MAX;
for (Chunk *chnk : Chunking::getViewableChunks(plr->chunkPos)) {
for (const EntityRef& ref : chnk->entities) {
if (ref.type == EntityType::PLAYER)
continue;
int32_t id = ref.id;
if (NPCManager::NPCs.find(id) == NPCManager::NPCs.end())
BaseNPC* npc = (BaseNPC*)ref.getEntity();
int distXY = std::hypot(plr->x - npc->x, plr->y - npc->y);
int dist = std::hypot(distXY, plr->z - npc->z);
if (dist >= lastDist)
continue;
BaseNPC* npc = NPCManager::NPCs[id];
for (auto it = NPCManager::Warps.begin(); it != NPCManager::Warps.end(); it++) {
if ((*it).second.npcID == npc->appearanceData.iNPCType) {
taskID = (*it).second.limitTaskID;
if (it->second.npcID == npc->appearanceData.iNPCType) {
taskID = it->second.limitTaskID;
missionID = Missions::Tasks[taskID]->task["m_iHMissionID"];
found++;
lastDist = dist;
break;
}
}
}
}
if (missionID == -1 || taskID == -1) {
Chat::sendServerMessage(sock, "You are NOT standing near a lair portal; move around and try again!");
return;
}
if (found > 1) {
Chat::sendServerMessage(sock, "More than one lair found; decrease chunk size and try again!");
Chat::sendServerMessage(sock, "No nearby Lair portals found.");
return;
}