Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified documentation/brushfire.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified documentation/flow-field-path.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified documentation/flow-field.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified documentation/goal-based-pathfinder.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified documentation/jump-point-search-pathfinder.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 23 additions & 41 deletions test/NosCore.PathFinder.Tests/BrushFireTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NosCore.PathFinder.Brushfire;
using NosCore.PathFinder.Heuristic;
using SixLabors.Fonts;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;

namespace NosCore.PathFinder.Tests
{
Expand All @@ -27,49 +22,36 @@ public void Test_BrushFire()
{
(short X, short Y) characterPosition = (6, 10);
var brushFire = _map.LoadBrushFire(characterPosition, new OctileDistanceHeuristic());
using var image = new Image<Rgba32>(_map.Width * TestHelper.Scale, _map.Height * TestHelper.Scale);
var listPixel = new List<Rgba32>();
var image = new MapCanvas(_map.Width * TestHelper.Scale, _map.Height * TestHelper.Scale);
var listPixel = new List<Rgba>();
TestHelper.DrawMap(_map, TestHelper.Scale, listPixel, image, (0, 0), characterPosition);
var font = TestHelper.GetFont();

image.Mutate(ctx =>
for (short y = 0; y < _map.Height; y++)
{
for (short y = 0; y < _map.Height; y++)
for (short x = 0; x < _map.Width; x++)
{
for (short x = 0; x < _map.Width; x++)
if ((x, y) == characterPosition)
{
var rect = new RectangleF(x * TestHelper.Scale, y * TestHelper.Scale, TestHelper.Scale, TestHelper.Scale);
if ((x, y) != characterPosition)
{
if (brushFire[x, y] != null)
{
ctx.Fill(Color.White, rect);
var alpha = (byte)((brushFire[x, y] * 12 > 255 ? 255 : (brushFire[x, y] ?? 0) * 12));
var color = new Rgba32(0, 0, 255, alpha);
var textOptions = new RichTextOptions(font)
{
Origin = new PointF(x * TestHelper.Scale + TestHelper.Scale / 2f, y * TestHelper.Scale + TestHelper.Scale / 2f),
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
ctx.DrawText(textOptions, brushFire[x, y]?.ToString("N0") ?? "", Color.Black);
ctx.Fill(color, rect);
listPixel.Add(color);
}
else
{
var textOptions = new RichTextOptions(font)
{
Origin = new PointF(x * TestHelper.Scale + TestHelper.Scale / 2f, y * TestHelper.Scale + TestHelper.Scale / 2f),
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
ctx.DrawText(textOptions, "∞", Color.White);
}
}
continue;
}

var centerX = x * TestHelper.Scale + TestHelper.Scale / 2f;
var centerY = y * TestHelper.Scale + TestHelper.Scale / 2f;
if (brushFire[x, y] != null)
{
image.FillRect(x * TestHelper.Scale, y * TestHelper.Scale, TestHelper.Scale, TestHelper.Scale, Colors.White);
var alpha = (byte)((brushFire[x, y] * 12 > 255 ? 255 : (brushFire[x, y] ?? 0) * 12));
var color = new Rgba(0, 0, 255, alpha);
image.DrawTextCentered(centerX, centerY, brushFire[x, y]?.ToString("N0") ?? "", Colors.Black);
image.FillRect(x * TestHelper.Scale, y * TestHelper.Scale, TestHelper.Scale, TestHelper.Scale, color);
listPixel.Add(color);
}
else
{
image.DrawTextCentered(centerX, centerY, "∞", Colors.White);
}
}
});
}

TestHelper.VerifyFile("brushfire.png", image, listPixel, "Brushfire");
}
Expand Down
95 changes: 33 additions & 62 deletions test/NosCore.PathFinder.Tests/FlowFieldTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NosCore.PathFinder.Brushfire;
using NosCore.PathFinder.Heuristic;
using SixLabors.Fonts;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;

namespace NosCore.PathFinder.Tests
{
Expand All @@ -30,31 +25,24 @@ public void Test_FlowField()
var brushFire = _map.LoadBrushFire(characterPosition, new OctileDistanceHeuristic());
var flowField = brushFire.GetFlowField(_map);

using var image = new Image<Rgba32>(_map.Width * TestHelper.Scale, _map.Height * TestHelper.Scale);
var listPixel = new List<Rgba32>();
var image = new MapCanvas(_map.Width * TestHelper.Scale, _map.Height * TestHelper.Scale);
var listPixel = new List<Rgba>();
TestHelper.DrawMap(_map, TestHelper.Scale, listPixel, image, (0, 0), characterPosition);

image.Mutate(ctx =>
for (short y = 0; y < _map.Height; y++)
{
for (short y = 0; y < _map.Height; y++)
for (short x = 0; x < _map.Width; x++)
{
for (short x = 0; x < _map.Width; x++)
if ((x, y) != characterPosition && brushFire[x, y] != null)
{
var rect = new RectangleF(x * TestHelper.Scale, y * TestHelper.Scale, TestHelper.Scale, TestHelper.Scale);
if ((x, y) != characterPosition)
{
if (brushFire[x, y] != null)
{
ctx.Fill(Color.White, rect);
var alpha = (byte)((brushFire[x, y] * 12 > 255 ? 255 : (brushFire[x, y] ?? 0) * 12));
var color = new Rgba32(0, 0, 255, alpha);
ctx.Fill(color, rect);
listPixel.Add(color);
}
}
image.FillRect(x * TestHelper.Scale, y * TestHelper.Scale, TestHelper.Scale, TestHelper.Scale, Colors.White);
var alpha = (byte)((brushFire[x, y] * 12 > 255 ? 255 : (brushFire[x, y] ?? 0) * 12));
var color = new Rgba(0, 0, 255, alpha);
image.FillRect(x * TestHelper.Scale, y * TestHelper.Scale, TestHelper.Scale, TestHelper.Scale, color);
listPixel.Add(color);
}
}
});
}

for (short y = 0; y < _map.Height; y++)
{
Expand All @@ -65,7 +53,7 @@ public void Test_FlowField()
var vector = flowField[x, y];
if (vector != null)
{
TestHelper.DrawArrow(image, x, y, vector.Value.X, vector.Value.Y, TestHelper.Scale, Color.White.ToPixel<Rgba32>());
TestHelper.DrawArrow(image, x, y, vector.Value.X, vector.Value.Y, TestHelper.Scale, Colors.White);
}
}
}
Expand All @@ -85,32 +73,24 @@ public void Test_FlowField_MonsterPath()

var path = TraceFlowFieldPath(flowField, monsterPosition, characterPosition);

using var image = new Image<Rgba32>(_map.Width * TestHelper.Scale, _map.Height * TestHelper.Scale);
var listPixel = new List<Rgba32>();
var image = new MapCanvas(_map.Width * TestHelper.Scale, _map.Height * TestHelper.Scale);
var listPixel = new List<Rgba>();
TestHelper.DrawMap(_map, TestHelper.Scale, listPixel, image, monsterPosition, characterPosition);
var font = TestHelper.GetFont();

image.Mutate(ctx =>
for (short y = 0; y < _map.Height; y++)
{
for (short y = 0; y < _map.Height; y++)
for (short x = 0; x < _map.Width; x++)
{
for (short x = 0; x < _map.Width; x++)
if ((x, y) != characterPosition && (x, y) != monsterPosition && brushFire[x, y] != null)
{
var rect = new RectangleF(x * TestHelper.Scale, y * TestHelper.Scale, TestHelper.Scale, TestHelper.Scale);
if ((x, y) != characterPosition && (x, y) != monsterPosition)
{
if (brushFire[x, y] != null)
{
ctx.Fill(Color.White, rect);
var alpha = (byte)((brushFire[x, y] * 12 > 255 ? 255 : (brushFire[x, y] ?? 0) * 12));
var color = new Rgba32(0, 0, 255, alpha);
ctx.Fill(color, rect);
listPixel.Add(color);
}
}
image.FillRect(x * TestHelper.Scale, y * TestHelper.Scale, TestHelper.Scale, TestHelper.Scale, Colors.White);
var alpha = (byte)((brushFire[x, y] * 12 > 255 ? 255 : (brushFire[x, y] ?? 0) * 12));
var color = new Rgba(0, 0, 255, alpha);
image.FillRect(x * TestHelper.Scale, y * TestHelper.Scale, TestHelper.Scale, TestHelper.Scale, color);
listPixel.Add(color);
}
}
});
}

