// *** Spells for all Wild Magic related kits and classes ***

// Relocate Nahal's Reckless Dweomer (SPWI124 by default)
// Spell slot might have been changed by another mod
OUTER_SET old_slot = 2124
ACTION_FOR_EACH symbol IN ~WIZARD_NAHALS_RECKLESS_DWEOMER~ ~WIZARD_ALARM~ BEGIN
  OUTER_SET code = IDS_OF_SYMBOL(~spell~ ~%symbol%~)
  ACTION_IF (code > 0 && code != old_slot) BEGIN
    OUTER_SET old_slot = code
  END
END
LAF a7_resref_of_spell_code INT_VAR code = old_slot RET resref END
OUTER_TEXT_SPRINT old_spell_nrd_resref ~%resref%~

ACTION_IF (old_slot = 2124 || old_slot < 2100 || old_slot >= 2200) BEGIN
  LAF a7_find_free_spell_slot INT_VAR type = 2 level = 1 RET slot resref END
  OUTER_TEXT_SPRINT spell_nrd_resref ~%resref%~
END ELSE BEGIN
  OUTER_SET slot = old_slot
  OUTER_TEXT_SPRINT spell_nrd_resref ~%old_spell_nrd_resref%~
END

ACTION_IF (slot >= 0) BEGIN
  ACTION_IF (slot != old_slot) BEGIN
    // updating symbolic spell references in scripts
    OUTER_PATCH ~~ BEGIN LOOKUP_IDS_SYMBOL_OF_INT spell_name ~spell~ old_slot END
    ACTION_IF (NOT ~%spell_name%~ STR_EQ ~%old_slot%~) BEGIN
      COPY_EXISTING_REGEXP ~.+\.bcs~ ~override~
        DECOMPILE_AND_PATCH BEGIN
          REPLACE_TEXTUALLY ~%spell_name%~ ~%slot%~
        END
      BUT_ONLY
    END

    // updating SPELL.IDS entries
    COPY_EXISTING ~SPELL.IDS~ ~override~
      LPF a7_update_spell_ids_code INT_VAR code = old_slot new_code = slot END
  END

  COPY_EXISTING ~%old_spell_nrd_resref%.SPL~ ~override/%spell_nrd_resref%.SPL~
    WRITE_BYTE 0x25 10  // Primary type: Wild Mage
    WRITE_LONG 0x1e (THIS | 0b111111111000000)  // Exclude generalist/specialist mages except Wild Mage
    LPF a7_update_resref STR_VAR resref_src = ~SPWI124B~ resref_dst = ~A7_SP01B~ END
    LPF a7_update_resref STR_VAR resref_src = ~SPWI124C~ resref_dst = ~A7_SP01C~ END
    LPF ADD_SPELL_EFFECT
    INT_VAR
      opcode = 321  // Remove effects by resource
      target = 1    // Self
    STR_VAR
      resource = ~SPWI124~
    END

  COPY_EXISTING ~SPWI124A.BAM~ ~override/A7_SP01A.BAM~
                ~SPWI124B.BAM~ ~override/A7_SP01B.BAM~
                ~SPWI124C.BAM~ ~override/A7_SP01C.BAM~

  // updating references in game resources
  COPY_EXISTING ~CONTINGX.2DA~ ~override~
                ~BGEE.LUA~ ~override~
    LPF a7_update_resref STR_VAR resref_src = EVAL ~%old_spell_nrd_resref%~ resref_dst = EVAL ~%spell_nrd_resref%~ END
  BUT_ONLY IF_EXISTS

  COPY_EXISTING_REGEXP ~.+\.bcs~ ~override~
    LPF a7_update_resref STR_VAR resref_src = EVAL ~%old_spell_nrd_resref%~ resref_dst = EVAL ~%spell_nrd_resref%~ END
  BUT_ONLY

  COPY_EXISTING_REGEXP ~.+\.dlg~ ~override~
    LPF a7_update_resref STR_VAR resref_src = EVAL ~%old_spell_nrd_resref%~ resref_dst = EVAL ~%spell_nrd_resref%~ END
  BUT_ONLY

  COPY_EXISTING_REGEXP ~.+\.cre~ ~override~
    LPF a7_update_resref STR_VAR resref_src = EVAL ~%old_spell_nrd_resref%~ resref_dst = EVAL ~%spell_nrd_resref%~ END
  BUT_ONLY

  // spell should appear on character spell selection screen, except on pre-2.0 games
  ACTION_IF (FILE_EXISTS_IN_GAME ~engine.lua~) BEGIN
    COPY_EXISTING ~hidespl.2da~ ~override~
      LPF a7_remove_2da_row INT_VAR column = 0 STR_VAR match = EVAL ~%spell_nrd_resref%~ END
    BUT_ONLY
  END ELSE BEGIN
    APPEND ~hidespl.2da~ ~%spell_nrd_resref% 1     0~ UNLESS ~%spell_nrd_resref%~
  END
