aboutsummaryrefslogtreecommitdiff
path: root/entity/actions.gbasm
blob: a8045ddfccdd45992a01288b9635cb2a3e184eef (plain)
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
Entities_Actions:
	CALL =Reset_Entities_Collision_Map

	LD HL, $mem_entities_list
	CALL =Entity_Action
	CALL =Entity_Action
	CALL =Entity_Action
	CALL =Entity_Action
	CALL =Entity_Action
	CALL =Entity_Action
	CALL =Entity_Action
	CALL =Entity_Action
	CALL =Entity_Action
	CALL =Entity_Action
	CALL =Entity_Action
	CALL =Entity_Action
	CALL =Entity_Action
	CALL =Entity_Action
	CALL =Entity_Action
	CALL =Entity_Action
	RET

Entity_Action:
	LD A, (HL+)
	CP $00
	JR Z, =Entity_Action.skip

	INC HL
	INC HL
	INC HL

	LD E, (HL)
	PUSH HL

	LD A, L
	AND $f0
	LD L, A

	LD BC, =Turn_Jump_table
	LD A, E

	.JUMP_TABLE

	POP HL

	Entity_Action.skip:
	LD A, L
	AND $f0
	ADD $10
	LD L, A
	RET

Update_Animation_Steps:
	LD A, $mem_map_loading_flags
	BIT 3, A
	JR Z, =Update_Animation_Steps.update_mode

	LD A, $mem_moving_animation_step
	INC A
	AND $0f
	LD $mem_moving_animation_step, A

	CP $00
	JR NZ, =Update_Animation_Steps.end

	LD A, $mem_map_loading_flags
	RES 3, A
	SET 1, A
	LD $mem_map_loading_flags, A

	Update_Animation_Steps.update_mode:

	LD A, $mem_bunny_health
	CP $00
	JR Z =Update_Animation_Steps.Dead_mode

	LD A, $mem_requested_mode
	LD $mem_current_mode, A

	Update_Animation_Steps.end:
	RET

	Update_Animation_Steps.Dead_mode:

	LD A, $enum_dead_mode
	LD $mem_current_mode, A

	LD A, $00
	LD $mem_display_flag, A

	RET

Turn_Jump_table:
	; 00
	RET
	NOP
	NOP
	NOP

	; 01
	JP =Move_Bunny
	NOP

	; 02
	JP =Fox_Turn
	NOP

Interaction_Jump_table:
	; 00
	RET
	NOP
	NOP
	NOP

	; 01
	JP =Fox_Interaction
	NOP

	; 02
	JP =Open_Dialogue
	NOP

Fox_Interaction:
	LD A, L
	AND $f0
	ADD $06
	LD L, A
	LD A, (HL)
	DEC A
	DAA
	LD (HL), A
	RET

Open_Dialogue:
	LD HL, $dialogue_first_line
	LD BC, =Bunny_Prefix
	CALL =Print_str

	LD HL, $dialogue_third_line
	LD BC, =Text_1
	CALL =Print_str

	LD A, $07
	LD $mem_display_flag, A

	LD A, $mem_bunny_direction
	AND $0f
	LD $mem_bunny_direction, A

	LD A, $enum_dungeon_dialogue_mode
	LD $mem_requested_mode, A
	RET

