From 64f0d402568e7436f0eece0e3b3165f199c2b677 Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Fri, 30 Oct 2026 11:05:20 +0100 Subject: [PATCH] fix: a called-back command heads home from where it turned round: its return's sent_at lies as far back as it still had to go, so the map's straight line puts it in the right spot Co-Authored-By: Claude Opus 5.5 (1M context) --- app/Engine/Rally.php | 6 +++++- docs/architecture.md | 2 +- tests/Feature/RallyTest.php | 22 ++++++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/Engine/Rally.php b/app/Engine/Rally.php index 19ce269..6db5420 100644 --- a/app/Engine/Rally.php +++ b/app/Engine/Rally.php @@ -190,13 +190,17 @@ final class Rally ->where('subject_id', $movement->id)->whereNull('processed_at')->delete(); Push::queue($movement->target_id); + // Home on the same line at the same pace: as if it had left the target as long ago + // as it still had to go, so a straight line from sent_at to arrives_at puts it where + // it turned round. $elapsed = $now->getTimestampMs() - $movement->sent_at->getTimestampMs(); + $left = $movement->arrives_at->getTimestampMs() - $now->getTimestampMs(); $movement->update([ 'type' => Movement::RETURN, 'origin_id' => $movement->target_id, 'target_id' => $this->village->id, 'catapult_target' => null, - 'sent_at' => $now, + 'sent_at' => $now->subMilliseconds($left), 'arrives_at' => $now->addMilliseconds($elapsed), 'visible_at' => $now, ]); diff --git a/docs/architecture.md b/docs/architecture.md index faf5f0b..9e81453 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -198,7 +198,7 @@ Every group on the map is a `movement`: an attack, support, return, or trade car - The rally point sends an attack or support from the units at home to the village at x|y (`rally.send`; `rally.preview` shows the target, distance and travel time first). A village needs a rally point to send anything. Travel time is the slowest unit's minutes per field × distance / (world speed × `movement.unit_speed` × mode speed), with the distance as the crow flies. - The troops leave at once. Their arrival is a `movement.arrives` event on the target, so it runs under the target's lock and in the target's event order; a `movement.spotted` event at `visible_at` only pushes the change. Reading a village first catches up the targets of its own due groups, so the owner never sees a group that should have arrived. -- Within `movement.cancel_window` (10 real minutes) the sender can call a command back. It turns around and takes as long to get home as it had been under way. +- Within `movement.cancel_window` (10 real minutes) the sender can call a command back. It turns around where it is and takes as long to get home as it had been under way. The return runs from the target with `sent_at` set as far back as the group still had to go, so the straight line from `sent_at` to `arrives_at` puts it where it turned. - An attack with survivors turns into its own return trip with the loot, taking as long as the way there. Loot that doesn't fit the storage is lost on arrival. - Support stands in the host village as unit stacks with their own `home_village_id`. The host feeds it (it counts against the host's food budget), and hunger sends it home before any own soldier deserts. The host can send any support home (`rally.sendHome`), and the owner can withdraw it (`rally.withdraw`, a `support.recalled` event on the host's queue); both start a return trip. Groups under way eat nothing. - Barbarian villages take no support, and own villages can't be attacked. diff --git a/tests/Feature/RallyTest.php b/tests/Feature/RallyTest.php index 9d59eb5..5159aa1 100644 --- a/tests/Feature/RallyTest.php +++ b/tests/Feature/RallyTest.php @@ -152,6 +152,28 @@ it('calls an attack back within the cancel window', function () { expect(rule(fn () => app(CancelMovement::class)(owner($village), $village->id, Movement::sole()->id)))->toBe('too_late'); }); +it('draws a called-back command where it turned round, not at the target', function () { + $village = rallied(startVillage(), ['spear' => 10]); + $barbarian = barbarianNear($village, 3, 4); + // 5 fields at 18 minutes: 5400 s out. + send($village, $barbarian, ['spear' => 10]); + + $this->travel(600)->seconds(); + app(CancelMovement::class)(owner($village), $village->id, Movement::sole()->id); + $back = Movement::sole(); + // On the line from the target home at the pace it marched, it stands 600 s from home. + $total = $back->arrives_at->getTimestamp() - $back->sent_at->getTimestamp(); + $done = now()->getTimestamp() - $back->sent_at->getTimestamp(); + expect($back->origin_id)->toBe($barbarian->id) + ->and($back->target_id)->toBe($village->id) + ->and($back->arrives_at->toIso8601ZuluString())->toBe('2026-10-29T12:20:00Z') + ->and($total)->toBe(5400) + ->and($done)->toBe(4800); + + $this->travel(600)->seconds(); + expect((array) shown($village)['troops'])->toBe(['spear' => 10]); +}); + it('refuses what the rally point cannot do', function () { $village = startVillage(); $other = startVillage();