Generate Crystal or Treasure

From Final Fantasy Hacktics Wiki
Jump to navigation Jump to search
ROUTINE : GENERATE CRYSTAL/TREASURE
   PARAMETERS:
       r4 = [In-battle unit index of moving unit]
       
   RETURNS:
       r2                  =   0x8006623c (Pointer);
       MEMORY[0x8006623c]  =   -1, If no crystal/treasure unit found; 0, If no abilities to inherit from a crystal; 1, Otherwise;
       MEMORY[0x80066254]  =   [Crystal abilities: List of flags by skillset (1-8,9-16,17-24) for which ability slots are contained in crystal];
       MEMORY[0x8006628d]  =   [In-battle unit index of unit found], if a unit was found;
       MEMORY[0x8006628e]  =   [Treasure item ID], if unit is obtaining treasure; 2, Otherwise;
       MEMORY[0x8006628f]  =   ?
# SECTION 1 : SET UP ROUTINE
00180530: 27bdff70 addiu r29,r29,0xff70
00180534: afb20070 sw r18,0x0070(r29)
00180538: 00809021 addu r18,r4,r0           #   r18 = (Moving unit)
0018053c: 34070041 ori r7,r0,0x0041         #   r7 = 0x41 = (Flags for find-unit-at-coords routine: Crystal, Treasure)
00180540: afbf008c sw r31,0x008c(r29)
00180544: afbe0088 sw r30,0x0088(r29)
00180548: afb70084 sw r23,0x0084(r29)
0018054c: afb60080 sw r22,0x0080(r29)
00180550: afb5007c sw r21,0x007c(r29)
00180554: afb40078 sw r20,0x0078(r29)
00180558: afb30074 sw r19,0x0074(r29)
0018055c: afb1006c sw r17,0x006c(r29)
00180560: afb00068 sw r16,0x0068(r29)
# SECTION 2 : FIND APPROPRIATE UNIT, BEGIN LIST?
00180564: 92440047 lbu r4,0x0047(r18)       #   Load in coordinates (X,Y,IsOnUpperLevel) of moving unit
00180568: 96460048 lhu r6,0x0048(r18)
0018056c: 92450048 lbu r5,0x0048(r18)
00180570: 0c05f72a jal 0x0017dca8           #   Routine: Find unit at coordinates (Flags: Crystal, Treasure)
00180574: 000633c2 srl r6,r6,0x0f           
00180578: 00401821 addu r3,r2,r0            #   r3 = r2 (Index of unit found)
0018057c: 04610006 bgez r3,0x00180598       #   IF (No unit found) {
00180580: 000310c0 sll r2,r3,0x03
00180584: 3c028006 lui r2,0x8006
00180588: 2442623c addiu r2,r2,0x623c
0018058c: 2403ffff addiu r3,r0,0xffff               MEM[0x06623c] = -1 (Code for empty?)
00180590: 08060234 j 0x001808d0                     RETURN 0x8006623c
00180594: ac430000 sw r3,0x0000(r2)         #   }
00180598: 3c118006 lui r17,0x8006
0018059c: 2631623c addiu r17,r17,0x623c     #   r17 = 0x8006623c
001805a0: 00431023 subu r2,r2,r3
001805a4: 00021180 sll r2,r2,0x06
001805a8: ae200000 sw r0,0x0000(r17)        #   MEM[0x06623c] = 0
001805ac: 3c018006 lui r1,0x8006
001805b0: a023628d sb r3,0x628d(r1)         #   MEM[0x06628d] = [Coordinate routine result?]
001805b4: 3c038019 lui r3,0x8019
001805b8: 246308cc addiu r3,r3,0x08cc       
001805bc: 00438021 addu r16,r2,r3           #   r16 = (Found unit)
001805c0: 92020059 lbu r2,0x0059(r16)   
001805c4: 00000000 nop
001805c8: 30420001 andi r2,r2,0x0001         
001805cc: 10400008 beq r2,r0,0x001805f0     #   IF (Found unit is treasure status) {
001805d0: 02002021 addu r4,r16,r0
001805d4: 34020004 ori r2,r0,0x0004
001805d8: 0c060282 jal 0x00180a08                   Routine: Generate treasure from treasure unit
001805dc: ae220000 sw r2,0x0000(r17)
001805e0: 3c018006 lui r1,0x8006
001805e4: a022628e sb r2,0x628e(r1)                 MEM[0x6628e] = [Routine result]
001805e8: 08060232 j 0x001808c8                     RETURN 0x8006623c
001805ec: 00000000 nop                      #   }
001805f0: 34020002 ori r2,r0,0x0002
001805f4: ae220000 sw r2,0x0000(r17)        #   MEM[0x6628e] = 2
001805f8: 924b0006 lbu r11,0x0006(r18)      
001805fc: 00000000 nop
00180600: 31620020 andi r2,r11,0x0020
00180604: afab0040 sw r11,0x0040(r29)       #   STACK[0x40] = [Gender/monster flags of moving unit]
00180608: 92030006 lbu r3,0x0006(r16)
0018060c: 144000ae bne r2,r0,0x001808c8     #   IF (Moving unit or found unit is monster), RETURN 0x8006623c
00180610: 30620020 andi r2,r3,0x0020
00180614: 144000ac bne r2,r0,0x001808c8
00180618: 00000000 nop
0018061c: 26240053 addiu r4,r17,0x0053      #   r4 = 0x800662e1
00180620: 34050040 ori r5,r0,0x0040         #   r5 = 0x40 (64)
00180624: 27aa0010 addiu r10,r29,0x0010
00180628: 27ab0018 addiu r11,r29,0x0018
0018062c: 0240b021 addu r22,r18,r0          #   r22 = (Moving unit)
00180630: afa00028 sw r0,0x0028(r29)        #   STACK[0x28] = 0     
00180634: afaa0058 sw r10,0x0058(r29)       #   STACK[0x58] = (Pointer to stack) + 0x10
00180638: afab0050 sw r11,0x0050(r29)       #   STACK[0x50] = (Pointer to stack) + 0x18
0018063c: afb00060 sw r16,0x0060(r29)       #   STACK[0x60] = (Found unit) 
00180640: 92c20096 lbu r2,0x0096(r22)       #   r2 = [Moving unit jobs unlocked 1]  
00180644: 92c30097 lbu r3,0x0097(r22)       #   r3 = [Moving unit jobs unlocked 2]
00180648: 92c60098 lbu r6,0x0098(r22)       #   r6 = [Moving unit jobs unlocked 3]
0018064c: 00021400 sll r2,r2,0x10
00180650: 00031a00 sll r3,r3,0x08
00180654: 00431021 addu r2,r2,r3
00180658: 00461021 addu r2,r2,r6
0018065c: afa20048 sw r2,0x0048(r29)        #   STACK[0x48] = [Moving unit unlocked job info]
00180660: 92ca0000 lbu r10,0x0000(r22)      #   r10 = [Moving unit base job]
00180664: 0000a821 addu r21,r0,r0           #   r21 = 0
00180668: afaa0030 sw r10,0x0030(r29)       #   STACK[0x30] = [Moving unit base job]
0018066c: 920b0000 lbu r11,0x0000(r16)      #   r11 = [Found unit base job]
00180670: 26370018 addiu r23,r17,0x0018     #   r23 = 0x66254 (Starting list position?)
00180674: 0c017991 jal 0x0005e644           #   Routine: Clear (zero out) array (pointer=0x6628F, size=0x40=64)
00180678: afab0038 sw r11,0x0038(r29)       #   STACK[0x38] = [Found unit base job]

