// Goblin Lord guarding the reserve
ON INIT {
 SET £type goblin_lord
 INVENTORY ADD_FROM_SCENE "KEY_BASE_0003"
 SET §chatpos 0
 SET §ronde 1
 SET £friend "goblin_castle"
 SET £key_carried "KEY_BASE_0003 Portcullis"
 SET £init_marker "Marker_0280"
 SET §fire_angry 0
// INVENTORY ADD "ARMOR\\CHEST_LEATHER\\CHEST_LEATHER"
 INVENTORY ADD "PROVISIONS\\TORCH\\TORCH"
 ACCEPT
}

ON INITEND {
>>ROND
 BEHAVIOR Move_to
 SETTARGET Marker_0280
 SETMOVEMODE WALK
 SET £flee_marker "FLEE_MARKER_0046"
 SET £helping_buddy "GOBLIN_BASE_0035"
 SENDEVENT -ir DURABILITY 100 "41"
 SENDEVENT -ir DURABILITY_ARMOR 100 "45"
 INVENTORY ADDMULTI "JEWELRY\\GOLD_COIN\\GOLD_COIN" 17
 SETWEAPON Longsword_Iron
 SET_NPC_STAT damages 8
 ACCEPT
}

ON LOAD {
 USE_MESH "Goblin_lord\\Goblin_lord.teo"
 ACCEPT
}
ON RELOAD {
 SET §fire_angry 0
 ACCEPT
}
ON _HAT {
 IF (^SPEAKING == 1) ACCEPT
 IF (§chatpos == 0) {
   GOSUB START_CHAT
   SET #SHUT_UP 1
   PLAYERLOOKAT ME
   CINEMASCOPE -s ON
   SETPLAYERCONTROLS OFF
   PLAYERINTERFACE -s HIDE
   SPEAK -p [player_open_door] SPEAK [GoblinLord_noneshallpass] GOSUB END_CHAT SET §chatpos 1 GOTO Player_knows
   ACCEPT
 }
 IF (§chatpos == 1) {
   GOSUB START_CHAT
   SPEAK [GoblinLord_onlyatok] GOSUB END_CHAT
   SET §chatpos 4
   ACCEPT
  }
  IF (§chatpos == 2) {
//   GOSUB START_CHAT
   TIMERrond1 OFF
   TIMERrond2 OFF
   CINEMASCOPE -s ON
   PLAYERINTERFACE -s HIDE
   SETPLAYERCONTROLS OFF
   SET #SHUT_UP 1
   TELEPORT -p MARKER_0562
   TIMERlookat -m 1 10 PLAYERLOOKAT MARKER_0723
   BEHAVIOR MOVE_TO
   SETTARGET MARKER_0563
   SENDEVENT CUSTOM LIGHT_DOOR_0030 "UNLOCK"
   //SENDEVENT CUSTOM Goblin_base_0042 "Return"
   SET §chatpos 4
   SPEAK [GoblinLord_youcango] NOP 
   ACCEPT
  }
  IF (§chatpos == 4) {
   GOSUB START_CHAT
   SPEAK [Goblinlord_warning] GOSUB END_CHAT
   ACCEPT
  }
  ACCEPT
 }

ON _USTOM {
  IF (^$PARAM1 == "RIGHT") {
   IF (§chatpos != 4) {
    SPEAK [Goblinlord_thief] NOP
    SENDEVENT ATTACK_PLAYER SELF ""
    ACCEPT
   }
   ACCEPT
  }
  IF (^$PARAM1 == "KING_OPEN" ) {
    SET §chatpos 2
    ACCEPT
  }
  IF (^$PARAM1 == "Die") {
   SETGROUP -r £friend
   SET £friend "NONE"
   FORCEDEATH SELF
   ACCEPT
  }
   IF (^$PARAM1 == "Thief") {
   IF (^SPEAKING == 1) {
    SET §enemy 1
    SENDEVENT ATTACK_PLAYER SELF ""
    ACCEPT
   }
   SPEAK [Goblinlord_thief] NOP
   SET §enemy 1
   SENDEVENT ATTACK_PLAYER SELF ""
   ACCEPT
  }
 IF (^$PARAM1 == "LIGHT_FIRE") GOTO LIGHT_FIRE
 ACCEPT
}

ON _EACHEDTARGET {
 IF (^TARGET == Marker_0563) {
  BEHAVIOR FRIENDLY
  SETTARGET Light_door_0030
  PLAY "Door_unlock"
  TIMERgt 1 1 GOTO ENDC
  ACCEPT
 }
 IF (^TARGET == Marker_0280) {
  BEHAVIOR FRIENDLY
  SETTARGET Marker_0281
  TIMERrond1 1 15 GOTO NEXT1
  ACCEPT
 }
 IF (^TARGET == Marker_0283) {
  IF (§ronde == 0) {
   BEHAVIOR FRIENDLY
   SETTARGET Marker_0281
   ACCEPT
  }
  BEHAVIOR FRIENDLY
  SETTARGET Marker_0282
  TIMERrond2 1 15 GOTO NEXT2
  ACCEPT
 }
 IF (^TARGET == MARKER_0650) {
  BEHAVIOR FRIENDLY
  SETTARGET CHEST_METAL_0002
  SPELLCAST -SMF 1 IGNIT MARKER_0650  
  TIMERgo 1 1 GOTO ROND
  ACCEPT
 }
 ACCEPT
}

