//The different states
//0: Guard
//1: Ranged Combat: Get in position
//2: Ranged Combat: Aim
//3: Ranged Combat: Shoot
//4: Melee Combat
//5: Stand ground
//6: Attack training dolls
//7: Move towards the trouble
//8: Guard Captain
//On spawn the content equals the character state (Which AI script to run)
//After spawn the content determines the speech trigger (0 equals to speak, higher does not)

//-----------------------------------------------------------------------------------------
//Special for bishopia
IfSpawned
  JoinGoodTeam	//Just in case
  GetContent
  SetState

//-----------------------------------------------------------------------------------------
//What to do if target is dead
IfTargetKilled
  tmpargument = 7
  IfStateIsNot		//Dont stop coming for aid
    SetTargetToNearestEnemy
      tmpargument = 1
      SetTime		//Engage next target
    Else
      tmpargument = 0	//Or return to post
      SetState
      tmpx = selfspawnx
      tmpy = selfspawny
      ClearWaypoints
      AddWaypoint
      tmpargument = 400
      SetTime

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

  //Drop goodies
  tmpargument = 65535
  DropMoney
  DropItems
  DropKeys

  //Death sound
  tmpargument = 1
  tmpdistance = rand & 1027 + 11000
  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
  tmpdistance = rand & 6000 + 7000
  PlaySound
  SetTargetToWhoeverAttacked
    IfTargetIsOnHatedTeam
      CallForHelp		//Enemy attacked
    Else
      IfTargetIsAPlayer		//Player got into trouble
        CallForHelp
        JoinEvilTeam
     Else			//Wops, a friendly NPC attacked
       SetTargetToOldTarget
  IfStateIs5
    CallForHelp
    JoinEvilTeam
    tmpargument = 0
    SetState

IfCalledForHelp
  DisableExport
  JoinEvilTeam
  IfTargetIsOnHatedTeam
    GetState
  Else
    tmpargument = 7
    SetState

//-----------------------------------------------------------------------------------------
IfTimeOut
  tmpargument = 0
  SetContent			//Reset speech counter

  //MOVE AROUND - GUARD
  IfStateIs0
    Sneak			//Move at 33% speed
    tmpargument = rand & 60 + 60
    SetTime
    SetTargetToNearestEnemy
      tmpargument = rand & 20 + 20
      IfHoldingRangedWeapon	//Engage ranged attack code
        tmpargument = 1
        SetState
      IfHoldingMeleeWeapon	//Charge in melee!
        tmpargument = 4
        SetState
    Else			//Else wander around
      SetTurnModeToVelocity
      tmpx = rand & 1024 - 512 + selfx
      tmpy = rand & 1024 - 512 + selfy
      tmpturn = rand & 512 - 256 + selfturn
      Compass
      ClearWaypoints
      AddWaypoint

  // RANGED COMBAT - GET IN POSITON
  IfStateIs1
    tmpargument = 50
    SetTime
    Run
    SetTurnModeToVelocity
    SetTargetToNearestEnemy
      tmpx = targetx
      tmpy = targety
      tmpturn = targetturnto + 32768
      tmpdistance = 650
      Compass
      ClearWaypoints
      AddWaypoint
      tmpargument = 2
      SetState
    Else
      tmpargument = 0		//No enemies, return to guard mode
      SetState

  // RANGED COMBAT - AIM
  IfStateIs2
    tmpargument = rand & 15 + 15
    SetTime

    //Standard movement
    SetTurnModeToVelocity
    tmpx = targetx
    tmpy = targety

    // Keep distance from friends
    SetTargetToNearestFriend
       tmpx = targetdistance
       tmpy = 100
       IfXIsLessThanY

         tmpx = targetx
         tmpy = targety
         SetTargetToOldTarget
       Else
         SetTargetToOldTarget
         tmpx = targetx
         tmpy = targety

     // Keep running away
     tmpturn = xyturnto + 32768
     tmpdistance = 500 			// Desired range
     Compass
     ClearWaypoints
     AddWaypoint

     // Check distance to target
     tmpx = targetx - selfx
     tmpy = targety - selfy
     tmpx = xydistance // Pythag thing...  targetdistance isn't as good
     tmpy = 200 // The desired range ( MIN )
     IfXIsMoreThanY
       tmpy = 1100 // The desired range ( MAX )
       IfXIsLessThanY
         // Target is in range, so switch to fire state
         tmpargument = 3
         SetState
         SetTurnModeToWatchTarget
         tmpargument = 30 // Time before shooting
         SetTime

  // RANGED COMBAT - SHOOT
  IfStateIs3 
    Run
    tmpargument = rand & 7 + 10
    SetTime

    //Is the target to far away or too close?
    tmpy = 500
    tmpx = targetdistance
    IfXIsLessThanY	//Too close
      tmpargument = 1
      SetState		//If so, go back to positioning
    Else
      tmpy = 900
      IfXIsMoreThanY	//Too far away
        tmpargument = 1 //If so, go back to positioning
        SetState

    // Don't shoot if friends are too close
    SetTargetToNearestFriend
      tmpx = targetdistance
      tmpy = 200
      IfXIsLessThanY
        // Keep distance from friends
        tmpx = targetx
        tmpy = targety
        tmpturn = rand & 8191 + 28000 + targetturnto
        tmpdistance = 300
        Compass
        ClearWaypoints
        AddWaypoint
      Else
        // Safe to fire
        SetTargetToOldTarget
          SetTurnModeToWatchTarget
          IfFacingTarget
            tmpargument = LATCHRIGHT
            PressLatchButton   

    // Safe to fire
    IfFacingTarget
      IfTargetIsOnHatedTeam
        tmpargument = LATCHRIGHT
        PressLatchButton   
      Else
        SetTargetToNearestEnemy

    //Reload weapon
    IfHoldingRangedWeapon
      GetState
    Else
      SetTargetToSelf
      tmpargument = ACTIONMC
      DoAction
      tmpargument = [XBOW]
      RestockTargetAmmoIDFirst
      tmpargument = [LBOW]
      RestockTargetAmmoIDFirst
      tmpargument = 1
      SendMessageNear
      tmpargument = 9
      tmpdistance = rand & 8000 + 11000
      PlaySound
      SetTargetToOldTarget
    

  //MELEE COMBAT - CHARGE AND FIGHT
  IfStateIs4
    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

  //STAND GROUND - GUARD
  IfStateIs5
    tmpargument = rand & 10 + 5
    SetTime
    SetTargetToWideEnemy		//Enemy in sight
      IfHoldingMeleeWeapon		//charge in melee
        tmpargument = 3
        SetState
      Else
        tmpargument = 1			//Keep distance with ranged
        SetState
    Else
      tmpx = selfspawnx			//Go back to start position
      tmpy = selfspawny
      Compass
      ClearWaypoints
      AddWaypoint

  //MOVING AROUND - COMING FOR AID
  IfStateIs7
    tmpargument = 25
    SetTime
    SetTargetToWideEnemy	//Enemy located, attack him
      tmpargument = 0
      SetState
    Else
      SetTargetToWhoeverCalledForHelp
        tmpx = targetx		//Move towards the trouble
        tmpy = targety
        tmpturn = targetturnto
        ClearWaypoints
        AddWaypoint

