-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_loop_create_food.asm
executable file
·409 lines (279 loc) · 7.25 KB
/
3_loop_create_food.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
; /////////////////////////////////////////////////////////////////////////////
; Znake (ZX Spectrum 48K)
; -----------------------------------------------------------------------------
; 3_loop_create_food.asm
; -----------------------------------------------------------------------------
; Copyright (C) 2016, Chris Wyatt
; All rights reserved
; Distributed under the Apache 2 license (see LICENSE)
; /////////////////////////////////////////////////////////////////////////////
next_game_loop:
; Create a new snake history table, where the data starts at the beginning
; of the table: this means we can free up a register later, as we will not
; need a counter
ld h,(TBL_SNAKE_HISTORY >> 8) & $FF
ld a,(snake_history_tail_offset)
ld l,a
; Initialise counter
ld a,(snake_history_head_offset)
sub l
inc a
ld b,a
ld (snake_history_clean_length),a
ld de,TBL_SNAKE_HISTORY_CLEAN
transfer_byte:
; Transfer from one table to another. LDIR is not used here, as we need to
; jump to the beginning at table boundaries.
ld a,(hl)
ld (de),a
inc l
inc e
dec b
jr nz,transfer_byte
; Invalidate any free square quadrants that contain the new position for
; the head
ld h,(TBL_SNAKE_HISTORY_CLEAN >> 8) & $FF
ld a,(snake_history_clean_length)
dec a
ld l,a
ld a,(hl)
bit 3,a
jr nz,ivsq_head_in_bottom_half
; Head in top half
bit 7,a
jr nz,ivsq_head_in_top_right
; Head in top left
ld hl,flags
set 7,(hl)
jp find_free_space
ivsq_head_in_top_right:
ld hl,flags
set 6,(hl)
jp find_free_space
ivsq_head_in_bottom_half:
bit 7,a
jr nz,ivsq_head_in_bottom_right
; Head in bottom left
ld hl,flags
set 4,(hl)
jp find_free_space
ivsq_head_in_bottom_right:
ld hl,flags
set 5,(hl)
find_free_space:
; The following routine finds free space that is not occupied by the snake, and
; places new food in this free space. It divides the map into 4 quadrants
; (TR, BR, BL, TL); any quadrant that does not contain snake is added to the
; free squares tables; any quadrant that does contain snake is further divided
; into quadrants until all free space is mapped.
ld ix,next_quadrant
ld iy,check_quadrant
; Check bottom-left free squares cache invalid
ld a,(flags)
bit 4,a
jr z,check_br_invalid
; d: location of top-left quadrant
; e: offset to bottom-left quadrant / width / height
ld de,0x0804
ld hl,free_squares_bl_length
ld (hl),0
ld (free_squares_length_pointer),hl
ld a,TBL_FREE_SQUARES_BL_TL_TABLE & 0x00ff
ld (free_squares_low),a
call check_quadrants
ld hl,flags
res 4,(hl)
check_br_invalid:
ld a,(flags)
bit 5,a
jr z,check_tr_invalid
; d: location of top-left quadrant
; e: offset to bottom-left quadrant / width / height
ld de,0x8804
ld hl,free_squares_br_length
ld (hl),0
ld (free_squares_length_pointer),hl
ld a,TBL_FREE_SQUARES_BR_TL_TABLE & 0x00ff
ld (free_squares_low),a
call check_quadrants
ld hl,flags
res 5,(hl)
check_tr_invalid:
ld a,(flags)
bit 6,a
jr z,check_tl_invalid
; d: location of top-left quadrant
; e: offset to bottom-left quadrant / width / height
ld de,0x8004
ld hl,free_squares_tr_length
ld (hl),0
ld (free_squares_length_pointer),hl
ld a,TBL_FREE_SQUARES_TR_TL_TABLE & 0x00ff
ld (free_squares_low),a
call check_quadrants
ld hl,flags
res 6,(hl)
check_tl_invalid:
ld a,(flags)
bit 7,a
jr z,check_create_food
; d: location of top-left quadrant
; e: offset to bottom-left quadrant / width / height
ld de,0x0004
ld hl,free_squares_tl_length
ld (hl),0
ld (free_squares_length_pointer),hl
ld a,TBL_FREE_SQUARES_TL_TL_TABLE & 0x00ff
ld (free_squares_low),a
call check_quadrants
ld hl,flags
res 7,(hl)
check_create_food:
ld iy,draw_line
; If food eaten flag is not set, jump
ld a,(flags)
bit 0,a
jp z,invalidate_free_square_quadrants_tail
; Create food
; If a quadrant does not contain free squares, it is skipped. The counter
; will be decremented for each quadrant checked.
ld b,5
; Pick a random quadrant
call random
and 0xc0
jp pe,create_food_left
; Otherwise, random quadrant is on the right
bit 7,a
jr nz,create_food_tr
create_food_br:
djnz create_food_br_continue
jp collision
create_food_br_continue:
ld a,(free_squares_br_length)
or a
jr z,create_food_tr
ld e,TBL_FREE_SQUARES_BR_TL_TABLE & 0x00ff
jp create_food
create_food_left:
jr nz,create_food_tl
create_food_bl:
djnz create_food_bl_continue
jp collision
create_food_bl_continue:
ld a,(free_squares_bl_length)
or a
jr z,create_food_br
ld e,TBL_FREE_SQUARES_BL_TL_TABLE & 0x00ff
jp create_food
create_food_tr:
djnz create_food_tr_continue
jp collision
create_food_tr_continue:
ld a,(free_squares_tr_length)
or a
jr z,create_food_tl
ld e,TBL_FREE_SQUARES_TR_TL_TABLE & 0x00ff
jp create_food
create_food_tl:
djnz create_food_tl_continue
jp collision
create_food_tl_continue:
ld a,(free_squares_tl_length)
or a
jr z,create_food_bl
ld e,TBL_FREE_SQUARES_TL_TL_TABLE & 0x00ff
create_food:
; Pick a random free square
; Random number limit
ld c,a
call random
ld b,a
call modulo
add a,e
ld l,a
; High byte is the same for BL, BR, TR and TL tables
ld h,(TBL_FREE_SQUARES_BL_TL_TABLE >> 8) & $FF
ld d,(hl)
inc h ; TBL_FREE_SQUARES_??_BR_TABLE
ld e,(hl)
; Pick a random position within the free square
; Load square width/height to register h
ld a,e
sub d
ld h,a
; Random number limit
and 0x0f
ld c,a
; X-coordinate
push hl
call random
pop hl
ld b,a
call modulo
rlca
rlca
rlca
rlca
and 0xf0
ld b,a
; Y-coordinate
push hl
call random
pop hl
push bc
call modulo
pop bc
or b
; Add location of top-left of square, to get the actual food location
add a,d
ld (current_food_location),a
ld e,a
; Location of food
ld hl,0x8178
; Column/row
rrca
rrca
rrca
rrca
and 0x0f
add a,8
ld d,a
ld a,e
and 0x0f
add a,4
ld e,a
ld iy,draw_line
halt
call draw_char
; Reset food eaten flag
ld hl,flags
res 0,(hl)
invalidate_free_square_quadrants_tail:
; Invalidate any free square quadrants that contain the current position
; for the tail. This tail will have moved by the time we do the next
; free square finding calculations, which is why we do the check now.
ld hl,TBL_SNAKE_HISTORY_CLEAN
ld a,(hl)
bit 3,a
jr nz,ivsq_tail_in_bottom_half
; Tail in top half
bit 7,a
jr nz,ivsq_tail_in_top_right
; Tail in top left
ld hl,flags
set 7,(hl)
jp next_input_loop
ivsq_tail_in_top_right:
ld hl,flags
set 6,(hl)
jp next_input_loop
ivsq_tail_in_bottom_half:
bit 7,a
jr nz,ivsq_tail_in_bottom_right
; Tail in bottom left
ld hl,flags
set 4,(hl)
jp next_input_loop
ivsq_tail_in_bottom_right:
ld hl,flags
set 5,(hl)