Clean up /path command handler

This commit is contained in:
gsemaj 2021-05-06 12:06:40 -04:00 committed by Gent Semaj
parent 2d7aa3c536
commit 32daa68458

View File

@ -58,6 +58,18 @@ bool CustomCommands::runCmd(std::string full, CNSocket* sock) {
return false; return false;
} }
//
static void updatePathMarkers(CNSocket* sock) {
Player* plr = PlayerManager::getPlayer(sock);
if (TableData::RunningNPCPaths.find(plr->iID) != TableData::RunningNPCPaths.end()) {
for (BaseNPC* marker : TableData::RunningNPCPaths[plr->iID].second)
marker->enterIntoViewOf(sock);
}
}
//
static void helpCommand(std::string full, std::vector<std::string>& args, CNSocket* sock) { static void helpCommand(std::string full, std::vector<std::string>& args, CNSocket* sock) {
Chat::sendServerMessage(sock, "Commands available to you:"); Chat::sendServerMessage(sock, "Commands available to you:");
Player *plr = PlayerManager::getPlayer(sock); Player *plr = PlayerManager::getPlayer(sock);
@ -948,7 +960,8 @@ static void pathCommand(std::string full, std::vector<std::string>& args, CNSock
if (args.size() < 2) { if (args.size() < 2) {
Chat::sendServerMessage(sock, "[PATH] Too few arguments"); Chat::sendServerMessage(sock, "[PATH] Too few arguments");
Chat::sendServerMessage(sock, "[PATH] Usage: /path <start/kf/undo/here/test/cancel/end>"); Chat::sendServerMessage(sock, "[PATH] Usage: /path <start/kf/undo/here/test/cancel/end>");
goto update; updatePathMarkers(sock);
return;
} }
// /path start // /path start
@ -957,14 +970,15 @@ static void pathCommand(std::string full, std::vector<std::string>& args, CNSock
if (TableData::RunningNPCPaths.find(plr->iID) != TableData::RunningNPCPaths.end()) { if (TableData::RunningNPCPaths.find(plr->iID) != TableData::RunningNPCPaths.end()) {
Chat::sendServerMessage(sock, "[PATH] An NPC is already following you"); Chat::sendServerMessage(sock, "[PATH] An NPC is already following you");
Chat::sendServerMessage(sock, "[PATH] Run '/path end' first, if you're done"); Chat::sendServerMessage(sock, "[PATH] Run '/path end' first, if you're done");
goto update; updatePathMarkers(sock);
return;
} }
// find nearest NPC // find nearest NPC
BaseNPC* npc = NPCManager::getNearestNPC(&plr->viewableChunks, plr->x, plr->y, plr->z); BaseNPC* npc = NPCManager::getNearestNPC(&plr->viewableChunks, plr->x, plr->y, plr->z);
if (npc == nullptr) { if (npc == nullptr) {
Chat::sendServerMessage(sock, "[PATH] No NPCs found nearby"); Chat::sendServerMessage(sock, "[PATH] No NPCs found nearby");
goto update; return;
} }
// add first point at NPC's current location // add first point at NPC's current location
@ -974,13 +988,15 @@ static void pathCommand(std::string full, std::vector<std::string>& args, CNSock
// map from player // map from player
TableData::RunningNPCPaths[plr->iID] = std::make_pair(npc, pathPoints); TableData::RunningNPCPaths[plr->iID] = std::make_pair(npc, pathPoints);
Chat::sendServerMessage(sock, "[PATH] NPC " + std::to_string(npc->appearanceData.iNPC_ID) + " is now following you"); Chat::sendServerMessage(sock, "[PATH] NPC " + std::to_string(npc->appearanceData.iNPC_ID) + " is now following you");
goto update; updatePathMarkers(sock);
} else { return;
}
// make sure the player has a follower // make sure the player has a follower
if (TableData::RunningNPCPaths.find(plr->iID) == TableData::RunningNPCPaths.end()) { if (TableData::RunningNPCPaths.find(plr->iID) == TableData::RunningNPCPaths.end()) {
Chat::sendServerMessage(sock, "[PATH] No NPC is currently following you"); Chat::sendServerMessage(sock, "[PATH] No NPC is currently following you");
Chat::sendServerMessage(sock, "[PATH] Run '/path start' near an NPC first"); Chat::sendServerMessage(sock, "[PATH] Run '/path start' near an NPC first");
goto update; return;
} }
std::pair<BaseNPC*, std::vector<BaseNPC*>>* entry = &TableData::RunningNPCPaths[plr->iID]; std::pair<BaseNPC*, std::vector<BaseNPC*>>* entry = &TableData::RunningNPCPaths[plr->iID];
@ -991,7 +1007,8 @@ static void pathCommand(std::string full, std::vector<std::string>& args, CNSock
BaseNPC* marker = new BaseNPC(npc->x, npc->y, npc->z, 0, plr->instanceID, 1386, NPCManager::nextId--); BaseNPC* marker = new BaseNPC(npc->x, npc->y, npc->z, 0, plr->instanceID, 1386, NPCManager::nextId--);
entry->second.push_back(marker); entry->second.push_back(marker);
Chat::sendServerMessage(sock, "[PATH] Added keyframe"); Chat::sendServerMessage(sock, "[PATH] Added keyframe");
goto update; updatePathMarkers(sock);
return;
} }
// /path here // /path here
@ -1002,14 +1019,14 @@ static void pathCommand(std::string full, std::vector<std::string>& args, CNSock
npc->disappearFromViewOf(sock); npc->disappearFromViewOf(sock);
npc->enterIntoViewOf(sock); npc->enterIntoViewOf(sock);
Chat::sendServerMessage(sock, "[PATH] Come here"); Chat::sendServerMessage(sock, "[PATH] Come here");
goto update; return;
} }
// /path undo // /path undo
if (args[1] == "undo") { if (args[1] == "undo") {
if (entry->second.size() == 1) { if (entry->second.size() == 1) {
Chat::sendServerMessage(sock, "[PATH] Nothing to undo"); Chat::sendServerMessage(sock, "[PATH] Nothing to undo");
goto update; return;
} }
BaseNPC* marker = entry->second.back(); BaseNPC* marker = entry->second.back();
@ -1018,7 +1035,8 @@ static void pathCommand(std::string full, std::vector<std::string>& args, CNSock
entry->second.pop_back(); // remove from the vector entry->second.pop_back(); // remove from the vector
Chat::sendServerMessage(sock, "[PATH] Undid last keyframe"); Chat::sendServerMessage(sock, "[PATH] Undid last keyframe");
goto update; updatePathMarkers(sock);
return;
} }
// /path test // /path test
@ -1030,7 +1048,7 @@ static void pathCommand(std::string full, std::vector<std::string>& args, CNSock
int speedArg = std::strtol(args[2].c_str(), &buf, 10); int speedArg = std::strtol(args[2].c_str(), &buf, 10);
if (*buf) { if (*buf) {
Chat::sendServerMessage(sock, "[PATH] Invalid speed " + args[2]); Chat::sendServerMessage(sock, "[PATH] Invalid speed " + args[2]);
goto update; return;
} }
speed = speedArg; speed = speedArg;
} }
@ -1057,7 +1075,7 @@ static void pathCommand(std::string full, std::vector<std::string>& args, CNSock
entry->second.pop_back(); // remove temp end point entry->second.pop_back(); // remove temp end point
Chat::sendServerMessage(sock, "[PATH] Testing NPC path"); Chat::sendServerMessage(sock, "[PATH] Testing NPC path");
goto update; return;
} }
// /path cancel // /path cancel
@ -1076,7 +1094,7 @@ static void pathCommand(std::string full, std::vector<std::string>& args, CNSock
// unmap // unmap
TableData::RunningNPCPaths.erase(plr->iID); TableData::RunningNPCPaths.erase(plr->iID);
Chat::sendServerMessage(sock, "[PATH] NPC " + std::to_string(npc->appearanceData.iNPC_ID) + " is no longer following you"); Chat::sendServerMessage(sock, "[PATH] NPC " + std::to_string(npc->appearanceData.iNPC_ID) + " is no longer following you");
goto update; return;
} }
// /path end // /path end
@ -1084,7 +1102,7 @@ static void pathCommand(std::string full, std::vector<std::string>& args, CNSock
if (args.size() < 2) { if (args.size() < 2) {
Chat::sendServerMessage(sock, "[PATH] Too few arguments"); Chat::sendServerMessage(sock, "[PATH] Too few arguments");
Chat::sendServerMessage(sock, "[PATH] Usage: /path end [speed] [a/r]"); Chat::sendServerMessage(sock, "[PATH] Usage: /path end [speed] [a/r]");
goto update; return;
} }
int speed = NPC_DEFAULT_SPEED; int speed = NPC_DEFAULT_SPEED;
@ -1094,7 +1112,7 @@ static void pathCommand(std::string full, std::vector<std::string>& args, CNSock
int speedArg = std::strtol(args[2].c_str(), &buf, 10); int speedArg = std::strtol(args[2].c_str(), &buf, 10);
if (*buf) { if (*buf) {
Chat::sendServerMessage(sock, "[PATH] Invalid speed " + args[2]); Chat::sendServerMessage(sock, "[PATH] Invalid speed " + args[2]);
goto update; return;
} }
speed = speedArg; speed = speedArg;
} }
@ -1162,18 +1180,11 @@ static void pathCommand(std::string full, std::vector<std::string>& args, CNSock
TableData::flush(); TableData::flush();
Chat::sendServerMessage(sock, "[PATH] Path saved to gruntwork"); Chat::sendServerMessage(sock, "[PATH] Path saved to gruntwork");
goto update; return;
} }
// /path ??? // /path ???
Chat::sendServerMessage(sock, "[PATH] Unknown argument '" + args[1] + "'"); Chat::sendServerMessage(sock, "[PATH] Unknown argument '" + args[1] + "'");
}
update:
if (TableData::RunningNPCPaths.find(plr->iID) != TableData::RunningNPCPaths.end()) {
for (BaseNPC* marker : TableData::RunningNPCPaths[plr->iID].second)
marker->enterIntoViewOf(sock);
}
} }
static void registerCommand(std::string cmd, int requiredLevel, CommandHandler handlr, std::string help) { static void registerCommand(std::string cmd, int requiredLevel, CommandHandler handlr, std::string help) {