// This component adds additional spell sequencer spells.


// Add Spell / Scroll Icons
COPY "%MOD_FOLDER%/Components/101-BonusSequencers/icon/" override


// ---- START Add Simple Sequencer ----
// If Spell Revisions is installed, remove the mage specialization restrictions and use a streamlined description
ACTION_IF (MOD_IS_INSTALLED "SETUP-SPELL_REV.TP2" 0) BEGIN
ADD_SPELL "%MOD_FOLDER%/Components/101-BonusSequencers/spell/ZUSQ01.SPL" 2 3 ZU_SIMPLE_SEQUENCER
	SAY NAME1 @10103
	SAY UNIDENTIFIED_DESC @10104
	SAY NAME2 @10103
	SAY DESC @10104
	WRITE_LONG 0x1e 0
	WRITE_BYTE 0x25 9
END ELSE BEGIN
// If Spell Revisions is not installed, use a lengthier description
ADD_SPELL "%MOD_FOLDER%/Components/101-BonusSequencers/spell/ZUSQ01.SPL" 2 3 ZU_SIMPLE_SEQUENCER
	SAY NAME1 @10101
	SAY UNIDENTIFIED_DESC @10102
	SAY NAME2 @10101
	SAY DESC @10102
END

// Since the above function slots the spell in wherever it will fit, load the resource name it was assigned
LAF RES_NAME_OF_SPELL_NUM
	INT_VAR
		spell_num = ZU_SIMPLE_SEQUENCER
	RET
		spell_res
END

// Copy the two placeholder spells using the assigned resource name
COPY_EXISTING "SPWI420D.SPL" "override/%spell_res%D.SPL"
	SAY NAME1 @10101
	SAY UNIDENTIFIED_DESC @10101
	SAY NAME2 @10101
	SAY DESC @10101
	WRITE_ASCII 0x76 "ZUSQ01B" #8 // Change icon
	WRITE_ASCII 0xae "%spell_res%" #8 // Effect 1 resource target
	WRITE_ASCII 0xde "%spell_res%D" #8 // Effect 2 resource target

COPY_EXISTING "SPWI420P.SPL" "override/%spell_res%P.SPL"
	SAY NAME1 @10101
	SAY UNIDENTIFIED_DESC @10101
	SAY NAME2 @10101
	SAY DESC @10101
	WRITE_ASCII 0x76 "ZUSQ01B" #8 // Change icon
	WRITE_ASCII 0xae "%spell_res%" #8 // Effect 1 resource target
	WRITE_ASCII 0xde "%spell_res%P" #8 // Effect 2 resource target

// Add the relevant scroll and assign the spell resource to the relevant effects
// If Spell Revisions is installed, allow Enchanters to use the scroll
ACTION_IF (MOD_IS_INSTALLED "SETUP-SPELL_REV.TP2" 0) BEGIN
COPY "%MOD_FOLDER%/Components/101-BonusSequencers/items/ZUSQSC01.ITM" override
	SAY NAME1 @10103
	SAY UNIDENTIFIED_DESC @10104
	SAY NAME2 @10103
	SAY DESC @10104
	WRITE_BYTE 0x2d 0
	WRITE_ASCII 0xf6 "%spell_res%" #8
	WRITE_ASCII 0x126 "%spell_res%" #8
END ELSE BEGIN
COPY "%MOD_FOLDER%/Components/101-BonusSequencers/items/ZUSQSC01.ITM" override
	SAY NAME1 @10101
	SAY UNIDENTIFIED_DESC @10102
	SAY NAME2 @10101
	SAY DESC @10102
	WRITE_ASCII 0xf6 "%spell_res%" #8
	WRITE_ASCII 0x126 "%spell_res%" #8
END

