Find New Formation Screen Unit Index

From Final Fantasy Hacktics Wiki
Jump to navigation Jump to search

Find New Formation Screen Unit Index

Finds the new formation screen unit index after considering button input (directional, L1/R1)

Parameters

Location Name Value Notes
r4 unitsPerRow 4 Number of units per row in the formation screen
r5 numRows 5 Number of rows in the formation screen
r6 maxFormationUnitIndex (*0x801cd788 - 1) Number of units - 1. Maximum index into the formation screen unit array
r7 oldFormationUnitIndex *0x8018ba20 Currently selected formation unit index
*(sp + 16) buttonInput Button input from Get Inverted Button Input function

Returns

Location Name Notes
r2 formationUnitIndex New formation unit index, after considering button input

Code

8012bb88: 00804821 addu r9,r4,r0                   #   unitsPerRow
8012bb8c: 8faa0010 lw r10,0x0010(r29)              #   buttonInput
8012bb90: 00000000 nop
8012bb94: 31421000 andi r2,r10,0x1000              
8012bb98: 10400011 beq r2,r0,0x8012bbe0            
8012bb9c: 00e04021 addu r8,r7,r0                   #   formationUnitIndex = oldFormationUnitIndex
                                                   #   if (buttonInput & BUTTON_UP) {
8012bba0: 01094023 subu r8,r8,r9                   #       formationUnitIndex = formationUnitIndex - unitsPerRow
8012bba4: 05010022 bgez r8,0x8012bc30                            
8012bba8: 31422000 andi r2,r10,0x2000
                                                   #       if (formationUnitIndex < 0) {
8012bbac: 01250018 mult r9,r5
8012bbb0: 00001012 mflo r2
8012bbb4: 01024021 addu r8,r8,r2                   #           formationUnitIndex = formationUnitIndex + (unitsPerRow * numRows)
8012bbb8: 00c8102a slt r2,r6,r8
8012bbbc: 1040001c beq r2,r0,0x8012bc30
8012bbc0: 31422000 andi r2,r10,0x2000
                                                   #           if (maxFormationUnitIndex < formationUnitIndex) {
8012bbc4: 01094023 subu r8,r8,r9                   #               formationUnitIndex = formationUnitIndex - unitsPerRow                           
8012bbc8: 00c8102a slt r2,r6,r8                    #               do {
8012bbcc: 1440fffe bne r2,r0,0x8012bbc8
8012bbd0: 01094023 subu r8,r8,r9                   #                   formationUnitIndex = formationUnitIndex - unitsPerRow
                                                   #               } while (maxFormationUnitIndex < formationUnitIndex);
8012bbd4: 01094021 addu r8,r8,r9                   #               formationUnitIndex = formationUnitIndex + unitsPerRow
8012bbd8: 0804af0c j 0x8012bc30
8012bbdc: 31422000 andi r2,r10,0x2000
                                                   #           }
                                                   #       }
                                                   #   }
8012bbe0: 31424000 andi r2,r10,0x4000
8012bbe4: 10400012 beq r2,r0,0x8012bc30            #   else if (buttonInput & BUTTON_DOWN) {
8012bbe8: 31422000 andi r2,r10,0x2000
8012bbec: 0109001a div r8,r9
8012bbf0: 00001012 mflo r2                         #       oldRow = formationUnitIndex / unitsPerRow
8012bbf4: 00002810 mfhi r5                         #       oldCol = formationUnitIndex % unitsPerRow
8012bbf8: 00000000 nop
8012bbfc: 00000000 nop
8012bc00: 00c9001a div r6,r9                              
8012bc04: 00001812 mflo r3                         #       maxRow = maxFormationUnitIndex / unitsPerRow
8012bc08: 00000000 nop
8012bc0c: 14430003 bne r2,r3,0x8012bc1c
8012bc10: 01094021 addu r8,r8,r9                   #       formationUnitIndex = formationUnitIndex + unitsPerRow
                                                   #       if (oldRow == maxRow) {
8012bc14: 0804af0b j 0x8012bc2c
8012bc18: 00a04021 addu r8,r5,r0                   #           formationUnitIndex = oldCol
8012bc1c: 00c8102a slt r2,r6,r8                    
8012bc20: 10400003 beq r2,r0,0x8012bc30            #       } else if (maxFormationUnitIndex < formationUnitIndex) {
8012bc24: 31422000 andi r2,r10,0x2000
8012bc28: 00c04021 addu r8,r6,r0                   #           formationUnitIndex = maxFormationUnitIndex
8012bc2c: 31422000 andi r2,r10,0x2000
                                                   #       }
                                                   #   }
