Find Unit at Specified Coordinates

From Final Fantasy Hacktics Wiki
Jump to navigation Jump to search
# ROUTINE: FIND UNIT AT SPECIFIED COORDINATES (0x17dca8)    -    Duplicated here Check_if_another_unit_is_on_the_same_Tile (with newbies friendly formatting)

#   PARAMETERS:
#       r4 = X-Coordinate
#       r5 = Y-Coordinate
#       r6 = Is on Upper Level
#       r7 = Flags to specify which units to find (Example 0x41 = Crystal and Treasure units)
#           0x80 : Chocobo
#           0x40 : Crystal
#           0x10 : Transparent
#           0x01 : Treasure
        
#   RETURNS:
#       r2  =   -2, If coordinates invalid;
#               -1, If no unit found at coordinates with any of the specified flags;
#               >=0, If unit found at coordinates with at least one specified flag; Value = [index of unit found] 
# SECTION 1 : SET UP ROUTINE 

0017dca8: 27bdffd0 addiu r29,r29,0xffd0
0017dcac: afb40020 sw r20,0x0020(r29)
0017dcb0: 0080a021 addu r20,r4,r0               #   r20 = r4 (X)
0017dcb4: afb50024 sw r21,0x0024(r29)
0017dcb8: 00a0a821 addu r21,r5,r0               #   r21 = r5 (Y)
0017dcbc: afb60028 sw r22,0x0028(r29)
0017dcc0: 00c0b021 addu r22,r6,r0               #   r22 = r6 (Is on Upper Level)
0017dcc4: afb20018 sw r18,0x0018(r29)   
0017dcc8: 00e09021 addu r18,r7,r0               #   r18 = r7 (Flags)
0017dccc: afbf002c sw r31,0x002c(r29)
0017dcd0: afb3001c sw r19,0x001c(r29)
0017dcd4: afb10014 sw r17,0x0014(r29)

# SECTION 2 : INTEGRITY CHECK ON COORDINATES

0017dcd8: 0680002c bltz r20,0x0017dd8c          #   IF (X < 0) 
0017dcdc: afb00010 sw r16,0x0010(r29)           #       RETURN -2
0017dce0: 3c03800e lui r3,0x800e
0017dce4: 90634e9c lbu r3,0x4e9c(r3)            #   r3 = [Map MAX X]        
0017dce8: 00000000 nop
0017dcec: 0283102a slt r2,r20,r3                #   IF (X >= [Map MAX X])
0017dcf0: 10400026 beq r2,r0,0x0017dd8c         #       RETURN -2
0017dcf4: 00000000 nop
0017dcf8: 06a0006a bltz r21,0x0017dea4          #   IF (Y < 0) 
0017dcfc: 2402fffe addiu r2,r0,0xfffe           #       RETURN -2
0017dd00: 3c02800e lui r2,0x800e
0017dd04: 90424ea0 lbu r2,0x4ea0(r2)            
0017dd08: 00000000 nop
0017dd0c: 02a2102a slt r2,r21,r2                #   IF (Y >= [Map MAX Y])
0017dd10: 1040001e beq r2,r0,0x0017dd8c         #       RETURN -2
0017dd14: 2ec20002 sltiu r2,r22,0x0002          #   IF (r22 >= 2)
0017dd18: 1040001c beq r2,r0,0x0017dd8c         #       RETURN -2
0017dd1c: 02a30018 mult r21,r3                  
0017dd20: 00161200 sll r2,r22,0x08              
0017dd24: 00001812 mflo r3                      
0017dd28: 00431021 addu r2,r2,r3
0017dd2c: 00541021 addu r2,r2,r20               
0017dd30: 000210c0 sll r2,r2,0x03
0017dd34: 3c038019 lui r3,0x8019
0017dd38: 2463f8cc addiu r3,r3,0xf8cc
0017dd3c: 00431821 addu r3,r2,r3                #   r3 = [Tile data for current tile specified by (X,Y)]
0017dd40: 90620006 lbu r2,0x0006(r3)
0017dd44: 00000000 nop
0017dd48: 30420003 andi r2,r2,0x0003            #   IF (current tile unselectable and/or cannot target)
0017dd4c: 14400055 bne r2,r0,0x0017dea4         #       RETURN -2
0017dd50: 2402fffe addiu r2,r0,0xfffe
0017dd54: 30e20002 andi r2,r7,0x0002            #   r17 = 0; IF (?) (TRUE for r7=65)
0017dd58: 10400010 beq r2,r0,0x0017dd9c         #       JUMP to [SECTION 3]
0017dd5c: 00008821 addu r17,r0,r0               
0017dd60: 90620003 lbu r2,0x0003(r3)
0017dd64: 00000000 nop
0017dd68: 304200e0 andi r2,r2,0x00e0            #   IF (tile depth >= 2)
0017dd6c: 1440004d bne r2,r0,0x0017dea4         #       RETURN -2
0017dd70: 2402fffe addiu r2,r0,0xfffe
0017dd74: 90620003 lbu r2,0x0003(r3)
0017dd78: 00000000 nop
0017dd7c: 3042001f andi r2,r2,0x001f
0017dd80: 28420003 slti r2,r2,0x0003            #   IF (tile height < 3) ??
0017dd84: 14400006 bne r2,r0,0x0017dda0         #       JUMP to [SECTION 3]
0017dd88: 00009821 addu r19,r0,r0
0017dd8c: 0805f7a9 j 0x0017dea4
0017dd90: 2402fffe addiu r2,r0,0xfffe           #   RETURN -2
0017dd94: 0805f7a9 j 0x0017dea4
0017dd98: 3082001f andi r2,r4,0x001f            #   RETURN r4 (low 5 bits)
0017dd9c: 00009821 addu r19,r0,r0               #   r19 = 0 (Offset to current unit in loop)

