001: ; idiot.g
002: 
003: ; a variant of stupid.g
004: 
005: ; start by moving up for 6 steps, then right for 6 steps, etc,
006: ; then always move into the available direction closest to lambda-man's position
007: 
008: 
009: ; detect sudden jumps in position and reset the start counter
010:   int 3
011:   int 5
012:   sub a, [1]
013:   sub b, [2]
014:   add a, b
015:   add a, 1
016:   jlt nojump, a, 3
017:   mov [0], 0
018: 
019: nojump:
020:   int 3
021:   int 5
022:   mov [1], a
023:   mov [2], b
024:   jgt follow, [0], 24
025:   ; the start counter is low: try to get out of the start box
026:   inc [0]
027:   mov b, [0]
028:   div b, 6
029:   int 3
030:   add a, b
031:   and a, 3
032:   int 0
033:   hlt
034: 
035: follow:
036:   int 1     ; get lman's coordinates in A and B
037:   mov c, a  ; c = lman.x
038:   mov d, b  ; d = lman.y
039:   int 3     ; get this ghost's index
040:   int 5
041:   mov e, a  ; e = my.x
042:   mov f, b  ; f = my.y
043: 
044: ; compute direction
045: 
046:   mov a, 1     ; alpha
047:   mov b, 1     ; phi
048:   mov g, c
049:   sub g, e     ; absx = dx  = lman.x - my.x
050:   mov h, f
051:   sub h, d     ; absy = -dy = my.y - lman.y
052: 
053:   jgt l1, c, e     ; if lman.x =< my.x then
054:     add a, 6
055:     mov g, e
056:     sub g, c   ; absx = -dx
057:     xor b, 254 ; phi := -phi
058: l1:
059:   jlt l2, d, f     ; if lman.y >= my.y then
060:     xor a, 2
061:     mov h, d
062:     sub h, f   ; absy = dy
063:     xor b, 254 ; phi := -phi
064: 
065: 
066: l2:
067: ; from now on, c and d are the main and secondary direction
068:   mov c, a
069:   mov d, a
070: 
071:   jlt l3, g, h    ; if absx >= absy then
072:     xor b, 254    ; phi := -phi
073: l3:
074:     sub c, b      ; add phi for main direction
075:     add d, b      ; subtract phi for secondary direction
076: 
077: l4:
078:   div c, 2        ; main direction of lambda-man
079:   div d, 2        ; secondary direction
080: ; if panic mode, reverse direction
081: 
082:   int 3
083:   int 6
084:   jeq test, a, 0
085: 
086:   add c, 2
087:   add d, 2
088: 
089: test:
090:   int 3
091:   int 5
092:   and c, 3
093:   mov [254], c
094:   mov [255], ret1
095:   mov PC, next    ; get coordinates of square in direction c
096: ret1:
097:   int 7           ; get square
098:   jeq secondary, a, 0    ; go in main direction if not wall
099: 
100:   int 3
101:   int 6
102:   add b, 2
103:   and b, 3
104:   and c, 3
105:   jeq secondary, b, c
106:   jmp go
107: 
108: secondary:
109:   int 8
110:   mov c, d        ; go in secondary direction
111: 
112: go:
113:   and c, 3        ; reduce modulo 4
114:   mov a, c
115:   int 0           ; direction is in a
116:   hlt
117: 
118: 
119: 
120: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
121: ; next a <- X, b <- Y, [254] <- DIR, [255] <- RETURN
122: next:
123:         MUL [254], 2
124:         ADD [254], 3
125:         MOV H, PC
126:         ADD H, [254]
127:         MOV PC, H
128:         SUB B, 1                ; code for 0
129:         JMP next_end
130:         ADD A, 1                ; code for 1
131:         JMP next_end
132:         ADD B, 1                ; code for 2
133:         JMP next_end
134:         SUB A, 1                ; code for 3
135: next_end:
136:         MOV PC, [255]
137: