// Define main variables
OUTER_SET er_fireb1 = 2
OUTER_SET er_fireb2 = 3
OUTER_SPRINT er_tutu ~~
OUTER_SPRINT er_tutf ~F~
OUTER_SPRINT er_port ~tutu~
OUTER_SPRINT er_imoen ~Imoen~
OUTER_SPRINT er_uimoen ~IMOEN~
OUTER_SPRINT er_tra_path ~%MOD_FOLDER%/tra~

// (Re)define variables to handle differences between games
ACTION_IF GAME_IS ~eet~ THEN BEGIN
    OUTER_SET er_fireb1 = 1
    OUTER_SET er_fireb2 = 0
    OUTER_SPRINT er_port ~bgee~
    OUTER_SPRINT er_imoen ~Imoen2~
    OUTER_SPRINT er_uimoen ~IMOEN2~
    OUTER_SPRINT er_tra_path ~%MOD_FOLDER%/tra/bgee~
    OUTER_SPRINT er_bdynah ~BDYNAH~
    OUTER_SPRINT er_bimoen ~BIMOEN~
    OUTER_SPRINT er_bjahei ~BJAHEI~
    OUTER_SPRINT er_bvicon ~BVICON~
    OUTER_SPRINT er_dynahj ~DYNAHJ~
    OUTER_SPRINT er_tazok ~TAZOK_~
    OUTER_SPRINT er_ftown2 ~FTOWN2_~
    OUTER_SPRINT er_ftown3 ~FTOWN3_~
    OUTER_SPRINT er_BladeStarsL1 ~BG0105~
    OUTER_SPRINT er_BlushingMermaidL1 ~BG0114~
    OUTER_SPRINT er_BlushingMermaidL2 ~BG0115~
    OUTER_SPRINT er_HelmCloakL1 ~BG0116~
    OUTER_SPRINT er_MerchantLeagueL2 ~BG0128~
    OUTER_SPRINT er_LowLanternL1 ~BG0133~
    OUTER_SPRINT er_KeexieTavernL1 ~BG0154~
    OUTER_SPRINT er_NWBG_GenericTavernL1 ~BG0165~
    OUTER_SPRINT er_KeexieTavernL2 ~BG0171~
    OUTER_SPRINT er_IronThroneL4 ~BG0614~
    OUTER_SPRINT er_ElfsongTavernL1 ~BG0705~
    OUTER_SPRINT er_ElfsongTavernL2 ~BG0706~
    OUTER_SPRINT er_DrakonTavern ~BG0720~
    OUTER_SPRINT er_EBG_GenericInnL1 ~BG0807~
    OUTER_SPRINT er_EBG_GenericInnL2 ~BG0808~
    OUTER_SPRINT er_UlgothBeardsInn ~BG1001~
    OUTER_SPRINT er_SWBG_GenericTavernL1 ~BG1109~
    OUTER_SPRINT er_SWBG_GenericTavernL2 ~BG1110~
    OUTER_SPRINT er_YeOldeInnL1 ~BG1113~
    OUTER_SPRINT er_JopalinsTavern ~BG1215~
    OUTER_SPRINT er_SEBG_GenericInnL1 ~BG1306~
    OUTER_SPRINT er_FriendlyArmInnL1 ~BG2301~
    OUTER_SPRINT er_FriendlyArmInnL2 ~BG2302~
    OUTER_SPRINT er_FriendlyArmInnL3 ~BG2303~
    OUTER_SPRINT er_CandlekeepPrologue ~BG2600~
    OUTER_SPRINT er_CandlekeepCh6InnL1 ~BG2629~
    OUTER_SPRINT er_CandlekeepCh6InnL2 ~BG2630~
    OUTER_SPRINT er_CandlekeepCh6Barracks ~BG2631~
    OUTER_SPRINT er_JovialJugglerL1 ~BG3304~
    OUTER_SPRINT er_JovialJugglerL2 ~BG3305~
    OUTER_SPRINT er_BurningWizardL1 ~BG3307~
    OUTER_SPRINT er_BurningWizardL2 ~BG3308~
    OUTER_SPRINT er_FeldepostsInnL1 ~BG3351~
    OUTER_SPRINT er_FeldepostsInnL2 ~BG3352~
    OUTER_SPRINT er_RedSheafL1 ~BG3357~
    OUTER_SPRINT er_JenkalsHomeL2 ~BG4002~
    OUTER_SPRINT er_NashkelInn ~BG4801~
    OUTER_SPRINT er_NashkelManorL1 ~BG4804~
    OUTER_SPRINT er_BelchingDragonTavern ~BG4809~
    OUTER_SPRINT er_FirewineRuins ~BG5201~
    OUTER_SPRINT er_1803 ~BG1803~
    OUTER_SPRINT er_1900 ~BG1900~
    OUTER_SPRINT er_2400 ~BG2400~
    OUTER_SPRINT er_2600 ~BG2600~
    OUTER_SPRINT er_3300 ~BG3300~
    OUTER_SPRINT er_3357 ~BG3357~
    OUTER_SPRINT er_4002 ~BG4002~
    OUTER_SPRINT er_4804 ~BG4804~
    OUTER_SPRINT er_4900 ~BG4900~
    OUTER_SPRINT eet_var ~_~
END
ACTION_IF GAME_IS ~tutu tutu_totsc~ THEN BEGIN
  OUTER_SPRINT er_tutu ~_~
  OUTER_SPRINT er_tutf ~_~
  OUTER_SPRINT er_bdynah ~_BDYNAH~
  OUTER_SPRINT er_bimoen ~_BIMOEN~
  OUTER_SPRINT er_bjahei ~_BJAHEI~
  OUTER_SPRINT er_bvicon ~_BVICON~
  OUTER_SPRINT er_dynahj ~_DYNAHJ~
  OUTER_SPRINT er_tazok ~_TAZOK~
  OUTER_SPRINT er_ftown2 ~_FTOWN2~
  OUTER_SPRINT er_ftown3 ~_FTOWN3~
  OUTER_SPRINT er_BladeStarsL1 ~FW0105~
  OUTER_SPRINT er_BlushingMermaidL1 ~FW0114~
  OUTER_SPRINT er_BlushingMermaidL2 ~FW0115~
  OUTER_SPRINT er_HelmCloakL1 ~FW0116~
  OUTER_SPRINT er_MerchantLeagueL2 ~FW0128~
  OUTER_SPRINT er_LowLanternL1 ~FW0133~
  OUTER_SPRINT er_KeexieTavernL1 ~FW0154~
  OUTER_SPRINT er_NWBG_GenericTavernL1 ~FW0165~
  OUTER_SPRINT er_KeexieTavernL2 ~FW0171~
  OUTER_SPRINT er_IronThroneL4 ~FW0614~
  OUTER_SPRINT er_ElfsongTavernL1 ~FW0705~
  OUTER_SPRINT er_ElfsongTavernL2 ~FW0706~
  OUTER_SPRINT er_DrakonTavern ~FW0720~
  OUTER_SPRINT er_EBG_GenericInnL1 ~FW0807~
  OUTER_SPRINT er_EBG_GenericInnL2 ~FW0808~
  OUTER_SPRINT er_UlgothBeardsInn ~FW1001~
  OUTER_SPRINT er_SWBG_GenericTavernL1 ~FW1109~
  OUTER_SPRINT er_SWBG_GenericTavernL2 ~FW1110~
  OUTER_SPRINT er_YeOldeInnL1 ~FW1113~
  OUTER_SPRINT er_JopalinsTavern ~FW1215~
  OUTER_SPRINT er_SEBG_GenericInnL1 ~FW1306~
  OUTER_SPRINT er_FriendlyArmInnL1 ~FW2301~
  OUTER_SPRINT er_FriendlyArmInnL2 ~FW2302~
  OUTER_SPRINT er_FriendlyArmInnL3 ~FW2303~
  OUTER_SPRINT er_CandlekeepPrologue ~FW2600~
  OUTER_SPRINT er_CandlekeepCh6InnL1 ~FW2629~
  OUTER_SPRINT er_CandlekeepCh6InnL2 ~FW2630~
  OUTER_SPRINT er_CandlekeepCh6Barracks ~FW2631~
  OUTER_SPRINT er_JovialJugglerL1 ~FW3304~
  OUTER_SPRINT er_JovialJugglerL2 ~FW3305~
  OUTER_SPRINT er_BurningWizardL1 ~FW3307~
  OUTER_SPRINT er_BurningWizardL2 ~FW3308~
  OUTER_SPRINT er_FeldepostsInnL1 ~FW3351~
  OUTER_SPRINT er_FeldepostsInnL2 ~FW3352~
  OUTER_SPRINT er_RedSheafL1 ~FW3357~
  OUTER_SPRINT er_JenkalsHomeL2 ~FW4002~
  OUTER_SPRINT er_NashkelInn ~FW4801~
  OUTER_SPRINT er_NashkelManorL1 ~FW4804~
  OUTER_SPRINT er_BelchingDragonTavern ~FW4809~
  OUTER_SPRINT er_FirewineRuins ~FW5201~
  OUTER_SPRINT er_1803 ~_AR1803~
  OUTER_SPRINT er_1900 ~_AR1900~
  OUTER_SPRINT er_2400 ~_AR2400~
  OUTER_SPRINT er_2600 ~_AR2600~
  OUTER_SPRINT er_3300 ~_AR3300~
  OUTER_SPRINT er_3357 ~_AR3357~
  OUTER_SPRINT er_4002 ~_AR4002~
  OUTER_SPRINT er_4804 ~_AR4804~
  OUTER_SPRINT er_4900 ~_AR4900~
    OUTER_SPRINT eet_var ~~
