OpenFusion/src/NPCManager.cpp

360 lines
13 KiB
C++
Raw Normal View History

2020-08-19 22:21:35 +00:00
#include "NPCManager.hpp"
#include "Items.hpp"
2020-08-20 21:43:48 +00:00
#include "settings.hpp"
#include "Combat.hpp"
#include "Missions.hpp"
#include "Chunking.hpp"
#include "Nanos.hpp"
2020-11-08 08:42:49 +00:00
#include "TableData.hpp"
#include "Groups.hpp"
#include "Racing.hpp"
#include "Vendor.hpp"
#include "Abilities.hpp"
2021-03-17 21:28:24 +00:00
#include "Eggs.hpp"
2020-08-20 21:43:48 +00:00
#include <cmath>
#include <algorithm>
#include <list>
2020-08-21 22:14:11 +00:00
#include <fstream>
#include <vector>
#include <assert.h>
#include <limits.h>
2020-08-21 22:14:11 +00:00
#include "JSON.hpp"
2020-08-20 21:43:48 +00:00
using namespace NPCManager;
std::map<int32_t, BaseNPC*> NPCManager::NPCs;
std::map<int32_t, WarpLocation> NPCManager::Warps;
std::vector<WarpLocation> NPCManager::RespawnPoints;
nlohmann::json NPCManager::NPCData;
2020-08-19 22:21:35 +00:00
/*
* Initialized at the end of TableData::init().
* This allows us to summon and kill mobs in arbitrary order without
* NPC ID collisions.
*/
int32_t NPCManager::nextId;
2020-09-18 21:24:15 +00:00
2020-09-25 01:58:20 +00:00
void NPCManager::destroyNPC(int32_t id) {
2020-09-18 21:24:15 +00:00
// sanity check
if (NPCs.find(id) == NPCs.end()) {
std::cout << "npc not found: " << id << std::endl;
2020-09-18 21:24:15 +00:00
return;
}
BaseNPC* entity = NPCs[id];
// sanity check
if (!Chunking::chunkExists(entity->chunkPos)) {
2020-09-18 21:24:15 +00:00
std::cout << "chunk not found!" << std::endl;
return;
}
// remove NPC from the chunk
Chunking::untrackNPC(entity->chunkPos, id);
2020-09-25 01:58:20 +00:00
// remove from viewable chunks
Chunking::removeNPCFromChunks(Chunking::getViewableChunks(entity->chunkPos), id);
2020-09-23 04:08:51 +00:00
2020-09-25 01:58:20 +00:00
// remove from mob manager
if (MobAI::Mobs.find(id) != MobAI::Mobs.end())
MobAI::Mobs.erase(id);
2020-10-24 19:48:55 +00:00
// remove from eggs
2021-03-17 21:28:24 +00:00
if (Eggs::Eggs.find(id) != Eggs::Eggs.end())
Eggs::Eggs.erase(id);
2020-10-24 19:48:55 +00:00
2020-09-18 21:24:15 +00:00
// finally, remove it from the map and free it
2020-11-26 03:49:37 +00:00
delete entity->viewableChunks;
2020-09-18 21:24:15 +00:00
NPCs.erase(id);
delete entity;
2020-09-17 22:45:43 +00:00
}
2020-11-18 00:07:04 +00:00
void NPCManager::updateNPCPosition(int32_t id, int X, int Y, int Z, uint64_t I, int angle) {
BaseNPC* npc = NPCs[id];
2020-11-18 00:07:04 +00:00
npc->appearanceData.iAngle = angle;
2020-11-19 01:37:58 +00:00
ChunkPos oldChunk = npc->chunkPos;
ChunkPos newChunk = Chunking::chunkPosAt(X, Y, I);
npc->appearanceData.iX = X;
npc->appearanceData.iY = Y;
npc->appearanceData.iZ = Z;
2020-11-18 00:07:04 +00:00
npc->instanceID = I;
if (oldChunk == newChunk)
return; // didn't change chunks
Chunking::updateNPCChunk(id, oldChunk, newChunk);
}
void NPCManager::sendToViewable(BaseNPC *npc, void *buf, uint32_t type, size_t size) {
2020-11-19 22:19:46 +00:00
for (auto it = npc->viewableChunks->begin(); it != npc->viewableChunks->end(); it++) {
Chunk* chunk = *it;
for (CNSocket *s : chunk->players) {
s->sendPacket(buf, type, size);
}
}
}
static void npcBarkHandler(CNSocket* sock, CNPacketData* data) {
2020-12-15 23:42:22 +00:00
sP_CL2FE_REQ_BARKER* req = (sP_CL2FE_REQ_BARKER*)data->buf;
// get bark IDs from task data
TaskData* td = Missions::Tasks[req->iMissionTaskID];
2020-12-15 23:42:22 +00:00
std::vector<int> barks;
for (int i = 0; i < 4; i++) {
if (td->task["m_iHBarkerTextID"][i] != 0) // non-zeroes only
barks.push_back(td->task["m_iHBarkerTextID"][i]);
}
if (barks.empty())
return; // no barks
INITSTRUCT(sP_FE2CL_REP_BARKER, resp);
resp.iNPC_ID = req->iNPC_ID;
resp.iMissionStringID = barks[rand() % barks.size()];
sock->sendPacket((void*)&resp, P_FE2CL_REP_BARKER, sizeof(sP_FE2CL_REP_BARKER));
}
static void npcUnsummonHandler(CNSocket* sock, CNPacketData* data) {
2020-09-28 18:11:13 +00:00
Player* plr = PlayerManager::getPlayer(sock);
if (plr->accountLevel > 30)
2020-09-18 21:24:15 +00:00
return;
2020-09-18 21:24:15 +00:00
sP_CL2FE_REQ_NPC_UNSUMMON* req = (sP_CL2FE_REQ_NPC_UNSUMMON*)data->buf;
2020-09-25 01:58:20 +00:00
NPCManager::destroyNPC(req->iNPC_ID);
2020-09-18 21:24:15 +00:00
}
// type must already be checked and updateNPCPosition() must be called on the result
BaseNPC *NPCManager::summonNPC(int x, int y, int z, uint64_t instance, int type, bool respawn, bool baseInstance) {
uint64_t inst = baseInstance ? MAPNUM(instance) : instance;
#define EXTRA_HEIGHT 0
assert(nextId < INT32_MAX);
int id = nextId++;
int team = NPCData[type]["m_iTeam"];
BaseNPC *npc = nullptr;
if (team == 2) {
npc = new Mob(x, y, z + EXTRA_HEIGHT, inst, type, NPCData[type], id);
MobAI::Mobs[id] = (Mob*)npc;
// re-enable respawning, if desired
((Mob*)npc)->summoned = !respawn;
} else
npc = new BaseNPC(x, y, z + EXTRA_HEIGHT, 0, inst, type, id);
NPCs[id] = npc;
return npc;
}
static void npcSummonHandler(CNSocket* sock, CNPacketData* data) {
sP_CL2FE_REQ_NPC_SUMMON* req = (sP_CL2FE_REQ_NPC_SUMMON*)data->buf;
Player* plr = PlayerManager::getPlayer(sock);
2020-12-21 00:57:56 +00:00
int limit = NPCData.back()["m_iNpcNumber"];
// permission & sanity check
if (plr->accountLevel > 30 || req->iNPCType > limit || req->iNPCCnt > 100)
return;
for (int i = 0; i < req->iNPCCnt; i++) {
BaseNPC *npc = summonNPC(plr->x, plr->y, plr->z, plr->instanceID, req->iNPCType);
updateNPCPosition(npc->appearanceData.iNPC_ID, plr->x, plr->y, plr->z, plr->instanceID, 0);
}
}
static void handleWarp(CNSocket* sock, int32_t warpId) {
2020-11-17 23:16:16 +00:00
Player* plr = PlayerManager::getPlayer(sock);
// sanity check
if (Warps.find(warpId) == Warps.end())
return;
if (plr->iPCState & 8) {
2020-12-28 16:39:08 +00:00
// remove the player's vehicle
plr->iPCState &= ~8;
// send to self
INITSTRUCT(sP_FE2CL_PC_VEHICLE_OFF_SUCC, off);
sock->sendPacket((void*)&off, P_FE2CL_PC_VEHICLE_OFF_SUCC, sizeof(sP_FE2CL_PC_VEHICLE_OFF_SUCC));
// send to others
INITSTRUCT(sP_FE2CL_PC_STATE_CHANGE, chg);
chg.iPC_ID = plr->iID;
chg.iState = plr->iPCState;
PlayerManager::sendToViewable(sock, (void*)&chg, P_FE2CL_PC_STATE_CHANGE, sizeof(sP_FE2CL_PC_STATE_CHANGE));
}
// std::cerr << "Warped to Map Num:" << Warps[warpId].instanceID << " NPC ID " << Warps[warpId].npcID << std::endl;
if (Warps[warpId].isInstance) {
2020-10-12 16:55:41 +00:00
uint64_t instanceID = Warps[warpId].instanceID;
// if warp requires you to be on a mission, it's gotta be a unique instance
if (Warps[warpId].limitTaskID != 0 || instanceID == 14) { // 14 is a special case for the Time Lab
2020-11-17 23:16:16 +00:00
instanceID += ((uint64_t)plr->iIDGroup << 32); // upper 32 bits are leader ID
Chunking::createInstance(instanceID);
// save Lair entrance coords as a pseudo-Resurrect 'Em
plr->recallX = Warps[warpId].x;
plr->recallY = Warps[warpId].y;
plr->recallZ = Warps[warpId].z + RESURRECT_HEIGHT;
plr->recallInstance = instanceID;
2020-10-12 16:55:41 +00:00
}
2020-11-17 23:16:16 +00:00
if (plr->iID == plr->iIDGroup && plr->groupCnt == 1)
PlayerManager::sendPlayerTo(sock, Warps[warpId].x, Warps[warpId].y, Warps[warpId].z, instanceID);
else {
2020-11-17 23:16:16 +00:00
Player* leaderPlr = PlayerManager::getPlayerFromID(plr->iIDGroup);
for (int i = 0; i < leaderPlr->groupCnt; i++) {
Player* otherPlr = PlayerManager::getPlayerFromID(leaderPlr->groupIDs[i]);
CNSocket* sockTo = PlayerManager::getSockFromID(leaderPlr->groupIDs[i]);
if (otherPlr == nullptr || sockTo == nullptr)
continue;
// save Lair entrance coords for everyone else as well
otherPlr->recallX = Warps[warpId].x;
otherPlr->recallY = Warps[warpId].y;
otherPlr->recallZ = Warps[warpId].z + RESURRECT_HEIGHT;
otherPlr->recallInstance = instanceID;
// remove their vehicle if they're on one
if (otherPlr->iPCState & 8) {
2020-12-28 16:39:08 +00:00
// remove the player's vehicle
otherPlr->iPCState &= ~8;
// send to self
INITSTRUCT(sP_FE2CL_PC_VEHICLE_OFF_SUCC, off);
sockTo->sendPacket((void*)&off, P_FE2CL_PC_VEHICLE_OFF_SUCC, sizeof(sP_FE2CL_PC_VEHICLE_OFF_SUCC));
// send to others
INITSTRUCT(sP_FE2CL_PC_STATE_CHANGE, chg);
chg.iPC_ID = otherPlr->iID;
chg.iState = otherPlr->iPCState;
PlayerManager::sendToViewable(sockTo, (void*)&chg, P_FE2CL_PC_STATE_CHANGE, sizeof(sP_FE2CL_PC_STATE_CHANGE));
}
PlayerManager::sendPlayerTo(sockTo, Warps[warpId].x, Warps[warpId].y, Warps[warpId].z, instanceID);
}
}
}
else
{
2020-11-18 00:07:04 +00:00
INITSTRUCT(sP_FE2CL_REP_PC_WARP_USE_NPC_SUCC, resp); // Can only be used for exiting instances because it sets the instance flag to false
resp.iX = Warps[warpId].x;
resp.iY = Warps[warpId].y;
resp.iZ = Warps[warpId].z;
2020-11-17 23:16:16 +00:00
resp.iCandy = plr->money;
resp.eIL = 4; // do not take away any items
2020-12-31 02:31:46 +00:00
uint64_t fromInstance = plr->instanceID; // pre-warp instance, saved for post-warp
2020-11-17 23:16:16 +00:00
plr->instanceID = INSTANCE_OVERWORLD;
Missions::failInstancedMissions(sock); // fail any instanced missions
sock->sendPacket((void*)&resp, P_FE2CL_REP_PC_WARP_USE_NPC_SUCC, sizeof(sP_FE2CL_REP_PC_WARP_USE_NPC_SUCC));
Chunking::updatePlayerChunk(sock, plr->chunkPos, std::make_tuple(0, 0, 0)); // force player to reload chunks
2020-11-18 00:07:04 +00:00
PlayerManager::updatePlayerPosition(sock, resp.iX, resp.iY, resp.iZ, INSTANCE_OVERWORLD, plr->angle);
// remove the player's ongoing race, if any
if (Racing::EPRaces.find(sock) != Racing::EPRaces.end())
Racing::EPRaces.erase(sock);
2020-12-31 02:31:46 +00:00
// post-warp: check if the source instance has no more players in it and delete it if so
Chunking::destroyInstanceIfEmpty(fromInstance);
}
}
static void npcWarpHandler(CNSocket* sock, CNPacketData* data) {
sP_CL2FE_REQ_PC_WARP_USE_NPC* warpNpc = (sP_CL2FE_REQ_PC_WARP_USE_NPC*)data->buf;
handleWarp(sock, warpNpc->iWarpID);
}
static void npcWarpTimeMachine(CNSocket* sock, CNPacketData* data) {
// this is just a warp request
handleWarp(sock, 28);
}
/*
* Helper function to get NPC closest to coordinates in specified chunks
*/
2020-11-23 00:14:46 +00:00
BaseNPC* NPCManager::getNearestNPC(std::set<Chunk*>* chunks, int X, int Y, int Z) {
BaseNPC* npc = nullptr;
int lastDist = INT_MAX;
2020-11-23 00:14:46 +00:00
for (auto c = chunks->begin(); c != chunks->end(); c++) { // haha get it
Chunk* chunk = *c;
for (auto _npc = chunk->NPCs.begin(); _npc != chunk->NPCs.end(); _npc++) {
BaseNPC* npcTemp = NPCs[*_npc];
int distXY = std::hypot(X - npcTemp->appearanceData.iX, Y - npcTemp->appearanceData.iY);
int dist = std::hypot(distXY, Z - npcTemp->appearanceData.iZ);
if (dist < lastDist) {
npc = npcTemp;
lastDist = dist;
}
}
}
return npc;
}
// TODO: Move this to MobAI, possibly
#pragma region NPCEvents
// summon right arm and stage 2 body
static void lordFuseStageTwo(CNSocket *sock, BaseNPC *npc) {
Mob *oldbody = (Mob*)npc; // adaptium, stun
Player *plr = PlayerManager::getPlayer(sock);
std::cout << "Lord Fuse stage two" << std::endl;
// Fuse doesn't move; spawnX, etc. is shorter to write than *appearanceData*
// Blastons, Heal
Mob *newbody = (Mob*)NPCManager::summonNPC(oldbody->spawnX, oldbody->spawnY, oldbody->spawnZ, plr->instanceID, 2467);
newbody->appearanceData.iAngle = oldbody->appearanceData.iAngle;
NPCManager::updateNPCPosition(newbody->appearanceData.iNPC_ID, newbody->spawnX, newbody->spawnY, newbody->spawnZ,
plr->instanceID, oldbody->appearanceData.iAngle);
// right arm, Adaptium, Stun
Mob *arm = (Mob*)NPCManager::summonNPC(oldbody->spawnX - 600, oldbody->spawnY, oldbody->spawnZ, plr->instanceID, 2469);
arm->appearanceData.iAngle = oldbody->appearanceData.iAngle;
NPCManager::updateNPCPosition(arm->appearanceData.iNPC_ID, arm->spawnX, arm->spawnY, arm->spawnZ,
plr->instanceID, oldbody->appearanceData.iAngle);
}
// summon left arm and stage 3 body
static void lordFuseStageThree(CNSocket *sock, BaseNPC *npc) {
Mob *oldbody = (Mob*)npc;
Player *plr = PlayerManager::getPlayer(sock);
std::cout << "Lord Fuse stage three" << std::endl;
// Cosmix, Damage Point
Mob *newbody = (Mob*)NPCManager::summonNPC(oldbody->spawnX, oldbody->spawnY, oldbody->spawnZ, plr->instanceID, 2468);
newbody->appearanceData.iAngle = oldbody->appearanceData.iAngle;
NPCManager::updateNPCPosition(newbody->appearanceData.iNPC_ID, newbody->spawnX, newbody->spawnY, newbody->spawnZ,
plr->instanceID, oldbody->appearanceData.iAngle);
// Blastons, Heal
Mob *arm = (Mob*)NPCManager::summonNPC(oldbody->spawnX + 600, oldbody->spawnY, oldbody->spawnZ, plr->instanceID, 2470);
arm->appearanceData.iAngle = oldbody->appearanceData.iAngle;
NPCManager::updateNPCPosition(arm->appearanceData.iNPC_ID, arm->spawnX, arm->spawnY, arm->spawnZ,
plr->instanceID, oldbody->appearanceData.iAngle);
}
std::vector<NPCEvent> NPCManager::NPCEvents = {
NPCEvent(2466, ON_KILLED, lordFuseStageTwo),
NPCEvent(2467, ON_KILLED, lordFuseStageThree),
};
#pragma endregion NPCEvents
void NPCManager::init() {
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_WARP_USE_NPC, npcWarpHandler);
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_TIME_TO_GO_WARP, npcWarpTimeMachine);
REGISTER_SHARD_PACKET(P_CL2FE_REQ_NPC_SUMMON, npcSummonHandler);
REGISTER_SHARD_PACKET(P_CL2FE_REQ_NPC_UNSUMMON, npcUnsummonHandler);
REGISTER_SHARD_PACKET(P_CL2FE_REQ_BARKER, npcBarkHandler);
}