//======Generic melee/ranged fighter AI========
//- Uses mounts
//- Handles inventory stuff. grabs and drops
//  equipment.
//- Jumps over obstacles
//- Can use both ranged and melee
//- Will try to avoid hitting friendlies.
//- Can be backstabbed with POKE damage.
//- Also uses shield according to DEX value

//Guide to the States
//0 - Wander
//1 - Charge and attack melee
//2 - Get in ranged position
//3 - Do ranged combat
//4 - Retreat!

//Sustained by magic
IfSpawned
  EnchantTarget

//------------------------------------------------------------------------------
// Jump over barriers, onto mounts
IfBumped
  SetTargetToWhoeverBumped
  IfTargetIsAlive
    IfTargetIsOnHatedTeam
      DoNothing
    Else
    IfTargetIsAlive
      IfTargetIsAPlatform
        // Jump over some stuff
        tmpargument = LATCHJUMP
        PressLatchButton
      IfTargetIsAMount
        tmpargument = [BALI] // Don't mount ballistas.
        IfTargetHasID
          DoNothing
        Else
          // Jump onto mounts
          tmpx = targetx
          tmpy = targety
          ClearWaypoints
          AddWaypoint

        tmpargument = 30
        SetTime
    Else
      // Jump over corpses
      tmpargument = LATCHJUMP
      PressLatchButton
    SetTargetToOldTarget

//------------------------------------------------------------------------------
//Use shield
IfTargetIsAttacking
  IfTargetIsOnHatedTeam
    IfFacingTarget
      IfHoldingShield
        tmpturn = selfturn - targetturn      //Target facing self?
        tmpx = tmpturn
        tmpy = 24768
        IfXIsMoreThanY
          tmpy = 40768
          IfXIsLessThanY
            tmpx = selfdex         //Check dexterity to block, 10 is average
            tmpy = rand & 7120
            IfXIsMoreThanY
              PressLatchButton

//------------------------------------------------------------------------------
IfKilled
  // Tell the players
  tmpargument = 0
  IfTargetIsOnSameTeam
    tmpargument = 1
  SendMessageNear

  // Just to be sure
  tmpargument = ACTIONKA
  DoActionOverride
  KeepAction

  //Death sound
  tmpargument = 5
  PlaySound

  // Drop goodies
  tmpargument = 65535
  DropMoney
  DropItems
  DropKeys

  // Make the character body
  tmpargument = 40
  SetBumpHeight

//------------------------------------------------------------------------------
IfAttacked
  SetTargetToWhoeverAttacked
    IfTargetIsOnHatedTeam
      IfHoldingMeleeWeapon
        tmpargument = 1
      Else
        tmpargument = 2
      SetState

      // Check for instant death
      tmpargument = [DISA]
      IfTargetHasSkillID
        // Allow Rogues to backstab
        GetDamageType
        tmpx = tmpargument
        tmpy = DAMAGEPOKE
        IfXIsEqualToY
          IfHitFromBehind
            tmpargument = 8
            SendMessageNear

            tmpargument = 30
            tmpdistance = EXPMURDER
            GiveExperienceToTarget

            // Do it
            SetTargetToSelf
            KillTarget
    Else
      // Friendly fire, get out of the way
      tmpx = rand & 511 - 256 + selfx
      tmpy = rand & 511 - 256 + selfy
      ClearWaypoints
      AddWaypoint
      SetTargetToOldTarget

    //Play ouch sound
    tmpargument = 3
    PlaySound

//Don't hack dead bodies
IfTargetIsAlive
  DoNothing
Else
  tmpargument = 0
  SetState

