// *** Spells for Chaos Sorcerer ***

INCLUDE ~%MOD_FOLDER%/lib/spells_base.tph~


// spell: Chaotic Weave
INCLUDE ~%MOD_FOLDER%/lib/spell_selection.tph~
COPY ~%MOD_FOLDER%/spells/a7_sp01.spl~ ~override~
  SAY NAME1 @2000 // Chaotic Weave
  SAY UNIDENTIFIED_DESC @2001 // Chaotic Weave description

  // Adding spell selection lists
  // Order of entries: ability header, prob_min, prob_max => table name
  PATCH_DEFINE_ASSOCIATIVE_ARRAY tiers BEGIN
    // Tier 1
    ~1~, ~98~, ~100~ => ~A7_SP1N~
    ~1~, ~96~, ~97~ => ~A7_SP1M~
    ~1~, ~93~, ~95~ => ~A7_SP1L~
    ~1~, ~89~, ~92~ => ~A7_SP1K~
    ~1~, ~83~, ~88~ => ~A7_SP1J~
    ~1~, ~76~, ~82~ => ~A7_SP1I~
    ~1~, ~67~, ~75~ => ~A7_SP1H~
    ~1~, ~58~, ~66~ => ~A7_SP1G~
    ~1~, ~50~, ~57~ => ~A7_SP1F~
    ~1~, ~42~, ~49~ => ~A7_SP1E~
    ~1~, ~33~, ~41~ => ~A7_SP1D~
    ~1~, ~22~, ~32~ => ~A7_SP1C~
    ~1~, ~11~, ~21~ => ~A7_SP1B~
    ~1~, ~0~, ~10~ => ~A7_SP1A~
    // Tier 2
    ~2~, ~98~, ~100~ => ~A7_SP2N~
    ~2~, ~96~, ~97~ => ~A7_SP2M~
    ~2~, ~93~, ~95~ => ~A7_SP2L~
    ~2~, ~89~, ~92~ => ~A7_SP2K~
    ~2~, ~83~, ~88~ => ~A7_SP2J~
    ~2~, ~76~, ~82~ => ~A7_SP2I~
    ~2~, ~67~, ~75~ => ~A7_SP2H~
    ~2~, ~58~, ~66~ => ~A7_SP2G~
    ~2~, ~50~, ~57~ => ~A7_SP2F~
    ~2~, ~42~, ~49~ => ~A7_SP2E~
    ~2~, ~33~, ~41~ => ~A7_SP2D~
    ~2~, ~22~, ~32~ => ~A7_SP2C~
    ~2~, ~11~, ~21~ => ~A7_SP2B~
    ~2~, ~0~, ~10~ => ~A7_SP2A~
    // Tier 3
    ~3~, ~98~, ~100~ => ~A7_SP3N~
    ~3~, ~96~, ~97~ => ~A7_SP3M~
    ~3~, ~93~, ~95~ => ~A7_SP3L~
    ~3~, ~89~, ~92~ => ~A7_SP3K~
    ~3~, ~83~, ~88~ => ~A7_SP3J~
    ~3~, ~76~, ~82~ => ~A7_SP3I~
    ~3~, ~67~, ~75~ => ~A7_SP3H~
    ~3~, ~58~, ~66~ => ~A7_SP3G~
    ~3~, ~50~, ~57~ => ~A7_SP3F~
    ~3~, ~42~, ~49~ => ~A7_SP3E~
    ~3~, ~33~, ~41~ => ~A7_SP3D~
    ~3~, ~22~, ~32~ => ~A7_SP3C~
    ~3~, ~11~, ~21~ => ~A7_SP3B~
    ~3~, ~0~, ~10~ => ~A7_SP3A~
    // Tier 4
    ~4~, ~96~, ~100~ => ~A7_SP4M~
    ~4~, ~93~, ~95~ => ~A7_SP4L~
    ~4~, ~89~, ~92~ => ~A7_SP4K~
    ~4~, ~83~, ~88~ => ~A7_SP4J~
    ~4~, ~76~, ~82~ => ~A7_SP4I~
    ~4~, ~67~, ~75~ => ~A7_SP4H~
    ~4~, ~58~, ~66~ => ~A7_SP4G~
    ~4~, ~50~, ~57~ => ~A7_SP4F~
    ~4~, ~42~, ~49~ => ~A7_SP4E~
    ~4~, ~33~, ~41~ => ~A7_SP4D~
    ~4~, ~22~, ~32~ => ~A7_SP4C~
    ~4~, ~11~, ~21~ => ~A7_SP4B~
    ~4~, ~0~, ~10~ => ~A7_SP4A~
    // Tier 5
    ~5~, ~82~, ~100~ => ~A7_SP5E~
    ~5~, ~67~, ~81~ => ~A7_SP5D~
    ~5~, ~50~, ~66~ => ~A7_SP5C~
    ~5~, ~25~, ~49~ => ~A7_SP5B~
    ~5~, ~0~, ~24~ => ~A7_SP5A~
    // Tier 6
    ~6~, ~0~, ~100~ => ~A7_SP6A~
  END

  PATCH_PHP_EACH tiers AS numbers => resref BEGIN
    LPF apply_opcode_214
    INT_VAR
      header = numbers_0
      prob_min = numbers_1
      prob_max = numbers_2
    STR_VAR
      resource = EVAL ~%resref%~
    END
  END

