diff options
author | Astatin <[email protected]> | 2024-11-05 16:03:28 +0900 |
---|---|---|
committer | Astatin <[email protected]> | 2024-11-05 16:03:28 +0900 |
commit | 14ae7d8def1159b395bf1176b351651cbd98ba19 (patch) | |
tree | 3c7a191ef9d19930f456620b68331a822f5d2731 | |
parent | 17a186217dbf12e344e90537d50f713e3c5a6416 (diff) |
Use jump tables for entities turn AIs + actions on entity interaction
-rw-r--r-- | definitions.gbasm | 3 | ||||
-rw-r--r-- | dialogues.gbasm | 10 | ||||
-rw-r--r-- | dialogues.gbtxt | 5 | ||||
-rw-r--r-- | entity/actions.gbasm | 193 | ||||
-rw-r--r-- | entity/bunny.gbasm | 59 | ||||
-rw-r--r-- | entity/init.gbasm | 18 | ||||
-rw-r--r-- | main.gbasm | 27 |
7 files changed, 217 insertions, 98 deletions
diff --git a/definitions.gbasm b/definitions.gbasm index a4216a1..2d7420a 100644 --- a/definitions.gbasm +++ b/definitions.gbasm @@ -96,7 +96,8 @@ ; x: u8, ; y: u8, ; direction: u8 (animation = bit 7-4, bit 3 = is_moving, bit 2-0: direction), -; ai_function_pointer: u16 +; turn_function_jump_table_index: u8 +; action_function_jump_table_index: u8 ; health: u8 (DAA decimal !!) ; _padding: u72 ; diff --git a/dialogues.gbasm b/dialogues.gbasm index 908975a..d16181a 100644 --- a/dialogues.gbasm +++ b/dialogues.gbasm @@ -1,6 +1,4 @@ -Lain_Text: -.DB 0x95, 0x8a, 0x92, 0x97, 0x0, 0x92, 0x9c, 0x0, 0x8c, 0x9e, 0x9d, 0x8e, 0xa5, 0xff -Owl_Text: -.DB 0x98, 0xa0, 0x95, 0x0, 0x92, 0x9c, 0x0, 0x8c, 0x9e, 0x9d, 0x8e, 0xa5, 0xff -Auz_Text: -.DB 0xa0, 0x92, 0x9d, 0x8c, 0x91, 0x0, 0x92, 0x9c, 0x0, 0x8c, 0x9e, 0x9d, 0x8e, 0xa5, 0xff +Bunny_Prefix: +.DB 0x8b, 0x9e, 0x97, 0x97, 0xa2, 0xaa, 0xff +Text_1: +.DB 0x91, 0x8e, 0x95, 0x95, 0x98, 0x0, 0xa5, 0xff diff --git a/dialogues.gbtxt b/dialogues.gbtxt index 13fa017..ae3374c 100644 --- a/dialogues.gbtxt +++ b/dialogues.gbtxt @@ -1,3 +1,2 @@ -Lain_Text: Lain is cute! -Owl_Text: Owl is cute! -Auz_Text: Witch is cute! +Bunny_Prefix: Bunny: +Text_1: Hello ! diff --git a/entity/actions.gbasm b/entity/actions.gbasm index bcf89c1..524c28d 100644 --- a/entity/actions.gbasm +++ b/entity/actions.gbasm @@ -29,10 +29,13 @@ Entity_Action: INC HL INC HL - LD A, (HL+) - LD D, A - LD A, (HL+) - LD E, A + LD E, (HL) + LD D, $00 + + SLA E + RL D + SLA E + RL D PUSH HL @@ -40,6 +43,14 @@ Entity_Action: AND $f0 LD L, A + LD BC, =Turn_Jump_table + LD A, E + ADD C + LD E, A + LD A, D + ADC B + LD D, A + ; This is confusing but this thing is actually CALL DE LD BC, =Entity_Action.interaction_end PUSH BC @@ -77,7 +88,65 @@ Update_Animation_Steps: Update_Animation_Steps.end: RET -Fox_AI: +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, $9dc1 + LD BC, =Bunny_Prefix + CALL =Print_str + + LD HL, $9e01 + LD BC, =Text_1 + CALL =Print_str + + LD A, $03 + LD $mem_display_flag, A + + LD A, $mem_bunny_direction + AND $0f + LD $mem_bunny_direction, A + RET + +Fox_Turn: PUSH HL PUSH BC PUSH DE @@ -89,85 +158,89 @@ Fox_AI: LD A, (HL+) LD D, A - Fox_AI.Start_action_or_movement: + Fox_Turn.Start_action_or_movement: LD A, $mem_map_loading_flags BIT 3, A - JP Z, =Fox_AI.Start_action_or_movement.end + JP Z, =Fox_Turn.Start_action_or_movement.end LD A, $mem_moving_animation_step CP $00 - JP NZ, =Fox_AI.Start_action_or_movement.end + JP NZ, =Fox_Turn.Start_action_or_movement.end ; Is bunny right next to fox - Fox_AI.Check_next_to_vertical: + Fox_Turn.Check_next_to_vertical: LD A, $mem_bunny_x CP B - JR NZ, =Fox_AI.Check_next_to_horizontal + JR NZ, =Fox_Turn.Check_next_to_horizontal ; up LD E, $14 LD A, $mem_bunny_y SUB $01 CP C - JR Z, =Fox_AI.Start_Attack + JR Z, =Fox_Turn.Start_Attack ; down LD E, $13 ADD $02 CP C - JR Z, =Fox_AI.Start_Attack + JR Z, =Fox_Turn.Start_Attack - JR =Fox_AI.Check_next_to.end + JR =Fox_Turn.Check_next_to.end - Fox_AI.Check_next_to_horizontal: + Fox_Turn.Check_next_to_horizontal: LD A, $mem_bunny_y CP C - JR NZ, =Fox_AI.Check_next_to.end + JR NZ, =Fox_Turn.Check_next_to.end ; left LD E, $12 LD A, $mem_bunny_x SUB $01 CP B - JR Z, =Fox_AI.Start_Attack + JR Z, =Fox_Turn.Start_Attack ; right LD E, $11 ADD $02 CP B - JR Z, =Fox_AI.Start_Attack + JR Z, =Fox_Turn.Start_Attack - JR NZ, =Fox_AI.Check_next_to.end + JR NZ, =Fox_Turn.Check_next_to.end - Fox_AI.Start_Attack: + Fox_Turn.Start_Attack: LD D, E LD A, $mem_bunny_health DEC A DAA + JR NC, =Fox_Turn.Skip_health_underflow_fix + + LD A, $00 + Fox_Turn.Skip_health_underflow_fix: LD $mem_bunny_health, A - JP =Fox_AI.Check_Collision + JP =Fox_Turn.Check_Collision - Fox_AI.Check_next_to.end: + 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_AI.Random_walker + JR NC, =Fox_Turn.Random_walker LD A, $mem_bunny_y SUB C .ABS CP $08 - JR NC, =Fox_AI.Random_walker + JR NC, =Fox_Turn.Random_walker - JR =Fox_AI.Follow_bunny + JR =Fox_Turn.Follow_bunny - Fox_AI.Random_walker: + Fox_Turn.Random_walker: CALL =RNG_Step LD E, $00 RR A @@ -177,9 +250,9 @@ Fox_AI: INC A OR E LD D, A - JP =Fox_AI.Check_Collision + JP =Fox_Turn.Check_Collision - Fox_AI.Follow_bunny: + Fox_Turn.Follow_bunny: LD A, $mem_bunny_x LD $tmp_var_1, A @@ -191,7 +264,7 @@ Fox_AI: LD $tmp_var_3, A CP $00 - JR Z, =Fox_AI.skip_invert_axis + JR Z, =Fox_Turn.skip_invert_axis LD A, $mem_bunny_y LD $tmp_var_1, A @@ -203,7 +276,7 @@ Fox_AI: LD B, C LD C, E - Fox_AI.skip_invert_axis: + Fox_Turn.skip_invert_axis: ; Choose direction LD A, B @@ -212,16 +285,16 @@ Fox_AI: LD A, $tmp_var_1 ADD $80 CP E - JR Z =Fox_AI.Vertical - JR C =Fox_AI.Go_Left + JR Z =Fox_Turn.Vertical + JR C =Fox_Turn.Go_Left - Fox_AI.Go_Right: + Fox_Turn.Go_Right: LD A, $enum_direction_right - JR =Fox_AI.Check_Horizontal_Collision - Fox_AI.Go_Left: + JR =Fox_Turn.Check_Horizontal_Collision + Fox_Turn.Go_Left: LD A, $enum_direction_left - Fox_AI.Check_Horizontal_Collision: + Fox_Turn.Check_Horizontal_Collision: LD E, A DEC E LD A, $tmp_var_3 @@ -236,9 +309,9 @@ Fox_AI: POP BC CP $00 LD A, E - JR Z, =Fox_AI.Direction_check_end + JR Z, =Fox_Turn.Direction_check_end - Fox_AI.Vertical: + Fox_Turn.Vertical: LD A, C ADD $80 LD E, A @@ -246,15 +319,15 @@ Fox_AI: ADD $80 CP E - JR Z =Fox_AI.No_movement - JR C =Fox_AI.Go_Up - Fox_AI.Go_Down: + JR Z =Fox_Turn.No_movement + JR C =Fox_Turn.Go_Up + Fox_Turn.Go_Down: LD A, $enum_direction_down - JR =Fox_AI.Direction_check_end - Fox_AI.Go_Up: + JR =Fox_Turn.Direction_check_end + Fox_Turn.Go_Up: LD A, $enum_direction_up - Fox_AI.Direction_check_end: + Fox_Turn.Direction_check_end: DEC A LD E, A @@ -266,16 +339,16 @@ Fox_AI: ; Check collision - Fox_AI.No_movement: + Fox_Turn.No_movement: LD A, $tmp_var_3 CP $00 - JR Z, =Fox_AI.skip_invert_axis2 + JR Z, =Fox_Turn.skip_invert_axis2 LD E, B LD B, C LD C, E - Fox_AI.skip_invert_axis2: + Fox_Turn.skip_invert_axis2: - Fox_AI.Check_Collision: + Fox_Turn.Check_Collision: LD A, D PUSH BC CALL =Get_Position_After_Move @@ -284,33 +357,33 @@ Fox_AI: CALL =Carve_Entity_Collision_Map POP BC CP $00 - JR Z, =Fox_AI.Start_action_or_movement.not_collision + JR Z, =Fox_Turn.Start_action_or_movement.not_collision RES 3, D - Fox_AI.Start_action_or_movement.not_collision: + Fox_Turn.Start_action_or_movement.not_collision: - Fox_AI.Start_action_or_movement.end: + Fox_Turn.Start_action_or_movement.end: - Fox_AI.End_movement: + Fox_Turn.End_movement: LD A, $mem_map_loading_flags BIT 3, A - JP Z, =Fox_AI.End_movement.end + JP Z, =Fox_Turn.End_movement.end LD A, $mem_moving_animation_step CP $0f - JP NZ, =Fox_AI.End_movement.end + JP NZ, =Fox_Turn.End_movement.end BIT 3, D - JR Z, =Fox_AI.End_movement.end + JR Z, =Fox_Turn.End_movement.end LD A, $01 .ADD_A_TO_DIRECTION_BC - Fox_AI.End_movement.end: + Fox_Turn.End_movement.end: - Fox_AI.Health_check: + Fox_Turn.Health_check: LD A, $mem_map_loading_flags BIT 3, A - JP Z, =Fox_AI.Health_check.end + JP Z, =Fox_Turn.Health_check.end LD A, $mem_moving_animation_step CP $03 - JR NZ, =Fox_AI.Health_check.end + JR NZ, =Fox_Turn.Health_check.end LD A, L AND $f0 @@ -319,14 +392,14 @@ Fox_AI: LD A, (HL) CP $00 - JR NZ, =Fox_AI.Health_check.end + JR NZ, =Fox_Turn.Health_check.end LD A, L AND $f0 LD L, A LD (HL), $00 - Fox_AI.Health_check.end: + Fox_Turn.Health_check.end: LD A, L AND $f0 diff --git a/entity/bunny.gbasm b/entity/bunny.gbasm index 8601d79..f180e9e 100644 --- a/entity/bunny.gbasm +++ b/entity/bunny.gbasm @@ -119,18 +119,18 @@ Move_Bunny: LD $mem_viewport_y, A End_movement.end: - Attack: + Interaction: LD A, $mem_map_loading_flags BIT 3, A - JP Z, =Attack.end + JP Z, =Interaction.end LD A, $mem_moving_animation_step CP $02 - JP NZ, =Attack.end + JP NZ, =Interaction.end LD A, $mem_bunny_direction AND $f8 CP $10 - JR NZ, =Attack.end + JR NZ, =Interaction.end LD A, $mem_bunny_x LD B, A @@ -144,38 +144,59 @@ Move_Bunny: .ADD_A_TO_DIRECTION_BC LD HL, $mem_entities_list - Attack.entities_loop: + Interaction.entities_loop: LD A, L AND $f0 ADD $10 LD L, A CP $00 - JR Z, =Attack.end + JR Z, =Interaction.end LD A, (HL+) CP $00 - JR Z, =Attack.entities_loop.next + JR Z, =Interaction.entities_loop.next LD A, (HL+) CP B - JR NZ, =Attack.entities_loop.next + JR NZ, =Interaction.entities_loop.next LD A, (HL+) CP C - JR NZ, =Attack.entities_loop.next + JR NZ, =Interaction.entities_loop.next INC HL INC HL - INC HL - - LD A, (HL) - DEC A - DAA - LD (HL), A - - Attack.entities_loop.next: - JR =Attack.entities_loop - Attack.end: + PUSH HL + PUSH BC + + LD E, (HL) + LD D, $00 + + SLA E + RL D + SLA E + RL D + + LD BC, =Interaction_Jump_table + LD A, E + ADD C + LD E, A + LD A, D + ADC B + LD D, A + + LD BC, =Interaction.interaction_end + PUSH BC + PUSH DE + RET + + Interaction.interaction_end: + POP BC + POP HL + + Interaction.entities_loop.next: + JR =Interaction.entities_loop + Interaction.end: Check_End_Action: LD A, $mem_bunny_direction diff --git a/entity/init.gbasm b/entity/init.gbasm index 5404ead..c5f0b0e 100644 --- a/entity/init.gbasm +++ b/entity/init.gbasm @@ -33,10 +33,13 @@ Initialize_Entities: ADD D LD (HL+), A INC HL - LD BC, =Move_Bunny - LD A, B + + ; Turn + LD A, $01 LD (HL+), A - LD A, C + + ; Action + LD A, $00 LD (HL+), A LD A, $20 @@ -113,10 +116,13 @@ Initialize_Fox: LD (HL+), A LD A, $03 LD (HL+), A - LD BC, =Fox_AI - LD A, B + + ; Turn + LD A, $02 LD (HL+), A - LD A, C + + ; Action + LD A, $01 LD (HL+), A LD A, $04 @@ -18,9 +18,6 @@ LD $reg_viewport_x, A LD $reg_viewport_y, A - LD A, $palette_bold_font - LD $reg_bg_palette, A - LD A, $lcdc_window_enabled LD $reg_lcd_controller, A .END @@ -86,6 +83,7 @@ New_Dungeon: LD A, $00 LD $mem_display_flag, A + .ENABLE_WINDOW_NO_WAIT_HBLANK .ENABLE_VBLANK_INTERRUPTS EI @@ -96,6 +94,10 @@ New_Dungeon: VBLANK_Entrypoint: .ENABLE_WINDOW_NO_WAIT_HBLANK + + LD A, $palette_bold_font + LD $reg_bg_palette, A + LD HL, $9c00 LD A, $mem_bunny_health CALL =Print_8bit @@ -130,10 +132,29 @@ STAT_Entrypoint: LD A, $reg_lyc CP $09 JR Z, =STAT_Entrypoint.End_Top_Bar + CP $67 + JR Z, =STAT_Entrypoint.Start_dialogue + STAT_Entrypoint.Thin_font: + LD A, $palette_thin_font + LD $reg_bg_palette, A + JR =STAT_Entrypoint.skip_dialogue + + STAT_Entrypoint.Start_dialogue: LD A, $mem_display_flag BIT 0, A JR Z, =STAT_Entrypoint.skip_dialogue .ENABLE_WINDOW + LD A, $mem_display_flag + BIT 1, A + JR Z, =STAT_Entrypoint.Thin_font + LD A, $palette_bold_font + LD $reg_bg_palette, A + LD A, $77 + LD $reg_lyc, A + .RESET_STAT_INTERRUPT + POP AF + RETI + STAT_Entrypoint.skip_dialogue: .DISABLE_LYC_INTERRUPT POP AF |