From 5728ebc309b4753c939d751c7fd489b108e5dc09 Mon Sep 17 00:00:00 2001 From: Martin Kagamino Lehoux Date: Sun, 1 Mar 2026 21:52:49 +0100 Subject: fix: prevent entities from overlapping when arriving at the same cell Update UpdateMoveSystem to maintain the moveMap as entities move within a tick. When an entity snaps to a grid cell: - Block the new cell in the moveMap immediately - Unblock the old cell Before snapping, check if the target cell was already claimed by another entity earlier in the same tick. If so, repath instead of overlapping. --- integration_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'integration_test.go') diff --git a/integration_test.go b/integration_test.go index 8305719..b8f9ba5 100644 --- a/integration_test.go +++ b/integration_test.go @@ -5,6 +5,8 @@ import ( "time" "age_of_empires/physics" + + "github.com/stretchr/testify/assert" ) func TestGatherLoop(t *testing.T) { @@ -62,3 +64,31 @@ func TestGatherLoop(t *testing.T) { t.Errorf("Unit failed to gather resources within %d ticks. ResourceAmount: %d", maxTicks, game.ResourceAmount) } } + +func TestTwoEntitiesMovingToSameCellDontOverlap(t *testing.T) { + // Two units close together both ordered to the same cell should not overlap. + game := &Game{} + + target := physics.Point{X: 500, Y: 0} + + unitA := game.Append(NewEntityBuilder(). + WithPosition(physics.Point{X: 200, Y: 0}). + WithMove().WithOrder().Build()) + unitB := game.Append(NewEntityBuilder(). + WithPosition(physics.Point{X: 100, Y: 0}). + WithMove().WithOrder().Build()) + + moveMap := game.getMoveMap() + MainAction(game, unitA, target, moveMap) + MainAction(game, unitB, target, moveMap) + + // MOVE_SPEED=10, cell=100 → 10 ticks per cell. B detours around A → ~60 ticks. + for range 60 { + game.Update() + } + + assert.False(t, game.Move[unitA].IsActive, "Entity A should have stopped moving") + assert.False(t, game.Move[unitB].IsActive, "Entity B should have stopped moving") + assert.NotEqual(t, game.Position[unitA], game.Position[unitB], + "Entity A and B should NOT overlap: A at %s, B at %s", game.Position[unitA], game.Position[unitB]) +} -- cgit v1.3