//////////////////////////////////////////////////////////////////////
/////////////////////// BONS IRON MODDER - KISS //////////////////////
//////////////////////////////////////////////////////////////////////

COPY ~questpack/encounters/bons/sukiss1.cre~ ~override~
  SAY NAME1 @123 // Dahlia
  SAY NAME2 @123 // Dahlia

COMPILE EVALUATE_BUFFER ~questpack/encounters/bons/BonsKiss.d~ USING
        ~%tra_path%/english/bonskiss.tra~
        ~%tra_path%/%LANGUAGE%/bonskiss.tra~

COPY ~questpack/encounters/bons/SUKISSK.wav~ ~override~

EXTEND_TOP ~AR0800.bcs~ ~questpack/encounters/bons/appendAR0800.baf~ EVALUATE_BUFFER

COMPILE EVALUATE_BUFFER ~questpack/encounters/bons/SUKISSC1.baf~

//////////////////////////////////////////////////////////////////////
/////////////////////// BONS IRON MODDER - REGRETS ///////////////////
//////////////////////////////////////////////////////////////////////

ACTION_IF GAME_IS ~tob bgt bg2ee eet~ BEGIN
  COPY ~questpack/encounters/bons2/sumist.cre~ ~override~
    SAY NAME1 @275 // Spectre
    SAY NAME2 @275 // Spectre

  COPY ~questpack/encounters/bons2/suspyim.cre~ ~override~

  COPY_EXISTING ~spwi914.spl~ ~override/d0wi914.spl~
    SAY NAME1 @276 // Retribution of Regrets
    SAY NAME2 @276 // Retribution of Regrets

  EXTEND_TOP ~AR5006.bcs~ ~questpack/encounters/bons2/appendAR5006.baf~ EVALUATE_BUFFER

  COMPILE EVALUATE_BUFFER ~questpack/encounters/bons2/sumist.d~
          ~questpack/encounters/bons2/SUMIST.baf~
          ~questpack/encounters/bons2/SUSPYIM.baf~ USING
          ~%tra_path%/english/sumist.tra~
          ~%tra_path%/%LANGUAGE%/sumist.tra~

  ADD_JOURNAL @21 @50 USING
              ~%tra_path%/english/sumist.tra~
              ~%tra_path%/%LANGUAGE%/sumist.tra~
END

//////////////////////////////////////////////////////////////////////
/////////////////////// BLUE IRON MODDER - INDIGO ////////////////////
//////////////////////////////////////////////////////////////////////

COPY ~questpack/encounters/blue/d0indigo.cre~ ~override~
  SAY NAME1 @133 // Indigo
  SAY NAME2 @133 // Indigo

ACTION_IF FILE_EXISTS_IN_GAME ~FWKETO.DLG~ THEN BEGIN
  COMPILE EVALUATE_BUFFER ~questpack/encounters/blue/lmindi.d~
          ~questpack/encounters/blue/keto.d~ USING
          ~%tra_path%/english/indigo.tra~
          ~%tra_path%/%LANGUAGE%/indigo.tra~
END ELSE BEGIN
  COMPILE EVALUATE_BUFFER ~questpack/encounters/blue/lmindi.d~ USING
          ~%tra_path%/english/indigo.tra~
          ~%tra_path%/%LANGUAGE%/indigo.tra~
END

EXTEND_TOP ~ar1105.bcs~ ~questpack/encounters/blue/spawn.baf~ EVALUATE_BUFFER

//////////////////////////////////////////////////////////////////////
/////////////////////// DISPLACER BEASTS /////////////////////////////
//////////////////////////////////////////////////////////////////////

COPY ~questpack/encounters/ding0/displacer/JY_01DB.ITM~ ~override~
     ~questpack/encounters/ding0/displacer/JY_02DB.ITM~ ~override~
     ~questpack/encounters/ding0/displacer/JY_03DB.ITM~ ~override~
  SAY NAME1 #-1
  SAY NAME2 #-1
  SAY UNIDENTIFIED_DESC #-1
  SAY IDENTIFIED_DESC #-1

COPY ~questpack/encounters/ding0/displacer/JY_00DB.CRE~ ~override~
  SAY NAME1 @278 // Greater Displacer Beast
  SAY NAME2 @278 // Greater Displacer Beast
  WRITE_ASCII 0x34 "" #16

COPY ~questpack/encounters/ding0/displacer/JY_00DB1.CRE~ ~override~
  SAY NAME1 @277 // Displacer Beast
  SAY NAME2 @277 // Displacer Beast
  WRITE_ASCII 0x34 "" #16

COPY_EXISTING ~ar2100.are~ ~override~
  PATCH_FOR_EACH creature IN JY_00DB1 JY_00DB BEGIN
    LPF add_rest_encounter STR_VAR creature END
  END
BUT_ONLY

//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
/////////////////////// COMMENCE DING0! //////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////
/////////////////////// POTION CASES & AMMO BELTS ////////////////////
//////////////////////////////////////////////////////////////////////

ACTION_IF FILE_EXISTS_IN_GAME ~BAG05.ITM~ AND
          FILE_EXISTS_IN_GAME ~BAG06.ITM~
BEGIN
  COPY_EXISTING ~bag05.itm~ ~override/d0bag05.itm~
                ~bag05.itm~ ~override/d0bag05b.itm~
                ~bag06.itm~ ~override/d0bag06.itm~
                ~bag05.sto~ ~override/d0bag05.sto~
                ~bag05.sto~ ~override/d0bag05b.sto~
                ~bag06.sto~ ~override/d0bag06.sto~

  COPY_EXISTING ~MURCRAG.STO~ ~override~
    ADD_STORE_ITEM "d0bag06" #0 #0 #0 ~IDENTIFIED~ #1
  BUT_ONLY

  COPY_EXISTING ~SHOP05.STO~ ~override~
    ADD_STORE_ITEM "d0bag05" #0 #0 #0 ~IDENTIFIED~ #1
  BUT_ONLY

  COPY_EXISTING ~TRMER03.STO~ ~override~
    ADD_STORE_ITEM "d0bag05b" #0 #0 #0 ~IDENTIFIED~ #1
  BUT_ONLY
END

//////////////////////////////////////////////////////////////////////
/////////////////////// ADDITIONAL RANDOM ENCOUNTERS /////////////////
//////////////////////////////////////////////////////////////////////

ACTION_IF fl#ADDITIONAL_RANDOM_ENCOUNTERS BEGIN
  INCLUDE ~%MOD_FOLDER%/tp2/quest/randenc.tph~
END

//////////////////////////////////////////////////////////////////////
/////////////////////// RIBALD ARCHMAGI ROBE /////////////////////////
//////////////////////////////////////////////////////////////////////

COPY_EXISTING ~ribald.cre~ ~override~
  ADD_CRE_ITEM ~clck16~ #0 #0 #0 ~NONE~ ~ARMOR~

//////////////////////////////////////////////////////////////////////
/////////////////////// BATTLE HORRORS ///////////////////////////////
//////////////////////////////////////////////////////////////////////

COPY ~questpack/encounters/ding0/d0batho~ ~override~

EXTEND_BOTTOM ~ar1514.bcs~ ~questpack/encounters/ding0/et_1514.baf~ EVALUATE_BUFFER

COPY_EXISTING ~ar0330.are~ ~override~
  REPLACE_TEXTUALLY "ICMIN01" "D0BATHO" (8)
BUT_ONLY

//////////////////////////////////////////////////////////////////////
/////////////////////// ARROWS OF DETONATION /////////////////////////
//////////////////////////////////////////////////////////////////////

COPY_EXISTING ~shop05.sto~ ~override~
              ~ribald3.sto~ ~override~
  ADD_STORE_ITEM "arow06" AFTER "arow05" #40 #0 #0 ~IDENTIFIED~ #2

//////////////////////////////////////////////////////////////////////
/////////////////////// FRIENDLY DOCKS DOG ///////////////////////////
//////////////////////////////////////////////////////////////////////

COPY ~questpack/encounters/ding0/d0fundog.cre~ ~override~
  WRITE_ASCII 0x280 "d0fundog" #32

COMPILE EVALUATE_BUFFER ~questpack/encounters/ding0/d0fundog.baf~

EXTEND_TOP ~ar0300.bcs~ ~questpack/encounters/ding0/et_0300.baf~ EVALUATE_BUFFER

COPY ~questpack/encounters/ding0/dogfix~ ~override~

//////////////////////////////////////////////////////////////////////
/////////////////////// DOCK ORPHAN //////////////////////////////////
//////////////////////////////////////////////////////////////////////

COPY_EXISTING ~DFTOWN2.CRE~ ~override/D0FTOWN2.CRE~
  WRITE_ASCII 0x258 "WTRUNSGT" #8
  WRITE_ASCII 0x280 "D0FTOWN2" #32
  WRITE_ASCII 0x2cc "D0FTOWN2" #8

COPY_EXISTING ~DFTOWN2.DLG~ ~override/D0FTOWN2.DLG~

COPY_EXISTING ~ar0300.are~ ~override~
  GET_OFFSET_ARRAY actors 0x54 4 0x58 2 0 0 0x110
  PHP_EACH actors AS i => off BEGIN
    READ_SHORT off + 0x20 locx
    READ_SHORT off + 0x22 locy
    PATCH_IF locx = 3668 AND locy = 2314 BEGIN
      WRITE_ASCII off + 0x80 "D0FTOWN2" #8
    END
  END
BUT_ONLY

COPY_EXISTING ~dlost.cre~ ~override~
  WRITE_ASCII 0x250 "DLOST" #8

COMPILE EVALUATE_BUFFER ~questpack/encounters/ding0/dlost.baf~

//////////////////////////////////////////////////////////////////////
/////////////////////// COWLED ARREST CUTSCENE ///////////////////////
//////////////////////////////////////////////////////////////////////

/*
COPY ~questpack/encounters/ding0/d0qpcow1.cre~ ~override~
  WRITE_ASCII 0x280 "d0qpcow1" #32
  WRITE_ASCII 0x2cc "d0qpcow1" #8
  WRITE_ASCII 0x248 "d0cowsht" #8

COPY ~questpack/encounters/ding0/d0qpcow1.cre~ ~override/d0qpcow2.cre~
  SAY NAME1 @136
  SAY NAME2 @136
  WRITE_ASCII 0x280 "d0qpcow2" #32
  WRITE_ASCII 0x2cc "d0qpcow2" #8
  WRITE_ASCII 0x248 "d0cowsht" #8

COPY ~questpack/encounters/ding0/d0qpcow1.cre~ ~override/d0qpcow3.cre~
  SAY NAME1 #3139
  SAY NAME2 #3139
  WRITE_SHORT 0x28 25088
  WRITE_ASCII 0x280 "d0qpcow3" #32
  WRITE_ASCII 0x2cc "d0qpcow3" #8
  WRITE_BYTE 0x2d 46
  WRITE_BYTE 0x2e 47
  WRITE_ASCII 0x248 "d0cowsht" #8

EXTEND_TOP ~ar0500.bcs~ ~questpack/encounters/ding0/et_0500.baf~ EVALUATE_BUFFER

COMPILE EVALUATE_BUFFER ~questpack/encounters/ding0/d0cowsht.baf~
*/

//////////////////////////////////////////////////////////////////////
/////////////////////// MADULF FIGHTS WITH PALADINS //////////////////
//////////////////////////////////////////////////////////////////////

EXTEND_TOP ~ar1100.bcs~ ~questpack/encounters/ding0/et_1100.baf~ EVALUATE_BUFFER

COPY_EXISTING ~uhogre01.cre~ ~override~
  WRITE_ASCII 0x268 "WTASIGHT" #8
  WRITE_ASCII 0x260 "" #8
  WRITE_ASCII 0x258 "MADULF" #8
  WRITE_ASCII 0x250 "" #8
  WRITE_ASCII 0x248 "INITRG10" #8

COPY_EXISTING ~uhogre02.cre~ ~override~
  WRITE_ASCII 0x268 "WTASIGHT" #8
  WRITE_ASCII 0x260 "" #8
  WRITE_ASCII 0x258 "OGRE" #8
  WRITE_ASCII 0x250 "" #8
  WRITE_ASCII 0x248 "UHOGRE" #8

COPY_EXISTING ~uhogre03.cre~ ~override~
  WRITE_ASCII 0x268 "WTASIGHT" #8
  WRITE_ASCII 0x260 "" #8
  WRITE_ASCII 0x258 "" #8
  WRITE_ASCII 0x250 "" #8
  WRITE_ASCII 0x248 "UHOGRE" #8

COPY_EXISTING ~uhogre04.cre~ ~override~
  WRITE_ASCII 0x268 "WTASIGHT" #8
  WRITE_ASCII 0x260 "" #8
  WRITE_ASCII 0x258 "" #8
  WRITE_ASCII 0x250 "" #8
  WRITE_ASCII 0x248 "UHOGRE" #8

