Files
ants/asm/twotrail.ant
2026-03-26 18:43:01 +01:00

136 lines
3.0 KiB
XML

; ── SWARM FORAGER ────────────────────────────
; Random walk to find food, dead reckoning to
; navigate home. Pheromone marks food for others.
; Hit play, then try improving it!
; ── Registers ──
.alias dx r1 ; x displacement from nest
.alias dy r2 ; y displacement from nest
.alias ret r3 ; return address for CALL
; ── Pheromone ──
.const PH_FOOD CH_RED ; marks food locations
.const PH_NEST CH_BLUE
.const PHER_INIT 255
.const PHER_DECAY 2
; ── Roles (visible in simulation viewer) ──
.tag 0 foraging
.tag 1 homing
; ──────────────────────────────────────────────
main:
set r7 PHER_INIT
loop:
CARRYING
JNE r0 0 go_home
TAG foraging
; ── SEARCH: find food ─────────────────────────
search:
SENSE FOOD
JNE r0 0 grab
; if we are too far from the food, dont mark
JLT r7 PHER_DECAY smell_trail
SUB r7 PHER_DECAY
; if there is already a trail here, dont mark
SNIFF PH_NEST HERE r0
JNE r0 0 smell_trail
MARK PH_NEST r7
smell_trail:
SMELL PH_FOOD
JNE r0 0 step
SENSE EMPTY
step:
CALL ret move_track
JMP loop
grab:
CALL ret move_track
PICKUP
MARK PH_FOOD 255
SET r6 PHER_INIT
JMP loop
; ── GO HOME: dead reckon toward nest ──────────
go_home:
TAG homing
; if we are too far from the food, dont mark
JLT r6 PHER_DECAY sense_nest
SUB r6 PHER_DECAY
; if there is already a trail here, dont mark
SNIFF PH_FOOD HERE r0
JNE r0 0 sense_nest
MARK PH_FOOD r6
sense_nest:
SENSE NEST
JNE r0 0 deliver
smell_nest:
SMELL PH_NEST
JNE r0 0 move_track
RANDOM r0 4
JEQ r0 0 wander ; 25% random to unstick walls
CALL ret home_dir
CALL ret move_track
JMP main
wander:
SENSE EMPTY
CALL ret move_track
JMP main
deliver:
CALL ret move_track
DROP
SET r7 PHER_INIT
JMP main
; ══════════════════════════════════════════════
; SUBROUTINES — CALL ret <label>, return JMP ret
; ══════════════════════════════════════════════
; home_dir: set r0 = direction toward nest
home_dir:
SET r4 dx
JGT dx 0 hd_dxp
SET r4 0
SUB r4 dx
hd_dxp:
SET r5 dy
JGT dy 0 hd_dyp
SET r5 0
SUB r5 dy
hd_dyp:
JGT r5 r4 hd_y
JGT dx 0 hd_w
SET r0 2
JMP ret
hd_w:
SET r0 4
JMP ret
hd_y:
JGT dy 0 hd_n
SET r0 3
JMP ret
hd_n:
SET r0 1
JMP ret
; move_track: MOVE r0, then update dx/dy
move_track:
MOVE r0
JEQ r0 1 mt_n
JEQ r0 2 mt_e
JEQ r0 3 mt_s
SUB dx 1
JMP ret
mt_n:
SUB dy 1
JMP ret
mt_e:
ADD dx 1
JMP ret
mt_s:
ADD dy 1
JMP ret