END 
ACTION_IF GAME_IS ~bgee~ THEN BEGIN
    OUTER_SET er_fireb1 = 1
    OUTER_SET er_fireb2 = 0
    OUTER_SPRINT er_port ~bgee~
    OUTER_SPRINT er_tra_path ~%MOD_FOLDER%/tra/bgee~
    OUTER_SPRINT er_bdynah ~BDYNAH~
    OUTER_SPRINT er_bimoen ~BIMOEN~
    OUTER_SPRINT er_bjahei ~BJAHEI~
    OUTER_SPRINT er_bvicon ~BVICON~
    OUTER_SPRINT er_dynahj ~DYNAHJ~
    OUTER_SPRINT er_tazok ~TAZOK~
    OUTER_SPRINT er_ftown2 ~FTOWN2~
    OUTER_SPRINT er_ftown3 ~FTOWN3~
    OUTER_SPRINT er_BladeStarsL1 ~AR0105~
    OUTER_SPRINT er_BlushingMermaidL1 ~AR0114~
    OUTER_SPRINT er_BlushingMermaidL2 ~AR0115~
    OUTER_SPRINT er_HelmCloakL1 ~AR0116~
    OUTER_SPRINT er_MerchantLeagueL2 ~AR0128~
    OUTER_SPRINT er_LowLanternL1 ~AR0133~
    OUTER_SPRINT er_KeexieTavernL1 ~AR0154~
    OUTER_SPRINT er_NWBG_GenericTavernL1 ~AR0165~
    OUTER_SPRINT er_KeexieTavernL2 ~AR0171~
    OUTER_SPRINT er_IronThroneL4 ~AR0614~
    OUTER_SPRINT er_ElfsongTavernL1 ~AR0705~
    OUTER_SPRINT er_ElfsongTavernL2 ~AR0706~
    OUTER_SPRINT er_DrakonTavern ~AR0720~
    OUTER_SPRINT er_EBG_GenericInnL1 ~AR0807~
    OUTER_SPRINT er_EBG_GenericInnL2 ~AR0808~
    OUTER_SPRINT er_UlgothBeardsInn ~AR1001~
    OUTER_SPRINT er_SWBG_GenericTavernL1 ~AR1109~
    OUTER_SPRINT er_SWBG_GenericTavernL2 ~AR1110~
    OUTER_SPRINT er_YeOldeInnL1 ~AR1113~
    OUTER_SPRINT er_JopalinsTavern ~AR1215~
    OUTER_SPRINT er_SEBG_GenericInnL1 ~AR1306~
    OUTER_SPRINT er_FriendlyArmInnL1 ~AR2301~
    OUTER_SPRINT er_FriendlyArmInnL2 ~AR2302~
    OUTER_SPRINT er_FriendlyArmInnL3 ~AR2303~
    OUTER_SPRINT er_CandlekeepPrologue ~AR2600~
    OUTER_SPRINT er_CandlekeepCh6InnL1 ~AR2629~
    OUTER_SPRINT er_CandlekeepCh6InnL2 ~AR2630~
    OUTER_SPRINT er_CandlekeepCh6Barracks ~AR2631~
    OUTER_SPRINT er_JovialJugglerL1 ~AR3304~
    OUTER_SPRINT er_JovialJugglerL2 ~AR3305~
    OUTER_SPRINT er_BurningWizardL1 ~AR3307~
    OUTER_SPRINT er_BurningWizardL2 ~AR3308~
    OUTER_SPRINT er_FeldepostsInnL1 ~AR3351~
    OUTER_SPRINT er_FeldepostsInnL2 ~AR3352~
    OUTER_SPRINT er_RedSheafL1 ~AR3357~
    OUTER_SPRINT er_JenkalsHomeL2 ~AR4002~
    OUTER_SPRINT er_NashkelInn ~AR4801~
    OUTER_SPRINT er_NashkelManorL1 ~AR4804~
    OUTER_SPRINT er_BelchingDragonTavern ~AR4809~
    OUTER_SPRINT er_FirewineRuins ~AR5201~
    OUTER_SPRINT er_1803 ~AR1803~
    OUTER_SPRINT er_1900 ~AR1900~
    OUTER_SPRINT er_2400 ~AR2400~
    OUTER_SPRINT er_2600 ~AR2600~
    OUTER_SPRINT er_3300 ~AR3300~
    OUTER_SPRINT er_3357 ~AR3357~
    OUTER_SPRINT er_4002 ~AR4002~
    OUTER_SPRINT er_4804 ~AR4804~
    OUTER_SPRINT er_4900 ~AR4900~
    OUTER_SPRINT eet_var ~~
