
BEGIN_ACTION_DEFINITION      // note: because you don't want to See the target, it goes in the argument slot, not the target slot
        Name(FindIfOffscreen)
        TRIGGER
	   !See(NearestEnemyOf(Myself))
	   !Detect(scsargument1)
	   !StateCheck(scsargument1,STATE_INVISIBLE)
	   !CheckStatGT(scsargument1,0,SANCTUARY)
	ACTION
           RESPONSE #scsprob1
	   MoveToObject(scsargument1)
END

BEGIN_ACTION_DEFINITION
	Name(ShoutPlayer)
	TRIGGER
	!GlobalGT("helptarget","LOCALS",0)
	Allegiance(Myself,ENEMY)
	See(Playerscsargument1)
	ACTION
	RESPONSE #scsprob1
		Shout(666scsargument1)
		SetGlobal("helptarget","LOCALS",scsargument1)
		SetGlobalTimer("helpduration","LOCALS",12)
		Continue()
END


/// shouts:
///
/// 666: general shout, uttered when I see an enemy. Causes hostility (if I'm in the group) and shouting of 777 (if I'm non-innocent and non-stupid)
/// 777: general shout, uttered when I hear 666. Causes hostility
/// 666N: specific shout, uttered when I see player N. Causes non-innocents who hear it to shout 777N (if non-stupid) and move towards player N
/// 777N: specific shout, uttered when I hear 666N. Causes non-innocents who hear it to move towards player N.

/// handle general (666) shout; replicate previous script, and add CONTINUE to avoid delays before giving specific shouts

/// shout if attacked while neutral, or if hostile and seeing enemies for the first time

/// 777 shouts only happen on Core difficulty or higher

IF
       IgnoreBlock(NoNeutralHelp)
	   Allegiance(Myself,NEUTRAL)
       OR(6)
          AttackedBy([GOODCUTOFF.0.0.0.0.SUMMONED],DEFAULT)
          AttackedBy([PC],DEFAULT)
          AttackedBy([FAMILIAR],DEFAULT)
          AttackedBy([CONTROLLED],DEFAULT)
          AttackedBy([CHARMED],DEFAULT)
          SpellCastOnMe([GOODCUTOFF.0.0.0.0.SUMMONED],0)
	   !StateCheck(LastAttackerOf(Myself),STATE_CONFUSED)	 
THEN
    RESPONSE #100
            Enemy()
            Shout(666)
            Continue()
END

IF
        
	   Global("inafight","LOCALS",1) // don't shout in first script round
	   OR(5)
			HitBy([GOODCUTOFF.0.0.0.0.SUMMONED],CRUSHING)
			HitBy([PC],CRUSHING)
			HitBy([FAMILIAR],CRUSHING)
			HitBy([CONTROLLED],CRUSHING)
			HitBy([CHARMED],CRUSHING)
	   !StateCheck(LastAttackerOf(Myself),STATE_CONFUSED)	
THEN
    RESPONSE #100
             Shout(666)
             Continue()
END

IF
         
	   Allegiance(Myself,ENEMY)
         See(NearestEnemyOf(Myself))
         !GlobalTimerNotExpired("help_initial","LOCALS")
THEN 
         RESPONSE #100
             SetGlobalTimer("help_initial","LOCALS",18)
             Shout(666)
             Continue()
END

/// on hearing 666, go hostile if not hostile already and shout 777
/// innocents of L1 do not propagate shouts and do not go hostile,
/// but they set a variable which causes them to run if they have WDRUNSGT/WTRUNSGT
/// we don't propagate on IWD

IF
         Heard([ANYONE],666)
         groupcheck
         Allegiance(Myself,NEUTRAL)
         Class(Myself,INNOCENT)
         !CheckStatGT(Myself,1,LEVEL)

THEN
         RESPONSE #100
             SetGlobalTimer("runfromPC","LOCALS",60)
             Continue()
END

IF TRIGGER
		 IgnoreBlock(NoNeutralHelp)
		 IgnoreBlock(NoPropagation)
         Heard([ANYONE],666)
         TriggerBlock(CorePlus)
         groupcheck
         Allegiance(Myself,NEUTRAL)
	!HasItem("dw#nohlp",Myself)
         OR(2)
             !Class(Myself,INNOCENT)
             CheckStatGT(Myself,1,LEVEL)
THEN DO
                  Enemy()
                  Shout(777)
                  Continue()
