Skip to content

Commit

Permalink
[HS-1375] spell with maxtargets > actual targets hits target multiple…
Browse files Browse the repository at this point in the history
… times
  • Loading branch information
baughj committed Aug 22, 2023
1 parent 0749c88 commit 1019ab4
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
50 changes: 50 additions & 0 deletions Hybrasyl.Tests/Targeting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public void ClosestTargetFirst()
Fixture.TestUser.SkillBook.Clear();
Fixture.TestUser.SpellBook.Clear();
Fixture.TestUser.Stats.Level = 41; // Test trap formula for uses is 2 uses > 40, 1 use otherwise
Fixture.Map.Clear();

Fixture.TestUser.Teleport(Fixture.Map.Id, 20, 20);

var baitTemplate = Game.World.WorldData.Get<Creature>("Honey Bee");
Expand Down Expand Up @@ -57,4 +59,52 @@ public void ClosestTargetFirst()
Assert.Equal(Fixture.TestUser.Y, lastTarget.Y);
}

[Fact]
public void NoDuplicateTargets()
{

Fixture.TestUser.SkillBook.Clear();
Fixture.TestUser.SpellBook.Clear();
Fixture.TestUser.Teleport(Fixture.Map.Id, 20, 20);
Fixture.Map.Clear();
var baitTemplate = Game.World.WorldData.Get<Creature>("Honey Bee");

var bait = new Monster(baitTemplate, SpawnFlags.AiDisabled, 99)
{
Stats =
{
BaseHp = 500,
Hp = 500
},
Name = "Bee Bait",
X = (byte) (Fixture.TestUser.X - 1),
Y = Fixture.TestUser.Y
};
var bait2 = new Monster(baitTemplate, SpawnFlags.AiDisabled, 99)
{
Stats =
{
BaseHp = 500,
Hp = 500
},
Name = "Bee Bait",
X = (byte) (bait.X - 1),
Y = bait.Y
};

Fixture.Map.InsertCreature(bait);
Fixture.Map.InsertCreature(bait2);

var castable = Game.World.WorldData.GetByIndex<Castable>("athar meall");
Assert.NotNull(castable);

var targets = Fixture.TestUser.GetTargets(castable, bait);
Assert.Equal(2, targets.Count);
Fixture.Map.Clear();
Fixture.Map.InsertCreature(bait2);
var targets2 = Fixture.TestUser.GetTargets(castable, bait2);
Assert.Single(targets2);

}

}
3 changes: 2 additions & 1 deletion hybrasyl/Objects/Creature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,8 @@ public virtual List<Creature> GetTargets(Castable castable, Creature target = nu
}
}

return finalTargets;
// Lastly, remove any duplicates
return finalTargets.DistinctBy(x => x.Guid).ToList();
}

public virtual bool UseCastable(Castable castableXml, Creature target = null)
Expand Down
9 changes: 9 additions & 0 deletions hybrasyl/Objects/MapObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ public void MapUnmute()
AllowSpeaking = true;
}

public void Clear()
{
Objects = new HashSet<VisibleObject>();
Users = new Dictionary<string, User>();
Warps = new Dictionary<Tuple<byte, byte>, Warp>();
EntityTree = new QuadTree<VisibleObject>(1, 1, X, Y);
Reactors = new Dictionary<(byte X, byte Y), Dictionary<Guid, Reactor>>();
}

public void Init()
{
RawData = new byte[0];
Expand Down

0 comments on commit 1019ab4

Please sign in to comment.