//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
//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
  tmpargument = 0
  SetContent

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

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

IfCalledForHelp
  DisableExport
  JoinEvilTeam
  SetTargetToWideEnemy
    DoNothing
  Else
    tmpargument = 7
    SetState

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

  //Reset speech counter
  tmpargument = 0
  SetContent

  //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 & 511 - 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
      IfFacingTarget
        tmpx = targetdistance
        tmpy = 100
        IfXIsLessThanY
          tmpx = targetx
          tmpy = targety            //Target is now friend
    SetTargetToOldTarget

    // 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
      IfFacingTarget
        tmpx = targetdistance
        tmpy = 200
        IfXIsLessThanY
          // Keep distance from friends
          tmpx = targetx
          tmpy = targety
          tmpturn = rand & 8191 + 28000 + targetturnto	//TargetTurnAway
          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
      SetOldTarget
      SetTargetToSelf
      
      //First get a target on the ranged weapon
      SetTargetToTargetRightHand
        DoNothing
      Else
        SetTargetToTargetLeftHand
      
      //Are we low on ammo?
      tmpx = targetammo
      tmpy = 5
      IfXIsLessThanY
        SetTargetToSelf
        tmpargument = ACTIONMC
        DoAction
        tmpargument = [XBOW]
        RestockTargetAmmoIDFirst
        tmpargument = [LBOW]
        RestockTargetAmmoIDFirst
        tmpargument = 1
        SendMessageNear
        tmpargument = 9
        PlaySound
      SetTargetToOldTarget


  //MELEE COMBAT - CHARGE AND FIGHT
  IfStateIs4
    Run
    tmpargument = 15
    SetTime
    SetTargetToNearestEnemy	//Move towards enemy
      tmpx = targetx
      tmpy = targety
      tmpturn = targetturnto
      tmpdistance = 800
      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
    SetTargetToNearestEnemy		//Enemy in sight
      IfHoldingMeleeWeapon		//charge in melee
        tmpargument = 4
        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
    SetTargetToNearestEnemy	//Enemy located, attack him
      IfHoldingMeleeWeapon		//charge in melee
        tmpargument = 4
        SetState
      Else
        tmpargument = 1			//Keep distance with ranged
        SetState
    Else
      SetTargetToWhoeverCalledForHelp
        tmpx = targetx		//Move towards the trouble
        tmpy = targety
        tmpturn = targetturnto
        tmpdistance = 1500
        ClearWaypoints
        AddWaypoint

//-----------------------------------------------------------------------------------------
//Use shield against attacking enemies
IfTargetIsAttacking		//Must be attacking
  IfTargetIsOnHatedTeam		//Must be enemy
    tmpargument = [SHIE]	//Holding shield?
    IfHoldingItemID
      IfFacingTarget
        tmpx = targetdistance
        tmpy = 128*4
        IfXIsLessThanY              //Close enough?
          tmpargument = LATCHLEFT
          PressLatchButton	//Yep, defend!

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

      SetTargetToOldTarget

//-----------------------------------------------------------------------------------------
//Handle death
IfKilled
  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

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