// Atok, the traitor

ON INIT {
 SET §cinematic 0
 SET §guilty 0
 SET §player_knows 0
 SET §traitor 0
 SET £friend "goblin_castle"
 SET §reflection_mode 0
 SET §end 0
 ACCEPT
}
ON AGGRESSION {
 IF (§end == 1) {
  SET §end 2
  GOTO CINE
  ACCEPT
 }
 ACCEPT
}

ON INITEND {
 INVENTORY DESTROY
 INVENTORY CREATE
 INVENTORY ADD "MAGIC\\RING_MISERY\\RING_MISERY"
 INVENTORY ADDMULTI "JEWELRY\\GOLD_COIN\\GOLD_COIN" 73
 TWEAK HEAD "goblin_nohelm"
 TWEAK SKIN "npc_goblin_nohelm_head" "npc_goblin_atok_new"
 TWEAK SKIN "npc_Goblin_base_body" "npc_Goblin_leather"
 SET_CONTROLLED_ZONE ATOK_ZONE2
 SETCONTROLLEDZONE KEY
 SET §reflection_mode 0
 INVENTORY ADDFROMSCENE "KEY_BASE_0039" 
 SET £key_carried "KEY_BASE_0003KEY_BASE_0039Portcullis"
 BEHAVIOR FRIENDLY
 SETTARGET PLAYER
 SET_NPC_STAT armor_class 8
 SET_NPC_STAT absorb 10
 SET_NPC_STAT damages 8
 SET_NPC_STAT tohit 30
 SET_NPC_STAT aimtime 10
 SET_NPC_STAT life 32
 SET_XP_VALUE 80
 SET £flee_marker "FLEE_MARKER_0038"
 SET £helping_buddy "GOBLIN_BASE_0035"
// INVENTORY ADD "ARMOR\\CHEST_LEATHER\\CHEST_LEATHER"
 SENDEVENT -ir DURABILITY 100 "37"
 SENDEVENT -ir DURABILITY_ARMOR 100 "44"
 SETWEAPON AXE
 ACCEPT
}

ON CONTROLLEDZONE_ENTER {
 IF (^SPEAKING == 1) ACCEPT
 IF (^$PARAM1 != PLAYER) ACCEPT
 IF (§enemy == 1) ACCEPT
 IF (^$PARAM2 == "ATOK_ZONE2") {
  SET #shut_up 1
  GOTO Dialogue_introduce
  ACCEPT
 }
 ACCEPT
}
ON CONTROLLEDZONE_LEAVE {
 IF (§enemy == 1) ACCEPT
 IF (^$PARAM2 == "KEY") {
  IF (^$PARAM1 == KEY_GOB_CHEST_IDOL_0001) {
   IF (§player_in_sight == 1) {
    UNSET_CONTROLLED_ZONE KEY
    BEHAVIOR FRIENDLY
    SETTARGET PLAYER
    SPEAK [goblin_thief] SENDEVENT ATTACK_PLAYER SELF ""
    ACCEPT 
   }
   ACCEPT
  }
  IF (^$PARAM1 == CUSCION_0001) {
   IF (§player_in_sight == 0) ACCEPT
   TIMERtc OFF
   TIMERgo  OFF
   BEHAVIOR FRIENDLY
   SETTARGET PLAYER
   SPEAK [goblin_thief] NOP
   TIMERgo -i 1 5 GOTO RANDOM  
   ACCEPT
  }
  ACCEPT
 }
 ACCEPT
} 

ON COMBINE {
 IF (§enemy == 1) ACCEPT
 IF (^SPEAKING == 1) SPEAK -p [player_not_now] NOP ACCEPT
 IF (^$PARAM1 == Key_gob_chest_idol_0001) {
  BEHAVIOR FRIENDLY
  SETTARGET PLAYER
  SPEAK -a [Goblin_Thief] SENDEVENT ATTACK_PLAYER SELF ""
  ACCEPT
 }
 ACCEPT
}

ON CHAT {
 IF (^SPEAKING == 1) ACCEPT
 IF (§enemy == 1) ACCEPT
 IF (§chatpos == 0) GOTO Dialogue_introduce
 IF (§chatpos == 1) {
  IF (§player_knows == 0) {
   GOSUB START_CHAT
   SPEAK [atok_misc] GOSUB END_CHAT
   ACCEPT
  } 
  IF (§player_knows == 1) GOTO Dialogue_embarrassed
  ACCEPT
 }
 ACCEPT
}

ON CUSTOM {
  IF (^$PARAM1 == "PLAYER_KNOWS" ) {
    SET §player_knows 1
    ACCEPT
  }
  IF (^$PARAM1 == "IDOL_FOUND" ) {
    IF (§enemy == 1) ACCEPT  // no use to consider the case if Atok allready attacks player
    SET §end 1
    SET £hail []
    SET £victory []
    SET £misc []
    SET £threat []
    SET £search []
    SET £youmad []
    SET £help []
    SET §reflection_mode 0
    SET §door_open 0
    SETGROUP -r £friend
    SET £friend "NONE"
    SENDEVENT CUSTOM Goblin_base_0045 "Die"
    SET §cowardice 0
    TIMERgo OFF 
    BEHAVIOR FRIENDLY
    //TELEPORT Marker_0270
    TELEPORT Marker_0283
    SETTARGET PLAYER
    ACCEPT
  }
  IF (^$PARAM1 == "Go") GOTO DIALOGUE_guilty
 ACCEPT
}
ON REACHEDTARGET {
 IF (^TARGET == MARKER_0270) {
  BEHAVIOR FRIENDLY
  SETTARGET PLAYER
  GOTO REACH
  ACCEPT
 }
 IF (^TARGET == MARKER_0822) {
  BEHAVIOR FRIENDLY 
  SETTARGET BARREL_0008
  TIMERgo 1 5 GOTO RANDOM2 
  ACCEPT
 }
 IF (^TARGET == MARKER_0821) {
  BEHAVIOR WANDER_AROUND 300 
  SETTARGET NONE
  TIMERgo -i 1 15 GOTO RANDOM
  ACCEPT
 }
 ACCEPT
}


