mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-11-19 02:05:28 +00:00
fix(missions): a mission is only turned in when its rewards fit, as the client
LWOMissionComponent::MissionWindowOK (0x00d501f0) turns a mission in only when CanReceiveAllRewards (msgCanReceiveAllRewards 0x00d17e20, sub_00d17750) passes for the reward the player picked: the reward items (the repeat ones once completed, each at least 1) must fit after the items the tasks take (a gather task's target unless it keeps them, one of a delivery task's target) with the first completion's bag rewards added. The fit is LWOInventoryComponent_Common::CheckCanAddItems (0x00d53de0): turned in items leave first (an item of 1 settles the whole count), a unique item fails while one is held outside the buyback, then each item tops up stacks of its LOT (stack size 0 is no limit, LookupItemInfo 0x00c1c190) and takes free slots. MissionDialogueOK now refuses a turn-in that does not pass, which the client never sends. The client's max currency part is not checked: the server writes no cm, so the client has no limit either. InventoryComponent::GetDefaultInventorySize is split out of GetInventory. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -5,6 +5,9 @@
|
||||
#include "EconomyLedger.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include <optional>
|
||||
|
||||
#include "Entity.h"
|
||||
#include "Item.h"
|
||||
@@ -157,6 +160,17 @@ void InventoryComponent::LoadDataFromTemplate() {
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t InventoryComponent::GetDefaultInventorySize(const eInventoryType type) {
|
||||
switch (type) {
|
||||
case eInventoryType::ITEMS: return 20u;
|
||||
case eInventoryType::VAULT_MODELS:
|
||||
case eInventoryType::VAULT_ITEMS: return 40u;
|
||||
case eInventoryType::VENDOR_BUYBACK: return 27u;
|
||||
case eInventoryType::DONATION: return 24u;
|
||||
default: return 240u;
|
||||
}
|
||||
}
|
||||
|
||||
Inventory* InventoryComponent::GetInventory(const eInventoryType type) {
|
||||
const auto index = m_Inventories.find(type);
|
||||
|
||||
@@ -165,25 +179,7 @@ Inventory* InventoryComponent::GetInventory(const eInventoryType type) {
|
||||
}
|
||||
|
||||
// Create new empty inventory
|
||||
uint32_t size = 240u;
|
||||
|
||||
switch (type) {
|
||||
case eInventoryType::ITEMS:
|
||||
size = 20u;
|
||||
break;
|
||||
case eInventoryType::VAULT_MODELS:
|
||||
case eInventoryType::VAULT_ITEMS:
|
||||
size = 40u;
|
||||
break;
|
||||
case eInventoryType::VENDOR_BUYBACK:
|
||||
size = 27u;
|
||||
break;
|
||||
case eInventoryType::DONATION:
|
||||
size = 24u;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
const auto size = GetDefaultInventorySize(type);
|
||||
|
||||
auto* inventory = new Inventory(type, size, {}, this);
|
||||
|
||||
@@ -646,6 +642,104 @@ bool InventoryComponent::HasSpaceForLoot(const Loot::Return& loot) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool InventoryComponent::CanAddItems(const std::vector<std::pair<LOT, uint32_t>>& items, const std::vector<std::pair<LOT, uint32_t>>& itemsToTurnIn,
|
||||
const std::map<eInventoryType, uint32_t>& sizeIncrease) const {
|
||||
// A copy of the inventories: per inventory its items in ID order (new ones last, as the client's new IDs)
|
||||
struct SimItem {
|
||||
LOT lot;
|
||||
uint32_t count;
|
||||
uint32_t slot;
|
||||
LWOOBJID subKey;
|
||||
};
|
||||
std::map<eInventoryType, std::vector<SimItem>> inventories;
|
||||
std::map<eInventoryType, uint32_t> sizes;
|
||||
for (const auto& [type, inventory] : m_Inventories) {
|
||||
if (!inventory) continue;
|
||||
auto& simItems = inventories[type];
|
||||
for (const auto& [id, item] : inventory->GetItems()) {
|
||||
if (item) simItems.push_back({ item->GetLot(), item->GetCount(), item->GetSlot(), item->GetSubKey() });
|
||||
}
|
||||
sizes[type] = inventory->GetSize();
|
||||
}
|
||||
for (const auto& [type, increase] : sizeIncrease) sizes[type] += increase;
|
||||
|
||||
// Turned in items leave first; an item of 1 settles the whole count, as the client
|
||||
for (auto [lot, count] : itemsToTurnIn) {
|
||||
for (auto& [type, simItems] : inventories) {
|
||||
for (auto it = simItems.begin(); count != 0 && it != simItems.end();) {
|
||||
if (it->lot != lot) {
|
||||
++it;
|
||||
continue;
|
||||
}
|
||||
if (it->count < 2) {
|
||||
it = simItems.erase(it);
|
||||
count = 0;
|
||||
continue;
|
||||
}
|
||||
const auto taken = std::min(count, it->count);
|
||||
it->count -= taken;
|
||||
count -= taken;
|
||||
it = it->count == 0 ? simItems.erase(it) : it + 1;
|
||||
}
|
||||
if (count == 0) break;
|
||||
}
|
||||
}
|
||||
|
||||
// LWOInventoryComponent_Common::GetInvItemCount over every inventory
|
||||
const auto heldCount = [this](const LOT lot, const std::optional<eInventoryType> only) {
|
||||
uint32_t total = 0;
|
||||
for (const auto& [type, inventory] : m_Inventories) {
|
||||
if (!inventory || (only && type != *only)) continue;
|
||||
for (const auto& [id, item] : inventory->GetItems()) {
|
||||
if (item && item->GetLot() == lot) total += item->GetCount();
|
||||
}
|
||||
}
|
||||
return total;
|
||||
};
|
||||
|
||||
for (const auto& [lot, wanted] : items) {
|
||||
const auto type = Inventory::FindInventoryTypeForLot(lot);
|
||||
// An inventory the server has not made yet is empty (the client refuses one it does not have, but has every
|
||||
// one the server sent)
|
||||
if (!inventories.contains(type)) sizes[type] += GetDefaultInventorySize(type);
|
||||
auto& simItems = inventories[type];
|
||||
|
||||
const auto& info = Inventory::FindItemComponent(lot);
|
||||
// LookupItemInfo (0x00c1c190): a stack size of 0 is no limit
|
||||
const uint32_t stack = info.stackSize == 0 ? std::numeric_limits<uint32_t>::max() : info.stackSize;
|
||||
auto left = wanted;
|
||||
if (info.isUnique) {
|
||||
if (heldCount(lot, std::nullopt) != heldCount(lot, eInventoryType::VENDOR_BUYBACK)) return false; // UniqueItemStillExistsInAnInventory
|
||||
left = 1;
|
||||
}
|
||||
|
||||
for (auto& simItem : simItems) {
|
||||
if (left == 0) break;
|
||||
if (simItem.lot != lot || simItem.subKey != LWOOBJID_EMPTY || simItem.count >= stack) continue;
|
||||
const auto added = std::min(left, stack - simItem.count);
|
||||
simItem.count += added;
|
||||
left -= added;
|
||||
}
|
||||
|
||||
if (left == 0) continue;
|
||||
std::set<uint32_t> takenSlots;
|
||||
for (const auto& simItem : simItems) takenSlots.insert(simItem.slot);
|
||||
const auto size = sizes[type];
|
||||
while (left != 0) {
|
||||
if (takenSlots.size() >= size) return false; // TargetInventoryOutOfRoom
|
||||
uint32_t slot = 0;
|
||||
while (slot < size && takenSlots.contains(slot)) slot++;
|
||||
if (slot >= size) return false; // items sit beyond the size; the client would look for a slot forever
|
||||
takenSlots.insert(slot);
|
||||
const auto added = std::min(left, stack);
|
||||
left -= added;
|
||||
simItems.push_back({ lot, added, slot, LWOOBJID_EMPTY });
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
namespace {
|
||||
/**
|
||||
* Cars and rockets built before builds were recorded have no subkey, so the client has no blueprint id to ask for
|
||||
|
||||
@@ -126,6 +126,9 @@ public:
|
||||
*/
|
||||
Inventory* GetInventory(eInventoryType type);
|
||||
|
||||
// The size an inventory starts with when the server makes it
|
||||
static uint32_t GetDefaultInventorySize(eInventoryType type);
|
||||
|
||||
/**
|
||||
* Returns all the inventories this entity has, indexed by type
|
||||
* @return all the inventories this entity has, indexed by type
|
||||
@@ -255,6 +258,18 @@ public:
|
||||
*/
|
||||
bool HasSpaceForLoot(const Loot::Return& loot);
|
||||
|
||||
/**
|
||||
* Whether all of these items would fit, as the client works it out before turning in a mission
|
||||
* (LWOInventoryComponent_Common::CheckCanAddItems 0x00d53de0): the items to turn in are taken out first, the bag
|
||||
* sizes grow by sizeIncrease, a unique item fails while one is held outside the buyback, then each item tops up
|
||||
* stacks of its LOT and takes free slots. Nothing changes.
|
||||
* @param items LOTs and counts to add
|
||||
* @param itemsToTurnIn LOTs and counts taken from any inventory first
|
||||
* @param sizeIncrease extra slots per inventory
|
||||
*/
|
||||
bool CanAddItems(const std::vector<std::pair<LOT, uint32_t>>& items, const std::vector<std::pair<LOT, uint32_t>>& itemsToTurnIn,
|
||||
const std::map<eInventoryType, uint32_t>& sizeIncrease) const;
|
||||
|
||||
/**
|
||||
* Equips an item in the specified slot
|
||||
* @param location the location to store the item (e.g. chest, left hand, etc.)
|
||||
|
||||
@@ -160,6 +160,14 @@ namespace GameMessages {
|
||||
if (iMissionState == eMissionState::AVAILABLE || iMissionState == eMissionState::COMPLETE_AVAILABLE) {
|
||||
missionComponent->AcceptMission(missionID);
|
||||
} else if (iMissionState == eMissionState::READY_TO_COMPLETE || iMissionState == eMissionState::COMPLETE_READY_TO_COMPLETE) {
|
||||
// LWOMissionComponent::MissionWindowOK (0x00d501f0) only turns a mission in when every reward fits
|
||||
// (CanReceiveAllRewards, with the reward the player picked)
|
||||
const auto* const mission = missionComponent->GetMission(missionID);
|
||||
const auto chosen = mission && mission->GetReward() > 0 ? mission->GetReward() : -1;
|
||||
if (!mission || !mission->CanReceiveAllRewards(chosen)) {
|
||||
LOG("Player %llu cannot receive all the rewards of mission %i, not turning it in", player->GetObjectID(), missionID);
|
||||
return;
|
||||
}
|
||||
missionComponent->CompleteMission(missionID);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
#include "ChatServerLink.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <cctype>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
@@ -464,6 +467,56 @@ void Mission::Catchup() {
|
||||
}
|
||||
}
|
||||
|
||||
bool Mission::CanReceiveAllRewards(const LOT chosenReward) const {
|
||||
if (IsComplete()) return false;
|
||||
|
||||
auto* const entity = GetAssociate();
|
||||
auto* const inventoryComponent = entity ? entity->GetComponent<InventoryComponent>() : nullptr;
|
||||
if (!inventoryComponent) return false;
|
||||
|
||||
// The repeat rewards once ever completed; a reward item counts at least 1 (LWOMission 0x00fa35d0)
|
||||
const bool repeat = m_Completions > 0;
|
||||
const std::array<std::pair<LOT, int32_t>, 4> rewards = repeat
|
||||
? std::array<std::pair<LOT, int32_t>, 4>{ { { info.reward_item1_repeatable, info.reward_item1_repeat_count }, { info.reward_item2_repeatable, info.reward_item2_repeat_count },
|
||||
{ info.reward_item3_repeatable, info.reward_item3_repeat_count }, { info.reward_item4_repeatable, info.reward_item4_repeat_count } } }
|
||||
: std::array<std::pair<LOT, int32_t>, 4>{ { { info.reward_item1, info.reward_item1_count }, { info.reward_item2, info.reward_item2_count },
|
||||
{ info.reward_item3, info.reward_item3_count }, { info.reward_item4, info.reward_item4_count } } };
|
||||
std::vector<std::pair<LOT, uint32_t>> items;
|
||||
for (const auto& [lot, count] : rewards) {
|
||||
if (lot == LOT_NULL || (chosenReward != -1 && lot != chosenReward)) continue;
|
||||
items.emplace_back(lot, static_cast<uint32_t>(std::max(count, 1)));
|
||||
}
|
||||
|
||||
// The bag rewards come with the first completion only
|
||||
std::map<eInventoryType, uint32_t> sizeIncrease;
|
||||
if (!repeat) {
|
||||
if (info.reward_maxinventory > 0) sizeIncrease[eInventoryType::ITEMS] += info.reward_maxinventory;
|
||||
if (info.reward_maxmodel > 0) sizeIncrease[eInventoryType::MODELS] += info.reward_maxmodel;
|
||||
}
|
||||
|
||||
// The items the tasks take (sub_00fa2f20): a gather task's target unless it keeps them (LWOGatherTask 0x00f919c0),
|
||||
// one of a delivery task's target
|
||||
const bool noGiver = (info.offer_objectID == -1 || info.offer_objectID == 0) && (info.target_objectID == -1 || info.target_objectID == 0);
|
||||
std::vector<std::pair<LOT, uint32_t>> itemsToTurnIn;
|
||||
for (const auto* const task : m_Tasks) {
|
||||
const auto& taskInfo = task->GetClientInfo();
|
||||
const auto target = static_cast<LOT>(taskInfo.target);
|
||||
if (task->GetType() == eMissionTaskType::GATHER) {
|
||||
const auto& parameters = task->GetParameters();
|
||||
int32_t param = parameters.empty() ? 2 : static_cast<int32_t>(parameters[0]);
|
||||
if (noGiver && (param & 2) != 0) param = 1; // an achievement never takes items
|
||||
const bool keep = (param & 1) != 0 && (param & 2) == 0;
|
||||
if (!keep && target != LOT_NULL) itemsToTurnIn.emplace_back(target, static_cast<uint32_t>(taskInfo.targetValue));
|
||||
} else if (task->GetType() == eMissionTaskType::DELIVERY) {
|
||||
if (target != LOT_NULL) itemsToTurnIn.emplace_back(target, 1);
|
||||
}
|
||||
}
|
||||
|
||||
// The client also refuses when the coin reward would pass its max currency (the save's cm, no limit without it);
|
||||
// the server writes no cm, so there is no limit to check
|
||||
return inventoryComponent->CanAddItems(items, itemsToTurnIn, sizeIncrease);
|
||||
}
|
||||
|
||||
void Mission::YieldRewards() {
|
||||
auto* entity = GetAssociate();
|
||||
|
||||
|
||||
@@ -250,6 +250,14 @@ public:
|
||||
* Whether the player has looked at this mission in progress (LWOMissionComponent::msgSetMissionViewed 0x00bec7a0),
|
||||
* saved as v on the mission's <cur> entry
|
||||
*/
|
||||
/**
|
||||
* Whether the player can take every reward of this mission in progress, as the client checks before turning it in
|
||||
* (LWOMissionComponent::msgCanReceiveAllRewards 0x00d17e20, sub_00d17750): the reward items (all, or only the
|
||||
* chosen one) must fit in the inventory after the items the tasks take, with the bag rewards added.
|
||||
* @param chosenReward the reward the player picked, -1 for all
|
||||
*/
|
||||
bool CanReceiveAllRewards(LOT chosenReward = -1) const;
|
||||
|
||||
bool GetViewed() const { return m_Viewed; }
|
||||
void SetViewed(const bool viewed) { m_Viewed = viewed; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user