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