[refac] Stop using WarpLocation for coordinates and introduce a Vec3

This commit is contained in:
gsemaj 2021-05-01 11:04:28 -04:00 committed by Gent Semaj
parent a0e758f5b7
commit e546d3948c
6 changed files with 51 additions and 47 deletions

View File

@ -127,7 +127,7 @@ static void mssCommand(std::string full, std::vector<std::string>& args, CNSocke
} }
// get the route (if it doesn't exist yet, this will also make it) // get the route (if it doesn't exist yet, this will also make it)
std::vector<WarpLocation>* route = &TableData::RunningSkywayRoutes[routeNum]; std::vector<Vec3>* route = &TableData::RunningSkywayRoutes[routeNum];
// mss <route> add <height> // mss <route> add <height>
if (args[2] == "add") { if (args[2] == "add") {
@ -158,7 +158,7 @@ static void mssCommand(std::string full, std::vector<std::string>& args, CNSocke
return; return;
} }
WarpLocation pulled = route->back(); Vec3 pulled = route->back();
route->pop_back(); // remove point at top of stack 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)); 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; return;
@ -171,7 +171,7 @@ static void mssCommand(std::string full, std::vector<std::string>& args, CNSocke
return; return;
} }
WarpLocation pulled = route->back(); Vec3 pulled = route->back();
PlayerManager::sendPlayerTo(sock, pulled.x, pulled.y, pulled.z); PlayerManager::sendPlayerTo(sock, pulled.x, pulled.y, pulled.z);
return; return;
} }
@ -190,7 +190,7 @@ static void mssCommand(std::string full, std::vector<std::string>& args, CNSocke
return; return;
} }
WarpLocation pulled = route->front(); Vec3 pulled = route->front();
PlayerManager::sendPlayerTo(sock, pulled.x, pulled.y, pulled.z); PlayerManager::sendPlayerTo(sock, pulled.x, pulled.y, pulled.z);
Transport::testMssRoute(sock, route); Transport::testMssRoute(sock, route);
return; return;

View File

@ -701,9 +701,9 @@ static void roamingStep(Mob *mob, time_t currTime) {
if (mob->appearanceData.iConditionBitFlag & CSB_BIT_DN_MOVE_SPEED) if (mob->appearanceData.iConditionBitFlag & CSB_BIT_DN_MOVE_SPEED)
speed /= 2; speed /= 2;
std::queue<WarpLocation> queue; std::queue<Vec3> queue;
WarpLocation from = { mob->x, mob->y, mob->z }; Vec3 from = { mob->x, mob->y, mob->z };
WarpLocation to = { farX, farY, mob->z }; Vec3 to = { farX, farY, mob->z };
// add a route to the queue; to be processed in Transport::stepNPCPathing() // add a route to the queue; to be processed in Transport::stepNPCPathing()
Transport::lerp(&queue, from, to, speed); Transport::lerp(&queue, from, to, speed);
@ -720,7 +720,7 @@ static void roamingStep(Mob *mob, time_t currTime) {
continue; continue;
} }
std::queue<WarpLocation> queue2; std::queue<Vec3> queue2;
Mob* followerMob = (Mob*)NPCManager::NPCs[mob->groupMember[i]]; Mob* followerMob = (Mob*)NPCManager::NPCs[mob->groupMember[i]];
from = { followerMob->x, followerMob->y, followerMob->z }; from = { followerMob->x, followerMob->y, followerMob->z };
to = { farX + followerMob->offsetX, farY + followerMob->offsetY, followerMob->z }; to = { farX + followerMob->offsetX, farY + followerMob->offsetY, followerMob->z };

View File

