// Rinco spinning the wheel in Tavern

ON INIT {
 SET §winner 0
 SET £friend "TAVERN"
 INVENTORY ADDMULTI "Jewelry\Gold_coin\Gold_coin" 356
 INVENTORY ADDFROMSCENE "KEY_BASE_0054"
 SET §reflection_mode 0
 SET §won 0		// Used to check gold lost by npc
 ACCEPT
}

ON INITEND {
 TWEAK HEAD "human_villager_head3"
 TWEAK TORSO "human_villager_chest2"
 TWEAK LEGS "human_villager_legs1"
 LOADANIM ACTION4 "human_misc_crowd_cheering"
 SETWEAPON DAGGER
 SENDEVENT -ir DURABILITY 100 "60"
 SET_NPC_STAT armor_class 12
 SET_NPC_STAT absorb 40
 SET_NPC_STAT damages 14
 SET_NPC_STAT tohit 70
 SET_NPC_STAT aimtime 1000
 SET_NPC_STAT life 60
 SET_XP_VALUE 140
 SET §cowardice 5
 SET §pain 4
 SET_CONTROLLED_ZONE RINCO_ZONE
 PLAYANIM WAIT
 ACCEPT
}

ON RELOAD {
 IF (#RINCO_DEAD > 0) DESTROY SELF
 ACCEPT
}

ON PLAYER_ENEMY {
 SENDEVENT CUSTOM GAMBLING_WHEEL_0002 "NO_MORE_BET"
 ACCEPT
}

ON OUCH {
 UNSET_CONTROLLED_ZONE RINCO_ZONE
 TIMERhit -m 1 100 SENDEVENT CUSTOM GAMBLING_WHEEL_0002 "NO_MORE_BET"
 ACCEPT
}

ON CONTROLLEDZONE_ENTER {
 IF (§enemy == 1) ACCEPT
 IF (^$PARAM1 != PLAYER) ACCEPT
 IF (§won > 3) ACCEPT
 IF (^SPEAKING == 1) ACCEPT
 PLAYANIM ACTION4
 SPEAK [Rinco_inn_come_misc] NOP
 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 (§enemy == 1) ACCEPT
  IF (§won > 3) {
    GOSUB START_CHAT
    SPEAK -a [Rinco_inn_loose_too_much] GOSUB END_CHAT
    ACCEPT
  }
  IF (§chatpos == 0) {
     GOSUB START_CHAT
     SET §chatpos 1
	 SETNAME [description_Rinco]
     SPEAK [Rinco_inn_greetings] GOSUB END_CHAT
     ACCEPT
  }
  IF (§chatpos == 1) {
    GOSUB START_CHAT
    RANDOM 50 SPEAK [Rinco_inn_come_misc] GOSUB END_CHAT
    ELSE SPEAK [Rinco_inn_spin_misc] NOP
    ACCEPT
  }
 ACCEPT 
}  

ON WIN {
 IF (§enemy == 1) ACCEPT
 SET §winner 1
 IF (#DEBUG == 1) POPUP "WIN"
 IF (§chatpos == 0) SET §chatpos 1
 INC §won 1
 IF (§won > 3) GOTO LEAVE
 SPEAK -h [Rinco_inn_player_win_misc] SENDEVENT CUSTOM GAMBLING_WHEEL_0002 "PLAY" SET §winner 0
 ACCEPT
}

ON LOOSE {
 IF (§enemy == 1) ACCEPT
 IF (§winner == 1) ACCEPT
 IF (§chatpos == 0) SET §chatpos 1
 IF (^SPEAKING == 1) ACCEPT
 TIMERfeinte 1 2 GOTO SUITE
 ACCEPT
}

>>SUITE
 IF (^SPEAKING == 1) ACCEPT
 SPEAK -a [Rinco_inn_player_loose_misc] SENDEVENT CUSTOM GAMBLING_WHEEL_0002 "PLAY"
 ACCEPT

ON REACHEDTARGET {
  IF (^TARGET == MARKER_0197) {
    OBJECT_HIDE SELF ON
    TIMERdestroy 1 5 DESTROY ME
    ACCEPT
  }
 ACCEPT
}

ON DIE {
 SET #RINCO_DEAD 2
 SENDEVENT CUSTOM GAMBLING_WHEEL_0002 "NO_MORE_BET"
 ACCEPT
}

>>LEAVE
 SENDEVENT CUSTOM GAMBLING_WHEEL_0002 "NO_MORE_BET"
 TIMERlookat -m 1 10 PLAYERLOOKAT SELF
 TIMERloose 1 1 SPEAK -a [Rinco_inn_loose_too_much] GOTO LEAVE_BIS
 ACCEPT

>>LEAVE_BIS
 BEHAVIOR MOVE_TO 
 SETTARGET MARKER_0197 
 ACCEPT