# SECTION 3 : LOOP THROUGH IN-BATTLE UNITS

#   SECTION 3.0 : START OF LOOP : BEGIN FLAG CHECKS : CRYSTAL=0x40

0017dda0: 3c028019 lui r2,0x8019                #   DO {
0017dda4: 244208cc addiu r2,r2,0x08cc           #       
0017dda8: 02628021 addu r16,r19,r2              #       r16 = [Current unit in loop]
0017ddac: 92030001 lbu r3,0x0001(r16)
0017ddb0: 340200ff ori r2,r0,0x00ff
0017ddb4: 10620036 beq r3,r2,0x0017de90         #       IF (current unit doesn't exist)
0017ddb8: 32420051 andi r2,r18,0x0051           #           CONTINUE (skip to next loop iteration)
0017ddbc: 10400018 beq r2,r0,0x0017de20         #       (Check r18 = fourth parameter) : bits 0x40, 0x10, 0x01
0017ddc0: 32420040 andi r2,r18,0x0040           #       IF (Checked bits) == 0
0017ddc4: 10400007 beq r2,r0,0x0017dde4         #           r2 = (Check r18 bit 0x01); JUMP to [SECTION 3.1]
0017ddc8: 32420001 andi r2,r18,0x0001           
0017ddcc: 92020058 lbu r2,0x0058(r16)           
0017ddd0: 00000000 nop
0017ddd4: 30420040 andi r2,r2,0x0040            #       IF (current unit is crystal)
0017ddd8: 14400012 bne r2,r0,0x0017de24         #           r2 = (Check r18 bit 0x80); JUMP TO [SECTION 3.3]
0017dddc: 32420080 andi r2,r18,0x0080
0017dde0: 32420001 andi r2,r18,0x0001           #       r2 = (Check r18 bit 0x01)

#   SECTION 3.1 : FLAG CHECKS SUBSECTION 1 : TREASURE=0x01 

0017dde4: 10400007 beq r2,r0,0x0017de04         #       IF (Check r18 bit 0x01) == 0
0017dde8: 32420010 andi r2,r18,0x0010           #           r2 = (Check r18 bit 0x10); JUMP to [SECTION 3.2]
0017ddec: 92020059 lbu r2,0x0059(r16)
0017ddf0: 00000000 nop
0017ddf4: 30420001 andi r2,r2,0x0001            #       IF (current unit is treasure)
0017ddf8: 1440000a bne r2,r0,0x0017de24         #           r2 = (Check r18 bit 0x80); JUMP to [SECTION 3.3]
0017ddfc: 32420080 andi r2,r18,0x0080
0017de00: 32420010 andi r2,r18,0x0010           #       r2 = (Check r18 bit 0x10)

#   SECTION 3.2 : FLAG CHECKS SUBSECTION 2 : TRANSPARENT=0x10 

