// Goblin City Gate Keeper

ON INIT {
 SET §atok_here 0
 SET §going 0
 SET §opened 0
 SET §given 0
 SET £friend "goblin_castle"
 SET £key_carried "Portcullis"
 SET §first 0 
 INVENTORY ADD "PROVISIONS\\BREAD_HALF\\BREAD_HALF"
 ACCEPT
}

ON INITEND {
 LOADANIM ACTION4 "goblin_normal_actionlever"
 BEHAVIOR FRIENDLY
 SETTARGET MARKER_0083
 SET £flee_marker "FLEE_MARKER_0047"
 SET £helping_buddy "GOBLIN_BASE_0035"
 SENDEVENT -ir DURABILITY 100 "32"
 SENDEVENT -ir DURABILITY_ARMOR 100 "27"
 TWEAK SKIN "npc_Goblin_base_body" "npc_Goblin_lordboi"
 ACCEPT
}

ON CHAT {
//GOTO NOWWW
 IF (^SPEAKING == 1) ACCEPT
 IF (§enemy == 1) ACCEPT
 IF (§chatpos == 0) {
  >>RETALK
  GOSUB START_CHAT
  SENDEVENT CUSTOM GOBLIN_BASE_0041 "NOTNOW"
  PLAYERINTERFACE HIDE
  SET #SHUT_UP 1
  CINEMASCOPE -s ON
  SET_PLAYER_CONTROLS OFF
  SPEAK -p [player_open_door] GOTO DIALOGUE01
  ACCEPT
 }
 IF (§chatpos == 11) {
  SPEAK [Goblin_generic] NOP
  ACCEPT
 }
 IF (§chatpos == 3) {
  IF (§atok_here == 1) {
   SPEAK [Goblin_notrade] SENDEVENT CUSTOM Goblin_base_0041 "Go_on" 
   ACCEPT
  } 
  SPEAK [Goblin_generic] NOP
  ACCEPT
 }
 IF (§chatpos == 4) {
  IF (§opened == 1) {
   GOSUB START_CHAT 
   SPEAK [Goblin_generic] GOSUB END_CHAT
   ACCEPT
  }
  IF (§opened == 0) SPEAK -p [player_open_door] SETEVENT CHAT OFF BEHAVIOR MOVE_TO SETTARGET -a Lever_0005 //SET §going 1 
  ACCEPT
 }
 ACCEPT
}

ON COMBINE {
  IF (^SPEAKING == 1) {
    SPEAK -p [player_not_now] NOP
    ACCEPT
  }
  IF (§given == 1) ACCEPT
  IF (§chatpos == 11) {
  IF (^$PARAM1 ISCLASS "GEM_DEALER_BADGE" ) {
    SET §given 1
    GOTO DIALOGUE02
    ACCEPT
  }
   ACCEPT
  }
 ACCEPT
}

ON CUSTOM {
  IF (^$PARAM1 == "PORTCULLIS_OPEN" ) {
>>NOWWW
    SET §atok_here 0
    SENDEVENT CUSTOM LEVER_0005 "SECURED"
    PLAYERLOOKAT ME
    SPEAK [Goblin_ok] NOP //SENDEVENT CUSTOM LEVER_0005 "Open" 
    CINEMASCOPE -s OFF
    SET §reflection_mode 1
    SET §chatpos 4
    SET §open 1 
    SET §opened 1
    BEHAVIOR MOVE_TO
    SETTARGET -a LEVER_0005
    //SETTARGET PLAYER
     TIMERenter 1 20 GOTO LEVER_DECIDE 
    ACCEPT
  }
  IF (^$PARAM1 == "Opening") {
   IF (§enemy == 1) ACCEPT
   SET §opened 1
  TIMERclose 1 15 GOTO LEVER
   ACCEPT
  }
  IF (^$PARAM1 == "Closing") {
   IF (§enemy == 1) ACCEPT
   TIMERclose OFF
   SET §opened 0
   BEHAVIOR MOVE_TO
   SETTARGET -a Marker_0269
   ACCEPT
  }
  IF (^$PARAM1 == "Attak_me") {
   BEHAVIOR STACK
   SETEVENT CHAT OFF
   BEHAVIOR -f Move_to
   SETTARGET Goblin_base_0017
   ACCEPT
  }
  IF (^$PARAM1 == "Stop_attak") {
   BEHAVIOR UNSTACK
   SETEVENT CHAT OFF
   ACCEPT
  }
 IF (^$PARAM1 == "HERE") {
  IF (§chapos == 3) ACCEPT
  SET §atok_here 1
  SET §reflection_mode 0
  ACCEPT
 }
 ACCEPT
}

