// Miguel, the blacksmith
ON INIT {
 INVENTORY ADD_FROM_SCENE "KEY_BASE_0031"
 INVENTORY ADD_FROM_SCENE "KEY_BASE_0074"
 INVENTORY ADDMULTI "JEWELRY\\GOLD_COIN\\GOLD_COIN" 16
 INVENTORY ADD "ARMOR\\HELMET_CHAIN\\HELMET_CHAIN"
 SET £key_carried "KEY_BASE_0031KEY_BASE_0074"
 SET #FORBIDEN_SHOP1 0					// returns 1 if Miguel is in shop
 SET §knownName 0
 SET §talked_superweapon 0				// returns player has asked for superweapon
 SET §teleported 0					// Indicates Miguel has already been teleported
 SET £friend "kingdom"
 SET_GROUP ANTI_THIEF
 SET §inuse 0
 SET £weapon "none"
 SET §refuse 0						// Used to check if npc can collide with player
 SET §allready 0
 ACCEPT
}

ON INITEND {
 LOADANIM ACTION3 "human_work_blacksmith"
 TWEAK HEAD "human_villager_head5"
 TWEAK LEGS "human_villager_legs1"
 TWEAK TORSO "human_villager_chest1"
 SETWEAPON NONE
 SENDEVENT -ir DURABILITY 100 "90"
 SET_NPC_STAT damages 20
 SET_NPC_STAT aimtime 700
 TWEAK SKIN "npc_human_villager5_bard" "npc_human_villager5_miguel"
 TWEAK SKIN "npc_human_villager_leg1b_chest1_a" "npc_human_clothes_a"
 TWEAK SKIN "npc_human_villager_leg1a_chest1_b" "npc_human_clothes_a"
 TIMERdecision 1 10 GOTO WALK_AROUND_FIRST
 ACCEPT
}

ON RELOAD {
  IF (#PLAYER_ON_QUEST == 6) {
    SET §reflection_mode 0
    TIMERdebugor 1 1 GOTO RELOAD2
    ACCEPT
  }  
  IF (#PLAYER_ON_QUEST == 7) {
   IF (§allready == 1) GOTO RELOAD3
   SET §allready 1
   TIMERdebugor 1 1 GOTO RELOAD4
   ACCEPT
  }
  IF (#PLAYER_ON_QUEST > 4) {
    SET §teleported 0
    IF (§teleported == 1) ACCEPT
    TIMERdebugor 1 1 GOTO RELOAD3
    ACCEPT
  }
  IF (#PLAYER_ON_QUEST == 4) {
    SET §teleported 0
    IF (§teleported == 1) ACCEPT
    TIMERdebugor 1 1 GOTO RELOAD4
    ACCEPT
  }
  IF (#PLAYER_ON_QUEST > 0) {
    IF (§teleported == 1) ACCEPT
    TIMERdebugor 1 1 GOTO RELOAD1
   }
 ACCEPT
}

ON COLLIDE_NPC {
 IF (§refuse == 1) REFUSE
 ACCEPT
}

>>RELOAD1
 TIMERdecision OFF
 TIMERgo_back OFF
 SET §teleported 1
 TELEPORT MARKER_0077
 BEHAVIOR FRIENDLY
 SETTARGET CHEST_METAL_0039
 SENDEVENT CUSTOM LIGHT_DOOR_0028 "UNLOCK"
 SENDEVENT CUSTOM CHEST_METAL_0034 "UNLOCK"
 SENDEVENT CUSTOM CHEST_METAL_0035 "UNLOCK"
 SENDEVENT CUSTOM CHEST_METAL_0036 "UNLOCK"
 SENDEVENT CUSTOM CHEST_METAL_0037 "UNLOCK"
 SENDEVENT CUSTOM CHEST_METAL_0038 "UNLOCK"
 SENDEVENT CUSTOM CHEST_METAL_0039 "UNLOCK"
 SENDEVENT CUSTOM CHEST_METAL_0040 "UNLOCK"
 SENDEVENT CUSTOM CHEST_METAL_0041 "UNLOCK"
 SET_CONTROLLED_ZONE FORBIDEN_SHOP1
 SETBLACKSMITH ON
 SET §refuse 1
 SET §chatpos 1
 SET #FORBIDEN_SHOP1 1
 ACCEPT

>>RELOAD2
 SET #FORBIDEN_SHOP1 0
 TIMERdecision OFF
 TIMERgo_back OFF
 TELEPORT MARKER_0017
 PLAYANIM NONE
 SET §frozen 0
 BEHAVIOR NONE
 SETTARGET NONE
 SETBLACKSMITH OFF
 OBJECT_HIDE SELF ON
 ACCEPT

