Fix MPos projection

This commit is contained in:
Gustas
2025-07-31 20:01:26 +03:00
committed by Paul Chote
parent 831cce2f9c
commit fb5aae827a
2 changed files with 66 additions and 24 deletions

View File

@@ -78,17 +78,9 @@ namespace OpenRA
return new MPos(X, Y);
// Convert from RectangularIsometric cell (x, y) position to rectangular map position (u, v)
// - The staggered rows make this fiddly (hint: draw a diagram!)
// (a) Consider the relationships:
// - +1x (even -> odd) adds (0, 1) to (u, v)
// - +1x (odd -> even) adds (1, 1) to (u, v)
// - +1y (even -> odd) adds (-1, 1) to (u, v)
// - +1y (odd -> even) adds (0, 1) to (u, v)
// (b) Therefore:
// - ax + by adds (a - b)/2 to u (only even increments count)
// - ax + by adds a + b to v
var u = (X - Y) / 2;
// The staggered rows make this fiddly.
var v = X + Y;
var u = (v - (v & 1)) / 2 - Y;
return new MPos(u, v);
}

View File

@@ -10,6 +10,7 @@
#endregion
using System;
using System.Linq;
using NUnit.Framework;
namespace OpenRA.Test
@@ -17,25 +18,74 @@ namespace OpenRA.Test
[TestFixture]
public class CoordinateTest
{
[TestCase(TestName = "Test CPos and MPos conversion and back again.")]
public void CoarseToMapProjection()
[TestCase(TestName = "Test CPos to MPos conversion and back again.")]
public void CPosConversionRoundtrip()
{
foreach (var gridType in Enum.GetValues<MapGridType>())
{
for (var x = 0; x < 12; x++)
{
for (var y = 0; y < 12; y++)
{
var cell = new CPos(x, y);
var expected = new CellCoordsRegion(new CPos(-12, -12), new CPos(12, 12));
var actual = expected.Select(pos => pos.ToMPos(gridType).ToCPos(gridType)).ToArray();
// Known problem on isometric mods that shouldn't be visible to players as these are outside the map.
if (gridType == MapGridType.RectangularIsometric && y > x)
continue;
Assert.That(cell, Is.EqualTo(cell.ToMPos(gridType).ToCPos(gridType)));
}
}
Assert.That(expected, Is.EqualTo(actual));
}
}
[TestCase(TestName = "Test MPos to CPos conversion and back again.")]
public void MPosConversionRoundtrip()
{
foreach (var gridType in Enum.GetValues<MapGridType>())
{
var expected = new MapCoordsRegion(new MPos(-12, -12), new MPos(12, 12));
var actual = expected.Select(pos => pos.ToCPos(gridType).ToMPos(gridType)).ToArray();
Assert.That(expected, Is.EqualTo(actual));
}
}
[TestCase(TestName = "Test directional movement of ToCPos.")]
public void TestIsometricCPosConversion()
{
const MapGridType Isometric = MapGridType.RectangularIsometric;
Assert.That(new CPos(0, 0), Is.EqualTo(new MPos(0, 0).ToCPos(Isometric)));
Assert.That(new CPos(1, 1), Is.EqualTo(new MPos(0, 2).ToCPos(Isometric)));
Assert.That(new CPos(2, 2), Is.EqualTo(new MPos(0, 4).ToCPos(Isometric)));
Assert.That(new CPos(3, 3), Is.EqualTo(new MPos(0, 6).ToCPos(Isometric)));
Assert.That(new CPos(1, 0), Is.EqualTo(new MPos(0, 1).ToCPos(Isometric)));
Assert.That(new CPos(2, 0), Is.EqualTo(new MPos(1, 2).ToCPos(Isometric)));
Assert.That(new CPos(3, 0), Is.EqualTo(new MPos(1, 3).ToCPos(Isometric)));
Assert.That(new CPos(0, 1), Is.EqualTo(new MPos(-1, 1).ToCPos(Isometric)));
Assert.That(new CPos(0, 2), Is.EqualTo(new MPos(-1, 2).ToCPos(Isometric)));
Assert.That(new CPos(0, 3), Is.EqualTo(new MPos(-2, 3).ToCPos(Isometric)));
Assert.That(new CPos(1, -1), Is.EqualTo(new MPos(1, 0).ToCPos(Isometric)));
Assert.That(new CPos(2, -2), Is.EqualTo(new MPos(2, 0).ToCPos(Isometric)));
Assert.That(new CPos(3, -3), Is.EqualTo(new MPos(3, 0).ToCPos(Isometric)));
}
[TestCase(TestName = "Test directional movement of ToMPos.")]
public void TestIsometricMPosConversion()
{
const MapGridType Isometric = MapGridType.RectangularIsometric;
Assert.That(new MPos(0, 0), Is.EqualTo(new CPos(0, 0).ToMPos(Isometric)));
Assert.That(new MPos(0, 2), Is.EqualTo(new CPos(1, 1).ToMPos(Isometric)));
Assert.That(new MPos(0, 4), Is.EqualTo(new CPos(2, 2).ToMPos(Isometric)));
Assert.That(new MPos(0, 6), Is.EqualTo(new CPos(3, 3).ToMPos(Isometric)));
Assert.That(new MPos(0, 1), Is.EqualTo(new CPos(1, 0).ToMPos(Isometric)));
Assert.That(new MPos(1, 2), Is.EqualTo(new CPos(2, 0).ToMPos(Isometric)));
Assert.That(new MPos(1, 3), Is.EqualTo(new CPos(3, 0).ToMPos(Isometric)));
Assert.That(new MPos(-1, 1), Is.EqualTo(new CPos(0, 1).ToMPos(Isometric)));
Assert.That(new MPos(-1, 2), Is.EqualTo(new CPos(0, 2).ToMPos(Isometric)));
Assert.That(new MPos(-2, 3), Is.EqualTo(new CPos(0, 3).ToMPos(Isometric)));
Assert.That(new MPos(1, 0), Is.EqualTo(new CPos(1, -1).ToMPos(Isometric)));
Assert.That(new MPos(2, 0), Is.EqualTo(new CPos(2, -2).ToMPos(Isometric)));
Assert.That(new MPos(3, 0), Is.EqualTo(new CPos(3, -3).ToMPos(Isometric)));
}
}
}