END ELSE BEGIN
  FAIL ~Failed to find unoccupied level 1 spell slot.~
END


// Install new revision of Chaos Shield (former SPWI222)
// Spell slots might have been changed by another mod
OUTER_SET old_slot = IDS_OF_SYMBOL(~spell~ ~WIZARD_CHAOS_SHIELD~)
LAF a7_resref_of_spell_code INT_VAR code = old_slot RET resref END
OUTER_TEXT_SPRINT old_spell_chaos_shield_resref ~%resref%~
OUTER_SET old_slot2 = IDS_OF_SYMBOL(~spell~ ~WIZARD_IMPROVED_CHAOS_SHIELD~)
LAF a7_resref_of_spell_code INT_VAR code = old_slot2 RET resref END
OUTER_TEXT_SPRINT old_spell_improved_chaos_shield_resref ~%resref%~

ACTION_IF (old_slot < 2300 || old_slot >= 2400) BEGIN
  LAF a7_find_free_spell_slot INT_VAR type = 2 level = 3 RET slot resref END
  OUTER_TEXT_SPRINT spell_chaos_shield_resref ~%resref%~
END ELSE BEGIN
  OUTER_SET slot = old_slot
  OUTER_TEXT_SPRINT spell_chaos_shield_resref ~%old_spell_chaos_shield_resref%~
END

ACTION_IF (slot >= 0) BEGIN
  // updating SPELL.IDS entries
  COPY_EXISTING ~SPELL.IDS~ ~override~
    LPF a7_update_spell_ids_code INT_VAR code = old_slot new_code = slot END
    // Improved Chaos Shield is redirected to Chaos Shield
    LPF a7_update_spell_ids_code INT_VAR code = old_slot2 new_code = slot END

  COPY_EXISTING - ~%old_spell_chaos_shield_resref%.spl~ ~override~
    READ_LONG NAME1 strref_name

  COPY ~%MOD_FOLDER%/spells/spwi2xx.spl~ ~override/%spell_chaos_shield_resref%.SPL~
    WRITE_LONG NAME1 strref_name  // use original name
    SAY UNIDENTIFIED_DESC @2651   // new Chaos Shield description
    // opcode 321: resource SPWI222 -> new resref
    LPF a7_update_effect_params
    INT_VAR
      match_resource = 1
      opcode = 321
    STR_VAR
      resource = EVAL ~%old_spell_chaos_shield_resref%~
      new_resource = EVAL ~%spell_chaos_shield_resref%~
    END
    PATCH_IF (NOT FILE_EXISTS_IN_GAME ~splstate.ids~) BEGIN
      // pre-patch 2.0/1.4 game found
      LPF DELETE_SPELL_EFFECT INT_VAR opcode_to_delete = 328 END
    END

  COPY ~%MOD_FOLDER%/spells/a7_sp02a.bam~ ~override~
       ~%MOD_FOLDER%/spells/a7_sp02b.bam~ ~override~
       ~%MOD_FOLDER%/spells/a7_sp02c.bam~ ~override~

  // updating references in game resources
  COPY_EXISTING ~BGEE.LUA~ ~override~
    LPF a7_update_resref STR_VAR resref_src = EVAL ~%old_spell_chaos_shield_resref%~ resref_dst = EVAL ~%spell_chaos_shield_resref%~ END
    LPF a7_update_resref STR_VAR resref_src = EVAL ~%old_spell_improved_chaos_shield_resref%~ resref_dst = EVAL ~%spell_chaos_shield_resref%~ END
  BUT_ONLY IF_EXISTS

  COPY_EXISTING_REGEXP ~.+\.bcs~ ~override~
    LPF a7_update_resref STR_VAR resref_src = EVAL ~%old_spell_chaos_shield_resref%~ resref_dst = EVAL ~%spell_chaos_shield_resref%~ END
    LPF a7_update_resref STR_VAR resref_src = EVAL ~%old_spell_improved_chaos_shield_resref%~ resref_dst = EVAL ~%spell_chaos_shield_resref%~ END
  BUT_ONLY

  COPY_EXISTING_REGEXP ~.+\.dlg~ ~override~
    LPF a7_update_resref STR_VAR resref_src = EVAL ~%old_spell_chaos_shield_resref%~ resref_dst = EVAL ~%spell_chaos_shield_resref%~ END
    LPF a7_update_resref STR_VAR resref_src = EVAL ~%old_spell_improved_chaos_shield_resref%~ resref_dst = EVAL ~%spell_chaos_shield_resref%~ END
  BUT_ONLY

  COPY_EXISTING_REGEXP ~.+\.cre~ ~override~
    PATCH_IF (INDEX_BUFFER(~%old_spell_chaos_shield_resref%~) > 0) BEGIN
      ADD_KNOWN_SPELL ~%spell_chaos_shield_resref%~ #2 ~wizard~
    END
  BUT_ONLY

  COPY_EXISTING ~a7_csrc.2da~ ~override~
    REPLACE_TEXTUALLY ~GA_A7_SP02~ ~GA_%spell_chaos_shield_resref%~
  BUT_ONLY
  
  // spell should appear on character spell selection screen
  COPY_EXISTING ~hidespl.2da~ ~override~
    LPF a7_remove_2da_row INT_VAR column = 0 STR_VAR match = EVAL ~%spell_chaos_shield_resref%~ END
  BUT_ONLY
