// Kultar in Jail
ON INIT {
 SET §helped_kultar 0
 SET §reflection_mode 0
 SET §cowardice 0
 SET §gobdead 0				// Used to check if player has killed the goblin
 SET §leverdown 0				// Used to check if player has already open the cell
 SET §dooropen 0                    // metal door
 SET §talked 0				// Check kultar has talked to player
 SET §attacked 0
 SET §player_still_in_cell 0		// Returns player has thrown object on gob and still in
 SET §stone 0				// Used to check if stone has been removed.
 ACCEPT
}

ON INITEND {
 INVENTORY DESTROY
 INVENTORY CREATE
 SET_NPC_STAT life 5
 SET §cowardice 10
 LOADANIM WALK "human_kultar_walk_wounded"
 LOADANIM WAIT "human_kultar_wait_wounded"
 LOADANIM ACTION1 "human_guard_wounded_cycle"
 LOADANIM ACTION2 "human_guard_wounded_getup"
 TWEAK LEGS "human_villager_legs1"
 TWEAK HEAD "human_kultar"
 TWEAK SKIN "npc_human_hero_naked_body" "npc_human_base_naked_body"
 PLAYANIM -L ACTION1
 SET §frozen 1
 INVENTORY DESTROY // reset empty inventory 
 INVENTORY CREATE
 ACCEPT
}

ON RELOAD {
 IF (#HELPED_KULTAR > 0) SET §helped_kultar 1
 DESTROY SELF 
 ACCEPT
}

ON GAME_READY {
 SETGORE OFF
 ACCEPT
}

ON DIE {
  IF (§helped_kultar == 0) {
    SEND_EVENT CUSTOM JAIL_WOOD_GRID_0001 "TALK"
    ACCEPT
  }
 REFUSE
}

ON MAIN {
  IF (§chatpos == 3) {			//warning about gob
    IF (^DIST_PLAYER < 400) {
      IF (^SPEAKING == 0) {
        SET §chatpos 4
        SPEAK [KULTAR_encourage22] NOP
        ACCEPT
      }
     ACCEPT
    }
   ACCEPT
  }
 ACCEPT
}

>>CHECK_DIST
  IF (^DIST_PLAYER < 500) {		// first presentation
    TIMERdist OFF
    SET §chatpos 2
    GOTO dialogue
    ACCEPT
  }
 ACCEPT

>>LETMEOUT
 IF (^SPEAKING == 1) ACCEPT
 SPEAK [KULTAR_letmeout] NOP
 ACCEPT

ON REACHEDTARGET {
  IF (^TARGET == MARKER_0117) {	// walk near grids
    BEHAVIOR FRIENDLY
    SETTARGET PLAYER
    IF (§player_still_in_cell == 1) ACCEPT
    IF (§chatpos == 0) {
      IF (§stone == 1) SET §chatpos 1 ACCEPT
      SPEAK -o [KULTAR_INTRODUCTION] TIMERdist 0 1 GOTO CHECK_DIST
      ACCEPT
    }
   ACCEPT
  }
  IF (^TARGET == MARKER_0001) {			//walk to portcullis when gob dead
    IF (§leverdown == 1) {
      SET §chatpos 7
      ACCEPT
    }
   SET §chatpos 6
   BEHAVIOR FRIENDLY
   SETTARGET PLAYER
   TIMERhelp 4 18 GOTO LETMEOUT
   ACCEPT
  }
  IF (^TARGET == MARKER_0854) {
    BEHAVIOR MOVE_TO
    SETTARGET MARKER_0150
    ACCEPT
  }
  IF (^TARGET == MARKER_0882) {
    BEHAVIOR WANDER_AROUND 200
    SETTARGET NONE
    ACCEPT
  }
  IF (^TARGET == MARKER_0150) {		//walk outside of the cell if gob dead
    IF (§talked == 0) { 
      PLAYER_LOOKAT SELF  
      BEHAVIOR FRIENDLY
      SETTARGET PLAYER
      SET §talked 1
      GOTO dialogue10
      ACCEPT
    }
   ACCEPT
  }
  IF (^TARGET == MARKER_0381) {		// Kultar waiting around for player to go
    BEHAVIOR FRIENDLY
    SETTARGET JAIL_WOOD_GRID_0001
    ACCEPT
  }
  IF (^TARGET == MARKER_0679) {
    BEHAVIOR FRIENDLY
    SETTARGET JAIL_WOOD_GRID_0001
    SPEAK -o [Kultar_cantgo] GOTO dialogue15
    TIMERtrois 1 4 SETTARGET PLAYER
    ACCEPT
  }
 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 == 1) {
    SET §chatpos 2
    GOTO dialogue
    ACCEPT
  }
  IF (§chatpos == 2) {			//player must find way out
    SPEAK [KULTAR_LOOKWELL] SENDEVENT CUSTOM goblin_base_0006 "PRISONER_CHATTING"
    ACCEPT
  }
  IF (§chatpos == 3) {
    SET §chatpos 4
    GOSUB START_CHAT
    SPEAK [KULTAR_encourage] GOSUB END_CHAT
    ACCEPT
  }
  IF (§chatpos == 4) {			//way out found, beware of the gob
    SPEAK [KULTAR_encourage2] SENDEVENT CUSTOM goblin_base_0006 "PRISONER_CHATTING"
    ACCEPT
  }
  IF (§chatpos == 6) {			//gob dead but cell closed
    SPEAK [KULTAR_letmeout] TIMERhelp OFF
    ACCEPT
  }
  IF (§chatpos == 7) {
    IF (§leverdown == 0) GOTO LETMEOUT
    GOTO OUT
    ACCEPT
  }
  IF (§chatpos == 8) {
    GOSUB START_CHAT
    SPEAK [Kultar_killthegob] GOSUB END_CHAT
    ACCEPT
  }
  IF (§chatpos == 9) {			//kultar is out waiting for player to go 
    GOSUB START_CHAT
    SPEAK [KULTAR_thanks_again] GOSUB END_CHAT
    ACCEPT
  }
 ACCEPT
}

