//AI note: Set Content to 1 if you want a Ulna thrower. Leader Ulna are
//automatically transformed when level is 2 or higher (This is only checked on spawn).
//Dexterity increases attack speed
//Ranged attacks are always made from left hand
//------------------------------------------------------------------------------
// Check for a kill order
IfOrdered
  tmpx = selforder
  tmpy = [UNDO]
  IfXIsEqualToY
    UndoEnchant
    
//------------------------------------------------------------------------------
// Crushed as a umm.. dead body
IfCrushed
  tmpargument = 5
  SendMessageNear
  tmpargument = 1
  tmpx = selfx
  tmpy = selfy
  tmpdistance = selfz
  SpawnExactParticle
  SpawnExactParticle
  SpawnExactParticle
  SpawnExactParticle
  SpawnExactParticle
  
//------------------------------------------------------------------------------
// Setup character
IfSpawned
  SetTargetToSelf
  tmpx = targetlevel
  tmpy = 0
  IfXIsMoreThanY	//Ulna is a leader?
    tmpargument = 1
    ChangeArmor
    BecomeLeader
  EnchantTarget 	// target is self...
  Walk
  GetContent	//Set ranged or melee AI
  SetState		//0 is melee and 1 is ranged (Leaders automatically use both)
  MakeCrushValid	//Ulnas can be crushed

//Make the bones flying
IfCrushed
  tmpx = selfx
  tmpy = selfy
  tmpdistance = selfz
  tmpargument = 1
  SpawnExactParticle
  SpawnExactParticle
  SpawnExactParticle
  SpawnExactParticle
  GoPoof
  tmpargument = 1
  SendMessageNear

//------------------------------------------------------------------------------
//Handle death by sending a message and other stuff
IfKilled
  tmpargument = 1		//Ulna Captain
  IfArmorIs
    DoNothing
  Else
    tmpargument = 0		//Normal Ulna
    IfContentIs
      DoNothing
    Else
      tmpargument = 2		//Bone thrower
  IfTargetIsOnSameTeam
    tmpargument = 3		//Teamkill
  IfTargetIsSelf
    tmpargument = 6
  SendMessageNear

  // Drop goodies
  tmpargument = selfmoney
  DropMoney

  // Make the character body
  tmpargument = 45
  SetBumpHeight

  //Crushy!
  MakeCrushValid
//------------------------------------------------------------------------------
// For helper AIs
IfLeaderKilled
  BecomeLeader

//------------------------------------------------------------------------------
//New round
IfTimeOut
  Walk
  SetTargetToWideEnemy		//Find enemy

    //MELEE AI
    IfStateIs0
      tmpx = targetdistance
      tmpy = 220
      IfXIsLessThanY
        IfFacingTarget
          tmpargument = 1		//Leader Ulna always react fast
          IfArmorIs
            tmpargument = LATCHRIGHT
            PressLatchButton
          Else
            tmpx = rand % 25600
            tmpy = 3940 + selfdex		//15% chance to attack +1% per dex
            IfXIsLessThanY		//See if he reacts fast enough to attack
              tmpargument = LATCHRIGHT
              PressLatchButton
        tmpx = targetx
        tmpy = targety
        tmpdistance = 200
        tmpturn = targetturnto
        Compass
      Else			//Randomize attack approach
        Run			//Move fast
        tmpx = rand & 255 + targetx - 128
        tmpy = rand & 255 + targety - 128
        tmpargument = 1		//Leader Ulna throws a bone
        IfArmorIs
          IfFacingTarget		//Make sure to aim
            SetTargetToSelf
            SetTargetToTargetLeftHand	//Check for left hand items
              DoNothing			//Left hand free for use
            Else
              tmpargument = LATCHLEFT	//Assuming having no weapon in
              PressLatchButton		//left hand
            SetTargetToOldTarget
      tmpargument = 20
      SetTime

    //RANGED COMBAT AI
    IfStateIs1
      tmpx = targetdistance
      tmpy = 150
      IfXIsMoreThanY		//Far enough away?
        IfFacingTarget		//Aim
          tmpx = rand % 25600
          tmpy = selfdex		//1% per dex
          IfXIsLessThanY		//See if he reacts fast enough to throw
            tmpargument = LATCHLEFT
            PressLatchButton
        tmpx = targetx		//Keep moving towards enemy
        tmpy = targety
        tmpturn = targetturnto
        tmpdistance = 0
        Compass
      Else			//Too close, run away
        Run
        tmpx = rand & 255 + targetx - 128
        tmpy = rand & 255 + targety - 128
        tmpturn = targetturnto
        tmpdistance = 400
        Compass
        tmpargument = rand & 15 + 5
        SetTime

  Else				//No enemy found, wander instead
    tmpargument = rand & 31 + 120
    SetTime
    tmpx = rand & 1023 - 512 + selfspawnx
    tmpy = rand & 1023 - 512 + selfspawny
  ClearWaypoints
  AddWaypoint

IfAtLastWaypoint		//Don't stand still
  tmpargument = 0
  SetTime

//Dont spam attack
IfUsed
  IfStateIs1
    tmpargument = 80
    SetReloadTime

//------------------------------------------------------------------------------
//Handle being attacked by blocking or countering or running away
IfAttacked

  // Chase the attacker
  SetTargetToWhoeverAttacked
  IfTargetIsOnHatedTeam
    tmpx = targetx
    tmpy = targety
    tmpdistance = 200
    tmpturn = targetturnto
    Compass
    ClearWaypoints
    AddWaypoint
    tmpargument = 30
    SetTime

//------------------------------------------------------------------------------
//Instantly counterattack
IfBumped
  SetTargetToWhoeverBumped
  IfTargetIsOnHatedTeam
    IfFacingTarget
      tmpargument = LATCHRIGHT
      PressLatchButton
    Else
      tmpx = targetx
      tmpy = targety
      tmpdistance = 200
      tmpturn = targetturnto
      Compass
      ClearWaypoints
      AddWaypoint
      tmpargument = 30
      SetTime
  Else			//Not an enemy
    tmpargument = 0
    SetTime
    SetTargetToOldTarget

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