for (short y = 0; y < _map.Height; y++)
{
Expand All @@ -121,34 +101,25 @@ public void Test_FlowField_MonsterPath()
var vector = flowField[x, y];
if (vector != null)
{
TestHelper.DrawArrow(image, x, y, vector.Value.X, vector.Value.Y, TestHelper.Scale, Color.White.ToPixel<Rgba32>());
TestHelper.DrawArrow(image, x, y, vector.Value.X, vector.Value.Y, TestHelper.Scale, Colors.White);
}
}
}
}

var pathArray = path.ToArray();
image.Mutate(ctx =>
for (var i = 0; i < pathArray.Length; i++)
{
for (var i = 0; i < pathArray.Length; i++)
var (x, y) = pathArray[i];
if ((x, y) != monsterPosition && (x, y) != characterPosition)
{
var (x, y) = pathArray[i];
if ((x, y) != monsterPosition && (x, y) != characterPosition)
{
var rect = new RectangleF(x * TestHelper.Scale, y * TestHelper.Scale, TestHelper.Scale, TestHelper.Scale);
var color = Color.LightPink;
ctx.Fill(color, rect);
var textOptions = new RichTextOptions(font)
{
Origin = new PointF(x * TestHelper.Scale + TestHelper.Scale / 2f, y * TestHelper.Scale + TestHelper.Scale / 2f),
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
ctx.DrawText(textOptions, i.ToString(), Color.Black);
listPixel.Add(color.ToPixel<Rgba32>());
}
var color = Colors.LightPink;
image.FillRect(x * TestHelper.Scale, y * TestHelper.Scale, TestHelper.Scale, TestHelper.Scale, color);
image.DrawTextCentered(x * TestHelper.Scale + TestHelper.Scale / 2f,
y * TestHelper.Scale + TestHelper.Scale / 2f, i.ToString(), Colors.Black);
listPixel.Add(color);
}
});
}