0017de04: 10400022 beq r2,r0,0x0017de90         #       IF (Check r18 bit 0x10) == 0
0017de08: 00000000 nop                          #           CONTINUE (skip to next loop iteration)
0017de0c: 9202005a lbu r2,0x005a(r16)                   
0017de10: 00000000 nop
0017de14: 30420010 andi r2,r2,0x0010
0017de18: 1040001d beq r2,r0,0x0017de90         #       IF (current unit is not transparent)
0017de1c: 00000000 nop                          #           CONTINUE (skip to next loop iteration)
0017de20: 32420080 andi r2,r18,0x0080           #       r2 = (Check r18 bit 0x80)

#   SECTION 3.3 : FLAG CHECKS SUBSECTION 3 : CHOCOBO=0x80

0017de24: 10400005 beq r2,r0,0x0017de3c         #       IF (Check r18 bit 0x80) == 0
0017de28: 00000000 nop                          #           JUMP to [SECTION 3.4]
0017de2c: 0c05d0f2 jal 0x001743c8               #       Routine: Chocobo check? (r4 = current unit pointer)
0017de30: 02002021 addu r4,r16,r0
0017de34: 10400016 beq r2,r0,0x0017de90         #       IF (Chocobo check result == 0)
0017de38: 00000000 nop                          #           CONTINUE (skip to next loop iteration)

#   SECTION 3.4 : UNIT CHECKS

0017de3c: 92020047 lbu r2,0x0047(r16)
0017de40: 00000000 nop
0017de44: 14540012 bne r2,r20,0x0017de90        #       IF (current unit X-coord != X)
0017de48: 00000000 nop                          #           CONTINUE (skip to next loop iteration)
0017de4c: 92020048 lbu r2,0x0048(r16)           
0017de50: 00000000 nop
0017de54: 1455000e bne r2,r21,0x0017de90        #       IF (current unit Y-coord != Y)
0017de58: 00000000 nop                          #           CONTINUE (skip to next loop iteration)
0017de5c: 96020048 lhu r2,0x0048(r16)
0017de60: 00000000 nop
0017de64: 000213c2 srl r2,r2,0x0f               #       IF (current unit IsOnUpperLevel != IsOnUpperLevel)
0017de68: 14560009 bne r2,r22,0x0017de90        #           CONTINUE (skip to next loop iteration)
0017de6c: 00000000 nop
0017de70: 92040182 lbu r4,0x0182(r16)
0017de74: 00000000 nop
0017de78: 308200ff andi r2,r4,0x00ff
0017de7c: 2c420080 sltiu r2,r2,0x0080           #       IF (current unit is riding a unit?)
0017de80: 1040ffc4 beq r2,r0,0x0017dd94         #           RETURN (??)
0017de84: 02201021 addu r2,r17,r0               #       
0017de88: 0805f7a9 j 0x0017dea4                 #       RETURN (current unit index)
0017de8c: 00000000 nop

#   SECTION 3.5 : END OF LOOP

0017de90: 26310001 addiu r17,r17,0x0001
0017de94: 2a220015 slti r2,r17,0x0015
0017de98: 1440ffc1 bne r2,r0,0x0017dda0         #   } WHILE (r17 (current unit index) < 21)
0017de9c: 267301c0 addiu r19,r19,0x01c0
0017dea0: 2402ffff addiu r2,r0,0xffff           #   r2 (Return result) = -1 (No unit found)

# SECTION 4 : RETURN r2

0017dea4: 8fbf002c lw r31,0x002c(r29)
0017dea8: 8fb60028 lw r22,0x0028(r29)
0017deac: 8fb50024 lw r21,0x0024(r29)
0017deb0: 8fb40020 lw r20,0x0020(r29)
0017deb4: 8fb3001c lw r19,0x001c(r29)
0017deb8: 8fb20018 lw r18,0x0018(r29)
0017debc: 8fb10014 lw r17,0x0014(r29)
0017dec0: 8fb00010 lw r16,0x0010(r29)
0017dec4: 27bd0030 addiu r29,r29,0x0030
0017dec8: 03e00008 jr r31
0017decc: 00000000 nop

Return locations

0017dc98: Get_ID_of_Unit_if_Tile_is_Targetable              r7 = 0x00 (just looking for Unit ID at location)
0017fe48: Set_Rider/Mount's_X/Y_Data                        r7 = 0x80 (chocobo)
0017fff0: Set_target_for_mounted_unit,_move_find_item/trap  r7 = 0x41 (Crystal - treasure)
00180314: Set_map_Item/trap_data                            r7 = 0x10 (transparent) 
00180578: Generate_Crystal_or_Treasure                      r7 = 0x41 (crystal - treasure)