SECTION 3 : START OF LOOP

0018067c: 27a40020 addiu r4,r29,0x0020      #   DO {
00180680: 26c60099 addiu r6,r22,0x0099      #       r6 = [Moving unit's action abilities for current class]
00180684: 02e01821 addu r3,r23,r0           #       r3 = [List position]
00180688: 27a90023 addiu r9,r29,0x0023
0018068c: 8faa0060 lw r10,0x0060(r29)       #       r10 = [Found unit]
00180690: 8fa70050 lw r7,0x0050(r29)        #       r7 = (Pointer to stack) + 0x18
00180694: 8fa50058 lw r5,0x0058(r29)        #       r5 = (Pointer to stack) + 0x10
00180698: 25480099 addiu r8,r10,0x0099      #       r8 = [Found unit's action abilities for current class]

SECTION 4 : LOOP THROUGH BASE ACTION ABILITIES

0018069c: a0600000 sb r0,0x0000(r3)         #       FOR (i=1; i < 3; i++) {
001806a0: a0a00000 sb r0,0x0000(r5)         #           MEM[r3] = 0; STACK[r5] = 0
001806a4: 90c20000 lbu r2,0x0000(r6)        #           r2 = (i-th Moving unit base action abilities)
001806a8: 24c60001 addiu r6,r6,0x0001       #           r6++
001806ac: 24a50001 addiu r5,r5,0x0001       #           r5++
001806b0: 24630001 addiu r3,r3,0x0001       #           r3++
001806b4: a0e20000 sb r2,0x0000(r7)         #           STACK[r7] = (i-th Moving unit base action abilities)
001806b8: 91020000 lbu r2,0x0000(r8)        #           r2 = (i-th Found unit base action abilities)
001806bc: 25080001 addiu r8,r8,0x0001       #           r8++
001806c0: a0820000 sb r2,0x0000(r4)         #           STACK[r4] = (i-th Found unit base action abilities)
001806c4: 24840001 addiu r4,r4,0x0001       #           r4++; r7++
001806c8: 0089102a slt r2,r4,r9             #       }
001806cc: 1440fff3 bne r2,r0,0x0018069c
001806d0: 24e70001 addiu r7,r7,0x0001        

