load warps into memory in NPCManager::init

This commit is contained in:
CPunch 2020-08-23 11:26:25 -05:00
parent 0d27412d81
commit 561a809f33
4 changed files with 46 additions and 22 deletions

View File

@ -9,28 +9,48 @@
#include "contrib/JSON.hpp"
std::map<int32_t, BaseNPC> NPCManager::NPCs;
std::map<int32_t, WarpLocation> NPCManager::Warps;
void NPCManager::init() {
// load NPCs from NPCs.json into our NPC manager
try {
std::ifstream inFile("NPCs.json");
nlohmann::json jsonData;
nlohmann::json npcData;
// read file into jsonData
inFile >> jsonData;
// read file into json
inFile >> npcData;
for (auto& npc : jsonData) {
BaseNPC tmp(npc["x"], npc["y"], npc["z"], npc["id"]);
NPCManager::NPCs[tmp.appearanceData.iNPC_ID] = tmp;
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;
}
std::cout << "populated " << NPCs.size() << " NPCs" << std::endl;
std::cout << "[INFO] populated " << NPCs.size() << " NPCs" << std::endl;
} catch (const std::exception& err) {
std::cerr << "[WARN] Malformed NPCs.json file! Reason:" << err.what() << std::endl;
}
catch (const std::exception& err) {
std::cerr << "[WARN] Malformed NPC.json file! Reason:" << std::endl << err.what() << std::endl;
try {
std::ifstream infile("warps.json");
nlohmann::json warpData;
// read file into json
infile >> warpData;
for (nlohmann::json::iterator warp = warpData.begin(); warp != warpData.end(); warp++) {
WarpLocation warpLoc = {warp.value()["m_iToX"], warp.value()["m_iToY"], warp.value()["m_iToZ"]};
int warpID = atoi(warp.key().c_str());
Warps[warpID] = warpLoc;
}
std::cout << "[INFO] populated " << Warps.size() << " Warps" << std::endl;
} catch (const std::exception& err) {
std::cerr << "[WARN] Malformed warps.json file! Reason:" << err.what() << std::endl;
}
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_WARP_USE_NPC, npcWarpManager);
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_WARP_USE_NPC, npcWarpManager);
}
void NPCManager::updatePlayerNPCS(CNSocket* sock, PlayerView& view) {
@ -81,24 +101,23 @@ void NPCManager::updatePlayerNPCS(CNSocket* sock, PlayerView& view) {
PlayerManager::players[sock].viewableNPCs = view.viewableNPCs;
}
void NPCManager::npcWarpManager(CNSocket* sock, CNPacketData* data)
{
std::ifstream warp_file("warps.json", std::ifstream::binary);
nlohmann::json warp;
warp_file >> warp;
if (data->size != sizeof(sP_CL2FE_REQ_PC_WARP_USE_NPC))
return; // malformed packet
sP_CL2FE_REQ_PC_WARP_USE_NPC* warpNpc = (sP_CL2FE_REQ_PC_WARP_USE_NPC*)data->buf;
//Send to Client
// sanity check
if (Warps.find(warpNpc->iWarpID) == Warps.end())
return;
// send to client
INITSTRUCT(sP_FE2CL_REP_PC_WARP_USE_NPC_SUCC, resp);
resp.iX = warp[std::to_string(warpNpc->iWarpID)]["m_iToX"];
resp.iY = warp[std::to_string(warpNpc->iWarpID)]["m_iToY"];
resp.iZ = warp[std::to_string(warpNpc->iWarpID)]["m_iToZ"];
//Add Instance Stuff Later
std::cerr << "OpenFusion: Warp using " << "Warp ID" <<warpNpc->iWarpID << "Warp to Z:"<< warp[std::to_string(warpNpc->iWarpID)]["m_iToZ"] << std::endl;
resp.iX = Warps[warpNpc->iWarpID].x;
resp.iY = Warps[warpNpc->iWarpID].y;
resp.iZ = Warps[warpNpc->iWarpID].z;
sock->sendPacket((void*)&resp, P_FE2CL_REP_PC_WARP_USE_NPC_SUCC, sizeof(sP_FE2CL_REP_PC_WARP_USE_NPC_SUCC));

View File

@ -7,8 +7,13 @@
#include <map>
struct WarpLocation {
int x, y, z;
};
namespace NPCManager {
extern std::map<int32_t, BaseNPC> NPCs;
extern std::map<int32_t, WarpLocation> Warps;
void init();
void updatePlayerNPCS(CNSocket* sock, PlayerView& plr);

File diff suppressed because one or more lines are too long

1
warps.json Normal file

File diff suppressed because one or more lines are too long