TestHelper.VerifyFile("flow-field-path.png", image, listPixel, "Flow Field Path (Monster following vectors to Player)");
}
Expand Down
91 changes: 32 additions & 59 deletions test/NosCore.PathFinder.Tests/GoalBasedPathfinderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
using NosCore.PathFinder.Heuristic;
using NosCore.PathFinder.Interfaces;
using NosCore.PathFinder.Pathfinder;
using SixLabors.Fonts;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;

namespace NosCore.PathFinder.Tests
{
Expand All @@ -39,73 +34,51 @@ public GoalBasedPathfinderTests()
[TestMethod]
public void Test_GoalBasedPathfinder()
{
using var image = new Image<Rgba32>(_map.Width * TestHelper.Scale, _map.Height * TestHelper.Scale);
var image = new MapCanvas(_map.Width * TestHelper.Scale, _map.Height * TestHelper.Scale);
(short X, short Y) target = (15, 16);
var listPixel = new List<Rgba32>();
var listPixel = new List<Rgba>();
TestHelper.DrawMap(_map, TestHelper.Scale, listPixel, image, target, _characterPosition);
var font = TestHelper.GetFont();

image.Mutate(ctx =>
for (short y = 0; y < _map.Height; y++)
{
for (short y = 0; y < _map.Height; y++)
for (short x = 0; x < _map.Width; x++)
{
for (short x = 0; x < _map.Width; x++)
if ((x, y) == target || (x, y) == _characterPosition)
{
var rect = new RectangleF(x * TestHelper.Scale, y * TestHelper.Scale, TestHelper.Scale, TestHelper.Scale);
if ((x, y) != target && (x, y) != _characterPosition)
{
if (_brushFire[x, y] != null)
{
ctx.Fill(Color.White, rect);
var alpha = (byte)((_brushFire[x, y] * 12 > 255 ? 255 : (_brushFire[x, y] ?? 0) * 12));
var color = new Rgba32(0, 0, 255, alpha);
var textOptions = new RichTextOptions(font)
{
Origin = new PointF(x * TestHelper.Scale + TestHelper.Scale / 2f, y * TestHelper.Scale + TestHelper.Scale / 2f),
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
ctx.DrawText(textOptions, _brushFire[x, y]?.ToString("N0") ?? "", Color.Black);
ctx.Fill(color, rect);
listPixel.Add(color);
}
else
{
var textOptions = new RichTextOptions(font)
{
Origin = new PointF(x * TestHelper.Scale + TestHelper.Scale / 2f, y * TestHelper.Scale + TestHelper.Scale / 2f),
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
ctx.DrawText(textOptions, "∞", Color.White);
}
}
continue;
}
}
});

var centerX = x * TestHelper.Scale + TestHelper.Scale / 2f;
var centerY = y * TestHelper.Scale + TestHelper.Scale / 2f;
if (_brushFire[x, y] != null)
{
image.FillRect(x * TestHelper.Scale, y * TestHelper.Scale, TestHelper.Scale, TestHelper.Scale, Colors.White);
var alpha = (byte)((_brushFire[x, y] * 12 > 255 ? 255 : (_brushFire[x, y] ?? 0) * 12));
var color = new Rgba(0, 0, 255, alpha);
image.DrawTextCentered(centerX, centerY, _brushFire[x, y]?.ToString("N0") ?? "", Colors.Black);
image.FillRect(x * TestHelper.Scale, y * TestHelper.Scale, TestHelper.Scale, TestHelper.Scale, color);
listPixel.Add(color);
}
else
{
image.DrawTextCentered(centerX, centerY, "∞", Colors.White);
}
}
}

var path = _goalPathfinder.FindPath(target, _characterPosition).ToList();
image.Mutate(ctx =>
foreach (var (x, y) in path)
{
foreach (var (x, y) in path)
if ((x, y) != target && (x, y) != _characterPosition)
{
if ((x, y) != target && (x, y) != _characterPosition)
{
var rect = new RectangleF(x * TestHelper.Scale, y * TestHelper.Scale, TestHelper.Scale, TestHelper.Scale);
var color = Color.LightPink;
ctx.Fill(color, rect);
var textOptions = new RichTextOptions(font)
{
Origin = new PointF(x * TestHelper.Scale + TestHelper.Scale / 2f, y * TestHelper.Scale + TestHelper.Scale / 2f),
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
ctx.DrawText(textOptions, Array.IndexOf(path.ToArray(), (x, y)).ToString(), Color.Black);
listPixel.Add(color.ToPixel<Rgba32>());
}
var color = Colors.LightPink;
image.FillRect(x * TestHelper.Scale, y * TestHelper.Scale, TestHelper.Scale, TestHelper.Scale, color);
image.DrawTextCentered(x * TestHelper.Scale + TestHelper.Scale / 2f,
y * TestHelper.Scale + TestHelper.Scale / 2f,
Array.IndexOf(path.ToArray(), (x, y)).ToString(), Colors.Black);
listPixel.Add(color);
}
});
}

TestHelper.VerifyFile("goal-based-pathfinder.png", image, listPixel, "Goal Based Pathfinder");
}
Expand Down
Loading
Loading