mirror of
https://github.com/OpenFusionProject/OpenFusion.git
synced 2024-11-05 06:50:04 +00:00
load warps into memory in NPCManager::init
This commit is contained in:
parent
0d27412d81
commit
561a809f33
@ -9,27 +9,47 @@
|
|||||||
#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;
|
||||||
|
|
||||||
void NPCManager::init() {
|
void NPCManager::init() {
|
||||||
// load NPCs from NPCs.json into our NPC manager
|
// load NPCs from NPCs.json into our NPC manager
|
||||||
|
|
||||||
try {
|
try {
|
||||||
std::ifstream inFile("NPCs.json");
|
std::ifstream inFile("NPCs.json");
|
||||||
nlohmann::json jsonData;
|
nlohmann::json npcData;
|
||||||
|
|
||||||
// read file into jsonData
|
// read file into json
|
||||||
inFile >> jsonData;
|
inFile >> npcData;
|
||||||
|
|
||||||
for (auto& npc : jsonData) {
|
for (nlohmann::json::iterator npc = npcData.begin(); npc != npcData.end(); npc++) {
|
||||||
BaseNPC tmp(npc["x"], npc["y"], npc["z"], npc["id"]);
|
BaseNPC tmp(npc.value()["x"], npc.value()["y"], npc.value()["z"], npc.value()["id"]);
|
||||||
NPCManager::NPCs[tmp.appearanceData.iNPC_ID] = tmp;
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,24 +101,23 @@ void NPCManager::updatePlayerNPCS(CNSocket* sock, PlayerView& view) {
|
|||||||
|
|
||||||
PlayerManager::players[sock].viewableNPCs = view.viewableNPCs;
|
PlayerManager::players[sock].viewableNPCs = view.viewableNPCs;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NPCManager::npcWarpManager(CNSocket* sock, CNPacketData* data)
|
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))
|
if (data->size != sizeof(sP_CL2FE_REQ_PC_WARP_USE_NPC))
|
||||||
return; // malformed packet
|
return; // malformed packet
|
||||||
|
|
||||||
sP_CL2FE_REQ_PC_WARP_USE_NPC* warpNpc = (sP_CL2FE_REQ_PC_WARP_USE_NPC*)data->buf;
|
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);
|
INITSTRUCT(sP_FE2CL_REP_PC_WARP_USE_NPC_SUCC, resp);
|
||||||
resp.iX = warp[std::to_string(warpNpc->iWarpID)]["m_iToX"];
|
resp.iX = Warps[warpNpc->iWarpID].x;
|
||||||
resp.iY = warp[std::to_string(warpNpc->iWarpID)]["m_iToY"];
|
resp.iY = Warps[warpNpc->iWarpID].y;
|
||||||
resp.iZ = warp[std::to_string(warpNpc->iWarpID)]["m_iToZ"];
|
resp.iZ = Warps[warpNpc->iWarpID].z;
|
||||||
//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;
|
|
||||||
|
|
||||||
sock->sendPacket((void*)&resp, P_FE2CL_REP_PC_WARP_USE_NPC_SUCC, sizeof(sP_FE2CL_REP_PC_WARP_USE_NPC_SUCC));
|
sock->sendPacket((void*)&resp, P_FE2CL_REP_PC_WARP_USE_NPC_SUCC, sizeof(sP_FE2CL_REP_PC_WARP_USE_NPC_SUCC));
|
||||||
|
|
||||||
|
@ -7,8 +7,13 @@
|
|||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
|
struct WarpLocation {
|
||||||
|
int x, y, z;
|
||||||
|
};
|
||||||
|
|
||||||
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;
|
||||||
void init();
|
void init();
|
||||||
|
|
||||||
void updatePlayerNPCS(CNSocket* sock, PlayerView& plr);
|
void updatePlayerNPCS(CNSocket* sock, PlayerView& plr);
|
||||||
|
File diff suppressed because one or more lines are too long
1
warps.json
Normal file
1
warps.json
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user