//-----------------------------------------------------------------------------------------
//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		  //
    IfTargetIsOnHatedTeam               //Enemy!
      IfHoldingRangedWeapon
        tmpargument = 1			      //Run away and get in position
        SetState
      Else
        tmpargument = 4			//Attack melee
        SetState
    Else				    //Friendly bumper

      //If its a player, find out what to say
      IfStateIs0					//Not in combat
        IfTargetIsAPlayer				//Only talk to players
          tmpargument = 0				
          IfContentIs					//Check speak counter
            tmpargument = 1
            SetContent					//Set to no more talking
            tmpx = rand % 5				//Randomize speech

            //We serve and protect
            tmpy = 0
            IfXIsEqualToY
              tmpargument = 2
              SendMessageNear
              tmpargument = 8
              tmpdistance = rand & 6000 + 8000
              PlaySound

            //Hey! (If male) or Hey baby! (If female), Rouges get a warning instead
            tmpy = 1
            IfXIsEqualToY
              tmpargument = [DISA]
              IfTargetHasSkillID		//Obey the law or...
                tmpargument = 9
                SendMessageNear
                tmpargument = 7
                PlaySound
              Else
                IfTargetIsFemale		//Hey baby...
                  tmpargument = 3
                  SendMessageNear
                  tmpargument = 11
                  PlaySound
                Else				//Hey!
                  tmpargument = 6
                  SendMessageNear
                  PlaySound
  
            //Stay out of trouble!
            tmpy = 2
            IfXIsEqualToY
              tmpargument = 8
              SendMessageNear
              tmpargument = 12
              PlaySound

            //Waddya want?
            tmpy = 3
            IfXIsEqualToY
              tmpargument = 7
              SendMessageNear
              tmpargument = 10
              PlaySound

            //Keep moving!
            tmpy = 4
            IfXIsEqualToY
              tmpargument = 10
              SendMessageNear
              tmpargument = 13
              PlaySound

          //Try to move out of the way of friendlies
          tmpx = rand & 511 + targetx - 256	    
          tmpy = rand & 511 + targety - 256
          tmpturn = selfturn
          tmpdistance = rand & 250
          Compass		    
          ClearWaypoints			    
          AddWaypoint
          tmpargument = 40			     
          SetTime

      //Guard Captain words
      IfStateIs8
        IfTargetIsAPlayer
          tmpargument = 0				
          IfContentIs					//Check speak counter
            tmpargument = 1
            SetContent					//Set to no more talking
            tmpargument = 80			     
            SetTime

            tmpx = targetlevel
            tmpy = 3
            IfXIsMoreThanY
              tmpargument = 12			//TODO: Add Griffin Tower quest
              SendMessageNear
            Else
              tmpargument = 11			//Sorry, too low level
              SendMessageNear
              tmpargument = 13
              PlaySound


      SetTargetToOldTarget

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