Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for item drop rates up to 255 #7533

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Source/itemdat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ tl::expected<item_drop_rate, std::string> ParseItemDropRate(std::string_view val
if (value == "Never") return IDROP_NEVER;
if (value == "Regular") return IDROP_REGULAR;
if (value == "Double") return IDROP_DOUBLE;
ephphatha marked this conversation as resolved.
Show resolved Hide resolved
ParseIntResult<std::underlying_type_t<item_drop_rate>> numericValue = ParseInt<std::underlying_type_t<item_drop_rate>>(value);
if (numericValue.has_value()) return static_cast<item_drop_rate>(numericValue.value());
if (numericValue.error() == ParseIntError::OutOfRange) return tl::make_unexpected("Value too large");
return tl::make_unexpected("Unknown enum value");
}

Expand Down
22 changes: 12 additions & 10 deletions Source/items.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1350,11 +1350,17 @@ void GetItemBonus(const Player &player, Item &item, int minlvl, int maxlvl, bool
}
}

struct WeightedItemIndex {
_item_indexes index;
unsigned cumulativeWeight;
};

_item_indexes GetItemIndexForDroppableItem(bool considerDropRate, tl::function_ref<bool(const ItemData &item)> isItemOkay)
{
static std::array<_item_indexes, IDI_LAST * 2> ril;
static std::vector<WeightedItemIndex> ril;
ril.clear();

size_t ri = 0;
unsigned cumulativeWeight = 0;
for (std::underlying_type_t<_item_indexes> i = IDI_GOLD; i <= IDI_LAST; i++) {
if (!IsItemAvailable(i))
continue;
Expand All @@ -1365,15 +1371,11 @@ _item_indexes GetItemIndexForDroppableItem(bool considerDropRate, tl::function_r
continue;
if (!isItemOkay(item))
continue;
ril[ri] = static_cast<_item_indexes>(i);
ri++;
if (item.iRnd == IDROP_DOUBLE && considerDropRate) {
ril[ri] = static_cast<_item_indexes>(i);
ri++;
}
cumulativeWeight += considerDropRate ? static_cast<unsigned>(item.iRnd) : 1;
ril.push_back({ static_cast<_item_indexes>(i), cumulativeWeight });
}

return ril[GenerateRnd(static_cast<int>(ri))];
unsigned targetWeight = static_cast<unsigned>(RandomIntLessThan(static_cast<int>(cumulativeWeight)));
return std::upper_bound(ril.begin(), ril.end(), targetWeight, [](unsigned target, const WeightedItemIndex &value) { return target < value.cumulativeWeight; })->index;
}

_item_indexes RndUItem(Monster *monster)
Expand Down
Loading