2020-08-19 22:21:35 +00:00
|
|
|
#include "NPCManager.hpp"
|
2020-09-09 01:15:25 +00:00
|
|
|
#include "ItemManager.hpp"
|
2020-08-20 21:43:48 +00:00
|
|
|
#include "settings.hpp"
|
2020-09-16 23:43:48 +00:00
|
|
|
#include "MobManager.hpp"
|
2020-10-12 23:29:00 +00:00
|
|
|
#include "MissionManager.hpp"
|
2020-10-13 03:42:47 +00:00
|
|
|
#include "ChunkManager.hpp"
|
2020-10-22 08:23:12 +00:00
|
|
|
#include "NanoManager.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>
|
2020-08-25 01:34:53 +00:00
|
|
|
#include <vector>
|
2020-09-24 22:36:25 +00:00
|
|
|
#include <assert.h>
|
2020-10-11 03:12:12 +00:00
|
|
|
#include <limits.h>
|
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
|
|
|
|
2020-09-16 23: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-10-22 09:28:18 +00:00
|
|
|
/// sock, CBFlag -> until
|
2020-10-22 09:09:50 +00:00
|
|
|
std::map<std::pair<CNSocket*, int32_t>, time_t> NPCManager::EggBuffs;
|
2020-09-24 22:36:25 +00:00
|
|
|
nlohmann::json NPCManager::NPCData;
|
2020-08-19 22:21:35 +00:00
|
|
|
|
2020-09-24 22:36:25 +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-08-19 22:21:35 +00:00
|
|
|
void NPCManager::init() {
|
2020-08-25 02:28:42 +00:00
|
|
|
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_WARP_USE_NPC, npcWarpHandler);
|
2020-09-25 05:35:27 +00:00
|
|
|
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_TIME_TO_GO_WARP, npcWarpTimeMachine);
|
2020-08-25 02:28:42 +00:00
|
|
|
REGISTER_SHARD_PACKET(P_CL2FE_REQ_NPC_SUMMON, npcSummonHandler);
|
2020-09-18 21:24:15 +00:00
|
|
|
REGISTER_SHARD_PACKET(P_CL2FE_REQ_NPC_UNSUMMON, npcUnsummonHandler);
|
2020-08-29 11:14:21 +00:00
|
|
|
REGISTER_SHARD_PACKET(P_CL2FE_REQ_BARKER, npcBarkHandler);
|
2020-09-09 01:15:25 +00:00
|
|
|
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_VENDOR_START, npcVendorStart);
|
|
|
|
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_VENDOR_TABLE_UPDATE, npcVendorTable);
|
|
|
|
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_VENDOR_ITEM_BUY, npcVendorBuy);
|
2020-09-12 02:04:23 +00:00
|
|
|
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_VENDOR_ITEM_SELL, npcVendorSell);
|
2020-09-14 14:27:52 +00:00
|
|
|
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_VENDOR_ITEM_RESTORE_BUY, npcVendorBuyback);
|
|
|
|
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_VENDOR_BATTERY_BUY, npcVendorBuyBattery);
|
2020-09-16 00:30:01 +00:00
|
|
|
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_ITEM_COMBINATION, npcCombineItems);
|
2020-10-22 09:09:50 +00:00
|
|
|
|
|
|
|
REGISTER_SHARD_TIMER(buffStep, 1000);
|
2020-09-09 01:15:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 01:58:20 +00:00
|
|
|
void NPCManager::removeNPC(std::vector<Chunk*> viewableChunks, int32_t id) {
|
|
|
|
BaseNPC* npc = NPCs[id];
|
|
|
|
|
|
|
|
switch (npc->npcClass) {
|
|
|
|
case NPC_BUS:
|
|
|
|
INITSTRUCT(sP_FE2CL_TRANSPORTATION_EXIT, exitBusData);
|
|
|
|
exitBusData.eTT = 3;
|
|
|
|
exitBusData.iT_ID = id;
|
|
|
|
|
|
|
|
for (Chunk* chunk : viewableChunks) {
|
|
|
|
for (CNSocket* sock : chunk->players) {
|
|
|
|
// send to socket
|
|
|
|
sock->sendPacket((void*)&exitBusData, P_FE2CL_TRANSPORTATION_EXIT, sizeof(sP_FE2CL_TRANSPORTATION_EXIT));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// create struct
|
|
|
|
INITSTRUCT(sP_FE2CL_NPC_EXIT, exitData);
|
|
|
|
exitData.iNPC_ID = id;
|
|
|
|
|
|
|
|
// remove it from the clients
|
|
|
|
for (Chunk* chunk : viewableChunks) {
|
|
|
|
for (CNSocket* sock : chunk->players) {
|
|
|
|
// send to socket
|
|
|
|
sock->sendPacket((void*)&exitData, P_FE2CL_NPC_EXIT, sizeof(sP_FE2CL_NPC_EXIT));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-17 22:45:43 +00:00
|
|
|
void NPCManager::addNPC(std::vector<Chunk*> viewableChunks, int32_t id) {
|
|
|
|
BaseNPC* npc = NPCs[id];
|
|
|
|
|
2020-09-23 04:08:51 +00:00
|
|
|
switch (npc->npcClass) {
|
|
|
|
case NPC_BUS:
|
|
|
|
INITSTRUCT(sP_FE2CL_TRANSPORTATION_ENTER, enterBusData);
|
|
|
|
enterBusData.AppearanceData = { 3, npc->appearanceData.iNPC_ID, npc->appearanceData.iNPCType, npc->appearanceData.iX, npc->appearanceData.iY, npc->appearanceData.iZ };
|
|
|
|
|
|
|
|
for (Chunk* chunk : viewableChunks) {
|
|
|
|
for (CNSocket* sock : chunk->players) {
|
|
|
|
// send to socket
|
|
|
|
sock->sendPacket((void*)&enterBusData, P_FE2CL_TRANSPORTATION_ENTER, sizeof(sP_FE2CL_TRANSPORTATION_ENTER));
|
|
|
|
}
|
2020-09-17 22:45:43 +00:00
|
|
|
}
|
2020-09-23 04:08:51 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// create struct
|
|
|
|
INITSTRUCT(sP_FE2CL_NPC_ENTER, enterData);
|
|
|
|
enterData.NPCAppearanceData = npc->appearanceData;
|
|
|
|
|
|
|
|
for (Chunk* chunk : viewableChunks) {
|
|
|
|
for (CNSocket* sock : chunk->players) {
|
|
|
|
// send to socket
|
|
|
|
sock->sendPacket((void*)&enterData, P_FE2CL_NPC_ENTER, sizeof(sP_FE2CL_NPC_ENTER));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2020-09-17 22:45:43 +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()) {
|
2020-10-20 22:55:58 +00:00
|
|
|
std::cout << "npc not found: " << id << std::endl;
|
2020-09-18 21:24:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BaseNPC* entity = NPCs[id];
|
|
|
|
|
|
|
|
// sanity check
|
2020-09-25 01:58:20 +00:00
|
|
|
if (ChunkManager::chunks.find(entity->chunkPos) == ChunkManager::chunks.end()) {
|
2020-09-18 21:24:15 +00:00
|
|
|
std::cout << "chunk not found!" << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove NPC from the chunk
|
2020-09-25 01:58:20 +00:00
|
|
|
Chunk* chunk = ChunkManager::chunks[entity->chunkPos];
|
2020-09-18 21:24:15 +00:00
|
|
|
chunk->NPCs.erase(id);
|
2020-10-05 00:03:13 +00:00
|
|
|
|
2020-09-25 01:58:20 +00:00
|
|
|
// remove from viewable chunks
|
|
|
|
removeNPC(entity->currentChunks, id);
|
2020-09-23 04:08:51 +00:00
|
|
|
|
2020-09-25 01:58:20 +00:00
|
|
|
// remove from mob manager
|
|
|
|
if (MobManager::Mobs.find(id) != MobManager::Mobs.end())
|
2020-10-15 02:36:38 +00:00
|
|
|
MobManager::Mobs.erase(id);
|
2020-10-05 00:03:13 +00:00
|
|
|
|
2020-09-18 21:24:15 +00:00
|
|
|
// finally, remove it from the map and free it
|
|
|
|
NPCs.erase(id);
|
|
|
|
delete entity;
|
2020-09-17 22:45:43 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 21:57:46 +00:00
|
|
|
void NPCManager::updateNPCPosition(int32_t id, int X, int Y, int Z, int angle) {
|
|
|
|
NPCs[id]->appearanceData.iAngle = angle;
|
|
|
|
updateNPCPosition(id, X, Y, Z);
|
|
|
|
}
|
|
|
|
|
2020-09-23 14:53:06 +00:00
|
|
|
void NPCManager::updateNPCPosition(int32_t id, int X, int Y, int Z) {
|
|
|
|
BaseNPC* npc = NPCs[id];
|
|
|
|
|
|
|
|
npc->appearanceData.iX = X;
|
|
|
|
npc->appearanceData.iY = Y;
|
|
|
|
npc->appearanceData.iZ = Z;
|
2020-10-12 16:55:41 +00:00
|
|
|
std::tuple<int, int, uint64_t> newPos = ChunkManager::grabChunk(X, Y, npc->instanceID);
|
2020-09-23 14:53:06 +00:00
|
|
|
|
2020-09-25 01:58:20 +00:00
|
|
|
// nothing to be done (but we should also update currentChunks to add/remove stale chunks)
|
|
|
|
if (newPos == npc->chunkPos) {
|
|
|
|
npc->currentChunks = ChunkManager::grabChunks(newPos);
|
2020-09-23 14:53:06 +00:00
|
|
|
return;
|
2020-09-25 01:58:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Chunk*> allChunks = ChunkManager::grabChunks(newPos);
|
|
|
|
|
|
|
|
// send npc exit to stale chunks
|
|
|
|
removeNPC(ChunkManager::getDeltaChunks(npc->currentChunks, allChunks), id);
|
|
|
|
|
|
|
|
// send npc enter to new chunks
|
|
|
|
addNPC(ChunkManager::getDeltaChunks(allChunks, npc->currentChunks), id);
|
|
|
|
|
2020-10-15 02:36:38 +00:00
|
|
|
Chunk *chunk = nullptr;
|
|
|
|
if (ChunkManager::checkChunk(npc->chunkPos))
|
|
|
|
chunk = ChunkManager::chunks[npc->chunkPos];
|
|
|
|
|
|
|
|
if (ChunkManager::removeNPC(npc->chunkPos, id)) {
|
|
|
|
// if the old chunk was deallocated, remove it
|
|
|
|
allChunks.erase(std::remove(allChunks.begin(), allChunks.end(), chunk), allChunks.end());
|
|
|
|
}
|
|
|
|
|
2020-10-01 01:44:37 +00:00
|
|
|
ChunkManager::addNPC(X, Y, npc->instanceID, id);
|
2020-09-23 14:53:06 +00:00
|
|
|
|
2020-09-25 01:58:20 +00:00
|
|
|
npc->chunkPos = newPos;
|
|
|
|
npc->currentChunks = allChunks;
|
2020-09-23 14:53:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 16:55:41 +00:00
|
|
|
void NPCManager::updateNPCInstance(int32_t npcID, uint64_t instanceID) {
|
2020-10-02 20:17:24 +00:00
|
|
|
BaseNPC* npc = NPCs[npcID];
|
|
|
|
npc->instanceID = instanceID;
|
|
|
|
updateNPCPosition(npcID, npc->appearanceData.iX, npc->appearanceData.iY, npc->appearanceData.iZ);
|
|
|
|
}
|
|
|
|
|
2020-09-25 00:00:26 +00:00
|
|
|
void NPCManager::sendToViewable(BaseNPC *npc, void *buf, uint32_t type, size_t size) {
|
|
|
|
for (Chunk *chunk : npc->currentChunks) {
|
|
|
|
for (CNSocket *s : chunk->players) {
|
|
|
|
s->sendPacket(buf, type, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-09 01:15:25 +00:00
|
|
|
void NPCManager::npcVendorBuy(CNSocket* sock, CNPacketData* data) {
|
|
|
|
if (data->size != sizeof(sP_CL2FE_REQ_PC_VENDOR_ITEM_BUY))
|
|
|
|
return; // malformed packet
|
|
|
|
|
|
|
|
sP_CL2FE_REQ_PC_VENDOR_ITEM_BUY* req = (sP_CL2FE_REQ_PC_VENDOR_ITEM_BUY*)data->buf;
|
|
|
|
Player *plr = PlayerManager::getPlayer(sock);
|
2020-09-28 18:11:13 +00:00
|
|
|
|
|
|
|
if (plr == nullptr)
|
|
|
|
return;
|
|
|
|
|
2020-09-14 14:27:52 +00:00
|
|
|
Item* item = ItemManager::getItemData(req->Item.iID, req->Item.iType);
|
2020-09-09 01:15:25 +00:00
|
|
|
|
2020-09-14 14:27:52 +00:00
|
|
|
if (item == nullptr) {
|
2020-09-14 05:27:20 +00:00
|
|
|
std::cout << "[WARN] Item id " << req->Item.iID << " with type " << req->Item.iType << " not found (buy)" << std::endl;
|
2020-09-14 04:25:14 +00:00
|
|
|
// NOTE: VENDOR_ITEM_BUY_FAIL is not actually handled client-side.
|
|
|
|
INITSTRUCT(sP_FE2CL_REP_PC_VENDOR_ITEM_BUY_FAIL, failResp);
|
|
|
|
failResp.iErrorCode = 0;
|
|
|
|
sock->sendPacket((void*)&failResp, P_FE2CL_REP_PC_VENDOR_ITEM_BUY_FAIL, sizeof(sP_FE2CL_REP_PC_VENDOR_ITEM_BUY_FAIL));
|
|
|
|
return;
|
|
|
|
}
|
2020-09-09 01:15:25 +00:00
|
|
|
|
2020-09-30 17:30:19 +00:00
|
|
|
int itemCost = item->buyPrice * (item->stackSize > 1 ? req->Item.iOpt : 1);
|
2020-09-09 01:15:25 +00:00
|
|
|
int slot = ItemManager::findFreeSlot(plr);
|
|
|
|
if (itemCost > plr->money || slot == -1) {
|
|
|
|
// NOTE: VENDOR_ITEM_BUY_FAIL is not actually handled client-side.
|
|
|
|
INITSTRUCT(sP_FE2CL_REP_PC_VENDOR_ITEM_BUY_FAIL, failResp);
|
|
|
|
failResp.iErrorCode = 0;
|
|
|
|
sock->sendPacket((void*)&failResp, P_FE2CL_REP_PC_VENDOR_ITEM_BUY_FAIL, sizeof(sP_FE2CL_REP_PC_VENDOR_ITEM_BUY_FAIL));
|
|
|
|
return;
|
|
|
|
}
|
2020-09-22 11:16:09 +00:00
|
|
|
// if vehicle
|
|
|
|
if (req->Item.iType == 10)
|
|
|
|
// set time limit: current time + 7days
|
2020-09-23 09:05:18 +00:00
|
|
|
req->Item.iTimeLimit = getTimestamp() + 604800;
|
2020-09-09 01:15:25 +00:00
|
|
|
|
|
|
|
if (slot != req->iInvenSlotNum) {
|
|
|
|
// possible item stacking?
|
2020-09-12 00:25:45 +00:00
|
|
|
std::cout << "[WARN] Client and server disagree on bought item slot (" << req->iInvenSlotNum << " vs " << slot << ")" << std::endl;
|
2020-09-09 01:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
INITSTRUCT(sP_FE2CL_REP_PC_VENDOR_ITEM_BUY_SUCC, resp);
|
|
|
|
|
|
|
|
plr->money = plr->money - itemCost;
|
|
|
|
plr->Inven[slot] = req->Item;
|
|
|
|
|
|
|
|
resp.iCandy = plr->money;
|
|
|
|
resp.iInvenSlotNum = slot;
|
|
|
|
resp.Item = req->Item;
|
|
|
|
|
|
|
|
sock->sendPacket((void*)&resp, P_FE2CL_REP_PC_VENDOR_ITEM_BUY_SUCC, sizeof(sP_FE2CL_REP_PC_VENDOR_ITEM_BUY_SUCC));
|
|
|
|
}
|
|
|
|
|
2020-09-12 02:04:23 +00:00
|
|
|
void NPCManager::npcVendorSell(CNSocket* sock, CNPacketData* data) {
|
|
|
|
if (data->size != sizeof(sP_CL2FE_REQ_PC_VENDOR_ITEM_SELL))
|
|
|
|
return; // malformed packet
|
|
|
|
|
|
|
|
sP_CL2FE_REQ_PC_VENDOR_ITEM_SELL* req = (sP_CL2FE_REQ_PC_VENDOR_ITEM_SELL*)data->buf;
|
|
|
|
Player* plr = PlayerManager::getPlayer(sock);
|
|
|
|
|
2020-09-28 18:11:13 +00:00
|
|
|
if (plr == nullptr)
|
|
|
|
return;
|
|
|
|
|
2020-09-12 14:10:53 +00:00
|
|
|
if (req->iInvenSlotNum < 0 || req->iInvenSlotNum >= AINVEN_COUNT || req->iItemCnt < 0) {
|
2020-09-12 02:04:23 +00:00
|
|
|
std::cout << "[WARN] Client failed to sell item in slot " << req->iInvenSlotNum << std::endl;
|
|
|
|
INITSTRUCT(sP_FE2CL_REP_PC_VENDOR_ITEM_SELL_FAIL, failResp);
|
|
|
|
failResp.iErrorCode = 0;
|
|
|
|
sock->sendPacket((void*)&failResp, P_FE2CL_REP_PC_VENDOR_ITEM_SELL_FAIL, sizeof(sP_FE2CL_REP_PC_VENDOR_ITEM_SELL_FAIL));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-12 14:10:53 +00:00
|
|
|
sItemBase* item = &plr->Inven[req->iInvenSlotNum];
|
2020-09-14 14:27:52 +00:00
|
|
|
Item* itemData = ItemManager::getItemData(item->iID, item->iType);
|
2020-09-12 14:10:53 +00:00
|
|
|
|
2020-09-14 14:27:52 +00:00
|
|
|
if (itemData == nullptr || !itemData->sellable) { // sanity + sellable check
|
2020-09-14 05:27:20 +00:00
|
|
|
std::cout << "[WARN] Item id " << item->iID << " with type " << item->iType << " not found (sell)" << std::endl;
|
|
|
|
INITSTRUCT(sP_FE2CL_REP_PC_VENDOR_ITEM_SELL_FAIL, failResp);
|
|
|
|
failResp.iErrorCode = 0;
|
|
|
|
sock->sendPacket((void*)&failResp, P_FE2CL_REP_PC_VENDOR_ITEM_SELL_FAIL, sizeof(sP_FE2CL_REP_PC_VENDOR_ITEM_SELL_FAIL));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sItemBase original;
|
|
|
|
memcpy(&original, item, sizeof(sItemBase));
|
|
|
|
|
2020-09-12 02:04:23 +00:00
|
|
|
INITSTRUCT(sP_FE2CL_REP_PC_VENDOR_ITEM_SELL_SUCC, resp);
|
2020-10-05 00:03:13 +00:00
|
|
|
|
2020-09-14 14:27:52 +00:00
|
|
|
int sellValue = itemData->sellPrice * req->iItemCnt;
|
2020-09-12 02:04:23 +00:00
|
|
|
|
|
|
|
// increment taros
|
|
|
|
plr->money = plr->money + sellValue;
|
|
|
|
|
2020-09-14 05:52:15 +00:00
|
|
|
// modify item
|
|
|
|
if (plr->Inven[req->iInvenSlotNum].iOpt - req->iItemCnt > 0) { // selling part of a stack
|
|
|
|
item->iOpt -= req->iItemCnt;
|
|
|
|
original.iOpt = req->iItemCnt;
|
2020-10-19 17:26:14 +00:00
|
|
|
} else { // selling entire slot
|
2020-09-14 05:52:15 +00:00
|
|
|
item->iID = 0;
|
|
|
|
item->iOpt = 0;
|
|
|
|
item->iType = 0;
|
|
|
|
item->iTimeLimit = 0;
|
|
|
|
}
|
2020-09-12 02:04:23 +00:00
|
|
|
|
|
|
|
// response parameters
|
|
|
|
resp.iInvenSlotNum = req->iInvenSlotNum;
|
|
|
|
resp.iCandy = plr->money;
|
2020-09-14 05:27:20 +00:00
|
|
|
resp.Item = original; // the item that gets sent to buyback
|
|
|
|
resp.ItemStay = *item; // the void item that gets put in the slot
|
2020-09-12 02:04:23 +00:00
|
|
|
|
|
|
|
sock->sendPacket((void*)&resp, P_FE2CL_REP_PC_VENDOR_ITEM_SELL_SUCC, sizeof(sP_FE2CL_REP_PC_VENDOR_ITEM_SELL_SUCC));
|
|
|
|
}
|
|
|
|
|
2020-09-14 14:27:52 +00:00
|
|
|
void NPCManager::npcVendorBuyback(CNSocket* sock, CNPacketData* data) {
|
2020-09-14 05:27:20 +00:00
|
|
|
if (data->size != sizeof(sP_CL2FE_REQ_PC_VENDOR_ITEM_RESTORE_BUY))
|
|
|
|
return; // malformed packet
|
|
|
|
|
|
|
|
sP_CL2FE_REQ_PC_VENDOR_ITEM_RESTORE_BUY* req = (sP_CL2FE_REQ_PC_VENDOR_ITEM_RESTORE_BUY*)data->buf;
|
|
|
|
Player* plr = PlayerManager::getPlayer(sock);
|
2020-09-28 18:11:13 +00:00
|
|
|
|
|
|
|
if (plr == nullptr)
|
|
|
|
return;
|
|
|
|
|
2020-09-14 14:27:52 +00:00
|
|
|
Item* item = ItemManager::getItemData(req->Item.iID, req->Item.iType);
|
2020-09-14 05:27:20 +00:00
|
|
|
|
2020-09-14 14:27:52 +00:00
|
|
|
if (item == nullptr) {
|
2020-09-14 05:27:20 +00:00
|
|
|
std::cout << "[WARN] Item id " << req->Item.iID << " with type " << req->Item.iType << " not found (rebuy)" << std::endl;
|
|
|
|
// NOTE: VENDOR_ITEM_BUY_FAIL is not actually handled client-side.
|
|
|
|
INITSTRUCT(sP_FE2CL_REP_PC_VENDOR_ITEM_RESTORE_BUY_FAIL, failResp);
|
|
|
|
failResp.iErrorCode = 0;
|
|
|
|
sock->sendPacket((void*)&failResp, P_FE2CL_REP_PC_VENDOR_ITEM_RESTORE_BUY_FAIL, sizeof(sP_FE2CL_REP_PC_VENDOR_ITEM_RESTORE_BUY_FAIL));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-14 14:27:52 +00:00
|
|
|
// sell price is used on rebuy. ternary identifies stacked items
|
|
|
|
int itemCost = item->sellPrice * (item->stackSize > 1 ? req->Item.iOpt : 1);
|
2020-09-14 05:27:20 +00:00
|
|
|
int slot = ItemManager::findFreeSlot(plr);
|
|
|
|
if (itemCost > plr->money || slot == -1) {
|
|
|
|
// NOTE: VENDOR_ITEM_BUY_FAIL is not actually handled client-side.
|
|
|
|
INITSTRUCT(sP_FE2CL_REP_PC_VENDOR_ITEM_RESTORE_BUY_FAIL, failResp);
|
|
|
|
failResp.iErrorCode = 0;
|
|
|
|
sock->sendPacket((void*)&failResp, P_FE2CL_REP_PC_VENDOR_ITEM_RESTORE_BUY_FAIL, sizeof(sP_FE2CL_REP_PC_VENDOR_ITEM_RESTORE_BUY_FAIL));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (slot != req->iInvenSlotNum) {
|
|
|
|
// possible item stacking?
|
|
|
|
std::cout << "[WARN] Client and server disagree on bought item slot (" << req->iInvenSlotNum << " vs " << slot << ")" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
plr->money = plr->money - itemCost;
|
|
|
|
plr->Inven[slot] = req->Item;
|
|
|
|
|
|
|
|
INITSTRUCT(sP_FE2CL_REP_PC_VENDOR_ITEM_RESTORE_BUY_SUCC, resp);
|
|
|
|
// response parameters
|
|
|
|
resp.iCandy = plr->money;
|
|
|
|
resp.iInvenSlotNum = slot;
|
|
|
|
resp.Item = req->Item;
|
|
|
|
|
|
|
|
sock->sendPacket((void*)&resp, P_FE2CL_REP_PC_VENDOR_ITEM_RESTORE_BUY_SUCC, sizeof(sP_FE2CL_REP_PC_VENDOR_ITEM_RESTORE_BUY_SUCC));
|
|
|
|
}
|
|
|
|
|
2020-09-09 01:15:25 +00:00
|
|
|
void NPCManager::npcVendorTable(CNSocket* sock, CNPacketData* data) {
|
|
|
|
if (data->size != sizeof(sP_CL2FE_REQ_PC_VENDOR_TABLE_UPDATE))
|
|
|
|
return; // malformed packet
|
|
|
|
|
2020-09-13 22:54:47 +00:00
|
|
|
sP_CL2FE_REQ_PC_VENDOR_TABLE_UPDATE* req = (sP_CL2FE_REQ_PC_VENDOR_TABLE_UPDATE*)data->buf;
|
2020-10-05 00:03:13 +00:00
|
|
|
|
2020-09-13 22:54:47 +00:00
|
|
|
if (req->iVendorID != req->iNPC_ID || ItemManager::VendorTables.find(req->iNPC_ID) == ItemManager::VendorTables.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::vector<VendorListing> listings = ItemManager::VendorTables[req->iNPC_ID]; // maybe use iVendorID instead...?
|
|
|
|
|
2020-09-09 01:15:25 +00:00
|
|
|
INITSTRUCT(sP_FE2CL_REP_PC_VENDOR_TABLE_UPDATE_SUCC, resp);
|
2020-09-13 22:54:47 +00:00
|
|
|
|
2020-10-07 17:03:00 +00:00
|
|
|
for (int i = 0; i < (int)listings.size() && i < 20; i++) { // 20 is the max
|
2020-09-14 14:27:52 +00:00
|
|
|
sItemBase base;
|
|
|
|
base.iID = listings[i].iID;
|
|
|
|
base.iOpt = 0;
|
|
|
|
base.iTimeLimit = 0;
|
|
|
|
base.iType = listings[i].type;
|
2020-09-13 22:54:47 +00:00
|
|
|
|
2020-09-14 14:27:52 +00:00
|
|
|
sItemVendor vItem;
|
|
|
|
vItem.item = base;
|
|
|
|
vItem.iSortNum = listings[i].sort;
|
|
|
|
vItem.iVendorID = req->iVendorID;
|
2020-10-19 17:26:14 +00:00
|
|
|
//vItem.fBuyCost = listings[i].price; // this value is not actually the one that is used
|
2020-09-13 22:54:47 +00:00
|
|
|
|
2020-09-14 14:27:52 +00:00
|
|
|
resp.item[i] = vItem;
|
2020-09-13 22:54:47 +00:00
|
|
|
}
|
2020-09-09 01:15:25 +00:00
|
|
|
|
|
|
|
sock->sendPacket((void*)&resp, P_FE2CL_REP_PC_VENDOR_TABLE_UPDATE_SUCC, sizeof(sP_FE2CL_REP_PC_VENDOR_TABLE_UPDATE_SUCC));
|
|
|
|
}
|
|
|
|
|
|
|
|
void NPCManager::npcVendorStart(CNSocket* sock, CNPacketData* data) {
|
|
|
|
if (data->size != sizeof(sP_CL2FE_REQ_PC_VENDOR_START))
|
|
|
|
return; // malformed packet
|
|
|
|
|
|
|
|
sP_CL2FE_REQ_PC_VENDOR_START* req = (sP_CL2FE_REQ_PC_VENDOR_START*)data->buf;
|
|
|
|
INITSTRUCT(sP_FE2CL_REP_PC_VENDOR_START_SUCC, resp);
|
|
|
|
|
|
|
|
resp.iNPC_ID = req->iNPC_ID;
|
|
|
|
resp.iVendorID = req->iVendorID;
|
|
|
|
|
|
|
|
sock->sendPacket((void*)&resp, P_FE2CL_REP_PC_VENDOR_START_SUCC, sizeof(sP_FE2CL_REP_PC_VENDOR_START_SUCC));
|
2020-08-20 21:43:48 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 14:27:52 +00:00
|
|
|
void NPCManager::npcVendorBuyBattery(CNSocket* sock, CNPacketData* data) {
|
|
|
|
if (data->size != sizeof(sP_CL2FE_REQ_PC_VENDOR_BATTERY_BUY))
|
|
|
|
return; // malformed packet
|
|
|
|
|
|
|
|
sP_CL2FE_REQ_PC_VENDOR_BATTERY_BUY* req = (sP_CL2FE_REQ_PC_VENDOR_BATTERY_BUY*)data->buf;
|
|
|
|
Player* plr = PlayerManager::getPlayer(sock);
|
|
|
|
|
2020-09-28 18:11:13 +00:00
|
|
|
if (plr == nullptr)
|
|
|
|
return;
|
|
|
|
|
2020-10-28 21:05:01 +00:00
|
|
|
int cost = req->Item.iOpt * 100;
|
2020-09-14 14:27:52 +00:00
|
|
|
if ((req->Item.iID == 3 ? (plr->batteryW >= 9999) : (plr->batteryN >= 9999)) || plr->money < cost) { // sanity check
|
|
|
|
INITSTRUCT(sP_FE2CL_REP_PC_VENDOR_BATTERY_BUY_FAIL, failResp);
|
|
|
|
failResp.iErrorCode = 0;
|
|
|
|
sock->sendPacket((void*)&failResp, P_FE2CL_REP_PC_VENDOR_BATTERY_BUY_FAIL, sizeof(sP_FE2CL_REP_PC_VENDOR_BATTERY_BUY_FAIL));
|
|
|
|
}
|
|
|
|
|
2020-10-28 21:05:01 +00:00
|
|
|
cost = plr->batteryW + plr->batteryN;
|
|
|
|
plr->batteryW += req->Item.iID == 3 ? req->Item.iOpt * 100 : 0;
|
|
|
|
plr->batteryN += req->Item.iID == 4 ? req->Item.iOpt * 100 : 0;
|
2020-09-14 14:27:52 +00:00
|
|
|
|
|
|
|
// caps
|
|
|
|
if (plr->batteryW > 9999)
|
|
|
|
plr->batteryW = 9999;
|
|
|
|
if (plr->batteryN > 9999)
|
|
|
|
plr->batteryN = 9999;
|
|
|
|
|
2020-10-28 21:05:01 +00:00
|
|
|
cost = plr->batteryW + plr->batteryN - cost;
|
|
|
|
plr->money -= cost;
|
|
|
|
|
2020-09-14 14:27:52 +00:00
|
|
|
INITSTRUCT(sP_FE2CL_REP_PC_VENDOR_BATTERY_BUY_SUCC, resp);
|
|
|
|
|
|
|
|
resp.iCandy = plr->money;
|
|
|
|
resp.iBatteryW = plr->batteryW;
|
|
|
|
resp.iBatteryN = plr->batteryN;
|
|
|
|
|
|
|
|
sock->sendPacket((void*)&resp, P_FE2CL_REP_PC_VENDOR_BATTERY_BUY_SUCC, sizeof(sP_FE2CL_REP_PC_VENDOR_BATTERY_BUY_SUCC));
|
|
|
|
}
|
|
|
|
|
2020-09-16 00:30:01 +00:00
|
|
|
void NPCManager::npcCombineItems(CNSocket* sock, CNPacketData* data) {
|
|
|
|
if (data->size != sizeof(sP_CL2FE_REQ_PC_ITEM_COMBINATION))
|
|
|
|
return; // malformed packet
|
|
|
|
|
|
|
|
sP_CL2FE_REQ_PC_ITEM_COMBINATION* req = (sP_CL2FE_REQ_PC_ITEM_COMBINATION*)data->buf;
|
|
|
|
Player* plr = PlayerManager::getPlayer(sock);
|
2020-09-28 18:11:13 +00:00
|
|
|
|
|
|
|
if (plr == nullptr)
|
|
|
|
return;
|
2020-10-05 00:03:13 +00:00
|
|
|
|
2020-09-16 00:30:01 +00:00
|
|
|
if (req->iCostumeItemSlot < 0 || req->iCostumeItemSlot >= AINVEN_COUNT || req->iStatItemSlot < 0 || req->iStatItemSlot >= AINVEN_COUNT) { // sanity check 1
|
|
|
|
INITSTRUCT(sP_FE2CL_REP_PC_ITEM_COMBINATION_FAIL, failResp);
|
|
|
|
failResp.iCostumeItemSlot = req->iCostumeItemSlot;
|
|
|
|
failResp.iStatItemSlot = req->iStatItemSlot;
|
|
|
|
failResp.iErrorCode = 0;
|
|
|
|
sock->sendPacket((void*)&failResp, P_FE2CL_REP_PC_ITEM_COMBINATION_FAIL, sizeof(sP_FE2CL_REP_PC_ITEM_COMBINATION_FAIL));
|
|
|
|
std::cout << "[WARN] Inventory slot(s) out of range (" << req->iStatItemSlot << " and " << req->iCostumeItemSlot << ")" << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sItemBase* itemStats = &plr->Inven[req->iStatItemSlot];
|
|
|
|
sItemBase* itemLooks = &plr->Inven[req->iCostumeItemSlot];
|
|
|
|
Item* itemStatsDat = ItemManager::getItemData(itemStats->iID, itemStats->iType);
|
|
|
|
Item* itemLooksDat = ItemManager::getItemData(itemLooks->iID, itemLooks->iType);
|
|
|
|
|
|
|
|
if (itemStatsDat == nullptr || itemLooksDat == nullptr
|
2020-10-19 17:26:14 +00:00
|
|
|
|| ItemManager::CrocPotTable.find(abs(itemStatsDat->level - itemLooksDat->level)) == ItemManager::CrocPotTable.end()) { // sanity check 2
|
2020-09-16 00:30:01 +00:00
|
|
|
INITSTRUCT(sP_FE2CL_REP_PC_ITEM_COMBINATION_FAIL, failResp);
|
|
|
|
failResp.iCostumeItemSlot = req->iCostumeItemSlot;
|
|
|
|
failResp.iStatItemSlot = req->iStatItemSlot;
|
|
|
|
failResp.iErrorCode = 0;
|
|
|
|
std::cout << "[WARN] Either item ids or croc pot value set not found" << std::endl;
|
|
|
|
sock->sendPacket((void*)&failResp, P_FE2CL_REP_PC_ITEM_COMBINATION_FAIL, sizeof(sP_FE2CL_REP_PC_ITEM_COMBINATION_FAIL));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CrocPotEntry* recipe = &ItemManager::CrocPotTable[abs(itemStatsDat->level - itemLooksDat->level)];
|
|
|
|
int cost = itemStatsDat->buyPrice * recipe->multStats + itemLooksDat->buyPrice * recipe->multLooks;
|
2020-09-19 20:22:28 +00:00
|
|
|
float successChance = recipe->base / 100.0f; // base success chance
|
2020-09-16 00:30:01 +00:00
|
|
|
|
|
|
|
// rarity gap multiplier
|
2020-10-19 17:26:14 +00:00
|
|
|
switch(abs(itemStatsDat->rarity - itemLooksDat->rarity)) {
|
2020-09-16 00:30:01 +00:00
|
|
|
case 0:
|
|
|
|
successChance *= recipe->rd0;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
successChance *= recipe->rd1;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
successChance *= recipe->rd2;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
successChance *= recipe->rd3;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-09-19 20:22:28 +00:00
|
|
|
float rolled = (rand() * 1.0f / RAND_MAX) * 100.0f; // success chance out of 100
|
|
|
|
//std::cout << rolled << " vs " << successChance << std::endl;
|
2020-09-16 00:30:01 +00:00
|
|
|
plr->money -= cost;
|
|
|
|
|
|
|
|
|
|
|
|
INITSTRUCT(sP_FE2CL_REP_PC_ITEM_COMBINATION_SUCC, resp);
|
|
|
|
if (rolled < successChance) {
|
|
|
|
// success
|
|
|
|
resp.iSuccessFlag = 1;
|
|
|
|
|
|
|
|
// modify the looks item with the new stats and set the appearance through iOpt
|
2020-10-16 23:54:05 +00:00
|
|
|
itemLooks->iOpt = (int32_t)((itemLooks->iOpt) >> 16 > 0 ? (itemLooks->iOpt >> 16) : itemLooks->iID) << 16;
|
2020-09-16 00:30:01 +00:00
|
|
|
itemLooks->iID = itemStats->iID;
|
|
|
|
|
|
|
|
// delete stats item
|
|
|
|
itemStats->iID = 0;
|
|
|
|
itemStats->iOpt = 0;
|
|
|
|
itemStats->iTimeLimit = 0;
|
|
|
|
itemStats->iType = 0;
|
2020-10-19 17:26:14 +00:00
|
|
|
} else {
|
2020-09-16 00:30:01 +00:00
|
|
|
// failure; don't do anything?
|
|
|
|
resp.iSuccessFlag = 0;
|
|
|
|
}
|
|
|
|
resp.iCandy = plr->money;
|
|
|
|
resp.iNewItemSlot = req->iCostumeItemSlot;
|
|
|
|
resp.iStatItemSlot = req->iStatItemSlot;
|
|
|
|
resp.sNewItem = *itemLooks;
|
2020-10-05 00:03:13 +00:00
|
|
|
|
2020-09-16 00:30:01 +00:00
|
|
|
sock->sendPacket((void*)&resp, P_FE2CL_REP_PC_ITEM_COMBINATION_SUCC, sizeof(sP_FE2CL_REP_PC_ITEM_COMBINATION_SUCC));
|
|
|
|
}
|
|
|
|
|
2020-09-06 14:59:43 +00:00
|
|
|
void NPCManager::npcBarkHandler(CNSocket* sock, CNPacketData* data) {} // stubbed for now
|
2020-08-25 02:28:42 +00:00
|
|
|
|
2020-09-18 21:24:15 +00:00
|
|
|
void NPCManager::npcUnsummonHandler(CNSocket* sock, CNPacketData* data) {
|
|
|
|
if (data->size != sizeof(sP_CL2FE_REQ_NPC_UNSUMMON))
|
|
|
|
return; // malformed packet
|
|
|
|
|
2020-09-28 18:11:13 +00:00
|
|
|
Player* plr = PlayerManager::getPlayer(sock);
|
|
|
|
|
|
|
|
if (plr == nullptr || plr->accountLevel > 30)
|
2020-09-18 21:24:15 +00:00
|
|
|
return;
|
2020-10-05 00:03:13 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-08-29 11:14:21 +00:00
|
|
|
void NPCManager::npcSummonHandler(CNSocket* sock, CNPacketData* data) {
|
2020-08-25 02:28:42 +00:00
|
|
|
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;
|
2020-08-29 11:14:21 +00:00
|
|
|
Player* plr = PlayerManager::getPlayer(sock);
|
2020-08-25 02:28:42 +00:00
|
|
|
|
|
|
|
// permission & sanity check
|
2020-10-13 19:50:14 +00:00
|
|
|
if (plr == nullptr || plr->accountLevel > 30 || req->iNPCType >= 3314 || req->iNPCCnt > 100)
|
2020-08-25 02:28:42 +00:00
|
|
|
return;
|
|
|
|
|
2020-09-24 22:36:25 +00:00
|
|
|
int team = NPCData[req->iNPCType]["m_iTeam"];
|
|
|
|
|
2020-10-13 19:50:14 +00:00
|
|
|
for (int i = 0; i < req->iNPCCnt; i++) {
|
|
|
|
assert(nextId < INT32_MAX);
|
|
|
|
int id = nextId++;
|
2020-08-25 02:28:42 +00:00
|
|
|
|
2020-10-13 19:50:14 +00:00
|
|
|
if (team == 2) {
|
|
|
|
NPCs[id] = new Mob(plr->x, plr->y, plr->z, plr->instanceID, req->iNPCType, NPCData[req->iNPCType], id);
|
|
|
|
MobManager::Mobs[id] = (Mob*)NPCs[id];
|
|
|
|
} else
|
2020-10-14 00:30:54 +00:00
|
|
|
NPCs[id] = new BaseNPC(plr->x, plr->y, plr->z, 0, plr->instanceID, req->iNPCType, id);
|
2020-09-18 21:24:15 +00:00
|
|
|
|
2020-10-13 19:50:14 +00:00
|
|
|
updateNPCPosition(id, plr->x, plr->y, plr->z);
|
|
|
|
}
|
2020-08-25 02:28:42 +00:00
|
|
|
}
|
2020-08-29 11:14:21 +00:00
|
|
|
|
|
|
|
void NPCManager::npcWarpHandler(CNSocket* sock, CNPacketData* data) {
|
|
|
|
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-09-25 05:35:27 +00:00
|
|
|
handleWarp(sock, warpNpc->iWarpID);
|
|
|
|
}
|
2020-08-29 11:14:21 +00:00
|
|
|
|
2020-09-25 05:35:27 +00:00
|
|
|
void NPCManager::npcWarpTimeMachine(CNSocket* sock, CNPacketData* data) {
|
|
|
|
if (data->size != sizeof(sP_CL2FE_REQ_PC_TIME_TO_GO_WARP))
|
|
|
|
return; // malformed packet
|
|
|
|
// this is just a warp request
|
|
|
|
handleWarp(sock, 28);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NPCManager::handleWarp(CNSocket* sock, int32_t warpId) {
|
2020-10-02 20:17:24 +00:00
|
|
|
PlayerView& plrv = PlayerManager::players[sock];
|
2020-08-29 11:14:21 +00:00
|
|
|
// sanity check
|
2020-09-25 05:35:27 +00:00
|
|
|
if (Warps.find(warpId) == Warps.end())
|
2020-08-29 11:14:21 +00:00
|
|
|
return;
|
|
|
|
|
2020-10-02 20:17:24 +00:00
|
|
|
// std::cerr << "Warped to Map Num:" << Warps[warpId].instanceID << " NPC ID " << Warps[warpId].npcID << std::endl;
|
2020-10-19 17:26:14 +00:00
|
|
|
if (Warps[warpId].isInstance) {
|
2020-10-12 16:55:41 +00:00
|
|
|
uint64_t instanceID = Warps[warpId].instanceID;
|
2020-10-20 22:55:58 +00:00
|
|
|
|
|
|
|
// 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-10-12 16:55:41 +00:00
|
|
|
instanceID += ((uint64_t)plrv.plr->iIDGroup << 32); // upper 32 bits are leader ID
|
2020-10-13 03:42:47 +00:00
|
|
|
ChunkManager::createInstance(instanceID);
|
2020-10-12 16:55:41 +00:00
|
|
|
}
|
2020-10-19 17:26:14 +00:00
|
|
|
|
2020-10-17 04:58:51 +00:00
|
|
|
if (plrv.plr->iID == plrv.plr->iIDGroup && plrv.plr->groupCnt == 1)
|
|
|
|
PlayerManager::sendPlayerTo(sock, Warps[warpId].x, Warps[warpId].y, Warps[warpId].z, instanceID);
|
|
|
|
else {
|
|
|
|
Player* leaderPlr = PlayerManager::getPlayerFromID(plrv.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;
|
|
|
|
|
|
|
|
PlayerManager::sendPlayerTo(sockTo, Warps[warpId].x, Warps[warpId].y, Warps[warpId].z, instanceID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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
|
2020-10-02 20:17:24 +00:00
|
|
|
resp.iX = Warps[warpId].x;
|
|
|
|
resp.iY = Warps[warpId].y;
|
|
|
|
resp.iZ = Warps[warpId].z;
|
|
|
|
resp.iCandy = plrv.plr->money;
|
|
|
|
resp.eIL = 4; // do not take away any items
|
|
|
|
PlayerManager::removePlayerFromChunks(plrv.currentChunks, sock);
|
|
|
|
plrv.currentChunks.clear();
|
2020-10-12 16:55:41 +00:00
|
|
|
plrv.plr->instanceID = INSTANCE_OVERWORLD;
|
2020-10-02 20:17:24 +00:00
|
|
|
sock->sendPacket((void*)&resp, P_FE2CL_REP_PC_WARP_USE_NPC_SUCC, sizeof(sP_FE2CL_REP_PC_WARP_USE_NPC_SUCC));
|
|
|
|
}
|
2020-09-07 20:12:53 +00:00
|
|
|
}
|
2020-10-11 03:12:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper function to get NPC closest to coordinates in specified chunks
|
|
|
|
*/
|
|
|
|
BaseNPC* NPCManager::getNearestNPC(std::vector<Chunk*> chunks, int X, int Y, int Z) {
|
|
|
|
BaseNPC* npc = nullptr;
|
|
|
|
int lastDist = INT_MAX;
|
|
|
|
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;
|
|
|
|
}
|
2020-10-22 08:23:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
int NPCManager::eggBuffPlayer(CNSocket* sock, int skillId, int duration) {
|
|
|
|
|
|
|
|
Player* plr = PlayerManager::getPlayer(sock);
|
|
|
|
if (plr == nullptr)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
int32_t CBFlag = -1, iValue, CSTB;
|
|
|
|
|
|
|
|
// find the right passive power data
|
|
|
|
for (auto& pwr : NanoManager::PassivePowers)
|
|
|
|
if (pwr.powers.count(skillId)) {
|
|
|
|
CBFlag = pwr.iCBFlag;
|
|
|
|
CSTB = pwr.eCharStatusTimeBuffID;
|
|
|
|
iValue = pwr.iValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CBFlag < 0)
|
|
|
|
return -1;
|
|
|
|
|
2020-10-22 09:09:50 +00:00
|
|
|
std::pair<CNSocket*, int32_t> key = std::make_pair(sock, CBFlag);
|
2020-10-22 08:23:12 +00:00
|
|
|
|
|
|
|
// if player doesn't have this buff yet
|
|
|
|
if (EggBuffs.find(key) == EggBuffs.end())
|
|
|
|
{
|
|
|
|
// save new cbflag serverside
|
|
|
|
plr->iEggConditionBitFlag |= CBFlag;
|
|
|
|
|
|
|
|
// send buff update package
|
|
|
|
INITSTRUCT(sP_FE2CL_PC_BUFF_UPDATE, updatePacket);
|
|
|
|
updatePacket.eCSTB = CSTB; // eCharStatusTimeBuffID
|
|
|
|
updatePacket.eTBU = 1; // eTimeBuffUpdate 1 means Add
|
|
|
|
updatePacket.eTBT = 3; // eTimeBuffType 3 means egg
|
|
|
|
updatePacket.TimeBuff.iValue = iValue;
|
|
|
|
int32_t updatedFlag = plr->iConditionBitFlag | plr->iGroupConditionBitFlag | plr->iEggConditionBitFlag;
|
|
|
|
updatePacket.iConditionBitFlag = updatedFlag;
|
|
|
|
|
|
|
|
sock->sendPacket((void*)&updatePacket, P_FE2CL_PC_BUFF_UPDATE, sizeof(sP_FE2CL_PC_BUFF_UPDATE));
|
|
|
|
}
|
|
|
|
|
2020-10-22 09:28:18 +00:00
|
|
|
// save the buff serverside;
|
|
|
|
// if you get the same buff again, new duration will override the previous one
|
|
|
|
time_t until = getTimestamp() + duration;
|
|
|
|
EggBuffs[key] = until;
|
2020-10-22 08:23:12 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-10-22 09:09:50 +00:00
|
|
|
|
|
|
|
void NPCManager::buffStep(CNServer* serv, time_t currTime) {
|
|
|
|
|
|
|
|
auto it = EggBuffs.begin();
|
|
|
|
while (it != EggBuffs.end()) {
|
2020-10-22 09:28:18 +00:00
|
|
|
// check remaining time
|
|
|
|
if (it->second > getTimestamp())
|
2020-10-22 09:09:50 +00:00
|
|
|
it++;
|
|
|
|
|
|
|
|
// if time reached 0
|
|
|
|
else{
|
|
|
|
CNSocket* sock = it->first.first;
|
|
|
|
Player* plr = PlayerManager::getPlayer(sock);
|
|
|
|
|
|
|
|
// if player is still on the server
|
|
|
|
if (plr != nullptr) {
|
|
|
|
int32_t CBFlag = it->first.second;
|
|
|
|
int32_t CSTB = -1, iValue;
|
|
|
|
|
|
|
|
// find CSTB Value
|
|
|
|
for (auto pwr : NanoManager::PassivePowers) {
|
|
|
|
if (pwr.iCBFlag == CBFlag) {
|
|
|
|
CSTB = pwr.eCharStatusTimeBuffID;
|
|
|
|
iValue = pwr.iValue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CSTB >= 0) {
|
|
|
|
// update CBFlag serverside
|
|
|
|
plr->iEggConditionBitFlag &= ~CBFlag;
|
|
|
|
// send buff update packet
|
|
|
|
INITSTRUCT(sP_FE2CL_PC_BUFF_UPDATE, updatePacket);
|
|
|
|
updatePacket.eCSTB = CSTB; // eCharStatusTimeBuffID
|
|
|
|
updatePacket.eTBU = 2; // eTimeBuffUpdate 2 means remove
|
|
|
|
updatePacket.eTBT = 3; // eTimeBuffType 3 means egg
|
|
|
|
updatePacket.iConditionBitFlag = plr->iConditionBitFlag | plr->iGroupConditionBitFlag | plr->iEggConditionBitFlag;
|
|
|
|
updatePacket.TimeBuff.iValue = iValue;
|
|
|
|
sock->sendPacket((void*)&updatePacket, P_FE2CL_PC_BUFF_UPDATE, sizeof(sP_FE2CL_PC_BUFF_UPDATE));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// remove buff from the map
|
|
|
|
it = EggBuffs.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|