COPY ~%MOD_FOLDER%/spells/a7_sp01b.bam~ ~override~
     ~%MOD_FOLDER%/spells/a7_sp01c.bam~ ~override~


// HLA: Chaotic Eruption
COPY ~%MOD_FOLDER%/spells/a7_sp10.spl~ ~override~
  SAY NAME1 @2100 // Chaotic Eruption
  SAY UNIDENTIFIED_DESC @2101 // Chaotic Eruption description

COPY ~%MOD_FOLDER%/spells/a7_sp10b.bam~ ~override~
     ~%MOD_FOLDER%/spells/a7_sp10c.bam~ ~override~
     ~%MOD_FOLDER%/spells/a7_cs1a.bam~ ~override~
     ~%MOD_FOLDER%/spells/a7_cs2a.bam~ ~override~
     ~%MOD_FOLDER%/spells/a7_cs1b.bmp~ ~override~
     ~%MOD_FOLDER%/spells/a7_cs2b.bmp~ ~override~
     ~%MOD_FOLDER%/spells/a7_cs1a.vvc~ ~override~
     ~%MOD_FOLDER%/spells/a7_cs1b.vvc~ ~override~
     ~%MOD_FOLDER%/spells/a7_cs2a.vvc~ ~override~
     ~%MOD_FOLDER%/spells/a7_cs2b.vvc~ ~override~

COPY ~%MOD_FOLDER%/creatures/a7_cel1a.cre~ ~override~
  SAY NAME1 @10000  // Chaos Elemental
  SAY NAME2 @10000  // Chaos Elemental

COPY ~%MOD_FOLDER%/creatures/a7_cel2a.cre~ ~override~
  SAY NAME1 @10001  // Greater Chaos Elemental
  SAY NAME2 @10001  // Greater Chaos Elemental

COPY ~%MOD_FOLDER%/spells/a7_cel1a.eff~ ~override~
     ~%MOD_FOLDER%/spells/a7_cel2a.eff~ ~override~
     ~%MOD_FOLDER%/spells/a7_cel3.spl~ ~override~

COPY ~%MOD_FOLDER%/spells/a7_cel1.spl~ ~override~
  SAY NAME1 @5000 // Confusion
  SAY UNIDENTIFIED_DESC @5001 // ...

COPY ~%MOD_FOLDER%/spells/a7_cel2.spl~ ~override~
  SAY NAME1 @5050 // Mass Confusion
  SAY UNIDENTIFIED_DESC @5051 // ...