COPY_EXISTING ~uhogre05.cre~ ~override~
  WRITE_ASCII 0x268 "WTARSGT" #8
  WRITE_ASCII 0x260 "" #8
  WRITE_ASCII 0x258 "" #8
  WRITE_ASCII 0x250 "" #8
  WRITE_ASCII 0x248 "UHOGRE" #8

COMPILE EVALUATE_BUFFER ~questpack/encounters/ding0/madulf.baf~
        ~questpack/encounters/ding0/uhogre.baf~

COPY_EXISTING ~plshkn01.cre~ ~override~
              ~plshkn02.cre~ ~override~
  WRITE_ASCII 0x250 "" #8

//////////////////////////////////////////////////////////////////////
/////////////////////// MUMMY RESURRECTION ///////////////////////////
//////////////////////////////////////////////////////////////////////

COPY ~questpack/encounters/ding0/d0mumfig.cre~ ~override~

COPY_EXISTING ~MUMGRE01.CRE~ ~override~
              ~RIFTCR03.CRE~ ~override~
  WRITE_ASCII 0x248 "MUMMY01" #8
  WRITE_BYTE 0x24f 0

COPY_EXISTING ~MUMMY.CRE~ ~override~
  WRITE_ASCII 0x250 "MUMMY01" #8

COMPILE EVALUATE_BUFFER ~questpack/encounters/ding0/mummy01.baf~

COPY_EXISTING ~mumfig01.cre~ ~override~
  WRITE_BYTE 0x59 0
  WRITE_BYTE 0x5a 0
  WRITE_BYTE 0x5b 0
  WRITE_BYTE 0x5c 0
  WRITE_BYTE 0x5d 0
  WRITE_BYTE 0x5e 0
  WRITE_BYTE 0x5f 0
  WRITE_BYTE 0x60 0
  WRITE_BYTE 0x61 0
  WRITE_BYTE 0x62 0
  WRITE_BYTE 0x63 0
  WRITE_BYTE 0x27b 18

COPY_EXISTING rods03.itm override
  LPF DELETE_SPELL_EFFECT INT_VAR opcode_to_delete = "-1" END
  LPF ADD_SPELL_EFFECT
    INT_VAR
      opcode = 146
      target = 2
      parameter1 = 9
      timing = 7
      resist_dispel = 2
      duration = 1
    STR_VAR
      resource = d0pr712
  END

COPY_EXISTING sppr712.spl ~override/d0pr712.spl~
  WRITE_SHORT 0x22 0 // No casting animation
  LPF ALTER_SPELL_HEADER // 0 casting time
    INT_VAR
      match_header = "-1"
      speed = 0
  END

COPY ~questpack/encounters/ding0/d0mummy.eff~ ~override~

COPY_EXISTING ~SPJA01.SPL~ ~override~
//            ~SPWISH10.SPL~ ~override~ // exclude mass raise dead - only affects party members
              ~BHAAL4A.SPL~ ~override~
//            ~SPPR999.SPL~ ~override~ // only affects party members
//            ~SPPR729.SPL~ ~override~ // exclude mass raise dead - only affects party members
              ~SPPR504.SPL~ ~override~
              ~SPPR712.SPL~ ~override~
              ~d0pr712.spl~  override
  PATCH_IF SOURCE_SIZE > 0x71 BEGIN // protects against invalid files
    GET_OFFSET_ARRAY headers 0x64 4 0x68 2 0 0 0x28
    PHP_EACH headers AS i => off BEGIN
      LPF get_effect_info
        INT_VAR
          target_opcode = 32
          header_off = off
        RET
          target
          resist_dispel
      END
      LPF ADD_SPELL_EFFECT
        INT_VAR
          insert_point = 0
          header = i
          opcode = 177
          target
          power = 4
          parameter1 = 117
          parameter2 = 5
          timing = 1
          resist_dispel
        STR_VAR
          resource = D0Mummy
      END
    END
  END
BUT_ONLY

//////////////////////////////////////////////////////////////////////
/////////////////////// WINDSPEAR KNIGHTS ILLUSION ///////////////////
//////////////////////////////////////////////////////////////////////

COPY_EXISTING ~wish01.cre~ ~override~
  WRITE_ASCII 0x280 "WISH01" #32