ON PLAYER_IN_SHOP {
 IF (§player_in_sight == 0) ACCEPT
 IF (^PLAYER_ZONE != "FORBIDEN_SHOP1") ACCEPT
 SPEAK -a ~£thief~ NOP
 SENDEVENT ATTACK_PLAYER SELF ""
 ACCEPT
}

ON CONTROLLEDZONE_ENTER {
 IF (^$PARAM1 != PLAYER) ACCEPT
 UNSET_CONTROLLED_ZONE FORBIDEN_SHOP1
 BEHAVIOR FRIENDLY
 SETTARGET PLAYER
 SET §knownName 1
 SETNAME [description_miguel]
 MAPMARKER 195 390 1 [description_miguel]
 SPEAK [miguel_welcome] SET §chatpos 1
 TIMERwork 1 10 BEHAVIOR MOVE_TO SETTARGET MARKER_0297
 ACCEPT
}

ON REACHEDTARGET {
  IF (^TARGET == MARKER_0297) {
    BEHAVIOR NONE
    SETTARGET NONE
    ATTACH TOOL_BLACKSMITH_HAMMER3_0002 "ACTION POINT" SELF "primary_attach"
    OBJECT_HIDE TOOL_BLACKSMITH_HAMMER3_0002 OFF
    SET £attached_object "TOOL_BLACKSMITH_HAMMER3_0002"
    ATTACH TOOL_BLACKSMITH_SWORD2_0005 "ACTION POINT" SELF "left_attach"
    OBJECT_HIDE TOOL_BLACKSMITH_SWORD2_0005 OFF
    SET £attached_object "TOOL_BLACKSMITH_SWORD2_0005"
    PLAYANIM -l ACTION3
    SET §frozen 1
    SET §refuse 0
    ACCEPT
  }
  IF (^TARGET == MARKER_0017) {
    BEHAVIOR WANDER_AROUND 200
    SETTARGET NONE
    ACCEPT
  }
  IF (^TARGET == MARKER_0061) {
    BEHAVIOR WANDER_AROUND 400
    SETTARGET NONE
    TIMERgo_back 1 60 GOTO BACK_HOME
    ACCEPT
  }
 ACCEPT
}

ON DIE {
 SENDEVENT CUSTOM CHEST_METAL_0034 "DEAD"
 SENDEVENT CUSTOM CHEST_METAL_0035 "DEAD"
 SENDEVENT CUSTOM CHEST_METAL_0036 "DEAD"
 SENDEVENT CUSTOM CHEST_METAL_0037 "DEAD"
 SENDEVENT CUSTOM CHEST_METAL_0038 "DEAD"
 SENDEVENT CUSTOM CHEST_METAL_0039 "DEAD"
 SENDEVENT CUSTOM CHEST_METAL_0040 "DEAD"
 SENDEVENT CUSTOM CHEST_METAL_0041 "DEAD"
 SETBLACKSMITH OFF
 ACCEPT
}

ON PLAYER_ENEMY {
SETBLACKSMITH OFF
ACCEPT
}

ON OUCH {
SETBLACKSMITH OFF
ACCEPT
}

ON COMBINE {
  IF (§inuse == 1) GOTO NOTNOW
  IF (^SPEAKING == 1) GOTO NOTNOW
  SET £weapon ^$PARAM1
  IF (£weapon ISGROUP ARMORY) {
    SET £price ^REPAIRPRICE_~£weapon~
    IF (^REPAIRPRICE_~£weapon~ < 0) GOTO FULL
    IF (^REPAIRPRICE_~£weapon~ > ^PLAYER_GOLD) GOTO NOTENOUGHGOLD
    SET #TMP -1
    DEC #TMP ~£price~
    ADDGOLD #TMP
    SET §inuse 1
    TIMERklong 1 1 PLAY "Anvil_hit"
    TIMERrepair 1 1 GOTO REPAIR
    ACCEPT
  }
 SPEAK -p [player_no2] NOP
 ACCEPT
}

>>FULL
 SPEAK -p [player_weapon_already_repaired] NOP
 ACCEPT

>>NOTNOW
 SPEAK -p [player_not_now] NOP
 ACCEPT

>>REPAIR
 REPAIR ~£weapon~ 200
 SPEAK -p [player_weapon_repaired_in_full] NOP
 SET §inuse 0
 ACCEPT

>>NOTENOUGHGOLD
 HEROSAY [system_not_enough_gold]
 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_SHOP1 == 1) {
    IF (§talked_superweapon == 0) {
      IF (#need_superweapon == 1) {
        GOTO SUPERWEAPON1_1
      }
    }
    IF (§chatpos == 1) {
      GOSUB START_CHAT
      SPEAK [miguel_buy_misc] GOSUB END_CHAT
      ACCEPT
    }
   ACCEPT
  }
 GOSUB START_CHAT
 MAPMARKER 195 390 1 [description_miguel]
 IF (§knownName == 0) SETNAME [description_shopkeeper]
 SPEAK [male_shop_visit] GOSUB END_CHAT
 ACCEPT
}