END ELSE BEGIN
  FAIL ~Failed to find unoccupied level 3 spell slot.~
END


// Install Surge Control (SPWI124)
COPY ~%MOD_FOLDER%/spells/spwi124.spl~ ~override~
  SAY NAME1 @2500 // Surge Control
  SAY UNIDENTIFIED_DESC @2501 // Surge Control description
  // opcode 321: resource SPWI222 -> new resref
  LPF a7_update_effect_params
  INT_VAR
    match_resource = 1
    opcode = 321
  STR_VAR
    resource = EVAL ~%old_spell_chaos_shield_resref%~
    new_resource = EVAL ~%spell_chaos_shield_resref%~
  END

COPY ~%MOD_FOLDER%/spells/spwi124d.spl~ ~override~
     ~%MOD_FOLDER%/spells/spwi124b.bam~ ~override~
     ~%MOD_FOLDER%/spells/spwi124c.bam~ ~override~

// spell does not appear on character spell selection screen, except on pre-2.0 games
ACTION_IF (FILE_EXISTS ~engine.lua~) BEGIN
  APPEND ~hidespl.2da~ ~SPWI124 1     0          0~ UNLESS ~SPWI124~
END ELSE BEGIN
  COPY_EXISTING ~hidespl.2da~ ~override~
    LPF a7_remove_2da_row INT_VAR column = 0 STR_VAR match = ~SPWI124~ END
  BUT_ONLY
END

// Updating spellbooks
COPY_EXISTING_REGEXP ~.+\.cre~ ~override~
  PATCH_IF (INDEX_BUFFER(~%spell_nrd_resref%~) > 0) BEGIN
    ADD_KNOWN_SPELL ~SPWI124~ #0 ~wizard~
  END
BUT_ONLY


// Install Unluck (SPWI222)
COPY ~%MOD_FOLDER%/spells/spwi222.spl~ ~override~
  SAY NAME1 @2550 // Unluck
  SAY UNIDENTIFIED_DESC @2551 // Unluck description

COPY ~%MOD_FOLDER%/spells/spwi222b.bam~ ~override~
     ~%MOD_FOLDER%/spells/spwi222c.bam~ ~override~

// spell should appear on character spell selection screen
COPY_EXISTING ~hidespl.2da~ ~override~
  LPF a7_remove_2da_row INT_VAR column = 0 STR_VAR match = ~SPWI222~ END
BUT_ONLY


// Install Nahal's Wildstrike (SPWI723)
COPY ~%MOD_FOLDER%/spells/spwi723.spl~ ~override~
  SAY NAME1 @2600 // Nahal's Wildstrike
  SAY UNIDENTIFIED_DESC @2601 // Nahal's Wildstrike description
  PATCH_IF (NOT FILE_EXISTS_IN_GAME ~splstate.ids~) BEGIN
    // pre-patch 2.0/1.4 game found
    LPF DELETE_SPELL_EFFECT INT_VAR opcode_to_delete = 328 END
  END

COPY ~%MOD_FOLDER%/spells/spwi723b.bam~ ~override~
     ~%MOD_FOLDER%/spells/spwi723c.bam~ ~override~
     ~%MOD_FOLDER%/spells/a7_cs3a.bam~ ~override~
     ~%MOD_FOLDER%/spells/a7_cs3a.vvc~ ~override~

