Merge pull request #117 from gsemaj/slider

Load sliders from paths.json
This commit is contained in:
dongresource 2020-09-27 20:03:41 +02:00 committed by GitHub
commit 97c2c532f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 19 deletions

View File

@ -40,7 +40,7 @@ void TableData::init() {
std::cerr << "[WARN] Malformed NPCs.json file! Reason:" << err.what() << std::endl; std::cerr << "[WARN] Malformed NPCs.json file! Reason:" << err.what() << std::endl;
} }
loadPaths(); // load paths loadPaths(&nextId); // load paths
// load everything else from xdttable // load everything else from xdttable
std::cout << "[INFO] Parsing xdt.json..." << std::endl; std::cout << "[INFO] Parsing xdt.json..." << std::endl;
@ -222,7 +222,7 @@ int TableData::getItemType(int itemSet) {
/* /*
* Load paths from paths JSON. * Load paths from paths JSON.
*/ */
void TableData::loadPaths() { void TableData::loadPaths(int* nextId) {
try { try {
std::ifstream inFile(settings::PATHJSON); std::ifstream inFile(settings::PATHJSON);
nlohmann::json pathData; nlohmann::json pathData;
@ -237,6 +237,23 @@ void TableData::loadPaths() {
} }
std::cout << "[INFO] Loaded " << TransportManager::SkywayPaths.size() << " skyway paths" << std::endl; std::cout << "[INFO] Loaded " << TransportManager::SkywayPaths.size() << " skyway paths" << std::endl;
// slider circuit
int sliders = 0;
nlohmann::json pathDataSlider = pathData["slider"];
for (nlohmann::json::iterator _sliderPoint = pathDataSlider.begin(); _sliderPoint != pathDataSlider.end(); _sliderPoint++) {
auto sliderPoint = _sliderPoint.value();
if (sliderPoint["stop"] && sliders % 2 == 0) { // check if this point in the circuit is a stop
// spawn a slider
std::cout << "bus ID was " << *nextId << std::endl;
BaseNPC* slider = new BaseNPC(sliderPoint["iX"], sliderPoint["iY"], sliderPoint["iZ"], 1, (*nextId)++, NPC_BUS);
NPCManager::NPCs[slider->appearanceData.iNPC_ID] = slider;
NPCManager::updateNPCPosition(slider->appearanceData.iNPC_ID, slider->appearanceData.iX, slider->appearanceData.iY, slider->appearanceData.iZ);
// set slider path to a rotation of the circuit
constructPathSlider(pathDataSlider, 0, slider->appearanceData.iNPC_ID);
sliders++;
}
}
// npc paths // npc paths
nlohmann::json pathDataNPC = pathData["npc"]; nlohmann::json pathDataNPC = pathData["npc"];
for (nlohmann::json::iterator npcPath = pathDataNPC.begin(); npcPath != pathDataNPC.end(); npcPath++) { for (nlohmann::json::iterator npcPath = pathDataNPC.begin(); npcPath != pathDataNPC.end(); npcPath++) {
@ -271,6 +288,33 @@ void TableData::constructPathSkyway(nlohmann::json::iterator _pathData) {
TransportManager::SkywayPaths[pathData["iRouteID"]] = points; TransportManager::SkywayPaths[pathData["iRouteID"]] = points;
} }
void TableData::constructPathSlider(nlohmann::json points, int rotations, int sliderID) {
std::queue<WarpLocation> route;
std::rotate(points.begin(), points.begin() + rotations, points.end()); // rotate points
nlohmann::json::iterator _point = points.begin(); // iterator
auto point = _point.value();
WarpLocation from = { point["iX"] , point["iY"] , point["iZ"] }; // point A coords
int stopTime = point["stop"] ? SLIDER_STOP_TICKS : 0; // arbitrary stop length
for (_point++; _point != points.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
route.push(from); // add point A to the queue
WarpLocation to = { point["iX"] , point["iY"] , point["iZ"] }; // point B coords
float curve = 1;
if (stopTime > 0) { // point A is a stop
curve = 2.0f;
} else if (point["stop"]) { // point B is a stop
curve = 0.35f;
}
TransportManager::lerp(&route, from, to, SLIDER_SPEED, curve); // lerp from A to B (arbitrary speed)
from = to; // update point A
stopTime = point["stop"] ? SLIDER_STOP_TICKS : 0;
}
std::rotate(points.rbegin(), points.rbegin() + rotations, points.rend()); // undo rotation
TransportManager::NPCQueues[sliderID] = route;
}
void TableData::constructPathNPC(nlohmann::json::iterator _pathData) { void TableData::constructPathNPC(nlohmann::json::iterator _pathData) {
auto pathData = _pathData.value(); auto pathData = _pathData.value();
// Interpolate // Interpolate

View File

@ -8,7 +8,8 @@ namespace TableData {
void cleanup(); void cleanup();
int getItemType(int); int getItemType(int);
void loadPaths(); void loadPaths(int*);
void constructPathSkyway(nlohmann::json::iterator); void constructPathSkyway(nlohmann::json::iterator);
void constructPathSlider(nlohmann::json, int, int);
void constructPathNPC(nlohmann::json::iterator); void constructPathNPC(nlohmann::json::iterator);
} }

View File

@ -17,18 +17,6 @@ void TransportManager::init() {
REGISTER_SHARD_PACKET(P_CL2FE_REQ_REGIST_TRANSPORTATION_LOCATION, transportRegisterLocationHandler); REGISTER_SHARD_PACKET(P_CL2FE_REQ_REGIST_TRANSPORTATION_LOCATION, transportRegisterLocationHandler);
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_WARP_USE_TRANSPORTATION, transportWarpHandler); REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_WARP_USE_TRANSPORTATION, transportWarpHandler);
BaseNPC* bus = new BaseNPC(220447, 162431, -3650, 1, NPCManager::nextId++, NPC_BUS);
NPCManager::NPCs[bus->appearanceData.iNPC_ID] = bus;
ChunkManager::addNPC(bus->appearanceData.iX, bus->appearanceData.iY, bus->appearanceData.iNPC_ID);
std::queue<WarpLocation> busPoints;
WarpLocation start = { bus->appearanceData.iX, bus->appearanceData.iY, -3650 };
WarpLocation end = { 220441, 188102, -3653 };
busPoints.push(start);
TransportManager::lerp(&busPoints, start, end, 1000);
busPoints.push(end);
TransportManager::lerp(&busPoints, end, start, 1000);
NPCQueues[bus->appearanceData.iNPC_ID] = busPoints;
} }
void TransportManager::transportRegisterLocationHandler(CNSocket* sock, CNPacketData* data) { void TransportManager::transportRegisterLocationHandler(CNSocket* sock, CNPacketData* data) {
@ -323,17 +311,21 @@ void TransportManager::stepNPCPathing() {
/* /*
* Linearly interpolate between two points and insert the results into a queue. * Linearly interpolate between two points and insert the results into a queue.
*/ */
void TransportManager::lerp(std::queue<WarpLocation>* queue, WarpLocation start, WarpLocation end, int gapSize) { void TransportManager::lerp(std::queue<WarpLocation>* queue, WarpLocation start, WarpLocation end, int gapSize, float curve) {
int dXY = hypot(end.x - start.x, end.y - start.y); // XY plane distance 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 distanceBetween = hypot(dXY, end.z - start.z); // total distance
int lerps = distanceBetween / gapSize; // integer division to ensure a whole number of in-between points int lerps = distanceBetween / gapSize; // number of intermediate points to add
for (int i = 0; i < lerps; i++) { for (int i = 1; i <= lerps; i++) {
WarpLocation lerp; WarpLocation lerp;
// lerp math // lerp math
float frac = (i + 1) * 1.0f / (lerps + 1); //float frac = i / (lerps + 1);
float frac = powf(i, curve) / powf(lerps + 1, curve);
lerp.x = (start.x * (1.0f - frac)) + (end.x * frac); lerp.x = (start.x * (1.0f - frac)) + (end.x * frac);
lerp.y = (start.y * (1.0f - frac)) + (end.y * frac); lerp.y = (start.y * (1.0f - frac)) + (end.y * frac);
lerp.z = (start.z * (1.0f - frac)) + (end.z * frac); lerp.z = (start.z * (1.0f - frac)) + (end.z * frac);
queue->push(lerp); // add lerp'd point queue->push(lerp); // add lerp'd point
} }
} }
void TransportManager::lerp(std::queue<WarpLocation>* queue, WarpLocation start, WarpLocation end, int gapSize) {
lerp(queue, start, end, gapSize, 1);
}

View File

@ -5,6 +5,9 @@
#include <unordered_map> #include <unordered_map>
const int SLIDER_SPEED = 1200;
const int SLIDER_STOP_TICKS = 8;
struct WarpLocation; struct WarpLocation;
struct TransportRoute { struct TransportRoute {
@ -31,5 +34,6 @@ namespace TransportManager {
void stepNPCPathing(); void stepNPCPathing();
void stepSkywaySystem(); void stepSkywaySystem();
void lerp(std::queue<WarpLocation>*, WarpLocation, WarpLocation, int, float);
void lerp(std::queue<WarpLocation>*, WarpLocation, WarpLocation, int); void lerp(std::queue<WarpLocation>*, WarpLocation, WarpLocation, int);
} }