COMPILE ~%MOD_FOLDER%/scripts/a7_cel1a.baf~
        ~%MOD_FOLDER%/scripts/a7_cel1x.baf~
        ~%MOD_FOLDER%/scripts/a7_cel2a.baf~
        ~%MOD_FOLDER%/scripts/a7_cel2x.baf~


// Spell: Conjure Chaos Elemental
COPY ~%MOD_FOLDER%/spells/a7_sp03.spl~ ~override~
    SAY NAME1 @2050 // Conjure Chaos Elemental
    SAY UNIDENTIFIED_DESC @2051 // ...

ACTION_IF (NOT FILE_EXISTS_IN_GAME ~spin906.spl~) BEGIN
  COPY ~%MOD_FOLDER%/spells/spin906.spl~ ~override~
  APPEND ~spell.ids~ ~3906 THREE_ROUND_ENCHANTMENT_IMMUNITY~ UNLESS ~THREE_ROUND_ENCHANTMENT_IMMUNITY~
END

COMPILE ~%MOD_FOLDER%/scripts/a7_cel1b.baf~
        ~%MOD_FOLDER%/scripts/a7_cel1y.baf~

COPY ~%MOD_FOLDER%/creatures/a7_cel1b.cre~ ~override~
  SAY NAME1 @10000  // Chaos Elemental
  SAY NAME2 @10000  // Chaos Elemental

ACTION_IF (GAME_IS ~iwdee~) BEGIN
  COPY ~%MOD_FOLDER%/spells/iwdee/a7_sp03b.bam~ ~override~
       ~%MOD_FOLDER%/spells/iwdee/a7_sp03c.bam~ ~override~
END ELSE BEGIN
  COPY ~%MOD_FOLDER%/spells/a7_sp03b.bam~ ~override~
       ~%MOD_FOLDER%/spells/a7_sp03c.bam~ ~override~
END
COPY ~%MOD_FOLDER%/spells/a7_sp03a.bam~ ~override~
     ~%MOD_FOLDER%/spells/a7_sp03.eff~ ~override~
     ~%MOD_FOLDER%/items/a7_ring1.itm~ ~override~


// Spell: Conjure Greater Chaos Elemental
COPY ~%MOD_FOLDER%/spells/a7_sp04.spl~ ~override~
    SAY NAME1 @2150 // Conjure Greater Chaos Elemental
    SAY UNIDENTIFIED_DESC @2151 // ...

ACTION_IF (NOT FILE_EXISTS_IN_GAME ~spin906.spl~) BEGIN
  COPY ~%MOD_FOLDER%/spells/spin906.spl~ ~override~
  APPEND ~spell.ids~ ~3906 THREE_ROUND_ENCHANTMENT_IMMUNITY~ UNLESS ~THREE_ROUND_ENCHANTMENT_IMMUNITY~
END

COMPILE ~%MOD_FOLDER%/scripts/a7_cel2b.baf~
        ~%MOD_FOLDER%/scripts/a7_cel2y.baf~

COPY ~%MOD_FOLDER%/creatures/a7_cel2b.cre~ ~override~
  SAY NAME1 @10001  // Greater Chaos Elemental
  SAY NAME2 @10001  // Greater Chaos Elemental

ACTION_IF (GAME_IS ~iwdee~) BEGIN
  COPY ~%MOD_FOLDER%/spells/iwdee/a7_sp04b.bam~ ~override~
       ~%MOD_FOLDER%/spells/iwdee/a7_sp04c.bam~ ~override~
END ELSE BEGIN
  COPY ~%MOD_FOLDER%/spells/a7_sp04b.bam~ ~override~
       ~%MOD_FOLDER%/spells/a7_sp04c.bam~ ~override~
END
COPY ~%MOD_FOLDER%/spells/a7_sp04a.bam~ ~override~
     ~%MOD_FOLDER%/spells/a7_sp04.eff~ ~override~


