Skip to content

Commit

Permalink
showto() fixes for reactor; work on monster flee
Browse files Browse the repository at this point in the history
  • Loading branch information
baughj committed Jan 14, 2024
1 parent 63d0ff5 commit 36a38fc
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 36 deletions.
4 changes: 2 additions & 2 deletions Hybrasyl.Tests/Hybrasyl.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Serilog.Sinks.XUnit" Version="3.0.5" />
<PackageReference Include="xunit" Version="2.6.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
<PackageReference Include="xunit" Version="2.6.5" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
4 changes: 2 additions & 2 deletions hybrasyl/Hybrasyl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
<PackageReference Include="Hybrasyl.Xml" Version="0.9.3.29" />
<PackageReference Include="Hybrasyl.Xml" Version="0.9.3.30" />
<PackageReference Include="MoonSharp" Version="2.0.0" />
<PackageReference Include="MSBuildGitHash" Version="2.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NCalcSync" Version="3.9.0" />
<PackageReference Include="NCalcSync" Version="3.10.0" />
<PackageReference Include="NETStandard.Library" Version="2.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.7.0" />
Expand Down
37 changes: 37 additions & 0 deletions hybrasyl/Objects/ConditionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Hybrasyl.Enums;
using Hybrasyl.Xml.Objects;
using Newtonsoft.Json;
using StackExchange.Redis;

namespace Hybrasyl.Objects;

Expand Down Expand Up @@ -234,6 +235,42 @@ public bool Disoriented
}
}

public bool Feared
{
get => Conditions.HasFlag(CreatureCondition.Fear);
set
{
if (value == false)
Conditions &= ~CreatureCondition.Fear;
else
Conditions |= CreatureCondition.Fear;
}
}

public bool Disarmed
{
get => Conditions.HasFlag(CreatureCondition.Disarm);
set
{
if (value == false)
Conditions &= ~CreatureCondition.Disarm;
else
Conditions |= CreatureCondition.Disarm;
}
}

public bool Charmed
{
get => Conditions.HasFlag(CreatureCondition.Charm);
set
{
if (value == false)
Conditions &= ~CreatureCondition.Charm;
else
Conditions |= CreatureCondition.Charm;
}
}

// The following apply to users only

public bool Comatose
Expand Down
25 changes: 24 additions & 1 deletion hybrasyl/Objects/Monster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Hybrasyl.ChatCommands;

namespace Hybrasyl.Objects;

Expand All @@ -37,7 +38,8 @@ public enum MobAction
Attack,
Move,
Idle,
Death
Death,
Flee
}

public class Monster : Creature, ICloneable, IEphemeral
Expand Down Expand Up @@ -872,6 +874,12 @@ public void NextAction()
return;
}

if (Condition.Feared)
{
_actionQueue.Enqueue(MobAction.Flee);
return;
}

if (ThreatInfo.HighestThreat != null)
{
if (Distance(ThreatInfo.HighestThreat) == 1)
Expand All @@ -898,6 +906,21 @@ public void ProcessActions()
GameLog.SpawnDebug($"ActionQueue: {action}");
switch (action)
{
case MobAction.Flee:
// Run awaaaay!
var fleeFrom = ThreatInfo.LastCaster ?? ThreatInfo.HighestThreat ?? Target;
if (fleeFrom == null)
{
var dir = Relation(fleeFrom.X, fleeFrom.Y);
if (Direction == Opposite(dir))
Walk(dir);
else
{
Turn(dir);
Walk(dir);
}
}
break;
case MobAction.Attack:
var next = CastableController.GetNextCastable();
if (next is null)
Expand Down
29 changes: 17 additions & 12 deletions hybrasyl/Objects/Reactor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,25 @@ public bool Ready
public virtual List<DialogSequence> DialogSequences { get; set; } = new();
public virtual Dictionary<string, DialogSequence> SequenceIndex { get; set; } = new();

public bool VisibleTo(IVisible obj)
{
if (Expired) return false;
if (obj is not User user) return false;
var casterObj = Caster?.GetUserObject();
if (casterObj == null) return false;
if (VisibleToOwner && user.Name == Caster.Name) return true;
if (VisibleToGroup && casterObj != null && (casterObj.Group?.Contains(user) ?? false)) return true;
if (VisibleToCookies.Any(x => user.HasCookie(x))) return true;
if (user.Statuses.Any(x => VisibleToStatuses.Contains(x.Name))) return true;

return false;
}

public override void ShowTo(IVisible obj)
{
if (Expired) return;
if (!VisibleTo(obj)) return;
if (obj is not User user) return;

// TODO: improve, this isn't sufficient to work with Say/Shout currently
var p = new ServerPacket(0x07);
p.WriteUInt16(1);
Expand Down Expand Up @@ -215,17 +230,7 @@ public override void AoiEntry(VisibleObject obj)
if (!Ready || Caster == null) return;
var casterObj = Caster.GetUserObject();
if (obj is User user)
{
if (VisibleToOwner && user.Name == Caster.Name)
ShowTo(obj);
else if (VisibleToGroup && casterObj != null && (casterObj.Group?.Contains(user) ?? false))
ShowTo(obj);
else if (VisibleToCookies.Any(x => user.HasCookie(x)))
ShowTo(obj);
else if (user.Statuses.Any(x => VisibleToStatuses.Contains(x.Name)))
ShowTo(obj);
}

ShowTo(user);
Script.ExecuteFunction("AoiEntry", GetBaseEnvironment(obj));
}

Expand Down
27 changes: 8 additions & 19 deletions hybrasyl/Objects/ThreatInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@

namespace Hybrasyl.Objects;

public class ThreatEntry : IComparable
{
public ThreatEntry(Guid id)
{
Target = id;
}

public Guid Target { get; set; }
public class ThreatEntry(Guid id) : IComparable
{
public Guid Target { get; set; } = id;
public Creature TargetObject => Game.World.WorldState.GetWorldObject<Creature>(Target);
public uint Threat { get; set; }
public bool IsHealer => TotalHeals > 0;
Expand Down Expand Up @@ -41,16 +36,9 @@ public int CompareTo(object e)
}

[MoonSharpUserData]
public class ThreatInfo
public class ThreatInfo(Guid id)
{
public ThreatInfo(Guid id)
{
ThreatTableByCreature = new Dictionary<Guid, ThreatEntry>();
ThreatTableByThreat = new SortedDictionary<ThreatEntry, Guid>();
Owner = id;
}

public Guid Owner { get; set; }
public Guid Owner { get; set; } = id;
public Creature OwnerObject => Game.World.WorldState.GetWorldObject<Creature>(Owner);

public Creature HighestThreat => ThreatTableByThreat.Count == 0
Expand All @@ -60,8 +48,9 @@ public ThreatInfo(Guid id)
public ThreatEntry HighestThreatEntry => ThreatTableByThreat.First().Key;

public int Count => ThreatTableByCreature.Count;
public Dictionary<Guid, ThreatEntry> ThreatTableByCreature { get; }
public SortedDictionary<ThreatEntry, Guid> ThreatTableByThreat { get; }
public Dictionary<Guid, ThreatEntry> ThreatTableByCreature { get; } = new();
public SortedDictionary<ThreatEntry, Guid> ThreatTableByThreat { get; } = new();
public Creature LastCaster { get; set; }

public uint this[Creature threat]
{
Expand Down

0 comments on commit 36a38fc

Please sign in to comment.