Skip to content

Commit

Permalink
Add mob reward positioning for multi-drops
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaioru committed Dec 7, 2023
1 parent f97f035 commit faaa95f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public override IPacket GetEnterFieldPacket()
public override IPacket GetLeaveFieldPacket()
=> GetLeaveFieldPacket(1);

public IPacket GetEnterFieldPacket(byte enterType, IPoint2D? sourcePosition = null, short z = 0)
public IPacket GetEnterFieldPacket(byte enterType, IPoint2D? sourcePosition = null, short delay = 0)
{
using var packet = new PacketWriter(PacketSendOperations.DropEnterField);

Expand All @@ -61,7 +61,7 @@ public IPacket GetEnterFieldPacket(byte enterType, IPoint2D? sourcePosition = nu
if (enterType is 0 or 1 or 3 or 4)
{
packet.WritePoint2D(sourcePosition ?? Position);
packet.WriteShort(z);
packet.WriteShort(delay);
}

if (!IsMoney) // TODO: handle this properly
Expand Down
55 changes: 43 additions & 12 deletions src/common/Edelstein.Common.Gameplay.Game/Objects/Mob/FieldMob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Edelstein.Common.Utilities.Spatial;
using Edelstein.Protocol.Gameplay.Game.Combat.Damage;
using Edelstein.Protocol.Gameplay.Game.Objects;
using Edelstein.Protocol.Gameplay.Game.Objects.Drop;
using Edelstein.Protocol.Gameplay.Game.Objects.Mob;
using Edelstein.Protocol.Gameplay.Game.Objects.Mob.Stats;
using Edelstein.Protocol.Gameplay.Game.Objects.Mob.Stats.Modify;
Expand Down Expand Up @@ -94,29 +95,59 @@ public async Task Damage(int damage, IFieldUser? attacker = null, IPoint2D? posi
{
if (attacker != null)
{
var random = new Random();
var rewardPool = attacker.StageUser.Context.Managers.MobRewardPool;
var rewards = await rewardPool.CalculateRewards(attacker, this);
var offset = 0;
var index = 0;

foreach (var reward in rewards)
{
if (reward.ItemID == null) continue;
var template = await attacker.StageUser.Context.Templates.Item.Retrieve(reward.ItemID.Value);
if (template == null) continue;

var position = positionHit ?? Position;
var positionOffset = position.X + offset;

positionOffset = Math.Min(Field.Template.Bounds.MaxX - 25, positionOffset);
positionOffset = Math.Max(Field.Template.Bounds.MinX + 25, positionOffset);

var foothold = Field.Template.Footholds
.FindBelow(new Point2D(
position.X,
positionOffset,
position.Y - 100
))
.FirstOrDefault();
var drop = new FieldDropItem(
foothold?.Line.AtX(position.X) ?? position,
template.ToItemSlot(ItemVariationOption.Normal),
sourceID: ObjectID ?? 0
);

await Field.Enter(drop, () => drop.GetEnterFieldPacket(1, position));
AbstractFieldDrop? drop = null;

if (reward.ItemID > 0)
{
if (reward.ItemID == null) continue;
var template = await attacker.StageUser.Context.Templates.Item.Retrieve(reward.ItemID.Value);
if (template == null) continue;
var item = template.ToItemSlot(ItemVariationOption.Normal);

if (item is ItemSlotBundle bundle)
bundle.Number = (short)random.Next(reward.NumberMin ?? 1, reward.NumberMax ?? 1);

drop = new FieldDropItem(
foothold?.Line.AtX(positionOffset) ?? position,
item,
sourceID: ObjectID ?? 0
);
}
else if (reward.Money > 0)
{
drop = new FieldDropMoney(
foothold?.Line.AtX(positionOffset) ?? position,
(int)(reward.Money.Value * (random.Next(75, 100) / 100D)),
sourceID: ObjectID ?? 0
);
}

if (drop == null) continue;

await Field.Enter(drop, () => drop.GetEnterFieldPacket(1, position, (short)(0 * index)));

offset = offset < 0 ? Math.Abs(offset) : -(offset + 25);
index++;
}

_ = attacker.StageUser.Context.Managers.Quest.UpdateMobKill(attacker, Template.ID);
Expand Down

0 comments on commit faaa95f

Please sign in to comment.