// Add a 'magebookstrings' entry in 'baldur.lua' 
COPY_EXISTING "bgee.lua" override
  REPLACE_TEXTUALLY CASE_INSENSITIVE EXACT_MATCH ~mageBookStrings = {~
  ~mageBookStrings = {
      %spell_res% = {tip = 60420, title = 'Simple Sequencer', action = "ADD_SPELLS_SEQUENCER_LABEL"},~
// ---- END Add Simple Sequencer ----



// ---- START Add Lesser Sequencer ----
// If Spell Revisions is installed, remove the mage specialization restrictions and use a streamlined description
ACTION_IF (MOD_IS_INSTALLED "SETUP-SPELL_REV.TP2" 0) BEGIN
ADD_SPELL "%MOD_FOLDER%/Components/101-BonusSequencers/spell/ZUSQ02.SPL" 2 5 ZU_LESSER_SEQUENCER
	SAY NAME1 @10107
	SAY UNIDENTIFIED_DESC @10108
	SAY NAME2 @10107
	SAY DESC @10108
	WRITE_LONG 0x1e 0
	WRITE_BYTE 0x25 9
END ELSE BEGIN
// If Spell Revisions is not installed, use a lengthier description
ADD_SPELL "%MOD_FOLDER%/Components/101-BonusSequencers/spell/ZUSQ02.SPL" 2 5 ZU_LESSER_SEQUENCER
	SAY NAME1 @10105
	SAY UNIDENTIFIED_DESC @10106
	SAY NAME2 @10105
	SAY DESC @10106
END

// Since the above function slots the spell in wherever it will fit, load the resource name it was assigned
LAF RES_NAME_OF_SPELL_NUM
	INT_VAR
		spell_num = ZU_LESSER_SEQUENCER
	RET
		spell_res
END

// Copy the two placeholder spells using the assigned resource name
COPY_EXISTING "SPWI420D.SPL" "override/%spell_res%D.SPL"
	SAY NAME1 @10105
	SAY UNIDENTIFIED_DESC @10105
	SAY NAME2 @10105
	SAY DESC @10105
	WRITE_ASCII 0x76 "ZUSQ02B" #8 // Change icon
	WRITE_ASCII 0xae "%spell_res%" #8 // Effect 1 resource target
	WRITE_ASCII 0xde "%spell_res%D" #8 // Effect 2 resource target

COPY_EXISTING "SPWI420P.SPL" "override/%spell_res%P.SPL"
	SAY NAME1 @10105
	SAY UNIDENTIFIED_DESC @10105
	SAY NAME2 @10105
	SAY DESC @10105
	WRITE_ASCII 0x76 "ZUSQ02B" #8 // Change icon
	WRITE_ASCII 0xae "%spell_res%" #8 // Effect 1 resource target
	WRITE_ASCII 0xde "%spell_res%P" #8 // Effect 2 resource target

// Add the relevant scroll and assign the spell resource to the relevant effects
// If Spell Revisions is installed, allow Enchanters to use the scroll
ACTION_IF (MOD_IS_INSTALLED "SETUP-SPELL_REV.TP2" 0) BEGIN
COPY "%MOD_FOLDER%/Components/101-BonusSequencers/items/ZUSQSC02.ITM" override
	SAY NAME1 @10107
	SAY UNIDENTIFIED_DESC @10108
	SAY NAME2 @10107
	SAY DESC @10108
	WRITE_BYTE 0x2d 0
	WRITE_ASCII 0xf6 "%spell_res%" #8
	WRITE_ASCII 0x126 "%spell_res%" #8
END ELSE BEGIN
COPY "%MOD_FOLDER%/Components/101-BonusSequencers/items/ZUSQSC02.ITM" override
	SAY NAME1 @10105
	SAY UNIDENTIFIED_DESC @10106
	SAY NAME2 @10105
	SAY DESC @10106
	WRITE_ASCII 0xf6 "%spell_res%" #8
	WRITE_ASCII 0x126 "%spell_res%" #8
END

// Add a 'magebookstrings' entry in 'baldur.lua' 
COPY_EXISTING "bgee.lua" override
  REPLACE_TEXTUALLY CASE_INSENSITIVE EXACT_MATCH ~mageBookStrings = {~
  ~mageBookStrings = {
      %spell_res% = {tip = 60420, title = 'Lesser Sequencer', action = "ADD_SPELLS_SEQUENCER_LABEL"},~
// ---- END Add Simple Sequencer ----



// ---- START Add Scrolls to the World ----
// High Hedge
COPY_EXISTING "HIGHHEDG.sto" override
    ADD_STORE_ITEM "ZUSQSC01" AFTER "SCRL6G" #0 #0 #0 "IDENTIFIED" #1

// Sorcerous Sundries
COPY_EXISTING "STO0703.sto" override
    ADD_STORE_ITEM "ZUSQSC01" AFTER "SCRL1S" #0 #0 #0 "IDENTIFIED" #1
    ADD_STORE_ITEM "ZUSQSC02" AFTER "SCRL5K" #0 #0 #0 "IDENTIFIED" #1

// Sorcerous Sundries (SoD)
COPY_EXISTING "STO0703.sto" override
    ADD_STORE_ITEM "ZUSQSC01" AFTER "SCRL1S" #0 #0 #0 "IDENTIFIED" #2
    ADD_STORE_ITEM "ZUSQSC02" AFTER "SCRL5N" #0 #0 #0 "IDENTIFIED" #1

// Adventurer Mart
COPY_EXISTING "RIBALD.sto" override
    ADD_STORE_ITEM "ZUSQSC01" AFTER "SCRLA5" #0 #0 #0 "IDENTIFIED" #3
    ADD_STORE_ITEM "ZUSQSC02" AFTER "SCRL3G" #0 #0 #0 "IDENTIFIED" #2

// Lendarn in Firewine Ruins
COPY_EXISTING "LENDAR.CRE" override
	ADD_CRE_ITEM "ZUSQSC01" #0 #0 #0 "IDENFITIED" INV

// Candlekeep Catacombs Level 1
COPY_EXISTING "BG2613.ARE" override
	LAUNCH_PATCH_FUNCTION ADD_AREA_ITEM 
		INT_VAR container_to_add_to = 2
		STR_VAR item_to_add = "ZUSQSC01"
	END

// Repository of Undeath / Lich's Outpost
COPY_EXISTING "BD1200.ARE" override
	LAUNCH_PATCH_FUNCTION ADD_AREA_ITEM 
		INT_VAR container_to_add_to = 12
		STR_VAR item_to_add = "ZUSQSC01"
	END

// Durlag's Tower Labyrinth Level 3
COPY_EXISTING "BG0513.ARE" override
	LAUNCH_PATCH_FUNCTION ADD_AREA_ITEM 
		INT_VAR container_to_add_to = 1
		STR_VAR item_to_add = "ZUSQSC02"
	END

// Dragonspear Castle Basement
COPY_EXISTING "BD4300.ARE" override
	LAUNCH_PATCH_FUNCTION ADD_AREA_ITEM 
		INT_VAR container_to_add_to = 5
		STR_VAR item_to_add = "ZUSQSC02"
	END

// Spellhold Bodhi's Hunt Level 2
COPY_EXISTING "AR1513.ARE" override
	LAUNCH_PATCH_FUNCTION ADD_AREA_ITEM 
		INT_VAR container_to_add_to = 7
		STR_VAR item_to_add = "ZUSQSC02"
	END

// Add the scrolls to the random loot tables, but only replace duplicate scrolls
COPY_EXISTING ~RNDSCROL.2DA~ override
	// Replace "1st" SCRL66 with "ZUSQSC01"
	SET_2DA_ENTRY 0 4 21 "ZUSQSC01"
	// Replace "3rd_BG1" SCRL1M with "ZUSQSC01"
	SET_2DA_ENTRY 5 10 21 "ZUSQSC01"
	// Replace "4th_BG1" SCRL5M with "ZUSQSC02"
	SET_2DA_ENTRY 6 10 21 "ZUSQSC02"
// ---- END Add Scrolls to the World ----