Skip to content

Commit

Permalink
* HS-1280: improve fear
Browse files Browse the repository at this point in the history
* Add IsPrivileged to scripting
* Fix null reference under certain targeting conditions
  • Loading branch information
baughj committed Mar 31, 2024
1 parent 9d772b4 commit c3458b4
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 9 deletions.
2 changes: 2 additions & 0 deletions hybrasyl/Casting/Rotation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Serilog;

namespace Hybrasyl.Casting;

Expand Down Expand Up @@ -31,6 +32,7 @@ public Rotation(CreatureCastingSet set)
public bool Active { get; set; } = true;

public RotationEntry LastCastable { get; set; }

public RotationEntry CurrentCastable => Castables[CurrentIndex];

public RotationEntry NextCastable
Expand Down
53 changes: 53 additions & 0 deletions hybrasyl/Objects/Creature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography.Xml;

namespace Hybrasyl.Objects;

Expand Down Expand Up @@ -150,6 +151,58 @@ public override void OnClick(User invoker)
invoker.SendSystemMessage(ret);
}

public List<Direction> GetClearSides()
{
var sides = new List<Direction>();
if (Direction is Direction.North or Direction.South)
{
// Consider West, East
if (GetDirectionalTarget(Direction.West) == null && !Map.IsWall(GetCoordinatesInDirection(Direction.West)))
sides.Add(Direction.West);
if (GetDirectionalTarget(Direction.East) == null && !Map.IsWall(GetCoordinatesInDirection(Direction.East)))
sides.Add(Direction.East);
}

if (Direction is Direction.East or Direction.West)
{
// consider North, South
if (GetDirectionalTarget(Direction.North) == null && !Map.IsWall(GetCoordinatesInDirection(Direction.North)))
sides.Add(Direction.North);
if (GetDirectionalTarget(Direction.South) == null && !Map.IsWall(GetCoordinatesInDirection(Direction.South)))
sides.Add(Direction.South);
}
return sides;
}

public (byte x, byte y) GetCoordinatesInDirection(Direction direction)
{
var a = (int) X;
var b = (int) Y;

switch (direction)
{
case Direction.East:
a = X + 1;
b = Y;
break;
case Direction.West:
a = X - 1;
b = Y;
break;
case Xml.Objects.Direction.North:
a = X;
b = Y - 1;
break;
case Xml.Objects.Direction.South:
a = X;
b = Y + 1;
break;
}

return ((byte) Math.Clamp(a, byte.MinValue, byte.MaxValue), (byte) Math.Clamp(b, byte.MinValue, byte.MaxValue));

}

public Creature GetDirectionalTarget(Direction direction)
{
VisibleObject obj;
Expand Down
1 change: 1 addition & 0 deletions hybrasyl/Objects/MapObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public MapObject(Xml.Objects.Map newMap, World theWorld)

public bool IsWall(int x, int y) => IsWall((byte)x, (byte)y);
public bool IsWall(byte x, byte y) => Collisions.Contains((x, y));
public bool IsWall((byte x, byte y) coordinate) => IsWall(coordinate.x, coordinate.y);

public void ToggleCollisions(byte x, byte y)
{
Expand Down
21 changes: 12 additions & 9 deletions hybrasyl/Objects/Monster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ public void SendImmunityMessage(CreatureImmunity immunity, Creature attacker = n
}

}

public override void Damage(double damage, ElementType element = ElementType.None,
DamageType damageType = DamageType.Direct, DamageFlags damageFlags = DamageFlags.None,
Creature attacker = null, Castable castable = null, bool onDeath = true)
Expand Down Expand Up @@ -912,16 +913,18 @@ public void ProcessActions()
case MobAction.Flee:
// Run awaaaay!
var fleeFrom = ThreatInfo.LastCaster ?? ThreatInfo.HighestThreat ?? Target;
if (fleeFrom == null)
if (fleeFrom != null)
{
var dir = Relation(fleeFrom.X, fleeFrom.Y);
if (Direction == Opposite(dir))
Walk(dir);
else
{
Turn(dir);
Walk(dir);
}
if (Direction != Opposite(dir))
Turn(Opposite(dir));
if (Walk(Direction))
return;
// Something is blocking our path, can we move to either side?
var sides = GetClearSides();
if (sides.Count == 0)
return; // cower in fear, we can't move anywhere
Walk(sides.PickRandom());
}
break;
case MobAction.Attack:
Expand Down Expand Up @@ -983,7 +986,7 @@ public void ProcessActions()
}
case MobAction.Move:
{
var target = ThreatInfo.GetTargets(nextCastable.CurrentPriority).FirstOrDefault();
var target = ThreatInfo.GetTargets(nextCastable?.CurrentPriority ?? CreatureTargetPriority.RandomAttacker).FirstOrDefault();

if (target == null)
{
Expand Down
2 changes: 2 additions & 0 deletions hybrasyl/Scripting/HybrasylUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public HybrasylItemObject FirstInventorySlot

public override bool IsPlayer => true;

public bool IsPrivileged => User.AuthInfo.IsPrivileged;

/// <summary>
/// The gender of the player. For Darkages purpose, this will evaluate to Male or Female.
/// </summary>
Expand Down

0 comments on commit c3458b4

Please sign in to comment.