// Tafiok in throne room

ON INIT {
 SET £friend "kingdom"
 INVENTORY ADDMULTI "JEWELRY\\GOLD_COIN\\GOLD_COIN" 80
 INVENTORY ADD "JEWELRY\\GEM\\GEM"
 INVENTORY ADD "QUEST_ITEM\\gem_dealer_badge\\gem_dealer_badge"
 ACCEPT
}

ON INITEND {
 SET §reflection_mode 0
 OBJECT_HIDE -m SELF ON
 TWEAK HEAD "human_villager_head1"
 TWEAK TORSO "human_villager_chest1"
 TWEAK LEGS "human_villager_legs3"
 TWEAK SKIN "npc_human_villager_leg1b_chest1_a" "npc_human_clothes_c"
 TWEAK SKIN "npc_human_villager_leg3a_chest2b" "npc_human_clothes_b"
 IF (#TAFIOK_IDed == 1) SETNAME [description_tafiok]
 ACCEPT
}

ON RELOAD {
  IF (#tafiok_dead == 1) DESTROY SELF
  IF (#PLAYER_ON_QUEST == 4) {
    OBJECT_HIDE SELF OFF
    ACCEPT
  }
  IF (#PLAYER_ON_QUEST == 6) DESTROY SELF
 ACCEPT
}

ON DIE {
 SET #tafiok_dead 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 == 1) {
    GOSUB START_CHAT
    SPEAK [Human_male_hello] GOSUB END_CHAT
    ACCEPT
  }
 GOSUB START_CHAT
 TIMERspeak 1 20 GOTO TALK    
 SET §chatpos 1
 SPEAK [Human_male_hello] GOSUB END_CHAT
 ACCEPT
}
 
ON CUSTOM {
  IF (#SHUT_UP == 1) ACCEPT
  IF (^SPEAKING == 1) {
    SENDEVENT CUSTOM HUMAN_BASE_0139 "TALK"
    ACCEPT
  }
  IF (^$PARAM1 == "TALK") {
    BEHAVIOR FRIENDLY
    SETTARGET HUMAN_BASE_0139
    SPEAK [customer_tavern_reflection_male] TIMERanswerb 1 2 SENDEVENT CUSTOM HUMAN_BASE_0139 "TALK"
    TIMERspeak 1 20 GOTO TALK    
    ACCEPT
  }
 ACCEPT
}
 
>>TALK
 IF (#SHUT_UP == 1) ACCEPT
 BEHAVIOR FRIENDLY
 SETTARGET HUMAN_BASE_0139
 SPEAK [customer_tavern_reflection_male] TIMERanswera 1 2 SENDEVENT CUSTOM HUMAN_BASE_0139 "TALK"
 ACCEPT