2020-08-19 22:21:35 +00:00
|
|
|
#include "NPCManager.hpp"
|
2020-08-20 21:43:48 +00:00
|
|
|
#include "settings.hpp"
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <list>
|
2020-08-21 22:14:11 +00:00
|
|
|
#include <fstream>
|
2020-08-25 01:34:53 +00:00
|
|
|
#include <vector>
|
2020-08-21 22:14:11 +00:00
|
|
|
|
2020-08-22 02:03:12 +00:00
|
|
|
#include "contrib/JSON.hpp"
|
2020-08-20 21:43:48 +00:00
|
|
|
|
|
|
|
std::map<int32_t, BaseNPC> NPCManager::NPCs;
|
2020-08-23 16:26:25 +00:00
|
|
|
std::map<int32_t, WarpLocation> NPCManager::Warps;
|
2020-08-25 01:34:53 +00:00
|
|
|
std::vector<WarpLocation> NPCManager::RespawnPoints;
|
2020-08-19 22:21:35 +00:00
|
|
|
|
|
|
|
void NPCManager::init() {
|
2020-08-21 22:14:11 +00:00
|
|
|
// load NPCs from NPCs.json into our NPC manager
|
|
|
|
|
2020-08-22 06:46:52 +00:00
|
|
|
try {
|
2020-08-25 01:45:04 +00:00
|
|
|
std::ifstream inFile(settings::NPCJSON);
|
2020-08-23 16:26:25 +00:00
|
|
|
nlohmann::json npcData;
|
2020-08-21 22:14:11 +00:00
|
|
|
|
2020-08-23 16:26:25 +00:00
|
|
|
// read file into json
|
|
|
|
inFile >> npcData;
|
2020-08-21 22:14:11 +00:00
|
|
|
|
2020-08-23 16:26:25 +00:00
|
|
|
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;
|
2020-08-25 01:34:53 +00:00
|
|
|
|
|
|
|
if (npc.value()["id"] == 641 || npc.value()["id"] == 642)
|
|
|
|
RespawnPoints.push_back({npc.value()["x"], npc.value()["y"], ((int)npc.value()["z"]) + RESURRECT_HEIGHT});
|
2020-08-22 06:46:52 +00:00
|
|
|
}
|
2020-08-21 22:14:11 +00:00
|
|
|
|
2020-08-23 16:26:25 +00:00
|
|
|
} catch (const std::exception& err) {
|
|
|
|
std::cerr << "[WARN] Malformed NPCs.json file! Reason:" << err.what() << std::endl;
|
2020-08-22 06:46:52 +00:00
|
|
|
}
|
2020-08-23 16:26:25 +00:00
|
|
|
|
2020-08-25 17:42:56 +00:00
|
|
|
// load temporary mob dump
|
|
|
|
try {
|
2020-08-28 16:31:53 +00:00
|
|
|
std::ifstream inFile("data/mobs.json"); // not in settings, since it's temp
|
2020-08-25 17:42:56 +00:00
|
|
|
nlohmann::json npcData;
|
|
|
|
|
|
|
|
// read file into json
|
|
|
|
inFile >> npcData;
|
|
|
|
|
|
|
|
for (nlohmann::json::iterator npc = npcData.begin(); npc != npcData.end(); npc++) {
|
|
|
|
BaseNPC tmp(npc.value()["iX"], npc.value()["iY"], npc.value()["iZ"], npc.value()["iNPCType"],
|
|
|
|
npc.value()["iHP"], npc.value()["iConditionBitFlag"], npc.value()["iAngle"], npc.value()["iBarkerType"]);
|
|
|
|
|
|
|
|
NPCs[tmp.appearanceData.iNPC_ID] = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "[INFO] populated " << NPCs.size() << " NPCs" << std::endl;
|
|
|
|
} catch (const std::exception& err) {
|
|
|
|
std::cerr << "[WARN] Malformed mobs.json file! Reason:" << err.what() << std::endl;
|
|
|
|
}
|
|
|
|
|
2020-08-23 16:26:25 +00:00
|
|
|
try {
|
2020-08-25 01:45:04 +00:00
|
|
|
std::ifstream infile(settings::WARPJSON);
|
2020-08-23 16:26:25 +00:00
|
|
|
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;
|
2020-08-21 22:14:11 +00:00
|
|
|
}
|
2020-08-23 16:26:25 +00:00
|
|
|
|
|
|
|
|
2020-08-25 02:28:42 +00:00
|
|
|
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_WARP_USE_NPC, npcWarpHandler);
|
|
|
|
REGISTER_SHARD_PACKET(P_CL2FE_REQ_NPC_SUMMON, npcSummonHandler);
|
2020-08-20 21:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NPCManager::updatePlayerNPCS(CNSocket* sock, PlayerView& view) {
|
|
|
|
std::list<int32_t> yesView;
|
|
|
|
std::list<int32_t> noView;
|
|
|
|
|
2020-08-22 06:46:52 +00:00
|
|
|
for (auto& pair : NPCs) {
|
2020-08-24 22:02:07 +00:00
|
|
|
int diffX = abs(view.plr->x - pair.second.appearanceData.iX);
|
|
|
|
int diffY = abs(view.plr->y - pair.second.appearanceData.iY);
|
2020-08-20 21:43:48 +00:00
|
|
|
|
2020-08-26 02:53:27 +00:00
|
|
|
if (diffX < settings::NPCDISTANCE && diffY < settings::NPCDISTANCE) {
|
2020-08-20 21:43:48 +00:00
|
|
|
yesView.push_back(pair.first);
|
|
|
|
} else {
|
|
|
|
noView.push_back(pair.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 00:26:18 +00:00
|
|
|
INITSTRUCT(sP_FE2CL_NPC_EXIT, exitData);
|
2020-08-20 21:43:48 +00:00
|
|
|
std::list<int32_t>::iterator i = view.viewableNPCs.begin();
|
|
|
|
while (i != view.viewableNPCs.end()) {
|
|
|
|
int32_t id = *i;
|
|
|
|
|
|
|
|
if (std::find(noView.begin(), noView.end(), id) != noView.end()) {
|
|
|
|
// it shouldn't be visible, send NPC_EXIT
|
2020-08-25 17:42:56 +00:00
|
|
|
|
2020-08-22 23:31:09 +00:00
|
|
|
exitData.iNPC_ID = id;
|
|
|
|
sock->sendPacket((void*)&exitData, P_FE2CL_NPC_EXIT, sizeof(sP_FE2CL_NPC_EXIT));
|
2020-08-25 17:42:56 +00:00
|
|
|
|
2020-08-20 21:43:48 +00:00
|
|
|
// remove from view
|
|
|
|
view.viewableNPCs.erase(i++);
|
2020-08-26 20:22:52 +00:00
|
|
|
} else {
|
|
|
|
i++;
|
2020-08-20 21:43:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 00:26:18 +00:00
|
|
|
INITSTRUCT(sP_FE2CL_NPC_ENTER, enterData);
|
2020-08-20 21:43:48 +00:00
|
|
|
for (int32_t id : yesView) {
|
|
|
|
if (std::find(view.viewableNPCs.begin(), view.viewableNPCs.end(), id) == view.viewableNPCs.end()) {
|
|
|
|
// needs to be added to viewableNPCs! send NPC_ENTER
|
|
|
|
|
2020-08-22 23:31:09 +00:00
|
|
|
enterData.NPCAppearanceData = NPCs[id].appearanceData;
|
|
|
|
sock->sendPacket((void*)&enterData, P_FE2CL_NPC_ENTER, sizeof(sP_FE2CL_NPC_ENTER));
|
2020-08-25 17:42:56 +00:00
|
|
|
|
2020-08-22 23:31:09 +00:00
|
|
|
// add to viewable
|
2020-08-20 21:43:48 +00:00
|
|
|
view.viewableNPCs.push_back(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PlayerManager::players[sock].viewableNPCs = view.viewableNPCs;
|
2020-08-23 15:32:25 +00:00
|
|
|
}
|
2020-08-23 16:26:25 +00:00
|
|
|
|
2020-08-25 02:28:42 +00:00
|
|
|
void NPCManager::npcWarpHandler(CNSocket* sock, CNPacketData* data) {
|
2020-08-23 15:32:25 +00:00
|
|
|
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;
|
2020-08-25 18:42:52 +00:00
|
|
|
PlayerView& plrv = PlayerManager::players[sock];
|
2020-08-23 15:32:25 +00:00
|
|
|
|
2020-08-23 16:26:25 +00:00
|
|
|
// sanity check
|
|
|
|
if (Warps.find(warpNpc->iWarpID) == Warps.end())
|
|
|
|
return;
|
2020-08-23 15:32:25 +00:00
|
|
|
|
2020-08-23 16:26:25 +00:00
|
|
|
// send to client
|
|
|
|
INITSTRUCT(sP_FE2CL_REP_PC_WARP_USE_NPC_SUCC, resp);
|
|
|
|
resp.iX = Warps[warpNpc->iWarpID].x;
|
|
|
|
resp.iY = Warps[warpNpc->iWarpID].y;
|
|
|
|
resp.iZ = Warps[warpNpc->iWarpID].z;
|
2020-08-25 18:42:52 +00:00
|
|
|
|
|
|
|
// force player & NPC reload
|
|
|
|
plrv.viewable.clear();
|
|
|
|
plrv.viewableNPCs.clear();
|
2020-08-25 17:42:56 +00:00
|
|
|
|
2020-08-23 15:32:25 +00:00
|
|
|
sock->sendPacket((void*)&resp, P_FE2CL_REP_PC_WARP_USE_NPC_SUCC, sizeof(sP_FE2CL_REP_PC_WARP_USE_NPC_SUCC));
|
|
|
|
|
|
|
|
}
|
2020-08-25 02:28:42 +00:00
|
|
|
|
|
|
|
void NPCManager::npcSummonHandler(CNSocket *sock, CNPacketData *data) {
|
|
|
|
if (data->size != sizeof(sP_CL2FE_REQ_NPC_SUMMON))
|
|
|
|
return; // malformed packet
|
|
|
|
|
|
|
|
sP_CL2FE_REQ_NPC_SUMMON* req = (sP_CL2FE_REQ_NPC_SUMMON*)data->buf;
|
|
|
|
INITSTRUCT(sP_FE2CL_NPC_ENTER, resp);
|
|
|
|
Player *plr = PlayerManager::getPlayer(sock);
|
|
|
|
|
|
|
|
// permission & sanity check
|
2020-08-25 17:42:56 +00:00
|
|
|
if (!plr->IsGM || req->iNPCType >= 3314)
|
2020-08-25 02:28:42 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
resp.NPCAppearanceData.iNPC_ID = rand(); // cpunch-style
|
|
|
|
resp.NPCAppearanceData.iNPCType = req->iNPCType;
|
|
|
|
resp.NPCAppearanceData.iHP = 1000; // TODO: placeholder
|
|
|
|
resp.NPCAppearanceData.iX = plr->x;
|
|
|
|
resp.NPCAppearanceData.iY = plr->y;
|
|
|
|
resp.NPCAppearanceData.iZ = plr->z;
|
|
|
|
|
|
|
|
sock->sendPacket((void*)&resp, P_FE2CL_NPC_ENTER, sizeof(sP_FE2CL_NPC_ENTER));
|
|
|
|
}
|