ON REACHEDTARGET {
 IF (^TARGET == Lever_0005) {
  IF (§first == 0) {
   SET §first 1
   BEHAVIOR MOVE_TO
   SENDEVENT CUSTOM LEVER_0005 "Open"
   PLAYANIM -e ACTION4 SETTARGET -a MARKER_0269
   ACCEPT
  }
  BEHAVIOR FRIENDLY
  SETTARGET Lever_0005
  IF (§opened == 1) GOTO Reached
  IF (§opened == 0) GOTO Reached2
  IF (§opened == 2) GOTO Stop
  ACCEPT
 }
 IF (^TARGET == MARKER_0269) {
  SETEVENT CHAT ON
  BEHAVIOR FRIENDLY
  SETTARGET MARKER_0083
  ACCEPT
 }
 ACCEPT
}

//DIALOGUES

>>DIALOGUE01
 SPEAK -o [Goblin_no_access] GOTO DIALOGUE01_FOLLOWUP01
ACCEPT

>>DIALOGUE01_FOLLOWUP01
 SPEAK -p [player_Heyopen] GOTO DIALOGUE01_FOLLOWUP02
ACCEPT

>>DIALOGUE01_FOLLOWUP02
 PLAYERINTERFACE SHOW
 CINEMASCOPE -s OFF
 SET #SHUT_UP 0
 SET §chatpos 11
 SET_PLAYER_CONTROLS ON 
 GOSUB END_CHAT
 SPEAK -o [Goblin_guard_toplayer3]
ACCEPT

>>DIALOGUE02
GOSUB START_CHAT
  SENDEVENT CUSTOM GOBLIN_BASE_0041 "NOTNOW"
  PLAYERINTERFACE HIDE
  SET #SHUT_UP 1
  CINEMASCOPE -s ON  
  SET_PLAYER_CONTROLS OFF 
SPEAK -p [player_gemdealer] goto DIALOGUE02_FOLLOWUP01
ACCEPT

>>DIALOGUE02_FOLLOWUP01
IF (§atok_here == 1) {
 SPEAK [Goblin_notrade] SENDEVENT CUSTOM Goblin_base_0041 "Go_on" 
 GOSUB END_CHAT
 ACCEPT
}
SPEAK [Goblin_notrade] GOTO DIALOGUE02_FOLLOWUP02
ACCEPT

>>DIALOGUE02_FOLLOWUP02
SPEAK -p [player_just_walking] GOTO DIALOGUE02_FOLLOWUP03
ACCEPT

>>DIALOGUE02_FOLLOWUP03
SPEAK -o [Goblin_notrade2] NOP
PLAYERINTERFACE SHOW
CINEMASCOPE -s OFF
SET #SHUT_UP 0
SET_PLAYER_CONTROLS ON 
SET §chatpos 3
GOSUB END_CHAT
ACCEPT

>>Reached
IF (§going == 1) {
 SENDEVENT CUSTOM SELF "Opening"
 SET §going 0
 ACCEPT
}
PLAYANIM ACTION4
TIMERleft -m 1 400 SENDEVENT CUSTOM Lever_0005 "Close"
GOTO Reached1_1
ACCEPT

>>Reached1_1
BEHAVIOR Move_to
SETTARGET -a Marker_0269
Set §opened 0
ACCEPT

>>Reached2
BEHAVIOR Move_to
PLAYANIM -e ACTION4 SETTARGET -a Marker_0269
TIMERleft -m 1 400 SENDEVENT CUSTOM Lever_0005 "Open"
GOTO Reached2_2
ACCEPT

>>Reached2_2
BEHAVIOR Move_to
SETTARGET -a Marker_0269
SET §opened 1
ACCEPT

>>Stop
BEHAVIOR Move_to
SETTARGET -a Marker_0269
SET §opened 0
ACCEPT

>>LEVER_DECIDE
IF ( §main_behavior_stacked != 0 ) {
 BEHAVIOR NONE
 SETTARGET NONE
 TIMERtrc 1 5 GOTO LEVER_DECIDE
 ACCEPT
}
BEHAVIOR MOVE_TO
SETTARGET -a Lever_0005
ACCEPT

>>LEVER
BEHAVIOR MOVE_TO
SETTARGET -a Lever_0005
ACCEPT

>>LEVERCLOSE
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 -----------------------------------------------------------
// ---------------------------------------------------------------------------------

//#