>>WALK_AROUND_FIRST
 BEHAVIOR MOVE_TO
 SETTARGET MARKER_0061
 ACCEPT

>>BACK_HOME
 BEHAVIOR MOVE_TO
 SETTARGET MARKER_0017
 ACCEPT

>>SUPERWEAPON1_1
 GOSUB START_CHAT
 SPEAK -p [player_about_dwarf] GOTO SUPERWEAPON1_2
 ACCEPT

>>SUPERWEAPON1_2
 SET §talked_superweapon 1
 SPEAK [miguel_about_dwarf] GOSUB END_CHAT
 ACCEPT

>>RELOAD3
 SENDEVENT CUSTOM LIGHT_DOOR_0029 "LOCK"
 SENDEVENT CUSTOM LIGHT_DOOR_0028 "UNLOCK"
 SENDEVENT CUSTOM CHEST_METAL_0034 "UNLOCK"
 SENDEVENT CUSTOM CHEST_METAL_0035 "UNLOCK"
 SENDEVENT CUSTOM CHEST_METAL_0036 "UNLOCK"
 SENDEVENT CUSTOM CHEST_METAL_0037 "UNLOCK"
 SENDEVENT CUSTOM CHEST_METAL_0038 "UNLOCK"
 SENDEVENT CUSTOM CHEST_METAL_0039 "UNLOCK"
 SENDEVENT CUSTOM CHEST_METAL_0040 "UNLOCK"
 SENDEVENT CUSTOM CHEST_METAL_0041 "UNLOCK"
 SET §teleported 1
 TIMERdecision OFF
 TIMERgo_back OFF
 OBJECT_HIDE SELF OFF
 SETBLACKSMITH ON
 SET §reflection_mode 1
 SET #FORBIDEN_SHOP1 1
 TELEPORT MARKER_0297
 FORCE_ANGLE 180
 BEHAVIOR NONE
 SETTARGET NONE
 SENDEVENT CUSTOM TOOL_BLACKSMITH_HAMMER3_0002 "SHOW"
 SENDEVENT CUSTOM TOOL_BLACKSMITH_SWORD2_0005 "SHOW"
 ATTACH TOOL_BLACKSMITH_HAMMER3_0002 "ACTION POINT" SELF "primary_attach"
 OBJECT_HIDE TOOL_BLACKSMITH_HAMMER3_0002 OFF
 SET £attached_object "TOOL_BLACKSMITH_HAMMER3_0002"
 ATTACH TOOL_BLACKSMITH_SWORD2_0005 "ACTION POINT" SELF "left_attach"
 OBJECT_HIDE TOOL_BLACKSMITH_SWORD2_0005 OFF
 SET £attached_object "TOOL_BLACKSMITH_SWORD2_0005"
 PLAYANIM -l ACTION3
 SET §frozen 1
 ACCEPT

>>RELOAD4
 SENDEVENT CUSTOM LIGHT_DOOR_0029 "UNLOCK"
 SENDEVENT CUSTOM LIGHT_DOOR_0028 "LOCK"
 SENDEVENT CUSTOM CHEST_METAL_0034 "LOCK"
 SENDEVENT CUSTOM CHEST_METAL_0035 "LOCK"
 SENDEVENT CUSTOM CHEST_METAL_0036 "LOCK"
 SENDEVENT CUSTOM CHEST_METAL_0037 "LOCK"
 SENDEVENT CUSTOM CHEST_METAL_0038 "LOCK"
 SENDEVENT CUSTOM CHEST_METAL_0039 "LOCK"
 SENDEVENT CUSTOM CHEST_METAL_0040 "LOCK"
 SENDEVENT CUSTOM CHEST_METAL_0041 "LOCK"
 SET #FORBIDEN_SHOP1 0
 SET §teleported 1
 TIMERdecision OFF
 TIMERgo_back OFF
 OBJECT_HIDE SELF OFF
 TELEPORT MARKER_0017
 PLAYANIM NONE
 SET §frozen 0
 DETACH TOOL_BLACKSMITH_HAMMER3_0002 SELF
 SENDEVENT CUSTOM TOOL_BLACKSMITH_HAMMER3_0002 "HIDE"
 DETACH TOOL_BLACKSMITH_SWORD2_0005 SELF
 SENDEVENT CUSTOM TOOL_BLACKSMITH_SWORD2_0005 "HIDE"
 SET £attached_object "NONE"
 BEHAVIOR WANDER_AROUND 200
 SETTARGET NONE
 SETBLACKSMITH OFF
 ACCEPT