From e546d3948c063dc5648938d574cd2e14da8f3e56 Mon Sep 17 00:00:00 2001 From: gsemaj Date: Sat, 1 May 2021 11:04:28 -0400 Subject: [PATCH] [refac] Stop using WarpLocation for coordinates and introduce a Vec3 --- src/CustomCommands.cpp | 8 ++++---- src/MobAI.cpp | 8 ++++---- src/TableData.cpp | 30 +++++++++++++++--------------- src/TableData.hpp | 2 +- src/Transport.cpp | 34 +++++++++++++++++----------------- src/Transport.hpp | 16 ++++++++++------ 6 files changed, 51 insertions(+), 47 deletions(-) diff --git a/src/CustomCommands.cpp b/src/CustomCommands.cpp index 2a4da3c..5083c3a 100644 --- a/src/CustomCommands.cpp +++ b/src/CustomCommands.cpp @@ -127,7 +127,7 @@ static void mssCommand(std::string full, std::vector& args, CNSocke } // get the route (if it doesn't exist yet, this will also make it) - std::vector* route = &TableData::RunningSkywayRoutes[routeNum]; + std::vector* route = &TableData::RunningSkywayRoutes[routeNum]; // mss add if (args[2] == "add") { @@ -158,7 +158,7 @@ static void mssCommand(std::string full, std::vector& args, CNSocke return; } - WarpLocation pulled = route->back(); + Vec3 pulled = route->back(); route->pop_back(); // remove point at top of stack Chat::sendServerMessage(sock, "[MSS] Removed point (" + std::to_string(pulled.x) + ", " + std::to_string(pulled.y) + ", " + std::to_string(pulled.z) + ") from route " + std::to_string(routeNum)); return; @@ -171,7 +171,7 @@ static void mssCommand(std::string full, std::vector& args, CNSocke return; } - WarpLocation pulled = route->back(); + Vec3 pulled = route->back(); PlayerManager::sendPlayerTo(sock, pulled.x, pulled.y, pulled.z); return; } @@ -190,7 +190,7 @@ static void mssCommand(std::string full, std::vector& args, CNSocke return; } - WarpLocation pulled = route->front(); + Vec3 pulled = route->front(); PlayerManager::sendPlayerTo(sock, pulled.x, pulled.y, pulled.z); Transport::testMssRoute(sock, route); return; diff --git a/src/MobAI.cpp b/src/MobAI.cpp index 5101a8b..d078c05 100644 --- a/src/MobAI.cpp +++ b/src/MobAI.cpp @@ -701,9 +701,9 @@ static void roamingStep(Mob *mob, time_t currTime) { if (mob->appearanceData.iConditionBitFlag & CSB_BIT_DN_MOVE_SPEED) speed /= 2; - std::queue queue; - WarpLocation from = { mob->x, mob->y, mob->z }; - WarpLocation to = { farX, farY, mob->z }; + std::queue queue; + Vec3 from = { mob->x, mob->y, mob->z }; + Vec3 to = { farX, farY, mob->z }; // add a route to the queue; to be processed in Transport::stepNPCPathing() Transport::lerp(&queue, from, to, speed); @@ -720,7 +720,7 @@ static void roamingStep(Mob *mob, time_t currTime) { continue; } - std::queue queue2; + std::queue queue2; Mob* followerMob = (Mob*)NPCManager::NPCs[mob->groupMember[i]]; from = { followerMob->x, followerMob->y, followerMob->z }; to = { farX + followerMob->offsetX, farY + followerMob->offsetY, followerMob->z }; diff --git a/src/TableData.cpp b/src/TableData.cpp index 0e4c785..36d956e 100644 --- a/src/TableData.cpp +++ b/src/TableData.cpp @@ -18,7 +18,7 @@ using namespace TableData; -std::map> TableData::RunningSkywayRoutes; +std::map> TableData::RunningSkywayRoutes; std::map TableData::RunningNPCRotations; std::map TableData::RunningNPCMapNumbers; std::map TableData::RunningMobs; @@ -40,14 +40,14 @@ public: static void constructPathSkyway(json& pathData) { // Interpolate json pathPoints = pathData["points"]; - std::queue points; + std::queue points; json::iterator _point = pathPoints.begin(); auto point = _point.value(); - WarpLocation last = { point["iX"] , point["iY"] , point["iZ"] }; // start pos + Vec3 last = { point["iX"] , point["iY"] , point["iZ"] }; // start pos // use some for loop trickery; start position should not be a point for (_point++; _point != pathPoints.end(); _point++) { point = _point.value(); - WarpLocation coords = { point["iX"] , point["iY"] , point["iZ"] }; + Vec3 coords = { point["iX"] , point["iY"] , point["iZ"] }; Transport::lerp(&points, last, coords, pathData["iMonkeySpeed"]); points.push(coords); // add keyframe to the queue last = coords; // update start pos @@ -58,16 +58,16 @@ static void constructPathSkyway(json& pathData) { static void constructPathNPC(json& pathData, int32_t id=0) { // Interpolate json pathPoints = pathData["points"]; - std::queue points; + std::queue points; json::iterator _point = pathPoints.begin(); auto point = _point.value(); - WarpLocation from = { point["iX"] , point["iY"] , point["iZ"] }; // point A coords + Vec3 from = { point["iX"] , point["iY"] , point["iZ"] }; // point A coords int stopTime = point["stop"]; for (_point++; _point != pathPoints.end(); _point++) { // loop through all point Bs point = _point.value(); for(int i = 0; i < stopTime + 1; i++) // repeat point if it's a stop points.push(from); // add point A to the queue - WarpLocation to = { point["iX"] , point["iY"] , point["iZ"] }; // point B coords + Vec3 to = { point["iX"] , point["iY"] , point["iZ"] }; // point B coords Transport::lerp(&points, from, to, pathData["iBaseSpeed"]); // lerp from A to B from = to; // update point A stopTime = point["stop"]; @@ -282,11 +282,11 @@ static void loadPaths(json& pathData, int32_t* nextId) { // slider circuit json pathDataSlider = pathData["slider"]; // lerp between keyframes - std::queue route; + std::queue route; // initial point json::iterator _point = pathDataSlider.begin(); // iterator auto point = _point.value(); - WarpLocation from = { point["iX"] , point["iY"] , point["iZ"] }; // point A coords + Vec3 from = { point["iX"] , point["iY"] , point["iZ"] }; // point A coords int stopTime = point["stop"] ? SLIDER_STOP_TICKS : 0; // arbitrary stop length // remaining points for (_point++; _point != pathDataSlider.end(); _point++) { // loop through all point Bs @@ -294,7 +294,7 @@ static void loadPaths(json& pathData, int32_t* nextId) { for (int i = 0; i < stopTime + 1; i++) { // repeat point if it's a stop route.push(from); // add point A to the queue } - WarpLocation to = { point["iX"] , point["iY"] , point["iZ"] }; // point B coords + Vec3 to = { point["iX"] , point["iY"] , point["iZ"] }; // point B coords // we may need to change this later; right now, the speed is cut before and after stops (no accel) float curve = 1; if (stopTime > 0) { // point A is a stop @@ -310,11 +310,11 @@ static void loadPaths(json& pathData, int32_t* nextId) { int passedDistance = 0; // initial point int pos = 0; - WarpLocation lastPoint = route.front(); + Vec3 lastPoint = route.front(); route.pop(); route.push(lastPoint); for (pos = 1; pos < route.size(); pos++) { - WarpLocation point = route.front(); + Vec3 point = route.front(); passedDistance += hypot(point.x - lastPoint.x, point.y - lastPoint.y); if (passedDistance >= SLIDER_GAP_SIZE) { // space them out uniformaly passedDistance -= SLIDER_GAP_SIZE; // step down @@ -685,11 +685,11 @@ static void loadGruntwork(json& gruntwork, int32_t* nextId) { auto skyway = gruntwork["skyway"]; for (auto _route = skyway.begin(); _route != skyway.end(); _route++) { auto route = _route.value(); - std::vector points; + std::vector points; for (auto _point = route["points"].begin(); _point != route["points"].end(); _point++) { auto point = _point.value(); - points.push_back(WarpLocation{point["x"], point["y"], point["z"]}); + points.push_back(Vec3{point["x"], point["y"], point["z"]}); } RunningSkywayRoutes[(int)route["iRouteID"]] = points; @@ -1006,7 +1006,7 @@ void TableData::flush() { route["iMonkeySpeed"] = 1500; std::cout << "serializing mss route " << (int)pair.first << std::endl; - for (WarpLocation& point : pair.second) { + for (Vec3& point : pair.second) { json tmp; tmp["x"] = point.x; diff --git a/src/TableData.hpp b/src/TableData.hpp index 71189d1..6c62fe7 100644 --- a/src/TableData.hpp +++ b/src/TableData.hpp @@ -12,7 +12,7 @@ const int MOB_GROUP_ID_OFFSET = 20000; typedef nlohmann::json json; namespace TableData { - extern std::map> RunningSkywayRoutes; + extern std::map> RunningSkywayRoutes; extern std::map RunningNPCRotations; extern std::map RunningNPCMapNumbers; extern std::map RunningMobs; diff --git a/src/Transport.cpp b/src/Transport.cpp index 240092f..3de4c42 100644 --- a/src/Transport.cpp +++ b/src/Transport.cpp @@ -13,9 +13,9 @@ using namespace Transport; std::map Transport::Routes; std::map Transport::Locations; -std::map> Transport::SkywayPaths; -std::unordered_map> Transport::SkywayQueues; -std::unordered_map> Transport::NPCQueues; +std::map> Transport::SkywayPaths; +std::unordered_map> Transport::SkywayQueues; +std::unordered_map> Transport::NPCQueues; static void transportRegisterLocationHandler(CNSocket* sock, CNPacketData* data) { auto transport = (sP_CL2FE_REQ_REGIST_TRANSPORTATION_LOCATION*)data->buf; @@ -131,7 +131,7 @@ static void transportWarpHandler(CNSocket* sock, CNPacketData* data) { plr->onMonkey = true; break; } else if (TableData::RunningSkywayRoutes.find(route.mssRouteNum) != TableData::RunningSkywayRoutes.end()) { - std::vector* _route = &TableData::RunningSkywayRoutes[route.mssRouteNum]; + std::vector* _route = &TableData::RunningSkywayRoutes[route.mssRouteNum]; Nanos::summonNano(sock, -1); testMssRoute(sock, _route); plr->onMonkey = true; @@ -169,13 +169,13 @@ static void transportWarpHandler(CNSocket* sock, CNPacketData* data) { PlayerManager::updatePlayerPosition(sock, target->x, target->y, target->z, INSTANCE_OVERWORLD, plr->angle); } -void Transport::testMssRoute(CNSocket *sock, std::vector* route) { +void Transport::testMssRoute(CNSocket *sock, std::vector* route) { int speed = 1500; // TODO: make this adjustable - std::queue path; - WarpLocation last = route->front(); // start pos + std::queue path; + Vec3 last = route->front(); // start pos for (int i = 1; i < route->size(); i++) { - WarpLocation coords = route->at(i); + Vec3 coords = route->at(i); Transport::lerp(&path, last, coords, speed); path.push(coords); // add keyframe to the queue last = coords; // update start pos @@ -191,10 +191,10 @@ void Transport::testMssRoute(CNSocket *sock, std::vector* route) { static void stepSkywaySystem() { // using an unordered map so we can remove finished players in one iteration - std::unordered_map>::iterator it = SkywayQueues.begin(); + std::unordered_map>::iterator it = SkywayQueues.begin(); while (it != SkywayQueues.end()) { - std::queue* queue = &it->second; + std::queue* queue = &it->second; if (PlayerManager::players.find(it->first) == PlayerManager::players.end()) { // pluck out dead socket + update iterator @@ -218,7 +218,7 @@ static void stepSkywaySystem() { it = SkywayQueues.erase(it); // remove player from tracking map + update iterator plr->onMonkey = false; } else { - WarpLocation point = queue->front(); // get point + Vec3 point = queue->front(); // get point queue->pop(); // remove point from front of queue INITSTRUCT(sP_FE2CL_PC_BROOMSTICK_MOVE, bmstk); @@ -240,10 +240,10 @@ static void stepSkywaySystem() { static void stepNPCPathing() { // all NPC pathing queues - std::unordered_map>::iterator it = NPCQueues.begin(); + std::unordered_map>::iterator it = NPCQueues.begin(); while (it != NPCQueues.end()) { - std::queue* queue = &it->second; + std::queue* queue = &it->second; BaseNPC* npc = nullptr; if (NPCManager::NPCs.find(it->first) != NPCManager::NPCs.end()) @@ -267,7 +267,7 @@ static void stepNPCPathing() { continue; } - WarpLocation point = queue->front(); // get point + Vec3 point = queue->front(); // get point queue->pop(); // remove point from front of queue // calculate displacement @@ -327,12 +327,12 @@ static void tickTransportationSystem(CNServer* serv, time_t currTime) { /* * Linearly interpolate between two points and insert the results into a queue. */ -void Transport::lerp(std::queue* queue, WarpLocation start, WarpLocation end, int gapSize, float curve) { +void Transport::lerp(std::queue* queue, Vec3 start, Vec3 end, int gapSize, float curve) { int dXY = hypot(end.x - start.x, end.y - start.y); // XY plane distance int distanceBetween = hypot(dXY, end.z - start.z); // total distance int lerps = distanceBetween / gapSize; // number of intermediate points to add for (int i = 1; i <= lerps; i++) { - WarpLocation lerp; + Vec3 lerp; // lerp math //float frac = i / (lerps + 1); float frac = powf(i, curve) / powf(lerps + 1, curve); @@ -342,7 +342,7 @@ void Transport::lerp(std::queue* queue, WarpLocation start, WarpLo queue->push(lerp); // add lerp'd point } } -void Transport::lerp(std::queue* queue, WarpLocation start, WarpLocation end, int gapSize) { +void Transport::lerp(std::queue* queue, Vec3 start, Vec3 end, int gapSize) { lerp(queue, start, end, gapSize, 1); } diff --git a/src/Transport.hpp b/src/Transport.hpp index 0a05959..251fe1a 100644 --- a/src/Transport.hpp +++ b/src/Transport.hpp @@ -8,6 +8,10 @@ const int SLIDER_SPEED = 1200; const int SLIDER_STOP_TICKS = 16; const int SLIDER_GAP_SIZE = 45000; +struct Vec3 { + int x, y, z; +}; + struct WarpLocation { int x, y, z, instanceID, isInstance, limitTaskID, npcID; }; @@ -23,14 +27,14 @@ struct TransportLocation { namespace Transport { extern std::map Routes; extern std::map Locations; - extern std::map> SkywayPaths; // predefined skyway paths with points - extern std::unordered_map> SkywayQueues; // player sockets with queued broomstick points - extern std::unordered_map> NPCQueues; // NPC ids with queued pathing points + extern std::map> SkywayPaths; // predefined skyway paths with points + extern std::unordered_map> SkywayQueues; // player sockets with queued broomstick points + extern std::unordered_map> NPCQueues; // NPC ids with queued pathing points void init(); - void testMssRoute(CNSocket *sock, std::vector* route); + void testMssRoute(CNSocket *sock, std::vector* route); - void lerp(std::queue*, WarpLocation, WarpLocation, int, float); - void lerp(std::queue*, WarpLocation, WarpLocation, int); + void lerp(std::queue*, Vec3, Vec3, int, float); + void lerp(std::queue*, Vec3, Vec3, int); }