END 
ACTION_IF GAME_IS ~bgt~ THEN BEGIN
      OUTER_SPRINT er_imoen ~Imoen2~
      OUTER_SPRINT er_uimoen ~IMOEN2~
      OUTER_SPRINT er_bdynah ~BDYNA~
      OUTER_SPRINT er_bimoen ~BIMOEN2~
      OUTER_SPRINT er_bjahei ~BJAHEIR~
      OUTER_SPRINT er_bvicon ~BVICONI~
      OUTER_SPRINT er_dynahj ~DYNAJ~
      OUTER_SPRINT er_tazok ~BGTAZOK~
      OUTER_SPRINT er_ftown2 ~BGFTOWN2~
      OUTER_SPRINT er_ftown3 ~BGFTOWN3~
      OUTER_SPRINT er_BladeStarsL1 ~AR7205~
      OUTER_SPRINT er_BlushingMermaidL1 ~AR7214~
      OUTER_SPRINT er_BlushingMermaidL2 ~AR7215~
      OUTER_SPRINT er_HelmCloakL1 ~AR7216~
      OUTER_SPRINT er_MerchantLeagueL2 ~AR7228~
      OUTER_SPRINT er_LowLanternL1 ~AR7233~
      OUTER_SPRINT er_KeexieTavernL1 ~AR7254~
      OUTER_SPRINT er_NWBG_GenericTavernL1 ~AR7265~
      OUTER_SPRINT er_KeexieTavernL2 ~AR7271~
      OUTER_SPRINT er_IronThroneL4 ~AR7614~
      OUTER_SPRINT er_ElfsongTavernL1 ~AR7705~
      OUTER_SPRINT er_ElfsongTavernL2 ~AR7706~
      OUTER_SPRINT er_DrakonTavern ~AR7720~
      OUTER_SPRINT er_EBG_GenericInnL1 ~AR7807~
      OUTER_SPRINT er_EBG_GenericInnL2 ~AR7808~
      OUTER_SPRINT er_UlgothBeardsInn ~ARU001~
      OUTER_SPRINT er_SWBG_GenericTavernL1 ~AR8009~
      OUTER_SPRINT er_SWBG_GenericTavernL2 ~AR8010~
      OUTER_SPRINT er_YeOldeInnL1 ~AR8013~
      OUTER_SPRINT er_JopalinsTavern ~AR8115~
      OUTER_SPRINT er_SEBG_GenericInnL1 ~AR8206~
      OUTER_SPRINT er_FriendlyArmInnL1 ~AR6801~
      OUTER_SPRINT er_FriendlyArmInnL2 ~AR6802~
      OUTER_SPRINT er_FriendlyArmInnL3 ~AR6803~
      OUTER_SPRINT er_CandlekeepPrologue ~AR0015~
      OUTER_SPRINT er_CandlekeepCh6InnL1 ~AR6529~
      OUTER_SPRINT er_CandlekeepCh6InnL2 ~AR6530~
      OUTER_SPRINT er_CandlekeepCh6Barracks ~AR6531~
      OUTER_SPRINT er_JovialJugglerL1 ~AR6704~
      OUTER_SPRINT er_JovialJugglerL2 ~AR6705~
      OUTER_SPRINT er_BurningWizardL1 ~AR6707~
      OUTER_SPRINT er_BurningWizardL2 ~AR6708~
      OUTER_SPRINT er_FeldepostsInnL1 ~AR6751~
      OUTER_SPRINT er_FeldepostsInnL2 ~AR6752~
      OUTER_SPRINT er_RedSheafL1 ~AR6757~
      OUTER_SPRINT er_JenkalsHomeL2 ~AR9902~
      OUTER_SPRINT er_NashkelInn ~AR3701~
      OUTER_SPRINT er_NashkelManorL1 ~AR3704~
      OUTER_SPRINT er_BelchingDragonTavern ~AR3709~
      OUTER_SPRINT er_FirewineRuins ~AR3401~
      OUTER_SPRINT er_1803 ~AR8603~
      OUTER_SPRINT er_1900 ~AR8700~
      OUTER_SPRINT er_2400 ~AR8900~
      OUTER_SPRINT er_2600 ~AR0015~
      OUTER_SPRINT er_3300 ~AR6700~
      OUTER_SPRINT er_3357 ~AR6757~
      OUTER_SPRINT er_4002 ~AR9902~
      OUTER_SPRINT er_4804 ~AR3704~
      OUTER_SPRINT er_4900 ~AR3800~
    OUTER_SPRINT eet_var ~~
