//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
  IfTargetIsAPlayer
    CallForHelp			//Warn friends

  //Drop goodies
  tmpargument = selfmoney
  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
    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 & 63 + 40
    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 = rand % 500 + 300 
        IfXIsLessThanY
          tmpargument = LATCHLEFT
          PressLatchButton	//Yep, defend!
      Else
        GetAttackTurn		//Position from the attack
        tmpx = selfx
        tmpy = selfy
        tmpdistance = 0 - 200
        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 = 120
      SetTime

      //Bringing in the required quest item?
      tmpargument = [DIAR]
      CostTargetItemID
        tmpargument = 9
        SendMessage
        PlayFullSound			//Level up sound
        tmpargument = 150
        tmpdistance = EXPQUEST
        GiveExperienceToTarget
        tmpargument = 8
        SendMessage
        tmpargument = 7
        PlayFullSound			//"It is done!"
        tmpargument = 256
        GiveLifeToTarget		//Permanent life gain
        tmpargument = 4
        SetContent            //It is done now
        tmpargument = [GRIF]
        BeatQuestAllPlayers
      Else
        tmpx = targetlevel
        tmpy = 3                  //Need to be level 5
        IfXIsMoreThanY			//Tell them about the need

          tmpargument = [GRIF]
          IfTargetHasQuest
            tmpx = tmpdistance
            tmpy = 1
            IfXIsEqualToY        //Repeat that it's done
              tmpargument = 7
              PlayFullSound			//"It is done!"
              tmpargument = 9
              SendMessage
            Else                    //Repeat the long quest speech
              tmpargument = 5
              SendMessage
              tmpargument = 6
              SendMessage
              tmpargument = 7
              SendMessage
              tmpargument = 8
              PlayFullSound
          Else

            //CONTENT 2 - Give quest
            tmpargument = 2
            IfContentIs
              tmpargument = 1
              SendMessage
              tmpargument = 10
              PlaySound
              tmpargument = [GRIF]
              tmpdistance = 0
              AddQuestAllPlayers

            //CONTENT 1 - Present quest
            tmpargument = 1
            IfContentIs
              tmpargument = 2        	//Not quest engaged yet
              SetContent
              tmpargument = 5
              SendMessage
              tmpargument = 6
              SendMessage
              tmpargument = 7
              SendMessage
              tmpargument = 8
              PlayFullSound			//The long quest speech

            //CONTENT 0 - Introdouce
            tmpargument = 0
            IfContentIs
              tmpargument = 1        	//Not quest engaged yet
              SetContent
              tmpargument = 4
              SendMessage
              tmpargument = 11
              PlayFullSound			//"Praise aaron a hero!"
        Else
          tmpargument = 2
          SendMessage
          tmpargument = 6
          PlayFullSound			//"Don't need low levels..."

        tmpargument = 2		//Don't loop
        SetState

    SetTargetToOldTarget

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