END

IF TRIGGER
		 IgnoreBlock(NoNeutralHelp)
		 RequireBlock(NoPropagation)
         Heard([ANYONE],666)
         TriggerBlock(CorePlus)
         groupcheck
         Allegiance(Myself,NEUTRAL)
	!HasItem("dw#nohlp",Myself)
         OR(2)
             !Class(Myself,INNOCENT)
             CheckStatGT(Myself,1,LEVEL)
THEN DO
                  Enemy()
                  Continue()
END

IF TRIGGER
         IgnoreBlock(NoNeutralHelp)
		 Heard([ANYONE],666)
         TriggerBlock(EasyMinus)
         groupcheck
         Allegiance(Myself,NEUTRAL)
	!HasItem("dw#nohlp",Myself)
         OR(2)
             !Class(Myself,INNOCENT)
             CheckStatGT(Myself,1,LEVEL)
THEN DO
                  Enemy()
                  Continue()
END

/// on hearing 777, go hostile if not already (unless a L1 commoner)

IF
         Heard([ANYONE],777)
         IgnoreBlock(NoPropagation)
		 IgnoreBlock(NoNeutralHelp)
         groupcheck
         Allegiance(Myself,NEUTRAL)
	!HasItem("dw#nohlp",Myself)
         OR(2)
             !Class(Myself,INNOCENT)
             CheckStatGT(Myself,1,LEVEL)
THEN
        RESPONSE #100
                 Enemy()
                 Continue()
END

/// ordinary behaviour is now replicated; move on to the new stuff


/// if I'm currently trying to find a PC and I've been looking for too long, give up.

IF
	GlobalGT("helptarget","LOCALS",0)
	Global("looking","LOCALS",1)
	GlobalTimerExpired("helpduration","LOCALS")
	!See(NearestEnemyOf(Myself))
THEN
	RESPONSE #100
		SetGlobal("helptarget","LOCALS",0)
		SetGlobal("looking","LOCALS",0)
END


/// if I'm not currently chasing someone, am hostile, and see player X, give a Type 1 shout of X's number and set myself as chasing X

IF TRIGGER
THEN DO
	Action(ShoutPlayer,1)
END

IF TRIGGER
	!See(Player1)
THEN DO
	Action(ShoutPlayer,2)
END

IF TRIGGER
	!See(Player1)
	!See(Player2)
THEN DO
	Action(ShoutPlayer,3)
END

IF TRIGGER
	!See(Player1)
	!See(Player2)
	!See(Player3)
THEN DO
	Action(ShoutPlayer,4)
END

IF TRIGGER
	!See(Player1)
	!See(Player2)
	!See(Player3)
	!See(Player4)
THEN DO
	Action(ShoutPlayer,5)
END

IF TRIGGER
	!See(Player1)
	!See(Player2)
	!See(Player3)
	!See(Player4)
	!See(Player5)
THEN DO
	Action(ShoutPlayer,6)
END

BEGIN LOOP(scsplayer||1;2;3;4;5;6) // go through all the players



/// if I'm not currently chasing someone and I hear a type 1 shout about player X, give a type 2 shout of
/// X's number, go hostile, set myself as chasing X, and set a duration for how long I do it.
///

IF TRIGGER
	!GlobalGT("helptarget","LOCALS",0)
	!See(NearestEnemyOf(Myself))
	Heard([ANYONE],666scsplayer)
	IgnoreBlock(NoPropagation)
	TriggerBlock(CorePlus)
	groupcheck
	!GlobalTimerNotExpired("helpduration","LOCALS")
	!HasItem("dw#nohlp",Myself)
	Allegiance(Myself,ENEMY)
         OR(2)
             !Class(Myself,INNOCENT)
             CheckStatGT(Myself,1,LEVEL)
THEN DO
		Action(Literal)
		Shout(777scsplayer)
		SetGlobal("helptarget","LOCALS",scsplayer)
		SetGlobalTimer("helpduration","LOCALS",12)
END

/// if I'm not currently chasing someone and I hear a type 2 shout about player X by someone close to me, give a type 2 shout of
/// X's number, go hostile, set myself as chasing X, and set a duration for how long I do it