SECTION 5 : BASE JOB CHECK

                                            #       r3 = r21 + 0x4A (NOTE: Start of generic jobs? Job 0x4A = Squire)
001806d4: 16a0000b bne r21,r0,0x00180704    #       IF (r21 != 0)
001806d8: 26a3004a addiu r3,r21,0x004a      #           JUMP to [SECTION 6] 
001806dc: 8fab0030 lw r11,0x0030(r29)       #       r11 = [Moving unit base job]
001806e0: 8faa0038 lw r10,0x0038(r29)       #       r10 = [Found unit base job]
001806e4: 00000000 nop                      #       IF (r11 != r10)
001806e8: 156a0065 bne r11,r10,0x00180880   #           JUMP to [SECTION 14]
001806ec: 29620080 slti r2,r11,0x0080       #       IF (r11 >= 0x80)
001806f0: 1040000a beq r2,r0,0x0018071c     #           JUMP to [SECTION 7]
001806f4: 34020011 ori r2,r0,0x0011
001806f8: 8fa30030 lw r3,0x0030(r29)        #       r3 = [Moving unit base job]
001806fc: 080601c7 j 0x0018071c             #       JUMP to [SECTION 7]
00180700: 00000000 nop
SECTION 6 : JOB UNLOCKED CHECK

00180704: 3c020080 lui r2,0x0080
00180708: 8fab0048 lw r11,0x0048(r29)       #       r11 = [Moving unit unlocked job info]
0018070c: 02a21007 srav r2,r2,r21
00180710: 01621024 and r2,r11,r2            #       IF (Moving unit doesn't have this job unlocked)
00180714: 1040005a beq r2,r0,0x00180880     #           Jump to [SECTION 14]
00180718: 34020011 ori r2,r0,0x0011

SECTION 7 : BARD CHECK
0018071c: 16a20006 bne r21,r2,0x00180738    #       IF (r21 != 0x11 (BARD))
00180720: 34020012 ori r2,r0,0x0012         #           Jump to [SECTION 8]
00180724: 8faa0040 lw r10,0x0040(r29)       #       r10 = [Gender/monster flags of moving unit]
00180728: 00000000 nop
0018072c: 31420040 andi r2,r10,0x0040       
00180730: 14400053 bne r2,r0,0x00180880     #       IF (Moving unit is female)
00180734: 34020012 ori r2,r0,0x0012         #           JUMP to [SECTION 14] 
SECTION 8 : DANCER CHECK
                                            #       r17 = 0 (NULL?)
00180738: 16a20006 bne r21,r2,0x00180754    #       IF (r21 != 0x12 (DANCER))
0018073c: 00008821 addu r17,r0,r0           #           Jump to [SECTION 9]
00180740: 8fab0040 lw r11,0x0040(r29)       #       r11 = [Gender/monster flags of moving unit]
00180744: 00000000 nop
00180748: 31620080 andi r2,r11,0x0080       #       IF (Moving unit is male)
0018074c: 1440004c bne r2,r0,0x00180880     #           JUMP to [SECTION 14]
00180750: 00000000 nop
SECTION 9 : FIND SKILLSET ID
00180754: 00031040 sll r2,r3,0x01
00180758: 00431021 addu r2,r2,r3
0018075c: 3c038006 lui r3,0x8006
00180760: 8c636194 lw r3,0x6194(r3)         
00180764: 00021100 sll r2,r2,0x04
00180768: 00431021 addu r2,r2,r3            
0018076c: 905e0000 lbu r30,0x0000(r2)       #       r30 = [Skillset ID of current job]      
SECTION 10 : BEGIN INNER LOOP : LOOP THROUGH ABILITIES OF SKILLSET?

