BACKUP ~lolfixer/backup~
AUTHOR ~Lollorian~
VERSION ~19022016~

ALWAYS
  ACTION_CLEAR_ARRAY tutu_convert_itm
  ACTION_DEFINE_ASSOCIATIVE_ARRAY tutu_convert_itm BEGIN
    _AROW01    =>    AROW01
    _AROW02    =>    AROW02
    _AROW07    =>    AROW07
    _AX1H04    =>    AX1H04
    _BOW01     =>    BOW01
    _BOW03     =>    BOW03
    _BOW08     =>    BOW08
    _BLUN02    =>    BLUN02
    _BULL02    =>    BULL02
    _BRAC04    =>    BRAC04
    _CHAN01    =>    CHAN01
    _CHAN04    =>    CHAN04
    _CHAN05    =>    CHAN05
    _CHAN07    =>    CHAN07
    _CLCK17    =>    CLCK17
    _DAGG01    =>    DAGG01
    _DAGG03    =>    DAGG03
    _DAGG05    =>    DAGG05
    _DART01    =>    DART01
    _DART02    =>    DART02
    _HELM01    =>    HELM01
    _HELM09    =>    HELM09
    _HELM11    =>    HELM11
    _JELLGR1   =>    JELLGR1
    _LEAT01    =>    LEAT01
    _LEAT02    =>    LEAT02
    _LEAT04    =>    LEAT04
    _MAGE01    =>    MAGE02
    _MAGE02    =>    MAGE02
    _MAGE05    =>    MAGE05
    _AGEBRAC   =>    MAGEBRAC
    _MISC33    =>    MISC33
    _PLAT01    =>    PLAT01
    _PLAT04    =>    PLAT04
    _POTN08    =>    POTN08
    _POTN20    =>    POTN20
    _POTN21    =>    POTN21
    _POTN36    =>    POTN36
    _POTN37    =>    POTN37
    _POTN45    =>    POTN45
    _RING95    =>    RING95
    _SCRL96    =>    SCRL96
    _SHLD01    =>    SHLD01
    _SHLD08    =>    SHLD08
    _SLNG02    =>    SLNG02
    _SPER01    =>    SPER01
    _SPER02    =>    SPER02
    _STAF01    =>    STAF01
    _STAF02    =>    STAF02
    _STAF07    =>    STAF07
    _SW1H01    =>    SW1H01
    _SW1H04    =>    SW1H04
    _SW1H07    =>    SW1H07
    _SW1H08    =>    SW1H08
    _SW1H24    =>    SW1H24
    _SW2H01    =>    SW2H01

    // Typoed Items
    2HSW02     =>    SW2H02
    ANKSHELL   =>    MISC1A
    ARRW01     =>    AROW01
    BLUNT06    =>    BLUN06
    CHAM01     =>    CHAN01
    DWCHA01    =>    DWCHAN01
    GHOST01    =>    GHOST
    HRZRSHED   =>    SD_HRZR
    IOUNX6     =>    IOUNX8
    LEATH01    =>    LEAT01
    LEATH04    =>    LEAT04
    MIN1HP     =>    MINHP1
    RDNTRE03   =>    RNDTRE03
    RDNTRE04   =>    RNDTRE04
    RNGDEMN    =>    RINGDEMN
    SCRL1R     =>   "U!SCRL03" // 'cause WeiDU don't like dem exclamations in filenames
    SH1H04     =>    SW1H04
    SLING04    =>    SLNG04
    STAFF01    =>    STAF01
    UMBER1     =>    UMBER01
    VAMP01     =>    VAMP1
  END
END

BEGIN ~Area Stuff - MAJESTIC Area Fixer~
  GROUP ~Area Stuff~

ACTION_IF !(FILE_EXISTS ~override/xr2400.are~) THEN BEGIN
  COPY_EXISTING ~ar0087.are~ ~override/xr2400.are~
END
ACTION_IF !(FILE_EXISTS ~override/xr2600.are~) THEN BEGIN
  COPY_EXISTING ~ar0087.are~ ~override/xr2600.are~
END

COPY_EXISTING_REGEXP GLOB ~^.+\.are$~ ~override~
  PATCH_IF (SOURCE_SIZE > 0x11b) THEN BEGIN

    // Script Assigner
    READ_ASCII 0x94 "script"

    SET "exists" = 0
    SET "scexists" = 0
    SET "unassigned" = 0
    SET "missing" = 0
    PATCH_IF FILE_EXISTS_IN_GAME ~%SOURCE_RES%.bcs~ BEGIN
      SET "exists" = 1
    END
    PATCH_IF FILE_EXISTS_IN_GAME ~%script%.bcs~ BEGIN
      SET "scexists" = 1
    END
    PATCH_IF (("%scexists%" = 0) AND ("%exists%" = 0)) BEGIN
      SET "missing" = 1
    END
    PATCH_IF (("%scexists%" = 0) AND ("%exists%" = 1)) BEGIN
      SET "unassigned" = 1
    END
    PATCH_IF ("%unassigned%" = 1) BEGIN
      PATCH_PRINT "%SOURCE_FILE% issue: Area Script (%SOURCE_RES%.bcs) assigned"
      WRITE_ASCIIE 0x94 ~%SOURCE_RES%~ #8
    END ELSE
    PATCH_IF ("%missing%" = 1) BEGIN
      PATCH_IF NOT (("%script%" STRING_EQUAL_CASE "NONE") OR ("%script%" STRING_EQUAL_CASE "")) BEGIN
        PATCH_PRINT "%SOURCE_FILE% issue: Missing area Script (%script%.bcs) created"
        INNER_ACTION BEGIN
          <<<<<<<< %script%.BAF
>>>>>>>>
          COMPILE ~%script%.BAF~
        END
      END ELSE
      BEGIN
        PATCH_PRINT "%SOURCE_FILE% issue: Missing area Script (%SOURCE_RES%.bcs) created and assigned"
        INNER_ACTION BEGIN
          <<<<<<<< %SOURCE_RES%.BAF
