Skip to content

Commit

Permalink
HS-1293: implement cone intent for castables
Browse files Browse the repository at this point in the history
  • Loading branch information
baughj committed Apr 28, 2024
1 parent 1be7d83 commit 3c5da39
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
22 changes: 22 additions & 0 deletions hybrasyl/Objects/Creature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using Hybrasyl.Utility;

namespace Hybrasyl.Objects;

Expand Down Expand Up @@ -412,6 +414,26 @@ public virtual List<Creature> GetTargets(Castable castable, Creature target = nu
possibleTargets.Add(origin.GetDirectionalTarget(origin.GetIntentDirection(tile.Direction)));
}

foreach (var tile in intent.Cone)
{
var radius = Math.Min(tile.Radius, Game.ActiveConfiguration.Constants.ViewportSize / 2);
if (radius == 0)
continue;
var coneDirection = tile.Direction.Resolve(Direction);
for (var i = 1; i <= radius; i++)
{
var rect = coneDirection switch
{
Direction.North => new Rectangle(X - i + 1, Y - i, 2 * i - 1, 1),
Direction.South => new Rectangle(X - i + 1, Y + i, 2 * i - 1, 1),
Direction.East => new Rectangle(X + i, Y - i + 1, 1, 2 * i - i),
Direction.West => new Rectangle(X - i, Y - i + 1, 1, 2 * i - 1),
_ => throw new ArgumentOutOfRangeException(nameof(coneDirection)),
};
possibleTargets.AddRange(Map.EntityTree.GetObjects(rect).Where(predicate: e => e is Creature));
}
}

var possible = intent.MaxTargets > 0
? possibleTargets.Take(intent.MaxTargets).OfType<Creature>().ToList()
: possibleTargets.OfType<Creature>().ToList();
Expand Down
51 changes: 49 additions & 2 deletions hybrasyl/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using Hybrasyl.ChatCommands;
using Hybrasyl.Dialogs;

using Hybrasyl.Xml.Objects;

namespace Hybrasyl
{
Expand Down Expand Up @@ -324,5 +325,51 @@ public static class StringExtensions

public static string Normalize(string key) => Regex.Replace(key.ToLower(), @"\s+", "");
}

public static class DirectionExtensions
{
public static Direction Opposite(this Direction direction)
{
return direction switch
{
Direction.North => Direction.South,
Direction.South => Direction.North,
Direction.East => Direction.West,
Direction.West => Direction.East,
_ => throw new ArgumentOutOfRangeException(nameof(direction), direction, null),
};
}

public static Direction LeftOf(this Direction direction) => Opposite(RightOf(direction));

public static Direction RightOf(this Direction direction)
{
return direction switch
{
Direction.North => Direction.East,
Direction.South => Direction.West,
Direction.East => Direction.South,
Direction.West => Direction.North,
_ => throw new ArgumentOutOfRangeException(nameof(direction), direction, null),
};
}
}

public static class IntentDirectionExtensions
{
public static Direction Resolve(this IntentDirection intent, Direction direction)
{
return intent switch
{
IntentDirection.Front => direction,
IntentDirection.Back => direction.Opposite(),
IntentDirection.Left => direction.LeftOf(),
IntentDirection.Right => direction.RightOf(),
IntentDirection.None => direction,
_ => throw new ArgumentOutOfRangeException(nameof(intent), intent, null),
};
}

}
} // end Namespace:Utility
} // end Namespace: Hybrasyl

0 comments on commit 3c5da39

Please sign in to comment.