00180770: 06210002 bgez r17,0x0018077c      #       DO
00180774: 02201821 addu r3,r17,r0           #       {
00180778: 26230007 addiu r3,r17,0x0007      #           r3 = [Ability index]   NOTE: Calculation is: r3 =(r17 >= 0) ? r17 : (r17 + 7)
SECTION 11

0018077c: 000390c3 sra r18,r3,0x03
00180780: 001218c0 sll r3,r18,0x03
00180784: 02231823 subu r3,r17,r3           #           r3 = Offset?
00180788: 8faa0050 lw r10,0x0050(r29)       #           r10 = (Pointer to stack) + 0x18
0018078c: 34040080 ori r4,r0,0x0080         #           r4 = 0x80
00180790: 01521021 addu r2,r10,r18          #           r2 = Offset?
00180794: 90420000 lbu r2,0x0000(r2)        #           r2 = STACK[0x18+r18]
00180798: 00649807 srav r19,r4,r3
0018079c: 00531024 and r2,r2,r19            #           r2 = (Stack pointer offset?); IF (???)
001807a0: 14400029 bne r2,r0,0x00180848     #               JUMP to [SECTION 12]
001807a4: 03b21021 addu r2,r29,r18                  
001807a8: 90420020 lbu r2,0x0020(r2)        #           r2 = MEM[r2+0x20]
001807ac: 00000000 nop
001807b0: 00531024 and r2,r2,r19            #           r2 = Offset?
001807b4: 10400024 beq r2,r0,0x00180848     #           IF (???)
001807b8: 03c02021 addu r4,r30,r0           #               Jump to [SECTION 12]
001807bc: 0c01695a jal 0x0005a568           #           Routine : Get Ability ID (r4=[Skillset ID]; r5=[Ability index])
001807c0: 02202821 addu r5,r17,r0
001807c4: 0040a021 addu r20,r2,r0           
001807c8: 3290ffff andi r16,r20,0xffff      #           r16 = (Routine result)
001807cc: 1200001e beq r16,r0,0x00180848    #           IF (r16 == 0) JUMP to [SECTION 12]
001807d0: 001010c0 sll r2,r16,0x03          #           
001807d4: 3c018006 lui r1,0x8006
001807d8: 00220821 addu r1,r1,r2            
001807dc: 9025ebf2 lbu r5,-0x140e(r1)       #           r5 = (Ability's Chance to Learn?)
001807e0: 0c017833 jal 0x0005e0cc           #           Routine: Pass/Fail Roll (Possibilities=100, Threshold=r5)
001807e4: 34040064 ori r4,r0,0x0064
001807e8: 14400017 bne r2,r0,0x00180848     #           IF (Roll was a Fail)
001807ec: 00000000 nop                      #               JUMP to [SECTION 12]
001807f0: 8fab0058 lw r11,0x0058(r29)       #           r11 = (Pointer to stack) + 0x10
001807f4: 00000000 nop
001807f8: 01721021 addu r2,r11,r18          #           r2 = Offset?
001807fc: 8faa0028 lw r10,0x0028(r29)       #           
00180800: 90430000 lbu r3,0x0000(r2)        #           r3 = MEM[r2]
00180804: 254a0001 addiu r10,r10,0x0001     #           
00180808: 02631825 or r3,r19,r3             #           r3 = ?
0018080c: afaa0028 sw r10,0x0028(r29)       #           STACK[0x28]++
00180810: a0430000 sb r3,0x0000(r2)         #           MEM[r2] = r3
00180814: 2e020200 sltiu r2,r16,0x0200      #           IF (r16 >= 0x200)
00180818: 1040000b beq r2,r0,0x00180848     #               JUMP to [SECTION 12]
0018081c: 001020c2 srl r4,r16,0x03
00180820: 32850007 andi r5,r20,0x0007       #           r5 = r20 + 7
00180824: 34020001 ori r2,r0,0x0001         #           r2 = 1
00180828: 3c018006 lui r1,0x8006
0018082c: 00240821 addu r1,r1,r4
00180830: 9023628f lbu r3,0x628f(r1)        #           r3 = (Element of list)
00180834: 00a21004 sllv r2,r2,r5
00180838: 00431025 or r2,r2,r3
0018083c: 3c018006 lui r1,0x8006
00180840: 00240821 addu r1,r1,r4
00180844: a022628f sb r2,0x628f(r1)         #           (Save new element to list)
SECTION 12 : END OF INNER LOOP
00180848: 26310001 addiu r17,r17,0x0001
0018084c: 2a220018 slti r2,r17,0x0018
00180850: 1440ffc7 bne r2,r0,0x00180770     #       } WHILE (r17 < 0x18)
00180854: 26e50003 addiu r5,r23,0x0003
00180858: 02e01821 addu r3,r23,r0
0018085c: 8fa40058 lw r4,0x0058(r29)    
SECTION 13 : WRITE ELEMENTS TO LIST
       
