DEFINE_ACTION_FUNCTION install_scroll_icons
BEGIN
  // Scrolls for standard spells use spXXXXXa.bam

  COPY_EXISTING_REGEXP "^.*\.itm$" "override"
    LAUNCH_PATCH_FUNCTION ITM_read_category
      RET
      item_type	= value
    END

    PATCH_IF item_type = 0x0b
    BEGIN
      GET_OFFSET_ARRAY ab_array ITM_V10_HEADERS
      PHP_EACH ab_array AS ab_num => ab_offset
      BEGIN
        GET_OFFSET_ARRAY2 fx_array ab_offset ITM_V10_HEAD_EFFECTS
        PHP_EACH fx_array AS fx_num => fx_offset
        BEGIN
          READ_SHORT fx_offset opcode
	  PATCH_IF opcode = 146 OR opcode = 147 OR opcode = 148
	  BEGIN
	    READ_ASCII (fx_offset + 0x0014) scroll_spell

	    PATCH_IF FILE_EXISTS_IN_GAME "%scroll_spell%a.bam"
	    BEGIN
	      WRITE_ASCII (0x003a) "%scroll_spell%a" #8
	      WRITE_ASCII (ab_offset + 0x0004) "%scroll_spell%a" #8
	    END
	  END
        END
      END
    END

    BUT_ONLY_IF_IT_CHANGES


  // Protection from X scrolls are treated specially

  ACTION_DEFINE_ASSOCIATIVE_ARRAY protection_scrolls
  BEGIN
    "scroll_protection_from_acid"		=> "scrl03"
    "scroll_protection_from_cold"		=> "scrl04"
    "scroll_protection_from_electricity"	=> "scrl05"
    "scroll_protection_from_fire"		=> "scrl06"
    "scroll_protection_from_magic"		=> "scrl07"
    "scroll_protection_from_poison"		=> "scrl08"
    "scroll_protection_from_undead"		=> "scrl09"
    "scroll_protection_from_petrification"	=> "scrl15"
    "scroll_unpetrify"				=> "scrlpet"
  END

  ACTION_PHP_EACH protection_scrolls AS scroll_name => item_name
  BEGIN
    ACTION_IF FILE_EXISTS_IN_GAME "%item_name%.itm"
    BEGIN
      COPY_LARGE "%MOD_FOLDER%/%component_loc%/scroll_icons/%scroll_name%.bam"
                 "override/%item_name%.bam"

      MAKE_PATCH
        icon=>"%item_name%"
	patch_ability_inline=>~ability_icon=>"%item_name%"~
      END

      LAUNCH_ACTION_FUNCTION edit_item
        STR_VAR
	item	= "%item_name%"
	edits	= "patch_data"
      END
    END 
  END
END	// install_scroll_icons