>>Player_knows
 SET #SHUT_UP 0
 CINEMASCOPE -s OFF
 PLAYERINTERFACE -s SHOW
 SETPLAYERCONTROLS ON
 SENDEVENT CUSTOM GOBLIN_BASE_0017 "PLAYER_KNOWS"
 SENDEVENT CUSTOM GOBLIN_BASE_0042 "PLAYER_KNOWS"
 ACCEPT

>>ENDC
 SET #SHUT_UP 0
SENDEVENT CUSTOM Chest_metal_0002 "Open"
SENDEVENT OPEN LIGHT_DOOR_0030 ""
SET §ronde 0
BEHAVIOR MOVE_TO
SETTARGET MARKER_0283
SETPLAYERCONTROLS ON
PLAYERINTERFACE -s SHOW
CINEMASCOPE -s OFF
ACCEPT

>>NEXT1
IF ( §main_behavior_stacked != 0 ) {
 BEHAVIOR NONE
 SETTARGET NONE
 TIMERtc 1 5 GOTO NEXT1
 ACCEPT
}
BEHAVIOR Move_to 
SETTARGET Marker_0283
ACCEPT

>>NEXT2
IF ( §main_behavior_stacked != 0 ) {
 BEHAVIOR NONE
 SETTARGET NONE
 TIMERtrc 1 5 GOTO NEXT2
 ACCEPT
}
BEHAVIOR Move_to 
SETTARGET Marker_0280
ACCEPT

>>LIGHT_FIRE
IF (§fire_angry == 2) {
 SPEAK [Goblinlord_warning] SENDEVENT ATTACK_PLAYER SELF ""
 ACCEPT
}
IF (§enemy == 1) ACCEPT
SPEAK [Goblinlord_warning] NOP
TIMErond1 OFF
TIMERrond2 OFF
TIMERtc OFF
TIMERtrc OFF
BEHAVIOR MOVE_TO
SETTARGET MARKER_0650
INC §fire_angry 1
ACCEPT


//#behavior

// ---------------------------------------------------------------------------------
// BEHAVIOR INCLUDE START ----------------------------------------------------------
// ---------------------------------------------------------------------------------
>>CLEAR_MICE_TARGET
 // if we care about mice : clear current mice target and listen to other mice
 IF ( §care_about_mice == 1 )
 {
  SET £targeted_mice "NOMOUSE"
  SETGROUP MICECARE // listen to other mice
 }
RETURN

>>SAVE_BEHAVIOR
 TIMERcolplayer OFF 
// to avoid TIMERcolplayer to restore the behavior 1 sec later
 IF (§main_behavior_stacked == 0) 
 {
  IF (§frozen == 1)
  { 
// frozen anim -> wake up !
    SET §frozen 0
    PLAYANIM NONE
    PLAYANIM -2 NONE
    PHYSICAL ON
    COLLISION ON
    BEHAVIOR FRIENDLY
    SETTARGET PLAYER
  }
  SET §main_behavior_stacked 1
  HEROSAY -d "STACK"
  BEHAVIOR STACK
 }
 ELSE
 { // behavior already saved : clear mice target if one
  GOSUB CLEAR_MICE_TARGET
 }
RETURN

>>RESTORE_BEHAVIOR
 IF (§main_behavior_stacked == 1) 
 {
  GOSUB CLEAR_MICE_TARGET
  HEROSAY -d "UNSTACK"
  BEHAVIOR UNSTACK
  SET §main_behavior_stacked 0
  RETURN
 }
IF (£init_marker != "NONE")
  { 
   BEHAVIOR MOVE_TO
   SETTARGET -a ~£init_marker~
   SETMOVEMODE WALK
   RETURN
  }
HEROSAY -d "go_home"
BEHAVIOR GO_HOME
SETTARGET PLAYER
RETURN

// ---------------------------------------------------------------------------------
// BEHAVIOR INCLUDE END ------------------------------------------------------------
// ---------------------------------------------------------------------------------

//#
//#LocalChat
// ---------------------------------------------------------------------------------
// LocalChat INCLUDE START ---------------------------------------------------------
// ---------------------------------------------------------------------------------
>>START_CHAT
 // save misc reflection mode
 SET §saved_reflection §reflection_mode
 // no more stupid reflections !
 SET §reflection_mode 0
 // fo not change behavior if frozen
 IF ( §frozen == 1 ) RETURN
 // save behavior (if not saved)
 GOSUB SAVE_BEHAVIOR
 // look at player
 BEHAVIOR FRIENDLY
 SETTARGET PLAYER
RETURN