END

// Macro to handle multiple tra encodings (slightly adapted from Wisp's original code)
OUTER_SPRINT er_game ~sufinch~
DEFINE_ACTION_MACRO bgee_language BEGIN
  ACTION_IF GAME_IS ~bgee eet~ BEGIN
    LOAD_TRA ~%MOD_FOLDER%/tra/bgee/english/%er_game%.tra~
    LOAD_TRA ~%MOD_FOLDER%/tra/bgee/%LANGUAGE%/%er_game%.tra~
  END
END

// Convert items on creatures inventory (from Tutu to BG:EE/BGT names)
DEFINE_PATCH_MACRO ~ER_PATCH_ITEMS_ON_CRE~ 
BEGIN
  PATCH_IF GAME_IS ~bgee bgt eet~ BEGIN
    READ_LONG 0x2bc er_ioff
    READ_LONG 0x2c0 er_inum
    FOR (er_count = 0; er_count < %er_inum%; er_count += 1) BEGIN
      er_pos = er_ioff+er_count*0x14
      READ_ASCII  er_pos er_item (8)
      INNER_PATCH_SAVE er_imod ~%er_item%~ BEGIN
        REPLACE_TEXTUALLY ~^_~ ~~
      END
      WRITE_EVALUATED_ASCII er_pos ~%er_imod%~ (8)
    END
  END
END

// Patch Glasses of Identification for Finch
DEFINE_PATCH_MACRO ~ER_PATCH_GLASSES~ 
BEGIN
  //PATCH_IF GAME_IS ~bgee tutu tutu_totsc eet bgt~ BEGIN
    SAY NAME1 @60
    SAY NAME2 @60
    SAY UNIDENTIFIED_DESC @61
    SAY DESC @61
  //END
  READ_LONG 0x64 er_ability_offset
  WRITE_ASCII %er_ability_offset%+4 ~IMISC3P~ #8
END

DEFINE_PATCH_FUNCTION GET_SCRIPT_BLOCK
INT_VAR
  start_offset  = 0
  reverse       = 0
STR_VAR
  search_regexp = ~~
RET
  script_block
  start_offset
  end_offset
BEGIN
  SET offset = start_offset
  PATCH_IF (offset < 0) BEGIN SET offset = 0 END
  PATCH_IF (offset > BUFFER_LENGTH) BEGIN SET offset = BUFFER_LENGTH END

  TEXT_SPRINT script_block ~~
  SET start_offset = "-1"
  SET end_offset = "-1"

  PATCH_IF (NOT ~%search_regexp%~ STR_EQ ~~) BEGIN
    PATCH_IF (reverse) BEGIN
      SET offset = RINDEX_BUFFER(~%search_regexp%~ offset)
    END ELSE BEGIN
      SET offset = INDEX_BUFFER(~%search_regexp%~ offset)
    END

    PATCH_IF (offset >= 0) BEGIN
      SET ofsStart = RINDEX_BUFFER(~^IF$~ offset)
      PATCH_IF (ofsStart >= 0) BEGIN
        SET ofsEnd = INDEX_BUFFER(~^END$~ offset)
        PATCH_IF (ofsEnd >= 0) BEGIN
          SET ofsEnd += 3
          READ_ASCII ofsStart script_block (ofsEnd - ofsStart)
          SET start_offset = ofsStart
          SET end_offset = ofsEnd
        END
      END
    END
  END
END