//------------------------------------------------------------------------------
//This is the Main AI loop
IfTimeOut

  // Riding some type of animal...
  IfSitting
    tmpargument = rand & 15 + 15 // Time
    SetTime

    // Acquire a target
    SetTurnModeToVelocity
    SetTargetToWideEnemy
      // Charge towards enemy
      tmpx = targetx
      tmpy = targety
      tmpturn = targetturnto
      tmpdistance = 300
      Compass
      ClearWaypoints
      AddWaypoint

      // Check for special mount attacks
      tmpx = targetdistance
      tmpy = 200
      IfXIsLessThanY
        tmpargument = [XFUN]
        SetOldTarget
        SetTargetToWhoeverIsHolding
        IfTargetHasAnyID
          tmpargument = LATCHRIGHT
          PressLatchButton
          SetTargetToOldTarget
        Else
          // Don't let the enemy jump over
          SetTargetToOldTarget
          tmpx = selfz
          tmpy = targetz - 40
          IfXIsLessThanY
            IfHoldingMeleeWeapon
              PressLatchButton
    Else
      // Guard the start position
      tmpx = rand & 511 - 256 + selfspawnx
      tmpy = rand & 511 - 256 + selfspawny
      ClearWaypoints
      AddWaypoint
  Else
    // Foot soldier...
    // Behave according to state

    //STATE 0 - WANDER
    IfStateIs0
      tmpargument = rand & 15 + 45
      SetTime
      SetTurnModeToVelocity
      SetTargetToWideEnemy
        IfHoldingRangedWeapon
          tmpargument = 2
        Else
          tmpargument = 1
        SetState
      Else
        tmpx = rand & 1023 - 512 + selfspawnx
        tmpy = rand & 1023 - 512 + selfspawny
        ClearWaypoints           
        AddWaypoint
   

    //STATE 1 - MELEE
    IfStateIs1
      tmpargument = rand & 15 + 30 // Time
      SetTime
       
      // Charge and attack the enemy
      SetTargetToNearestEnemy
      tmpx = targetdistance
      tmpy = 2500            //Ckise enough to care?
      IfXIsLessThanY
        tmpy = 450            //Far away enough to charge?
        IfXIsMoreThanY
          tmpx = rand & 511 + targetx - 256
          tmpy = rand & 511 + targety - 256
          tmpturn = targetturnto
          tmpdistance = 1500
          Compass
          SetTurnModeToVelocity
        Else               //If not, simply engage.
          tmpx = targetx
          tmpy = targety
          tmpturn = targetturnto
          tmpdistance = 200
          Compass
          SetTurnModeToWatchTarget
        ClearWaypoints
        AddWaypoint
      Else
        tmpargument = 0
        SetState         //target ran away

      // Close enough to attack
      IfFacingTarget
        tmpx = targetdistance
        tmpy = 200
        IfXIsLessThanY
          IfHoldingMeleeWeapon
            IfFacingTarget
              PressLatchButton

      // Rummage for a weapon
      IfHoldingMeleeWeapon
        DoNothing
      Else
        tmpargument = 4         //panic a little
        SetState
        // Draw a new weapon
        tmpargument = LATCHPACKRIGHT
        PressLatchButton
 

    //STATE 2 - RANGED POSITION
    IfStateIs2
      tmpargument = rand & 15 + 30 // Time
      SetTime

      SetTargetToNearestEnemy
      SetTurnModeToWatchTarget
      tmpx = targetdistance
      tmpy = 2500         //Range for following the target
      IfXIsLessThanY
        tmpx = targetx
        tmpy = targety
        tmpturn = targetturnto
        tmpdistance = 600
        Compass
        ClearWaypoints
        AddWaypoint            //Move towards him

        //Suitable to shoot?
        IfFacingTarget
          tmpx = targetdistance
          tmpy = 700
          IfXIsLessThanY
            tmpargument = 3
            SetState

        // Don't shoot if friends are too close
        SetOldTarget
        SetTargetToNearestFriend
          IfFacingTarget
            tmpx = targetdistance
            tmpy = 300
            IfXIsLessThanY
              // Keep distance from friends
              tmpx = targetx
              tmpy = targety
              tmpturn = rand & 8191 + 28000 + targetturnto
              tmpdistance = 300
              Compass
              ClearWaypoints
              AddWaypoint
              tmpargument = 2   
              SetState         //not safe to shoot
            SetTargetToOldTarget
            tmpy = targetdistance
            IfXIsMoreThanY
              tmpargument = 3
              SetState         //Enemy is closer than friend, shoot after all
        SetTargetToOldTarget

      Else
        tmpargument = 0         //target ran away
        SetState
       

    //STATE 3 - RANGED COMBAT
    IfStateIs3
      tmpargument = rand & 15 + 30
      SetTime
      IfHoldingRangedWeapon
        PressLatchButton         //return to ranged movement
      Else               //ammo out
        // Draw a new weapon
        DropWeapons
        // Now using melee weapons
        tmpargument = 1
        SetState
      SetTargetToNearestEnemy

      //Continue ranged fighting or retreat?
      IfFacingTarget
        tmpx = targetdistance
        tmpy = 900
        IfXIsMoreThanY
          tmpargument = 2
          SetState
        tmpy = 400
        IfXIsLessThanY
          tmpargument = 4
          SetState   
      Else
        tmpargument = 2
        SetState   

    //STATE 4 - RETREAT
    IfStateIs4
      tmpargument = 150
      SetTime
      tmpx = targetx
      tmpy = targety
      tmpturn = rand & 32768 + targetturnto
      tmpdistance = 650
      Compass
      ClearWaypoints
      AddWaypoint

      IfHoldingMeleeWeapon
        tmpargument = 1
        SetState
      Else
        IfHoldingRangedWeapon
          tmpargument = 2
          SetState
        Else
          DoNothing      //TODO: GET WEAPONS!

//------------------------------------------------------------------------------
//Say the character looks better
IfHealed
  tmpargument = 7
  SendMessageNear

//------------------------------------------------------------------------------
//Spawn the life force

tmpargument = 2
tmpx = selfx
tmpy = selfy
tmpdistance = selfz + 59
SpawnExactParticle

//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
End
//------------------------------------------------------------------------------