@ -18,7 +18,7 @@
using namespace TableData; using namespace TableData;
std::map<int32_t, std::vector<WarpLocation>> TableData::RunningSkywayRoutes; std::map<int32_t, std::vector<Vec3>> TableData::RunningSkywayRoutes;
std::map<int32_t, int> TableData::RunningNPCRotations; std::map<int32_t, int> TableData::RunningNPCRotations;
std::map<int32_t, int> TableData::RunningNPCMapNumbers; std::map<int32_t, int> TableData::RunningNPCMapNumbers;
std::map<int32_t, BaseNPC*> TableData::RunningMobs; std::map<int32_t, BaseNPC*> TableData::RunningMobs;
@ -40,14 +40,14 @@ public:
static void constructPathSkyway(json& pathData) { static void constructPathSkyway(json& pathData) {
// Interpolate // Interpolate
json pathPoints = pathData["points"]; json pathPoints = pathData["points"];
std::queue<WarpLocation> points; std::queue<Vec3> points;
json::iterator _point = pathPoints.begin(); json::iterator _point = pathPoints.begin();
auto point = _point.value(); 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 // use some for loop trickery; start position should not be a point
for (_point++; _point != pathPoints.end(); _point++) { for (_point++; _point != pathPoints.end(); _point++) {
point = _point.value(); 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"]); Transport::lerp(&points, last, coords, pathData["iMonkeySpeed"]);
points.push(coords); // add keyframe to the queue points.push(coords); // add keyframe to the queue
last = coords; // update start pos last = coords; // update start pos
@ -58,16 +58,16 @@ static void constructPathSkyway(json& pathData) {
static void constructPathNPC(json& pathData, int32_t id=0) { static void constructPathNPC(json& pathData, int32_t id=0) {
// Interpolate // Interpolate
json pathPoints = pathData["points"]; json pathPoints = pathData["points"];
std::queue<WarpLocation> points; std::queue<Vec3> points;
json::iterator _point = pathPoints.begin(); json::iterator _point = pathPoints.begin();
auto point = _point.value(); 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"]; int stopTime = point["stop"];
for (_point++; _point != pathPoints.end(); _point++) { // loop through all point Bs for (_point++; _point != pathPoints.end(); _point++) { // loop through all point Bs
point = _point.value(); point = _point.value();
for(int i = 0; i < stopTime + 1; i++) // repeat point if it's a stop for(int i = 0; i < stopTime + 1; i++) // repeat point if it's a stop
points.push(from); // add point A to the queue 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 Transport::lerp(&points, from, to, pathData["iBaseSpeed"]); // lerp from A to B
from = to; // update point A from = to; // update point A
stopTime = point["stop"]; stopTime = point["stop"];
@ -282,11 +282,11 @@ static void loadPaths(json& pathData, int32_t* nextId) {
// slider circuit // slider circuit
json pathDataSlider = pathData["slider"]; json pathDataSlider = pathData["slider"];
// lerp between keyframes // lerp between keyframes
std::queue<WarpLocation> route; std::queue<Vec3> route;
// initial point // initial point
json::iterator _point = pathDataSlider.begin(); // iterator json::iterator _point = pathDataSlider.begin(); // iterator
auto point = _point.value(); 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 int stopTime = point["stop"] ? SLIDER_STOP_TICKS : 0; // arbitrary stop length
// remaining points // remaining points
for (_point++; _point != pathDataSlider.end(); _point++) { // loop through all point Bs 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 for (int i = 0; i < stopTime + 1; i++) { // repeat point if it's a stop
route.push(from); // add point A to the queue 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) // we may need to change this later; right now, the speed is cut before and after stops (no accel)
float curve = 1; float curve = 1;
if (stopTime > 0) { // point A is a stop if (stopTime > 0) { // point A is a stop
@ -310,11 +310,11 @@ static void loadPaths(json& pathData, int32_t* nextId) {
int passedDistance = 0; int passedDistance = 0;
// initial point // initial point
int pos = 0; int pos = 0;
WarpLocation lastPoint = route.front(); Vec3 lastPoint = route.front();
route.pop(); route.pop();
route.push(lastPoint); route.push(lastPoint);
for (pos = 1; pos < route.size(); pos++) { 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); passedDistance += hypot(point.x - lastPoint.x, point.y - lastPoint.y);
if (passedDistance >= SLIDER_GAP_SIZE) { // space them out uniformaly if (passedDistance >= SLIDER_GAP_SIZE) { // space them out uniformaly
passedDistance -= SLIDER_GAP_SIZE; // step down passedDistance -= SLIDER_GAP_SIZE; // step down
@ -685,11 +685,11 @@ static void loadGruntwork(json& gruntwork, int32_t* nextId) {
auto skyway = gruntwork["skyway"]; auto skyway = gruntwork["skyway"];
for (auto _route = skyway.begin(); _route != skyway.end(); _route++) { for (auto _route = skyway.begin(); _route != skyway.end(); _route++) {
auto route = _route.value(); auto route = _route.value();
std::vector<WarpLocation> points; std::vector<Vec3> points;
for (auto _point = route["points"].begin(); _point != route["points"].end(); _point++) { for (auto _point = route["points"].begin(); _point != route["points"].end(); _point++) {
auto point = _point.value(); 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; RunningSkywayRoutes[(int)route["iRouteID"]] = points;
@ -1006,7 +1006,7 @@ void TableData::flush() {
route["iMonkeySpeed"] = 1500; route["iMonkeySpeed"] = 1500;
std::cout << "serializing mss route " << (int)pair.first << std::endl; std::cout << "serializing mss route " << (int)pair.first << std::endl;
for (WarpLocation& point : pair.second) { for (Vec3& point : pair.second) {
json tmp; json tmp;
tmp["x"] = point.x; tmp["x"] = point.x;

View File

@ -12,7 +12,7 @@ const int MOB_GROUP_ID_OFFSET = 20000;
typedef nlohmann::json json; typedef nlohmann::json json;
namespace TableData { namespace TableData {
extern std::map<int32_t, std::vector<WarpLocation>> RunningSkywayRoutes; extern std::map<int32_t, std::vector<Vec3>> RunningSkywayRoutes;
extern std::map<int32_t, int> RunningNPCRotations; extern std::map<int32_t, int> RunningNPCRotations;
extern std::map<int32_t, int> RunningNPCMapNumbers; extern std::map<int32_t, int> RunningNPCMapNumbers;
extern std::map<int32_t, BaseNPC*> RunningMobs; extern std::map<int32_t, BaseNPC*> RunningMobs;

View File

@ -13,9 +13,9 @@ using namespace Transport;
std::map<int32_t, TransportRoute> Transport::Routes; std::map<int32_t, TransportRoute> Transport::Routes;
std::map<int32_t, TransportLocation> Transport::Locations; std::map<int32_t, TransportLocation> Transport::Locations;
std::map<int32_t, std::queue<WarpLocation>> Transport::SkywayPaths; std::map<int32_t, std::queue<Vec3>> Transport::SkywayPaths;
std::unordered_map<CNSocket*, std::queue<WarpLocation>> Transport::SkywayQueues; std::unordered_map<CNSocket*, std::queue<Vec3>> Transport::SkywayQueues;
std::unordered_map<int32_t, std::queue<WarpLocation>> Transport::NPCQueues; std::unordered_map<int32_t, std::queue<Vec3>> Transport::NPCQueues;
static void transportRegisterLocationHandler(CNSocket* sock, CNPacketData* data) { static void transportRegisterLocationHandler(CNSocket* sock, CNPacketData* data) {
auto transport = (sP_CL2FE_REQ_REGIST_TRANSPORTATION_LOCATION*)data->buf; auto transport = (sP_CL2FE_REQ_REGIST_TRANSPORTATION_LOCATION*)data->buf;
@ -131,7 +131,7 @@ static void transportWarpHandler(CNSocket* sock, CNPacketData* data) {
plr->onMonkey = true; plr->onMonkey = true;
break; break;
} else if (TableData::RunningSkywayRoutes.find(route.mssRouteNum) != TableData::RunningSkywayRoutes.end()) { } else if (TableData::RunningSkywayRoutes.find(route.mssRouteNum) != TableData::RunningSkywayRoutes.end()) {
std::vector<WarpLocation>* _route = &TableData::RunningSkywayRoutes[route.mssRouteNum]; std::vector<Vec3>* _route = &TableData::RunningSkywayRoutes[route.mssRouteNum];
Nanos::summonNano(sock, -1); Nanos::summonNano(sock, -1);
testMssRoute(sock, _route); testMssRoute(sock, _route);
plr->onMonkey = true; 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); PlayerManager::updatePlayerPosition(sock, target->x, target->y, target->z, INSTANCE_OVERWORLD, plr->angle);
} }
void Transport::testMssRoute(CNSocket *sock, std::vector<WarpLocation>* route) { void Transport::testMssRoute(CNSocket *sock, std::vector<Vec3>* route) {
int speed = 1500; // TODO: make this adjustable int speed = 1500; // TODO: make this adjustable
std::queue<WarpLocation> path; std::queue<Vec3> path;
WarpLocation last = route->front(); // start pos Vec3 last = route->front(); // start pos
for (int i = 1; i < route->size(); i++) { for (int i = 1; i < route->size(); i++) {
WarpLocation coords = route->at(i); Vec3 coords = route->at(i);
Transport::lerp(&path, last, coords, speed); Transport::lerp(&path, last, coords, speed);
path.push(coords); // add keyframe to the queue path.push(coords); // add keyframe to the queue
last = coords; // update start pos last = coords; // update start pos
@ -191,10 +191,10 @@ void Transport::testMssRoute(CNSocket *sock, std::vector<WarpLocation>* route) {
static void stepSkywaySystem() { static void stepSkywaySystem() {
// using an unordered map so we can remove finished players in one iteration // using an unordered map so we can remove finished players in one iteration
std::unordered_map<CNSocket*, std::queue<WarpLocation>>::iterator it = SkywayQueues.begin(); std::unordered_map<CNSocket*, std::queue<Vec3>>::iterator it = SkywayQueues.begin();
while (it != SkywayQueues.end()) { while (it != SkywayQueues.end()) {
std::queue<WarpLocation>* queue = &it->second; std::queue<Vec3>* queue = &it->second;
if (PlayerManager::players.find(it->first) == PlayerManager::players.end()) { if (PlayerManager::players.find(it->first) == PlayerManager::players.end()) {
// pluck out dead socket + update iterator // pluck out dead socket + update iterator
@ -218,7 +218,7 @@ static void stepSkywaySystem() {
it = SkywayQueues.erase(it); // remove player from tracking map + update iterator it = SkywayQueues.erase(it); // remove player from tracking map + update iterator
plr->onMonkey = false; plr->onMonkey = false;
} else { } else {
WarpLocation point = queue->front(); // get point Vec3 point = queue->front(); // get point
queue->pop(); // remove point from front of queue queue->pop(); // remove point from front of queue
INITSTRUCT(sP_FE2CL_PC_BROOMSTICK_MOVE, bmstk); INITSTRUCT(sP_FE2CL_PC_BROOMSTICK_MOVE, bmstk);
@ -240,10 +240,10 @@ static void stepSkywaySystem() {
static void stepNPCPathing() { static void stepNPCPathing() {
// all NPC pathing queues // all NPC pathing queues
std::unordered_map<int32_t, std::queue<WarpLocation>>::iterator it = NPCQueues.begin(); std::unordered_map<int32_t, std::queue<Vec3>>::iterator it = NPCQueues.begin();
while (it != NPCQueues.end()) { while (it != NPCQueues.end()) {
std::queue<WarpLocation>* queue = &it->second; std::queue<Vec3>* queue = &it->second;
BaseNPC* npc = nullptr; BaseNPC* npc = nullptr;
if (NPCManager::NPCs.find(it->first) != NPCManager::NPCs.end()) if (NPCManager::NPCs.find(it->first) != NPCManager::NPCs.end())
@ -267,7 +267,7 @@ static void stepNPCPathing() {
continue; continue;
} }
WarpLocation point = queue->front(); // get point Vec3 point = queue->front(); // get point
queue->pop(); // remove point from front of queue queue->pop(); // remove point from front of queue
// calculate displacement // 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. * Linearly interpolate between two points and insert the results into a queue.
*/ */
void Transport::lerp(std::queue<WarpLocation>* queue, WarpLocation start, WarpLocation end, int gapSize, float curve) { void Transport::lerp(std::queue<Vec3>* queue, Vec3 start, Vec3 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; // number of intermediate points to add int lerps = distanceBetween / gapSize; // number of intermediate points to add
for (int i = 1; i <= lerps; i++) { for (int i = 1; i <= lerps; i++) {
WarpLocation lerp; Vec3 lerp;
// lerp math // lerp math
//float frac = i / (lerps + 1); //float frac = i / (lerps + 1);
float frac = powf(i, curve) / powf(lerps + 1, curve); float frac = powf(i, curve) / powf(lerps + 1, curve);
@ -342,7 +342,7 @@ void Transport::lerp(std::queue<WarpLocation>* queue, WarpLocation start, WarpLo
queue->push(lerp); // add lerp'd point queue->push(lerp); // add lerp'd point
} }
} }
void Transport::lerp(std::queue<WarpLocation>* queue, WarpLocation start, WarpLocation end, int gapSize) { void Transport::lerp(std::queue<Vec3>* queue, Vec3 start, Vec3 end, int gapSize) {
lerp(queue, start, end, gapSize, 1); lerp(queue, start, end, gapSize, 1);
} }

View File

@ -8,6 +8,10 @@ const int SLIDER_SPEED = 1200;
const int SLIDER_STOP_TICKS = 16; const int SLIDER_STOP_TICKS = 16;
const int SLIDER_GAP_SIZE = 45000; const int SLIDER_GAP_SIZE = 45000;
struct Vec3 {
int x, y, z;
};
struct WarpLocation { struct WarpLocation {
int x, y, z, instanceID, isInstance, limitTaskID, npcID; int x, y, z, instanceID, isInstance, limitTaskID, npcID;
}; };
@ -23,14 +27,14 @@ struct TransportLocation {
namespace Transport { namespace Transport {
extern std::map<int32_t, TransportRoute> Routes; extern std::map<int32_t, TransportRoute> Routes;
extern std::map<int32_t, TransportLocation> Locations; extern std::map<int32_t, TransportLocation> Locations;
extern std::map<int32_t, std::queue<WarpLocation>> SkywayPaths; // predefined skyway paths with points extern std::map<int32_t, std::queue<Vec3>> SkywayPaths; // predefined skyway paths with points
extern std::unordered_map<CNSocket*, std::queue<WarpLocation>> SkywayQueues; // player sockets with queued broomstick points extern std::unordered_map<CNSocket*, std::queue<Vec3>> SkywayQueues; // player sockets with queued broomstick points
extern std::unordered_map<int32_t, std::queue<WarpLocation>> NPCQueues; // NPC ids with queued pathing points extern std::unordered_map<int32_t, std::queue<Vec3>> NPCQueues; // NPC ids with queued pathing points
void init(); void init();
void testMssRoute(CNSocket *sock, std::vector<WarpLocation>* route); void testMssRoute(CNSocket *sock, std::vector<Vec3>* route);
void lerp(std::queue<WarpLocation>*, WarpLocation, WarpLocation, int, float); void lerp(std::queue<Vec3>*, Vec3, Vec3, int, float);
void lerp(std::queue<WarpLocation>*, WarpLocation, WarpLocation, int); void lerp(std::queue<Vec3>*, Vec3, Vec3, int);
} }