8012bc30: 10400012 beq r2,r0,0x8012bc7c            #   if (buttonInput & BUTTON_RIGHT) {
8012bc34: 25040001 addiu r4,r8,0x0001              #       formationUnitIndex + 1
8012bc38: 0089001a div r4,r9
8012bc3c: 00001010 mfhi r2                         #       newCol = (formationUnitIndex + 1) % unitsPerRow
8012bc40: 00000000 nop
8012bc44: 10400003 beq r2,r0,0x8012bc54
8012bc48: 00c4102a slt r2,r6,r4
8012bc4c: 10400009 beq r2,r0,0x8012bc74            #       if ((newCol == 0) or (maxFormationUnitIndex < (formationUnitIndex + 1))) { 
8012bc50: 00000000 nop
8012bc54: 0109001a div r8,r9
8012bc58: 00001012 mflo r2                         #           oldRow = formationUnitIndex / unitsPerRow
8012bc5c: 00000000 nop
8012bc60: 00000000 nop
8012bc64: 01220018 mult r9,r2                      
8012bc68: 00004012 mflo r8                         #           formationUnitIndex = unitsPerRow * oldRow
8012bc6c: 0804af3e j 0x8012bcf8
8012bc70: 00000000 nop                             #       } else {
8012bc74: 0804af3e j 0x8012bcf8
8012bc78: 00804021 addu r8,r4,r0                   #           formationUnitIndex = formationUnitIndex + 1 
                                                   #       }
                                                   #   }
8012bc7c: 31428000 andi r2,r10,0x8000
8012bc80: 1040000f beq r2,r0,0x8012bcc0            #   else if (buttonInput & BUTTON_LEFT) {
8012bc84: 31420008 andi r2,r10,0x0008
8012bc88: 0109001a div r8,r9
8012bc8c: 00001010 mfhi r2                         #       oldCol = formationUnitIndex % unitsPerRow
8012bc90: 00000000 nop
8012bc94: 14400003 bne r2,r0,0x8012bca4
8012bc98: 01091021 addu r2,r8,r9                   
                                                   #       if (oldCol == 0) {
8012bc9c: 0804af2a j 0x8012bca8
8012bca0: 2442ffff addiu r2,r2,-0x0001             #           newValue = (formationUnitIndex + unitsPerRow) - 1
                                                   #       } else {
8012bca4: 2502ffff addiu r2,r8,-0x0001             #           newValue = formationUnitIndex - 1
                                                   #       }
8012bca8: 00404021 addu r8,r2,r0                   #       formationUnitIndex = newValue
8012bcac: 00c8102a slt r2,r6,r8
8012bcb0: 10400011 beq r2,r0,0x8012bcf8            #       if (maxFormationUnitIndex < formationUnitIndex) {
8012bcb4: 00000000 nop
8012bcb8: 0804af3e j 0x8012bcf8
8012bcbc: 00c04021 addu r8,r6,r0                   #           formationUnitIndex = maxFormationUnitIndex
                                                   #       }
8012bcc0: 10400007 beq r2,r0,0x8012bce0            #   } else if (buttonInput & BUTTON_R1) {
8012bcc4: 31420004 andi r2,r10,0x0004
8012bcc8: 25080001 addiu r8,r8,0x0001              #       formationUnitIndex = formationUnitIndex + 1 
8012bccc: 00c8102a slt r2,r6,r8
8012bcd0: 10400009 beq r2,r0,0x8012bcf8            #       if (maxFormationUnitIndex < formationUnitIndex) {
8012bcd4: 00000000 nop
8012bcd8: 0804af3e j 0x8012bcf8
8012bcdc: 00004021 addu r8,r0,r0                   #           formationUnitIndex = 0
                                                   #       }
8012bce0: 10400005 beq r2,r0,0x8012bcf8            #   } else if (buttonInput & BUTTON_L1) {
8012bce4: 00000000 nop
8012bce8: 2508ffff addiu r8,r8,-0x0001             #       formationUnitIndex = formationUnitIndex - 1
8012bcec: 05010002 bgez r8,0x8012bcf8              #       if (formationUnitIndex < 0) {
8012bcf0: 00000000 nop
8012bcf4: 00c04021 addu r8,r6,r0                   #           formationUnitIndex = maxFormationUnitIndex
                                                   #       }
                                                   #   }
8012bcf8: 11070004 beq r8,r7,0x8012bd0c            #   if (formationUnitIndex != oldFormationUnitIndex) {
8012bcfc: 00000000 nop
8012bd00: 34020003 ori r2,r0,0x0003
8012bd04: 3c018019 lui r1,0x8019
8012bd08: a022bacc sb r2,-0x4534(r1)               #       *0x8018bacc = 3
                                                   #   }
8012bd0c: 03e00008 jr r31                          #   return formationUnitIndex
8012bd10: 01001021 addu r2,r8,r0