Move lerp to td init, add variable gap size

This commit is contained in:
Gent 2020-09-17 05:04:00 -04:00
parent 6a78a301c9
commit a5ffe26c44
4 changed files with 35 additions and 36 deletions

View File

@ -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<WarpLocation> 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<WarpLocation> 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;
}

View File

@ -1,9 +1,12 @@
#pragma once
#include <map>
#include "contrib/JSON.hpp"
namespace TableData {
void init();
void cleanup();
int getItemType(int);
void constructPath(nlohmann::json::iterator);
}

View File

@ -7,7 +7,7 @@
std::map<int32_t, TransportRoute> TransportManager::Routes;
std::map<int32_t, TransportLocation> TransportManager::Locations;
std::map<int32_t, std::vector<WarpLocation>> TransportManager::SkywayPaths;
std::map<int32_t, std::queue<WarpLocation>> TransportManager::SkywayPaths;
std::unordered_map<CNSocket*, std::queue<WarpLocation>> 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<WarpLocation>* points = nullptr;
std::queue<WarpLocation>* 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<int, int>(0, 0);
if (points == nullptr) // no skyway path set
return;
// Interpolate
std::queue<WarpLocation> queue;
WarpLocation last = { plr->x, plr->y, plr->z }; // start pos
for (std::vector<WarpLocation>::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) {

View File

@ -5,8 +5,6 @@
#include <unordered_map>
#define LERP_GAP 3000
struct WarpLocation;
struct TransportRoute {
@ -20,7 +18,7 @@ struct TransportLocation {
namespace TransportManager {
extern std::map<int32_t, TransportRoute> Routes;
extern std::map<int32_t, TransportLocation> Locations;
extern std::map<int32_t, std::vector<WarpLocation>> SkywayPaths;
extern std::map<int32_t, std::queue<WarpLocation>> SkywayPaths;
extern std::unordered_map<CNSocket*, std::queue<WarpLocation>> SkywayQueue;
void init();