Add Unique Value to List

From Final Fantasy Hacktics Wiki
Jump to navigation Jump to search
#   ROUTINE: ADD UNIQUE VALUE TO LIST
#
#       Searches for the specified value (p_storeValue) inside a list defined at 0x8018f870.
#       If not found, inserts the value at the end of the list and increments the length (defined at 0x8018f89c).
#       If (p_maxPrevStoreCount == 0xff), then initializes the list and doesn't store anything.
#       Returns 0 if the length of the list is less than the specified max length (p_maxLength), or 1 if not.
#
#       Parameters:
#           r4 = (p_maxPrevStoreCount) Maximum number of times the list could have been stored into since being initialized
#               A value of 0xff indicates that the list should be initialized.
#           r5 = (p_storeValue) Value to add to the list
#           r6 = (p_maxLength) Maximum length of the list
#       Returns:
#           r2 = {
#               0, if the length of the list (after storing, if applicable) < (p_maxLength)
#               1, otherwise
#           }

8017fbec: 27bdfff8 addiu r29,r29,-0x0008
8017fbf0: 340200ff ori r2,r0,0x00ff                 #   0xff
8017fbf4: 14820005 bne r4,r2,0x8017fc0c             
8017fbf8: 00a04821 addu r9,r5,r0                    #   p2
                                                    #   if (p_maxPrevStoreCount == 0xff) { // Initialize list and return
8017fbfc: 3c018019 lui r1,0x8019
8017fc00: ac20f89c sw r0,-0x0764(r1)                #       length = 0              // (length = *0x8018f89c)
8017fc04: 0805ff2f j 0x8017fcbc                     #       return 0;
8017fc08: 00001021 addu r2,r0,r0
                                                    #   }
8017fc0c: 1480000a bne r4,r0,0x8017fc38             
8017fc10: 340700ff ori r7,r0,0x00ff                 #   valueIndex = 0xff
                                                    #   if (p_maxPrevStoreCount == 0) {    // List is empty, so don't need to search it
8017fc14: 34030001 ori r3,r0,0x0001
8017fc18: 3c018019 lui r1,0x8019
8017fc1c: ac23f89c sw r3,-0x0764(r1)                #       length = 1
8017fc20: 3c018019 lui r1,0x8019
8017fc24: a429f870 sh r9,-0x0790(r1)                #       list[0] = p_storeValue  // (list = 0x8018f870)
8017fc28: 0805ff2f j 0x8017fcbc                     #       return 0;
8017fc2c: 00001021 addu r2,r0,r0
                                                    #   }
8017fc30: 0805ff20 j 0x8017fc80
8017fc34: 00603821 addu r7,r3,r0
                                                    #   else {
8017fc38: 3c048019 lui r4,0x8019
8017fc3c: 8c84f89c lw r4,-0x0764(r4)                #       length      
8017fc40: 00000000 nop
8017fc44: 1880000e blez r4,0x8017fc80               #       if (length >= 0) {
8017fc48: 00001821 addu r3,r0,r0                    #           index = 0
8017fc4c: 00051400 sll r2,r5,0x10
8017fc50: 00024403 sra r8,r2,0x10                   #           p2
8017fc54: 00802821 addu r5,r4,r0                    #           length
8017fc58: 3c048019 lui r4,0x8019
8017fc5c: 2484f870 addiu r4,r4,-0x0790              #           listEntryPtr = 0x8018f870
                                                    #           do {
8017fc60: 84820000 lh r2,0x0000(r4)                 #               listEntryValue = *listEntryPtr
8017fc64: 00000000 nop
8017fc68: 1048fff1 beq r2,r8,0x8017fc30             #               if (listEntryValue == p_storeValue)     { valueIndex = index; break; }
8017fc6c: 00000000 nop
8017fc70: 24630001 addiu r3,r3,0x0001               #               index = index + 1
8017fc74: 0065102a slt r2,r3,r5
8017fc78: 1440fff9 bne r2,r0,0x8017fc60
8017fc7c: 24840002 addiu r4,r4,0x0002               #               listEntryPtr = listEntryPtr + 2         // Advance to next list entry
                                                    #           } while (index < length);
                                                    #       }
                                                    #   }
8017fc80: 340200ff ori r2,r0,0x00ff                 #   0xff
8017fc84: 14e2000c bne r7,r2,0x8017fcb8             #   if (valueIndex != 0xff) {
8017fc88: 00e6102a slt r2,r7,r6                     #       return (valueIndex < p_maxLength) ? 0 : 1;      // Return 0 if (valueIndex < p_maxLength), 1 otherwise
                                                    #   }
8017fc8c: 3c078019 lui r7,0x8019
8017fc90: 8ce7f89c lw r7,-0x0764(r7)                #   oldLength = length
8017fc94: 00000000 nop
8017fc98: 24e20001 addiu r2,r7,0x0001               #   oldLength + 1
8017fc9c: 3c018019 lui r1,0x8019
8017fca0: ac22f89c sw r2,-0x0764(r1)                #   length = oldLength + 1
8017fca4: 00071040 sll r2,r7,0x01                   #   oldLength * 2
8017fca8: 3c018019 lui r1,0x8019
8017fcac: 00220821 addu r1,r1,r2
8017fcb0: a429f870 sh r9,-0x0790(r1)                #   list[oldLength] = p_storeValue
8017fcb4: 00e6102a slt r2,r7,r6                     #   return (oldLength < p_maxLength) ? 0 : 1;           // Return 0 if (oldLength < p_maxLength), 1 otherwise
8017fcb8: 38420001 xori r2,r2,0x0001
8017fcbc: 27bd0008 addiu r29,r29,0x0008
8017fcc0: 03e00008 jr r31
8017fcc4: 00000000 nop