mirror of
https://github.com/OpenFusionProject/OpenFusion.git
synced 2024-11-22 13:30:06 +00:00
Fix unhandled exception in NPCManager (#24)
* fixed PROTOCOL_VERSION not being defined * handle exceptions in NPCManager init
This commit is contained in:
parent
78c493b461
commit
5f65a84b02
@ -10,37 +10,26 @@
|
|||||||
|
|
||||||
std::map<int32_t, BaseNPC> NPCManager::NPCs;
|
std::map<int32_t, BaseNPC> NPCManager::NPCs;
|
||||||
|
|
||||||
#define CHECKNPC(x) if (NPC.value().find(x) == NPC.value().end()) { \
|
|
||||||
std::cout << "[WARN] Malformed NPC.json file! failed to find " << x << std::endl; \
|
|
||||||
return; \
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
||||||
std::ifstream inFile("NPCs.json");
|
std::ifstream inFile("NPCs.json");
|
||||||
nlohmann::json jsonData;
|
nlohmann::json jsonData;
|
||||||
|
|
||||||
// read file into jsonData
|
// read file into jsonData
|
||||||
inFile >> jsonData;
|
inFile >> jsonData;
|
||||||
|
|
||||||
for (auto& NPC : jsonData.items()) {
|
for (auto& npc : jsonData) {
|
||||||
std::string name = NPC.key();
|
BaseNPC tmp(npc["x"], npc["y"], npc["z"], npc["id"]);
|
||||||
|
|
||||||
// sanity checks
|
|
||||||
CHECKNPC("id")
|
|
||||||
CHECKNPC("x")
|
|
||||||
CHECKNPC("y")
|
|
||||||
CHECKNPC("z")
|
|
||||||
|
|
||||||
int x = (int)(jsonData[NPC.key()]["x"]);
|
|
||||||
int y = (int)(jsonData[NPC.key()]["y"]);
|
|
||||||
int z = (int)(jsonData[NPC.key()]["z"]);
|
|
||||||
BaseNPC tmp(x, y, z, jsonData[NPC.key()]["id"]);
|
|
||||||
NPCManager::NPCs[tmp.appearanceData.iNPC_ID] = tmp;
|
NPCManager::NPCs[tmp.appearanceData.iNPC_ID] = tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "populated " << NPCs.size() << " NPCs" << std::endl;
|
std::cout << "populated " << NPCs.size() << " NPCs" << std::endl;
|
||||||
|
}
|
||||||
|
catch (const std::exception& err) {
|
||||||
|
std::cerr << "[WARN] Malformed NPC.json file! Reason:" << std::endl << err.what() << std::endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#undef CHECKNPC
|
#undef CHECKNPC
|
||||||
@ -49,7 +38,7 @@ void NPCManager::updatePlayerNPCS(CNSocket* sock, PlayerView& view) {
|
|||||||
std::list<int32_t> yesView;
|
std::list<int32_t> yesView;
|
||||||
std::list<int32_t> noView;
|
std::list<int32_t> noView;
|
||||||
|
|
||||||
for (auto pair : NPCs) {
|
for (auto& pair : NPCs) {
|
||||||
int diffX = abs(view.plr.x - pair.second.appearanceData.iX);
|
int diffX = abs(view.plr.x - pair.second.appearanceData.iX);
|
||||||
int diffY = abs(view.plr.y - pair.second.appearanceData.iY);
|
int diffY = abs(view.plr.y - pair.second.appearanceData.iY);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user