>>Dialogue_introduce
 SET #SHUT_UP 1
 SETNAME [description_atok]
 UNSET_CONTROLLED_ZONE ATOK_ZONE2
 SETCONTROLLEDZONE KEY
 SET_PLAYER_CONTROLS OFF
 GOSUB START_CHAT
 TELEPORT -p MARKER_0509
 TIMERlookat -m 1 10 PLAYERLOOKAT ME
 CINEMASCOPE -s ON
 PLAYERINTERFACE HIDE
 SPEAK -o [atok_greetings] GOTO Dialogue_introduce02
 ACCEPT

>>Dialogue_introduce02
 SPEAK -cp ZOOM 0 90 0 90 100 100 [player_atok_introduce] GOTO Dialogue_introduce03
 ACCEPT

>>Dialogue_introduce03
 SPEAK -c ZOOM 0 20 0 20 100 100 [atok_greetings2] GOTO Dialogue_introduce04
 ACCEPT

>>Dialogue_introduce04
 SPEAK -cp ZOOM 0 90 0 90 100 100 [player_Atok_sorry] GOTO Dialogue_introduce05
 ACCEPT

>>Dialogue_introduce05
 SPEAK -c ZOOM 0 20 0 20 100 100 [atok_insulting] GOTO Dialogue_introduce06
 ACCEPT

>>Dialogue_introduce06
 IF (§player_knows == 1) GOTO Dialogue_embabis
 GOTO Dialogue_introduce07
 ACCEPT

>>Dialogue_introduce07
 GOSUB END_CHAT
 SET #SHUT_UP 1
 SET §reflection_mode 1
 CINEMASCOPE -s off
 SET_PLAYER_CONTROLS ON
 PLAYERINTERFACE SHOW
 SET §chatpos 1 
 SETMOVEMODE WALK
 BEHAVIOR Wander_around 300
 SETTARGET NONE
 TIMERgo -i 1 15 GOTO RANDOM
 ACCEPT

>>RANDOM
IF ( §main_behavior_stacked != 0 ) {
 BEHAVIOR NONE
 SETTARGET NONE
 TIMERtc 1 5 GOTO RANDOM
 ACCEPT
}
BEHAVIOR MOVE_TO
SETTARGET MARKER_0822
TIMERgo OFF
ACCEPT

>>RANDOM2
IF ( §main_behavior_stacked != 0 ) {
 BEHAVIOR NONE
 SETTARGET NONE
 TIMERtc 1 5 GOTO RANDOM2
 ACCEPT
}
BEHAVIOR MOVE_TO
SETTARGET MARKER_0821
ACCEPT

>>Dialogue_embarrassed
 GOSUB START_CHAT
 SET #SHUT_UP 1
 SET_PLAYER_CONTROLS OFF
 PLAYERLOOKAT ME
 CINEMASCOPE -s ON 
 PLAYERINTERFACE -s HIDE
 SPEAK -p [player_to_atok01] GOTO Dialogue_embarrassed02
 ACCEPT

>>Dialogue_embarrassed02
SPEAK [atok_upset] GOSUB END_CHAT SET_PLAYER_CONTROLS ON CINEMASCOPE -s OFF PLAYERINTERFACE SHOW 
SET §player_knows 0
SET #shut_up 0
ACCEPT

>>Dialogue_embabis
 SET §chatpos 1 
 SPEAK -ocp SIDE goblin_base_0017 0 100 50 50 0 [player_to_atok01] GOTO Dialogue_emba2bis
 ACCEPT

>>Dialogue_emba2bis
SPEAK -oc KEEP [atok_upset] GOSUB END_CHAT SET §reflection_mode 1 BEHAVIOR Wander_around 300 SETTARGET NONE GOTO END
ACCEPT

>>END
SET #SHUT_UP 0
SETPLAYERCONTROLS ON
CINEMASCOPE -s OFF 
PLAYERINTERFACE -s SHOW
CONVERSATION OFF
SET §player_knows 0
BEHAVIOR Wander_around 200
SETTARGET NONE
TIMERgo -i 1 15 GOTO RANDOM
ACCEPT

>>CINE
SENDEVENT OPEN Light_door_0030 ""
GOTO CINE2
ACCEPT

>>DIALOGUE_guilty
 IF (§enemy == 1) ACCEPT
 IF (§guilty == 1) ACCEPT
>>CINE2
 CINEMASCOPE -s ON 
 SET_PLAYER_CONTROLS  OFF
 PLAYERINTERFACE -s HIDE
 SET §end 2
 SET §cine 2
 SET #shut_up 1
 SET §guilty 1
 BEHAVIOR MOVE_TO
 SETTARGET MARKER_0270
 TIMERlook -m 5 50 PLAYERLOOKAT ME
 ACCEPT

>>REACH
 PLAYERLOOKAT ME 
 TIMERpouet 1 1 SPEAK -o [atok_attack] GOTO ATTACK
 ACCEPT

>>ATTACK
SET #SHUT_UP 0
CINEMASCOPE -s OFF
SET_PLAYER_CONTROLS ON
PLAYERINTERFACE -s SHOW
SENDEVENT ATTACK_PLAYER SELF ""
SET §traitor 1
SETNAME [description_atok2]
SENDEVENT CUSTOM Light_door_0030 "LOCK"
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 -----------------------------------------------------------
// ---------------------------------------------------------------------------------

//#