IF TRIGGER
	IgnoreBlock(NoPropagation)
	TriggerBlock(CorePlus)
	!GlobalGT("helptarget","LOCALS",0)
	!See(NearestEnemyOf(Myself))
	Heard([ANYONE],777scsplayer)
	groupcheck
	Range(LastHeardBy(Myself),15)
	!Global("help_hostile","LOCALS",1)
	GlobalTimerExpired("helpduration","LOCALS")
	!HasItem("dw#nohlp",Myself)
	Allegiance(Myself,ENEMY)
         OR(2)
             !Class(Myself,INNOCENT)
             CheckStatGT(Myself,1,LEVEL)
THEN DO
                Action(Literal)
		SetGlobal("help_hostile","LOCALS",1)
		SetGlobal("helptarget","LOCALS",scsplayer)
		SetGlobalTimer("helpduration","LOCALS",12)
END

/// if I'm chasing player X but they become invisible, stop

IF
        !See(NearestEnemyOf(Myself))
        !Detect(Playerscsplayer)
	Global("helptarget","LOCALS",scsplayer)
         OR(2)
             !Class(Myself,INNOCENT)
             CheckStatGT(Myself,1,LEVEL)
	OR(2)
               StateCheck(Playerscsplayer,STATE_INVISIBLE)
               CheckStatGT(Playerscsplayer,0,SANCTUARY)
 THEN
    RESPONSE #100
              ClearActions(Myself)
              SetGlobal("helptarget","LOCALS",0)
 END


/// if I can't see an enemy, am chasing player X, and am not a L1 innocent, then move towards player X

IF TRIGGER
        ActionListEmpty()
	Global("helptarget","LOCALS",scsplayer)
	groupcheck
	OR(2)
            Class(Myself,THIEF_ALL)
            Kit(Myself,STALKER)
	!Kit(Myself,SWASHBUCKLER)
         OR(2)
             !Class(Myself,INNOCENT)
             CheckStatGT(Myself,1,LEVEL)
	!HasItem("dw#nohlp",Myself)
THEN DO
		ApplySpellRES("dw#silin",Myself)
		Action(FindIfOffscreen,Playerscsplayer)
END

IF TRIGGER
        ActionListEmpty()
	Global("helptarget","LOCALS",scsplayer)
	groupcheck
	!Class(Myself,INNOCENT)
	!HasItem("dw#nohlp",Myself)
THEN DO
		Action(FindIfOffscreen,Playerscsplayer)
END

END LOOP


/// if I hear a general shout, and am non-innocent,  head for the source.

IF
        OR(2)
            Allegiance(Myself,ENEMY)
            Allegiance(Myself,NEUTRAL)
	!See(NearestEnemyOf(Myself))
	!HasItem("dw#nohlp",Myself)
	Heard([ANYONE],666)
	groupcheck
         OR(2)
             !Class(Myself,INNOCENT)
             CheckStatGT(Myself,1,LEVEL)
THEN
	RESPONSE #100
		MoveToObject(LastHeardBy(Myself))
END

//// whine for help

IF
	RequireBlock(IsWhiner)
        General(Myself,HUMANOID)
	HPPercentLT(Myself,30)
	HPGT(Myself,1)
	!StateCheck(Myself,STATE_REALLY_DEAD)
	!StateCheck(Myself,STATE_DISABLED)
	Global("whined","LOCALS",0)
	See(NearestMyGroupOfType)
	Allegiance(Myself,ENEMY)
THEN
	RESPONSE #50
		SetGlobal("whined","LOCALS",1)
		DisplayStringHead(Myself,46151) // help!
		Shout(ASSIST)
	RESPONSE #50
		SetGlobal("whined","LOCALS",1)
		DisplayStringHead(Myself,49763) // I require assistance
		Shout(ASSIST)
	RESPONSE #50
		SetGlobal("whined","LOCALS",1)
		DisplayStringHead(Myself,49764) // Defend me!
		Shout(ASSIST)
	RESPONSE #50
		SetGlobal("whined","LOCALS",1)
		DisplayStringHead(Myself,49765) // Slay my attacker!
		Shout(ASSIST)
END

IF
        RequireBlock(IsWhiner)
        General(Myself,HUMANOID)
	BreakingPoint()
THEN
	RESPONSE #100
		DisplayStringHead(Myself,49766) // I can't take this
		Shout(ASSIST)
	RESPONSE #100
		DisplayStringHead(Myself,49767) // No more
		Shout(ASSIST)
	RESPONSE #100
		DisplayStringHead(Myself,49768) // Mercy!
		Shout(ASSIST)
END