// spell should appear on character spell selection screen
COPY_EXISTING ~hidespl.2da~ ~override~
  LPF a7_remove_2da_row INT_VAR column = 0 STR_VAR match = ~SPWI723~ END
BUT_ONLY


// Required Wild Mage adjustments
ACTION_IF (NOT FILE_EXISTS ~clabma01.2da~) BEGIN
  // Generate clabma01.2da if needed
  COPY_EXISTING - ~clabfi01.2da~ ~override~
    COUNT_2DA_COLS numCols

<<<<<<<< .../inlined/tables/stub.2da
2DA V1.0
****
>>>>>>>>
  COPY ~.../inlined/tables/stub.2da~ ~override/clabma01.2da~

  // table header
  OUTER_TEXT_SPRINT line ~ ~
  OUTER_FOR (idx = 1; idx < numCols; ++idx) BEGIN
    ACTION_IF (idx < 10) BEGIN
      OUTER_TEXT_SPRINT line ~%line%           %idx%~
    END ELSE BEGIN
      OUTER_TEXT_SPRINT line ~%line%          %idx%~
    END
  END
  APPEND ~clabma01.2da~ ~%line%~

  // table content
  OUTER_FOR (abil = 1; abil < 7; ++abil) BEGIN
    OUTER_TEXT_SPRINT line ~ABILITY%abil%~
    OUTER_FOR (idx = 1; idx < numCols; ++idx) BEGIN
      ACTION_IF (idx = 1) BEGIN
        OUTER_TEXT_SPRINT line ~%line%    ****~
      END ELSE BEGIN
        OUTER_TEXT_SPRINT line ~%line%        ****~
      END
    END
    APPEND ~clabma01.2da~ ~%line%~
  END
END

COPY ~%MOD_FOLDER%/spells/a7_cs05.eff~ ~override~
  WRITE_ASCIIE 0x30 ~%spell_nrd_resref%~ (8)

COPY ~%MOD_FOLDER%/spells/a7_cs06.eff~ ~override~
  WRITE_ASCIIE 0x30 ~%spell_chaos_shield_resref%~ (8)

COPY ~%MOD_FOLDER%/spells/a7_cs05.spl~ ~override~
     ~%MOD_FOLDER%/spells/a7_cs06.spl~ ~override~

// These spells have to be added manually
OUTER_TEXT_SPRINT give_abil_0 ~AP_A7_CS05~
OUTER_TEXT_SPRINT give_abil_1 ~AP_A7_CS06~
OUTER_SET give_abil = 2
OUTER_SET abilIdx = 0

// Adding custom abilities
COPY_EXISTING ~clabma01.2da~ ~override~
  COUNT_2DA_COLS numCols
  COUNT_2DA_ROWS numCols numRows
  FOR (row = 0; row < numRows; ++row) BEGIN
    READ_2DA_ENTRY row 1 numCols value
    PATCH_IF (~%value%~ STR_EQ ~****~) BEGIN
      TEXT_SPRINT entry EVAL ~%give_abil_%abilIdx%%~
      SET_2DA_ENTRY row 1 numCols ~%entry%~
      SET abilIdx += 1
    END
    PATCH_IF (abilIdx = give_abil) BEGIN
      SET row = numRows
    END
  END
BUT_ONLY

// In the unlikely case all slots are occupied already
OUTER_WHILE (abilIdx < give_abil) BEGIN
  OUTER_SET idx = numRows + 1
  OUTER_TEXT_SPRINT line ~ABILITY%idx%~
  OUTER_FOR (idx = 1; idx < numCols; ++idx) BEGIN
    ACTION_IF (idx = 1) BEGIN
      OUTER_TEXT_SPRINT entry EVAL ~%give_abil_%abilIdx%%~
      OUTER_TEXT_SPRINT line ~%line%    %entry%~
    END ELSE ACTION_IF (idx = 2) BEGIN
      OUTER_TEXT_SPRINT line ~%line%  ****~
    END ELSE BEGIN
      OUTER_TEXT_SPRINT line ~%line%        ****~
    END
  END
  APPEND ~clabma01.2da~ ~%line%~
  OUTER_SET abilIdx += 1
  OUTER_SET numRows += 1
END

COPY_EXISTING ~clabma01.2da~ ~override~
  PRETTY_PRINT_2DA
BUT_ONLY
