diff --git a/src/NPCManager.cpp b/src/NPCManager.cpp index 98d999e..22ce2ff 100644 --- a/src/NPCManager.cpp +++ b/src/NPCManager.cpp @@ -5,11 +5,13 @@ #include #include #include +#include #include "contrib/JSON.hpp" std::map NPCManager::NPCs; std::map NPCManager::Warps; +std::vector NPCManager::RespawnPoints; void NPCManager::init() { // load NPCs from NPCs.json into our NPC manager @@ -24,6 +26,9 @@ void NPCManager::init() { for (nlohmann::json::iterator npc = npcData.begin(); npc != npcData.end(); npc++) { BaseNPC tmp(npc.value()["x"], npc.value()["y"], npc.value()["z"], npc.value()["id"]); NPCs[tmp.appearanceData.iNPC_ID] = tmp; + + if (npc.value()["id"] == 641 || npc.value()["id"] == 642) + RespawnPoints.push_back({npc.value()["x"], npc.value()["y"], ((int)npc.value()["z"]) + RESURRECT_HEIGHT}); } std::cout << "[INFO] populated " << NPCs.size() << " NPCs" << std::endl; diff --git a/src/NPCManager.hpp b/src/NPCManager.hpp index b1897b1..12f17a3 100644 --- a/src/NPCManager.hpp +++ b/src/NPCManager.hpp @@ -5,7 +5,11 @@ #include "NPC.hpp" #include +#include +#define RESURRECT_HEIGHT 400 + +// this should really be called vec3 or something... struct WarpLocation { int x, y, z; }; @@ -13,9 +17,9 @@ struct WarpLocation { namespace NPCManager { extern std::map NPCs; extern std::map Warps; + extern std::vector RespawnPoints; void init(); void updatePlayerNPCS(CNSocket* sock, PlayerView& plr); void npcWarpManager(CNSocket* sock, CNPacketData* data); - } diff --git a/src/PlayerManager.cpp b/src/PlayerManager.cpp index 293b3b4..1d6989d 100644 --- a/src/PlayerManager.cpp +++ b/src/PlayerManager.cpp @@ -575,6 +575,7 @@ void PlayerManager::revivePlayer(CNSocket* sock, CNPacketData* data) { return; Player *plr = PlayerManager::getPlayer(sock); + WarpLocation target = PlayerManager::getRespawnPoint(plr); // players respawn at same spot they died at for now... sP_CL2FE_REQ_PC_REGEN* reviveData = (sP_CL2FE_REQ_PC_REGEN*)data->buf; @@ -582,9 +583,9 @@ void PlayerManager::revivePlayer(CNSocket* sock, CNPacketData* data) { response.bMoveLocation = reviveData->eIL; response.PCRegenData.iMapNum = reviveData->iIndex; response.PCRegenData.iHP = 1000 * plr->level; - response.PCRegenData.iX = plr->x; - response.PCRegenData.iY = plr->y; - response.PCRegenData.iZ = plr->z; + response.PCRegenData.iX = target.x; + response.PCRegenData.iY = target.y; + response.PCRegenData.iZ = target.z; sock->sendPacket((void*)&response, P_FE2CL_REP_PC_REGEN_SUCC, sizeof(sP_FE2CL_REP_PC_REGEN_SUCC)); } @@ -636,4 +637,19 @@ void PlayerManager::setSpecialSwitchPlayer(CNSocket* sock, CNPacketData* data) { Player *PlayerManager::getPlayer(CNSocket* key) { return players[key].plr; } + +WarpLocation PlayerManager::getRespawnPoint(Player *plr) { + WarpLocation best; + uint32_t curDist, bestDist = UINT32_MAX; + + for (auto targ : NPCManager::RespawnPoints) { + curDist = sqrt(pow(plr->x - targ.x, 2) + pow(plr->y - targ.y, 2)); + if (curDist < bestDist) { + best = targ; + bestDist = curDist; + } + } + + return best; +} #pragma endregion diff --git a/src/PlayerManager.hpp b/src/PlayerManager.hpp index f719b6a..78f904b 100644 --- a/src/PlayerManager.hpp +++ b/src/PlayerManager.hpp @@ -8,6 +8,8 @@ #include #include +struct WarpLocation; + struct PlayerView { std::list viewable; std::list viewableNPCs; @@ -22,7 +24,6 @@ namespace PlayerManager { void addPlayer(CNSocket* key, Player plr); void removePlayer(CNSocket* key); - Player *getPlayer(CNSocket* key); void updatePlayerPosition(CNSocket* sock, int X, int Y, int Z); std::list getNearbyPlayers(int X, int Y, int dist); @@ -47,4 +48,7 @@ namespace PlayerManager { void enterPlayerVehicle(CNSocket* sock, CNPacketData* data); void exitPlayerVehicle(CNSocket* sock, CNPacketData* data); + + Player *getPlayer(CNSocket* key); + WarpLocation getRespawnPoint(Player *plr); }