00180860: 00000000 nop                      #       DO {
00180864: 90820000 lbu r2,0x0000(r4)        
00180868: 00000000 nop
0018086c: a0620000 sb r2,0x0000(r3)         #           LIST[r23 + i] = STACK[0x10 + i]
00180870: 24630001 addiu r3,r3,0x0001       #           r3++; r4++
00180874: 0065102a slt r2,r3,r5             
00180878: 1440fff9 bne r2,r0,0x00180860     #       } WHILE (r3 < r5)
0018087c: 24840001 addiu r4,r4,0x0001
SECTION 14 : END OF OUTER LOOP : INCREMENT APPROPRIATE COUNTERS/VARIABLES
00180880: 26f70003 addiu r23,r23,0x0003     #       r23 += 3                NOTE: r23 = (Position in list?)
00180884: 26d60003 addiu r22,r22,0x0003     #       r22 += 3                NOTE: r22 = [Moving unit offset]
00180888: 26b50001 addiu r21,r21,0x0001     #       r21++                   NOTE: r21 = (Current job ID) - 0x4A (Loop counter)
0018088c: 8fab0060 lw r11,0x0060(r29)       
00180890: 2aa20013 slti r2,r21,0x0013       #       STACK[0x60] += 3        NOTE: STACK[0x60] = [Found unit offset]
00180894: 256b0003 addiu r11,r11,0x0003 
00180898: 1440ff78 bne r2,r0,0x0018067c     #   } WHILE (r21 < 0x13)
0018089c: afab0060 sw r11,0x0060(r29)
SECTION 15
001808a0: 8faa0028 lw r10,0x0028(r29)
001808a4: 00000000 nop
001808a8: 11400007 beq r10,r0,0x001808c8    #   IF (STACK[0x28] == 0)
001808ac: 00000000 nop                      #       RETURN 0x8006623c
001808b0: 3c038006 lui r3,0x8006
001808b4: 2463623c addiu r3,r3,0x623c
001808b8: 8c620000 lw r2,0x0000(r3)
001808bc: 00000000 nop
001808c0: 34420001 ori r2,r2,0x0001
001808c4: ac620000 sw r2,0x0000(r3)         #   MEM[0x6623c]++
SECTION 16 : RETURN 0x8006623c (Pointer to list?)
001808c8: 3c028006 lui r2,0x8006
001808cc: 2442623c addiu r2,r2,0x623c       #   RETURN 0x8006623c
001808d0: 8fbf008c lw r31,0x008c(r29)
001808d4: 8fbe0088 lw r30,0x0088(r29)
001808d8: 8fb70084 lw r23,0x0084(r29)
001808dc: 8fb60080 lw r22,0x0080(r29)
001808e0: 8fb5007c lw r21,0x007c(r29)
001808e4: 8fb40078 lw r20,0x0078(r29)
001808e8: 8fb30074 lw r19,0x0074(r29)
001808ec: 8fb20070 lw r18,0x0070(r29)
001808f0: 8fb1006c lw r17,0x006c(r29)
001808f4: 8fb00068 lw r16,0x0068(r29)
001808f8: 27bd0090 addiu r29,r29,0x0090
001808fc: 03e00008 jr r31
00180900: 00000000 nop