01: ; stupid.g
02: 
03: ; start by moving up for 6 steps, then right for 6 steps, etc,
04: ; then always move into the direction closest to lambda-man's position
05: 
06: 
07: ; detect sudden jumps in position and reset the start counter
08:   int 3
09:   int 5
10:   sub a, [1]
11:   sub b, [2]
12:   add a, b
13:   add a, 1
14:   jlt nojump, a, 3
15:   mov [0], 0
16: 
17: nojump:
18:   int 3
19:   int 5
20:   mov [1], a
21:   mov [2], b
22:   jgt follow, [0], 24
23:   ; the start counter is low: try to get out of the start box
24:   inc [0]
25:   mov b, [0]
26:   div b, 6
27:   int 3
28:   add a, b
29:   and a, 3
30:   int 0
31:   hlt
32: 
33: follow:
34:   int 1     ; get lman's coordinates in A and B
35:   mov c, a  ; c = lman.x
36:   mov d, b  ; d = lman.y
37:   int 3     ; get this ghost's index
38:   int 5
39:   mov e, a  ; e = my.x
40:   mov f, b  ; f = my.y
41: 
42: ; compute direction
43: 
44:   mov a, 1     ; alpha
45:   mov b, 1     ; phi
46:   mov g, c
47:   sub g, e     ; absx = dx  = lman.x - my.x
48:   mov h, f
49:   sub h, d     ; absy = -dy = my.y - lman.y
50: 
51:   jgt l1, c, e     ; if lman.x =< my.x then
52:     add a, 6
53:     mov g, e
54:     sub g, c   ; absx = -dx
55:     xor b, 254 ; phi := -phi
56: l1:
57:   jlt l2, d, f     ; if lman.y >= my.y then
58:     xor a, 2
59:     mov h, d
60:     sub h, f   ; absy = dy
61:     xor b, 254 ; phi := -phi
62: 
63: l2:
64:   jlt l3, g, h    ; if absx >= absy then
65:     add a, b      ; add phi
66:     jmp l4
67: l3:               ; else
68:     sub a, b      ; subtract phi
69: 
70: l4:
71:   div a, 2        ; direction of lambda-man
72:   mov c, a
73: 
74: ; if panic mode, reverse direction
75: 
76:   int 3
77:   int 6
78:   jeq go, a, 0
79: 
80:   add c, 2
81: 
82: go:
83:   and c, 3        ; reduce modulo 4
84:   mov a, c
85:   int 0           ; direction is in a
86:   hlt
87: