Main AI action setup

From Final Fantasy Hacktics Wiki
Jump to navigation Jump to search
BATTLE.BIN :  - Main AI action setup
Called 2 times per AI Action (Move and Act) - Select the action to perform - Update acting unit misc 0x158 to 0x16e.
------------------------------------------------------------------------------------------
Parameter : r4 = Acting Unit Battle ID (Battle 0x18a)
            r5 = Pointer to Acting Unit Misc 0x158
Return : r2 = 0x00 (default value)
         r2 = -0x01 (V_Sync Alert ?)
------------------------------------------------------------------------------------------
00193e50: 27bdffe0 addiu r29,r29,-0x0020     |
00193e54: afb20018 sw r18,0x0018(r29)        |
00193e58: 00809021 addu r18,r4,r0            |Preserves Acting Unit ID
00193e5c: afb10014 sw r17,0x0014(r29)        |
00193e60: 00a08821 addu r17,r5,r0            |Pointer to Acting Unit Misc 0x158
00193e64: 34040001 ori r4,r0,0x0001          |r4 = 1
00193e68: afbf001c sw r31,0x001c(r29)        |
00193e6c: 0c0076ea jal 0x0001dba8            |-->VSync_call
00193e70: afb00010 sw r16,0x0010(r29)        |
00193e74: 3c10801a lui r16,0x801a            |
00193e78: 2610f3c4 addiu r16,r16,0x-0c3c     |r16 = AI Data Pointer
00193e7c: 284201b9 slti r2,r2,0x01b9         |
00193e80: 104000d6 beq r2,r0,0x001941dc      #If r2 > 0x1b8 Exit Return -0x01 V_Sync alert : time to refresh animations ?
00193e84: 2402ffff addiu r2,r0,-0x0001       |
00193e88: 3c02801a lui r2,0x801a             |
00193e8c: 90420d7b lbu r2,0x0d7b(r2)         |AI 0x19b7 (0x01 if coming back from refreshing animation ?)
00193e90: 00000000 nop                       |

==== Return to where the code left off ====
00193e94: 1040000f beq r2,r0,0x00193ed4      #If AI 0x19b7 <> 0x00
00193e98: 34020001 ori r2,r0,0x0001              |r2 = 1
00193e9c: 3c03801a lui r3,0x801a                 |
00193ea0: 90630298 lbu r3,0x0298(r3)             |Load This routine memory Variable  (AI 0xed4)
00193ea4: 00000000 nop                           |
00193ea8: 1062005f beq r3,r2,0x00194028          #If Memory variable = 0x01 : return to  AI movement/panel data setting routine
00193eac: 28620002 slti r2,r3,0x0002             |
00193eb0: 10400005 beq r2,r0,0x00193ec8          #If Memory <0x2
00193eb4: 00000000 nop                               |
00193eb8: 1060004d beq r3,r0,0x00193ff0              #If Memory variable = 0x00 : return to  Set type of target for all targets routine
00193ebc: 00000000 nop                               |
00193ec0: 08064fb5 j 0x00193ed4                      >>If invalid :  Start from beginning
00193ec4: 00000000 nop                               |
00193ec8: 34020002 ori r2,r0,0x0002              |
00193ecc: 10620066 beq r3,r2,0x00194068          #If Memory variable = 0x02 : return to  AI_Initial_Targeting_Selection
00193ed0: 00000000 nop                           |

==== Start from beginning ====
00193ed4: 92031818 lbu r3,0x1818(r16)        |Load selected Action 0x1c AI 0x17fc + 0x1c
00193ed8: 340200ff ori r2,r0,0x00ff          |
00193edc: 1462000f bne r3,r2,0x00193f1c      #If AI 0x1818 = 0xff (Action already selected) Set in this routine after AI making decision (2nd pass ? 1st Act or move, then do the last action part ?)
00193ee0: 02121021 addu r2,r16,r18               |r2 = AI Data Pointer + Unit ID
00193ee4: 8e0417f8 lw r4,0x17f8(r16)             |Pointer to Acting Unit's attle data
00193ee8: 920219be lbu r2,0x19be(r16)            |Acting Unit preserved Movement Flag AI 0x19be (2nd  pass ? See last routine section)
00193eec: 90830187 lbu r3,0x0187(r4)             |Load Current Unit's Movement Taken? 
00193ef0: 00000000 nop                           |
00193ef4: 14620009 bne r3,r2,0x00193f1c          #If Acting Unit didn't move since last pass
00193ef8: 02121021 addu r2,r16,r18               |AI Data Pointer + Acting Unit ID
00193efc: 90830188 lbu r3,0x0188(r4)                 |Acting Unit preserved Action Flag
00193f00: 920219bd lbu r2,0x19bd(r16)                |Load Current Unit's Action Taken? 
00193f04: 00000000 nop                               |
00193f08: 14620004 bne r3,r2,0x00193f1c              #If Acting Unit didn't Act either since last pass (we're done here)
00193f0c: 02121021 addu r2,r16,r18                   |AI Data Pointer + Acting Unit ID
00193f10: 34020002 ori r2,r0,0x0002                      |r2 = 2
00193f14: 08065076 j 0x001941d8                          |Exit returns 0x00
00193f18: ae220000 sw r2,0x0000(r17)                     |Set Caster Misc 0x158 to 0x2 (Proceed end of turn)

-== Proceed : Action is not selected or Selected Action could be completed ==-
00193f1c: a2120e2e sb r18,0x0e2e(r16)        |Store Acting Unit's ID
00193f20: 9042198c lbu r2,0x198c(r2)         |Acting Unit's Battle ID
00193f24: 00000000 nop                       |
00193f28: 2c420010 sltiu r2,r2,0x0010        |
00193f2c: 14400004 bne r2,r0,0x00193f40      #If Acting Unit ID is invalid (>= 0x10)
00193f30: 00001821 addu r3,r0,r0             |Initialize Unit Counter
00193f34: 34020002 ori r2,r0,0x0002              |
00193f38: 08065076 j 0x001941d8                  |Exit Return 0x00
00193f3c: ae220000 sw r2,0x0000(r17)             |Store 0x02 in Acting unit misc 0x158 (leads to end of turn)
00193f40: 340500ff ori r5,r0,0x00ff          |
00193f44: 00002021 addu r4,r0,r0             |Initialize battle data offset
00193f48: 3c018019 lui r1,0x8019             @LOOP - Though all units (Check validity of all ID at AI 0x198c)
00193f4c: 00240821 addu r1,r1,r4                 |Base of dynamic pointer to Unit battle data
00193f50: 902208cd lbu r2,0x08cd(r1)             |Load Unit's Battle ID 801908cc + 0x01 + unit offset
00193f54: 00000000 nop                           |
00193f58: 10450007 beq r2,r5,0x00193f78          #If this unit do not exists : branch to  next unit
00193f5c: 248401c0 addiu r4,r4,0x01c0            |Battle data offset (+0x1c0 each iteration)
00193f60: 02031021 addu r2,r16,r3                |AI pointer + Unit counter
00193f64: 9042198c lbu r2,0x198c(r2)             |Load this iteration Unit's Battle ID (in AI data)
00193f68: 00000000 nop                           |
00193f6c: 2c420010 sltiu r2,r2,0x0010            |
00193f70: 1040007f beq r2,r0,0x00194170          #If This iteration unit ID is invalid Set Caster Misc 0x158 = 0x02 and Exit Return 0x00 0x02 leads to end of turn
00193f74: 34020002 ori r2,r0,0x0002              |
00193f78: 24630001 addiu r3,r3,0x0001            |Unit Counter + 1
00193f7c: 28620015 slti r2,r3,0x0015             |
00193f80: 1440fff1 bne r2,r0,0x00193f48      Λ Loop while Counter < 0x15
00193f84: 00000000 nop                       |
00193f88: 0c06507e jal 0x001941f8            |-->Acting Unit's Data Setup Set some Acting Unit Data in AI Data
00193f8c: 00000000 nop                       |
00193f90: 8e0217f8 lw r2,0x17f8(r16)         |Pointer to Acting Unit's Battle Data 
00193f94: 00000000 nop                       |
00193f98: 9043015d lbu r3,0x015d(r2)         |Load Acting Unit's Current Ability CT
00193f9c: 340200ff ori r2,r0,0x00ff          |
00193fa0: 14620007 bne r3,r2,0x00193fc0      #If Acting Unit is not charging anything
00193fa4: 00000000 nop                           |
00193fa8: 8e030e34 lw r3,0x0e34(r16)             |Load Acting Unit's AI Decision Pointer
00193fac: 00000000 nop                           |
00193fb0: 90620006 lbu r2,0x0006(r3)             |Load Targeting Flags  Caster AI 0x182c Byte 0x06
00193fb4: 00000000 nop                           |
00193fb8: 304200f7 andi r2,r2,0x00f7             |
00193fbc: a0620006 sb r2,0x0006(r3)              |Disable Flag 0x08  (Charging/Performing Flag ?) Caster AI 0x182c Byte 0x06
00193fc0: 92021818 lbu r2,0x1818(r16)        |Load [Action selected] flag
00193fc4: 00000000 nop                       |
00193fc8: 10400007 beq r2,r0,0x00193fe8      #If Actiong unit action is already selected This byte is set to 0xff in this very routine after AI choice
00193fcc: 00000000 nop                           |
00193fd0: 8e0217f8 lw r2,0x17f8(r16)             |Pointer to Acting Unit's Battle Data 
00193fd4: 00000000 nop                           |
00193fd8: 90420188 lbu r2,0x0188(r2)             |Load Unit's Action Taken Flag
00193fdc: 00000000 nop                           |
00193fe0: 10400038 beq r2,r0,0x001940c4          #If Acting Unit didn't act yet branch to  Selected Action resolution Acting unit moved first then act
00193fe4: 00000000 nop                           |

-== ACTION SELECTION   (Big AI routines are here) ==-
00193fe8: 0c063979 jal 0x0018e5e4            |-->Transfer Considered Action Data to AI Preserve current action data from 0x8019389c (at 0x80193d24)
00193fec: 00000000 nop                       |
00193ff0: 0c0650c5 jal 0x00194314            |-->Set type of target for all targets Set Some AI decision flags for all units (AI 0x182c)
00193ff4: 00000000 nop                       |
00193ff8: 2403ffff addiu r3,r0,-0x0001       |
00193ffc: 14430005 bne r2,r3,0x00194014      #If returned value = -0x01  VSync_call alert (time to refresh animations ?)
00194000: 2402ffff addiu r2,r0,-0x0001           |r2 = -0x01
00194004: 34030001 ori r3,r0,0x0001              |r3 =0x01
00194008: a2000ed4 sb r0,0x0ed4(r16)             |Memory Variable = 0x00 (AI 0xed4)
0019400c: 08065077 j 0x001941dc                  |Exit Return -0x01
00194010: a20319b7 sb r3,0x19b7(r16)             |Set AI 0x19b7 = 0x01 Enable [return to memory] flag
00194014: 8e0217f8 lw r2,0x17f8(r16)         |Active Unit Battle data pointer
00194018: 00000000 nop                       |
0019401c: 90440188 lbu r4,0x0188(r2)         |Action Taken Flag
00194020: 0c065396 jal 0x00194e58            |-->Set AI ability considerations for all units and self Update some Data at AI 0x182c - Set Data at AI 0xef0 (list of abilities for each unit)
00194024: 00000000 nop                       |
00194028: 8e0217f8 lw r2,0x17f8(r16)         |Acting Unit battle data pointer
0019402c: 00000000 nop                       |
00194030: 90440187 lbu r4,0x0187(r2)         |load Movement taken flag
00194034: 0c065251 jal 0x00194944            |-->AI movement/panel data setting Set Tiles Data in AI 0xc24 (walkable) and AI 0xa74 (Reachable)
00194038: 00000000 nop                       |
0019403c: 2403ffff addiu r3,r0,-0x0001       |
00194040: 14430005 bne r2,r3,0x00194058      #If returned value = -0x01  VSync_call alert (time to refresh animations ?)
00194044: 2402ffff addiu r2,r0,-0x0001           |
00194048: 34030001 ori r3,r0,0x0001              |
0019404c: a2030ed4 sb r3,0x0ed4(r16)             |Set Memory to 0x01 (Ai 0xed4)
00194050: 08065077 j 0x001941dc                  |Exit Return -0x01
00194054: a20319b7 sb r3,0x19b7(r16)             |Set AI 0x19b7 = 0x01 Enable [return to memory] flag
00194058: 0c06515c jal 0x00194570            |-->Some targetability setting Set some flags at AI 0xcb4 / AI 0x182c and AI 0x19bb
0019405c: a20019b7 sb r0,0x19b7(r16)         |Disable [return to memory] flag
00194060: 0c0651c7 jal 0x0019471c            |-->Set AI flags for target consideration Set Target for unit without special behavior
00194064: 00000000 nop                       |
00194068: 0c065765 jal 0x00195d94            |-->AI_Initial_Targeting_Selection  Select the best action and store it in AI 0x17fc
0019406c: 00000000 nop                       |
00194070: 2403ffff addiu r3,r0,-0x0001       |
00194074: 14430007 bne r2,r3,0x00194094      #If returned value = -0x01  VSync_call alert (time to refresh animations ?)
00194078: 340200ff ori r2,r0,0x00ff              |
0019407c: 2402ffff addiu r2,r0,-0x0001           |r2 = -0x01
00194080: 34030002 ori r3,r0,0x0002              |
00194084: a2030ed4 sb r3,0x0ed4(r16)             |Set Memory Variable (AI 0xed4) = 0x02
00194088: 34030001 ori r3,r0,0x0001              |
0019408c: 08065077 j 0x001941dc                  |Exit Return -0x01
00194090: a20319b7 sb r3,0x19b7(r16)             |Set AI 0x19b7 = 0x01 Enable [return to memory] flag
00194094: 920317fd lbu r3,0x17fd(r16)        |Load selected Action 0x01 AI 0x17fc + 0x01
00194098: a20019b7 sb r0,0x19b7(r16)         |Disable AI 0x19b7
0019409c: 10600007 beq r3,r0,0x001940bc      #if [0x17fd] <> 0x00 Selected Action 0x01 (unknown purpose)
001940a0: a2021818 sb r2,0x1818(r16)         |Set AI 0x1818 to 0xff (Enable [action is selected] flag ) Seems to be only used in this very routine
001940a4: 8e030e34 lw r3,0x0e34(r16)             |Acting Unit AI decision data pointer
001940a8: 00000000 nop                           |
001940ac: 90620006 lbu r2,0x0006(r3)             |[Decision 0x06]
001940b0: 00000000 nop                           |
001940b4: 34420008 ori r2,r2,0x0008              |
001940b8: a0620006 sb r2,0x0006(r3)              |Enable [Flag 0x08] of [AI Decision 0x06]
001940bc: 0c06398a jal 0x0018e628            |-->0018e628_-_0018e65c Restore current action data (from 0x80193d24 to 0x8019389c) - and Set Action state to Executing
001940c0: 00000000 nop                       |

-== SELECTED ACTION RESOLUTION ==-
001940c4: 8e0317f8 lw r3,0x17f8(r16)         |Pointer to Caster Battle data
001940c8: 00000000 nop                       |
001940cc: 90620187 lbu r2,0x0187(r3)         |Caster Movement Taken flag
001940d0: 00000000 nop                       |
001940d4: 14400019 bne r2,r0,0x0019413c      #If Caster Movement is already Taken : branch to Attack resolution section
001940d8: 00000000 nop                       |
001940dc: 90620188 lbu r2,0x0188(r3)         |Caster Action Taken flag
001940e0: 00000000 nop                       |
001940e4: 14400005 bne r2,r0,0x001940fc      #If Caster Action is already taken Taken  Movement resolution section
001940e8: 00000000 nop                       |
001940ec: 920217fc lbu r2,0x17fc(r16)        |Select action 0x00 AI 0x17fc
001940f0: 00000000 nop                       |
001940f4: 14400011 bne r2,r0,0x0019413c      #If Acting Unit will act first : branch to  Attack resolution section AI Action 0x00 is set to 0x01 if Unit act first and then move
001940f8: 00000000 nop                       |

--- MOVEMENT RESOLUTION -  Movement not taken and  Action taken or Move then Hit (AI 0x17fc = 0x00 ) ---
001940fc: a2001803 sb r0,0x1803(r16)             |Set Selected Action 0x07 = 0x00 AI 0x17fc + 0x07 - (unknown purpose, always seen at 0x00)
00194100: 8e031800 lw r3,0x1800(r16)             |Selected destination coordinates (X, Elevation and Y)
00194104: 8e020e30 lw r2,0x0e30(r16)             |Acting Unit  current Coordinates (X elevation and Y)
00194108: 00000000 nop                           |
0019410c: 1062000b beq r3,r2,0x0019413c          #If Caster is already at selected destination (not moving) : branch to Action resolution
00194110: 00000000 nop                           |
                                                 Else : Caster is not at selected destination (will move)
00194114: ae200000 sw r0,0x0000(r17)                 |Set action unit misc 0x158 = 0x00 (Will move)
00194118: 92021800 lbu r2,0x1800(r16)                |Load destination X
0019411c: 00000000 nop                               |
00194120: a6220004 sh r2,0x0004(r17)                 |Store Destination's X at Misc 0x15c
00194124: 92021802 lbu r2,0x1802(r16)                |Load Destination Y
00194128: 00000000 nop                               |
0019412c: a6220008 sh r2,0x0008(r17)                 |Store Destination's Y at Misc 0x160
00194130: 92021801 lbu r2,0x1801(r16)                |Load Destination Elevation
00194134: 08065063 j 0x0019418c                      >>Jump to  last section (transfer data)
00194138: a6220006 sh r2,0x0006(r17)                 |Store Destination's elvation at Misc 0x15e

--- ATTACK/ABILITY RESOLUTION - Movement Taken / Not moving / Hit then run (Action not taken and AI 0x17fc = 0x01) ---
0019413c: 8e0217f8 lw r2,0x17f8(r16)             |Acting Unit data pointer
00194140: 00000000 nop                           |
00194144: 90420188 lbu r2,0x0188(r2)             |Caster Action Taken flag
00194148: 00000000 nop                           |
0019414c: 14400006 bne r2,r0,0x00194168          #If Caster action is not Taken
00194150: 34020002 ori r2,r0,0x0002              |r2 = 0x02
00194154: 92021805 lbu r2,0x1805(r16)                |Selected Action Skillset
00194158: 00000000 nop                               |
0019415c: 14400006 bne r2,r0,0x00194178              #If Skillset <> 0x00 : branch to  Transfer Action data
00194160: 34020001 ori r2,r0,0x0001                  |
00194164: 34020002 ori r2,r0,0x0002                  |r2 = 0x02
                                                 Else : action is Taken or Skillset = 0x00 (selected action is move only)
00194168: 08065063 j 0x0019418c                      >>Jump to  last section (transfer data)
0019416c: ae220000 sw r2,0x0000(r17)                 |Store 0x02 at misc 0x158 (End of turn)

                  --- Illegal unit ID jump (r2 = 0x2) ---
00194170: 08065076 j 0x001941d8                      |Exit and do not transfer data return 0x00
00194174: ae220000 sw r2,0x0000(r17)                 |Store 0x02 at misc 0x158 (End of turn)

--- TRANSFERT ACTION DATA to MISC. Data (action not taken - skillset is valid) ---
                                                     '
00194178: ae220000 sw r2,0x0000(r17)             |Set Acting Unit misc 0x158 = 0x01 (will Act)
0019417c: 26240004 addiu r4,r17,0x0004           |Pointer to caster Misc 0x158 + 0x04
00194180: 26051804 addiu r5,r16,0x1804           |Pointer to selected Action + 0x08 (start of [battle 0x16e like] data
00194184: 0c066ac2 jal 0x0019ab08                |-->Transfer Halfword Values Transfer selected action data from  AI 0x1804 to misc 0x15c
00194188: 34060014 ori r6,r0,0x0014              |Copy 0x14 Bytes

-== LAST SECTION (transfer / preserve data) ==-
0019418c: 8e0217f8 lw r2,0x17f8(r16)         |Acting Unit data pointer
00194190: 8e0317f8 lw r3,0x17f8(r16)         |Acting Unit data pointer
00194194: 90420187 lbu r2,0x0187(r2)         |Movement Taken flag
00194198: 00000000 nop                       |
0019419c: a20219be sb r2,0x19be(r16)         |Preserve/Save movement taken flag
001941a0: 90620188 lbu r2,0x0188(r3)         |Action Taken flag
001941a4: 26040e30 addiu r4,r16,0x0e30       |Pointer to Active unit coordinates (AI 0xe30)
001941a8: 0c067c96 jal 0x0019f258            |-->Store units coordinates From AI data to Unit Battle data (0x47 0x48 0x49)
001941ac: a20219bd sb r2,0x19bd(r16)         |Preserve/Save action taken flag
001941b0: 3c04801a lui r4,0x801a             |
001941b4: 24840d94 addiu r4,r4,0x0d94        |AI 19d0  0x801a0d94 
001941b8: 24900018 addiu r16,r4,0x0018       |AI 19e8  0x801a0dac (1st part of selected action (move or act) may be stored here
001941bc: 02002821 addu r5,r16,r0            |
001941c0: 0c066ac2 jal 0x0019ab08            |-->Transfer Halfword Values Preserve AI 0x19e8 at AI 0x19d0 (can't find a case were 0x19d0 is read)
001941c4: 34060018 ori r6,r0,0x0018          |0x18 Bytes to send
001941c8: 02002021 addu r4,r16,r0            |0x801a0dac AI 19e8
001941cc: 02202821 addu r5,r17,r0            |Misc 0x158
001941d0: 0c066ac2 jal 0x0019ab08            |-->Transfer Halfword Values  Transfer data from Misc 0x158 to 0x801a0dac (AI 0x19e8) Update 19e8 with current instruction (move or act)
001941d4: 34060018 ori r6,r0,0x0018          |0x18 Bytes to send
001941d8: 00001021 addu r2,r0,r0             |Return 0x00

001941dc: 8fbf001c lw r31,0x001c(r29)
001941e0: 8fb20018 lw r18,0x0018(r29)
001941e4: 8fb10014 lw r17,0x0014(r29)
001941e8: 8fb00010 lw r16,0x0010(r29)
001941ec: 27bd0020 addiu r29,r29,0x0020
001941f0: 03e00008 jr r31
001941f4: 00000000 nop

Return locations

BATTLE.BIN
 00075384: Set_move/act_based_on_skillset