Load NPC paths from JSON

This commit is contained in:
Gent 2020-09-24 10:14:37 -04:00
parent c33f218e56
commit 72c16587e0
4 changed files with 35 additions and 7 deletions

View File

@ -238,10 +238,16 @@ void TableData::loadPaths() {
// skyway paths // skyway paths
nlohmann::json pathDataSkyway = pathData["skyway"]; nlohmann::json pathDataSkyway = pathData["skyway"];
for (nlohmann::json::iterator skywayPath = pathDataSkyway.begin(); skywayPath != pathDataSkyway.end(); skywayPath++) { for (nlohmann::json::iterator skywayPath = pathDataSkyway.begin(); skywayPath != pathDataSkyway.end(); skywayPath++) {
constructPath(skywayPath); constructPathSkyway(skywayPath);
} }
std::cout << "[INFO] Loaded " << TransportManager::SkywayPaths.size() << " skyway paths" << std::endl; std::cout << "[INFO] Loaded " << TransportManager::SkywayPaths.size() << " skyway paths" << std::endl;
// npc paths
nlohmann::json pathDataNPC = pathData["npc"];
for (nlohmann::json::iterator npcPath = pathDataNPC.begin(); npcPath != pathDataNPC.end(); npcPath++) {
constructPathNPC(npcPath);
}
std::cout << "[INFO] Loaded " << TransportManager::NPCQueues.size() << " NPC paths" << std::endl;
} }
catch (const std::exception& err) { catch (const std::exception& err) {
std::cerr << "[WARN] Malformed paths.json file! Reason:" << err.what() << std::endl; std::cerr << "[WARN] Malformed paths.json file! Reason:" << err.what() << std::endl;
@ -249,9 +255,9 @@ void TableData::loadPaths() {
} }
/* /*
* Create a full and properly-paced Skyway System path by interpolating between keyframes. * Create a full and properly-paced path by interpolating between keyframes.
*/ */
void TableData::constructPath(nlohmann::json::iterator _pathData) { void TableData::constructPathSkyway(nlohmann::json::iterator _pathData) {
auto pathData = _pathData.value(); auto pathData = _pathData.value();
// Interpolate // Interpolate
nlohmann::json pathPoints = pathData["points"]; nlohmann::json pathPoints = pathData["points"];
@ -269,3 +275,24 @@ void TableData::constructPath(nlohmann::json::iterator _pathData) {
} }
TransportManager::SkywayPaths[pathData["iRouteID"]] = points; TransportManager::SkywayPaths[pathData["iRouteID"]] = points;
} }
void TableData::constructPathNPC(nlohmann::json::iterator _pathData) {
auto pathData = _pathData.value();
// Interpolate
nlohmann::json pathPoints = pathData["points"];
std::queue<WarpLocation> points;
nlohmann::json::iterator _point = pathPoints.begin();
auto point = _point.value();
WarpLocation 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
TransportManager::lerp(&points, from, to, pathData["iBaseSpeed"]); // lerp from A to B
from = to; // update point A
stopTime = point["stop"];
}
TransportManager::NPCQueues[pathData["iNPCID"]] = points;
}

View File

@ -9,5 +9,6 @@ namespace TableData {
int getItemType(int); int getItemType(int);
void loadPaths(); void loadPaths();
void constructPath(nlohmann::json::iterator); void constructPathSkyway(nlohmann::json::iterator);
void constructPathNPC(nlohmann::json::iterator);
} }

View File

@ -304,7 +304,7 @@ void TransportManager::stepNPCPathing() {
move.iToX = point.x; move.iToX = point.x;
move.iToY = point.y; move.iToY = point.y;
move.iToZ = point.z; move.iToZ = point.z;
move.iSpeed = 600; // TODO: figure out a way to make this variable move.iSpeed = distanceBetween;
// send packet to players in view // send packet to players in view
for (Chunk* chunk : chunks) { for (Chunk* chunk : chunks) {

2
tdata

@ -1 +1 @@
Subproject commit dc47c07f115ac279a1301670783cc27b08246fb3 Subproject commit 2d16da519452c934b6fc2fa61adbcb25f855e95f