Fix unhandled exception in NPCManager (#24)

* fixed PROTOCOL_VERSION not being defined

* handle exceptions in NPCManager init
This commit is contained in:
FinnHornhoover 2020-08-22 09:46:52 +03:00 committed by GitHub
parent 78c493b461
commit 5f65a84b02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,37 +10,26 @@
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() {
// load NPCs from NPCs.json into our NPC manager
try {
std::ifstream inFile("NPCs.json");
nlohmann::json jsonData;
// read file into jsonData
inFile >> jsonData;
for (auto& NPC : jsonData.items()) {
std::string name = NPC.key();
// 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"]);
for (auto& npc : jsonData) {
BaseNPC tmp(npc["x"], npc["y"], npc["z"], npc["id"]);
NPCManager::NPCs[tmp.appearanceData.iNPC_ID] = tmp;
}
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
@ -49,7 +38,7 @@ void NPCManager::updatePlayerNPCS(CNSocket* sock, PlayerView& view) {
std::list<int32_t> yesView;
std::list<int32_t> noView;
for (auto pair : NPCs) {
for (auto& pair : NPCs) {
int diffX = abs(view.plr.x - pair.second.appearanceData.iX);
int diffY = abs(view.plr.y - pair.second.appearanceData.iY);