Derive each player's zen from the experience they actually gained

The money amount of a monster drop is computed as "gained experience + 7", but
in a party it was neither derived from, nor distributed like, that experience.

DistributeExperienceAfterKillAsync returned a party total without the game rate
and the personal experience rates, while AddExpAfterKillAsync returned a value
which had them applied. Both landed in the same variable in OnDeathAsync, so the
party number was orders of magnitude smaller. That mismatch was worked around by
recalculating the killer's solo experience for money purposes, which pinned the
party pool to a solo-sized amount: with the pool then split by member count, a
party of three received about a third of the solo zen each, while its experience
pool grows with the party size.

AwardExperienceAsync already computes the per member experience with all rates
applied, it just discarded it. It now returns that value, the distribution
returns the per member breakdown, and the money is split proportionally to it.
The workaround is gone, the pool follows the experience, and each member's zen
matches their own level, their own rates and their own master/normal branch. The
units lost to the integer division are handed to the shares which were cut the
most, one each, so no member is systematically favoured over many kills.

Two defects around the money rate are fixed on the way:

- MoneyAmountRate was applied for the killer when the drop was created and again
  for the receiver when a party picked it up, so a rate of 3.0 paid a party 9x.
  On the "money straight into the inventory" path for a solo killer it was not
  applied at all. It is now applied exactly once, for the receiver.
- ClampMoneyOnPickup was only honoured for solo pick ups. A party member at
  MaximumInventoryMoney silently lost their share, because the drop was consumed
  as soon as any other member could take one. The clamp now runs on the shared
  payout path, after the money rate, so it clamps the amount actually credited.

Money without a per player breakdown, such as the fixed amount of an item box,
keeps being split equally. Shares of players who are no longer eligible are
redistributed among the remaining ones instead of being lost.

(cherry picked from commit 53f25aca08e690255571f2bb6c796bb7c10934a2)
This commit is contained in:
nolt
2026-07-24 11:27:17 +02:00
committed by Acentech Dev
parent 9e0bcede2f
commit 6307ecfc24
8 changed files with 597 additions and 104 deletions

View File

@@ -6,7 +6,6 @@ namespace MUnique.OpenMU.GameLogic;
using System.Diagnostics;
using System.Threading;
using MUnique.OpenMU.GameLogic.Attributes;
using MUnique.OpenMU.Pathfinding;
using Nito.AsyncEx;
@@ -20,6 +19,8 @@ public sealed class DroppedMoney : AsyncDisposable, ILocateable
/// </summary>
private readonly AsyncLock _pickupLock;
private readonly IReadOnlyList<MoneyShare> _shares;
private Timer? _removeTimer;
private bool _availableToPick = true;
@@ -30,9 +31,14 @@ public sealed class DroppedMoney : AsyncDisposable, ILocateable
/// <param name="amount">The amount.</param>
/// <param name="position">The position where the item was dropped on the map.</param>
/// <param name="map">The map.</param>
public DroppedMoney(uint amount, Point position, GameMap map)
/// <param name="shares">
/// The part of the money which is reserved for each player, matching the experience they gained from the kill.
/// When it's empty - for example for money from an item box - the money is split equally instead.
/// </param>
public DroppedMoney(uint amount, Point position, GameMap map, IReadOnlyList<MoneyShare>? shares = null)
{
this.Amount = amount;
this._shares = shares ?? [];
this._pickupLock = new();
this.Position = position;
this.CurrentMap = map;
@@ -131,54 +137,28 @@ public sealed class DroppedMoney : AsyncDisposable, ILocateable
/// <returns><c>True</c>, if at least one player received money; Otherwise, <c>false</c>.</returns>
private bool TryGiveMoneyTo(Player player)
{
if (player.Party is { } party)
if (player.Party is not { } party)
{
var partyMembers = party.PartyList
.OfType<Player>()
.Where(p => p.CurrentMap == player.CurrentMap && !p.IsAtSafezone() && p.Attributes is { })
.ToList();
if (partyMembers.Count == 0)
if (!MoneyDistribution.TryPay(player, this.Amount))
{
player.Logger.LogDebug("No party member could receive the money, Player {0}, Money {1}", player, this);
player.Logger.LogDebug("Money could not be added to the inventory, Player {0}, Money {1}", player, this);
return false;
}
var share = (int)(this.Amount / partyMembers.Count);
var received = false;
foreach (var member in partyMembers)
{
received |= member.TryAddMoney((int)(share * member.Attributes![Stats.MoneyAmountRate]));
}
if (!received)
{
player.Logger.LogDebug("No party member could take the money, Player {0}, Money {1}", player, this);
}
return received;
return true;
}
var amountToAdd = (int)this.Amount;
if (player.GameContext?.Configuration?.ClampMoneyOnPickup ?? false)
var shares = this._shares.Count > 0
? this._shares
: MoneyDistribution.CreateEqualShares(this.Amount, party.PartyList.OfType<Player>().ToList());
var received = MoneyDistribution.TryPayShares(shares, member => party.IsEligibleForMoney(member, player));
if (!received)
{
var maxMoney = player.GameContext?.Configuration?.MaximumInventoryMoney ?? int.MaxValue;
amountToAdd = (int)Math.Min(this.Amount, (uint)Math.Max(0, maxMoney - player.Money));
if (amountToAdd <= 0)
{
player.Logger.LogDebug("Player is at maximum money limit, Player {0}, Money {1}", player, this);
return false;
}
player.Logger.LogDebug("No party member could take the money, Player {0}, Money {1}", player, this);
}
if (!player.TryAddMoney(amountToAdd))
{
player.Logger.LogDebug("Money could not be added to the inventory, Player {0}, Money {1}", player, this);
return false;
}
return true;
return received;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD100:Avoid async void methods", Justification = "Catching all Exceptions.")]