//The different states
//0: Guard Captain
//1: Ranged Combat: Get in position
//2: Ranged Combat: Aim
//3: Ranged Combat: Shoot
//4: Melee Combat
//7: Move towards the trouble

//-----------------------------------------------------------------------------------------
//Just in case
IfSpawned
  JoinGoodTeam

//-----------------------------------------------------------------------------------------
//Handle death
IfKilled
  CallForHelp			//Warn friends

  //Drop goodies
  tmpargument = 65535
  DropMoney
  DropItems
  DropKeys

  //Death sound
  tmpargument = 1
  PlaySound

  //Send message
  tmpargument = 0
  IfTargetIsOnSameTeam
    tmpargument = 5
    IfTargetIsSelf
      tmpargument = 4
  SendMessageNear

  // Make the character body
  tmpargument = 45
  SetBumpHeight

//-----------------------------------------------------------------------------------------
// Get mean if characters wanna be bad
IfAttacked
  tmpargument = rand & 1 + 4
  PlaySound
  SetTargetToWhoeverAttacked
    IfTargetIsOnHatedTeam
      CallForHelp		//Enemy attacked
    Else
      IfTargetIsAPlayer		//Player got into trouble
        CallForHelp
        JoinEvilTeam
        tmpargument = 1
        SetState
      Else			//Wops, a friendly NPC attacked
        SetTargetToOldTarget

IfCalledForHelp
  DisableExport
  JoinEvilTeam
  tmpargument = 1
  SetState

//-----------------------------------------------------------------------------------------
//Main AI loop
IfTimeOut

  //Reset speech
  IfStateIs2
    tmpargument = 0
    SetState

  //MOVE AROUND - GUARD
  IfStateIs0
    Sneak			//Move at 33% speed
    tmpargument = rand & 60 + 60
    SetTime
    SetTargetToNearestEnemy
      tmpargument = 1
      SetState			//Charge in melee!
    Else
      tmpx = selfspawnx
      tmpy = selfspawny
      ClearWaypoints
      AddWaypoint

  //MELEE COMBAT - CHARGE AND FIGHT
  IfStateIs1
    Run
    tmpargument = 15
    SetTime
    SetTargetToNearestEnemy	//Move towards enemy
      tmpx = targetx
      tmpy = targety
      tmpturn = targetturnto
      tmpdistance = 850
      Compass
      ClearWaypoints
      AddWaypoint
    Else				//No enemies in sight
      tmpargument = 0			//begin guarding again
      SetState
    tmpx = targetdistance
    tmpy = 200			//Can we attack?
    IfXIsLessThanY
      IfFacingTarget
        IfTargetIsAlive
          IfTargetIsOnHatedTeam
            tmpargument = LATCHRIGHT	//Yep!
            PressLatchButton

//-----------------------------------------------------------------------------------------
//Use shield against attacking enemies
IfTargetIsAttacking		//Must be attacking
  IfTargetIsOnHatedTeam		//Must be enemy
    tmpargument = [SHIE]	//Holding shield?
    IfHoldingItemID
      IfFacingTarget
        tmpx = targetdistance
        tmpy = 300 + rand & 500
        IfXIsLessThanY
          tmpargument = LATCHLEFT
          PressLatchButton	//Yep, defend!
      Else
        GetAttackTurn		//Position from the attack
        tmpx = selfx
        tmpy = selfy
        Compass
        ClearWaypoints
        AddWaypoint
      

//-----------------------------------------------------------------------------------------
//How to react if bumped
IfBumped				// Bumped
  SetTargetToWhoeverBumped

  //If its a player, find out what to say
  IfStateIs0					//Not in combat
    IfTargetIsAPlayer				//Only talk to players
      tmpargument = 80			     
      SetTime

      tmpx = targetlevel
      tmpy = 4
      IfXIsMoreThanY			//Tell them about the need

        //Bringing in the required quest item?
        tmpargument = [DIAR]
        CostTargetItemID
          tmpargument = 9
          SetContent
          PlayFullSound			//Level up sound
          tmpargument = 150
          tmpdistance = EXPQUEST
          GiveExperienceToTarget
          tmpargument = 8
          SendMessage
          tmpargument = 256
          GiveLifeToTarget		//Permanent life gain

        //Give quest
        tmpargument = 4				
        IfContentIs
          tmpargument = 1
          SendMessage
          tmpargument = 10
          PlaySound
          tmpargument = 5
          SetContent
          tmpargument = [GRIF]
          AddIDSZ


        tmpargument = 2
        IfContentIs
          tmpargument = 4        	//Not quest engaged yet
          SetContent
          tmpargument = 7
          PlayFullSound			//"Yes at last"
        tmpargument = 0
        IfContentIs
          tmpargument = 4        	//Not quest engaged yet
          SetContent
          tmpargument = 7
          PlayFullSound			//"Yes at last"

      Else
        tmpargument = 2		        //Set to no more talking
        SetContent
        tmpargument = 6
        PlayFullSound			//"I have other concerns..."

      //Display the talking message
      tmpargument = 5				
      IfContentIs		//Long speech
        tmpargument = 5
        SendMessage
        tmpargument = 6
        SendMessage
        tmpargument = 7
        SendMessage
        tmpargument = 8
        PlayFullSound			//"Dont just stand there!"
      Else			//Normal speech		
        GetContent
        SendMessage
      tmpargument = 2		//Don't loop
      SetState

  SetTargetToOldTarget

//-----------------------------------------------------------------------------------------
End					// All done
//-----------------------------------------------------------------------------------------
