Respawn points work now.

Note that some of them weren't present in clientnpc and will need to be
manually added later.
This commit is contained in:
dongresource 2020-08-25 03:34:53 +02:00
parent d025b611a1
commit d964a83d6d
4 changed files with 34 additions and 5 deletions

View File

@ -5,11 +5,13 @@
#include <algorithm> #include <algorithm>
#include <list> #include <list>
#include <fstream> #include <fstream>
#include <vector>
#include "contrib/JSON.hpp" #include "contrib/JSON.hpp"
std::map<int32_t, BaseNPC> NPCManager::NPCs; std::map<int32_t, BaseNPC> NPCManager::NPCs;
std::map<int32_t, WarpLocation> NPCManager::Warps; std::map<int32_t, WarpLocation> NPCManager::Warps;
std::vector<WarpLocation> NPCManager::RespawnPoints;
void NPCManager::init() { void NPCManager::init() {
// load NPCs from NPCs.json into our NPC manager // 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++) { 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"]); BaseNPC tmp(npc.value()["x"], npc.value()["y"], npc.value()["z"], npc.value()["id"]);
NPCs[tmp.appearanceData.iNPC_ID] = tmp; 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; std::cout << "[INFO] populated " << NPCs.size() << " NPCs" << std::endl;

View File

@ -5,7 +5,11 @@
#include "NPC.hpp" #include "NPC.hpp"
#include <map> #include <map>
#include <vector>
#define RESURRECT_HEIGHT 400
// this should really be called vec3 or something...
struct WarpLocation { struct WarpLocation {
int x, y, z; int x, y, z;
}; };
@ -13,9 +17,9 @@ struct WarpLocation {
namespace NPCManager { namespace NPCManager {
extern std::map<int32_t, BaseNPC> NPCs; extern std::map<int32_t, BaseNPC> NPCs;
extern std::map<int32_t, WarpLocation> Warps; extern std::map<int32_t, WarpLocation> Warps;
extern std::vector<WarpLocation> RespawnPoints;
void init(); void init();
void updatePlayerNPCS(CNSocket* sock, PlayerView& plr); void updatePlayerNPCS(CNSocket* sock, PlayerView& plr);
void npcWarpManager(CNSocket* sock, CNPacketData* data); void npcWarpManager(CNSocket* sock, CNPacketData* data);
} }

View File

@ -575,6 +575,7 @@ void PlayerManager::revivePlayer(CNSocket* sock, CNPacketData* data) {
return; return;
Player *plr = PlayerManager::getPlayer(sock); Player *plr = PlayerManager::getPlayer(sock);
WarpLocation target = PlayerManager::getRespawnPoint(plr);
// players respawn at same spot they died at for now... // players respawn at same spot they died at for now...
sP_CL2FE_REQ_PC_REGEN* reviveData = (sP_CL2FE_REQ_PC_REGEN*)data->buf; 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.bMoveLocation = reviveData->eIL;
response.PCRegenData.iMapNum = reviveData->iIndex; response.PCRegenData.iMapNum = reviveData->iIndex;
response.PCRegenData.iHP = 1000 * plr->level; response.PCRegenData.iHP = 1000 * plr->level;
response.PCRegenData.iX = plr->x; response.PCRegenData.iX = target.x;
response.PCRegenData.iY = plr->y; response.PCRegenData.iY = target.y;
response.PCRegenData.iZ = plr->z; response.PCRegenData.iZ = target.z;
sock->sendPacket((void*)&response, P_FE2CL_REP_PC_REGEN_SUCC, sizeof(sP_FE2CL_REP_PC_REGEN_SUCC)); 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) { Player *PlayerManager::getPlayer(CNSocket* key) {
return players[key].plr; 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 #pragma endregion

View File

@ -8,6 +8,8 @@
#include <map> #include <map>
#include <list> #include <list>
struct WarpLocation;
struct PlayerView { struct PlayerView {
std::list<CNSocket*> viewable; std::list<CNSocket*> viewable;
std::list<int32_t> viewableNPCs; std::list<int32_t> viewableNPCs;
@ -22,7 +24,6 @@ namespace PlayerManager {
void addPlayer(CNSocket* key, Player plr); void addPlayer(CNSocket* key, Player plr);
void removePlayer(CNSocket* key); void removePlayer(CNSocket* key);
Player *getPlayer(CNSocket* key);
void updatePlayerPosition(CNSocket* sock, int X, int Y, int Z); void updatePlayerPosition(CNSocket* sock, int X, int Y, int Z);
std::list<CNSocket*> getNearbyPlayers(int X, int Y, int dist); std::list<CNSocket*> getNearbyPlayers(int X, int Y, int dist);
@ -47,4 +48,7 @@ namespace PlayerManager {
void enterPlayerVehicle(CNSocket* sock, CNPacketData* data); void enterPlayerVehicle(CNSocket* sock, CNPacketData* data);
void exitPlayerVehicle(CNSocket* sock, CNPacketData* data); void exitPlayerVehicle(CNSocket* sock, CNPacketData* data);
Player *getPlayer(CNSocket* key);
WarpLocation getRespawnPoint(Player *plr);
} }