>>END_CHAT
 SET_EVENT COLLIDE_NPC ON
 SET §collided_player 0
 // restor behavior only if not in fighting mode
 IF ( §fighting_mode == 0 )
 {
  // restore misc reflection mode
  SET §reflection_mode §saved_reflection
  // if frozen : don't restore behavior
  IF ( §frozen == 1 ) RETURN
  // restore behavior
  GOSUB RESTORE_BEHAVIOR
 }
RETURN
// ---------------------------------------------------------------------------------
// LocalChat INCLUDE END -----------------------------------------------------------
// ---------------------------------------------------------------------------------

//#

ON CHAT {
 IF (^SPEAKING == 1) ACCEPT
 IF (§chatpos == 0) {
   GOSUB START_CHAT
   SET #SHUT_UP 1
   PLAYERLOOKAT ME
   CINEMASCOPE -s ON
   SETPLAYERCONTROLS OFF
   PLAYERINTERFACE -s HIDE
   SPEAK -p [player_open_door] SPEAK [GoblinLord_noneshallpass] GOSUB END_CHAT SET §chatpos 1 GOTO Player_knows
   ACCEPT
 }
 IF (§chatpos == 1) {
   GOSUB START_CHAT
   SPEAK [GoblinLord_onlyatok] GOSUB END_CHAT
   SET §chatpos 4
   ACCEPT
  }
  IF (§chatpos == 2) {
//   GOSUB START_CHAT
   TIMERrond1 OFF
   TIMERrond2 OFF
   CINEMASCOPE -s ON
   PLAYERINTERFACE -s HIDE
   SETPLAYERCONTROLS OFF
   SET #SHUT_UP 1
   TELEPORT -p MARKER_0562
   TIMERlookat -m 1 10 PLAYERLOOKAT MARKER_0723
   BEHAVIOR MOVE_TO
   SETTARGET MARKER_0563
   SENDEVENT CUSTOM LIGHT_DOOR_0030 "UNLOCK"
   //SENDEVENT CUSTOM Goblin_base_0042 "Return"
   SET §chatpos 4
   SPEAK [GoblinLord_youcango] NOP 
   TIMERend 1 4 GOTO ENDER
   ACCEPT
  }
  IF (§chatpos == 5) {
   GOSUB START_CHAT
   SET §chatpos 4
   SPEAK [GoblinLord_youcango] GOSUB END_CHAT
   ACCEPT
  }
  IF (§chatpos == 4) {
   GOSUB START_CHAT
   SPEAK [Goblinlord_warning] GOSUB END_CHAT
   ACCEPT
  }
  ACCEPT
 }

ON REACHEDTARGET {
 IF (^TARGET == Marker_0563) {
 >>ENDER
  IF (§ender == 1) ACCEPT
  SET §ender 1
  BEHAVIOR FRIENDLY
  SETTARGET Light_door_0030
  PLAY "Door_unlock"
  TIMERgt 1 1 GOTO ENDC
  ACCEPT
 }
 IF (^TARGET == Marker_0280) {
  BEHAVIOR FRIENDLY
  SETTARGET Marker_0281
  TIMERrond1 1 15 GOTO NEXT1
  ACCEPT
 }
 IF (^TARGET == Marker_0283) {
  IF (§ronde == 0) {
   BEHAVIOR FRIENDLY
   SETTARGET Marker_0281
   ACCEPT
  }
  BEHAVIOR FRIENDLY
  SETTARGET Marker_0282
  TIMERrond2 1 15 GOTO NEXT2
  ACCEPT
 }
 IF (^TARGET == MARKER_0650) {
  BEHAVIOR FRIENDLY
  SETTARGET CHEST_METAL_0002
  SPELLCAST -SMF 1 IGNIT MARKER_0650  
  TIMERgo 1 1 GOTO ROND
  ACCEPT
 }
 ACCEPT
}

ON CUSTOM {
  IF (^$PARAM1 == "RIGHT") {
   IF (§chatpos < 4) {
    SPEAK [Goblinlord_thief] NOP
    SENDEVENT ATTACK_PLAYER SELF ""
    ACCEPT
   }
   ACCEPT
  }
  IF (^$PARAM1 == "KING_OPEN" ) {
    SENDEVENT CUSTOM LIGHT_DOOR_0030 "UNLOCK"
    SET §chatpos 5
    ACCEPT
  }
  IF (^$PARAM1 == "Die") {
   SETGROUP -r £friend
   SET £friend "NONE"
   FORCEDEATH SELF
   ACCEPT
  }
   IF (^$PARAM1 == "Thief") {
   IF (^SPEAKING == 1) {
    SET §enemy 1
    SENDEVENT ATTACK_PLAYER SELF ""
    ACCEPT
   }
   SPEAK [Goblinlord_thief] NOP
   SET §enemy 1
   SENDEVENT ATTACK_PLAYER SELF ""
   ACCEPT
  }
 IF (^$PARAM1 == "LIGHT_FIRE") GOTO LIGHT_FIRE
 ACCEPT
}

ON DIE {
 INVENTORY ADD "ARMOR\\HELMET_CHAIN\\HELMET_CHAIN"
  ACCEPT
 }