// Tafiok, in the jewelery shop

ON INIT {
 INVENTORY ADD_FROM_SCENE "KEY_BASE_0029"
 INVENTORY ADD_FROM_SCENE "KEY_BASE_0073"
 INVENTORY ADD "JEWELRY\\GOLD_RING\\GOLD_RING"
 INVENTORY ADD "JEWELRY\\GEM\\GEM"
 INVENTORY ADD "JEWELRY\\necklace\\necklace"
 SET £key_carried "KEY_BASE_0029KEY_BASE_0073"
 SET #FORBIDEN_SHOP3 0	// returns 1 if Tafiok is in shop
 SET #tafiok_dead 0		// Used to check if player has killed Tafiok in another level
 SET £friend "kingdom"
 SET §map 0				// Used to check if marker is already on the map
 SET_GROUP ANTI_THIEF
 ACCEPT
}

ON INITEND {
 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"
 SETWEAPON DAGGER_JEWELD
 SENDEVENT -ir DURABILITY 100 "75"
 SET_NPC_STAT damages 12
 SET_NPC_STAT aimtime 700
 BEHAVIOR FRIENDLY
 SETTARGET PLAYER
 ACCEPT
} 

ON RELOAD {
  IF (#tafiok_dead == 1) DESTROY SELF
  IF (#PLAYER_ON_QUEST == 0) {
>>OPEN
    TELEPORT MARKER_0030
    BEHAVIOR NONE
    SETTARGET NONE
    FORCE_ANGLE 270
    SENDEVENT CUSTOM LIGHT_DOOR_0022 "UNLOCK"
    SENDEVENT CUSTOM LIGHT_DOOR_0024 "LOCK"
    SENDEVENT CUSTOM CHEST_METAL_0024 "UNLOCK"
    SENDEVENT CUSTOM CHEST_METAL_0033 "UNLOCK"
    SENDEVENT CUSTOM CHEST_METAL_0042 "UNLOCK"
    SENDEVENT CUSTOM CHEST_METAL_0116 "UNLOCK"
    SET #FORBIDEN_SHOP3 1
	IF (#TAFIOK_IDed == 0) SETNAME [description_shopkeeper]
    ACCEPT
  } 
  IF (#PLAYER_ON_QUEST == 2) {
   GOTO OPEN
   ACCEPT
  }
  IF (#PLAYER_ON_QUEST == 3) {
   GOTO OPEN
   ACCEPT
  }
  IF (#PLAYER_ON_QUEST == 1) {
    TELEPORT MARKER_0027
    BEHAVIOR FRIENDLY
    SETTARGET PLAYER
    SENDEVENT CUSTOM LIGHT_DOOR_0022 "LOCK"
    SENDEVENT CUSTOM LIGHT_DOOR_0024 "UNLOCK"
    SENDEVENT CUSTOM CHEST_METAL_0024 "LOCK"
    SENDEVENT CUSTOM CHEST_METAL_0033 "LOCK"
    SENDEVENT CUSTOM CHEST_METAL_0042 "LOCK"
    SENDEVENT CUSTOM CHEST_METAL_0116 "LOCK"
    SET #FORBIDEN_SHOP3 0
	IF (#TAFIOK_IDed == 0) SETNAME [description_human_male]
    ACCEPT
  }
  IF (#PLAYER_ON_QUEST == 4) {
    OBJECT_HIDE SELF ON
    SENDEVENT CUSTOM LIGHT_DOOR_0022 "LOCK"
    SENDEVENT CUSTOM LIGHT_DOOR_0024 "LOCK"
    SENDEVENT CUSTOM CHEST_METAL_0024 "LOCK"
    SENDEVENT CUSTOM CHEST_METAL_0033 "LOCK"
    SENDEVENT CUSTOM CHEST_METAL_0042 "LOCK"
    SENDEVENT CUSTOM CHEST_METAL_0116 "LOCK"
	IF (#TAFIOK_IDed == 0) SETNAME [description_human_male]
    ACCEPT
  }
  IF (#PLAYER_ON_QUEST == 5) {
   OBJECT_HIDE SELF OFF
   GOTO OPEN
   ACCEPT
  }
  IF (#PLAYER_ON_QUEST == 6) {
    TELEPORT MARKER_0027
    SET §reflection_mode 0
    PLAYANIM NONE
    BEHAVIOR NONE
    SETTARGET NONE
    OBJECT_HIDE SELF ON
    ACCEPT
  }
  IF (#PLAYER_ON_QUEST > 6) {
    OBJECT_HIDE SELF OFF
    SET §reflection_mode 1
    TELEPORT MARKER_0030
    FORCE_ANGLE 270
    BEHAVIOR NONE
    SETTARGET NONE
    SENDEVENT CUSTOM LIGHT_DOOR_0022 "UNLOCK"
    SENDEVENT CUSTOM LIGHT_DOOR_0024 "LOCK"
    SENDEVENT CUSTOM CHEST_METAL_0024 "UNLOCK"
    SENDEVENT CUSTOM CHEST_METAL_0033 "UNLOCK"
    SENDEVENT CUSTOM CHEST_METAL_0042 "UNLOCK"
    SENDEVENT CUSTOM CHEST_METAL_0116 "UNLOCK"
    SET #FORBIDEN_SHOP3 1
	IF (#TAFIOK_IDed == 0) SETNAME [description_shopkeeper]
    ACCEPT
  } 
 ACCEPT
}

ON AGGRESSION {
 SENDEVENT PLAYER_ENEMY HUMAN_BASE_0114 ""
 ACCEPT
}

ON PLAYER_IN_SHOP {
 IF (§player_in_sight == 0) ACCEPT
 IF (^PLAYER_ZONE != "FORBIDEN_SHOP3") ACCEPT
 SPEAK -a ~£thief~ NOP
 SENDEVENT ATTACK_PLAYER SELF ""
 ACCEPT
}
 
ON DIE {
 SENDEVENT CUSTOM CHEST_METAL_0024 "DEAD"
 SENDEVENT CUSTOM CHEST_METAL_0042 "DEAD"
 SENDEVENT CUSTOM CHEST_METAL_0033 "DEAD"
 SENDEVENT CUSTOM CHEST_METAL_0116 "DEAD"
 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 (#FORBIDEN_SHOP3 == 1) {
    IF (§chatpos == 1) {
     IF (§map == 0) {
       SET §map 1
       MAPMARKER 365 305 1 [description_tafiok]
     }
     IF (#troll_quest > 1) {
       SET §chatpos 2
       GOTO STRIKE_OFF
       ACCEPT
      }
      GOSUB START_CHAT
      SPEAK [tafiok_still_strike_misc] GOSUB END_CHAT
      ACCEPT
    }
    IF (§chatpos == 2) {
>>STRIKE_OFF
      GOSUB START_CHAT
      SPEAK [male_shop_offer] GOSUB END_CHAT
	  IF (#TAFIOK_IDed == 0) SETNAME [description_shopkeeper]
      ACCEPT
    }
    IF (§chatpos == 3) {
      SPEAK [tafiok_thanks] SET §chatpos 2
      ACCEPT
    }
   GOTO DIALOGUE1_1
   ACCEPT
  }
 GOSUB START_CHAT
 MAPMARKER 365 305 1 [description_tafiok]
 SPEAK [male_shop_visit] GOSUB END_CHAT
 IF (#TAFIOK_IDed == 0) SETNAME [description_shopkeeper]
 ACCEPT
}

ON COMBINE {
 IF (^SPEAKING == 1) {
   SPEAK -p [player_not_now] NOP
   ACCEPT
 }
 IF (^$PARAM1 ISGROUP "RAWGEM") GOTO DIALOGUE3_1
 IF (^$PARAM1 ISGROUP "JEWELRY") SPEAK [male_shop_offer]
 ACCEPT
}

////////////////////////////////////////
// First dialogue and troll on strike //
////////////////////////////////////////

>>DIALOGUE1_1
 IF (#FORBIDEN_SHOP3 == 0) ACCEPT
 SET_PLAYER_CONTROLS OFF
 PLAYER_LOOKAT ME
 PLAYER_INTERFACE -s HIDE
 CINEMASCOPE -s ON
 GOSUB START_CHAT
 IF (#TROLL_QUEST < 2) GOTO DIALOGUE1_2
 SET §chatpos 2
 SET #TAFIOK_IDed 1
 SETNAME [description_tafiok]
 SPEAK [tafiok_my_name] GOTO DIALOGUE1_5
 ACCEPT

>>DIALOGUE1_2
 SET #TAFIOK_IDed 1
 SETNAME [description_tafiok]
 SPEAK [tafiok_bad_days] GOTO DIALOGUE1_3
 ACCEPT

>>DIALOGUE1_3
 SPEAK -p [player_another_way] GOTO DIALOGUE1_4
 ACCEPT

>>DIALOGUE1_4
 SPEAK [tafiok_no_goblins] GOTO DIALOGUE1_5
 ACCEPT

>>DIALOGUE1_5
 SET §chatpos 1
 PLAYER_INTERFACE -s SHOW
 CINEMASCOPE -s OFF
 SET_PLAYER_CONTROLS ON
 GOSUB END_CHAT
 ACCEPT

////////////////////////////////////////////
// Player gives gem while troll on strike //
////////////////////////////////////////////

>>DIALOGUE3_1
  IF (#TROLL_QUEST > 1) {
    SPEAK [male_shop_offer] SET §chatpos 2
	IF (#TAFIOK_IDed == 0) SETNAME [description_shopkeeper]
    ACCEPT
  }
  IF (§chatpos == 3) {
    DESTROY ^SENDER
    SENDEVENT CUSTOM CHEST_METAL_0024 "GOTGEMS"
    SENDEVENT CUSTOM CHEST_METAL_0042 "GOTGEMS"
    SENDEVENT CUSTOM CHEST_METAL_0033 "GOTGEMS"
    SENDEVENT CUSTOM CHEST_METAL_0116 "GOTGEMS"
	ADDGOLD 180
    ACCEPT
  } 
 GOSUB START_CHAT
 DESTROY ^SENDER
 SENDEVENT CUSTOM CHEST_METAL_0024 "GOTGEMS"
 SENDEVENT CUSTOM CHEST_METAL_0042 "GOTGEMS"
 SENDEVENT CUSTOM CHEST_METAL_0033 "GOTGEMS"
 SENDEVENT CUSTOM CHEST_METAL_0116 "GOTGEMS"
 ADDGOLD 180
 SET §chatpos 3
 SPEAK [tafiok_thanks] GOSUB END_CHAT
 ACCEPT