COPY_EXISTING ~spin684.spl~ ~override~
              ~sppr950.spl~ ~override~
              ~spin696.spl~ ~override~
              ~spcl232.spl~ ~override~
              ~spcl232d.spl~ ~override~
              ~spwi322.spl~ ~override~
              ~spwi515.spl~ ~override~
              ~spwi609.spl~ ~override~
              ~spwi609d.spl~ ~override~
              ~sppr505.spl~ ~override~
              ~sppr505d.spl~ ~override~
              ~spcl732.spl~ ~override~
              ~spin805.spl~ ~override~
  PATCH_IF SOURCE_SIZE > 0x71 BEGIN // protects against invalid files
    GET_OFFSET_ARRAY headers 0x64 4 0x68 2 0 0 0x28
    PHP_EACH headers AS i => off BEGIN
      LPF get_effect_info
        INT_VAR
          target_opcode = 220
          header_off = off
        RET
          target
          resist_dispel
      END
      PATCH_FOR_EACH resource IN D0QPPLKM flqppaln BEGIN
        LPF ADD_SPELL_EFFECT
          INT_VAR
            header = i
            insert_point = 0
            opcode = 177
            target
            power = 4
            parameter1 = 134
            parameter2 = 6
            timing = 1
            resist_dispel
          STR_VAR
            resource
        END
      END
    END
  END
BUT_ONLY

COPY ~questpack/encounters/ding0/effs~ ~override~

COPY_EXISTING d0qpplka.eff ~override/flqppaln.eff~
  WRITE_LONG 0x10 103 // Change name
  WRITE_LONG 0x1c 10536 // Knight of the Order
  WRITE_LONG 0x20 0
  WRITE_ASCII 0x30 "" #8

/* ToBEx' "Use EFF file fix" breaks on sufficiently nested Use EFF
 * (177), so we remove a layer (vide supra) and change all paladins
 * into guys. Sorry, equality, male default wins again.
 */

COPY_EXISTING ~palkni01.cre~ ~override~
              ~palkni02.cre~ ~override~
              ~palkni03.cre~ ~override~
              ~palkni04.cre~ ~override~
              ~palkni05.cre~ ~override~
  WRITE_BYTE 0x237 1 // Male
  WRITE_BYTE 0x274 134 // Specific
  WRITE_BYTE 0x275 1 // Male
  SAY NAME2 #-1 // Otherwise their names remain "Ogre" and such
BUT_ONLY

//////////////////////////////////////////////////////////////////////
/////////////////////// RAKSHASAS AND FLAIL //////////////////////////
//////////////////////////////////////////////////////////////////////

EXTEND_TOP ~trrak01.bcs~ ~questpack/encounters/ding0/trrak01.baf~ EVALUATE_BUFFER

//////////////////////////////////////////////////////////////////////
/////////////////////// VAMPIRIC SIME ////////////////////////////////
//////////////////////////////////////////////////////////////////////

COPY ~questpack/encounters/ding0/d0vamsim.cre~ ~override~

EXTEND_TOP ~ppsime.bcs~ ~questpack/encounters/ding0/et_sime.baf~ EVALUATE_BUFFER

EXTEND_BOTTOM ~ar1514.bcs~ ~questpack/encounters/ding0/et_spawnsime.baf~ EVALUATE_BUFFER

COPY_EXISTING ~vammat01.cre~ ~override~
  WRITE_ASCII 0x280 "VAMMAT01" #32

//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
/////////////////////// DIALOGUE /////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////

// Dialogue compilation
COMPILE EVALUATE_BUFFER ~questpack/encounters/ding0/dialog.d~ USING
        ~%tra_path%/english/miscellaneous.tra~
        ~%tra_path%/%LANGUAGE%/miscellaneous.tra~
ACTION_IF GAME_IS ~tob bgt bg2ee eet~ BEGIN
  COMPILE EVALUATE_BUFFER ~questpack/encounters/ding0/25dialog.d~ USING
          ~%tra_path%/english/miscellaneous.tra~
          ~%tra_path%/%LANGUAGE%/miscellaneous.tra~
END

// Cut: Lie to Gracen
// EXTEND_TOP ~ar0801.bcs~ ~questpack/encounters/ding0/et_0801.baf~
// COMPILE ~questpack/encounters/ding0/bodfgt03.baf~

// Easter Egg
COPY ~questpack/encounters/ding0/d0gnoll/d0gnoll.bam~ ~override~
     ~questpack/encounters/ding0/d0gnoll/d0gnoll.vvc~ ~override~
// I promise this doesn't show up ingame!

EXTEND_BOTTOM ~baldur.bcs~ ~questpack/encounters/ding0/d0gnoll/d0gnoll.baf~ EVALUATE_BUFFER