>>>>>>>>
          COMPILE ~%SOURCE_RES%.BAF~
        END
        WRITE_ASCIIE 0x94 ~%SOURCE_RES%~ #8
      END
    END

    // Container Content Validator
    READ_LONG  0x70 "cont_off" ELSE 0
    READ_SHORT 0x74 "cont_num" ELSE 0
    READ_SHORT 0x76 "itm_num"  ELSE 0
    READ_LONG  0x78 "itm_off"  ELSE 0

    PATCH_IF (cont_off > 0x11b) THEN BEGIN
      FOR (index = 0 ; index < cont_num ; index = index + 1) BEGIN

        // Non-empty containers that aren't visible get their items dropped on the ground
        READ_SHORT ("%cont_off%" + 0x24 + (0xc0 * "%index%")) "cont_type"
        READ_SHORT ("%cont_off%" + 0x38 + (0xc0 * "%index%")) "bound_a"
        READ_SHORT ("%cont_off%" + 0x3a + (0xc0 * "%index%")) "bound_b"
        READ_SHORT ("%cont_off%" + 0x3c + (0xc0 * "%index%")) "bound_c"
        READ_SHORT ("%cont_off%" + 0x3e + (0xc0 * "%index%")) "bound_d"
        READ_LONG  ("%cont_off%" + 0x44 + (0xc0 * "%index%")) "cont_itm_num"
        PATCH_IF ("%bound_a%" = 0) AND ("%bound_b%" = 0) AND ("%bound_c%" = 0) AND ("%bound_d%" = 0) AND ("%cont_type%" != 4) AND ("%cont_itm_num%" > 0) BEGIN
          WRITE_SHORT ("%cont_off%" + 0x24 + (0xc0 * "%index%")) 4
          PATCH_PRINT "%SOURCE_FILE% issue: Container #%index% has no bounding box but contains items! Dropping all items to ground..."
        END

        // Invalid container content checker
        READ_LONG  ("%cont_off%" + 0x40 + (0xc0 * "%index%")) "cont_itm_idx"
        READ_LONG  ("%cont_off%" + 0x44 + (0xc0 * "%index%")) "cont_itm_num"
        FOR (index2 = 0 ; index2 < cont_itm_num ; index2 = index2 + 1) BEGIN
          READ_ASCII ("%itm_off%" + (("%index2%" + "%cont_itm_idx%") * 0x14)) "resref"
          SET "exists" = 0
          PATCH_IF FILE_EXISTS_IN_GAME ~%resref%.itm~ BEGIN
            SET "exists" = 1
          END
          PATCH_IF ("%exists%" = 0) BEGIN
            SET "converted" = 0
            PHP_EACH tutu_convert_itm AS tutu_itm => bgt_itm BEGIN
              PATCH_IF ("%resref%" STRING_EQUAL_CASE "%tutu_itm%") AND (FILE_EXISTS_IN_GAME "%bgt_itm%.itm") BEGIN
                WRITE_ASCIIE ("%itm_off%" + (("%index2%" + "%cont_itm_idx%") * 0x14)) "%bgt_itm%" #8
                SET "converted" = 1
                PATCH_PRINT "%SOURCE_FILE% issue: Container #%index% Item #%index2% had Tutu item! (%resref% -> %bgt_itm%)!"
              END
            END
            PATCH_IF ("%converted%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Container #%index% Item #%index2% does not exist (%resref%.itm)! Removing Item #%index2%..."
              LAUNCH_PATCH_FUNCTION  ~fj_are_structure~
                INT_VAR fj_delete_mode    = "%index2%" + "%cont_itm_idx%"
                STR_VAR fj_structure_type = ~itm~
              END
              READ_LONG  0x70 "cont_off" ELSE 0
              READ_SHORT 0x74 "cont_num" ELSE 0
              READ_SHORT 0x76 "itm_num"  ELSE 0
              READ_LONG  0x78 "itm_off"  ELSE 0
              READ_LONG  ("%cont_off%" + 0x44 + (0xc0 * "%index%")) "cont_itm_num"
              SET index = 0 - 1
              SET index2 = cont_itm_num
            END
          END
        END

      END
    END

    // Missing Ambient Remover
    READ_SHORT 0x82 "amb_num" ELSE 0
    READ_LONG  0x84 "amb_off" ELSE 0

    PATCH_IF (amb_off > 0x11b) THEN BEGIN
      FOR (index = 0 ; index < amb_num ; index = index + 1) BEGIN
        FOR (index2 = 0 ; index2 < 10 ; index2 = index2 + 1) BEGIN
          READ_ASCII ("%amb_off%" + 0x30 + ("%index2%" * 0x08) + (0xd4 * "%index%")) "resref"
          PATCH_IF NOT (("%resref%" STRING_EQUAL_CASE "") OR ("%resref%" STRING_EQUAL_CASE "none")) BEGIN
            SET "exists" = 0
            PATCH_IF FILE_EXISTS_IN_GAME ~%resref%.wav~ BEGIN
              SET "exists" = 1
            END
            PATCH_IF ("%exists%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Ambient #%index% does not exist (%resref%.wav)! Nulling..."
              WRITE_ASCII ("%amb_off%" + 0x30 + ("%index2%" * 0x08) + (0xd4 * "%index%")) ~NONE~ #8
            END
          END
        END
      END
    END

    // Missing Actor Remover
    READ_LONG  0x54 "actor_off" ELSE 0
    READ_SHORT 0x58 "actor_num" ELSE 0

    PATCH_IF (actor_off > 0x11b) THEN BEGIN
      FOR (index = 0 ; index < actor_num ; index = index + 1) BEGIN
        READ_BYTE  ("%actor_off%" + 0x28 + (0x110 * "%index%")) "flags"  // flags first byte
        READ_ASCII ("%actor_off%" + 0x80 + (0x110 * "%index%")) "resref" // cre file
        PATCH_IF ("%resref%" STRING_COMPARE_REGEXP "^[Rr][Dd].+$") BEGIN // ignore creatures starting with RD
          SET "exists" = 0
          PATCH_IF FILE_EXISTS_IN_GAME ~%resref%.cre~ BEGIN
            SET "exists" = 1
          END
          PATCH_IF (("%exists%" = 0) AND (("%flags%" BAND 0b00000001) = 0b00000001)) BEGIN
            PATCH_PRINT "%SOURCE_FILE% issue: Actor #%index% does not exist (%resref%.cre)! Removing actor..."
            LPF fj_are_structure
              INT_VAR fj_delete_mode    = "%index%"
              STR_VAR fj_structure_type = actor
            END
            SET "index" = 0 - 1
            READ_SHORT 0x58 "actor_num"
          END
        END
      END
    END

    // Actor Miscellaneous Patching
    READ_LONG  0x54 "actor_off" ELSE 0
    READ_SHORT 0x58 "actor_num" ELSE 0

    PATCH_IF (actor_off > 0x11b) THEN BEGIN
      FOR (index = 0 ; index < actor_num ; index = index + 1) BEGIN

        // Invalid Embedded CRE Status Patching
        READ_BYTE  ("%actor_off%" + 0x28 + (0x110 * "%index%")) "flags"  // flags first byte
        READ_ASCII ("%actor_off%" + 0x80 + (0x110 * "%index%")) "resref" // cre file
        PATCH_IF NOT (("%resref%" STRING_EQUAL_CASE "NONE") OR ("%resref%" STRING_EQUAL_CASE "")) BEGIN
          PATCH_IF (("%flags%" BAND 0b00000001) = 0b00000000) BEGIN
            PATCH_PRINT "%SOURCE_FILE% issue: Actor #%index% (%resref%.CRE) exists and should not be attached to the ARE..."
            WRITE_BYTE ("%actor_off%" + 0x28 + (0x110 * "%index%")) ("%flags%" BOR 0b00000001)
          END
        END

        // Actor Removal Prevention
        READ_LONG  ("%actor_off%" + 0x38 + (0x110 * "%index%")) "removal_timer"
        PATCH_IF ("%removal_timer%" = 0) BEGIN
          PATCH_PRINT "%SOURCE_FILE% issue: Actor #%index% set to disappear after %removal_timer% seconds! Setting to -1..."
          WRITE_LONG ("%actor_off%" + 0x38 + (0x110 * "%index%")) 0xffffffff
        END ELSE
        PATCH_IF ("%removal_timer%" != 0xffffffff) BEGIN
          PATCH_PRINT "%SOURCE_FILE% issue: Actor #%index% set to disappear after %removal_timer% seconds!"
//        WRITE_LONG ("%actor_off%" + 0x38 + (0x110 * "%index%")) 0xffffffff
        END
      END
    END

    // Missing Spawn Remover
    READ_LONG  0x60 "spawn_off" ELSE 0
    READ_LONG  0x64 "spawn_num" ELSE 0

    PATCH_IF (spawn_off > 0x11b) THEN BEGIN
      FOR (index = 0 ; index < spawn_num ; index = index + 1) BEGIN
        FOR (index2 = 0 ; index2 < 10 ; index2 = index2 + 1) BEGIN
          READ_ASCII ("%spawn_off%" + 0x24 + ("%index2%" * 0x08) + ("%index%" * 0xc8)) "resref"
          PATCH_IF (("%resref%" STRING_COMPARE_CASE "") AND                  // ignore empty entries
                    ("%resref%" STRING_COMPARE_CASE "none") AND              // ignore 'none'
                    ("%resref%" STRING_COMPARE_REGEXP "^[Rr][Dd].+$")) BEGIN // ignore creatures starting with RD
            SET "exists" = 0
            PATCH_IF FILE_EXISTS_IN_GAME ~%resref%.cre~ BEGIN
              SET "exists" = 1
            END
            PATCH_IF ("%exists%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Spawn Point #%index% creature file does not exist (%resref%.cre)! Nulling..."
              WRITE_ASCII ("%spawn_off%" + 0x24 + ("%index2%" * 0x08) + ("%index%" * 0xc8)) ~NONE~ #8
            END
          END
        END
        SET "none_count" = 0
        FOR (index2 = 0 ; index2 < 10 ; index2 = index2 + 1) BEGIN
          READ_ASCII ("%spawn_off%" + 0x24 + ("%index2%" * 0x08) + ("%index%" * 0xc8)) "resref"
          PATCH_IF (("%resref%" STRING_EQUAL_CASE "") OR ("%resref%" STRING_EQUAL_CASE "none")) BEGIN
            SET "none_count" = "none_count" + 1
          END
        END
        PATCH_IF ("%none_count%" = 10) BEGIN
          PATCH_PRINT "%SOURCE_FILE% issue: Spawn Point #%index% has no creatures to spawn! Removing spawn entry..."
          LPF fj_are_structure
            INT_VAR fj_delete_mode    = "%index%"
            STR_VAR fj_structure_type = spawn
          END
          SET "index" = 0 - 1
          READ_LONG  0x64 "spawn_num"
        END
      END
    END

    // Missing Rest Spawn Remover
    READ_LONG  0xc0 "rest_off" ELSE 0

    PATCH_IF (rest_off > 0x11b) THEN BEGIN
      FOR (index = 0 ; index < 10 ; index = index + 1) BEGIN
        READ_ASCII ("%rest_off%" + 0x48 + ("%index%" * 0x08)) "resref"
        PATCH_IF (("%resref%" STRING_COMPARE_CASE "") AND                  // ignore empty entries
                  ("%resref%" STRING_COMPARE_CASE "none") AND              // ignore 'none'
                  ("%resref%" STRING_COMPARE_REGEXP "^[Rr][Dd].+$")) BEGIN // ignore creatures starting with RD
          SET "exists" = 0
          PATCH_IF FILE_EXISTS_IN_GAME ~%resref%.cre~ BEGIN
            SET "exists" = 1
          END
          PATCH_IF ("%exists%" = 0) BEGIN
            PATCH_PRINT "%SOURCE_FILE% issue: Rest Spawn creature file does not exist (%resref%.cre)! Nulling..."
            WRITE_ASCII ("%rest_off%" + 0x48 + ("%index%" * 0x08)) "NONE" #8
          END
        END
      END
      SET "none_count" = 0
      FOR (index = 0 ; index < 10 ; index = index + 1) BEGIN
        READ_ASCII ("%rest_off%" + 0x48 + ("%index%" * 0x08)) "resref"
        PATCH_IF (("%resref%" STRING_EQUAL_CASE "") OR ("%resref%" STRING_EQUAL_CASE "none")) BEGIN
          SET "none_count" = "none_count" + 1
        END
      END
      READ_SHORT ("%rest_off%" + 0x98) "spawncount"
      PATCH_IF (("%none_count%" = 10) AND ("%spawncount%" != 0)) BEGIN
        PATCH_PRINT "%SOURCE_FILE% issue: No creatures in Rest Spawn Entry but area still set to spawn something! Setting number of spawnable creatures to 0..."
        WRITE_SHORT ("%rest_off%" + 0x98) 0
      END
    END
  END
BUT_ONLY_IF_IT_CHANGES

BEGIN ~Creature Stuff - Creature Resource Fixer~
  GROUP ~Creature Stuff~

OUTER_SET existence_fix = 0
ACTION_IF (FILE_EXISTS "TobEx_ini/TobExCore.ini") AND (FILE_CONTAINS "TobEx_ini/TobExCore.ini" "Existence Sound Fix=1") THEN BEGIN
  PRINT "ToBEx Existence Sound Fix is set! Shifting creature EXISTANCE sounds where applicable..."
  PRINT "Setting Existance Delay Override to BG1 values for all non-joinable creatures..."
  OUTER_SET existence_fix = 1
  COPY_EXISTING ~PDIALOG.2DA~ ~override~
    PRETTY_PRINT_2DA
  BUT_ONLY_IF_IT_CHANGES
END

COPY_EXISTING_REGEXP GLOB ~^.+\.cre$~ ~override~
  LPF FJ_CRE_VALIDITY
    INT_VAR do_message = 1
  END

  PATCH_IF (existence_fix == 1) THEN BEGIN
    READ_ASCII 0x280 "DV" (32) NULL

    PATCH_IF (NOT FILE_CONTAINS_EVALUATED (~PDIALOG.2DA~ ~^%DV% ~)) BEGIN
      READ_LONG  0x1b8 ~soundreplace~
      READ_LONG  0x1bc ~soundreplace2~
      READ_LONG  0x1c0 ~soundreplace3~
      READ_LONG  0x1c4 ~soundreplace4~
      READ_LONG  0x1c8 ~soundreplace5~

      LPF ADD_CRE_EFFECT
        INT_VAR
           opcode     = 294
           target     = 1
           timing     = 9
           parameter1 = 500
      END

      PATCH_IF ("%soundreplace%" > 0) THEN BEGIN
        PATCH_PRINT "%SOURCE_FILE% issue: Soundset moved : PICKED_POCKET -> EXISTANCE1"
        WRITE_LONG 0x1b8 ~-1~
        WRITE_LONG 0x1bc %soundreplace%
      END
      PATCH_IF ("%soundreplace2%" > 0) THEN BEGIN
        PATCH_PRINT "%SOURCE_FILE% issue: Soundset moved : EXISTANCE1 -> EXISTANCE2"
        WRITE_LONG 0x1bc %soundreplace%
        WRITE_LONG 0x1c0 %soundreplace2%
      END
      PATCH_IF ("%soundreplace3%" > 0) THEN BEGIN
        PATCH_PRINT "%SOURCE_FILE% issue: Soundset moved : EXISTANCE2 -> EXISTANCE3"
        WRITE_LONG 0x1c0 %soundreplace2%
        WRITE_LONG 0x1c4 %soundreplace3%
      END
      PATCH_IF ("%soundreplace4%" > 0) THEN BEGIN
        PATCH_PRINT "%SOURCE_FILE% issue: Soundset moved : EXISTANCE3 -> EXISTANCE4"
        WRITE_LONG 0x1c4 %soundreplace3%
        WRITE_LONG 0x1c8 %soundreplace4%
      END
      PATCH_IF ("%soundreplace5%" > 0) THEN BEGIN
        PATCH_PRINT "%SOURCE_FILE% issue: Soundset moved : EXISTANCE4 -> EXISTANCE5"
        WRITE_LONG 0x1c8 %soundreplace4%
        WRITE_LONG 0x1cc %soundreplace5%
      END
    END
  END

  PATCH_IF ("%SOURCE_RES%" STRING_EQUAL_CASE "SENDRO03") AND (SOURCE_SIZE > 0x2d3) THEN BEGIN
    READ_LONG  0x2a8 "meminfo_off" ELSE 0
    READ_LONG  0x2ac "meminfo_num" ELSE 0
    READ_LONG  0x2b0 "mem_spl_off" ELSE 0
    READ_LONG  0x2b4 "mem_spl_num" ELSE 0

    PATCH_IF ("meminfo_off" > 0x2d3) AND ("mem_spl_off" > 0x2d3) THEN BEGIN
      READ_SHORT ("%meminfo_off%" + 0x04 + (0x10 * ("%meminfo_num%" - 1))) "spl_mem_num"
      READ_SHORT ("%meminfo_off%" + 0x08 + (0x10 * ("%meminfo_num%" - 1))) "spl_tbl_idx"
      READ_SHORT ("%meminfo_off%" + 0x0c + (0x10 * ("%meminfo_num%" - 1))) "spl_tbl_cnt"
      READ_ASCII ("%mem_spl_off%" +        (0x0c * ("%spl_tbl_idx%" + "%spl_tbl_cnt%"))) "resref1" ELSE 0
      READ_ASCII ("%mem_spl_off%" +        (0x0c * ("%spl_tbl_idx%" + "%spl_tbl_cnt%" + 1))) "resref2" ELSE 0
      READ_ASCII ("%mem_spl_off%" +        (0x0c * ("%spl_tbl_idx%" + "%spl_tbl_cnt%" + 2))) "resref3" ELSE 0
      READ_ASCII ("%mem_spl_off%" +        (0x0c * ("%spl_tbl_idx%" + "%spl_tbl_cnt%" + 3))) "resref4" ELSE 0
      PATCH_IF ("%resref1%" STRING_EQUAL_CASE "SPCL900") BEGIN
        PATCH_PRINT "%SOURCE_FILE% issue: Known ERROR in SENDRO03.CRE! (%resref1%, %resref2%, %resref3%, %resref4%) unaccounted for! Fixing..."
        WRITE_SHORT ("%meminfo_off%" + 0x04 + (0x10 * ("%meminfo_num%" - 1))) 6
        WRITE_SHORT ("%meminfo_off%" + 0x0c + (0x10 * ("%meminfo_num%" - 1))) 6
      END
    END
  END
BUT_ONLY_IF_IT_CHANGES

BEGIN ~Creature Stuff - Inventory Unborker (removes spurious references to the item table)~
  GROUP ~Creature Stuff~

COPY_EXISTING_REGEXP GLOB ~^.+\.cre$~ ~override~
  PATCH_IF (SOURCE_SIZE > 0x2d3) THEN BEGIN
    READ_LONG  0x2b8 "slot_off" ELSE 0
    READ_LONG  0x2bc "itm_off" ELSE 0
    READ_SHORT 0x2c0 "itm_num" ELSE 0

    PATCH_IF ("slot_off" > 0x2d3) THEN BEGIN
      FOR (index = 0 ; index < 37 ; index = index + 1) BEGIN
        READ_SHORT ("%slot_off%" + (0x02 * "%index%")) "ref" ELSE 0xffff
        PATCH_IF ("%ref%" != 0xffff) BEGIN
          PATCH_IF ((%ref% + 1) > %itm_num%) BEGIN
            PATCH_PRINT "%SOURCE_FILE% issue: Inventory Item Slot #%index% refers to an item outside the Item Table! Setting to -1..."
            WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
          END
        END
      END
    END
    PATCH_IF (("slot_off" > 0x2d3) AND ("itm_off" > 0x2d3)) THEN BEGIN
      FOR (index = 0 ; index < itm_num ; index = index + 1) BEGIN
        SET "critical" = 0
        SET "found" = 0
        SET "foundslot" = 0
        FOR (index1 = 0 ; index1 < 37 && found = 0 ; index1 = index1 + 1) BEGIN
          READ_SHORT ("%slot_off%" + (0x02 * "%index1%")) "ref" ELSE 0xffff
          PATCH_IF (%index% = %ref%) BEGIN
            SET "found" = 1
          END
        END
        PATCH_IF (%found% = 0) BEGIN
          READ_ASCII ("%itm_off%" + (0x14 * "%index%")) "item"
          PATCH_IF FILE_EXISTS_IN_GAME ~%item%.itm~ BEGIN
            INNER_PATCH_FILE ~%item%.itm~ BEGIN
              READ_BYTE  0x18 "flags"
              READ_SHORT 0x1c "type"
              READ_LONG  0x34 "price"
              PATCH_IF ("%item%" STRING_COMPARE_REGEXP "^[Rr][Nn][Dd].+$") AND ((("%flags%" BAND 0b00000001) = 0b00000001) OR ((%type% = 0) AND (%price% = 0))) BEGIN
                SET "critical" = 1
              END
            END
          END
          PATCH_IF %critical% = 1 BEGIN
            PATCH_PRINT "%SOURCE_FILE% issue: Inventory Item #%index% (%item%.ITM) is not actually in inventory! Potentially quest-breaking item! Removing..."
            REMOVE_CRE_ITEM ~%item%~
            SET "index" = 0 - 1
            READ_LONG  0x2b8 "slot_off" ELSE 0
            READ_LONG  0x2bc "itm_off" ELSE 0
            READ_SHORT 0x2c0 "itm_num" ELSE 0
          END ELSE
          BEGIN
            FOR (index1 = 21 ; index1 < 37 && foundslot = 0 ; index1 = index1 + 1) BEGIN
              READ_SHORT ("%slot_off%" + (0x02 * "%index1%")) "ref" ELSE 0xffff
              PATCH_IF ("%ref%" = 0xffff) BEGIN
                SET "foundslot" = "index1"
              END
            END
            PATCH_IF ("%foundslot%" > 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Inventory Item #%index% (%item%.ITM) is not actually in inventory! Adding to Inventory Slot #%foundslot%..."
              WRITE_SHORT ("%slot_off%" + (0x02 * "%foundslot%")) "index"
            END ELSE
            BEGIN
              FOR (index1 = 0 ; index1 < 22 && foundslot = 0 ; index1 = index1 + 1) BEGIN
                READ_SHORT ("%slot_off%" + (0x02 * "%index1%")) "ref" ELSE 0xffff
                PATCH_IF ("%ref%" = 0xffff) BEGIN
                  SET "foundslot" = "index1"
                END
              END
              PATCH_PRINT "%SOURCE_FILE% issue: Inventory Item #%index% (%item%.ITM) is not actually in inventory! No free inventory slot found! Adding to Slot #%foundslot%..."
              WRITE_SHORT ("%slot_off%" + (0x02 * "%foundslot%")) "index"
            END
          END
        END
      END
    END
  END
BUT_ONLY_IF_IT_CHANGES

BEGIN ~Creature Stuff - Inventory Cleaner (removes items that don't exist and converts them into already present items where applicable)~
  GROUP ~Creature Stuff~

COPY_EXISTING_REGEXP GLOB ~^.+\.cre$~ ~override~
  PATCH_IF ((SOURCE_SIZE > 0x2d3)) THEN BEGIN
    READ_LONG  0x2bc "itm_off" ELSE 0
    READ_LONG  0x2c0 "itm_num" ELSE 0

    PATCH_IF ("itm_off" > 0x2d3) THEN BEGIN
      FOR (index = 0 ; index < itm_num ; index = index + 1) BEGIN
        READ_ASCII ("%itm_off%" + (0x14 * "%index%")) "item"
        PATCH_IF (NOT (FILE_EXISTS_IN_GAME ~%item%.itm~) AND ("%item%" STRING_COMPARE_REGEXP "^[Rr][Nn][Dd].+$")) BEGIN
          SET "converted" = 0
          PHP_EACH tutu_convert_itm AS tutu_itm => bgt_itm BEGIN
            PATCH_IF ("%item%" STRING_EQUAL_CASE "%tutu_itm%") AND (FILE_EXISTS_IN_GAME "%bgt_itm%.itm") BEGIN
              WRITE_ASCIIE ("%itm_off%" + (0x14 * "%index%")) "%bgt_itm%" #8
              SET "converted" = 1
              PATCH_PRINT "%SOURCE_FILE% issue: Replacing %tutu_itm%.itm with %bgt_itm%.itm..."
            END
          END
          PATCH_IF ("%converted%" = 0) BEGIN
            PATCH_PRINT "%SOURCE_FILE% issue: Inventory Item #%index% (%item%.itm) does not exist! Removing item..."
            REMOVE_CRE_ITEM ~%item%~
            SET "index" = 0 - 1
            READ_LONG  0x2c0 "itm_num"
          END
        END
      END
    END
  END
BUT_ONLY_IF_IT_CHANGES

BEGIN ~Creature Stuff - Inventory Overhauler (moves invalid but equippable items to inventory and equips anything in the inventory to a free slot)~
  GROUP ~Creature Stuff~

//G3 Debugging Suite - Equip invalid items
COPY_EXISTING_REGEXP GLOB ~^.+\.cre$~ ~override~
  PATCH_IF (SOURCE_SIZE > 0x2d3) BEGIN
    READ_LONG  0x2b8 "slot_off" ELSE 0
    READ_LONG  0x2bc "itm_off" ELSE 0

    // tracking variables
    SET "2hweapon" = 0
    SET "offhand" = 0
    SET "bow" = 0
    SET "arrows" = 0
    SET "xbow" = 0
    SET "bolts" = 0
    SET "sling" = 0
    SET "bullets" = 0
    SET "helm" = 0
    SET "armor" = 0
    SET "gloves" = 0
    SET "lring" = 0
    SET "rring" = 0
    SET "boots" = 0
    SET "belt" = 0
    SET "amulet" = 0
    SET "weapon1" = 0
    SET "weapon2" = 0
    SET "weapon3" = 0
    SET "weapon4" = 0
    SET "quiver1" = 0
    SET "quiver2" = 0
    SET "quiver3" = 0
    SET "quiver4" = 0
    SET "cloak" = 0
    SET "2handedequip" = 0
    PATCH_IF (("slot_off" > 0x2d3) AND ("itm_off" > 0x2d3)) BEGIN
      FOR (index = 0 ; index < 36 ; index = index + 1) BEGIN
        READ_SHORT ("%slot_off%" + (0x02 * "%index%")) "ref"
        PATCH_IF ("%ref%" != 0xffff) BEGIN
          READ_ASCII ("%itm_off%" +        (0x14 * "%ref%")) "item"
          READ_BYTE  ("%itm_off%" + 0x10 + (0x14 * "%ref%")) "cre_flags"
          PATCH_IF FILE_EXISTS_IN_GAME ~%item%.itm~ THEN BEGIN
            INNER_PATCH_FILE ~%item%.itm~ BEGIN
              READ_SHORT 0x1c "type"
              READ_BYTE  0x18 "flags"
            END
            PATCH_IF ("%index%" = 0) BEGIN // helm slot
              SET "helm" = 1
              PATCH_IF (("%type%" != 7) AND (("%flags%" BAND 0b00010100) = 0b00000100)) AND (("%cre_flags%" BAND 0b00001000) = 0b00000000) BEGIN // helm
                SET "ref2" = 1
                FOR (index3 = 21 ; ref2 != 0xffff && index3 < 37 ; index3 = index3 + 1) BEGIN
                  READ_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref2"
                END
                SET "index3" = "index3" - 1
                PATCH_IF ("%ref2%" = 0xffff) BEGIN
                  SET "slot" = "index3" - 20
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid HELM: %item%.itm! Moving to Inventory Slot #%slot%~
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref"
                  WRITE_BYTE  ("%itm_off%" + 0x10 + (0x14 * "%ref%")) ("%cre_flags%" BOR 0b00000010)
                  SET "helm" = 0
                  SET "index" = 0 - 1
                END ELSE
                BEGIN
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid HELM: %item%.itm! Cannot move because Inventory full!~
                END
              END
            END
            PATCH_IF ("%index%" = 1) BEGIN // armor slot
              SET "armor" = 1
              PATCH_IF (("%type%" != 2) AND (("%flags%" BAND 0b00010100) = 0b00000100)) AND (("%cre_flags%" BAND 0b00001000) = 0b00000000) BEGIN // armor
                SET "ref2" = 1
                FOR (index3 = 21 ; ref2 != 0xffff && index3 < 37 ; index3 = index3 + 1) BEGIN
                  READ_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref2"
                END
                SET "index3" = "index3" - 1
                PATCH_IF ("%ref2%" = 0xffff) BEGIN
                  SET "slot" = "index3" - 20
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid ARMOR: %item%.itm! Moving to Inventory Slot #%slot%~
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref"
                  WRITE_BYTE  ("%itm_off%" + 0x10 + (0x14 * "%ref%")) ("%cre_flags%" BOR 0b00000010)
                  SET "armor" = 0
                  SET "index" = 0 - 1
                END ELSE
                BEGIN
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid ARMOR: %item%.itm! Cannot move because Inventory full!~
                END
              END
            END
            PATCH_IF ("%index%" = 2) BEGIN // shield slot
              SET "offhand" = 1
              PATCH_IF (("%flags%" BAND 0b00010110) = 0b00000110) AND (("%cre_flags%" BAND 0b00001000) = 0b00000000) BEGIN // 2h, uncursed and droppable check
                SET "ref2" = 1
                FOR (index3 = 21 ; ref2 != 0xffff && index3 < 37 ; index3 = index3 + 1) BEGIN
                  READ_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref2"
                END
                SET "index3" = "index3" - 1
                PATCH_IF ("%ref2%" = 0xffff) BEGIN
                  SET "slot" = "index3" - 20
                  PATCH_PRINT ~%SOURCE_FILE% issue: 2-HANDED OFFHAND: %item%.itm! Moving to Inventory Slot #%slot%~
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref"
                  WRITE_BYTE  ("%itm_off%" + 0x10 + (0x14 * "%ref%")) ("%cre_flags%" BOR 0b00000010)
                  SET "offhand" = 0
                  SET "index" = 0 - 1
                END ELSE
                BEGIN
                  PATCH_PRINT ~%SOURCE_FILE% issue: 2-HANDED OFFHAND: %item%.itm! Cannot move because Inventory full!~
                END
              END ELSE
              PATCH_IF (("%type%" != 12) AND (("%flags%" BAND 0b00010100) = 0b00000100)) AND (("%cre_flags%" BAND 0b00001000) = 0b00000000) BEGIN // if not shield, check for valid melee abilities
                SET "legit_offhand" = 0
                INNER_PATCH_FILE ~%item%.itm~ BEGIN
                  READ_LONG  0x64 "abil_off"
                  READ_SHORT 0x68 "abil_num"
                  FOR (index3 = 0 ; index3 < abil_num ; index3 = index3 + 1) BEGIN
                    READ_BYTE ("%abil_off%" + (0x38 * "%index3%")) "ability"
                    PATCH_IF ("%ability%" = 1) AND (%type% > 14) AND (%type% < 31) BEGIN // only melee weapons allowed in offhand
                      SET "legit_offhand" = 1
                    END
                  END
                END
                PATCH_IF ("%legit_offhand%" = 0) BEGIN
                  SET "ref2" = 1
                  FOR (index3 = 21 ; ref2 != 0xffff && index3 < 37 ; index3 = index3 + 1) BEGIN
                    READ_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref2"
                  END
                  SET "index3" = "index3" - 1
                  PATCH_IF ("%ref2%" = 0xffff) BEGIN
                    SET "slot" = "index3" - 20
                    PATCH_PRINT ~%SOURCE_FILE% issue: Invalid OFFHAND: %item%.itm! Moving to Inventory Slot #%slot%~
                    WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
                    WRITE_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref"
                    WRITE_BYTE  ("%itm_off%" + 0x10 + (0x14 * "%ref%")) ("%cre_flags%" BOR 0b00000010)
                    SET "offhand" = 0
                    SET "index" = 0 - 1
                  END ELSE
                  BEGIN
                    PATCH_PRINT ~%SOURCE_FILE% issue: Invalid OFFHAND: %item%.itm! Cannot move because Inventory full!~
                  END
                END
              END ELSE
              PATCH_IF ("%type%" = 12) AND (("%flags%" BAND 0b00010100) = 0b00000100) BEGIN
                READ_SHORT ("%slot_off%" + (0x02 * 9))  "refa"
                READ_SHORT ("%slot_off%" + (0x02 * 10)) "refb"
                READ_SHORT ("%slot_off%" + (0x02 * 11)) "refc"
                READ_SHORT ("%slot_off%" + (0x02 * 12)) "refd"
                PATCH_IF ("%refa%" != 0xffff) BEGIN
                  READ_ASCII ("%itm_off%" + (0x14 * "%refa%")) "item1"
                  PATCH_IF FILE_EXISTS_IN_GAME ~%item1%.itm~ THEN BEGIN
                    INNER_PATCH_FILE ~%item1%.itm~ BEGIN
                      READ_SHORT 0x1c "type1"
                      READ_BYTE  0x18 "flags1"
                      PATCH_IF (("%flags1%" BAND 0b00000010) = 0b00000010) AND ((%type1% > 14) AND (%type1% < 31)) THEN BEGIN
                        SET "2handedequip" = 1
                      END
                    END
                  END
                END
                PATCH_IF ("%refb%" != 0xffff) BEGIN
                  READ_ASCII ("%itm_off%" + (0x14 * "%refb%")) "item2"
                  PATCH_IF FILE_EXISTS_IN_GAME ~%item2%.itm~ THEN BEGIN
                    INNER_PATCH_FILE ~%item2%.itm~ BEGIN
                      READ_SHORT 0x1c "type2"
                      READ_BYTE  0x18 "flags2"
                      PATCH_IF (("%flags2%" BAND 0b00000010) = 0b00000010) AND ((%type2% > 14) AND (%type2% < 31)) THEN BEGIN
                        SET "2handedequip" = 1
                      END
                    END
                  END
                END
                PATCH_IF ("%refc%" != 0xffff) BEGIN
                  READ_ASCII ("%itm_off%" + (0x14 * "%refc%")) "item3"
                  PATCH_IF FILE_EXISTS_IN_GAME ~%item3%.itm~ THEN BEGIN
                    INNER_PATCH_FILE ~%item3%.itm~ BEGIN
                      READ_SHORT 0x1c "type3"
                      READ_BYTE  0x18 "flags3"
                      PATCH_IF (("%flags3%" BAND 0b00000010) = 0b00000010) AND ((%type3% > 14) AND (%type3% < 31)) THEN BEGIN
                        SET "2handedequip" = 1
                      END
                    END
                  END
                END
                PATCH_IF ("%refd%" != 0xffff) BEGIN
                  READ_ASCII ("%itm_off%" + (0x14 * "%refd%")) "item4"
                  PATCH_IF FILE_EXISTS_IN_GAME ~%item4%.itm~ THEN BEGIN
                    INNER_PATCH_FILE ~%item4%.itm~ BEGIN
                      READ_SHORT 0x1c "type4"
                      READ_BYTE  0x18 "flags4"
                      PATCH_IF (("%flags4%" BAND 0b00000010) = 0b00000010) AND ((%type4% > 14) AND (%type4% < 31)) THEN BEGIN
                        SET "2handedequip" = 1
                      END
                    END
                  END
                END
                PATCH_IF ("%2handedequip%" = 1) BEGIN
                  SET "ref2" = 1
                  FOR (index3 = 21 ; ref2 != 0xffff && index3 < 37 ; index3 = index3 + 1) BEGIN
                    READ_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref2"
                  END
                  SET "index3" = "index3" - 1
                  PATCH_IF ("%ref2%" = 0xffff) BEGIN
                    SET "slot" = "index3" - 20
                    PATCH_PRINT ~%SOURCE_FILE% issue: OFFHAND with 2-HAND: %item%.itm! Moving to Inventory Slot #%slot%~
                    WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
                    WRITE_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref"
                    WRITE_BYTE  ("%itm_off%" + 0x10 + (0x14 * "%ref%")) ("%cre_flags%" BOR 0b00000010)
                    SET "offhand" = 0
                    SET "index" = 0 - 1
                  END ELSE
                  BEGIN
                    PATCH_PRINT ~%SOURCE_FILE% issue: OFFHAND with 2-HAND: %item%.itm! Cannot move because Inventory full!~
                  END
                END
              END
            END
            PATCH_IF ("%index%" = 3) BEGIN // glove slot
              SET "gloves" = 1
              PATCH_IF (("%type%" != 6) AND (("%flags%" BAND 0b00010100) = 0b00000100)) AND (("%cre_flags%" BAND 0b00001000) = 0b00000000) BEGIN // gloves
                SET "ref2" = 1
                FOR (index3 = 21 ; ref2 != 0xffff && index3 < 37 ; index3 = index3 + 1) BEGIN
                  READ_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref2"
                END
                SET "index3" = "index3" - 1
                PATCH_IF ("%ref2%" = 0xffff) BEGIN
                  SET "slot" = "index3" - 20
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid GLOVES: %item%.itm! Moving to Inventory Slot #%slot%~
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref"
                  WRITE_BYTE  ("%itm_off%" + 0x10 + (0x14 * "%ref%")) ("%cre_flags%" BOR 0b00000010)
                  SET "gloves" = 0
                  SET "index" = 0 - 1
                END ELSE
                BEGIN
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid GLOVES: %item%.itm! Cannot move because Inventory full!~
                END
              END
            END
            PATCH_IF ("%index%" = 4) BEGIN // left ring slot
              SET "lring" = 1
              PATCH_IF (("%type%" != 10) AND (("%flags%" BAND 0b00010100) = 0b00000100)) AND (("%cre_flags%" BAND 0b00001000) = 0b00000000) BEGIN // rings
                SET "ref2" = 1
                FOR (index3 = 21 ; ref2 != 0xffff && index3 < 37 ; index3 = index3 + 1) BEGIN
                  READ_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref2"
                END
                SET "index3" = "index3" - 1
                PATCH_IF ("%ref2%" = 0xffff) BEGIN
                  SET "slot" = "index3" - 20
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid LRING: %item%.itm! Moving to Inventory Slot #%slot%~
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref"
                  WRITE_BYTE  ("%itm_off%" + 0x10 + (0x14 * "%ref%")) ("%cre_flags%" BOR 0b00000010)
                  SET "lring" = 0
                  SET "index" = 0 - 1
                END ELSE
                BEGIN
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid LRING: %item%.itm! Cannot move because Inventory full!~
                END
              END
            END
            PATCH_IF ("%index%" = 5) BEGIN // right ring slot
              SET "rring" = 1
              PATCH_IF (("%type%" != 10) AND (("%flags%" BAND 0b00010100) = 0b00000100)) AND (("%cre_flags%" BAND 0b00001000) = 0b00000000) BEGIN // rings
                SET "ref2" = 1
                FOR (index3 = 21 ; ref2 != 0xffff && index3 < 37 ; index3 = index3 + 1) BEGIN
                  READ_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref2"
                END
                SET "index3" = "index3" - 1
                PATCH_IF ("%ref2%" = 0xffff) BEGIN
                  SET "slot" = "index3" - 20
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid RRING: %item%.itm! Moving to Inventory Slot #%slot%~
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref"
                  WRITE_BYTE  ("%itm_off%" + 0x10 + (0x14 * "%ref%")) ("%cre_flags%" BOR 0b00000010)
                  SET "rring" = 0
                  SET "index" = 0 - 1
                END ELSE
                BEGIN
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid RRING: %item%.itm! Cannot move because Inventory full!~
                END
              END
            END
            PATCH_IF ("%index%" = 6) BEGIN // amulet slot
              SET "amulet" = 1
              PATCH_IF (("%type%" != 1) AND (("%flags%" BAND 0b00010100) = 0b00000100)) AND (("%cre_flags%" BAND 0b00001000) = 0b00000000) BEGIN // amulet
                SET "ref2" = 1
                FOR (index3 = 21 ; ref2 != 0xffff && index3 < 37 ; index3 = index3 + 1) BEGIN
                  READ_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref2"
                END
                SET "index3" = "index3" - 1
                PATCH_IF ("%ref2%" = 0xffff) BEGIN
                  SET "slot" = "index3" - 20
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid AMULET: %item%.itm! Moving to Inventory Slot #%slot%~
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref"
                  WRITE_BYTE  ("%itm_off%" + 0x10 + (0x14 * "%ref%")) ("%cre_flags%" BOR 0b00000010)
                  SET "amulet" = 0
                  SET "index" = 0 - 1
                END ELSE
                BEGIN
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid AMULET: %item%.itm! Cannot move because Inventory full!~
                END
              END
            END
            PATCH_IF ("%index%" = 7) BEGIN // belt slot
              SET "belt" = 1
              PATCH_IF (("%type%" != 3) AND (("%flags%" BAND 0b00010100) = 0b00000100)) AND (("%cre_flags%" BAND 0b00001000) = 0b00000000) BEGIN // belt
                SET "ref2" = 1
                FOR (index3 = 21 ; ref2 != 0xffff && index3 < 37 ; index3 = index3 + 1) BEGIN
                  READ_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref2"
                END
                SET "index3" = "index3" - 1
                PATCH_IF ("%ref2%" = 0xffff) BEGIN
                  SET "slot" = "index3" - 20
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid BELT: %item%.itm! Moving to Inventory Slot #%slot%~
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref"
                  WRITE_BYTE  ("%itm_off%" + 0x10 + (0x14 * "%ref%")) ("%cre_flags%" BOR 0b00000010)
                  SET "belt" = 0
                  SET "index" = 0 - 1
                END ELSE
                BEGIN
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid BELT: %item%.itm! Cannot move because Inventory full!~
                END
              END
            END
            PATCH_IF ("%index%" = 8) BEGIN // boot slot
              SET "boots" = 1
              PATCH_IF (("%type%" != 4) AND (("%flags%" BAND 0b00010100) = 0b00000100)) AND (("%cre_flags%" BAND 0b00001000) = 0b00000000) BEGIN // boot
                SET "ref2" = 1
                FOR (index3 = 21 ; ref2 != 0xffff && index3 < 37 ; index3 = index3 + 1) BEGIN
                  READ_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref2"
                END
                SET "index3" = "index3" - 1
                PATCH_IF ("%ref2%" = 0xffff) BEGIN
                  SET "slot" = "index3" - 20
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid BOOTS: %item%.itm! Moving to Inventory Slot #%slot%~
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref"
                  WRITE_BYTE  ("%itm_off%" + 0x10 + (0x14 * "%ref%")) ("%cre_flags%" BOR 0b00000010)
                  SET "boots" = 0
                  SET "index" = 0 - 1
                END ELSE
                BEGIN
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid BOOTS: %item%.itm! Cannot move because Inventory full!~
                END
              END
            END
            PATCH_IF ("%index%" = 9) BEGIN // first weapon slot
              SET "weapon1" = 1
              SET "legit_weapon" = 0
              INNER_PATCH_FILE ~%item%.itm~ BEGIN
                READ_BYTE  0x18 "flags"
                READ_LONG  0x64 "abil_off"
                READ_SHORT 0x68 "abil_num"
                FOR (index3 = 0 ; index3 < abil_num ; index3 = index3 + 1) BEGIN
                  READ_BYTE ("%abil_off%" + (0x38 * "%index3%")) "ability"
                  PATCH_IF (("%ability%" = 1) OR ("%ability%" = 2) OR ("%ability%" = 4)) AND ("%type%" > 14) AND ("%type%" < 31) BEGIN // melee, ranged, or launcher
                    SET "legit_weapon" = 1
                  END
                END
                PATCH_IF (("%legit_weapon%" = 1) AND (("%flags%" BAND 0b00000010) = 0b00000010)) BEGIN // 2h check
                    SET "2handedequip" = 1
                END
              END
              PATCH_IF (("%legit_weapon%" = 0) AND (("%flags%" BAND 0b00010100) = 0b00000100)) AND (("%cre_flags%" BAND 0b00001000) = 0b00000000) BEGIN
                SET "ref2" = 1
                FOR (index3 = 21 ; ref2 != 0xffff && index3 < 37 ; index3 = index3 + 1) BEGIN
                  READ_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref2"
                END
                SET "index3" = "index3" - 1
                PATCH_IF ("%ref2%" = 0xffff) BEGIN
                  SET "slot" = "index3" - 20
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid WEAPON1: %item%.itm! Moving to Inventory Slot #%slot%~
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref"
                  WRITE_BYTE  ("%itm_off%" + 0x10 + (0x14 * "%ref%")) ("%cre_flags%" BOR 0b00000010)
                  SET "weapon1" = 0
                  SET "index" = 0 - 1
                END ELSE
                BEGIN
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid WEAPON1: %item%.itm! Cannot move because Inventory full!~
                END
              END
            END
            PATCH_IF ("%index%" = 10) BEGIN // second weapon slot
              SET "weapon2" = 1
              SET "legit_weapon" = 0
              INNER_PATCH_FILE ~%item%.itm~ BEGIN
                READ_BYTE  0x18 "flags"
                READ_LONG  0x64 "abil_off"
                READ_SHORT 0x68 "abil_num"
                FOR (index3 = 0 ; index3 < abil_num ; index3 = index3 + 1) BEGIN
                  READ_BYTE ("%abil_off%" + (0x38 * "%index3%")) "ability"
                  PATCH_IF (("%ability%" = 1) OR ("%ability%" = 2) OR ("%ability%" = 4)) AND ("%type%" > 14) AND ("%type%" < 31) BEGIN // melee, ranged, or launcher
                    SET "legit_weapon" = 1
                  END
                END
                PATCH_IF (("%legit_weapon%" = 1) AND (("%flags%" BAND 0b00000010) = 0b00000010)) BEGIN // 2h check
                    SET "2handedequip" = 1
                END
              END
              PATCH_IF (("%legit_weapon%" = 0) AND (("%flags%" BAND 0b00010100) = 0b00000100)) AND (("%cre_flags%" BAND 0b00001000) = 0b00000000) BEGIN
                SET "ref2" = 1
                FOR (index3 = 21 ; ref2 != 0xffff && index3 < 37 ; index3 = index3 + 1) BEGIN
                  READ_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref2"
                END
                SET "index3" = "index3" - 1
                PATCH_IF ("%ref2%" = 0xffff) BEGIN
                  SET "slot" = "index3" - 20
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid WEAPON2: %item%.itm! Moving to Inventory Slot #%slot%~
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref"
                  WRITE_BYTE  ("%itm_off%" + 0x10 + (0x14 * "%ref%")) ("%cre_flags%" BOR 0b00000010)
                  SET "weapon2" = 0
                  SET "index" = 0 - 1
                END ELSE
                BEGIN
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid WEAPON2: %item%.itm! Cannot move because Inventory full!~
                END
              END
            END
            PATCH_IF ("%index%" = 11) BEGIN // third weapon slot
              SET "weapon3" = 1
              SET "legit_weapon" = 0
              INNER_PATCH_FILE ~%item%.itm~ BEGIN
                READ_BYTE  0x18 "flags"
                READ_LONG  0x64 "abil_off"
                READ_SHORT 0x68 "abil_num"
                FOR (index3 = 0 ; index3 < abil_num ; index3 = index3 + 1) BEGIN
                  READ_BYTE ("%abil_off%" + (0x38 * "%index3%")) "ability"
                  PATCH_IF (("%ability%" = 1) OR ("%ability%" = 2) OR ("%ability%" = 4)) AND ("%type%" > 14) AND ("%type%" < 31) BEGIN // melee, ranged, or launcher
                    SET "legit_weapon" = 1
                  END
                END
                PATCH_IF (("%legit_weapon%" = 1) AND (("%flags%" BAND 0b00000010) = 0b00000010)) BEGIN // 2h check
                    SET "2handedequip" = 1
                END
              END
              PATCH_IF (("%legit_weapon%" = 0) AND (("%flags%" BAND 0b00010100) = 0b00000100)) AND (("%cre_flags%" BAND 0b00001000) = 0b00000000) BEGIN
                SET "ref2" = 1
                FOR (index3 = 21 ; ref2 != 0xffff && index3 < 37 ; index3 = index3 + 1) BEGIN
                  READ_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref2"
                END
                SET "index3" = "index3" - 1
                PATCH_IF ("%ref2%" = 0xffff) BEGIN
                  SET "slot" = "index3" - 20
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid WEAPON3: %item%.itm! Moving to Inventory Slot #%slot%~
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref"
                  WRITE_BYTE  ("%itm_off%" + 0x10 + (0x14 * "%ref%")) ("%cre_flags%" BOR 0b00000010)
                  SET "weapon3" = 0
                  SET "index" = 0 - 1
                END ELSE
                BEGIN
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid WEAPON3: %item%.itm! Cannot move because Inventory full!~
                END
              END
            END
            PATCH_IF ("%index%" = 12) BEGIN // fourth weapon slot
              SET "weapon4" = 1
              SET "legit_weapon" = 0
              INNER_PATCH_FILE ~%item%.itm~ BEGIN
                READ_BYTE  0x18 "flags"
                READ_LONG  0x64 "abil_off"
                READ_SHORT 0x68 "abil_num"
                FOR (index3 = 0 ; index3 < abil_num ; index3 = index3 + 1) BEGIN
                  READ_BYTE ("%abil_off%" + (0x38 * "%index3%")) "ability"
                  PATCH_IF (("%ability%" = 1) OR ("%ability%" = 2) OR ("%ability%" = 4)) AND ("%type%" > 14) AND ("%type%" < 31) BEGIN // melee, ranged, or launcher
                    SET "legit_weapon" = 1
                  END
                END
                PATCH_IF (("%legit_weapon%" = 1) AND (("%flags%" BAND 0b00000010) = 0b00000010)) BEGIN // 2h check
                    SET "2handedequip" = 1
                END
              END
              PATCH_IF (("%legit_weapon%" = 0) AND (("%flags%" BAND 0b00010100) = 0b00000100)) AND (("%cre_flags%" BAND 0b00001000) = 0b00000000) BEGIN
                SET "ref2" = 1
                FOR (index3 = 21 ; ref2 != 0xffff && index3 < 37 ; index3 = index3 + 1) BEGIN
                  READ_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref2"
                END
                SET "index3" = "index3" - 1
                PATCH_IF ("%ref2%" = 0xffff) BEGIN
                  SET "slot" = "index3" - 20
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid WEAPON4: %item%.itm! Moving to Inventory Slot #%slot%~
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref"
                  WRITE_BYTE  ("%itm_off%" + 0x10 + (0x14 * "%ref%")) ("%cre_flags%" BOR 0b00000010)
                  SET "weapon4" = 0
                  SET "index" = 0 - 1
                END ELSE
                BEGIN
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid WEAPON4: %item%.itm! Cannot move because Inventory full!~
                END
              END
            END
            PATCH_IF ("%index%" = 13) BEGIN // first quiver slot
              SET "quiver1" = 1
              PATCH_IF (NOT (("%type%" = 5) OR ("%type%" = 31) OR ("%type%" = 14)) AND (("%flags%" BAND 0b00010100) = 0b00000100)) AND (("%cre_flags%" BAND 0b00001000) = 0b00000000) BEGIN
                SET "ref2" = 1
                FOR (index3 = 21 ; ref2 != 0xffff && index3 < 37 ; index3 = index3 + 1) BEGIN
                  READ_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref2"
                END
                SET "index3" = "index3" - 1
                PATCH_IF ("%ref2%" = 0xffff) BEGIN
                  SET "slot" = "index3" - 20
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid QUIVER1: %item%.itm! Moving to Inventory Slot #%slot%~
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref"
                  WRITE_BYTE  ("%itm_off%" + 0x10 + (0x14 * "%ref%")) ("%cre_flags%" BOR 0b00000010)
                  SET "quiver1" = 0
                  SET "index" = 0 - 1
                END ELSE
                BEGIN
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid QUIVER1: %item%.itm! Cannot move because Inventory full!~
                END
              END
            END
            PATCH_IF ("%index%" = 14) BEGIN // second quiver slot
              SET "quiver2" = 1
              PATCH_IF (NOT (("%type%" = 5) OR ("%type%" = 31) OR ("%type%" = 14)) AND (("%flags%" BAND 0b00010100) = 0b00000100)) AND (("%cre_flags%" BAND 0b00001000) = 0b00000000) BEGIN
                SET "ref2" = 1
                FOR (index3 = 21 ; ref2 != 0xffff && index3 < 37 ; index3 = index3 + 1) BEGIN
                  READ_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref2"
                END
                SET "index3" = "index3" - 1
                PATCH_IF ("%ref2%" = 0xffff) BEGIN
                  SET "slot" = "index3" - 20
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid QUIVER2: %item%.itm! Moving to Inventory Slot #%slot%~
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref"
                  WRITE_BYTE  ("%itm_off%" + 0x10 + (0x14 * "%ref%")) ("%cre_flags%" BOR 0b00000010)
                  SET "quiver2" = 0
                  SET "index" = 0 - 1
                END ELSE
                BEGIN
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid QUIVER2: %item%.itm! Cannot move because Inventory full!~
                END
              END
            END
            PATCH_IF ("%index%" = 15) BEGIN // third quiver slot
              SET "quiver3" = 1
              PATCH_IF (NOT (("%type%" = 5) OR ("%type%" = 31) OR ("%type%" = 14)) AND (("%flags%" BAND 0b00010100) = 0b00000100)) AND (("%cre_flags%" BAND 0b00001000) = 0b00000000) BEGIN
                SET "ref2" = 1
                FOR (index3 = 21 ; ref2 != 0xffff && index3 < 37 ; index3 = index3 + 1) BEGIN
                  READ_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref2"
                END
                SET "index3" = "index3" - 1
                PATCH_IF ("%ref2%" = 0xffff) BEGIN
                  SET "slot" = "index3" - 20
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid QUIVER3: %item%.itm! Moving to Inventory Slot #%slot%~
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref"
                  WRITE_BYTE  ("%itm_off%" + 0x10 + (0x14 * "%ref%")) ("%cre_flags%" BOR 0b00000010)
                  SET "quiver3" = 0
                  SET "index" = 0 - 1
                END ELSE
                BEGIN
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid QUIVER3: %item%.itm! Cannot move because Inventory full!~
                END
              END
            END
            PATCH_IF ("%index%" = 16) BEGIN // fourth quiver slot
              SET "quiver4" = 1
              PATCH_IF (NOT (("%type%" = 5) OR ("%type%" = 31) OR ("%type%" = 14)) AND (("%flags%" BAND 0b00010100) = 0b00000100)) AND (("%cre_flags%" BAND 0b00001000) = 0b00000000) BEGIN
                SET "ref2" = 1
                FOR (index3 = 21 ; ref2 != 0xffff && index3 < 37 ; index3 = index3 + 1) BEGIN
                  READ_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref2"
                END
                SET "index3" = "index3" - 1
                PATCH_IF ("%ref2%" = 0xffff) BEGIN
                  SET "slot" = "index3" - 20
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid QUIVER4: %item%.itm! Moving to Inventory Slot #%slot%~
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref"
                  WRITE_BYTE  ("%itm_off%" + 0x10 + (0x14 * "%ref%")) ("%cre_flags%" BOR 0b00000010)
                  SET "quiver4" = 0
                  SET "index" = 0 - 1
                END ELSE
                BEGIN
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid QUIVER4: %item%.itm! Cannot move because Inventory full!~
                END
              END
            END
            PATCH_IF ("%index%" = 17) BEGIN // cloak
              SET "cloak" = 1
              PATCH_IF (("%type%" != 32) AND (("%flags%" BAND 0b00010100) = 0b00000100)) AND (("%cre_flags%" BAND 0b00001000) = 0b00000000) BEGIN // cloak
                SET "ref2" = 1
                FOR (index3 = 21 ; ref2 != 0xffff && index3 < 37 ; index3 = index3 + 1) BEGIN
                  READ_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref2"
                END
                SET "index3" = "index3" - 1
                PATCH_IF ("%ref2%" = 0xffff) BEGIN
                  SET "slot" = "index3" - 20
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid CLOAK: %item%.itm! Moving to Inventory Slot #%slot%~
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
                  WRITE_SHORT ("%slot_off%" + (0x02 * "%index3%")) "ref"
                  WRITE_BYTE  ("%itm_off%" + 0x10 + (0x14 * "%ref%")) ("%cre_flags%" BOR 0b00000010)
                  SET "cloak" = 0
                  SET "index" = 0 - 1
                END ELSE
                BEGIN
                  PATCH_PRINT ~%SOURCE_FILE% issue: Invalid CLOAK: %item%.itm! Cannot move because Inventory full!~
                END
              END
            END
            PATCH_IF ((("%type%" =  2) AND ("%armor%" = 0)) AND (("%flags%" BAND 0b00010100) = 0b00000100)) AND ("%index%" > 17) BEGIN
              PATCH_PRINT ~%SOURCE_FILE% issue: Equippable items in inventory: %item%.itm! Equipping ARMOR~
              WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
              WRITE_SHORT ("%slot_off%" + (0x02))             "ref"
              SET "armor" = 1
            END ELSE
            PATCH_IF (((("%type%" =  5) OR
                        ("%type%" = 14) OR
                        ("%type%" = 31)) AND ("%quiver1%" = 0) AND (("%flags%" BAND 0b00010100) = 0b00000100)) AND ("%index%" > 17)) BEGIN
              PATCH_PRINT ~%SOURCE_FILE% issue: Equippable items in inventory: %item%.itm! Equipping QUIVER1~
              WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
              WRITE_SHORT ("%slot_off%" + (0x02 * 13))        "ref"
              SET "quiver1" = 1
            END ELSE
            PATCH_IF (((("%type%" =  5) OR
                        ("%type%" = 14) OR
                        ("%type%" = 31)) AND ("%quiver2%" = 0) AND (("%flags%" BAND 0b00010100) = 0b00000100)) AND ("%index%" > 17)) BEGIN
              PATCH_PRINT ~%SOURCE_FILE% issue: Equippable items in inventory: %item%.itm! Equipping QUIVER2~
              WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
              WRITE_SHORT ("%slot_off%" + (0x02 * 14))        "ref"
              WRITE_BYTE  ("%itm_off%" + 0x10 + (0x14 * "%ref%")) ("%cre_flags%" BOR 0b00000010)
              SET "quiver2" = 1
            END ELSE
            PATCH_IF (((("%type%" =  5) OR
                        ("%type%" = 14) OR
                        ("%type%" = 31)) AND ("%quiver3%" = 0) AND (("%flags%" BAND 0b00010100) = 0b00000100)) AND ("%index%" > 17)) BEGIN
              PATCH_PRINT ~%SOURCE_FILE% issue: Equippable items in inventory: %item%.itm! Equipping QUIVER3~
              WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
              WRITE_SHORT ("%slot_off%" + (0x02 * 15))        "ref"
              SET "quiver3" = 1
            END ELSE
            PATCH_IF (((("%type%" =  5) OR
                        ("%type%" = 14) OR
                        ("%type%" = 31)) AND ("%quiver4%" = 0)) AND (("%flags%" BAND 0b00010100) = 0b00000100) AND ("%index%" > 17)) BEGIN
              PATCH_PRINT ~%SOURCE_FILE% issue: Equippable items in inventory: %item%.itm! Equipping QUIVER4~
              WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
              WRITE_SHORT ("%slot_off%" + (0x02 * 16))        "ref"
              SET "quiver4" = 1
            END ELSE
            PATCH_IF ((("%type%" =  7) AND ("%helm%" = 0)) AND (("%flags%" BAND 0b00010100) = 0b00000100)) AND ("%index%" > 17) BEGIN
              PATCH_PRINT ~%SOURCE_FILE% issue: Equippable items in inventory: %item%.itm! Equipping HELM~
              WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
              WRITE_SHORT ("%slot_off%")                      "ref"
              SET "helm" = 1
            END ELSE
            PATCH_IF (((("%type%" = 15) OR // bow
                        ("%type%" = 16) OR // dagger
                        ("%type%" = 17) OR // maces
                        ("%type%" = 18) OR // sling
                        ("%type%" = 19) OR // short sword
                        ("%type%" = 20) OR // swords
                        ("%type%" = 21) OR // hammer
                        ("%type%" = 22) OR // morning stars
                        ("%type%" = 23) OR // flail
                        ("%type%" = 24) OR // darts
                        ("%type%" = 25) OR // axe
                        ("%type%" = 26) OR // staff
                        ("%type%" = 27) OR // xbow
                        ("%type%" = 28) OR // hand to hand
                        ("%type%" = 29) OR // Spear
                        ("%type%" = 30) OR // halberd
                        ("%type%" = 44)) AND ("%weapon1%" = 0) AND (("%flags%" BAND 0b00010100) = 0b00000100) AND ("%index%" > 17)))  BEGIN
              PATCH_IF ((("%flags%" BAND 0b00000010) != 0b00000010) AND ("%offhand%" = 0)) BEGIN
                PATCH_PRINT ~%SOURCE_FILE% issue: Equippable items in inventory: %item%.itm! Equipping WEAPON1~
                WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
                WRITE_SHORT ("%slot_off%" + (0x02 * 9))         "ref"
                SET "weapon1" = 1
              END
            END ELSE
            PATCH_IF (((("%type%" = 15) OR // bow
                        ("%type%" = 16) OR // dagger
                        ("%type%" = 17) OR // maces
                        ("%type%" = 18) OR // sling
                        ("%type%" = 19) OR // short sword
                        ("%type%" = 20) OR // swords
                        ("%type%" = 21) OR // hammer
                        ("%type%" = 22) OR // morning stars
                        ("%type%" = 23) OR // flail
                        ("%type%" = 24) OR // darts
                        ("%type%" = 25) OR // axe
                        ("%type%" = 26) OR // staff
                        ("%type%" = 27) OR // xbow
                        ("%type%" = 28) OR // hand to hand
                        ("%type%" = 29) OR // Spear
                        ("%type%" = 30) OR // halberd
                        ("%type%" = 44)) AND ("%weapon2%" = 0) AND (("%flags%" BAND 0b00010100) = 0b00000100) AND ("%index%" > 17)))  BEGIN
              PATCH_IF ((("%flags%" BAND 0b00000010) != 0b00000010) AND ("%offhand%" = 0)) BEGIN
                PATCH_PRINT ~%SOURCE_FILE% issue: Equippable items in inventory: %item%.itm! Equipping WEAPON2~
                WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
                WRITE_SHORT ("%slot_off%" + (0x02 * 10))        "ref"
                SET "weapon2" = 1
              END
            END ELSE
            PATCH_IF (((("%type%" = 15) OR // bow
                        ("%type%" = 16) OR // dagger
                        ("%type%" = 17) OR // maces
                        ("%type%" = 18) OR // sling
                        ("%type%" = 19) OR // short sword
                        ("%type%" = 20) OR // swords
                        ("%type%" = 21) OR // hammer
                        ("%type%" = 22) OR // morning stars
                        ("%type%" = 23) OR // flail
                        ("%type%" = 24) OR // darts
                        ("%type%" = 25) OR // axe
                        ("%type%" = 26) OR // staff
                        ("%type%" = 27) OR // xbow
                        ("%type%" = 28) OR // hand to hand
                        ("%type%" = 29) OR // Spear
                        ("%type%" = 30) OR // halberd
                        ("%type%" = 44)) AND ("%weapon3%" = 0) AND (("%flags%" BAND 0b00010100) = 0b00000100) AND ("%index%" > 17)))  BEGIN
              PATCH_IF ((("%flags%" BAND 0b00000010) != 0b00000010) AND ("%offhand%" = 0)) BEGIN
                PATCH_PRINT ~%SOURCE_FILE% issue: Equippable items in inventory: %item%.itm! Equipping WEAPON3~
                WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
                WRITE_SHORT ("%slot_off%" + (0x02 * 11))        "ref"
                SET "weapon3" = 1
              END
            END ELSE
            PATCH_IF (((("%type%" = 15) OR // bow
                        ("%type%" = 16) OR // dagger
                        ("%type%" = 17) OR // maces
                        ("%type%" = 18) OR // sling
                        ("%type%" = 19) OR // short sword
                        ("%type%" = 20) OR // swords
                        ("%type%" = 21) OR // hammer
                        ("%type%" = 22) OR // morning stars
                        ("%type%" = 23) OR // flail
                        ("%type%" = 24) OR // darts
                        ("%type%" = 25) OR // axe
                        ("%type%" = 26) OR // staff
                        ("%type%" = 27) OR // xbow
                        ("%type%" = 28) OR // hand to hand
                        ("%type%" = 29) OR // Spear
                        ("%type%" = 30) OR // halberd
                        ("%type%" = 44)) AND ("%weapon4%" = 0) AND (("%flags%" BAND 0b00010100) = 0b00000100) AND ("%index%" > 17)))  BEGIN
              PATCH_IF ((("%flags%" BAND 0b00000010) != 0b00000010) AND ("%offhand%" = 0)) BEGIN
                PATCH_PRINT ~%SOURCE_FILE% issue: Equippable items in inventory: %item%.itm! Equipping WEAPON4~
                WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
                WRITE_SHORT ("%slot_off%" + (0x02 * 12))        "ref"
                SET "weapon4" = 1
              END
            END ELSE
            PATCH_IF ((("%type%" = 12) AND ("%offhand%" = 0) AND ("2handedequip" = 0)) AND (("%flags%" BAND 0b00010100) = 0b00000100)) AND ("%index%" > 17) BEGIN
              PATCH_PRINT ~%SOURCE_FILE% issue: Equippable items in inventory: %item%.itm! Equipping SHIELD~
              WRITE_SHORT ("%slot_off%" + (0x02 * "%index%")) 0xffff
              WRITE_SHORT ("%slot_off%" + (0x02 * 2))         "ref"
              SET "offhand" = 1
            END
          END
        END
      END
    END
  END
BUT_ONLY_IF_IT_CHANGES

BEGIN ~Creature Stuff - MAJESTIC Creature Fixer~
  GROUP ~Creature Stuff~

COPY_EXISTING_REGEXP GLOB ~^.+\.cre$~ ~override~
  PATCH_IF (SOURCE_SIZE > 0x2d3) THEN BEGIN

    // Missing Animation Fixer
    READ_SHORT 0x28  "anim"

    LOOKUP_IDS_SYMBOL_OF_INT animation animate %anim%
    PATCH_IF ("%anim%" STRING_EQUAL "%animation%") BEGIN
      PATCH_PRINT "%SOURCE_FILE% issue: Missing animation! Assigning Gibberling animation..."
      WRITE_SHORT 0x28 30720 // Gibberling
    END

    // Missing Script Remover
    FOR (index = 0 ; index < 5 ; index = index + 1) BEGIN
      READ_ASCII (0x248 + (0x08 * "%index%")) "script"
      PATCH_IF NOT (("%script%" STRING_EQUAL_CASE "NONE") OR ("%script%" STRING_EQUAL_CASE "")) BEGIN
        SET exists = 0
        PATCH_IF FILE_EXISTS_IN_GAME ~%script%.bcs~ BEGIN
          SET exists = 1
        END
        PATCH_IF ("%exists%" = 0) BEGIN
          PATCH_IF ("%script%" STRING_EQUAL_CASE "_DPLAYER") BEGIN
            PATCH_PRINT "%SOURCE_FILE% issue: Non-existent script (%script%.BCS) changed into DPLAYER.BCS..."
            WRITE_ASCII (0x248 + (0x08 * "%index%")) ~DPLAYER~ #8
          END ELSE
          PATCH_IF ("%script%" STRING_EQUAL_CASE "BPMAG12D") BEGIN
            PATCH_PRINT "%SOURCE_FILE% issue: Non-existent script (%script%.BCS) changed into MAGE12D.BCS..."
            WRITE_ASCII (0x248 + (0x08 * "%index%")) ~MAGE12D~ #8
          END ELSE
          PATCH_IF ("%script%" STRING_EQUAL_CASE "BPMAG14D") BEGIN
            PATCH_PRINT "%SOURCE_FILE% issue: Non-existent script (%script%.BCS) changed into MAGE14D.BCS..."
            WRITE_ASCII (0x248 + (0x08 * "%index%")) ~MAGE14D~ #8
          END ELSE
          PATCH_IF ("%script%" STRING_EQUAL_CASE "BPWDASGT") BEGIN
            PATCH_PRINT "%SOURCE_FILE% issue: Non-existent script (%script%.BCS) changed into WDASIGHT.BCS..."
            WRITE_ASCII (0x248 + (0x08 * "%index%")) ~WDASIGHT~ #8
          END ELSE
          PATCH_IF ("%script%" STRING_EQUAL_CASE "BPWTSIGT") OR
                   ("%script%" STRING_EQUAL_CASE "WTSIGHT") OR
                   ("%script%" STRING_EQUAL_CASE "WSASIGHT") BEGIN
            PATCH_PRINT "%SOURCE_FILE% issue: Non-existent script (%script%.BCS) changed into WTASIGHT.BCS..."
            WRITE_ASCII (0x248 + (0x08 * "%index%")) ~WTASIGHT~ #8
          END ELSE
          BEGIN
            PATCH_PRINT "%SOURCE_FILE% issue: Non-existent script (%script%.BCS) nulled..."
            WRITE_ASCII (0x248 + (0x08 * "%index%")) ~~ #8
          END
        END
      END
    END

    // Missing Dialogue Remover
    READ_ASCII 0x2cc "dialog"

    PATCH_IF NOT (("%dialog%" STRING_EQUAL_CASE "NONE") OR ("%dialog%" STRING_EQUAL_CASE "")) BEGIN
      SET exists = 0
      SET dlgexists = 0
      PATCH_IF FILE_EXISTS_IN_GAME ~%dialog%.dlg~ BEGIN
        SET exists = 1
      END
      PATCH_IF FILE_EXISTS_IN_GAME ~%SOURCE_RES%.dlg~ BEGIN
        SET dlgexists = 1
      END
      PATCH_IF ("%exists%" = 0) AND ("%dlgexists%" = 1) BEGIN
        PATCH_PRINT "%SOURCE_FILE% issue: Non-existent dialogue (%dialog%.DLG) changed into %SOURCE_RES%.DLG..."
        WRITE_ASCIIE 0x2cc ~%SOURCE_RES%~ #8
      END ELSE
      PATCH_IF ("%exists%" = 0) BEGIN
        PATCH_PRINT "%SOURCE_FILE% issue: Non-existent dialogue (%dialog%.DLG) nulled..."
        WRITE_ASCII 0x2cc ~~ #8
      END
    END

    READ_LONG  0x2a0 "known_off" ELSE 0
    READ_LONG  0x2a4 "known_num" ELSE 0

    PATCH_IF (known_off > 0x2d3) THEN BEGIN
      FOR (index = 0 ; index < known_num ; index = index + 1) BEGIN
        READ_ASCII ("%known_off%" +        ("%index%" * 0x0c)) "resref"
        SET exists = 0
        INNER_PATCH_FILE ~%resref%.spl~ BEGIN
          READ_SHORT 0x1c "spl_type"
          READ_LONG  0x34 "spl_level"
          SET exists = 1
        END
        PATCH_IF ("%exists%" = 1) BEGIN

          // Spell Slot Fixer
          READ_SHORT ("%known_off%" + 0x0a + ("%index%" * 0x0c)) "type"
          PATCH_IF (("%spl_type%" = 2) AND ("%type%" != 0)) BEGIN // priest
            PATCH_IF ("%type%" = 1) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Known spell (%resref%.SPL) is a PRIEST spell (was MAGE)..."
            END ELSE
            PATCH_IF ("%type%" = 2) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Known spell (%resref%.SPL) is a PRIEST spell (was INNATE)..."
            END ELSE
            BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Known spell (%resref%.SPL) is a PRIEST spell (was %type%)..."
            END
            WRITE_SHORT ("%known_off%" + 0x0a + ("%index%" * 0x0c)) 0
          END
          PATCH_IF (("%spl_type%" = 1) AND ("%type%" != 1)) BEGIN // mage
            PATCH_IF ("%type%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Known spell (%resref%.SPL) is a MAGE spell (was PRIEST)..."
            END ELSE
            PATCH_IF ("%type%" = 2) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Known spell (%resref%.SPL) is a MAGE spell (was INNATE)..."
            END ELSE
            BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Known spell (%resref%.SPL) is a MAGE spell (was %type%)..."
            END
            WRITE_SHORT ("%known_off%" + 0x0a + ("%index%" * 0x0c)) 1
          END
          PATCH_IF (("%spl_type%" = 4) AND ("%type%" != 2)) BEGIN // innate
            PATCH_IF ("%type%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Known spell (%resref%.SPL) is an INNATE spell (was PRIEST)..."
            END ELSE
            PATCH_IF ("%type%" = 1) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Known spell (%resref%.SPL) is an INNATE spell (was MAGE)..."
            END ELSE
            BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Known spell (%resref%.SPL) is an INNATE spell (was %type%)..."
            END
            WRITE_SHORT ("%known_off%" + 0x0a + ("%index%" * 0x0c)) 2
          END

          // Spell Level Fixer
          READ_SHORT ("%known_off%" + 0x08 + ("%index%" * 0x0c)) "olevel"
          READ_SHORT ("%known_off%" + 0x0a + ("%index%" * 0x0c)) "type"
          SET level = %olevel% + 1
          PATCH_IF (("%level%" != "%spl_level%") AND ("%spl_level%" > 0) AND (("%spl_type%" = 1) OR ("%spl_type%" = 2))) BEGIN
            SET nlevel = %spl_level% - 1
            PATCH_PRINT "%SOURCE_FILE% issue: Known spell (%resref%.SPL) has wrong level (%olevel%)! Should be %nlevel%..."
            WRITE_SHORT ("%known_off%" + 0x08 + ("%index%" * 0x0c)) %nlevel%
          END
          PATCH_IF ("%olevel%" > 0) AND ("%spl_type%" = 4) BEGIN
            PATCH_PRINT "%SOURCE_FILE% issue: Known spell (%resref%.SPL) is INNATE with non-zero level (%olevel%)! Setting to 0 to avoid crash..."
            WRITE_SHORT ("%known_off%" + 0x08 + ("%index%" * 0x0c)) 0
          END

          // Invalid Spell Level Fixer
          READ_SHORT ("%known_off%" + 0x08 + ("%index%" * 0x0c)) "olevel"
          SET nlevel = %olevel% + 1
          PATCH_IF ("%olevel%" > 6) AND ("%spl_type%" = 2) BEGIN
            PATCH_PRINT "%SOURCE_FILE% issue: Spell level out of bounds (%resref%.SPL is a level %nlevel% PRIEST spell)! Removing to avoid crash..."
            REMOVE_KNOWN_SPELL ~%resref%~
            SET "index" = 0 - 1
            READ_LONG  0x2a4 "known_num"
          END ELSE
          PATCH_IF ("%olevel%" > 8) AND ("%spl_type%" = 1) BEGIN
            PATCH_PRINT "%SOURCE_FILE% issue: Spell level out of bounds (%resref%.SPL is a level %nlevel% MAGE spell)! Removing to avoid crash..."
            REMOVE_KNOWN_SPELL ~%resref%~
            SET "index" = 0 - 1
            READ_LONG  0x2a4 "known_num"
          END
        END ELSE

        // Missing Known Spell Remover
        BEGIN
          PATCH_PRINT "%SOURCE_FILE% issue: Known spell does not exist (%resref%.SPL)! Removing..."
          REMOVE_KNOWN_SPELL ~%resref%~
          SET "index" = 0 - 1
          READ_LONG  0x2a4 "known_num"
        END
      END
      END

    // Missing Memorized Spell Remover
    READ_LONG  0x2b0 "mem_off" ELSE 0
    READ_LONG  0x2b4 "mem_num" ELSE 0

    PATCH_IF (mem_off > 0x2d3) THEN BEGIN
      FOR (index = 0 ; index < mem_num ; index = index + 1) BEGIN
        READ_ASCII ("%mem_off%" +        ("%index%" * 0x0c)) "resref"
        SET exists = 0
        PATCH_IF FILE_EXISTS_IN_GAME ~%resref%.spl~ BEGIN
          SET exists = 1
        END
        PATCH_IF ("%exists%" = 0) BEGIN
          PATCH_PRINT "%SOURCE_FILE% issue: Memorized spell does not exist (%resref%.SPL)! Removing..."
          REMOVE_MEMORIZED_SPELL ~%resref%~
          SET "index" = 0 - 1
          READ_LONG  0x2b4 "mem_num"
        END
      END
    END
  END
BUT_ONLY_IF_IT_CHANGES

BEGIN ~Creature Stuff - Duplicate Creature Effect Remover~
  GROUP ~Creature Stuff~

COPY_EXISTING_REGEXP GLOB ~^.+\.cre$~ ~override~
  PATCH_IF (SOURCE_SIZE > 0x2d3) THEN BEGIN
    READ_BYTE  0x33  "eff_ver"
    READ_LONG  0x2a0 "spl_off" ELSE 0
    READ_LONG  0x2a8 "meminfo_off" ELSE 0
    READ_LONG  0x2b0 "mem_off" ELSE 0
    READ_LONG  0x2b8 "slot_off" ELSE 0
    READ_LONG  0x2bc "itm_off" ELSE 0
    READ_LONG  0x2c4 "eff_off" ELSE 0
    READ_LONG  0x2c8 "eff_num" ELSE 0

    PATCH_IF (eff_off > 0x2d3) THEN BEGIN
      PATCH_IF ("%eff_ver%" = 0) BEGIN
        FOR (index = 0 ; index < eff_num ; index = index + 1) BEGIN
          READ_SHORT ("%eff_off%" +        ("%index%" * 0x30)) "opcode"
          READ_BYTE  ("%eff_off%" + 0x02 + ("%index%" * 0x30)) "target"
          READ_BYTE  ("%eff_off%" + 0x03 + ("%index%" * 0x30)) "power"
          READ_LONG  ("%eff_off%" + 0x04 + ("%index%" * 0x30)) "param1"
          READ_LONG  ("%eff_off%" + 0x08 + ("%index%" * 0x30)) "param2"
          READ_BYTE  ("%eff_off%" + 0x0c + ("%index%" * 0x30)) "timing"
          READ_BYTE  ("%eff_off%" + 0x0d + ("%index%" * 0x30)) "dispel"
          READ_LONG  ("%eff_off%" + 0x0e + ("%index%" * 0x30)) "duration"
          READ_BYTE  ("%eff_off%" + 0x12 + ("%index%" * 0x30)) "prob1"
          READ_BYTE  ("%eff_off%" + 0x13 + ("%index%" * 0x30)) "prob2"
          READ_ASCII ("%eff_off%" + 0x14 + ("%index%" * 0x30)) "resref"
          READ_LONG  ("%eff_off%" + 0x1c + ("%index%" * 0x30)) "diccnt"
          READ_LONG  ("%eff_off%" + 0x20 + ("%index%" * 0x30)) "dicsid"
          READ_LONG  ("%eff_off%" + 0x24 + ("%index%" * 0x30)) "savtyp"
          READ_LONG  ("%eff_off%" + 0x28 + ("%index%" * 0x30)) "savbon"
          FOR (index1 = 0 ; index1 < eff_num ; index1 = index1 + 1) BEGIN
            PATCH_IF ("%index1%" != "%index%") BEGIN
              READ_SHORT ("%eff_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_BYTE  ("%eff_off%" + 0x02 + ("%index1%" * 0x30)) "eff_target"
              READ_BYTE  ("%eff_off%" + 0x03 + ("%index1%" * 0x30)) "eff_power"
              READ_LONG  ("%eff_off%" + 0x04 + ("%index1%" * 0x30)) "eff_param1"
              READ_LONG  ("%eff_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
              READ_BYTE  ("%eff_off%" + 0x0c + ("%index1%" * 0x30)) "eff_timing"
              READ_BYTE  ("%eff_off%" + 0x0d + ("%index1%" * 0x30)) "eff_dispel"
              READ_LONG  ("%eff_off%" + 0x0e + ("%index1%" * 0x30)) "eff_duration"
              READ_BYTE  ("%eff_off%" + 0x12 + ("%index1%" * 0x30)) "eff_prob1"
              READ_BYTE  ("%eff_off%" + 0x13 + ("%index1%" * 0x30)) "eff_prob2"
              READ_ASCII ("%eff_off%" + 0x14 + ("%index1%" * 0x30)) "eff_resref"
              READ_LONG  ("%eff_off%" + 0x1c + ("%index1%" * 0x30)) "eff_diccnt"
              READ_LONG  ("%eff_off%" + 0x20 + ("%index1%" * 0x30)) "eff_dicsid"
              READ_LONG  ("%eff_off%" + 0x24 + ("%index1%" * 0x30)) "eff_savtyp"
              READ_LONG  ("%eff_off%" + 0x28 + ("%index1%" * 0x30)) "eff_savbon"
              PATCH_IF ("%opcode%" = "%eff_opcode%") AND
                       ("%target%" = "%eff_target%") AND
                       ("%power%" = "%eff_power%") AND
                       ("%param1%" = "%eff_param1%") AND
                       ("%param2%" = "%eff_param2%") AND
                       ("%timing%" = "%eff_timing%") AND
                       ("%dispel%" = "%eff_dispel%") AND
                       ("%duration%" = "%eff_duration%") AND
                       ("%prob1%" = "%eff_prob1%") AND
                       ("%prob2%" = "%eff_prob2%") AND
                       ("%resref%" STRING_EQUAL_CASE "%eff_resref%") AND
                       ("%diccnt%" = "%eff_diccnt%") AND
                       ("%dicsid%" = "%eff_dicsid%") AND
                       ("%savtyp%" = "%eff_savtyp%") AND
                       ("%savbon%" = "%eff_savbon%") BEGIN
                PATCH_PRINT "%SOURCE_FILE% issue: Effect #%index1% (Opcode %opcode%) matches Effect #%index%! Removing..."
                offset = "%eff_off%" + ("%index1%" * 0x30)
                value  = 0x30
                DELETE_BYTES offset value
                SOURCE_SIZE = SOURCE_SIZE - value
                SET "index1" = 0 - 1
                SET "eff_num" = "eff_num" - 1
                WRITE_LONG 0x2c8 %eff_num%
                PATCH_IF ("%spl_off%" > "%eff_off%") BEGIN
                  SET "spl_off" = "spl_off" - 0x30
                  WRITE_LONG 0x2a0 %spl_off%
                END
                PATCH_IF ("%meminfo_off%" > "%eff_off%") BEGIN
                  SET "meminfo_off" = "meminfo_off" - 0x30
                  WRITE_LONG 0x2a8 %meminfo_off%
                END
                PATCH_IF ("%mem_off%" > "%eff_off%") BEGIN
                  SET "mem_off" = "mem_off" - 0x30
                  WRITE_LONG 0x2b0 %mem_off%
                END
                PATCH_IF ("%slot_off%" > "%eff_off%") BEGIN
                  SET "slot_off" = "slot_off" - 0x30
                  WRITE_LONG 0x2b8 %slot_off%
                END
                PATCH_IF ("%itm_off%" > "%eff_off%") BEGIN
                  SET "itm_off" = "itm_off" - 0x30
                  WRITE_LONG 0x2bc %itm_off%
                END
              END
            END
          END
        END
      END ELSE PATCH_IF ("%eff_ver%" = 1) BEGIN
        FOR (index = 0 ; index < eff_num ; index = index + 1) BEGIN
          READ_LONG  ("%eff_off%" + 0x08 + ("%index%" * 0x108)) "opcode"
          READ_LONG  ("%eff_off%" + 0x0c + ("%index%" * 0x108)) "target"
          READ_LONG  ("%eff_off%" + 0x10 + ("%index%" * 0x108)) "power"
          READ_LONG  ("%eff_off%" + 0x14 + ("%index%" * 0x108)) "param1"
          READ_LONG  ("%eff_off%" + 0x18 + ("%index%" * 0x108)) "param2"
          READ_SHORT ("%eff_off%" + 0x1c + ("%index%" * 0x108)) "timing"
          READ_LONG  ("%eff_off%" + 0x20 + ("%index%" * 0x108)) "duration"
          READ_SHORT ("%eff_off%" + 0x24 + ("%index%" * 0x108)) "prob1"
          READ_SHORT ("%eff_off%" + 0x26 + ("%index%" * 0x108)) "prob2"
          READ_ASCII ("%eff_off%" + 0x28 + ("%index%" * 0x108)) "resref"
          READ_LONG  ("%eff_off%" + 0x30 + ("%index%" * 0x108)) "diccnt"
          READ_LONG  ("%eff_off%" + 0x34 + ("%index%" * 0x108)) "dicsid"
          READ_LONG  ("%eff_off%" + 0x38 + ("%index%" * 0x108)) "savtyp"
          READ_LONG  ("%eff_off%" + 0x3c + ("%index%" * 0x108)) "savbon"
          READ_LONG  ("%eff_off%" + 0x54 + ("%index%" * 0x108)) "dispel"
          FOR (index1 = 0 ; index1 < eff_num ; index1 = index1 + 1) BEGIN
            PATCH_IF ("%index1%" != "%index%") BEGIN
              READ_LONG  ("%eff_off%" + 0x08 + ("%index1%" * 0x108)) "eff_opcode"
              READ_LONG  ("%eff_off%" + 0x0c + ("%index1%" * 0x108)) "eff_target"
              READ_LONG  ("%eff_off%" + 0x10 + ("%index1%" * 0x108)) "eff_power"
              READ_LONG  ("%eff_off%" + 0x14 + ("%index1%" * 0x108)) "eff_param1"
              READ_LONG  ("%eff_off%" + 0x18 + ("%index1%" * 0x108)) "eff_param2"
              READ_SHORT ("%eff_off%" + 0x1c + ("%index1%" * 0x108)) "eff_timing"
              READ_LONG  ("%eff_off%" + 0x20 + ("%index1%" * 0x108)) "eff_duration"
              READ_SHORT ("%eff_off%" + 0x24 + ("%index1%" * 0x108)) "eff_prob1"
              READ_SHORT ("%eff_off%" + 0x26 + ("%index1%" * 0x108)) "eff_prob2"
              READ_ASCII ("%eff_off%" + 0x28 + ("%index1%" * 0x108)) "eff_resref"
              READ_LONG  ("%eff_off%" + 0x30 + ("%index1%" * 0x108)) "eff_diccnt"
              READ_LONG  ("%eff_off%" + 0x34 + ("%index1%" * 0x108)) "eff_dicsid"
              READ_LONG  ("%eff_off%" + 0x38 + ("%index1%" * 0x108)) "eff_savtyp"
              READ_LONG  ("%eff_off%" + 0x3c + ("%index1%" * 0x108)) "eff_savbon"
              READ_LONG  ("%eff_off%" + 0x54 + ("%index1%" * 0x108)) "eff_dispel"
              PATCH_IF ("%opcode%" = "%eff_opcode%") AND
                       ("%target%" = "%eff_target%") AND
                       ("%power%" = "%eff_power%") AND
                       ("%param1%" = "%eff_param1%") AND
                       ("%param2%" = "%eff_param2%") AND
                       ("%timing%" = "%eff_timing%") AND
                       ("%dispel%" = "%eff_dispel%") AND
                       ("%duration%" = "%eff_duration%") AND
                       ("%prob1%" = "%eff_prob1%") AND
                       ("%prob2%" = "%eff_prob2%") AND
                       ("%resref%" STRING_EQUAL_CASE "%eff_resref%") AND
                       ("%diccnt%" = "%eff_diccnt%") AND
                       ("%dicsid%" = "%eff_dicsid%") AND
                       ("%savtyp%" = "%eff_savtyp%") AND
                       ("%savbon%" = "%eff_savbon%") BEGIN
                PATCH_PRINT "%SOURCE_FILE% issue: Effect #%index1% (Opcode %opcode%) matches Effect #%index%! Removing..."
                offset = "%eff_off%" + ("%index1%" * 0x108)
                value  = 0x108
                DELETE_BYTES offset value
                SOURCE_SIZE = SOURCE_SIZE - value
                SET "index1" = 0 - 1
                SET "eff_num" = "eff_num" - 1
                WRITE_LONG 0x2c8 %eff_num%
                PATCH_IF ("%spl_off%" > "%eff_off%") BEGIN
                  SET "spl_off" = "spl_off" - 0x108
                  WRITE_LONG 0x2a0 %spl_off%
                END
                PATCH_IF ("%meminfo_off%" > "%eff_off%") BEGIN
                  SET "meminfo_off" = "meminfo_off" - 0x108
                  WRITE_LONG 0x2a8 %meminfo_off%
                END
                PATCH_IF ("%mem_off%" > "%eff_off%") BEGIN
                  SET "mem_off" = "mem_off" - 0x108
                  WRITE_LONG 0x2b0 %mem_off%
                END
                PATCH_IF ("%slot_off%" > "%eff_off%") BEGIN
                  SET "slot_off" = "slot_off" - 0x108
                  WRITE_LONG 0x2b8 %slot_off%
                END
                PATCH_IF ("%itm_off%" > "%eff_off%") BEGIN
                  SET "itm_off" = "itm_off" - 0x108
                  WRITE_LONG 0x2bc %itm_off%
                END
              END
            END
          END
        END
      END
    END
  END
BUT_ONLY_IF_IT_CHANGES

BEGIN ~Item Stuff - Item Resource Fixer~
  GROUP ~Item Stuff~

COPY_EXISTING_REGEXP GLOB ~^.+\.itm$~ ~override~
  PATCH_IF (SOURCE_SIZE > 0x71) BEGIN
    READ_LONG  0x64 "abil_off"
    READ_SHORT 0x68 "abil_num"
    READ_LONG  0x6a "fx_off"
    READ_SHORT 0x70 "fx_num"
    PATCH_IF ((abil_off > fx_off) AND (abil_num > 1)) BEGIN
      READ_ASCII abil_off ~eh~ ELSE ~fail~ (0x38 * abil_num)
      PATCH_IF (~%eh%~ STRING_EQUAL ~fail~) BEGIN
        WHILE ((~%eh%~ STRING_EQUAL ~fail~) AND (abil_num > 0)) BEGIN
          READ_ASCII abil_off ~eh~ ELSE ~fail~ (0x38 * abil_num)
          abil_num -= 1
        END
      END
      offset = abil_off
      value  = 0x38 * abil_num
      PATCH_PRINT "%SOURCE_FILE% issue: Restructuring - Unused Ability Structure found! Trimming %value% bytes from offset %offset%..."
      DELETE_BYTES offset value
      SOURCE_SIZE = SOURCE_SIZE - value
      abil_off = 0x72
      WRITE_LONG 0x64 abil_off
      WRITE_SHORT 0x68 abil_num
      fx_off = (0x72 + (0x38 * abil_num))
      WRITE_LONG 0x6a fx_off
      PATCH_IF !(~%eh%~ STRING_EQUAL ~fail~) BEGIN
        INSERT_BYTES abil_off (0x38 * abil_num)
        WRITE_ASCIIE abil_off ~%eh%~
      END
    END ELSE PATCH_IF ((abil_off != 0x72) AND (abil_num = 0)) BEGIN
      abil_off = 0x72
      WRITE_LONG 0x64 abil_off
    END
    FOR (i1 = 0; i1 < (0x38 * abil_num); i1 += 0x38) BEGIN
      WRITE_SHORT (abil_off + i1 + 0x20) fx_num
      READ_SHORT (abil_off + i1 + 0x1e) cx
      fx_num += cx
    END
    PATCH_IF ((abil_off + (0x38 * abil_num)) < fx_off) BEGIN
      offset = abil_off + (0x38 * abil_num)
      value  = fx_off - ((abil_off + (0x38 * abil_num)))
      PATCH_PRINT "%SOURCE_FILE% issue: Restructuring - Unused Inter-structural Bytes found! Trimming %value% bytes from offset %offset%..."
      DELETE_BYTES offset value
      SOURCE_SIZE = SOURCE_SIZE - value
      fx_off = offset
      WRITE_LONG 0x6a %fx_off%
    END
    PATCH_IF (SOURCE_SIZE > (0x72 + (0x38 * abil_num) + (0x30 * fx_num))) BEGIN
      offset = 0x72 + (0x38 * abil_num) + (0x30 * fx_num)
      value  = SOURCE_SIZE - (0x72 + (0x38 * abil_num) + (0x30 * fx_num))
      PATCH_PRINT "%SOURCE_FILE% issue: Restructuring - Unused Trailing Bytes found! Trimming %value% bytes from offset %offset%..."
      DELETE_BYTES offset value
      SOURCE_SIZE = SOURCE_SIZE - value
    END
  END
BUT_ONLY_IF_IT_CHANGES

// BG2Fixpack BETA Core fixes causing self-inflicting burns on flaming swords and miscellany
ACTION_IF !(FILE_EXISTS_IN_GAME ~cdhalb04.eff~) THEN BEGIN
  COPY_EXISTING ~dragdam3.eff~ ~override/cdhalb04.eff~
END
ACTION_IF !(FILE_EXISTS_IN_GAME ~cdsw1h03.eff~) THEN BEGIN
  COPY_EXISTING ~lycandam.eff~ ~override/cdsw1h03.eff~
END

COPY_EXISTING_REGEXP GLOB ~^.+\.itm$~ ~override~
  PATCH_IF (SOURCE_SIZE > 0x71) THEN BEGIN // protects against invalid files
    READ_LONG   0x64 "abil_off"
    READ_SHORT  0x68 "abil_num"
    READ_LONG   0x6a "fx_off"
    READ_SHORT  0x70 "fx_num"
    SET delta = 0
    FOR (index = 0 ; index < fx_num ; ++index) BEGIN
      READ_SHORT ("%fx_off%" +        ("%index%" * 0x30)) "type"
      READ_ASCII ("%fx_off%" + 0x14 + ("%index%" * 0x30)) "eff_file"
      PATCH_IF (("%type%" = 177) AND
                (("%eff_file%" STRING_COMPARE_CASE "DAMACEA" = 0) OR
                 ("%eff_file%" STRING_COMPARE_CASE "DOPPDAM" = 0) OR
                 ("%eff_file%" STRING_COMPARE_CASE "DRAGDAM3" = 0) OR
                 ("%eff_file%" STRING_COMPARE_CASE "EVILDAM2" = 0) OR
                 ("%eff_file%" STRING_COMPARE_CASE "FLAMENOR" = 0) OR
                 ("%eff_file%" STRING_COMPARE_CASE "GIANTDAM" = 0) OR
                 ("%eff_file%" STRING_COMPARE_CASE "HOLYAVEN" = 0) OR
                 ("%eff_file%" STRING_COMPARE_CASE "LYCANDAM" = 0) OR
                 ("%eff_file%" STRING_COMPARE_CASE "LYCNDAM3" = 0) OR
                 ("%eff_file%" STRING_COMPARE_CASE "LYCNDAM4" = 0) OR
                 ("%eff_file%" STRING_COMPARE_CASE "PHANBLAD" = 0) OR
                 ("%eff_file%" STRING_COMPARE_CASE "SKULLCR" = 0) OR
                 ("%eff_file%" STRING_COMPARE_CASE "SW2H19A" = 0) OR
                 ("%eff_file%" STRING_COMPARE_CASE "UNDDAM1" = 0) OR
                 ("%eff_file%" STRING_COMPARE_CASE "TROLLD1" = 0) OR
                 ("%eff_file%" STRING_COMPARE_REGEXP "EQUAL0[1-8]" = 0) OR
                 ("%eff_file%" STRING_COMPARE_REGEXP "FT[1-3]DAM" = 0) OR
                 ("%eff_file%" STRING_COMPARE_REGEXP "SW1H6[45]A" = 0) OR
                 ("%eff_file%" STRING_COMPARE_REGEXP "SW1H62[BCDEF]" = 0))) BEGIN
        PATCH_IF ("%eff_file%" STRING_COMPARE_CASE "lycandam" = 0) BEGIN
          SPRINT eff_file "cdsw1h03"
        END
        PATCH_IF ("%eff_file%" STRING_COMPARE_CASE "dragdam3" = 0) BEGIN
          SPRINT eff_file "cdhalb04"
        END
        READ_ASCII   ("%fx_off%" +        ("%index%" * 0x30)) "clone" (48)
        PATCH_PRINT "%SOURCE_FILE% patched for BG2Fixpack EFF fixes..."
        offset = "%fx_off%" + ("%index%" * 0x30)
        value  = 0x30
        DELETE_BYTES offset value
        SOURCE_SIZE = SOURCE_SIZE - value
        SET fx_num = (fx_num - 1)
        SET delta  = (delta - 1)
        SET index  = (index - 1)
        INNER_ACTION BEGIN
          COPY_EXISTING ~%eff_file%.eff~ ~override~
            READ_ASCII 0x1c "ids" (8)
          BUT_ONLY
        END
        FOR (index2 = 0 ; index2 < abil_num ; index2 = index2 + 1) BEGIN
          READ_BYTE   ("%abil_off%" +        ("%index2%" * 0x38)) "type"
          READ_SHORT  ("%abil_off%" + 0x20 + ("%index2%" * 0x38)) "abil_fx_idx"
          SET "abil_fx_idx" = "%abil_fx_idx%" + "%delta%"
          WRITE_SHORT ("%abil_off%" + 0x20 + ("%index2%" * 0x38)) "%abil_fx_idx%"
          PATCH_IF (("%type%" = 1) OR ("%type%" = 2)) BEGIN
            INSERT_BYTES (fx_off +        (0x30 * abil_fx_idx)) 0x30
            WRITE_ASCIIE (fx_off +        (0x30 * abil_fx_idx)) "%clone%"
            WRITE_SHORT  (fx_off +        (0x30 * abil_fx_idx)) 177     // use eff
            WRITE_BYTE   (fx_off + 0x02 + (0x30 * abil_fx_idx))   2     // preset target
            WRITE_ASCIIE (fx_off + 0x04 + (0x30 * abil_fx_idx)) "%ids%" // ids target from eff
            WRITE_BYTE   (fx_off + 0x0c + (0x30 * abil_fx_idx))   1     // instant/perm
            WRITE_ASCIIE (fx_off + 0x14 + (0x30 * abil_fx_idx)) "%eff_file%" #8 // for the items with changing effs
            READ_SHORT  ("%abil_off%" + 0x1e + ("%index2%" * 0x38)) "abil_fx_num"
            WRITE_SHORT ("%abil_off%" + 0x1e + ("%index2%" * 0x38)) ("%abil_fx_num%" + 1)
            SET delta = (delta + 1)
          END
        END
      END
    END
    WRITE_SHORT  0x70 "%fx_num%"

    // Poison Resist Effect Fixer (needs ToBEx)
    READ_LONG   0x6a "fx_off" ELSE 0
    READ_SHORT  0x6e "fx_idx" ELSE 0
    READ_SHORT  0x70 "fx_num" ELSE 0

    PATCH_IF (fx_off > 0x71) BEGIN
      FOR (index = fx_idx ; index < fx_num ; index = index + 1) BEGIN
        READ_SHORT ("%fx_off%" +        ("%index%" * 0x30)) "opcode"
        READ_BYTE  ("%fx_off%" + 0x02 + ("%index%" * 0x30)) "target"
        READ_LONG  ("%fx_off%" + 0x04 + ("%index%" * 0x30)) "param1"
        READ_LONG  ("%fx_off%" + 0x08 + ("%index%" * 0x30)) "param2"
        PATCH_IF ("%opcode%" = 173) AND ("%target%" = 1) AND ("%param1%" < 100) AND ("%param2%" != 1) BEGIN
          PATCH_PRINT "%SOURCE_FILE% issue: Poison Resist set to INCREMENT to %param1%"
          WRITE_LONG  ("%fx_off%" + 0x08 + ("%index%" * 0x30)) 1
        END ELSE
        PATCH_IF ("%opcode%" = 173) AND ("%target%" = 1) AND ("%param1%" > 99) AND ("%param2%" != 0) BEGIN
          PATCH_PRINT "%SOURCE_FILE% issue: Poison Resist set to SET to %param1%"
          WRITE_LONG  ("%fx_off%" + 0x08 + ("%index%" * 0x30)) 0
        END
      END
    END
  END
BUT_ONLY

// 1PPv4 causing crashes on NPCs with BG1 animations wearing helms with animset JC
ACTION_IF (FILE_EXISTS_IN_GAME ~WPLJ8A1.bam~) AND NOT (FILE_EXISTS_IN_GAME ~WPLJCA1.bam~) THEN BEGIN
  PRINT "1PP detected! Adding missing JC Helm animation set frames for BG1 animation compatibility..."
  ACTION_FOR_EACH category IN ~L~ ~M~ ~S~ ~T~ BEGIN
    ACTION_FOR_EACH sequence IN ~A1~ ~A1E~ ~A2~ ~A2E~ ~A3~ ~A3E~ ~A4~ ~A4E~ ~A5~ ~A5E~ ~A6~ ~A6E~ ~CA~ ~CAE~ ~G1~ ~G1E~ ~SA~ ~SAE~ ~SX~ ~SXE~ ~W2~ ~W2E~ BEGIN
      COPY_EXISTING ~WP%category%J8%sequence%.BAM~ ~override/WP%category%JC%sequence%.BAM~
    END
  END
END

BEGIN ~Item Stuff - MAJESTIC Item Fixer~
  GROUP ~Item Stuff~

COPY_EXISTING_REGEXP GLOB ~^.+\.itm$~ ~override~
  PATCH_IF (SOURCE_SIZE > 0x71) BEGIN
    READ_BYTE  0x18 "flags"

    PATCH_IF (("%flags%" BAND 0b00000100) = 0b00000100) BEGIN  // droppable

      // 2-handed Flag Setter
      READ_BYTE  0x18 "flags"
      READ_ASCII 0x22 "anim" (2)

      PATCH_IF (("%anim%" STRING_EQUAL_CASE "BW") OR //bow
                ("%anim%" STRING_EQUAL_CASE "CB") OR //crossbow
                ("%anim%" STRING_EQUAL_CASE "HB") OR //halberd
                ("%anim%" STRING_EQUAL_CASE "QS") OR //quarterstaff
                ("%anim%" STRING_EQUAL_CASE "S2") OR //2-handed sword
                ("%anim%" STRING_EQUAL_CASE "SP")) AND //spear
               (("%flags%" BAND 0b00000010) = 0b00000000) BEGIN //2-handed
        PATCH_PRINT "%SOURCE_FILE% issue: Item needs to be 2-handed (%anim% anim) to prevent crash!"
        WRITE_BYTE 0x18 ("%flags%" BOR 0b00000010)
      END

      // Used Up Item Fixer
      READ_ASCII 0x10 "used_itm"

      PATCH_IF NOT ((FILE_EXISTS_IN_GAME ~%used_itm%.itm~) OR ("%used_itm%" STRING_EQUAL_CASE "") OR ("%used_itm%" STRING_EQUAL_CASE "none")) BEGIN
        PATCH_PRINT "%SOURCE_FILE% issue: Non-existent Used Up Item (%used_itm%.ITM)! Nulling..."
        WRITE_ASCII 0x10 ~NONE~ #8
      END

      // Missing Container Store Linker
      READ_SHORT 0x1c "type"

      PATCH_IF ("%type%" = 36) BEGIN
        PATCH_IF NOT (FILE_EXISTS_IN_GAME ~%SOURCE_RES%.sto~) BEGIN
          PATCH_PRINT "%SOURCE_FILE% issue: Container missing corresponding STO file! Linking makeshift store..."
          INNER_ACTION BEGIN

            ACTION_DEFINE_ASSOCIATIVE_ARRAY lol_stores BEGIN
              1 => ~Big World Burgers~
              2 => ~Lollorian's Lulz~
              3 => ~Leonardo's Locks~
              4 => ~Jarno's Jams~
              5 => ~I AM ERROR!~
            END

            COPY_EXISTING ~BAG04.STO~ ~override/%SOURCE_RES%.STO~ // copy bag of holding's store
              SET rnb = RANDOM (1 5)
              PHP_EACH lol_stores AS random_num => random_store BEGIN
                PATCH_IF ("%rnb%" = "%random_num%") BEGIN
                  SAY 0xc "%random_store%"
                END
              END
          END
        END
      END

      // Type Fixer
      READ_SHORT 0x1c "type"

      PATCH_IF ("%type%" = 39) OR ("%type%" = 40) OR ("%type%" = 42) OR ("%type%" = 50) OR ("%type%" = 55) OR ("%type%" = 59) BEGIN
        WRITE_SHORT 0x1c 0
        PATCH_PRINT "%SOURCE_FILE% issue: Invalid Item Type (%type%)! Setting to Misc type..."
      END
      PATCH_IF ("%type%" = 41) OR ("%type%" = 47) OR ("%type%" = 49) OR ("%type%" = 53) BEGIN
        WRITE_SHORT 0x1c 12
        PATCH_PRINT "%SOURCE_FILE% issue: Invalid Item Type (%type%)! Setting to Shield type..."
      END
      PATCH_IF ("%type%" = 44) BEGIN
        WRITE_SHORT 0x1c 17
        PATCH_PRINT "%SOURCE_FILE% issue: Invalid Item Type (%type%)! Setting to Mace type..."
      END
      PATCH_IF ("%type%" = 56) OR ("%type%" = 71) BEGIN
        WRITE_SHORT 0x1c 9
        PATCH_PRINT "%SOURCE_FILE% issue: Invalid Item Type (%type%)! Setting to Potion type..."
      END
      PATCH_IF ("%type%" = 57) OR ("%type%" = 69) BEGIN
        WRITE_SHORT 0x1c 20
        PATCH_PRINT "%SOURCE_FILE% issue: Invalid Item Type (%type%)! Setting to Large Sword type..."
      END
      PATCH_IF ("%type%" = 58) BEGIN
        WRITE_SHORT 0x1c 36
        PATCH_PRINT "%SOURCE_FILE% issue: Invalid Item Type (%type%)! Setting to Container type..."
      END
      PATCH_IF ("%type%" = 60) OR ("%type%" = 61) OR ("%type%" = 62) OR ("%type%" = 63) OR ("%type%" = 64) OR ("%type%" = 65) OR ("%type%" = 66) OR ("%type%" = 67) BEGIN
        WRITE_SHORT 0x1c 2
        PATCH_PRINT "%SOURCE_FILE% issue: Invalid Item Type (%type%)! Setting to Armor type..."
      END
      PATCH_IF ("%type%" = 70) BEGIN
        WRITE_SHORT 0x1c 32
        PATCH_PRINT "%SOURCE_FILE% issue: Invalid Item Type (%type%)! Setting to Cloak type..."
      END
      PATCH_IF ("%type%" = 72) BEGIN
        WRITE_SHORT 0x1c 7
        PATCH_PRINT "%SOURCE_FILE% issue: Invalid Item Type (%type%)! Setting to Headgear type..."
      END
      PATCH_IF ("%type%" = 73) BEGIN
        WRITE_SHORT 0x1c 6
        PATCH_PRINT "%SOURCE_FILE% issue: Invalid Item Type (%type%)! Setting to Gauntlet type..."
      END

      // Placeholder Icon Assigner
      READ_SHORT 0x1c "type"
      READ_ASCII 0x3a "inv_icon"
      READ_ASCII 0x44 "gnd_icon"
      READ_ASCII 0x58 "desc_icon"
      SET "inv_exists" = 0
      SET "gnd_exists" = 0
      SET "desc_exists" = 0

      PATCH_IF FILE_EXISTS_IN_GAME ~%inv_icon%.bam~ BEGIN
        SET "inv_exists" = 1
      END
      PATCH_IF FILE_EXISTS_IN_GAME ~%gnd_icon%.bam~ BEGIN
        SET "gnd_exists" = 1
      END
      PATCH_IF FILE_EXISTS_IN_GAME ~%desc_icon%.bam~ BEGIN
        SET "desc_exists" = 1
      END

      PATCH_IF ("%inv_exists%" = 0) BEGIN
        PATCH_IF ("%inv_icon%" STRING_EQUAL_CASE "none") OR ("%inv_icon%" STRING_EQUAL_CASE "") BEGIN
          PATCH_PRINT "%SOURCE_FILE% issue: Unassigned Inventory Icon! Assigning placeholder icon..."
        END ELSE
        BEGIN
          PATCH_PRINT "%SOURCE_FILE% issue: Non-existent Inventory Icon (%inv_icon%.BAM)! Assigning placeholder icon..."
        END
        WRITE_ASCII 0x3a ~IMISC6V~ #8
      END
      PATCH_IF ("%gnd_exists%" = 0) BEGIN
        PATCH_IF ("%gnd_icon%" STRING_EQUAL_CASE "GCLCKC01") BEGIN
          PATCH_PRINT "%SOURCE_FILE% issue: Non-existent Ground Icon (%gnd_icon%.BAM)! Assigning GCLCK01.BAM..."
          WRITE_ASCII 0x44 ~GCLCK01~ #8
        END ELSE
        PATCH_IF ("%gnd_icon%" STRING_EQUAL_CASE "_GPOTN01") OR
                 ("%gnd_icon%" STRING_EQUAL_CASE "GPOTN11") OR
                 ("%gnd_icon%" STRING_EQUAL_CASE "GPOTN41") OR
                 ("%gnd_icon%" STRING_EQUAL_CASE "PTION2KG") OR
                 ("%gnd_icon%" STRING_EQUAL_CASE "PTION2LG") OR
                 ("%gnd_icon%" STRING_EQUAL_CASE "PTION2MG") BEGIN
          PATCH_PRINT "%SOURCE_FILE% issue: Non-existent Ground Icon (%gnd_icon%.BAM)! Assigning GPOTN01.BAM..."
          WRITE_ASCII 0x44 ~GPOTN01~ #8
        END ELSE
        PATCH_IF ("%gnd_icon%" STRING_EQUAL_CASE "ICRATE01") BEGIN
          PATCH_PRINT "%SOURCE_FILE% issue: Non-existent Ground Icon (%gnd_icon%.BAM)! Assigning GCRATE01.BAM..."
          WRITE_ASCII 0x44 ~GCRATE01~ #8
        END ELSE
        PATCH_IF ("%gnd_icon%" STRING_EQUAL_CASE "none") OR ("%gnd_icon%" STRING_EQUAL_CASE "") BEGIN
          PATCH_PRINT "%SOURCE_FILE% issue: Unassigned Ground Icon! Assigning placeholder icon..."
          WRITE_ASCII 0x44 ~GCRATE01~ #8
        END ELSE
        BEGIN
          PATCH_PRINT "%SOURCE_FILE% issue: Non-existent Ground Icon (%gnd_icon%.BAM)! Assigning placeholder icon..."
          WRITE_ASCII 0x44 ~GCRATE01~ #8
        END
      END
      PATCH_IF ("%desc_exists%" = 0) BEGIN
        PATCH_IF ("%desc_icon%" STRING_EQUAL_CASE "none") OR ("%desc_icon%" STRING_EQUAL_CASE "") BEGIN
          PATCH_PRINT "%SOURCE_FILE% issue: Unassigned Description Icon! Assigning placeholder icon..."
        END ELSE
        BEGIN
          PATCH_PRINT "%SOURCE_FILE% issue: Non-existent Description Icon (%desc_icon%.BAM)! Assigning placeholder icon..."
        END
        WRITE_ASCII 0x58 ~CMISC6V~ #8
      END

      // Invalid Ability Remover
      READ_LONG  0x64 "abil_off" ELSE 0
      READ_SHORT 0x68 "abil_num" ELSE 0
      READ_LONG  0x6a "fx_off" ELSE 0
      READ_SHORT 0x6e "fx_idx" ELSE 0
      READ_SHORT 0x70 "fx_num" ELSE 0

      PATCH_IF (abil_off > 0x71) BEGIN
        FOR (index = 0; index < abil_num; index = index + 1) BEGIN
          READ_BYTE  ("%abil_off%" +        (0x38 * "%index%")) "abil_type"
          PATCH_IF ("%abil_type%" = 0) BEGIN
            READ_SHORT ("%abil_off%" + 0x1e + (0x38 * "%index%")) "abil_fx_num"
            READ_SHORT ("%abil_off%" + 0x20 + (0x38 * "%index%")) "abil_fx_idx"
            PATCH_IF ("%abil_fx_num%" > 0) BEGIN
              offset = "%fx_off%" + ("%abil_fx_idx%" * 0x30)
              value  = %abil_fx_num% * 0x30
              DELETE_BYTES offset value
              SOURCE_SIZE = SOURCE_SIZE - value
              PATCH_IF ("%abil_off%" > "%fx_off%") BEGIN
                SET "abil_off" = "abil_off" - (%abil_fx_num% * 0x30)
                WRITE_LONG  0x64 "abil_off"
              END
            END
            PATCH_IF ("%fx_idx%" > "%abil_fx_idx%") BEGIN
              SET "fx_idx" = "fx_idx" - "abil_fx_num"
              PATCH_PRINT "%SOURCE_FILE% issue: Writing new FX IDX = %fx_idx%!"
              WRITE_SHORT 0x6e "fx_idx"
            END
            FOR (index1 = 0; index1 < abil_num; index1 = index1 + 1) BEGIN
              PATCH_IF ("%index1%" != "%index%") BEGIN
                READ_SHORT ("%abil_off%" + 0x20 + (0x38 * "%index1%")) "abil_fx_idx1"
                PATCH_IF ("%abil_fx_idx1%" > "%abil_fx_idx%") BEGIN
                  SET "abil_fx_idx1" = "abil_fx_idx1" - "abil_fx_num"
                  PATCH_PRINT "%SOURCE_FILE% issue: Writing new ABIL #%index1% FX IDX = %abil_fx_idx1%!"
                  WRITE_SHORT ("%abil_off%" + 0x20 + (0x38 * "%index1%")) "abil_fx_idx1"
                END
              END
            END
            offset = "%abil_off%" + ("%index%" * 0x38)
            value  = 0x38
            DELETE_BYTES offset value
            SOURCE_SIZE = SOURCE_SIZE - value
            PATCH_PRINT "%SOURCE_FILE% issue: Ability #%index% is invalid! Deleting Ability #%index%..."
            SET "index" = 0 - 1
            SET "abil_num" = "abil_num" - 1
            WRITE_SHORT 0x68 "abil_num"
            PATCH_IF ("%fx_off%" > "%abil_off%") BEGIN
              SET "fx_off" = "fx_off" - 0x38
              WRITE_LONG  0x6a "fx_off"
            END
          END
        END
      END

      // Ability Icon Assigner
      READ_ASCII 0x3a "inv_icon"
      READ_LONG  0x64 "abil_off" ELSE 0
      READ_SHORT 0x68 "abil_num" ELSE 0

      PATCH_IF (abil_off > 0x71) BEGIN
        FOR (index = 0; index < abil_num; index = index + 1) BEGIN
          READ_BYTE  ("%abil_off%" +        (0x38 * "%index%")) "abil_type"
          READ_ASCII ("%abil_off%" + 0x04 + (0x38 * "%index%")) "abil_icon"
          PATCH_IF NOT (FILE_EXISTS_IN_GAME ~%abil_icon%.bam~) AND ("%abil_type%" > 0) BEGIN
            PATCH_IF ("%abil_icon%" STRING_EQUAL_CASE "none") OR ("%abil_icon%" STRING_EQUAL_CASE "") BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Ability #%index% has no icon assigned! Assigning Inventory Icon (%inv_icon%.BAM)..."
            END ELSE
            BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Ability #%index% has a non-existent icon (%abil_icon%.BAM)! Assigning Inventory Icon (%inv_icon%.BAM)..."
            END
            WRITE_ASCIIE ("%abil_off%" + 0x04 + (0x38 * "%index%")) ~%inv_icon%~ #8
          END
        END
      END

      // Enchantment Fixer
      READ_BYTE  0x18 "flags"
      READ_LONG  0x60 "enchantment"

      PATCH_IF (("%enchantment%" > 0) AND (("%flags%" BAND 0b01000000) != 0b01000000)) BEGIN
        PATCH_PRINT "%SOURCE_FILE% issue: Item is enchanted but not magical! Setting magical flag..."
        WRITE_BYTE 0x18 ("%flags%" BOR 0b01000000)
      END

      // Ability Slot Fixer
      READ_SHORT 0x1c "type"
      READ_LONG  0x64 "abil_off" ELSE 0
      READ_SHORT 0x68 "abil_num" ELSE 0

      PATCH_IF (abil_off > 0x71) BEGIN
        FOR (index = 0; index < abil_num; index = index + 1) BEGIN
          READ_BYTE  ("%abil_off%" + 0x00 + (0x38 * "%index%")) "abil_type"
          READ_BYTE  ("%abil_off%" + 0x02 + (0x38 * "%index%")) "slot"
          PATCH_IF ((((%type% > 14) AND (%type% < 31)) OR %type% = 44) AND (("%abil_type%" = 1) OR ("%abil_type%" = 2) OR ("%abil_type%" = 4)) AND ("%slot%" != 1)) BEGIN
            PATCH_IF ("%slot%" = 2) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Ability #%index% assigned to WEAPON slot (was SPELL)..."
            END ELSE
            PATCH_IF ("%slot%" = 3) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Ability #%index% assigned to WEAPON slot (was EQUIPMENT)..."
            END ELSE
            PATCH_IF ("%slot%" = 4) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Ability #%index% assigned to WEAPON slot (was INNATE)..."
            END ELSE
            BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Ability #%index% assigned to WEAPON slot (was %slot%)..."
            END
            WRITE_BYTE  ("%abil_off%" + 0x02 + (0x38 * "%index%")) 1
          END
          PATCH_IF ((%type% < 15 OR ((%type% > 30) AND (%type% != 44))) AND ("%abil_type%" = 3) AND ("%slot%" != 3)) BEGIN
            PATCH_IF ("%slot%" = 1) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Ability #%index% assigned to EQUIPMENT slot (was WEAPON)..."
            END ELSE
            PATCH_IF ("%slot%" = 2) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Ability #%index% assigned to EQUIPMENT slot (was SPELL)..."
            END ELSE
            PATCH_IF ("%slot%" = 4) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Ability #%index% assigned to EQUIPMENT slot (was INNATE)..."
            END ELSE
            BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Ability #%index% assigned to EQUIPMENT slot (was %slot%)..."
            END
            WRITE_BYTE  ("%abil_off%" + 0x02 + (0x38 * "%index%")) 3
          END
        END
      END

      // Animation and Ranged Ability Flag Fixer
      READ_BYTE  0x18 "flags"
      READ_LONG  0x64 "abil_off" ELSE 0
      READ_SHORT 0x68 "abil_num" ELSE 0

      PATCH_IF (abil_off > 0x71) BEGIN
        FOR (index = 0; index < abil_num; index = index + 1) BEGIN
          READ_SHORT ("%abil_off%" +        (0x38 * "%index%")) "abil_type"
          READ_SHORT ("%abil_off%" + 0x2c + (0x38 * "%index%")) "anim_overhand"
          READ_SHORT ("%abil_off%" + 0x2e + (0x38 * "%index%")) "anim_backhand"
          READ_SHORT ("%abil_off%" + 0x30 + (0x38 * "%index%")) "anim_thrust"
          READ_SHORT ("%abil_off%" + 0x32 + (0x38 * "%index%")) "is_arrow"
          READ_SHORT ("%abil_off%" + 0x34 + (0x38 * "%index%")) "is_bolt"
          READ_SHORT ("%abil_off%" + 0x36 + (0x38 * "%index%")) "is_bullet"
          PATCH_IF ((%anim_overhand% + %anim_backhand% + %anim_thrust%) > 0) AND (("%abil_type%" = 1) OR ("%abil_type%" = 2) OR ("%abil_type%" = 4)) AND ((%type% < 15) OR (%type% > 30)) BEGIN
            WRITE_SHORT ("%abil_off%" + 0x2c + (0x38 * "%index%")) 0
            WRITE_SHORT ("%abil_off%" + 0x30 + (0x38 * "%index%")) 0
            WRITE_SHORT ("%abil_off%" + 0x2e + (0x38 * "%index%")) 0
            PATCH_PRINT "%SOURCE_FILE% issue: Non weapon has animations!"
            PATCH_PRINT "  Overhand Swing: %anim_overhand% -> 0"
            PATCH_PRINT "  Backhand Swing: %anim_backhand% -> 0"
            PATCH_PRINT "  Thrust        : %anim_thrust% -> 0"
          END ELSE
          PATCH_IF ("%abil_type%" = 1) AND ((%anim_overhand% + %anim_backhand% + %anim_thrust%) != 100) BEGIN // Melee Ability
            PATCH_IF (("%type%" = 16)) AND // dagger
                     (("%anim_overhand%" != 0) OR
                      ("%anim_backhand%" != 20) OR
                      ("%anim_thrust%" != 80)) BEGIN
              SET "anim_overhand_new" = 0
              SET "anim_backhand_new" = 20
              SET "anim_thrust_new" = 80
              WRITE_SHORT ("%abil_off%" + 0x2c + (0x38 * "%index%")) 0
              WRITE_SHORT ("%abil_off%" + 0x30 + (0x38 * "%index%")) 20
              WRITE_SHORT ("%abil_off%" + 0x2e + (0x38 * "%index%")) 80
              PATCH_PRINT "%SOURCE_FILE% issue: Melee Animations have wrong probabilities! Setting Dagger probabilities..."
              PATCH_PRINT "  Overhand Swing: %anim_overhand% -> %anim_overhand_new%"
              PATCH_PRINT "  Backhand Swing: %anim_backhand% -> %anim_backhand_new%"
              PATCH_PRINT "  Thrust        : %anim_thrust% -> %anim_thrust_new%"
            END
            PATCH_IF (("%type%" = 19)) AND // short sword
                     (("%anim_overhand%" != 10) OR
                      ("%anim_backhand%" != 25) OR
                      ("%anim_thrust%" != 65)) BEGIN
              SET "anim_overhand_new" = 10
              SET "anim_backhand_new" = 25
              SET "anim_thrust_new" = 65
              WRITE_SHORT ("%abil_off%" + 0x2c + (0x38 * "%index%")) 10
              WRITE_SHORT ("%abil_off%" + 0x30 + (0x38 * "%index%")) 25
              WRITE_SHORT ("%abil_off%" + 0x2e + (0x38 * "%index%")) 65
              PATCH_PRINT "%SOURCE_FILE% issue: Melee Animations have wrong probabilities! Setting Short Sword probabilities..."
              PATCH_PRINT "  Overhand Swing: %anim_overhand% -> %anim_overhand_new%"
              PATCH_PRINT "  Backhand Swing: %anim_backhand% -> %anim_backhand_new%"
              PATCH_PRINT "  Thrust        : %anim_thrust% -> %anim_thrust_new%"
            END
            PATCH_IF (("%type%" = 17) OR // maces
                      ("%type%" = 18) OR // sling
                      ("%type%" = 20) OR // swords
                      ("%type%" = 21) OR // hammer
                      ("%type%" = 22) OR // morning stars
                      ("%type%" = 23) OR // flail
                      ("%type%" = 25) OR // axe
                      ("%type%" = 28)) AND // hand to hand
                     (("%flags%" BAND 0b00000010) != 0b00000010) AND // 1-handed
                     (("%anim_overhand%" != 50) OR
                      ("%anim_backhand%" != 50) OR
                      ("%anim_thrust%" != 0)) BEGIN
              SET "anim_overhand_new" = 50
              SET "anim_backhand_new" = 50
              SET "anim_thrust_new" = 0
              WRITE_SHORT ("%abil_off%" + 0x2c + (0x38 * "%index%")) 50
              WRITE_SHORT ("%abil_off%" + 0x30 + (0x38 * "%index%")) 50
              WRITE_SHORT ("%abil_off%" + 0x2e + (0x38 * "%index%")) 0
              PATCH_PRINT "%SOURCE_FILE% issue: Melee Animations have wrong probabilities! Setting Generic 1-Hander probabilities..."
              PATCH_PRINT "  Overhand Swing: %anim_overhand% -> %anim_overhand_new%"
              PATCH_PRINT "  Backhand Swing: %anim_backhand% -> %anim_backhand_new%"
              PATCH_PRINT "  Thrust        : %anim_thrust% -> %anim_thrust_new%"
            END
            PATCH_IF (("%type%" = 17) OR // maces
                      ("%type%" = 18) OR // sling
                      ("%type%" = 20) OR // swords
                      ("%type%" = 21) OR // hammer
                      ("%type%" = 22) OR // morning stars
                      ("%type%" = 23) OR // flail
                      ("%type%" = 25) OR // axe
                      ("%type%" = 28)) AND // hand to hand
                     (("%flags%" BAND 0b00000010) = 0b00000010) AND // 2-handed
                     (("%anim_overhand%" != 60) OR
                      ("%anim_backhand%" != 40) OR
                      ("%anim_thrust%" != 0)) BEGIN
              SET "anim_overhand_new" = 60
              SET "anim_backhand_new" = 40
              SET "anim_thrust_new" = 0
              WRITE_SHORT ("%abil_off%" + 0x2c + (0x38 * "%index%")) 60
              WRITE_SHORT ("%abil_off%" + 0x30 + (0x38 * "%index%")) 40
              WRITE_SHORT ("%abil_off%" + 0x2e + (0x38 * "%index%")) 0
              PATCH_PRINT "%SOURCE_FILE% issue: Melee Animations have wrong probabilities! Setting Generic 2-Hander probabilities..."
              PATCH_PRINT "  Overhand Swing: %anim_overhand% -> %anim_overhand_new%"
              PATCH_PRINT "  Backhand Swing: %anim_backhand% -> %anim_backhand_new%"
              PATCH_PRINT "  Thrust        : %anim_thrust% -> %anim_thrust_new%"
            END
            PATCH_IF (("%type%" = 30)) AND // halberd
                     (("%anim_overhand%" != 0) OR
                      ("%anim_backhand%" != 35) OR
                      ("%anim_thrust%" != 65)) BEGIN
              SET "anim_overhand_new" = 0
              SET "anim_backhand_new" = 25
              SET "anim_thrust_new" = 65
              WRITE_SHORT ("%abil_off%" + 0x2c + (0x38 * "%index%")) 0
              WRITE_SHORT ("%abil_off%" + 0x30 + (0x38 * "%index%")) 35
              WRITE_SHORT ("%abil_off%" + 0x2e + (0x38 * "%index%")) 65
              PATCH_PRINT "%SOURCE_FILE% issue: Melee Animations have wrong probabilities! Setting Halberd probabilities..."
              PATCH_PRINT "  Overhand Swing: %anim_overhand% -> %anim_overhand_new%"
              PATCH_PRINT "  Backhand Swing: %anim_backhand% -> %anim_backhand_new%"
              PATCH_PRINT "  Thrust        : %anim_thrust% -> %anim_thrust_new%"
            END
            PATCH_IF (("%type%" = 29)) AND // spear
                     (("%anim_overhand%" != 0) OR
                      ("%anim_backhand%" != 0) OR
                      ("%anim_thrust%" != 100)) BEGIN
              SET "anim_overhand_new" = 0
              SET "anim_backhand_new" = 0
              SET "anim_thrust_new" = 100
              WRITE_SHORT ("%abil_off%" + 0x2c + (0x38 * "%index%")) 0
              WRITE_SHORT ("%abil_off%" + 0x30 + (0x38 * "%index%")) 0
              WRITE_SHORT ("%abil_off%" + 0x2e + (0x38 * "%index%")) 100
              PATCH_PRINT "%SOURCE_FILE% issue: Melee Animations have wrong probabilities! Setting Spear probabilities..."
              PATCH_PRINT "  Overhand Swing: %anim_overhand% -> %anim_overhand_new%"
              PATCH_PRINT "  Backhand Swing: %anim_backhand% -> %anim_backhand_new%"
              PATCH_PRINT "  Thrust        : %anim_thrust% -> %anim_thrust_new%"
            END
            PATCH_IF (("%type%" = 26)) AND // staff
                     (("%anim_overhand%" != 20) OR
                      ("%anim_backhand%" != 70) OR
                      ("%anim_thrust%" != 10)) BEGIN
              SET "anim_overhand_new" = 20
              SET "anim_backhand_new" = 70
              SET "anim_thrust_new" = 10
              WRITE_SHORT ("%abil_off%" + 0x2c + (0x38 * "%index%")) 20
              WRITE_SHORT ("%abil_off%" + 0x30 + (0x38 * "%index%")) 70
              WRITE_SHORT ("%abil_off%" + 0x2e + (0x38 * "%index%")) 10
              PATCH_PRINT "%SOURCE_FILE% issue: Melee Animations have wrong probabilities! Setting Stave probabilities..."
              PATCH_PRINT "  Overhand Swing: %anim_overhand% -> %anim_overhand_new%"
              PATCH_PRINT "  Backhand Swing: %anim_backhand% -> %anim_backhand_new%"
              PATCH_PRINT "  Thrust        : %anim_thrust% -> %anim_thrust_new%"
            END
          END ELSE
          PATCH_IF ("%abil_type%" = 2) BEGIN // Ranged Ability (No ammunition or ammunition itself)
            PATCH_IF (("%type%" = 15) OR // bow
                      ("%type%" = 18) OR // sling
                      ("%type%" = 27)) AND // xbow
                     (("%anim_overhand%" != 0) OR
                      ("%anim_backhand%" != 0) OR
                      ("%anim_thrust%" != 0)) BEGIN
              SET "anim_overhand_new" = 0
              SET "anim_backhand_new" = 0
              SET "anim_thrust_new" = 0
              WRITE_SHORT ("%abil_off%" + 0x2c + (0x38 * "%index%")) 0
              WRITE_SHORT ("%abil_off%" + 0x30 + (0x38 * "%index%")) 0
              WRITE_SHORT ("%abil_off%" + 0x2e + (0x38 * "%index%")) 0
              PATCH_PRINT "%SOURCE_FILE% issue: Ranged Animations have wrong probabilities! Nulling probabilities..."
              PATCH_PRINT "  Overhand Swing: %anim_overhand% -> %anim_overhand_new%"
              PATCH_PRINT "  Backhand Swing: %anim_backhand% -> %anim_backhand_new%"
              PATCH_PRINT "  Thrust        : %anim_thrust% -> %anim_thrust_new%"
              PATCH_IF (("%type%" = 15) AND (("%is_arrow%" = 0) OR ("%is_bullet%" = 1) OR ("%is_bolt%" = 1))) BEGIN
                WRITE_SHORT ("%abil_off%" + 0x32 + (0x38 * "%index%")) 1
                WRITE_SHORT ("%abil_off%" + 0x34 + (0x38 * "%index%")) 0
                WRITE_SHORT ("%abil_off%" + 0x36 + (0x38 * "%index%")) 0
                PATCH_PRINT "%SOURCE_FILE% issue: Item is supposed to fire ARROWS!"
              END ELSE
              PATCH_IF (("%type%" = 18) AND (("%is_arrow%" = 1) OR ("%is_bullet%" = 0) OR ("%is_bolt%" = 1))) BEGIN
                WRITE_SHORT ("%abil_off%" + 0x32 + (0x38 * "%index%")) 0
                WRITE_SHORT ("%abil_off%" + 0x34 + (0x38 * "%index%")) 0
                WRITE_SHORT ("%abil_off%" + 0x36 + (0x38 * "%index%")) 1
                PATCH_PRINT "%SOURCE_FILE% issue: Item is supposed to fire BULLETS!"
              END ELSE
              PATCH_IF (("%type%" = 27) AND (("%is_arrow%" = 1) OR ("%is_bullet%" = 1) OR ("%is_bolt%" = 0))) BEGIN
                WRITE_SHORT ("%abil_off%" + 0x32 + (0x38 * "%index%")) 0
                WRITE_SHORT ("%abil_off%" + 0x34 + (0x38 * "%index%")) 1
                WRITE_SHORT ("%abil_off%" + 0x36 + (0x38 * "%index%")) 0
                PATCH_PRINT "%SOURCE_FILE% issue: Item is supposed to fire BOLTS!"
              END
            END
            PATCH_IF (("%type%" = 16) OR // dagger
                      ("%type%" = 17) OR // maces
                      ("%type%" = 19) OR // short sword
                      ("%type%" = 20) OR // swords
                      ("%type%" = 21) OR // hammer
                      ("%type%" = 22) OR // morning stars
                      ("%type%" = 23) OR // flail
                      ("%type%" = 24) OR // darts
                      ("%type%" = 25) OR // axe
                      ("%type%" = 26) OR // staff
                      ("%type%" = 28) OR // hand to hand
                      ("%type%" = 29) OR // Spear
                      ("%type%" = 30)) AND // halberd
                     ((%anim_overhand% + %anim_backhand% + %anim_thrust%) != 100) BEGIN
              SET "anim_overhand_new" = 100
              SET "anim_backhand_new" = 0
              SET "anim_thrust_new" = 0
              WRITE_SHORT ("%abil_off%" + 0x2c + (0x38 * "%index%")) 100
              WRITE_SHORT ("%abil_off%" + 0x30 + (0x38 * "%index%")) 0
              WRITE_SHORT ("%abil_off%" + 0x2e + (0x38 * "%index%")) 0
              PATCH_PRINT "%SOURCE_FILE% issue: Ranged Animations have wrong probabilities! Setting Generic Ranged probabilities..."
              PATCH_PRINT "  Overhand Swing: %anim_overhand% -> %anim_overhand_new%"
              PATCH_PRINT "  Backhand Swing: %anim_backhand% -> %anim_backhand_new%"
              PATCH_PRINT "  Thrust        : %anim_thrust% -> %anim_thrust_new%"
              PATCH_IF (("%type%" != 18) AND (("%is_arrow%" = 1) OR ("%is_bullet%" = 1) OR ("%is_bolt%" = 1))) BEGIN
                WRITE_SHORT ("%abil_off%" + 0x32 + (0x38 * "%index%")) 0
                WRITE_SHORT ("%abil_off%" + 0x34 + (0x38 * "%index%")) 0
                WRITE_SHORT ("%abil_off%" + 0x36 + (0x38 * "%index%")) 0
                PATCH_PRINT "%SOURCE_FILE% issue: Item is not a BOW/SLING/XBOW!"
              END
            END
          END ELSE
          PATCH_IF ("%abil_type%" = 4) BEGIN // Launcher Ability
            PATCH_IF (("%type%" = 15) OR // bow
                      ("%type%" = 18) OR // sling
                      ("%type%" = 27)) AND // xbow
                     (("%anim_overhand%" != 0) OR
                      ("%anim_backhand%" != 0) OR
                      ("%anim_thrust%" != 0)) BEGIN
              SET "anim_overhand_new" = 0
              SET "anim_backhand_new" = 0
              SET "anim_thrust_new" = 0
              WRITE_SHORT ("%abil_off%" + 0x2c + (0x38 * "%index%")) 0
              WRITE_SHORT ("%abil_off%" + 0x30 + (0x38 * "%index%")) 0
              WRITE_SHORT ("%abil_off%" + 0x2e + (0x38 * "%index%")) 0
              PATCH_PRINT "%SOURCE_FILE% issue: Launcher Animations have wrong probabilities! Nulling probabilities..."
              PATCH_PRINT "  Overhand Swing: %anim_overhand% -> %anim_overhand_new%"
              PATCH_PRINT "  Backhand Swing: %anim_backhand% -> %anim_backhand_new%"
              PATCH_PRINT "  Thrust        : %anim_thrust% -> %anim_thrust_new%"
              PATCH_IF ("%type%" = 15) AND (("%is_arrow%" = 0) OR ("%is_bullet%" = 1) OR ("%is_bolt%" = 1)) BEGIN
                WRITE_SHORT ("%abil_off%" + 0x32 + (0x38 * "%index%")) 1
                WRITE_SHORT ("%abil_off%" + 0x34 + (0x38 * "%index%")) 0
                WRITE_SHORT ("%abil_off%" + 0x36 + (0x38 * "%index%")) 0
                PATCH_PRINT "%SOURCE_FILE% issue: Item is supposed to fire ARROWS!"
              END ELSE
              PATCH_IF (("%type%" = 18) AND (("%is_arrow%" = 1) OR ("%is_bullet%" = 0) OR ("%is_bolt%" = 1))) BEGIN
                WRITE_SHORT ("%abil_off%" + 0x32 + (0x38 * "%index%")) 0
                WRITE_SHORT ("%abil_off%" + 0x34 + (0x38 * "%index%")) 0
                WRITE_SHORT ("%abil_off%" + 0x36 + (0x38 * "%index%")) 1
                PATCH_PRINT "%SOURCE_FILE% issue: Item is supposed to fire BULLETS!"
              END ELSE
              PATCH_IF ("%type%" = 27) AND (("%is_arrow%" = 1) OR ("%is_bullet%" = 1) OR ("%is_bolt%" = 0)) BEGIN
                WRITE_SHORT ("%abil_off%" + 0x32 + (0x38 * "%index%")) 0
                WRITE_SHORT ("%abil_off%" + 0x34 + (0x38 * "%index%")) 1
                WRITE_SHORT ("%abil_off%" + 0x36 + (0x38 * "%index%")) 0
                PATCH_PRINT "%SOURCE_FILE% issue: Item is supposed to fire BOLTS!"
              END
            END ELSE
            PATCH_IF (("%type%" = 16) OR // dagger
                      ("%type%" = 17) OR // maces
                      ("%type%" = 19) OR // short sword
                      ("%type%" = 20) OR // swords
                      ("%type%" = 21) OR // hammer
                      ("%type%" = 22) OR // morning stars
                      ("%type%" = 23) OR // flail
                      ("%type%" = 24) OR // darts
                      ("%type%" = 25) OR // axe
                      ("%type%" = 26) OR // staff
                      ("%type%" = 28) OR // hand to hand
                      ("%type%" = 29) OR // Spear
                      ("%type%" = 30)) AND // halberd
                     ((%anim_overhand% + %anim_backhand% + %anim_thrust%) != 100) BEGIN
              SET "anim_overhand_new" = 100
              SET "anim_backhand_new" = 0
              SET "anim_thrust_new" = 0
              WRITE_SHORT ("%abil_off%" + 0x2c + (0x38 * "%index%")) 100
              WRITE_SHORT ("%abil_off%" + 0x30 + (0x38 * "%index%")) 0
              WRITE_SHORT ("%abil_off%" + 0x2e + (0x38 * "%index%")) 0
              PATCH_PRINT "%SOURCE_FILE% issue: Launcher Animations have wrong probabilities! Setting Generic Ranged probabilities..."
              PATCH_PRINT "  Overhand Swing: %anim_overhand% -> %anim_overhand_new%"
              PATCH_PRINT "  Backhand Swing: %anim_backhand% -> %anim_backhand_new%"
              PATCH_PRINT "  Thrust        : %anim_thrust% -> %anim_thrust_new%"
              PATCH_IF (("%type%" != 18) AND (("%is_arrow%" = 1) OR ("%is_bullet%" = 1) OR ("%is_bolt%" = 1))) BEGIN
                WRITE_SHORT ("%abil_off%" + 0x32 + (0x38 * "%index%")) 0
                WRITE_SHORT ("%abil_off%" + 0x34 + (0x38 * "%index%")) 0
                WRITE_SHORT ("%abil_off%" + 0x36 + (0x38 * "%index%")) 0
                PATCH_PRINT "%SOURCE_FILE% issue: Item is not a BOW/SLING/XBOW!"
              END
            END
          END ELSE
          PATCH_IF ((%anim_overhand% + %anim_backhand% + %anim_thrust%) != 0) AND
                   ((%anim_overhand% + %anim_backhand% + %anim_thrust%) != 100) BEGIN
            WRITE_SHORT ("%abil_off%" + 0x2c + (0x38 * "%index%")) 0
            WRITE_SHORT ("%abil_off%" + 0x30 + (0x38 * "%index%")) 0
            WRITE_SHORT ("%abil_off%" + 0x2e + (0x38 * "%index%")) 0
            PATCH_PRINT "%SOURCE_FILE% issue: Improper Swing Probabilities! Setting to 0"
            PATCH_PRINT "  Overhand Swing: %anim_overhand% -> 0"
            PATCH_PRINT "  Backhand Swing: %anim_backhand% -> 0"
            PATCH_PRINT "  Thrust        : %anim_thrust% -> 0"
          END
        END
      END

      READ_LONG  0x6a "fx_off" ELSE 0
      READ_SHORT 0x6e "fx_idx" ELSE 0
      READ_SHORT 0x70 "fx_num" ELSE 0

      PATCH_IF (fx_off > 0x71) BEGIN
        FOR (index = fx_idx ; index < fx_num ; index = index + 1) BEGIN // check equipping effects
          READ_SHORT ("%fx_off%" +        ("%index%" * 0x30)) "opcode"
          READ_LONG  ("%fx_off%" + 0x04 + ("%index%" * 0x30)) "param1"
          READ_LONG  ("%fx_off%" + 0x08 + ("%index%" * 0x30)) "param2"

          PATCH_IF (("%opcode%" = 6)  OR
                    ("%opcode%" = 10) OR
                    ("%opcode%" = 15) OR
                    ("%opcode%" = 19) OR
                    ("%opcode%" = 44) OR
                    ("%opcode%" = 49)) AND ((("%param2%" = 1) OR ("%param2%" = 2)) AND ("%param1%" = 0)) BEGIN
            PATCH_PRINT "%SOURCE_FILE% issue: Stat Opcode (#%opcode%) setting to 0%! Incrementing by 1..."
            WRITE_LONG  ("%fx_off%" + 0x04 + ("%index%" * 0x30)) 1
            WRITE_LONG  ("%fx_off%" + 0x08 + ("%index%" * 0x30)) 0
          END
        END
      END
    END
  END
BUT_ONLY_IF_IT_CHANGES

BEGIN ~Item Stuff - Portrait Icon Assigner~
  GROUP ~Item Stuff~

COPY_EXISTING_REGEXP GLOB ~^.+\.itm$~ ~override~
  PATCH_IF (SOURCE_SIZE > 0x71) BEGIN
    READ_BYTE  0x18 "flags"

    PATCH_IF (("%flags%" BAND 0b00000100) = 0b00000100) BEGIN  // droppable

      // Equipped Effect Fixer
      READ_LONG  0x64 "abil_off" ELSE 0
      READ_SHORT 0x68 "abil_num" ELSE 0
      READ_LONG  0x6a "fx_off" ELSE 0
      READ_SHORT 0x6e "fx_idx" ELSE 0
      READ_SHORT 0x70 "fx_num" ELSE 0

      PATCH_IF (fx_off > 0x71) BEGIN
        PATCH_IF (("%flags%" BAND 0b00010000) = 0b00010000) BEGIN  // cursed
          SET "found" = 0
          FOR (index = fx_idx ; index < fx_num ; index = index + 1) BEGIN // check equipping effects
            READ_SHORT ("%fx_off%" +        ("%index%" * 0x30)) "eff_opcode"
            READ_LONG  ("%fx_off%" + 0x08 + ("%index%" * 0x30)) "eff_param2"
            PATCH_IF ("%eff_opcode%" = 142) AND ("%eff_param2%" = 35) BEGIN
              SET "found" = 1
            END
          END
          PATCH_IF ("%found%" = 0) BEGIN
            PATCH_PRINT "%SOURCE_FILE% issue: Adding Cursed Portrait..."
            LPF ADD_ITEM_EQEFFECT
              INT_VAR
                opcode     = 142
                target     = 1
                timing     = 2
                parameter2 = 35
            END
            READ_SHORT 0x70 "fx_num"
          END
        END

        FOR (index = fx_idx ; index < fx_num ; index = index + 1) BEGIN // check equipping effects
          READ_SHORT ("%fx_off%" +        ("%index%" * 0x30)) "opcode"
          READ_LONG  ("%fx_off%" + 0x04 + ("%index%" * 0x30)) "param1"
          READ_LONG  ("%fx_off%" + 0x08 + ("%index%" * 0x30)) "param2"

          PATCH_IF ("%opcode%" = 22) AND (((("%param2%" = 0) OR ("%param2%" = 1)) AND ("%param1%" < 0)) OR (("%param2%" = 2) AND ("%param1%" < 100))) BEGIN
            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              PATCH_IF ("%index1%" != "%index%") BEGIN
                READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
                READ_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) "eff_target"
                READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
                READ_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) "eff_timing"
                PATCH_IF ("%eff_opcode%" = 142) AND ("%eff_param2%" = 33) BEGIN
                  SET "found" = 1
                  PATCH_IF ("%eff_target%" != 1) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Bad Luck Portrait now displays on Self..."
                    WRITE_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) 1
                  END
                  PATCH_IF ("%eff_timing%" != 2) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Bad Luck Portrait now displays while equipped..."
                    WRITE_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) 2
                  END
                END
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding Bad Luck Portrait..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 142
                  target     = 1
                  timing     = 2
                  parameter2 = 33
              END
              READ_SHORT 0x70 "fx_num"
            END
          END

          PATCH_IF ("%opcode%" = 22) AND (((("%param2%" = 0) OR ("%param2%" = 1)) AND ("%param1%" > 0)) OR (("%param2%" = 2) AND ("%param1%" > 100))) BEGIN
            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              PATCH_IF ("%index1%" != "%index%") BEGIN
                READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
                READ_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) "eff_target"
                READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
                READ_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) "eff_timing"
                PATCH_IF ("%eff_opcode%" = 142) AND ("%eff_param2%" = 32) BEGIN
                  SET "found" = 1
                  PATCH_IF ("%eff_target%" != 1) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Good Luck Portrait now displays on Self..."
                    WRITE_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) 1
                  END
                  PATCH_IF ("%eff_timing%" != 2) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Good Luck Portrait now displays while equipped..."
                    WRITE_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) 2
                  END
                END
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding Good Luck Portrait..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 142
                  target     = 1
                  timing     = 2
                  parameter2 = 32
              END
              READ_SHORT 0x70 "fx_num"
            END
          END

          PATCH_IF ("%opcode%" = 27) AND (((("%param2%" = 0) OR ("%param2%" = 1)) AND ("%param1%" > 0)) OR (("%param2%" = 2) AND ("%param1%" > 100))) BEGIN
            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              PATCH_IF ("%index1%" != "%index%") BEGIN
                READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
                READ_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) "eff_target"
                READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
                READ_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) "eff_timing"
                PATCH_IF ("%eff_opcode%" = 142) AND ("%eff_param2%" = 24) BEGIN
                  SET "found" = 1
                  PATCH_IF ("%eff_target%" != 1) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Protection from Acid Portrait now displays on Self..."
                    WRITE_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) 1
                  END
                  PATCH_IF ("%eff_timing%" != 2) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Protection from Acid Portrait now displays while equipped..."
                    WRITE_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) 2
                  END
                END
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding Protection from Acid Portrait..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 142
                  target     = 1
                  timing     = 2
                  parameter2 = 24
              END
              READ_SHORT 0x70 "fx_num"
            END
          END

          PATCH_IF ("%opcode%" = 28) AND (((("%param2%" = 0) OR ("%param2%" = 1)) AND ("%param1%" > 0)) OR (("%param2%" = 2) AND ("%param1%" > 100))) BEGIN
            SET "foundboth" = 0
            SET "foundfireeffect" = 0
            SET "foundfireicon" = 0
            SET "foundcoldicon" = 0
            SET "coldiconindex" = 0
            SET "fireiconindex" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              PATCH_IF ("%index1%" != "%index%") BEGIN
                READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
                READ_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) "eff_target"
                READ_LONG  ("%fx_off%" + 0x04 + ("%index1%" * 0x30)) "eff_param1"
                READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
                READ_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) "eff_timing"
                PATCH_IF ("%eff_opcode%" = 30) AND (((("%eff_param2%" = 0) OR ("%eff_param2%" = 1)) AND ("%eff_param1%" > 0)) OR (("%eff_param2%" = 2) AND ("%eff_param1%" > 100))) BEGIN
                  SET "foundfireeffect" = 1
                END
                PATCH_IF ("%eff_opcode%" = 142) AND ("%eff_param2%" = 16) BEGIN
                  SET "foundfireicon" = 1
                  SET "fireiconindex" = "index1"
                  PATCH_IF ("%eff_target%" != 1) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Protection from Fire Portrait now displays on Self..."
                    WRITE_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) 1
                  END
                  PATCH_IF ("%eff_timing%" != 2) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Protection from Fire Portrait now displays while equipped..."
                    WRITE_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) 2
                  END
                END
                PATCH_IF ("%eff_opcode%" = 142) AND ("%eff_param2%" = 25) BEGIN
                  SET "foundcoldicon" = 1
                  SET "coldiconindex" = "index1"
                  PATCH_IF ("%eff_target%" != 1) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Protection from Cold Portrait now displays on Self..."
                    WRITE_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) 1
                  END
                  PATCH_IF ("%eff_timing%" != 2) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Protection from Cold Portrait now displays while equipped..."
                    WRITE_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) 2
                  END
                END
                PATCH_IF ("%eff_opcode%" = 142) AND ("%eff_param2%" = 26) BEGIN
                  SET "foundboth" = 1
                  PATCH_IF ("%eff_target%" != 1) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Resist Fire/Cold Portrait now displays on Self..."
                    WRITE_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) 1
                  END
                  PATCH_IF ("%eff_timing%" != 2) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Resist Fire/Cold Portrait now displays while equipped..."
                    WRITE_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) 2
                  END
                END
              END
            END
            PATCH_IF ("%foundboth%" = 0) AND ("%foundfireeffect%" = 1) BEGIN
              PATCH_IF ("%foundfireicon%" = 1) BEGIN
                PATCH_PRINT "%SOURCE_FILE% issue: Removing Protection from Fire Portrait..."
                offset = "%fx_off%" + ("%fireiconindex%" * 0x30)
                value  = 0x30
                DELETE_BYTES offset value
                SOURCE_SIZE = SOURCE_SIZE - value
                PATCH_IF ("%abil_off%" > "%fx_off%") BEGIN
                  SET "abil_off" = "abil_off" - 0x30
                  WRITE_LONG 0x64 %abil_off%
                END
                FOR (index2 = 0 ; index2 < abil_num ; index2 = index2 + 1) BEGIN
                  READ_SHORT  ("%abil_off%" + 0x20 + ("%index2%" * 0x38)) "abil_fx_idx"
                  PATCH_IF ("%abil_fx_idx%" > "%fireiconindex%") BEGIN
                    SET "abil_fx_idx" = "abil_fx_idx" - 1
                    WRITE_SHORT ("%abil_off%" + 0x20 + ("%index2%" * 0x38)) "abil_fx_idx"
                  END
                END
                PATCH_IF ("%coldiconindex%" > "%fireiconindex%") BEGIN
                  SET "coldiconindex" = "coldiconindex" - 1
                END
                SET "index1" = 0 - 1
                SET "fx_num" = "fx_num" - 1
                WRITE_SHORT 0x70 %fx_num%
              END
              PATCH_IF ("%foundcoldicon%" = 1) BEGIN
                PATCH_PRINT "%SOURCE_FILE% issue: Removing Protection from Cold Portrait..."
                offset = "%fx_off%" + ("%coldiconindex%" * 0x30)
                value  = 0x30
                DELETE_BYTES offset value
                SOURCE_SIZE = SOURCE_SIZE - value
                PATCH_IF ("%abil_off%" > "%fx_off%") BEGIN
                  SET "abil_off" = "abil_off" - 0x30
                  WRITE_LONG 0x64 %abil_off%
                END
                FOR (index2 = 0 ; index2 < abil_num ; index2 = index2 + 1) BEGIN
                  READ_SHORT  ("%abil_off%" + 0x20 + ("%index2%" * 0x38)) "abil_fx_idx"
                  PATCH_IF ("%abil_fx_idx%" > "%coldiconindex%") BEGIN
                    SET "abil_fx_idx" = "abil_fx_idx" - 1
                    WRITE_SHORT ("%abil_off%" + 0x20 + ("%index2%" * 0x38)) "abil_fx_idx"
                  END
                END
                PATCH_IF ("%fireiconindex%" > "%coldiconindex%") BEGIN
                  SET "fireiconindex" = "fireiconindex" - 1
                END
                SET "index1" = 0 - 1
                SET "fx_num" = "fx_num" - 1
                WRITE_SHORT 0x70 %fx_num%
              END
              PATCH_PRINT "%SOURCE_FILE% issue: Adding Resist Fire/Cold Portrait..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 142
                  target     = 1
                  timing     = 2
                  parameter2 = 26
              END
              READ_SHORT 0x70 "fx_num"
            END ELSE
            PATCH_IF ("%foundboth%" = 0) AND ("%foundcoldicon%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding Protection from Cold Portrait..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 142
                  target     = 1
                  timing     = 2
                  parameter2 = 25
              END
              READ_SHORT 0x70 "fx_num"
            END
          END

          PATCH_IF ("%opcode%" = 29) AND (((("%param2%" = 0) OR ("%param2%" = 1)) AND ("%param1%" > 0)) OR (("%param2%" = 2) AND ("%param1%" > 100))) BEGIN
            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              PATCH_IF ("%index1%" != "%index%") BEGIN
                READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
                READ_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) "eff_target"
                READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
                READ_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) "eff_timing"
                PATCH_IF ("%eff_opcode%" = 142) AND ("%eff_param2%" = 27) BEGIN
                  SET "found" = 1
                  PATCH_IF ("%eff_target%" != 1) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Protection from Electricity Portrait now displays on Self..."
                    WRITE_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) 1
                  END
                  PATCH_IF ("%eff_timing%" != 2) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Protection from Electricity Portrait now displays while equipped..."
                    WRITE_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) 2
                  END
                END
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding Protection from Electricity Portrait..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 142
                  target     = 1
                  timing     = 2
                  parameter2 = 27
              END
              READ_SHORT 0x70 "fx_num"
            END
          END

          PATCH_IF ("%opcode%" = 30) AND (((("%param2%" = 0) OR ("%param2%" = 1)) AND ("%param1%" > 0)) OR (("%param2%" = 2) AND ("%param1%" > 100))) BEGIN
            SET "foundboth" = 0
            SET "foundcoldeffect" = 0
            SET "foundcoldicon" = 0
            SET "foundfireicon" = 0
            SET "coldiconindex" = 0
            SET "fireiconindex" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              PATCH_IF ("%index1%" != "%index%") BEGIN
                READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
                READ_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) "eff_target"
                READ_LONG  ("%fx_off%" + 0x04 + ("%index1%" * 0x30)) "eff_param1"
                READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
                READ_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) "eff_timing"
                PATCH_IF ("%eff_opcode%" = 28) AND (((("%eff_param2%" = 0) OR ("%eff_param2%" = 1)) AND ("%eff_param1%" > 0)) OR (("%eff_param2%" = 2) AND ("%eff_param1%" > 100))) BEGIN
                  SET "foundcoldeffect" = 1
                END
                PATCH_IF ("%eff_opcode%" = 142) AND ("%eff_param2%" = 16) BEGIN
                  SET "foundfireicon" = 1
                  SET "fireiconindex" = "index1"
                  PATCH_IF ("%eff_target%" != 1) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Protection from Fire Portrait now displays on Self..."
                    WRITE_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) 1
                  END
                  PATCH_IF ("%eff_timing%" != 2) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Protection from Fire Portrait now displays while equipped..."
                    WRITE_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) 2
                  END
                END
                PATCH_IF ("%eff_opcode%" = 142) AND ("%eff_param2%" = 25) BEGIN
                  SET "foundcoldicon" = 1
                  SET "coldiconindex" = "index1"
                  PATCH_IF ("%eff_target%" != 1) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Protection from Cold Portrait now displays on Self..."
                    WRITE_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) 1
                  END
                  PATCH_IF ("%eff_timing%" != 2) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Protection from Cold Portrait now displays while equipped..."
                    WRITE_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) 2
                  END
                END
                PATCH_IF ("%eff_opcode%" = 142) AND ("%eff_param2%" = 26) BEGIN
                  SET "foundboth" = 1
                  PATCH_IF ("%eff_target%" != 1) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Resist Fire/Cold Portrait now displays on Self..."
                    WRITE_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) 1
                  END
                  PATCH_IF ("%eff_timing%" != 2) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Resist Fire/Cold Portrait now displays while equipped..."
                    WRITE_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) 2
                  END
                END
              END
            END
            PATCH_IF ("%foundboth%" = 0) AND ("%foundcoldeffect%" = 1) BEGIN
              PATCH_IF ("%foundfireicon%" = 1) BEGIN
                PATCH_PRINT "%SOURCE_FILE% issue: Removing Protection from Fire Portrait..."
                offset = "%fx_off%" + ("%fireiconindex%" * 0x30)
                value  = 0x30
                DELETE_BYTES offset value
                SOURCE_SIZE = SOURCE_SIZE - value
                PATCH_IF ("%abil_off%" > "%fx_off%") BEGIN
                  SET "abil_off" = "abil_off" - 0x30
                  WRITE_LONG 0x64 %abil_off%
                END
                FOR (index2 = 0 ; index2 < abil_num ; index2 = index2 + 1) BEGIN
                  READ_SHORT  ("%abil_off%" + 0x20 + ("%index2%" * 0x38)) "abil_fx_idx"
                  PATCH_IF ("%abil_fx_idx%" > "%fireiconindex%") BEGIN
                    SET "abil_fx_idx" = "abil_fx_idx" - 1
                    WRITE_SHORT ("%abil_off%" + 0x20 + ("%index2%" * 0x38)) "abil_fx_idx"
                  END
                END
                PATCH_IF ("%coldiconindex%" > "%fireiconindex%") BEGIN
                  SET "coldiconindex" = "coldiconindex" - 1
                END
                SET "index1" = 0 - 1
                SET "fx_num" = "fx_num" - 1
                WRITE_SHORT 0x70 %fx_num%
              END
              PATCH_IF ("%foundcoldicon%" = 1) BEGIN
                PATCH_PRINT "%SOURCE_FILE% issue: Removing Protection from Cold Portrait..."
                offset = "%fx_off%" + ("%coldiconindex%" * 0x30)
                value  = 0x30
                DELETE_BYTES offset value
                SOURCE_SIZE = SOURCE_SIZE - value
                PATCH_IF ("%abil_off%" > "%fx_off%") BEGIN
                  SET "abil_off" = "abil_off" - 0x30
                  WRITE_LONG 0x64 %abil_off%
                END
                FOR (index2 = 0 ; index2 < abil_num ; index2 = index2 + 1) BEGIN
                  READ_SHORT  ("%abil_off%" + 0x20 + ("%index2%" * 0x38)) "abil_fx_idx"
                  PATCH_IF ("%abil_fx_idx%" > "%coldiconindex%") BEGIN
                    SET "abil_fx_idx" = "abil_fx_idx" - 1
                    WRITE_SHORT ("%abil_off%" + 0x20 + ("%index2%" * 0x38)) "abil_fx_idx"
                  END
                END
                PATCH_IF ("%fireiconindex%" > "%coldiconindex%") BEGIN
                  SET "fireiconindex" = "fireiconindex" - 1
                END
                SET "index1" = 0 - 1
                SET "fx_num" = "fx_num" - 1
                WRITE_SHORT 0x70 %fx_num%
              END
              PATCH_PRINT "%SOURCE_FILE% issue: Adding Resist Fire/Cold Portrait..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 142
                  target     = 1
                  timing     = 2
                  parameter2 = 26
              END
              READ_SHORT 0x70 "fx_num"
            END ELSE
            PATCH_IF ("%foundboth%" = 0) AND ("%foundfireicon%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding Protection from Fire Portrait..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 142
                  target     = 1
                  timing     = 2
                  parameter2 = 16
              END
              READ_SHORT 0x70 "fx_num"
            END
          END

          PATCH_IF (("%opcode%" = 31) OR ("%opcode%" = 166)) AND (((("%param2%" = 0) OR ("%param2%" = 1)) AND ("%param1%" > 0)) OR (("%param2%" = 2) AND ("%param1%" > 100))) BEGIN
            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              PATCH_IF ("%index1%" != "%index%") BEGIN
                READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
                READ_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) "eff_target"
                READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
                READ_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) "eff_timing"
                PATCH_IF ("%eff_opcode%" = 142) AND (("%eff_param2%" = 28) OR ("%eff_param2%" = 63)) BEGIN
                  SET "found" = 1
                  PATCH_IF ("%eff_target%" != 1) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Protection from Magic Portrait now displays on Self..."
                    WRITE_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) 1
                  END
                  PATCH_IF ("%eff_timing%" != 2) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Protection from Magic Portrait now displays while equipped..."
                    WRITE_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) 2
                  END
                END
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding Protection from Magic Portrait..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 142
                  target     = 1
                  timing     = 2
                  parameter2 = 28
              END
              READ_SHORT 0x70 "fx_num"
            END
          END

          PATCH_IF ("%opcode%" = 48) BEGIN
            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              PATCH_IF ("%index1%" != "%index%") BEGIN
                READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
                READ_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) "eff_target"
                READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
                READ_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) "eff_timing"
                PATCH_IF ("%eff_opcode%" = 142) AND ("%eff_param2%" = 103) BEGIN
                  SET "found" = 1
                  PATCH_IF ("%eff_target%" != 1) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Vocalize Portrait now displays on Self..."
                    WRITE_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) 1
                  END
                  PATCH_IF ("%eff_timing%" != 2) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Vocalize Portrait now displays while equipped..."
                    WRITE_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) 2
                  END
                END
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding Vocalize Portrait..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 142
                  target     = 1
                  timing     = 2
                  parameter2 = 103
              END
              READ_SHORT 0x70 "fx_num"
            END
          END

          PATCH_IF ("%opcode%" = 63) BEGIN
            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              PATCH_IF ("%index1%" != "%index%") BEGIN
                READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
                READ_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) "eff_target"
                READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
                READ_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) "eff_timing"
                PATCH_IF ("%eff_opcode%" = 142) AND ("%eff_param2%" = 114) BEGIN
                  SET "found" = 1
                  PATCH_IF ("%eff_target%" != 1) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Infravision Portrait now displays on Self..."
                    WRITE_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) 1
                  END
                  PATCH_IF ("%eff_timing%" != 2) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Infravision Portrait now displays while equipped..."
                    WRITE_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) 2
                  END
                END
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding Infravision Portrait..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 142
                  target     = 1
                  timing     = 2
                  parameter2 = 114
              END
              READ_SHORT 0x70 "fx_num"
            END
          END

          PATCH_IF ("%opcode%" = 69) BEGIN
            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              PATCH_IF ("%index1%" != "%index%") BEGIN
                READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
                READ_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) "eff_target"
                READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
                READ_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) "eff_timing"
                PATCH_IF ("%eff_opcode%" = 142) AND ("%eff_param2%" = 31) BEGIN
                  SET "found" = 1
                  PATCH_IF ("%eff_target%" != 1) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Non-detection Portrait now displays on Self..."
                    WRITE_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) 1
                  END
                  PATCH_IF ("%eff_timing%" != 2) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Non-detection Portrait now displays while equipped..."
                    WRITE_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) 2
                  END
                END
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding Non-detection Portrait..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 142
                  target     = 1
                  timing     = 2
                  parameter2 = 31
              END
              READ_SHORT 0x70 "fx_num"
            END
          END

          PATCH_IF ("%opcode%" = 98) BEGIN
            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              PATCH_IF ("%index1%" != "%index%") BEGIN
                READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
                READ_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) "eff_target"
                READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
                READ_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) "eff_timing"
                PATCH_IF ("%eff_opcode%" = 142) AND (("%eff_param2%" = 42) OR ("%eff_param2%" = 56) OR ("%eff_param2%" = 87)) BEGIN
                  SET "found" = 1
                  PATCH_IF ("%eff_target%" != 1) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Regeneration Portrait now displays on Self..."
                    WRITE_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) 1
                  END
                  PATCH_IF ("%eff_timing%" != 2) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Regeneration Portrait now displays while equipped..."
                    WRITE_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) 2
                  END
                END
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding Regeneration Portrait..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 142
                  target     = 1
                  timing     = 2
                  parameter2 = 56
              END
              READ_SHORT 0x70 "fx_num"
            END
          END

          PATCH_IF ("%opcode%" = 101) AND ("%param2%" = 5) BEGIN
            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              PATCH_IF ("%index1%" != "%index%") BEGIN
                READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
                READ_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) "eff_target"
                READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
                READ_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) "eff_timing"
                PATCH_IF ("%eff_opcode%" = 142) AND ("%eff_param2%" = 52) BEGIN
                  SET "found" = 1
                  PATCH_IF ("%eff_target%" != 1) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Mind Shield Portrait now displays on Self..."
                    WRITE_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) 1
                  END
                  PATCH_IF ("%eff_timing%" != 2) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Mind Shield Portrait now displays while equipped..."
                    WRITE_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) 2
                  END
                END
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding Mind Shield Portrait..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 142
                  target     = 1
                  timing     = 2
                  parameter2 = 52
              END
              READ_SHORT 0x70 "fx_num"
            END
          END

          PATCH_IF ("%opcode%" = 163) BEGIN
            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              PATCH_IF ("%index1%" != "%index%") BEGIN
                READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
                READ_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) "eff_target"
                READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
                READ_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) "eff_timing"
                PATCH_IF ("%eff_opcode%" = 142) AND ("%eff_param2%" = 19) BEGIN
                  SET "found" = 1
                  PATCH_IF ("%eff_target%" != 1) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Free Action Portrait now displays on Self..."
                    WRITE_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) 1
                  END
                  PATCH_IF ("%eff_timing%" != 2) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Free Action Portrait now displays while equipped..."
                    WRITE_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) 2
                  END
                END
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding Free Action Portrait..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 142
                  target     = 1
                  timing     = 2
                  parameter2 = 19
              END
              READ_SHORT 0x70 "fx_num"
            END
          END

          PATCH_IF ("%opcode%" = 173) AND (((("%param2%" = 0) OR ("%param2%" = 1)) AND ("%param1%" > 0)) OR (("%param2%" = 2) AND ("%param1%" > 100))) BEGIN
            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              PATCH_IF ("%index1%" != "%index%") BEGIN
                READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
                READ_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) "eff_target"
                READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
                READ_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) "eff_timing"
                PATCH_IF ("%eff_opcode%" = 142) AND ("%eff_param2%" = 30) BEGIN
                  SET "found" = 1
                  PATCH_IF ("%eff_target%" != 1) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Protection from Poison Portrait now displays on Self..."
                    WRITE_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) 1
                  END
                  PATCH_IF ("%eff_timing%" != 2) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Protection from Poison Portrait now displays while equipped..."
                    WRITE_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) 2
                  END
                END
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding Protection from Poison Portrait..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 142
                  target     = 1
                  timing     = 2
                  parameter2 = 30
              END
              READ_SHORT 0x70 "fx_num"
            END
          END

          PATCH_IF ("%opcode%" = 176) AND (((("%param2%" = 0) OR ("%param2%" = 1)) AND ("%param1%" > 0)) OR (("%param2%" = 2) AND ("%param1%" > 100))) BEGIN
            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              PATCH_IF ("%index1%" != "%index%") BEGIN
                READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
                READ_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) "eff_target"
                READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
                READ_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) "eff_timing"
                PATCH_IF ("%eff_opcode%" = 142) AND ("%eff_param2%" = 38) BEGIN
                  SET "found" = 1
                  PATCH_IF ("%eff_target%" != 1) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Haste Portrait now displays on Self..."
                    WRITE_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) 1
                  END
                  PATCH_IF ("%eff_timing%" != 2) BEGIN
                    PATCH_PRINT "%SOURCE_FILE% issue: Haste Portrait now displays while equipped..."
                    WRITE_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) 2
                  END
                END
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding Haste Portrait..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 142
                  target     = 1
                  timing     = 2
                  parameter2 = 38
              END
              READ_SHORT 0x70 "fx_num"
            END
          END

        END
      END
    END
  END
