diff --git a/src/TableData.cpp b/src/TableData.cpp index 6c9f34c..30b43df 100644 --- a/src/TableData.cpp +++ b/src/TableData.cpp @@ -52,11 +52,7 @@ void TableData::init() { nlohmann::json pathDataSkyway = pathData["skyway"]; for (nlohmann::json::iterator skywayPath = pathDataSkyway.begin(); skywayPath != pathDataSkyway.end(); skywayPath++) { - std::vector points; - nlohmann::json pathPoints = skywayPath.value()["points"]; - for (nlohmann::json::iterator point = pathPoints.begin(); point != pathPoints.end(); point++) - points.push_back({point.value()["iX"], point.value()["iY"], point.value()["iZ"] }); - TransportManager::SkywayPaths[skywayPath.value()["iRouteID"]] = points; + constructPath(skywayPath); } std::cout << "[INFO] Loaded " << TransportManager::SkywayPaths.size() << " skyway paths" << std::endl; @@ -235,3 +231,29 @@ int TableData::getItemType(int itemSet) { } return overriden; } + +void TableData::constructPath(nlohmann::json::iterator pathData) { + // Interpolate + nlohmann::json pathPoints = pathData.value()["points"]; + std::queue points; + nlohmann::json::iterator point = pathPoints.begin(); + WarpLocation last = { point.value()["iX"] , point.value()["iY"] , point.value()["iZ"] }; // start pos + // use some for loop trickery + for (point++; point != pathPoints.end(); point++) { + WarpLocation coords = { point.value()["iX"] , point.value()["iY"] , point.value()["iZ"] }; + // avoiding pow here + int distanceBetween = sqrt((last.x - coords.x) * (last.x - coords.x) + (last.y - coords.y) * (last.y - coords.y) + (last.z - coords.z) * (last.z - coords.z)); + int lerps = distanceBetween / (int)pathData.value()["iMaxGapSize"]; // integer division to ensure a whole number + for (int i = 0; i < lerps; i++) { + WarpLocation lerp; + float frac = (i + 1) * 1.0f / (lerps + 1); + lerp.x = (last.x * (1.0f - frac)) + (coords.x * frac); + lerp.y = (last.y * (1.0f - frac)) + (coords.y * frac); + lerp.z = (last.z * (1.0f - frac)) + (coords.z * frac); + points.push(lerp); // add lerp'd point to the queue + } + points.push(coords); + last = coords; // update start pos + } + TransportManager::SkywayPaths[pathData.value()["iRouteID"]] = points; +} diff --git a/src/TableData.hpp b/src/TableData.hpp index bf89c68..065612b 100644 --- a/src/TableData.hpp +++ b/src/TableData.hpp @@ -1,9 +1,12 @@ #pragma once #include +#include "contrib/JSON.hpp" + namespace TableData { void init(); void cleanup(); int getItemType(int); + void constructPath(nlohmann::json::iterator); } diff --git a/src/TransportManager.cpp b/src/TransportManager.cpp index 31db81d..16fb6d8 100644 --- a/src/TransportManager.cpp +++ b/src/TransportManager.cpp @@ -7,7 +7,7 @@ std::map TransportManager::Routes; std::map TransportManager::Locations; -std::map> TransportManager::SkywayPaths; +std::map> TransportManager::SkywayPaths; std::unordered_map> TransportManager::SkywayQueue; void TransportManager::init() { @@ -118,7 +118,7 @@ void TransportManager::transportWarpHandler(CNSocket* sock, CNPacketData* data) TransportLocation target; PlayerView& plrv = PlayerManager::players[sock]; - std::vector* points = nullptr; + std::queue* points = nullptr; switch (route.type) { case 1: // S.C.A.M.P.E.R. @@ -153,37 +153,13 @@ void TransportManager::transportWarpHandler(CNSocket* sock, CNPacketData* data) resp.iZ = plr->z; sock->sendPacket((void*)&resp, P_FE2CL_REP_PC_WARP_USE_TRANSPORTATION_SUCC, sizeof(sP_FE2CL_REP_PC_WARP_USE_TRANSPORTATION_SUCC)); - /* - * Not strictly necessary since there isn't a valid SCAMPER that puts you in the - * same map tile you were already in, but we might as well force an NPC reload. - */ PlayerView& plrv = PlayerManager::players[sock]; PlayerManager::removePlayerFromChunks(plrv.currentChunks, sock); plrv.currentChunks.clear(); plrv.chunkPos = std::make_pair(0, 0); - if (points == nullptr) // no skyway path set - return; - - // Interpolate - std::queue queue; - WarpLocation last = { plr->x, plr->y, plr->z }; // start pos - for (std::vector::iterator point = points->begin(); point != points->end(); point++) { - // avoiding pow here - int distanceBetween = sqrt((last.x - point->x) * (last.x - point->x) + (last.y - point->y) * (last.y - point->y) + (last.z - point->z) * (last.z - point->z)); - int lerps = distanceBetween / (int)LERP_GAP; // integer division to ensure a whole number - for (int i = 0; i < lerps; i++) { - WarpLocation lerp; - float frac = (i + 1) * 1.0f / (lerps + 1); - lerp.x = (last.x * (1.0f - frac)) + (point->x * frac); - lerp.y = (last.y * (1.0f - frac)) + (point->y * frac); - lerp.z = (last.z * (1.0f - frac)) + (point->z * frac); - queue.push(lerp); // add lerp'd point to the queue - } - queue.push(*point); - last = { point->x, point->y, point->z }; // update start pos - } - SkywayQueue[sock] = queue; + if (points != nullptr) // check if skyway path set + SkywayQueue[sock] = *points; } void TransportManager::tickSkywaySystem(CNServer* serv, time_t currTime) { diff --git a/src/TransportManager.hpp b/src/TransportManager.hpp index ff30477..7ca3a25 100644 --- a/src/TransportManager.hpp +++ b/src/TransportManager.hpp @@ -5,8 +5,6 @@ #include -#define LERP_GAP 3000 - struct WarpLocation; struct TransportRoute { @@ -20,7 +18,7 @@ struct TransportLocation { namespace TransportManager { extern std::map Routes; extern std::map Locations; - extern std::map> SkywayPaths; + extern std::map> SkywayPaths; extern std::unordered_map> SkywayQueue; void init();