Find Highest Target Priority in Ability Range (0019bf2c)

From Final Fantasy Hacktics Wiki
Revision as of 14:01, 9 June 2024 by Orkney (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
BATTLE.BIN :  - Find_Highest_Target_Priority_in_Ability_Range_(0019bf2c)
Test All Tiles to target from all tiles to move - best outcome is stored in AI 0x0034
------------------------------------------------------------------------------------------
Parameter : r4 = Pointer to unit battle data (801908cc)
Return : r2 = 0x00 by default
         r2 = -0x01 (from VSync_call or  AI_Target_Unit_for_Direct_vs_Arc_Attack_(0019cd9c)  )
------------------------------------------------------------------------------------------
0019bf2c: 3c02801a lui r2,0x801a             |
0019bf30: 90420d7b lbu r2,0x0d7b(r2)         |AI 0x19b7
0019bf34: 27bdffe0 addiu r29,r29,-0x0020     |
0019bf38: afb00010 sw r16,0x0010(r29)        |
0019bf3c: 3c10801a lui r16,0x801a            |
0019bf40: 2610f3c4 addiu r16,r16,0xf3c4      |0x8019f3c4 (AI pointer)
0019bf44: afbf0018 sw r31,0x0018(r29)        |
0019bf48: 10400016 beq r2,r0,0x0019bfa4      #If AI 0x19b7 <> 0x00
0019bf4c: afb10014 sw r17,0x0014(r29)            |
0019bf50: 3c03801a lui r3,0x801a                 |
0019bf54: 9063029d lbu r3,0x029d(r3)             |Load AI 0xed9 This routine control variable ?
0019bf58: 00000000 nop                           |
0019bf5c: 1060001d beq r3,r0,0x0019bfd4          #if AI 0xed9 = 0x00 : branch to  Main priority loop without setting tiles to move and tile to target matrixes
0019bf60: 34020001 ori r2,r0,0x0001              |
0019bf64: 10620055 beq r3,r2,0x0019c0bc          #if AI 0xed9 = 0x01 : branch to  Priority calculation (only 1 combination ? - first loop) Destination and Tile to target are already decided ?
0019bf68: 00000000 nop                           |
0019bf6c: 3c02801a lui r2,0x801a                 |
0019bf70: 904202a0 lbu r2,0x02a0(r2)             |? Elevation
0019bf74: 00000000 nop                           |
0019bf78: 000218c0 sll r3,r2,0x03                |
0019bf7c: 00621821 addu r3,r3,r2                 |
0019bf80: 00031880 sll r3,r3,0x02                |Elevation offset
0019bf84: 3c02801a lui r2,0x801a                 |
0019bf88: 9042029f lbu r2,0x029f(r2)             |? Y coordinates
0019bf8c: 00701821 addu r3,r3,r16                |
0019bf90: 00021040 sll r2,r2,0x01                |Y offset (halfword)
0019bf94: 00621821 addu r3,r3,r2                 |0x8019f3c4 + Elevation and Y offset (1 bit per tile model)
0019bf98: 94710b94 lhu r17,0x0b94(r3)            |Y row of flags in AI 0xb94 Tiles from where you can hit a unit (it is not shifted at all - wtf ?)
0019bf9c: 08067059 j 0x0019c164                  >>jump to  Priority calculation (only 1 combination ? - second loop) Destination and Tile to target are already decided ?
0019bfa0: 00000000 nop                           |
                                             Else : AI 0x19b7 = 0x00
0019bfa4: 3c02801a lui r2,0x801a                 |
0019bfa8: 904201f2 lbu r2,0x01f2(r2)             |Load Acting Unit's ID aka AI  0xe2e
0019bfac: 00000000 nop                           |
0019bfb0: 00501021 addu r2,r2,r16                |
0019bfb4: 90510c8d lbu r17,0x0c8d(r2)            |Acting Unit flag in Potentiel Targets list (AI 0xc8d)
0019bfb8: 0c0670f5 jal 0x0019c3d4                |-->Evaluate_Linear_Ability_Behaviors Set all desirable Tile to move and tiles to target
0019bfbc: a0400c8d sb r0,0x0c8d(r2)              |Remove Unit in Targetable List
0019bfc0: 3c02801a lui r2,0x801a                 |
0019bfc4: 904201f2 lbu r2,0x01f2(r2)             |Load Acting Unit's ID
0019bfc8: 00000000 nop                           |
0019bfcc: 00501021 addu r2,r2,r16                |
0019bfd0: a0510c8d sb r17,0x0c8d(r2)             |Restore Unit Targetable Flag 0x8019f3c4 + c8d + Unit Offset

=== TEST ALL COMBINATIONS (MOVE + TARGET) ===
0019bfd4: 0c0076ea jal 0x0001dba8            |-->VSync_call
0019bfd8: 34040001 ori r4,r0,0x0001          |
0019bfdc: 284201b9 slti r2,r2,0x01b9         |
0019bfe0: 14400004 bne r2,r0,0x0019bff4      #If Vsync returned r2 > 0x1b8
0019bfe4: 3c030400 lui r3,0x0400                 |
0019bfe8: a2000ed9 sb r0,0x0ed9(r16)             |Reset AI 0xed9
0019bfec: 0806707d j 0x0019c1f4                  |Exit routine returns -0x01
0019bff0: 2402ffff addiu r2,r0,-0x0001           |
                                             Else Vsync Call is fine
0019bff4: 8e02000c lw r2,0x000c(r16)             |Load AI Flags 1 to 4 
0019bff8: a20019b7 sb r0,0x19b7(r16)             |Set AI 0x19b7 = 0x00
0019bffc: 00431024 and r2,r2,r3                  |
0019c000: 1040000a beq r2,r0,0x0019c02c          #If AI Byte 0x04 flag 0x04 is ON (?) Not monster skill - byte 0x04 is updated after loading data from SCUS (weapon range related ?)
0019c004: a2000e4d sb r0,0x0e4d(r16)             |Set AI 0xe4d = 0x00
0019c008: 3c03fdff lui r3,0xfdff                     |
0019c00c: 8e020cb4 lw r2,0x0cb4(r16)                 |AI 0xcb4 (AI processing order ?)
0019c010: 3463ffff ori r3,r3,0xffff                  |
0019c014: 00431024 and r2,r2,r3                      |
0019c018: 0806701a j 0x0019c068                      >>Jump to  Test all move/target combinations keep AI 0xe4d = 0x00
0019c01c: ae020cb4 sw r2,0x0cb4(r16)                 |Remove 0x04000000 Flag from AI 0xcb4

                                                        BACK JUMP (fail ?)
0019c020: a2020ed9 sb r2,0x0ed9(r16)                     |Update AI 0xed9 depending from where the code is coming control variable ?
0019c024: 0806707d j 0x0019c1f4                          |EXIT Return -0x01
0019c028: 2402ffff addiu r2,r0,-0x0001                   |

                                                 Else : Ability AI 0x000f flag 0x04 is OFF
0019c02c: 92030cbb lbu r3,0x0cbb(r16)                |Number of Tiles enabled in AI 0x0bdc Tiles to hit
0019c030: 92020cbc lbu r2,0x0cbc(r16)                |Nb of Tiles enabled in AI 0x0b94 Tiles to move
0019c034: 00000000 nop                               |
0019c038: 00620018 mult r3,r2                        |
0019c03c: a2030eda sb r3,0x0eda(r16)                 |Set AI 0x0eda to AI 0x0cbb
0019c040: 00001012 mflo r2                           |
0019c044: a2020eda sb r2,0x0eda(r16)                 |Set AI 0xeda to AI 0xcbb * AI 0xcbc ????
0019c048: 92020eda lbu r2,0x0eda(r16)                |AI 0x0eda Maximum possibilities ?
0019c04c: 00000000 nop                               |
0019c050: 2c42000b sltiu r2,r2,0x000b                |0x1 If AI 0xcbb * AI 0xcbc < 0x0b
0019c054: 14400003 bne r2,r0,0x0019c064              #If AI 0xcbb * AI 0xcbc > 0x0a
0019c058: 34020001 ori r2,r0,0x0001                      |
0019c05c: a2020e4d sb r2,0x0e4d(r16)                     |Set AI 0xe4d to 0x01
0019c060: 34020001 ori r2,r0,0x0001                      |
0019c064: a2020e4d sb r2,0x0e4d(r16)                 |This saves the exact same information to the exact same place.
0019c068: 8e020cb4 lw r2,0x0cb4(r16)             |AI 0xcb4 (AI processing order ?)
0019c06c: 3c030200 lui r3,0x0200                 |
0019c070: 00431024 and r2,r2,r3                  |
0019c074: 14400005 bne r2,r0,0x0019c08c          #If AI 0xcb4 flag 0x0200000 is OFF  Weapon range related ? Targeting type ?
0019c078: 00000000 nop                               |
0019c07c: 92020e4d lbu r2,0x0e4d(r16)                |Load 0xe4d   Set to 0x01 if AI 0x0f flag 0x04 is OFF
0019c080: 00000000 nop                               |
0019c084: 10400014 beq r2,r0,0x0019c0d8              #If AI 0xe4d = 0x00 : branch to  Second loop (not moving ?)
0019c088: 00000000 nop                               |

                  --- TEST ALL POSSIBILITIES (DESTINATION - TARGETING) ---
                                                @LOOP - Test all destination tiles
0019c08c: 92020cbb lbu r2,0x0cbb(r16)                |Load Nb Of Tiles enabled in AI 0x0bdc Nb of Targetable tiles to hit a unit
0019c090: 00000000 nop                               |
0019c094: 10400054 beq r2,r0,0x0019c1e8              #If there is no targetable tiles with this ability :  Exit loop restore data then exit  Might be Disabled during priority calculation
0019c098: 00000000 nop                               |
0019c09c: 92020cbc lbu r2,0x0cbc(r16)                |Load Nb Of Tiles enabled in AI 0x0b94 Nb of Tiles from where you can hit a unit
0019c0a0: 00000000 nop                               |
0019c0a4: 10400050 beq r2,r0,0x0019c1e8              #If there is no destination tiles with this ability : branch to  Exit loop restore data then exit All Tiles done in 19c280
0019c0a8: 00000000 nop                               |
0019c0ac: 0c0670a0 jal 0x0019c280                    |-->Start_searching_for_Highest_Target_Priority_(0019c280) Find Best Tile in AI 0xb94 and store its coordinates at AI 0x18 (then disable it)
0019c0b0: 00000000 nop                               |
0019c0b4: 0c067083 jal 0x0019c20c                    |-->0019c20c_-_0019c27c Set AI 0x14 - Set AI 0x18 to considered caster position
0019c0b8: 00000000 nop                               |
0019c0bc: 0c067367 jal 0x0019cd9c                    |-->AI_Target_Unit_for_Direct_vs_Arc_Attack_(0019cd9c) Test all Targetable tile from considered position (store best result at AI 0x34)
0019c0c0: 00000000 nop                               |
0019c0c4: 2403ffff addiu r3,r0,-0x0001               |
0019c0c8: 1043ffd5 beq r2,r3,0x0019c020              #If returned value is -0x01 : Set AI 0xed9 = 0x01 then Exit Return -0x01
0019c0cc: 34020001 ori r2,r0,0x0001                  |
0019c0d0: 08067023 j 0x0019c08c                  Λ Loop while there is still tiles in AI 0xbdc or 0xb94
0019c0d4: 00000000 nop                               |

                  --- TEST ALL POSSIBILITIES (TARGETING only from a given location) ---
0019c0d8: a2000edc sb r0,0x0edc(r16)             |Initialize Current Map Level
                                                @LOOP - Elevation
0019c0dc: 92020e3b lbu r2,0x0e3b(r16)                |Load Max Map Y
0019c0e0: 00000000 nop                               |
0019c0e4: 10400037 beq r2,r0,0x0019c1c4              #If Max Y is invalid : branch to  Next elevation
0019c0e8: a2000edb sb r0,0x0edb(r16)                 |Initialize Y Counter
                                                    @LOOP - Y rows
0019c0ec: 92020edc lbu r2,0x0edc(r16)                    |Load Current Map Level
0019c0f0: 00000000 nop                                   |
0019c0f4: 000218c0 sll r3,r2,0x03                        |
0019c0f8: 00621821 addu r3,r3,r2                         |
0019c0fc: 00031880 sll r3,r3,0x02                        |Elevation offset (0x24)
0019c100: 92020edb lbu r2,0x0edb(r16)                    |Load Current Y
0019c104: 00701821 addu r3,r3,r16                        |
0019c108: 00021040 sll r2,r2,0x01                        |Y offset (halfword)
0019c10c: 00621821 addu r3,r3,r2                         |0x8019f3c4 + Elevation and Y row offset
0019c110: 94710b94 lhu r17,0x0b94(r3)                    |This Y row of flags (AI 0x0b94)
0019c114: 00000000 nop                                   |
0019c118: 12200021 beq r17,r0,0x0019c1a0                 #If There is no flags in this row : branch to  Next Y row
0019c11c: 00000000 nop                                   |
0019c120: 92020e3a lbu r2,0x0e3a(r16)                    |Load Max Map X
0019c124: 00000000 nop                                   |
0019c128: 1040001d beq r2,r0,0x0019c1a0                  #If Map Max X is invalid : branch to  Next Y row
0019c12c: a2000eda sb r0,0x0eda(r16)                     |Initialize X Counter
                                                        @LOOP - Each tile in Y row
0019c130: 92020eda lbu r2,0x0eda(r16)                        |Load X counter
0019c134: 00000000 nop                                       |
0019c138: 00511004 sllv r2,r17,r2                            |Shift Row flag so this tile is in 0x8000 position
0019c13c: 30428000 andi r2,r2,0x8000                         |
0019c140: 1040000d beq r2,r0,0x0019c178                      #If This tile flag is off : branch to  Next X
0019c144: 00000000 nop                                       |
0019c148: 92020eda lbu r2,0x0eda(r16)                        |Load Current X
0019c14c: 92030edb lbu r3,0x0edb(r16)                        |Load Current Y
0019c150: 92040edc lbu r4,0x0edc(r16)                        |Load Current Map Level
0019c154: a2020018 sb r2,0x0018(r16)                         |Save as Acting Unit X
0019c158: a203001a sb r3,0x001a(r16)                         |Save as Acting Unit Y
0019c15c: 0c067083 jal 0x0019c20c                            |-->0019c20c_-_0019c27c Set AI 0x14 - Set AI 0x18 to considered caster position
0019c160: a2040019 sb r4,0x0019(r16)                         |Save as Acting Unit Map Level
0019c164: 0c067367 jal 0x0019cd9c                            |-->AI_Target_Unit_for_Direct_vs_Arc_Attack_(0019cd9c)  Try all targetable tiles (simulation) keep the better outcome in AI 0x34
0019c168: 00000000 nop                                       |
0019c16c: 2403ffff addiu r3,r0,-0x0001                       |
0019c170: 1043ffab beq r2,r3,0x0019c020                      #If returned value is - 0x01 :  Set AI 0xed9 = 0x02 then Exit Return -0x01
0019c174: 34020002 ori r2,r0,0x0002                          |
0019c178: 92020eda lbu r2,0x0eda(r16)                        |X counter
0019c17c: 00000000 nop                                       |
0019c180: 24420001 addiu r2,r2,0x0001                        |Current X++
0019c184: a2020eda sb r2,0x0eda(r16)                         |Increase X counter
0019c188: 92030eda lbu r3,0x0eda(r16)                        |
0019c18c: 92020e3a lbu r2,0x0e3a(r16)                        |Map Max X
0019c190: 00000000 nop                                       |
0019c194: 0062182b sltu r3,r3,r2                             |
0019c198: 1460ffe5 bne r3,r0,0x0019c130                  #Loop while X < Max X
0019c19c: 00000000 nop                                   |
0019c1a0: 92020edb lbu r2,0x0edb(r16)                    |Y counter
0019c1a4: 92030e3b lbu r3,0x0e3b(r16)                    |Max Y
0019c1a8: 24420001 addiu r2,r2,0x0001                    |Current Y++
0019c1ac: a2020edb sb r2,0x0edb(r16)                     |Increase Y counter
0019c1b0: 92020edb lbu r2,0x0edb(r16)                    |
0019c1b4: 00000000 nop                                   |
0019c1b8: 0043102b sltu r2,r2,r3                         |
0019c1bc: 1440ffcb bne r2,r0,0x0019c0ec              #Loop while Y < Max Y
0019c1c0: 00000000 nop                               |
0019c1c4: 92020edc lbu r2,0x0edc(r16)                |Elevation counter
0019c1c8: 00000000 nop                               |
0019c1cc: 24420001 addiu r2,r2,0x0001                |Current Map Level++
0019c1d0: a2020edc sb r2,0x0edc(r16)                 |Increase Elevation
0019c1d4: 92020edc lbu r2,0x0edc(r16)                |Load Current Map Level
0019c1d8: 00000000 nop                               |
0019c1dc: 2c420002 sltiu r2,r2,0x0002                |
0019c1e0: 1440ffbe bne r2,r0,0x0019c0dc          #Loop 2 times (both elevation)
0019c1e4: 00000000 nop                           |
0019c1e8: 0c067c96 jal 0x0019f258                |-->Store units coordinates Copy coordinates from AI 0xe30 to Battle 0x47 (restore caster location)
0019c1ec: 26040e30 addiu r4,r16,0x0e30           |r4 = AI Coordinate Data
0019c1f0: 00001021 addu r2,r0,r0                 |Returns 0x00
0019c1f4: 8fbf0018 lw r31,0x0018(r29)
0019c1f8: 8fb10014 lw r17,0x0014(r29)
0019c1fc: 8fb00010 lw r16,0x0010(r29)
0019c200: 27bd0020 addiu r29,r29,0x0020
0019c204: 03e00008 jr r31
0019c208: 00000000 nop

Return locations

BATTLE.BIN
 0019b168: AI_ability_use_control_routine
 0019b1d4: AI_ability_use_control_routine
 0019b248: AI_ability_use_control_routine
 0019b2c8: AI_ability_use_control_routine