ON COLLIDE_NPC {
 IF (^SENDER != PLAYER) ACCEPT
// IF (§chatpos == 7) GOTO OUT
 ACCEPT
}

ON AGGRESSION {
 IF (#HELPED_KULTAR == 0) ACCEPT
 SET #HELPED_KULTAR 0
 ACCEPT
}

ON OUCH {
 IF (§attacked == 0) SPEAK [Kultar_fightplayer]
 SET §attacked 1
 ACCEPT
}

>>DEATHFALL
 ADD_XP 40
 OBJECT_HIDE SELF ON
 PLAY "blackthing_arrival"
 SET #HELPED_KULTAR 10
 FORCEDEATH ME
 TIMERmort 1 1 DESTROY ME
ACCEPT

ON CUSTOM {
  IF (^$PARAM1 == "STONE") {			// Stone is removed
    SET §stone 1
    ACCEPT
  }
  IF (^$PARAM1 == "DIE") {
    SENDEVENT CUSTOM GOBLIN_BASE_0040 "DESTROY"
	SPEAK [Kultar_fall] GOTO DEATHFALL
    ACCEPT
  }  
  IF (§enemy == 1) ACCEPT
  IF (^$PARAM1 == "ATTACKING") {		// Goblin attaking player still in cell
    SET §chatpos 8
    SET §player_still_in_cell 1
    ACCEPT
  }
  IF (^$PARAM1 == "GETUP") {			// coming from marker 117
    SPEAK -o [Kultar_Hey] SET §frozen 0
    PLAYANIM -2 NONE
    PLAYANIM ACTION2				// this sends kultar to talk for the first time
    BEHAVIOR MOVE_TO
    SETTARGET MARKER_0117
    ACCEPT
  }
  IF (^$PARAM1 == "FOUND") {			// player activated untwisted portcullis2
    IF (^SPEAKING == 1) {
      TIMERdist OFF
      SET §player_still_in_cell 1
      ACCEPT
    }
    SPEAK [Kultar_encourage23] SET §chatpos 3
    TIMERdist OFF
    SET §player_still_in_cell 1
    BEHAVIOR WANDER_AROUND 200
    SETTARGET NONE
    ACCEPT
  }
  IF (^$PARAM1 == "HIT") {			//gob hit
    IF (§enemy == 1) ACCEPT
    RANDOM 40 {
      TIMERpouet -m 1 1500 SPEAK [Kultar_killthegob] NOP
      ACCEPT
    }
   ACCEPT
  }
  IF (^$PARAM1 == "DEAD") {			//gobdead
    IF (§enemy == 1) ACCEPT
    SET §gobdead 1
    SET §chatpos 0
    BEHAVIOR MOVE_TO
    SETTARGET MARKER_0001
    ACCEPT
  }
  IF (^$PARAM1 == "DOOR_OPEN") {		//metal door open
    SET §dooropen 1				//test if lever is down
    IF (§leverdown == 1) ACCEPT
    SPEAK [Kultar_please] SPEAK -p [player_amshagaar] NOP
    ACCEPT
  }
  IF (^$PARAM1 == "LEVER_UP") {
    SET §leverdown 0
    ACCEPT
  }
  IF (^$PARAM1 == "LEVER_DOWN") {
    SET §leverdown 1
    SET §cowardice 0
    IF (§talked == 1) ACCEPT
    IF (§gobdead == 0) ACCEPT
    TIMERhelp OFF
    SET §gobdead 2
>>OUT
    BEHAVIOR MOVE_TO
    SETTARGET MARKER_0854
    SET #HELPED_KULTAR 1
    SENDEVENT CUSTOM CAMERA_0080 "CINEMATIC"
    ACCEPT
  }
  IF (^$PARAM1 == "DOOR_LOCKED") {
    IF (#HELPED_KULTAR == 0) ACCEPT
    IF (^SPEAKING == 1) ACCEPT
    IF (§advicegiven == 1) ACCEPT
    SPEAK -o [Kultar_door_locked] SET §advicegiven 1
    ACCEPT
  }
  IF (^$PARAM1 == "GOB_OPEN") {
    IF (§gobdead == 1) ACCEPT
    SET §leverdown 1
    ACCEPT
  }
 ACCEPT
}

>>dialogue
 SET #SHUT_UP 1
 TELEPORT -p MARKER_0382
 SENDEVENT CUSTOM CAMERA_0039 "CINEMATIC"
 SET_PLAYER_CONTROLS OFF
 CINEMASCOPE -s ON
 PLAYER_INTERFACE -s HIDE
 TIMERlookat -m 1 10 PLAYERLOOKAT SELF 
 SPEAK -p [player_kultar_wait] GOTO dialogue2
 ACCEPT

>>dialogue2
 CAMERA_ACTIVATE CAMERA_0038
 SPEAK [kultar_explain] GOTO dialogue3
 ACCEPT

>>dialogue3
 SPEAK [Kultar_find_something] GOTO dialogue4
 ACCEPT

>>dialogue4
 CAMERA_ACTIVATE NONE
 SET_PLAYER_CONTROLS ON
 CINEMASCOPE -s OFF
 PLAYER_INTERFACE -s SHOW
 HEROSAY [system_tutorial_3]
 QUEST [system_tutorial_3]
 SET #SHUT_UP 0
 ACCEPT

>>dialogue10
 SET #SHUT_UP 1
 SETNAME [description_kultar]
 SENDEVENT CINEMATIC CAMERA_0080 ""
 SPEAK -o [kultar_thanks] GOTO dialogue11
 ACCEPT

>>dialogue11
 SENDEVENT CUSTOM JAIL_WOOD_GRID_0001 "TALK"
 SENDEVENT CINEMATIC CAMERA_0080 ""
 SPEAK -po [player_welcome_kultar] GOTO dialogue12
 ACCEPT

>>dialogue12
 SENDEVENT CINEMATIC CAMERA_0080 ""
 SPEAK -o [Kultar_traveler] GOTO dialogue13
 ACCEPT

>>dialogue13
 SENDEVENT CINEMATIC CAMERA_0080 ""
 SPEAK -po [Player_traveler_ok] GOTO dialogue14
 ACCEPT

>>dialogue14
 SENDEVENT CINEMATIC CAMERA_0080 ""
 BEHAVIOR MOVE_TO
 SETTARGET MARKER_0679
 ACCEPT

>>dialogue15
 TIMERtrois OFF
 BEHAVIOR MOVE_TO
 SETTARGET MARKER_0381
 QUEST [system_Quest_log_escape_gobs]
 HEROSAY [system_questbook_updated]
 PLAY "system"
 SENDEVENT CUSTOM CAMERA_0080 "END"
 SET §chatpos 9
 SET #SHUT_UP 0
 ACCEPT