Refactored mission data and implemented quest item mob drops.

Most future missions are now playable.
Quest items aren't being properly cleaned up yet.
This commit is contained in:
dongresource 2020-09-10 18:40:38 +02:00
parent 3665dc2c93
commit ddb5f782b7
5 changed files with 130 additions and 98 deletions

View File

@ -2,6 +2,7 @@
#include "PlayerManager.hpp" #include "PlayerManager.hpp"
#include "NPCManager.hpp" #include "NPCManager.hpp"
#include "ItemManager.hpp" #include "ItemManager.hpp"
#include "MissionManager.hpp"
#include <assert.h> #include <assert.h>
@ -58,11 +59,7 @@ void CombatManager::pcAttackNpcs(CNSocket *sock, CNPacketData *data) {
if (mob.appearanceData.iHP <= 0) { if (mob.appearanceData.iHP <= 0) {
giveReward(sock); giveReward(sock);
INITSTRUCT(sP_FE2CL_REP_PC_KILL_QUEST_NPCs_SUCC, kill); MissionManager::mobKilled(sock, mob.appearanceData.iNPCType);
kill.iNPCID = mob.appearanceData.iNPCType;
sock->sendPacket((void*)&kill, P_FE2CL_REP_PC_KILL_QUEST_NPCs_SUCC, sizeof(sP_FE2CL_REP_PC_KILL_QUEST_NPCs_SUCC));
// TODO: despawn mobs when they die // TODO: despawn mobs when they die
} }

View File

@ -7,29 +7,47 @@
#include "string.h" #include "string.h"
std::map<int32_t, Reward*> MissionManager::Rewards; std::map<int32_t, Reward*> MissionManager::Rewards;
std::map<int32_t, SUItem*> MissionManager::SUItems; std::map<int32_t, TaskData*> MissionManager::Tasks;
std::map<int32_t, QuestDropSet*> MissionManager::QuestDropSets;
std::map<int32_t, ItemCleanup*> MissionManager::ItemCleanups;
void MissionManager::init() { void MissionManager::init() {
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_TASK_START, acceptMission); REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_TASK_START, taskStart);
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_TASK_END, completeTask); REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_TASK_END, taskEnd);
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_SET_CURRENT_MISSION_ID, setMission); REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_SET_CURRENT_MISSION_ID, setMission);
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_TASK_STOP, quitMission); REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_TASK_STOP, quitMission);
} }
void MissionManager::acceptMission(CNSocket* sock, CNPacketData* data) { void MissionManager::taskStart(CNSocket* sock, CNPacketData* data) {
if (data->size != sizeof(sP_CL2FE_REQ_PC_TASK_START)) if (data->size != sizeof(sP_CL2FE_REQ_PC_TASK_START))
return; // malformed packet return; // malformed packet
sP_CL2FE_REQ_PC_TASK_START* missionData = (sP_CL2FE_REQ_PC_TASK_START*)data->buf; sP_CL2FE_REQ_PC_TASK_START* missionData = (sP_CL2FE_REQ_PC_TASK_START*)data->buf;
INITSTRUCT(sP_FE2CL_REP_PC_TASK_START_SUCC, response); INITSTRUCT(sP_FE2CL_REP_PC_TASK_START_SUCC, response);
Player *plr = PlayerManager::getPlayer(sock);
if (Tasks.find(missionData->iTaskNum) == Tasks.end()) {
std::cout << "[WARN] Player submitted unknown task!?" << std::endl;
// TODO: TASK_FAIL?
response.iTaskNum = missionData->iTaskNum;
sock->sendPacket((void*)&response, P_FE2CL_REP_PC_TASK_START_SUCC, sizeof(sP_FE2CL_REP_PC_TASK_START_SUCC));
return;
}
int i;
for (i = 0; i < ACTIVE_MISSION_COUNT; i++) {
if (plr->tasks[i] == 0) {
plr->tasks[i] = missionData->iTaskNum;
break;
}
}
if (i == ACTIVE_MISSION_COUNT - 1 && plr->tasks[i] != missionData->iTaskNum) {
std::cout << "[WARN] Player has more than 6 active missions!?" << std::endl;
}
response.iTaskNum = missionData->iTaskNum; response.iTaskNum = missionData->iTaskNum;
sock->sendPacket((void*)&response, P_FE2CL_REP_PC_TASK_START_SUCC, sizeof(sP_FE2CL_REP_PC_TASK_START_SUCC)); sock->sendPacket((void*)&response, P_FE2CL_REP_PC_TASK_START_SUCC, sizeof(sP_FE2CL_REP_PC_TASK_START_SUCC));
} }
void MissionManager::completeTask(CNSocket* sock, CNPacketData* data) { void MissionManager::taskEnd(CNSocket* sock, CNPacketData* data) {
if (data->size != sizeof(sP_CL2FE_REQ_PC_TASK_END)) if (data->size != sizeof(sP_CL2FE_REQ_PC_TASK_END))
return; // malformed packet return; // malformed packet
@ -40,22 +58,16 @@ void MissionManager::completeTask(CNSocket* sock, CNPacketData* data) {
response.iTaskNum = missionData->iTaskNum; response.iTaskNum = missionData->iTaskNum;
std::cout << missionData->iTaskNum << std::endl; std::cout << missionData->iTaskNum << std::endl;
// clean up quest items which have served their purpose if (Tasks.find(missionData->iTaskNum) == Tasks.end()) {
if (ItemCleanups.find(missionData->iTaskNum) != ItemCleanups.end()) { std::cout << "[WARN] Player submitted unknown task!?" << std::endl;
ItemCleanup *clean = ItemCleanups[missionData->iTaskNum]; // TODO: TASK_FAIL?
sock->sendPacket((void*)&response, P_FE2CL_REP_PC_TASK_END_SUCC, sizeof(sP_FE2CL_REP_PC_TASK_END_SUCC));
std::cout << "clearing qitems\n"; return;
for (int i = 0; i < AQINVEN_COUNT; i++)
for (int j = 0; j < 4; j++)
if (plr->QInven[i].iID == clean->itemIds[j])
memset(&plr->QInven[i], 0, sizeof(sItemBase));
/*
* NOTE: As quest items are not graphically enumerated client-side,
* the client does not need to be notified of item cleanup, since
* the items will just be clobbered upon assignment anyway.
*/
} }
// ugly pointer/reference juggling for the sake of operator overloading...
TaskData& task = *Tasks[missionData->iTaskNum];
/* /*
* SUItems * SUItems
* *
@ -65,12 +77,23 @@ void MissionManager::completeTask(CNSocket* sock, CNPacketData* data) {
* The server is responsible for dropping the correct item. * The server is responsible for dropping the correct item.
* Yes, this is idiotic. * Yes, this is idiotic.
*/ */
if (SUItems.find(missionData->iTaskNum) != SUItems.end()) { for (int i = 0; i < 3; i++)
SUItem *suitem = SUItems[missionData->iTaskNum]; if (task["m_iSUItem"][i] != 0)
dropQuestItem(sock, missionData->iTaskNum, 1, task["m_iSUItem"][i], 0);
for (int i = 0; i < 3; i++) // clean up quest items which have served their purpose
if (suitem->itemIds[i] != 0) if (task["m_iCSUItemID"][0] != 0) {
dropQuestItem(sock, missionData->iTaskNum, 1, suitem->itemIds[i], 0); for (int i = 0; i < AQINVEN_COUNT; i++)
for (int j = 0; j < 4; j++)
if (plr->QInven[i].iID == task["m_CSUItemID"][j]) {
std::cout << "deleting qitem " << i << std::endl;
memset(&plr->QInven[i], 0, sizeof(sItemBase));
}
/*
* NOTE: As quest items are not graphically enumerated client-side,
* the client does not need to be notified of item cleanup, since
* the items will just be clobbered upon assignment anyway.
*/
} }
// mission rewards // mission rewards
@ -78,6 +101,16 @@ void MissionManager::completeTask(CNSocket* sock, CNPacketData* data) {
giveMissionReward(sock, missionData->iTaskNum); giveMissionReward(sock, missionData->iTaskNum);
} }
// update player
int i;
for (i = 0; i < ACTIVE_MISSION_COUNT; i++) {
if (plr->tasks[i] == missionData->iTaskNum)
plr->tasks[i] = 0;
}
if (i == ACTIVE_MISSION_COUNT - 1 && plr->tasks[i] != 0) {
std::cout << "[WARN] Player completed non-active mission!?" << std::endl;
}
sock->sendPacket((void*)&response, P_FE2CL_REP_PC_TASK_END_SUCC, sizeof(sP_FE2CL_REP_PC_TASK_END_SUCC)); sock->sendPacket((void*)&response, P_FE2CL_REP_PC_TASK_END_SUCC, sizeof(sP_FE2CL_REP_PC_TASK_END_SUCC));
} }
@ -104,14 +137,14 @@ void MissionManager::quitMission(CNSocket* sock, CNPacketData* data) {
} }
// TODO: coalesce into ItemManager::findFreeSlot()? // TODO: coalesce into ItemManager::findFreeSlot()?
int MissionManager::findFreeQSlot(Player *plr) { int MissionManager::findQSlot(Player *plr, int id) {
int i; int i;
sItemBase free; sItemBase free;
memset(&free, 0, sizeof(sItemBase)); memset(&free, 0, sizeof(sItemBase));
for (i = 0; i < AQINVEN_COUNT; i++) for (i = 0; i < AQINVEN_COUNT; i++)
if (memcmp(&plr->QInven[i], &free, sizeof(sItemBase)) == 0) if (plr->QInven[i].iID == id || memcmp(&plr->QInven[i], &free, sizeof(sItemBase)) == 0)
return i; return i;
// not found // not found
@ -132,7 +165,7 @@ void MissionManager::dropQuestItem(CNSocket *sock, int task, int count, int id,
memset(respbuf, 0, resplen); memset(respbuf, 0, resplen);
// find free quest item slot // find free quest item slot
int slot = findFreeQSlot(plr); int slot = findQSlot(plr, id);
if (slot == -1) { if (slot == -1) {
// this should never happen // this should never happen
std::cout << "[WARN] Player has no room for quest item!?" << std::endl; std::cout << "[WARN] Player has no room for quest item!?" << std::endl;
@ -143,7 +176,7 @@ void MissionManager::dropQuestItem(CNSocket *sock, int task, int count, int id,
// update player // update player
plr->QInven[slot].iType = 8; plr->QInven[slot].iType = 8;
plr->QInven[slot].iID = id; plr->QInven[slot].iID = id;
plr->QInven[slot].iOpt = count; plr->QInven[slot].iOpt += count; // stacking
// preserve stats // preserve stats
reward->m_iCandy = plr->money; reward->m_iCandy = plr->money;
@ -159,7 +192,6 @@ void MissionManager::dropQuestItem(CNSocket *sock, int task, int count, int id,
item->iSlotNum = slot; item->iSlotNum = slot;
item->eIL = 2; item->eIL = 2;
std::cout << "sending quest item\n";
sock->sendPacket((void*)respbuf, P_FE2CL_REP_REWARD_ITEM, resplen); sock->sendPacket((void*)respbuf, P_FE2CL_REP_REWARD_ITEM, resplen);
} }
@ -220,3 +252,41 @@ void MissionManager::giveMissionReward(CNSocket *sock, int task) {
sock->sendPacket((void*)respbuf, P_FE2CL_REP_REWARD_ITEM, resplen); sock->sendPacket((void*)respbuf, P_FE2CL_REP_REWARD_ITEM, resplen);
} }
void MissionManager::mobKilled(CNSocket *sock, int mobid) {
Player *plr = PlayerManager::getPlayer(sock);
for (int i = 0; i < ACTIVE_MISSION_COUNT; i++) {
if (plr->tasks[i] == 0)
continue;
// tasks[] should always have valid IDs
TaskData& task = *Tasks[plr->tasks[i]];
for (int j = 0; j < 3; j++) {
if (task["m_iCSUEnemyID"][j] != mobid)
continue;
// acknowledge killing of mission mob
if (task["m_iCSUNumToKill"][j] != 0) {
INITSTRUCT(sP_FE2CL_REP_PC_KILL_QUEST_NPCs_SUCC, kill);
kill.iNPCID = mobid;
sock->sendPacket((void*)&kill, P_FE2CL_REP_PC_KILL_QUEST_NPCs_SUCC, sizeof(sP_FE2CL_REP_PC_KILL_QUEST_NPCs_SUCC));
}
// drop quest item
if (task["m_iCSUItemNumNeeded"][j] != 0) {
bool drop = rand() % 100 < task["m_iSTItemDropRate"][j];
if (drop) {
// XXX: are CSUItemID and CSTItemID the same?
dropQuestItem(sock, plr->tasks[i], 1, task["m_iCSUItemID"][j], mobid);
} else {
// fail to drop (itemID == 0)
dropQuestItem(sock, plr->tasks[i], 1, 0, mobid);
}
}
}
}
}

View File

@ -21,51 +21,33 @@ struct Reward {
}; };
}; };
struct SUItem { struct TaskData {
int32_t itemIds[3]; /*
* TODO: We'll probably want to keep only the data the server actually needs,
* but for now RE/development is much easier if we have everything at
* our fingertips.
*/
nlohmann::json task;
SUItem(nlohmann::json ids) { TaskData(nlohmann::json t) : task(t) {}
for (int i = 0; i < 3; i++) {
itemIds[i] = ids[i];
}
}
};
struct QuestDropSet { // convenience
int32_t mobIds[3]; auto operator[](std::string s) { return task[s]; }
int32_t itemIds[3];
QuestDropSet(nlohmann::json mobs, nlohmann::json items) {
for (int i = 0; i < 3; i++) {
mobIds[i] = mobs[i];
itemIds[i] = items[i];
}
}
};
struct ItemCleanup {
int32_t itemIds[4];
ItemCleanup(nlohmann::json ids) {
for (int i = 0; i < 4; i++) {
itemIds[i] = ids[i];
}
}
}; };
namespace MissionManager { namespace MissionManager {
extern std::map<int32_t, Reward*> Rewards; extern std::map<int32_t, Reward*> Rewards;
extern std::map<int32_t, SUItem*> SUItems; extern std::map<int32_t, TaskData*> Tasks;
extern std::map<int32_t, QuestDropSet*> QuestDropSets;
extern std::map<int32_t, ItemCleanup*> ItemCleanups;
void init(); void init();
void acceptMission(CNSocket* sock, CNPacketData* data); void taskStart(CNSocket* sock, CNPacketData* data);
void completeTask(CNSocket* sock, CNPacketData* data); void taskEnd(CNSocket* sock, CNPacketData* data);
void setMission(CNSocket* sock, CNPacketData* data); void setMission(CNSocket* sock, CNPacketData* data);
void quitMission(CNSocket* sock, CNPacketData* data); void quitMission(CNSocket* sock, CNPacketData* data);
int findFreeQSlot(Player *plr); int findQSlot(Player *plr, int id);
void dropQuestItem(CNSocket *sock, int task, int count, int id, int mobid); void dropQuestItem(CNSocket *sock, int task, int count, int id, int mobid);
void giveMissionReward(CNSocket *sock, int task); void giveMissionReward(CNSocket *sock, int task);
void mobKilled(CNSocket *sock, int mobid);
} }

View File

@ -6,6 +6,8 @@
#include "CNProtocol.hpp" #include "CNProtocol.hpp"
#include "CNStructs.hpp" #include "CNStructs.hpp"
#define ACTIVE_MISSION_COUNT 6
struct Player { struct Player {
int accountId; int accountId;
int64_t SerialKey; int64_t SerialKey;
@ -33,6 +35,6 @@ struct Player {
bool isTradeConfirm; bool isTradeConfirm;
bool IsGM; bool IsGM;
int tasks[6]; int tasks[ACTIVE_MISSION_COUNT];
sItemBase QInven[AQINVEN_COUNT]; sItemBase QInven[AQINVEN_COUNT];
}; };

View File

@ -55,7 +55,7 @@ void TableData::init() {
NPCManager::NPCs[tmp.appearanceData.iNPC_ID] = tmp; NPCManager::NPCs[tmp.appearanceData.iNPC_ID] = tmp;
} }
std::cout << "[INFO] populated " << NPCManager::NPCs.size() << " NPCs" << std::endl; std::cout << "[INFO] Populated " << NPCManager::NPCs.size() << " NPCs" << std::endl;
} }
catch (const std::exception& err) { catch (const std::exception& err) {
std::cerr << "[WARN] Malformed mobs.json file! Reason:" << err.what() << std::endl; std::cerr << "[WARN] Malformed mobs.json file! Reason:" << err.what() << std::endl;
@ -79,7 +79,7 @@ void TableData::init() {
NPCManager::Warps[warpID] = warpLoc; NPCManager::Warps[warpID] = warpLoc;
} }
std::cout << "[INFO] populated " << NPCManager::Warps.size() << " Warps" << std::endl; std::cout << "[INFO] Populated " << NPCManager::Warps.size() << " Warps" << std::endl;
// load mission-related data // load mission-related data
nlohmann::json tasks = xdtData["m_pMissionTable"]["m_pMissionData"]; nlohmann::json tasks = xdtData["m_pMissionTable"]["m_pMissionData"];
@ -89,30 +89,15 @@ void TableData::init() {
// rewards // rewards
if (task["m_iSUReward"] != 0) { if (task["m_iSUReward"] != 0) {
auto tmp = xdtData["m_pMissionTable"]["m_pRewardData"][(int)task["m_iSUReward"]]; auto _rew = xdtData["m_pMissionTable"]["m_pRewardData"][(int)task["m_iSUReward"]];
Reward *rew = new Reward(tmp["m_iMissionRewardID"], tmp["m_iMissionRewarItemType"], Reward *rew = new Reward(_rew["m_iMissionRewardID"], _rew["m_iMissionRewarItemType"],
tmp["m_iMissionRewardItemID"], tmp["m_iCash"], tmp["m_iFusionMatter"]); _rew["m_iMissionRewardItemID"], _rew["m_iCash"], _rew["m_iFusionMatter"]);
MissionManager::Rewards[task["m_iHTaskID"]] = rew; MissionManager::Rewards[task["m_iHTaskID"]] = rew;
} }
// quest items obtained after completing a certain task // everything else lol. see TaskData comment.
// (distinct from quest items dropped from mobs) MissionManager::Tasks[task["m_iHTaskID"]] = new TaskData(task);
if (task["m_iSUItem"][0] != 0)
MissionManager::SUItems[task["m_iHTaskID"]] = new SUItem(task["m_iSUItem"]);
// quest item mob drops
if (task["m_iCSUItemID"][0] != 0) {
MissionManager::QuestDropSets[task["m_iHTaskID"]] = new QuestDropSet(task["m_iCSUEnemyID"], task["m_iCSUItemID"]);
// TODO: timeouts, drop rates, etc.
// not sure if we need to keep track of NumNeeded/NumToKill server-side.
}
// quest item cleanup
if (task["m_iDelItemID"][0] != 0) {
std::cout << "adding DelItem for " << task["m_iHTaskID"] << std::endl;
MissionManager::ItemCleanups[task["m_iHTaskID"]] = new ItemCleanup(task["m_iDelItemID"]);
}
} }
std::cout << "[INFO] Loaded mission-related data" << std::endl; std::cout << "[INFO] Loaded mission-related data" << std::endl;
@ -129,10 +114,6 @@ void TableData::cleanup() {
*/ */
for (auto& pair : MissionManager::Rewards) for (auto& pair : MissionManager::Rewards)
delete pair.second; delete pair.second;
for (auto& pair : MissionManager::SUItems) for (auto& pair : MissionManager::Tasks)
delete pair.second;
for (auto& pair : MissionManager::QuestDropSets)
delete pair.second;
for (auto& pair : MissionManager::ItemCleanups)
delete pair.second; delete pair.second;
} }