Fox_Turn:
	LD A, $mem_map_loading_flags
	BIT 3, A
	RET Z

	PUSH HL
	PUSH BC
	PUSH DE
	INC HL
	LD A, (HL+)
	LD B, A
	LD A, (HL+)
	LD C, A
	LD A, (HL+)
	LD D, A

	Fox_Turn.Health_check:
		LD A, L
		AND $f0
		ADD $06
		LD L, A

		LD A, (HL)
		CP $00
		JR NZ, =Fox_Turn.Health_check.end

		LD A, L
		AND $f0
		LD L, A

		LD (HL), $00
		LD A, $mem_enemies_alive_count
		DEC A
		LD $mem_enemies_alive_count, A
		JP NZ, =Fox_Turn.Skip_turn
	Fox_Turn.Health_check.end:

	Fox_Turn.Freeze_shiver:
		LD A, $mem_moving_animation_step
		CP $00
		JP NZ, =Fox_Turn.Freeze_shiver.end
		LD A, L
		AND $f0
		ADD $08
		LD L, A
		LD A, $01
		CP (HL)
		JR NZ, =Fox_Turn.Freeze_shiver.end
		LD A, D
		AND $07
		LD D, A
		CALL =RNG_Step
		CP $55
		JP NC, =Fox_Turn.Skip_turn
		LD A, D
		OR $40
		LD D, A

		CALL =RNG_Step
		CP $55
		JP NC, =Fox_Turn.Skip_turn

		LD A, $00
		LD (HL-), A
		RES 1, (HL)
		JP =Fox_Turn.Skip_turn
	Fox_Turn.Freeze_shiver.end:

	Fox_Turn.Should_turn_be_skipped:
		LD A, L
		AND $f0
		ADD $08
		LD L, A

		BIT 0, (HL)
		JP NZ, =Fox_Turn.Skip_turn

	Fox_Turn.Start_action_or_movement:
		LD A, $mem_moving_animation_step
		CP $00
		JP NZ, =Fox_Turn.Start_action_or_movement.end

		; Is bunny right next to fox
		Fox_Turn.Check_next_to_vertical:

		LD A, $mem_bunny_x
		CP B
		JR NZ, =Fox_Turn.Check_next_to_horizontal

		; up
		LD E, $14
		LD A, $mem_bunny_y
		SUB $01
		CP C
		JR Z, =Fox_Turn.Start_Attack

		; down
		LD E, $13
		ADD $02
		CP C
		JR Z, =Fox_Turn.Start_Attack

		JR =Fox_Turn.Check_next_to.end

		Fox_Turn.Check_next_to_horizontal:

		LD A, $mem_bunny_y
		CP C
		JR NZ, =Fox_Turn.Check_next_to.end

		; left
		LD E, $12
		LD A, $mem_bunny_x
		SUB $01
		CP B
		JR Z, =Fox_Turn.Start_Attack

		; right
		LD E, $11
		ADD $02
		CP B
		JR Z, =Fox_Turn.Start_Attack

		JR NZ, =Fox_Turn.Check_next_to.end

		Fox_Turn.Start_Attack:
		LD D, E

		LD A, $mem_bunny_health
		SUB $01
		JR C, =Fox_Turn.health_underflow_fix
		DAA
		LD $mem_bunny_health, A
		JR =Fox_Turn.Skip_health_underflow_fix

		Fox_Turn.health_underflow_fix:
		LD A, $00
		LD $mem_bunny_health, A
		Fox_Turn.Skip_health_underflow_fix:

		JP =Fox_Turn.Check_Collision

		Fox_Turn.Check_next_to.end:

		; Is Bunny close enough to follow
		LD A, $mem_bunny_x
		SUB B
		.ABS
		CP $08
		JR NC, =Fox_Turn.Random_walker

		LD A, $mem_bunny_y
		SUB C
		.ABS
		CP $08
		JR NC, =Fox_Turn.Random_walker

		JR =Fox_Turn.Follow_bunny

		Fox_Turn.Random_walker:
		CALL =RNG_Step
		LD E, $00
		RR A
		RR E
		SWAP E
		AND $03
		INC A
		OR E
		LD D, A
		JP =Fox_Turn.Check_Collision

		Fox_Turn.Follow_bunny:
		LD A, $mem_bunny_x
		LD $tmp_var_1, A

		LD A, $mem_bunny_y
		LD $tmp_var_2, A

		CALL =RNG_Step
		AND $02
		LD $tmp_var_3, A

		CP $00
		JR Z, =Fox_Turn.skip_invert_axis

		LD A, $mem_bunny_y
		LD $tmp_var_1, A

		LD A, $mem_bunny_x
		LD $tmp_var_2, A

		LD E, B
		LD B, C
		LD C, E

		Fox_Turn.skip_invert_axis:

		; Choose direction
		LD A, B
		ADD $80
		LD E, A
		LD A, $tmp_var_1
		ADD $80
		CP E
		JR Z =Fox_Turn.Vertical
		JR C =Fox_Turn.Go_Left

		Fox_Turn.Go_Right:
		LD A, $enum_direction_right
		JR =Fox_Turn.Check_Horizontal_Collision
		Fox_Turn.Go_Left:
		LD A, $enum_direction_left

		Fox_Turn.Check_Horizontal_Collision:
		LD E, A
		DEC E
		LD A, $tmp_var_3
		XOR E
		INC E
		INC A
		OR $08
		PUSH BC
		CALL =Get_Position_After_Move
		LD A, C
		CALL =Is_Collisionable
		POP BC
		CP $00
		LD A, E
		JR Z, =Fox_Turn.Direction_check_end

		Fox_Turn.Vertical:
		LD A, C
		ADD $80
		LD E, A
		LD A, $tmp_var_2
		ADD $80
		CP E

		JR Z =Fox_Turn.No_movement
		JR C =Fox_Turn.Go_Up
		Fox_Turn.Go_Down:
		LD A, $enum_direction_down
		JR =Fox_Turn.Direction_check_end
		Fox_Turn.Go_Up:
		LD A, $enum_direction_up

		Fox_Turn.Direction_check_end:

		DEC A
		LD E, A
		LD A, $tmp_var_3
		XOR E
		INC A
		OR $08
		LD D, A

		; Check collision

		Fox_Turn.No_movement:
		LD A, $tmp_var_3
		CP $00
		JR Z, =Fox_Turn.skip_invert_axis2
		LD E, B
		LD B, C
		LD C, E
		Fox_Turn.skip_invert_axis2:

		Fox_Turn.Check_Collision:
		LD A, D
		PUSH BC
		CALL =Get_Position_After_Move
		LD A, C
		CALL =Is_Collisionable
		CALL =Carve_Entity_Collision_Map
		POP BC
		CP $00
		JR Z, =Fox_Turn.Start_action_or_movement.not_collision
		RES 3, D
		Fox_Turn.Start_action_or_movement.not_collision:

	Fox_Turn.Start_action_or_movement.end:

	Fox_Turn.End_movement:
		LD A, $mem_moving_animation_step
		CP $0f
		JP NZ, =Fox_Turn.End_movement.end
		LD A, D
		AND $07
		BIT 3, D
		LD D, A
		JR Z, =Fox_Turn.End_movement.end
		LD A, $01
		.ADD_A_TO_DIRECTION_BC
	Fox_Turn.End_movement.end:

	Fox_Turn.Skip_Turn:
	LD A, L
	AND $f0
	LD L, A
	INC HL
	LD A, B
	LD (HL+), A
	LD A, C
	LD (HL+), A
	LD A, D
	LD (HL+), A
	POP DE
	POP BC
	POP HL
	RET