Add /path command

This commit is contained in:
gsemaj 2021-05-04 19:57:06 -04:00 committed by Gent Semaj
parent 37b1d11948
commit c7e2e66a51
4 changed files with 230 additions and 0 deletions

View File

@ -942,6 +942,212 @@ static void unbanCommand(std::string full, std::vector<std::string>& args, CNSoc
}
}
static void pathCommand(std::string full, std::vector<std::string>& args, CNSocket* sock) {
Player* plr = PlayerManager::getPlayer(sock);
if (args.size() < 2) {
Chat::sendServerMessage(sock, "[PATH] Too few arguments");
Chat::sendServerMessage(sock, "[PATH] Usage: /path <start/kf/undo/test/cancel/end>");
goto update;
}
// /path start
if (args[1] == "start") {
// make sure the player doesn't have a follower
if (TableData::RunningNPCPaths.find(plr->iID) != TableData::RunningNPCPaths.end()) {
Chat::sendServerMessage(sock, "[PATH] An NPC is already following you");
Chat::sendServerMessage(sock, "[PATH] Run '/path end <endType>' first, if you're done");
goto update;
}
// find nearest NPC
BaseNPC* npc = NPCManager::getNearestNPC(&plr->viewableChunks, plr->x, plr->y, plr->z);
if (npc == nullptr) {
Chat::sendServerMessage(sock, "[PATH] No NPCs found nearby");
goto update;
}
// add first point at NPC's current location
std::vector<BaseNPC*> pathPoints;
BaseNPC* marker = new BaseNPC(npc->x, npc->y, npc->z, 0, plr->instanceID, 1386, NPCManager::nextId--);
pathPoints.push_back(marker);
// map from player
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");
goto update;
} else {
// make sure the player has a follower
if (TableData::RunningNPCPaths.find(plr->iID) == TableData::RunningNPCPaths.end()) {
Chat::sendServerMessage(sock, "[PATH] No NPC is currently following you");
Chat::sendServerMessage(sock, "[PATH] Run '/path start' near an NPC first");
goto update;
}
std::pair<BaseNPC*, std::vector<BaseNPC*>>* entry = &TableData::RunningNPCPaths[plr->iID];
BaseNPC* npc = entry->first;
// /path kf
if (args[1] == "kf") {
BaseNPC* marker = new BaseNPC(npc->x, npc->y, npc->z, 0, plr->instanceID, 1386, NPCManager::nextId--);
entry->second.push_back(marker);
Chat::sendServerMessage(sock, "[PATH] Added keyframe");
goto update;
}
// /path undo
if (args[1] == "undo") {
if (entry->second.size() == 1) {
Chat::sendServerMessage(sock, "[PATH] Nothing to undo");
goto update;
}
BaseNPC* marker = entry->second.back();
marker->disappearFromViewOf(sock); // vanish
delete marker; // destroy
entry->second.pop_back(); // remove from the vector
Chat::sendServerMessage(sock, "[PATH] Undid last keyframe");
goto update;
}
// /path test
if (args[1] == "test") {
int speed = NPC_DEFAULT_SPEED;
if (args.size() > 2) { // speed specified
char* buf;
int speedArg = std::strtol(args[2].c_str(), &buf, 10);
if (*buf) {
Chat::sendServerMessage(sock, "[PATH] Invalid speed " + args[2]);
goto update;
}
speed = speedArg;
}
// return NPC to home
Transport::NPCQueues.erase(npc->appearanceData.iNPC_ID); // delete transport queue
BaseNPC* home = entry->second[0];
NPCManager::updateNPCPosition(npc->appearanceData.iNPC_ID, home->x, home->y, home->z, npc->instanceID, 0);
npc->disappearFromViewOf(sock);
npc->enterIntoViewOf(sock);
// do lerping magic
entry->second.push_back(home); // temporary end point for loop completion
std::queue<Vec3> keyframes;
auto _point = entry->second.begin();
Vec3 from = { (*_point)->x, (*_point)->y, (*_point)->z }; // point A coords
for (_point++; _point != entry->second.end(); _point++) { // loop through all point Bs
Vec3 to = { (*_point)->x, (*_point)->y, (*_point)->z }; // point B coords
// add point A to the queue
keyframes.push(from);
Transport::lerp(&keyframes, from, to, speed); // lerp from A to B
from = to; // update point A
}
Transport::NPCQueues[npc->appearanceData.iNPC_ID] = keyframes;
entry->second.pop_back(); // remove temp end point
Chat::sendServerMessage(sock, "[PATH] Testing NPC path");
goto update;
}
// /path cancel
if (args[1] == "cancel") {
// return NPC to home
Transport::NPCQueues.erase(npc->appearanceData.iNPC_ID); // delete transport queue
BaseNPC* home = entry->second[0];
NPCManager::updateNPCPosition(npc->appearanceData.iNPC_ID, home->x, home->y, home->z, npc->instanceID, 0);
npc->disappearFromViewOf(sock);
npc->enterIntoViewOf(sock);
// deallocate markers
for (BaseNPC* marker : entry->second) {
marker->disappearFromViewOf(sock); // poof
delete marker; // destroy
}
// unmap
TableData::RunningNPCPaths.erase(plr->iID);
Chat::sendServerMessage(sock, "[PATH] NPC " + std::to_string(npc->appearanceData.iNPC_ID) + " is no longer following you");
goto update;
}
// /path end
if (args[1] == "end") {
if (args.size() < 2) {
Chat::sendServerMessage(sock, "[PATH] Too few arguments");
Chat::sendServerMessage(sock, "[PATH] Usage: /path end [speed] [a/r]");
goto update;
}
int speed = NPC_DEFAULT_SPEED;
bool relative = false;
if (args.size() > 2) { // speed specified
char* buf;
int speedArg = std::strtol(args[2].c_str(), &buf, 10);
if (*buf) {
Chat::sendServerMessage(sock, "[PATH] Invalid speed " + args[2]);
goto update;
}
speed = speedArg;
}
if (args.size() > 3 && args[3] == "r") { // relativity specified
relative = true;
}
// return NPC to home
Transport::NPCQueues.erase(npc->appearanceData.iNPC_ID); // delete transport queue
BaseNPC* home = entry->second[0];
NPCManager::updateNPCPosition(npc->appearanceData.iNPC_ID, home->x, home->y, home->z, npc->instanceID, 0);
npc->disappearFromViewOf(sock);
npc->enterIntoViewOf(sock);
// save to gruntwork
std::vector<Vec3> finalPoints;
for (BaseNPC* marker : entry->second) {
Vec3 coords = { marker->x, marker->y, marker->z };
if (relative) {
coords.x -= home->x;
coords.y -= home->y;
coords.z -= home->z;
}
finalPoints.push_back(coords);
}
// end point to complete the circuit
if (relative)
finalPoints.push_back({ 0, 0, 0 });
else
finalPoints.push_back({ home->x, home->y, home->z });
NPCPath finishedPath;
finishedPath.escortTaskID = -1;
finishedPath.isRelative = relative;
finishedPath.speed = speed;
finishedPath.points = finalPoints;
finishedPath.targetIDs.push_back(npc->appearanceData.iNPC_ID);
TableData::FinishedNPCPaths.push_back(finishedPath);
// deallocate markers
for (BaseNPC* marker : entry->second) {
marker->disappearFromViewOf(sock); // poof
delete marker; // destroy
}
// unmap
TableData::RunningNPCPaths.erase(plr->iID);
Chat::sendServerMessage(sock, "[PATH] Path saved to gruntwork");
Chat::sendServerMessage(sock, "[PATH] NPC " + std::to_string(npc->appearanceData.iNPC_ID) + " is no longer following you");
goto update;
}
// /path ???
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) {
commands[cmd] = ChatCommand(requiredLevel, handlr, help);
}
@ -980,4 +1186,5 @@ void CustomCommands::init() {
registerCommand("registerall", 50, registerallCommand, "register all SCAMPER and MSS destinations");
registerCommand("unregisterall", 50, unregisterallCommand, "clear all SCAMPER and MSS destinations");
registerCommand("redeem", 100, redeemCommand, "redeem a code item");
registerCommand("path", 30, pathCommand, "edit NPC paths");
}

View File

@ -1,5 +1,6 @@
#include "PlayerMovement.hpp"
#include "PlayerManager.hpp"
#include "TableData.hpp"
#include "core/Core.hpp"
static void movePlayer(CNSocket* sock, CNPacketData* data) {
@ -28,6 +29,24 @@ static void movePlayer(CNSocket* sock, CNPacketData* data) {
moveResponse.iSvrTime = tm;
PlayerManager::sendToViewable(sock, moveResponse, P_FE2CL_PC_MOVE);
// [gruntwork] check if player has a follower and move it
if (TableData::RunningNPCPaths.find(plr->iID) != TableData::RunningNPCPaths.end()) {
BaseNPC* follower = TableData::RunningNPCPaths[plr->iID].first;
Transport::NPCQueues.erase(follower->appearanceData.iNPC_ID); // erase existing points
std::queue<Vec3> queue;
Vec3 from = { follower->x, follower->y, follower->z };
float drag = 0.95f; // this ensures that they don't bump into the player
Vec3 to = {
(int)(follower->x + (moveData->iX - follower->x) * drag),
(int)(follower->y + (moveData->iY - follower->y) * drag),
(int)(follower->z + (moveData->iZ - follower->z) * drag)
};
// add a route to the queue; to be processed in Transport::stepNPCPathing()
Transport::lerp(&queue, from, to, NPC_DEFAULT_SPEED * 1.5); // little faster than typical
Transport::NPCQueues[follower->appearanceData.iNPC_ID] = queue;
}
}
static void stopPlayer(CNSocket* sock, CNPacketData* data) {

View File

@ -21,6 +21,8 @@ using namespace TableData;
std::map<int32_t, std::vector<Vec3>> TableData::RunningSkywayRoutes;
std::map<int32_t, int> TableData::RunningNPCRotations;
std::map<int32_t, int> TableData::RunningNPCMapNumbers;
std::unordered_map<int32_t, std::pair<BaseNPC*, std::vector<BaseNPC*>>> TableData::RunningNPCPaths;
std::vector<NPCPath> TableData::FinishedNPCPaths;
std::map<int32_t, BaseNPC*> TableData::RunningMobs;
std::map<int32_t, BaseNPC*> TableData::RunningGroups;
std::map<int32_t, BaseNPC*> TableData::RunningEggs;

View File

@ -16,6 +16,8 @@ namespace TableData {
extern std::map<int32_t, std::vector<Vec3>> RunningSkywayRoutes;
extern std::map<int32_t, int> RunningNPCRotations;
extern std::map<int32_t, int> RunningNPCMapNumbers;
extern std::unordered_map<int32_t, std::pair<BaseNPC*, std::vector<BaseNPC*>>> RunningNPCPaths; // player ID -> following NPC
extern std::vector<NPCPath> FinishedNPCPaths; // NPC ID -> path
extern std::map<int32_t, BaseNPC*> RunningMobs;
extern std::map<int32_t, BaseNPC*> RunningGroups;
extern std::map<int32_t, BaseNPC*> RunningEggs;