BUT_ONLY_IF_IT_CHANGES

BEGIN ~Item Stuff - Proper Immunity Assigner~
  GROUP ~Item Stuff~

COPY_EXISTING_REGEXP GLOB ~^.+\.itm$~ ~override~
  PATCH_IF (SOURCE_SIZE > 0x71) BEGIN
    READ_BYTE  0x18 "flags"

    PATCH_IF (("%flags%" BAND 0b00000100) = 0b00000100) BEGIN  // droppable

      // Equipped Effect Fixer
      READ_LONG  0x6a "fx_off" ELSE 0
      READ_SHORT 0x6e "fx_idx" ELSE 0
      READ_SHORT 0x70 "fx_num" ELSE 0

      PATCH_IF (fx_off > 0x71) BEGIN
        FOR (index = fx_idx ; index < fx_num ; index = index + 1) BEGIN // check equipping effects
          READ_SHORT ("%fx_off%" +        ("%index%" * 0x30)) "opcode"
          READ_LONG  ("%fx_off%" + 0x04 + ("%index%" * 0x30)) "param1"
          READ_LONG  ("%fx_off%" + 0x08 + ("%index%" * 0x30)) "param2"
          READ_ASCII ("%fx_off%" + 0x14 + ("%index%" * 0x30)) "resref"

          // Charm
          PATCH_IF (("%opcode%" = 206) AND ("%resref%" STRING_EQUAL_CASE "SPPR204")) OR
                   (("%opcode%" = 206) AND ("%resref%" STRING_EQUAL_CASE "SPPR405")) OR
                   (("%opcode%" = 206) AND ("%resref%" STRING_EQUAL_CASE "SPWI104")) OR
                   (("%opcode%" = 206) AND ("%resref%" STRING_EQUAL_CASE "SPWI316")) OR
                   (("%opcode%" = 206) AND ("%resref%" STRING_EQUAL_CASE "SPWI506")) OR
                   (("%opcode%" = 101) AND ("%param2%" = 5)) BEGIN
            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
              PATCH_IF ("%eff_opcode%" = 101) AND ("%eff_param2%" = 5) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding Immunity to Charm..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 101
                  target     = 1
                  timing     = 2
                  parameter2 = 5
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
              PATCH_IF ("%eff_opcode%" = 142) AND ("%eff_param2%" = 52) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding Mind Shield Portrait..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 142
                  target     = 1
                  timing     = 2
                  parameter2 = 52
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
              PATCH_IF ("%eff_opcode%" = 169) AND ("%eff_param2%" = 0) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Preventing Charm Icon..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 169
                  target     = 1
                  timing     = 2
                  parameter2 = 0
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
              PATCH_IF ("%eff_opcode%" = 169) AND ("%eff_param2%" = 1) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Preventing Dire Charm Icon..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 169
                  target     = 1
                  timing     = 2
                  parameter2 = 1
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
              PATCH_IF ("%eff_opcode%" = 169) AND ("%eff_param2%" = 43) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Preventing Domination Icon..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 169
                  target     = 1
                  timing     = 2
                  parameter2 = 43
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x04 + ("%index1%" * 0x30)) "eff_param1"
              PATCH_IF ("%eff_opcode%" = 267) AND ("%eff_param1%" = 8364) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Disabling 'Dominated' String..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 267
                  target     = 1
                  timing     = 2
                  parameter1 = 8364
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x04 + ("%index1%" * 0x30)) "eff_param1"
              PATCH_IF ("%eff_opcode%" = 267) AND ("%eff_param1%" = 14672) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Disabling 'Charmed' String..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 267
                  target     = 1
                  timing     = 2
                  parameter1 = 14672
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x04 + ("%index1%" * 0x30)) "eff_param1"
              PATCH_IF ("%eff_opcode%" = 267) AND ("%eff_param1%" = 14780) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Disabling 'Dire Charmed' String..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 267
                  target     = 1
                  timing     = 2
                  parameter1 = 14780
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_ASCII ("%fx_off%" + 0x14 + ("%index1%" * 0x30)) "eff_resref"
              PATCH_IF ("%eff_opcode%" = 296) AND ("%eff_resref%" STRING_EQUAL_CASE "spnwchrm") BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Preventing Charm Animation..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 296
                  target     = 1
                  timing     = 2
                STR_VAR
                  resource   = "spnwchrm"
              END
              READ_SHORT 0x70 "fx_num"
            END
          END

          // Hold
          PATCH_IF (("%opcode%" = 206) AND ("%resref%" STRING_EQUAL_CASE "SPPR208")) OR
                   (("%opcode%" = 206) AND ("%resref%" STRING_EQUAL_CASE "SPWI306")) OR
                   (("%opcode%" = 101) AND ("%param2%" = 109)) OR
                   (("%opcode%" = 101) AND ("%param2%" = 175)) OR
                   (("%opcode%" = 101) AND ("%param2%" = 185)) BEGIN
            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
              PATCH_IF ("%eff_opcode%" = 101) AND ("%eff_param2%" = 109) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding Immunity to Hold..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 101
                  target     = 1
                  timing     = 2
                  parameter2 = 109
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
              PATCH_IF ("%eff_opcode%" = 101) AND ("%eff_param2%" = 175) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding Immunity to Hold Creature Type 1..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 101
                  target     = 1
                  timing     = 2
                  parameter2 = 175
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
              PATCH_IF ("%eff_opcode%" = 101) AND ("%eff_param2%" = 185) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding Immunity to Hold Creature Type 2..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 101
                  target     = 1
                  timing     = 2
                  parameter2 = 185
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
              PATCH_IF ("%eff_opcode%" = 169) AND ("%eff_param2%" = 13) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Preventing Held Icon..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 169
                  target     = 1
                  timing     = 2
                  parameter2 = 13
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x04 + ("%index1%" * 0x30)) "eff_param1"
              PATCH_IF ("%eff_opcode%" = 267) AND ("%eff_param1%" = 14102) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Disabling 'Held' String..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 267
                  target     = 1
                  timing     = 2
                  parameter1 = 14102
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_ASCII ("%fx_off%" + 0x14 + ("%index1%" * 0x30)) "eff_resref"
              PATCH_IF ("%eff_opcode%" = 296) AND ("%eff_resref%" STRING_EQUAL_CASE "spmindat") BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Preventing Hold Animation 1..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 296
                  target     = 1
                  timing     = 2
                STR_VAR
                  resource   = "spmindat"
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_ASCII ("%fx_off%" + 0x14 + ("%index1%" * 0x30)) "eff_resref"
              PATCH_IF ("%eff_opcode%" = 296) AND ("%eff_resref%" STRING_EQUAL_CASE "spflayer") BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Preventing Hold Animation 2..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 296
                  target     = 1
                  timing     = 2
                STR_VAR
                  resource   = "spflayer"
              END
              READ_SHORT 0x70 "fx_num"
            END
          END

          // Confusion
          PATCH_IF (("%opcode%" = 206) AND ("%resref%" STRING_EQUAL_CASE "SPPR709")) OR
                   (("%opcode%" = 206) AND ("%resref%" STRING_EQUAL_CASE "SPWI401")) OR
                   (("%opcode%" = 101) AND ("%param2%" = 128)) BEGIN
            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
              PATCH_IF ("%eff_opcode%" = 101) AND ("%eff_param2%" = 128) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding Immunity to Confusion..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 101
                  target     = 1
                  timing     = 2
                  parameter2 = 128
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
              PATCH_IF ("%eff_opcode%" = 169) AND ("%eff_param2%" = 2) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Preventing Rigid Thinking Icon..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 169
                  target     = 1
                  timing     = 2
                  parameter2 = 2
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
              PATCH_IF ("%eff_opcode%" = 169) AND ("%eff_param2%" = 3) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Preventing Confused Icon..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 169
                  target     = 1
                  timing     = 2
                  parameter2 = 3
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
              PATCH_IF ("%eff_opcode%" = 169) AND ("%eff_param2%" = 47) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Preventing Chaos Icon..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 169
                  target     = 1
                  timing     = 2
                  parameter2 = 47
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x04 + ("%index1%" * 0x30)) "eff_param1"
              PATCH_IF ("%eff_opcode%" = 267) AND ("%eff_param1%" = 14782) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Disabling 'Confused' String..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 267
                  target     = 1
                  timing     = 2
                  parameter1 = 14782
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x04 + ("%index1%" * 0x30)) "eff_param1"
              PATCH_IF ("%eff_opcode%" = 267) AND ("%eff_param1%" = 14791) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Disabling 'Rigid Thinking' String..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 267
                  target     = 1
                  timing     = 2
                  parameter1 = 14791
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_ASCII ("%fx_off%" + 0x14 + ("%index1%" * 0x30)) "eff_resref"
              PATCH_IF ("%eff_opcode%" = 296) AND ("%eff_resref%" STRING_EQUAL_CASE "spconfus") BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Preventing Confusion Animation..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 296
                  target     = 1
                  timing     = 2
                STR_VAR
                  resource   = "spconfus"
              END
              READ_SHORT 0x70 "fx_num"
            END
          END

          // Feeblemind
          PATCH_IF (("%opcode%" = 206) AND ("%resref%" STRING_EQUAL_CASE "SPWI509")) OR
                   (("%opcode%" = 101) AND ("%param2%" = 76)) BEGIN
            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
              PATCH_IF ("%eff_opcode%" = 101) AND ("%eff_param2%" = 76) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding Immunity to Feeblemindedness..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 101
                  target     = 1
                  timing     = 2
                  parameter2 = 76
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
              PATCH_IF ("%eff_opcode%" = 169) AND ("%eff_param2%" = 48) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Preventing Feebleminded Icon..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 169
                  target     = 1
                  timing     = 2
                  parameter2 = 48
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_ASCII ("%fx_off%" + 0x14 + ("%index1%" * 0x30)) "eff_resref"
              PATCH_IF ("%eff_opcode%" = 296) AND ("%eff_resref%" STRING_EQUAL_CASE "spmindat") BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Preventing Feebleminded Animation..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 296
                  target     = 1
                  timing     = 2
                STR_VAR
                  resource   = "spmindat"
              END
              READ_SHORT 0x70 "fx_num"
            END
          END

          // Horror
          PATCH_IF (("%opcode%" = 206) AND ("%resref%" STRING_EQUAL_CASE "SPWI205")) OR
                   (("%opcode%" = 101) AND ("%param2%" = 23)) OR
                   (("%opcode%" = 101) AND ("%param2%" = 24)) OR
                   (("%opcode%" = 101) AND ("%param2%" = 106)) BEGIN
            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              PATCH_IF ("%eff_opcode%" = 23) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding effect to Reset Morale..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 23
                  target     = 1
                  timing     = 2
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
              PATCH_IF ("%eff_opcode%" = 101) AND ("%eff_param2%" = 23) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding Immunity to Morale Reset..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 101
                  target     = 1
                  timing     = 2
                  parameter2 = 23
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
              PATCH_IF ("%eff_opcode%" = 101) AND ("%eff_param2%" = 24) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding Immunity to Panic..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 101
                  target     = 1
                  timing     = 2
                  parameter2 = 24
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
              PATCH_IF ("%eff_opcode%" = 101) AND ("%eff_param2%" = 106) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding Immunity to Morale Break..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 101
                  target     = 1
                  timing     = 2
                  parameter2 = 106
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
              PATCH_IF ("%eff_opcode%" = 142) AND ("%eff_param2%" = 37) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding Resist Fear Portrait..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 142
                  target     = 1
                  timing     = 2
                  parameter2 = 37
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              PATCH_IF ("%eff_opcode%" = 161) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding effect to Remove Fear..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 161
                  target     = 1
                  timing     = 2
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
              PATCH_IF ("%eff_opcode%" = 169) AND ("%eff_param2%" = 36) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Preventing Panic Icon..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 169
                  target     = 1
                  timing     = 2
                  parameter2 = 36
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
              PATCH_IF ("%eff_opcode%" = 240) AND ("%eff_param2%" = 36) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Adding effect to Remove Panic Icon..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 240
                  target     = 1
                  timing     = 2
                  parameter2 = 36
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x04 + ("%index1%" * 0x30)) "eff_param1"
              PATCH_IF ("%eff_opcode%" = 267) AND ("%eff_param1%" = 14007) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Disabling 'Panic' String..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 267
                  target     = 1
                  timing     = 2
                  parameter1 = 14007
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x04 + ("%index1%" * 0x30)) "eff_param1"
              PATCH_IF ("%eff_opcode%" = 267) AND ("%eff_param1%" = 17427) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Disabling 'Panic' String..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 267
                  target     = 1
                  timing     = 2
                  parameter1 = 17427
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x04 + ("%index1%" * 0x30)) "eff_param1"
              PATCH_IF ("%eff_opcode%" = 267) AND ("%eff_param1%" = 20568) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Disabling 'Morale Failure: Panic' String..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 267
                  target     = 1
                  timing     = 2
                  parameter1 = 20568
              END
              READ_SHORT 0x70 "fx_num"
            END

            SET "found" = 0
            FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
              READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
              READ_LONG  ("%fx_off%" + 0x04 + ("%index1%" * 0x30)) "eff_param1"
              PATCH_IF ("%eff_opcode%" = 106) AND ("%eff_param1%" = 1) BEGIN
                SET "found" = 1
              END
            END
            PATCH_IF ("%found%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Preventing morale failure..."
              LPF ADD_ITEM_EQEFFECT
                INT_VAR
                  opcode     = 106
                  target     = 1
                  timing     = 2
                  parameter1 = 1
                  parameter2 = 1
              END
              READ_SHORT 0x70 "fx_num"
            END

            PATCH_IF FILE_EXISTS_IN_GAME ~cdhorror.vvc~ BEGIN
              SET "found" = 0
              FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN // check equipping effects
                READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
                READ_ASCII ("%fx_off%" + 0x14 + ("%index1%" * 0x30)) "eff_resref"
                PATCH_IF ("%eff_opcode%" = 296) AND ("%eff_resref%" STRING_EQUAL_CASE "cdhorror") BEGIN
                  SET "found" = 1
                END
              END
              PATCH_IF ("%found%" = 0) BEGIN
                PATCH_PRINT "%SOURCE_FILE% issue: Preventing BG2Fixpack confusion Animation..."
                LPF ADD_ITEM_EQEFFECT
                  INT_VAR
                    opcode     = 296
                    target     = 1
                    timing     = 2
                  STR_VAR
                    resource   = "cdhorror"
                END
                READ_SHORT 0x70 "fx_num"
              END
            END
          END

        END
      END
    END
  END
BUT_ONLY_IF_IT_CHANGES

BEGIN ~Item Stuff - Duplicate Item Effect Remover~
  GROUP ~Item Stuff~

COPY_EXISTING_REGEXP GLOB ~^.+\.itm$~ ~override~
  PATCH_IF (SOURCE_SIZE > 0x71) THEN BEGIN
    READ_LONG  0x64 "abil_off" ELSE 0
    READ_SHORT 0x68 "abil_num" ELSE 0
    READ_LONG  0x6a "fx_off" ELSE 0
    READ_SHORT 0x6e "fx_idx" ELSE 0
    READ_SHORT 0x70 "fx_num" ELSE 0

    PATCH_IF (abil_off > 0x71) AND (fx_off > 0x71) THEN BEGIN
      FOR (index = fx_idx ; index < fx_num ; index = index + 1) BEGIN
        READ_SHORT ("%fx_off%" +        ("%index%" * 0x30)) "opcode"
        READ_BYTE  ("%fx_off%" + 0x02 + ("%index%" * 0x30)) "target"
        READ_BYTE  ("%fx_off%" + 0x03 + ("%index%" * 0x30)) "power"
        READ_LONG  ("%fx_off%" + 0x04 + ("%index%" * 0x30)) "param1"
        READ_LONG  ("%fx_off%" + 0x08 + ("%index%" * 0x30)) "param2"
        READ_BYTE  ("%fx_off%" + 0x0c + ("%index%" * 0x30)) "timing"
        READ_BYTE  ("%fx_off%" + 0x0d + ("%index%" * 0x30)) "dispel"
        READ_LONG  ("%fx_off%" + 0x0e + ("%index%" * 0x30)) "duration"
        READ_BYTE  ("%fx_off%" + 0x12 + ("%index%" * 0x30)) "prob1"
        READ_BYTE  ("%fx_off%" + 0x13 + ("%index%" * 0x30)) "prob2"
        READ_ASCII ("%fx_off%" + 0x14 + ("%index%" * 0x30)) "resref"
        READ_LONG  ("%fx_off%" + 0x1c + ("%index%" * 0x30)) "diccnt"
        READ_LONG  ("%fx_off%" + 0x20 + ("%index%" * 0x30)) "dicsid"
        READ_LONG  ("%fx_off%" + 0x24 + ("%index%" * 0x30)) "savtyp"
        READ_LONG  ("%fx_off%" + 0x28 + ("%index%" * 0x30)) "savbon"
        FOR (index1 = fx_idx ; index1 < fx_num ; index1 = index1 + 1) BEGIN
          PATCH_IF ("%index1%" != "%index%") BEGIN
            READ_SHORT ("%fx_off%" +        ("%index1%" * 0x30)) "eff_opcode"
            READ_BYTE  ("%fx_off%" + 0x02 + ("%index1%" * 0x30)) "eff_target"
            READ_BYTE  ("%fx_off%" + 0x03 + ("%index1%" * 0x30)) "eff_power"
            READ_LONG  ("%fx_off%" + 0x04 + ("%index1%" * 0x30)) "eff_param1"
            READ_LONG  ("%fx_off%" + 0x08 + ("%index1%" * 0x30)) "eff_param2"
            READ_BYTE  ("%fx_off%" + 0x0c + ("%index1%" * 0x30)) "eff_timing"
            READ_BYTE  ("%fx_off%" + 0x0d + ("%index1%" * 0x30)) "eff_dispel"
            READ_LONG  ("%fx_off%" + 0x0e + ("%index1%" * 0x30)) "eff_duration"
            READ_BYTE  ("%fx_off%" + 0x12 + ("%index1%" * 0x30)) "eff_prob1"
            READ_BYTE  ("%fx_off%" + 0x13 + ("%index1%" * 0x30)) "eff_prob2"
            READ_ASCII ("%fx_off%" + 0x14 + ("%index1%" * 0x30)) "eff_resref"
            READ_LONG  ("%fx_off%" + 0x1c + ("%index1%" * 0x30)) "eff_diccnt"
            READ_LONG  ("%fx_off%" + 0x20 + ("%index1%" * 0x30)) "eff_dicsid"
            READ_LONG  ("%fx_off%" + 0x24 + ("%index1%" * 0x30)) "eff_savtyp"
            READ_LONG  ("%fx_off%" + 0x28 + ("%index1%" * 0x30)) "eff_savbon"
            PATCH_IF ("%opcode%" = "%eff_opcode%") AND
                     ("%target%" = "%eff_target%") AND
                     ("%power%" = "%eff_power%") AND
                     ("%param1%" = "%eff_param1%") AND
                     ("%param2%" = "%eff_param2%") AND
                     ("%timing%" = "%eff_timing%") AND
                     ("%dispel%" = "%eff_dispel%") AND
                     ("%duration%" = "%eff_duration%") AND
                     ("%prob1%" = "%eff_prob1%") AND
                     ("%prob2%" = "%eff_prob2%") AND
                     ("%resref%" STRING_EQUAL_CASE "%eff_resref%") AND
                     ("%diccnt%" = "%eff_diccnt%") AND
                     ("%dicsid%" = "%eff_dicsid%") AND
                     ("%savtyp%" = "%eff_savtyp%") AND
                     ("%savbon%" = "%eff_savbon%") BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Effect #%index1% (Opcode %opcode%) matches Effect #%index%! Removing..."
              offset = "%fx_off%" + ("%index1%" * 0x30)
              value  = 0x30
              DELETE_BYTES offset value
              SOURCE_SIZE = SOURCE_SIZE - value
              PATCH_IF ("%abil_off%" > "%fx_off%") BEGIN
                SET "abil_off" = "abil_off" - 0x30
                WRITE_LONG 0x64 %abil_off%
              END
              FOR (index2 = 0 ; index2 < abil_num ; index2 = index2 + 1) BEGIN
                READ_SHORT  ("%abil_off%" + 0x20 + ("%index2%" * 0x38)) "abil_fx_idx"
                PATCH_IF ("%abil_fx_idx%" > "%index1%") BEGIN
                  SET "abil_fx_idx" = "abil_fx_idx" - 1
                  WRITE_SHORT ("%abil_off%" + 0x20 + ("%index2%" * 0x38)) "abil_fx_idx"
                END
              END
              SET "index1" = 0 - 1
              SET "fx_num" = "fx_num" - 1
              WRITE_SHORT 0x70 %fx_num%
            END
          END
        END
      END
    END
  END
BUT_ONLY_IF_IT_CHANGES

BEGIN ~Item Stuff - MAJESTIC Item Checker~
  GROUP ~Item Stuff~

COPY_EXISTING_REGEXP GLOB ~^.+\.itm$~ ~override~
  PATCH_IF (SOURCE_SIZE > 0x71) BEGIN
    READ_BYTE  0x18 "flags"

    PATCH_IF (("%flags%" BAND 0b00000100) = 0b00000100) BEGIN  // droppable
      READ_LONG  0x6a "fx_off" ELSE 0
      READ_SHORT 0x6e "fx_idx" ELSE 0
      READ_SHORT 0x70 "fx_num" ELSE 0

      // Effect Missing Resource Finder
      READ_LONG  0x64 "abil_off" ELSE 0
      READ_SHORT 0x68 "abil_num" ELSE 0
      READ_LONG  0x6a "fx_off" ELSE 0
      READ_SHORT 0x6e "fx_idx" ELSE 0
      READ_SHORT 0x70 "fx_num" ELSE 0

      PATCH_IF ((fx_off > 0x71) AND (abil_off > 0x71)) BEGIN
        FOR (index = 0; index < abil_num; index = index + 1) BEGIN
          READ_SHORT  ("%abil_off%" + 0x1e + ("%index%" * 0x38)) "abil_fx_num"
          READ_SHORT  ("%abil_off%" + 0x20 + ("%index%" * 0x38)) "abil_fx_idx"
          FOR (index2 = 0 ; index2 < abil_fx_num ; index2 = index2 + 1) BEGIN
            READ_SHORT ("%fx_off%" +        (0x30 * ("%abil_fx_idx%" + "%index2%"))) "opcode"
            READ_BYTE  ("%fx_off%" + 0x02 + (0x30 * ("%abil_fx_idx%" + "%index2%"))) "target"
            READ_LONG  ("%fx_off%" + 0x04 + (0x30 * ("%abil_fx_idx%" + "%index2%"))) "param1"
            READ_LONG  ("%fx_off%" + 0x08 + (0x30 * ("%abil_fx_idx%" + "%index2%"))) "param2"
            READ_BYTE  ("%fx_off%" + 0x0c + (0x30 * ("%abil_fx_idx%" + "%index2%"))) "timing"
            READ_ASCII ("%fx_off%" + 0x14 + (0x30 * ("%abil_fx_idx%" + "%index2%"))) "resref"
            SET "exists" = 0
            PATCH_IF ("%opcode%" = 67) OR
                     ("%opcode%" = 135) OR
                     ("%opcode%" = 151) OR
                     ("%opcode%" = 168) BEGIN
              PATCH_IF FILE_EXISTS_IN_GAME ~%resref%.cre~ BEGIN
                SET "exists" = 1
              END
              PATCH_IF ("%exists%" = 0) BEGIN
                PATCH_PRINT "%SOURCE_FILE% issue: Ability #%index% Effect #%index2% Opcode (%opcode%) - Non-existent Resource (%resref%.CRE)"
              END
            END
            SET "exists" = 0
            PATCH_IF ("%opcode%" = 107) BEGIN
              PATCH_IF FILE_EXISTS_IN_GAME ~%resref%.bmp~ BEGIN
                SET "exists" = 1
              END
              PATCH_IF ("%exists%" = 0) BEGIN
                PATCH_PRINT "%SOURCE_FILE% issue: Ability #%index% Effect #%index2% Opcode (%opcode%) - Non-existent Resource (%resref%.BMP)"
              END
            END
            SET "exists" = 0
            PATCH_IF ("%opcode%" = 111) OR
                     ("%opcode%" = 112) OR
                     ("%opcode%" = 122) OR
                     ("%opcode%" = 143) BEGIN
              PATCH_IF FILE_EXISTS_IN_GAME ~%resref%.itm~ BEGIN
                SET "exists" = 1
              END
              PATCH_IF ("%exists%" = 0) BEGIN
                PATCH_PRINT "%SOURCE_FILE% issue: Ability #%index% Effect #%index2% Opcode (%opcode%) - Non-existent Resource (%resref%.ITM)"
              END
            END
            SET "exists" = 0
            PATCH_IF ("%opcode%" = 146) OR
                     ("%opcode%" = 147) OR
                     ("%opcode%" = 148) OR
                     ("%opcode%" = 171) OR
                     ("%opcode%" = 172) OR
                     ("%opcode%" = 207) OR
                     ("%opcode%" = 232) OR
                     ("%opcode%" = 266) BEGIN
              PATCH_IF FILE_EXISTS_IN_GAME ~%resref%.spl~ BEGIN
                SET "exists" = 1
              END
              PATCH_IF ("%exists%" = 0) BEGIN
                PATCH_PRINT "%SOURCE_FILE% issue: Ability #%index% Effect #%index2% Opcode (%opcode%) - Non-existent Resource (%resref%.SPL)"
              END
            END
            SET "exists" = 0
            PATCH_IF ("%opcode%" = 152) BEGIN
              PATCH_IF FILE_EXISTS_IN_GAME ~%resref%.mve~ BEGIN
                SET "exists" = 1
              END
              PATCH_IF ("%exists%" = 0) BEGIN
                PATCH_PRINT "%SOURCE_FILE% issue: Ability #%index% Effect #%index2% Opcode (%opcode%) - Non-existent Resource (%resref%.MVE)"
              END
            END
            SET "exists" = 0
            PATCH_IF ("%opcode%" = 174) BEGIN
              PATCH_IF FILE_EXISTS_IN_GAME ~%resref%.wav~ BEGIN
                SET "exists" = 1
              END
              PATCH_IF ("%exists%" = 0) BEGIN
                PATCH_PRINT "%SOURCE_FILE% issue: Ability #%index% Effect #%index2% Opcode (%opcode%) - Non-existent Resource (%resref%.WAV)"
              END
            END
            SET "exists" = 0
            PATCH_IF ("%opcode%" = 177) OR
                     ("%opcode%" = 248) OR
                     ("%opcode%" = 249) BEGIN
              PATCH_IF FILE_EXISTS_IN_GAME ~%resref%.eff~ BEGIN
                SET "exists" = 1
              END
              PATCH_IF ("%exists%" = 0) BEGIN
                PATCH_PRINT "%SOURCE_FILE% issue: Ability #%index% Effect #%index2% Opcode (%opcode%) - Non-existent Resource (%resref%.EFF)"
              END
            END
            SET "exists" = 0
            PATCH_IF ("%opcode%" = 186) BEGIN
              PATCH_IF FILE_EXISTS_IN_GAME ~%resref%.are~ BEGIN
                SET "exists" = 1
              END
              PATCH_IF ("%exists%" = 0) BEGIN
                PATCH_PRINT "%SOURCE_FILE% issue: Ability #%index% Effect #%index2% Opcode (%opcode%) - Non-existent Resource (%resref%.ARE)"
              END
            END
          END
        END
        FOR (index = 0; index < fx_num; index = index + 1) BEGIN
          READ_SHORT ("%fx_off%" +        (0x30 * "%index%")) "opcode"
          READ_BYTE  ("%fx_off%" + 0x02 + (0x30 * "%index%")) "target"
          READ_LONG  ("%fx_off%" + 0x04 + (0x30 * "%index%")) "param1"
          READ_LONG  ("%fx_off%" + 0x08 + (0x30 * "%index%")) "param2"
          READ_BYTE  ("%fx_off%" + 0x0c + (0x30 * "%index%")) "timing"
          READ_ASCII ("%fx_off%" + 0x14 + (0x30 * "%index%")) "resref"
          PATCH_IF (("%opcode%" = 27) OR
                    ("%opcode%" = 28) OR
                    ("%opcode%" = 29) OR
                    ("%opcode%" = 30) OR
                    ("%opcode%" = 31) OR
                    ("%opcode%" = 33) OR
                    ("%opcode%" = 34) OR
                    ("%opcode%" = 35) OR
                    ("%opcode%" = 36) OR
                    ("%opcode%" = 37)) AND ("%param2%" = 1) AND ("%param1%" < 100) BEGIN
            PATCH_PRINT "%SOURCE_FILE% issue: Equipped Effect #%index% Opcode (%opcode%) - Value sets to %param1%"
          END
          PATCH_IF (("%opcode%" = 27) OR
                    ("%opcode%" = 28) OR
                    ("%opcode%" = 29) OR
                    ("%opcode%" = 30) OR
                    ("%opcode%" = 31) OR
                    ("%opcode%" = 33) OR
                    ("%opcode%" = 34) OR
                    ("%opcode%" = 35) OR
                    ("%opcode%" = 36) OR
                    ("%opcode%" = 37)) AND ("%param2%" = 2) AND ("%param1%" = 100) BEGIN
            PATCH_PRINT "%SOURCE_FILE% issue: Equipped Effect #%index% Opcode (%opcode%) - Non-functional percentage change"
          END
          SET "exists" = 0
          PATCH_IF ("%opcode%" = 67) OR
                   ("%opcode%" = 135) OR
                   ("%opcode%" = 151) OR
                   ("%opcode%" = 168) BEGIN
            PATCH_IF FILE_EXISTS_IN_GAME ~%resref%.cre~ BEGIN
              SET "exists" = 1
            END
            PATCH_IF ("%exists%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Equipped Effect #%index% Opcode (%opcode%) - Non-existent Resource (%resref%.CRE)"
            END
          END
          SET "exists" = 0
          PATCH_IF ("%opcode%" = 107) BEGIN
            PATCH_IF FILE_EXISTS_IN_GAME ~%resref%.bmp~ BEGIN
              SET "exists" = 1
            END
            PATCH_IF ("%exists%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Equipped Effect #%index% Opcode (%opcode%) - Non-existent Resource (%resref%.BMP)"
            END
          END
          SET "exists" = 0
          PATCH_IF ("%opcode%" = 111) OR
                   ("%opcode%" = 112) OR
                   ("%opcode%" = 122) OR
                   ("%opcode%" = 143) BEGIN
            PATCH_IF FILE_EXISTS_IN_GAME ~%resref%.itm~ BEGIN
              SET "exists" = 1
            END
            PATCH_IF ("%exists%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Equipped Effect #%index% Opcode (%opcode%) - Non-existent Resource (%resref%.ITM)"
            END
          END
          SET "exists" = 0
          PATCH_IF ("%opcode%" = 146) OR
                   ("%opcode%" = 147) OR
                   ("%opcode%" = 148) OR
                   ("%opcode%" = 171) OR
                   ("%opcode%" = 172) OR
                   ("%opcode%" = 207) OR
                   ("%opcode%" = 232) OR
                   ("%opcode%" = 266) BEGIN
            PATCH_IF FILE_EXISTS_IN_GAME ~%resref%.spl~ BEGIN
              SET "exists" = 1
            END
            PATCH_IF ("%exists%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Equipped Effect #%index% Opcode (%opcode%) - Non-existent Resource (%resref%.SPL)"
            END
          END
          SET "exists" = 0
          PATCH_IF ("%opcode%" = 152) BEGIN
            PATCH_IF FILE_EXISTS_IN_GAME ~%resref%.mve~ BEGIN
              SET "exists" = 1
            END
            PATCH_IF ("%exists%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Equipped Effect #%index% Opcode (%opcode%) - Non-existent Resource (%resref%.MVE)"
            END
          END
          SET "exists" = 0
          PATCH_IF ("%opcode%" = 174) BEGIN
            PATCH_IF FILE_EXISTS_IN_GAME ~%resref%.wav~ BEGIN
              SET "exists" = 1
            END
            PATCH_IF ("%exists%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Equipped Effect #%index% Opcode (%opcode%) - Non-existent Resource (%resref%.WAV)"
            END
          END
          SET "exists" = 0
          PATCH_IF ("%opcode%" = 177) OR
                   ("%opcode%" = 248) OR
                   ("%opcode%" = 249) BEGIN
            PATCH_IF FILE_EXISTS_IN_GAME ~%resref%.eff~ BEGIN
              SET "exists" = 1
            END
            PATCH_IF ("%exists%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: Equipped Effect #%index% Opcode (%opcode%) - Non-existent Resource (%resref%.EFF)"
            END
          END
          SET "exists" = 0
          PATCH_IF ("%opcode%" = 186) BEGIN
            PATCH_IF FILE_EXISTS_IN_GAME ~%resref%.are~ BEGIN
              SET "exists" = 1
            END
            PATCH_IF ("%exists%" = 0) BEGIN
              PATCH_PRINT "%SOURCE_FILE% issue: AEquipped Effect #%index% Opcode (%opcode%) - Non-existent Resource (%resref%.ARE)"
            END
          END
        END
      END
    END
  END
BUT_ONLY_IF_IT_CHANGES

BEGIN ~Spell Stuff - Spell Resource Fixer~
  GROUP ~Spell Stuff~

COPY_EXISTING_REGEXP GLOB ~^.+\.spl$~ ~override~
  PATCH_IF (SOURCE_SIZE > 0x71) BEGIN
    READ_LONG  0x64 "abil_off"
    READ_SHORT 0x68 "abil_num"
    READ_LONG  0x6a "fx_off"
    READ_SHORT 0x70 "fx_num"
    PATCH_IF ((abil_off > fx_off) AND (abil_num > 1)) BEGIN
      READ_ASCII abil_off ~eh~ ELSE ~fail~ (0x28 * abil_num)
      PATCH_IF (~%eh%~ STRING_EQUAL ~fail~) BEGIN
        WHILE ((~%eh%~ STRING_EQUAL ~fail~) AND (abil_num > 0)) BEGIN
          READ_ASCII abil_off ~eh~ ELSE ~fail~ (0x28 * abil_num)
          abil_num -= 1
        END
      END
      offset = abil_off
      value  = 0x28 * abil_num
      PATCH_PRINT "%SOURCE_FILE% issue: Restructuring - Unused Ability Structure found! Trimming %value% bytes from offset %offset%..."
      DELETE_BYTES offset value
      SOURCE_SIZE = SOURCE_SIZE - value
      abil_off = 0x72
      WRITE_LONG 0x64 abil_off
      WRITE_SHORT 0x68 abil_num
      fx_off = (0x72 + (0x28 * abil_num))
      WRITE_LONG 0x6a fx_off
      PATCH_IF !(~%eh%~ STRING_EQUAL ~fail~) BEGIN
        INSERT_BYTES abil_off (0x28 * abil_num)
        WRITE_ASCIIE abil_off ~%eh%~
      END
    END ELSE PATCH_IF ((abil_off != 0x72) AND (abil_num = 0)) BEGIN
      abil_off = 0x72
      WRITE_LONG 0x64 abil_off
    END
    FOR (i1 = 0; i1 < (0x28 * abil_num); i1 += 0x28) BEGIN
      WRITE_SHORT (abil_off + i1 + 0x20) fx_num
      READ_SHORT (abil_off + i1 + 0x1e) cx
      fx_num += cx
    END
    PATCH_IF ((abil_off + (0x28 * abil_num)) < fx_off) BEGIN
      offset = abil_off + (0x28 * abil_num)
      value  = fx_off - ((abil_off + (0x28 * abil_num)))
      PATCH_PRINT "%SOURCE_FILE% issue: Restructuring - Unused Inter-structural Bytes found! Trimming %value% bytes from offset %offset%..."
      DELETE_BYTES offset value
      SOURCE_SIZE = SOURCE_SIZE - value
      fx_off = offset
      WRITE_LONG 0x6a %fx_off%
    END
    PATCH_IF (SOURCE_SIZE > (0x72 + (0x28 * abil_num) + (0x30 * fx_num))) BEGIN
      offset = 0x72 + (0x28 * abil_num) + (0x30 * fx_num)
      value  = SOURCE_SIZE - (0x72 + (0x28 * abil_num) + (0x30 * fx_num))
      PATCH_PRINT "%SOURCE_FILE% issue: Restructuring - Unused Trailing Bytes found! Trimming %value% bytes from offset %offset%..."
      DELETE_BYTES offset value
      SOURCE_SIZE = SOURCE_SIZE - value
    END
  END
BUT_ONLY_IF_IT_CHANGES

BEGIN ~Spell Stuff - MAJESTIC Spell Fixer~
  GROUP ~Spell Stuff~

COPY_EXISTING_REGEXP GLOB ~^.+\.spl$~ ~override~
  PATCH_IF (SOURCE_SIZE > 0x71) BEGIN

    // Innate Fixer
    READ_SHORT 0x1c "spell_type"
    READ_LONG  0x34 "spell_level"

    PATCH_IF (%spell_type% = 4) BEGIN
      PATCH_IF (%spell_level% != 1) BEGIN
        PATCH_PRINT "%SOURCE_FILE% issue: Innate Spell is not Level 1! Setting level to 1..."
        WRITE_LONG 0x34 ~1~
      END
    END

    READ_LONG  0x64 "abil_off" ELSE 0
    READ_SHORT 0x68 "abil_num" ELSE 0

    PATCH_IF (abil_off > 0x71) BEGIN
      FOR (index = 0; index < abil_num; index = index + 1) BEGIN
        READ_BYTE  ("%abil_off%" + 0x02 + (0x28 * "%index%")) "slot"
        READ_BYTE  ("%abil_off%" + 0x0c + (0x28 * "%index%")) "target"

        // Ability Target Fixer
        PATCH_IF (("%target%"  = 2) OR ("%target%" = 6)) BEGIN
          PATCH_PRINT "%SOURCE_FILE% issue: Ability #%index% target may crash! (was %target%) Setting to self..."
          WRITE_BYTE  ("%abil_off%" + 0x0c + (0x28 * "%index%")) 1
        END

        // Spell Slot Fixer
        PATCH_IF (("%spell_type%"  = 4) AND ("%slot%" != 4)) BEGIN
          PATCH_IF ("%slot%" = 1) BEGIN
            PATCH_PRINT "%SOURCE_FILE% issue: Ability #%index% assigned to INNATE slot (was WEAPON)..."
          END ELSE
          PATCH_IF ("%slot%" = 2) BEGIN
            PATCH_PRINT "%SOURCE_FILE% issue: Ability #%index% assigned to INNATE slot (was SPELL)..."
          END ELSE
          PATCH_IF ("%slot%" = 3) BEGIN
            PATCH_PRINT "%SOURCE_FILE% issue: Ability #%index% assigned to INNATE slot (was EQUIPMENT)..."
          END ELSE
          BEGIN
            PATCH_PRINT "%SOURCE_FILE% issue: Ability #%index% assigned to INNATE slot (was %slot%)..."
          END
          WRITE_BYTE  ("%abil_off%" + 0x02 + (0x28 * "%index%")) 4
        END
        PATCH_IF (("%spell_type%" != 4) AND ("%slot%" != 2)) BEGIN
          PATCH_IF ("%slot%" = 1) BEGIN
            PATCH_PRINT "%SOURCE_FILE% issue: Ability #%index% assigned to SPELL slot (was WEAPON)..."
          END ELSE
          PATCH_IF ("%slot%" = 3) BEGIN
            PATCH_PRINT "%SOURCE_FILE% issue: Ability #%index% assigned to SPELL slot (was EQUIPMENT)..."
          END ELSE
          PATCH_IF ("%slot%" = 4) BEGIN
            PATCH_PRINT "%SOURCE_FILE% issue: Ability #%index% assigned to SPELL slot (was INNATE)..."
          END ELSE
          BEGIN
            PATCH_PRINT "%SOURCE_FILE% issue: Ability #%index% assigned to SPELL slot (was %slot%)..."
          END
          WRITE_BYTE  ("%abil_off%" + 0x02 + (0x28 * "%index%")) 2
        END
      END
    END

    // Effect Target Fixer
    READ_LONG  0x6a "fx_off"
    READ_SHORT 0x6e "fx_idx"
    READ_SHORT 0x70 "fx_num"

    PATCH_IF (fx_off > 0x71) BEGIN
      FOR (index = fx_idx ; index < fx_num ; index = index + 1) BEGIN // check equipping effects
        READ_SHORT ("%fx_off%" +        ("%index%" * 0x30)) "opcode"
        READ_BYTE  ("%fx_off%" + 0x02 + ("%index%" * 0x30)) "target"
        PATCH_IF ("%target%" = 2) BEGIN
          PATCH_PRINT "%SOURCE_FILE% issue: Casting Effect #%index% (opcode %opcode%) targets preset target! Targetting self..."
          WRITE_BYTE  ("%fx_off%" + 0x02 + ("%index%" * 0x30)) 1
        END
      END
    END
  END
BUT_ONLY_IF_IT_CHANGES

// horred the plague's absolutely awesomerific BCS patching code for innate crashes

APPEND ~TRIGGER.IDS~ ~0x00A7 SpellCastInnateRES(S:Spell*,O:Object*)~
              UNLESS ~0x00A7 SpellCastInnateRES(S:Spell\*,O:Object\*)~

COPY_EXISTING_REGEXP GLOB ~.*\.bcs$~ ~override~
  DECOMPILE_AND_PATCH BEGIN
    REPLACE_EVALUATE CASE_INSENSITIVE ~SpellCastInnate(\(.*\),\([^0].*\))~
    BEGIN
      TEXT_SPRINT z "WASTEFUL"
      TEXT_SPRINT bs "0"
      TEXT_SPRINT whynow "%MATCH2%"
      PATCH_IF ("%whynow%" STRING_EQUAL_CASE "0" != 1) BEGIN
        y = IDS_OF_SYMBOL(~spell~ ~%MATCH2%~)
        PATCH_IF (%y% > 3999) BEGIN
          y -= 4000
          PATCH_IF (y < 10) BEGIN
            TEXT_SPRINT z "SPCL00%y%"
          END ELSE PATCH_IF (y > 9) AND (y <100) BEGIN
            TEXT_SPRINT z "SPCL0%y%"
          END ELSE BEGIN
            TEXT_SPRINT z "SPCL%y%"
          END
        END ELSE PATCH_IF (%y% > 2999 AND y < 4000) BEGIN
          y -= 3000
          PATCH_IF (y < 10) BEGIN
            TEXT_SPRINT z "SPIN00%y%"
          END ELSE PATCH_IF (y > 9) AND (y <100) BEGIN
            TEXT_SPRINT z "SPIN0%y%"
          END ELSE BEGIN
            TEXT_SPRINT z "SPIN%y%"
          END
        END
      END ELSE BEGIN
        PATCH_PRINT "%SOURCE_FILE%:  Modder chose I:Spell = 0, and it slipped through the cracks somehow."
        TEXT_SPRINT z "BUGGED"
      END
      PATCH_PRINT "%SOURCE_FILE% issue: Replacing SpellCastInnate(%MATCH1%,%MATCH2%) with SpellCastInnateRES('%z%',%MATCH1%)"
    END
    ~SpellCastInnateRES("%z%",%MATCH1%)~
  END
BUT_ONLY_IF_IT_CHANGES

BEGIN ~Store Stuff - MAJESTIC Store Fixer~
  GROUP ~Store Stuff~

COPY_EXISTING_REGEXP GLOB ~^.+\.sto$~ ~override~
  PATCH_IF (SOURCE_SIZE > 0x9b) THEN BEGIN

    READ_LONG  0x08 "type"

    // Sales Manager
    READ_BYTE  0x10 "flags"
    READ_LONG  0x30 "purch_num"
    READ_LONG  0x38 "sales_num"
    READ_LONG  0x50 "drink_num"
    READ_LONG  0x74 "cures_num"

    PATCH_IF (("%flags%" BAND 0b00000001) = 0b00000000) AND ("%sales_num%" > 0) AND ("%type%" = 0) BEGIN
      PATCH_PRINT "%SOURCE_FILE% issue: Stocked Items exist but store set to not sell! Initiating sales..."
      WRITE_BYTE 0x10 ("%flags%" BOR 0b00000001)
    END
    PATCH_IF (("%flags%" BAND 0b00000010) = 0b00000000) AND ("%purch_num%" > 0) AND ("%type%" = 0) BEGIN
      PATCH_PRINT "%SOURCE_FILE% issue: Purchasable Items exist but store set to not buy! Initiating sales..."
      WRITE_BYTE 0x10 ("%flags%" BOR 0b00000010)
    END
    PATCH_IF (("%flags%" BAND 0b00100000) = 0b00000000) AND ("%cures_num%" > 0) AND ("%type%" = 3) BEGIN
      PATCH_PRINT "%SOURCE_FILE% issue: Stocked Cures exist but temple set to not sell! Initiating sales..."
      WRITE_BYTE 0x10 ("%flags%" BOR 0b00100000)
    END
    PATCH_IF (("%flags%" BAND 0b01000000) = 0b00000000) AND ("%drink_num%" > 0) AND ("%type%" = 1) BEGIN
      PATCH_PRINT "%SOURCE_FILE% issue: Stocked Drinks exist but tavern set to not sell! Initiating sales..."
      WRITE_BYTE 0x10 ("%flags%" BOR 0b01000000)
    END
    PATCH_IF (("%flags%" BAND 0b01000000) = 0b00000000) AND ("%drink_num%" > 0) AND ("%type%" = 2) BEGIN
      PATCH_PRINT "%SOURCE_FILE% issue: Stocked Drinks exist but inn set to not sell! Initiating sales..."
      WRITE_BYTE 0x10 ("%flags%" BOR 0b01000000)
    END

    PATCH_IF (("%flags%" BAND 0b00000001) = 0b00000001) AND ("%sales_num%" = 0) AND ("%type%" = 0) BEGIN
      PATCH_PRINT "%SOURCE_FILE% issue: Store selling items but does not have any in stock! Stopping sales..."
      WRITE_BYTE 0x10 ("%flags%" BAND 0b11111110)
    END
    PATCH_IF (("%flags%" BAND 0b00000010) = 0b00000010) AND ("%purch_num%" = 0) AND ("%type%" = 0) BEGIN
      PATCH_PRINT "%SOURCE_FILE% issue: Store buying items but cannot buy any kind! Stopping sales..."
      WRITE_BYTE 0x10 ("%flags%" BAND 0b11111101)
    END
    PATCH_IF (("%flags%" BAND 0b00100000) = 0b00100000) AND ("%cures_num%" = 0) AND ("%type%" = 3) BEGIN
      PATCH_PRINT "%SOURCE_FILE% issue: Temple selling cures but does not have any in stock! Stopping sales..."
      WRITE_BYTE 0x10 ("%flags%" BAND 0b11011111)
    END
    PATCH_IF (("%flags%" BAND 0b01000000) = 0b01000000) AND ("%drink_num%" = 0) AND ("%type%" = 1) BEGIN
      PATCH_PRINT "%SOURCE_FILE% issue: Tavern selling drinks but does not have any in stock! Stopping sales..."
      WRITE_BYTE 0x10 ("%flags%" BAND 0b10111111)
    END
    PATCH_IF (("%flags%" BAND 0b01000000) = 0b01000000) AND ("%drink_num%" = 0) AND ("%type%" = 2) BEGIN
      PATCH_PRINT "%SOURCE_FILE% issue: Inn selling drinks but does not have any in stock! Stopping sales..."
      WRITE_BYTE 0x10 ("%flags%" BAND 0b10111111)
    END

    // Missing Item Remover
    READ_LONG  0x34 "sales_off" ELSE 0
    READ_LONG  0x38 "sales_num" ELSE 0

    PATCH_IF (sales_off > 0x9b) THEN BEGIN
      FOR (index = 0 ; index < sales_num ; index = index + 1) BEGIN
        READ_ASCII ("%sales_off%" +        (0x1c * "%index%")) "resref"
        PATCH_IF NOT (FILE_EXISTS_IN_GAME ~%resref%.itm~) BEGIN
          PATCH_PRINT "%SOURCE_FILE% issue: Stocked Item #%index% does not exist (%resref%.itm)! Removing item..."
          REMOVE_STORE_ITEM ~%resref%~
          SET "index" = 0 - 1
          READ_LONG  0x38 "sales_num"
        END
      END
    END
  END
BUT_ONLY_IF_IT_CHANGES