// Adding Chaos Sorcerer specific wild surges
ACTION_IF (FILE_EXISTS_IN_GAME ~wildmag.2da~) BEGIN
  OUTER_SET kit_id = IDS_OF_SYMBOL(~kit~ ~A7_CHAOS_SORCERER~)

  // Reading original surges from table
  COPY_EXISTING - ~wildmag.2da~ ~override~
    COUNT_2DA_COLS numCols
    COUNT_2DA_ROWS numCols numRows
    PATCH_IF (numRows > 53) BEGIN
      READ_2DA_ENTRY 49 1 numCols resref_surge_50
      READ_2DA_ENTRY 49 2 numCols strref_surge_50
      READ_2DA_ENTRY 53 1 numCols resref_surge_54
      READ_2DA_ENTRY 53 2 numCols strref_surge_54
    END ELSE BEGIN
      TEXT_SPRINT resref_surge_50 ~~
      SET strref_surge_50 = "-1"
      TEXT_SPRINT resref_surge_54 ~~
      SET strref_surge_54 = "-1"
    END

  ACTION_IF (NOT ~%resref_surge_50%~ STR_EQ ~~) BEGIN
    // Specific Wild Surge 50: Summon Chaos Elemental (controllable)
    COPY ~%MOD_FOLDER%/spells/a7_wm01.spl~ ~override~
      LPF a7_update_effect_params
      INT_VAR 
        opcode            = 177
        match_parameter1  = 1
        parameter1        = 9999
        new_parameter1    = kit_id
      END

    COPY ~%MOD_FOLDER%/spells/a7_wm1a.eff~ ~override~
      WRITE_LONG 0x1c strref_surge_50

    COPY ~%MOD_FOLDER%/spells/a7_wm1b.eff~ ~override~
      WRITE_ASCIIE 0x30 ~%resref_surge_50%~ (8)

    COPY ~%MOD_FOLDER%/spells/a7_wm1c.eff~ ~override~
      SAY 0x1c @10004 // Chaos Elemental summoned

    COPY ~%MOD_FOLDER%/spells/a7_wm1d.eff~ ~override~

    COPY ~%MOD_FOLDER%/creatures/a7_cel1c.cre~ ~override~
      SAY NAME1 @10000  // Chaos Elemental
      SAY NAME2 @10000  // Chaos Elemental

    COPY_EXISTING ~wildmag.2da~ ~override~
      SET_2DA_ENTRY 49 1 numCols ~A7_WM01~
      SET_2DA_ENTRY 49 2 numCols ~-1~
    BUT_ONLY
  END


  ACTION_IF (NOT ~%resref_surge_54%~ STR_EQ ~~) BEGIN
    // Specific Wild Surge 54: Call forth Greater Chaos Elemental (hostile)
    COPY ~%MOD_FOLDER%/spells/a7_wm02.spl~ ~override~
      LPF a7_update_effect_params
      INT_VAR 
        opcode            = 177
        match_parameter1  = 1
        parameter1        = 9999
        new_parameter1    = kit_id
      END

    COPY ~%MOD_FOLDER%/spells/a7_wm1a.eff~ ~override/a7_wm2a.eff~
      WRITE_LONG 0x1c strref_surge_54

    COPY ~%MOD_FOLDER%/spells/a7_wm1b.eff~ ~override/a7_wm2b.eff~
      WRITE_ASCIIE 0x30 ~%resref_surge_54%~ (8)

    COPY ~%MOD_FOLDER%/spells/a7_wm1c.eff~ ~override/a7_wm2c.eff~
      SAY 0x1c @10005 // Oops, a rift... Is that a chaos creature?!

    COPY ~%MOD_FOLDER%/spells/a7_wm2d.eff~ ~override~

    COPY ~%MOD_FOLDER%/creatures/a7_cel2c.cre~ ~override~
      SAY NAME1 @10001  // Greater Chaos Elemental
      SAY NAME2 @10001  // Greater Chaos Elemental

    COPY_EXISTING ~wildmag.2da~ ~override~
      SET_2DA_ENTRY 53 1 numCols ~A7_WM02~
      SET_2DA_ENTRY 53 2 numCols ~-1~
    BUT_ONLY
  END
END
