//------------------------------------------------------------------------------
//NOTES
//Armor 2 is a shaman with special abillities
//Armor 1 is captain/elite soldier
//The object's content is how many spears it has left (or recharge if shaman)
//------------------------------------------------------------------------------
//Spawn stuff
IfSpawned
  tmpargument = 1			//Elite
  IfArmorIs
    SetTargetToSelf
    tmpargument = rand & 511 + 512 	//2-4 bonus
    GiveLifeToTarget
    GiveStrengthToTarget
    GiveDexterityToTarget
  tmpargument = 2			//Shaman
  IfArmorIs
    SetTargetToSelf
    tmpargument = rand & 1023 + 1024 	//4-8 bonus
    GiveWisdomToTarget
    GiveIntelligenceToTarget
  tmpargument = 4
  SetContent				//BAD: hard coded 4 spears (i am to lazy to set content for all)

//------------------------------------------------------------------------------
//Growl
IfUsed
  tmpargument = rand & 1 + 5
  PlaySound

//------------------------------------------------------------------------------
//Jumping
IfBumped
  SetTargetToWhoeverBumped
   // Jump over barriers, onto mounts
    IfTargetIsAlive
      IfTargetIsAPlatform
        // Jump over some stuff
        tmpargument = LATCHJUMP
        PressLatchButton
    Else
      // Jump over corpses
      tmpargument = LATCHJUMP
      PressLatchButton
  SetTargetToOldTarget

//------------------------------------------------------------------------------
//Target is dead
IfTargetKilled
  tmpargument = 0
  SetState

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

//------------------------------------------------------------------------------
//Handle death and stuff
IfKilled
  // Drop goodies
  tmpargument = selfmoney
  DropMoney
  DropKeys
  DropItems

  // Make the character body
  tmpargument = 45
  SetBumpHeight

  tmpargument = 0
  IfArmorIs			//Normal dude
    IfTargetIsOnOtherTeam
      tmpargument = 0		//Enemy killed
    Else
      tmpargument = 1
    SendMessageNear		//Friendly fire
  Else				//Killed captain or shaman
    tmpargument = 25		//Bonus xp
    tmpdistance = EXPKILL
    GiveExperienceToTargetTeam
    tmpargument = 1
    IfArmorIs
      tmpargument = 5		//Elite
    Else
      tmpargument = 3		//Shaman
    SendMessageNear


  //Death sound
  tmpargument = 4
  PlaySound

//------------------------------------------------------------------------------
//Shaman spells
tmpargument = 2
IfArmorIs

  // Put the little mana ball on the
  // character's hand
  GetContent
  tmpx = tmpargument
  tmpy = 10
  IfXIsLessThanY
    tmpturn = tmpargument*2500
    tmpturn = 25000 - tmpturn
    tmpdistance = GRIPLEFT
    tmpargument = 3
    SpawnAttachedSizedParticle

  IfTimeOut
    //Recharge spells
    GetContent
    tmpx = tmpargument
    tmpy = 0
    IfXIsMoreThanY
      tmpargument = tmpargument -1		//Recharge some...
      SetContent

    //Grasping grass spell
    IfTargetIsOnHatedTeam
      IfFacingTarget
        GetContent
        tmpx = tmpargument
        tmpy = 0
        IfXIsEqualToY
          tmpargument = LATCHLEFT
          PressLatchButton
            tmpargument = 4
            tmpx = selfx
            tmpy = selfy
            tmpdistance = selfz
            SpawnExactParticle
            tmpargument = 10		//Recharge time
            SetContent
            tmpargument = 1
            SetState

    //Heal spell
    SetTargetToNearestFriend
      IfTargetIsHurt
        tmpx = targetx
        tmpy = targety
        ClearWaypoints
        AddWaypoint
        tmpx = targetdistance
        tmpy = 300
        IfXIsLessThanY			      //In Range?
          tmpx = 260			        //
          tmpy = selfmana			        //
          IfXIsLessThanY				// Enough to cast?
            IfFacingTarget
              tmpargument = LATCHLEFT
              PressLatchButton
                tmpx = targetx
                tmpy = targety
                tmpdistance = targetz
                tmpargument = 6
                SpawnExactParticle
                tmpargument = rand & 511 + 1024
                HealTarget
                SetTargetToSelf
                tmpargument = 256
                CostTargetMana
        Else
          SetTargetToOldTarget
      Else
        SetTargetToOldTarget

//------------------------------------------------------------------------------
//New round
IfTimeOut
  tmpargument = rand % 20 + 15
  SetTime

  //Growl
  tmpx = rand & 255
  tmpy = 25
  IfXIsLessThanY			  // Play a random grunt?
    tmpargument = rand & 1 + 2
    PlaySound

  //GUARD
  IfStateIs0
    tmpargument = 50
    SetTime
    SetTurnModeToVelocity
    tmpx = rand % 2047 + selfspawnx - 1024
    tmpy = rand % 2047 + selfspawny - 1024
    ClearWaypoints
    AddWaypoint					//Wander around and guarding
    SetTargetToWideEnemy
      GetContent
      tmpx = tmpargument			//Number of spears left
      tmpy = 1
      IfXIsMoreThanY
        tmpargument = 3				//Throw spear
        SetState
      Else
        tmpargument = 1		//Charge enemy
        SetState

  //CHARGE ENEMY
  IfStateIs1
    SetTurnModeToVelocity
    tmpx = targetdistance
    tmpy = 400
    IfXIsMoreThanY		//Move towards the enemy
      tmpx = rand & 511 + targetx - 255
      tmpy = rand & 511 + targety - 255
      tmpturn = targetturnto
      tmpdistance = 1500
      Compass
      ClearWaypoints
      AddWaypoint
      Run
    Else
      tmpargument = 2		//close combat
      SetState

  //COMBAT - MELEE
  IfStateIs2
    SetTurnModeToWatchTarget
    tmpargument = 15
    SetTime			//Quick reaction in close combat
    tmpx = targetdistance
    tmpy = 250
    IfXIsLessThanY		//Close enough?
      IfFacingTarget		//Looking at enemy?
        IfHoldingMeleeWeapon
          PressLatchButton
    tmpx = targetx
    tmpy = targety
    tmpdistance = 0 - 100
    tmpturn = targetturnto
    Compass
    ClearWaypoints
    AddWaypoint			//Move towards enemy

    //Enemy ran away?
    tmpx = targetdistance
    tmpy = 500
    IfXIsMoreThanY
      tmpargument = 1
      SetState			//Charge

  //COMBAT - THROW SPEAR
  IfStateIs3

    //Are we in wrong mode?
    GetContent
    tmpx = tmpargument
    tmpy = 1
    IfXIsLessThanY
      tmpargument = 1
      SetState			//Yes!!

    SetTurnModeToWatchTarget
    tmpx = targetdistance
    tmpy = 400
    IfXIsLessThanY		//Target coming too close
      tmpx = selfx
      tmpy = selfy
      tmpturn = targetturnto
      tmpdistance = 0 - 800
      Compass
      ClearWaypoints
      AddWaypoint				//Aim attack

      //Shoot when target is closer
      IfFacingTarget				//Looking at enemy?
        IfHoldingMeleeWeapon
          PressLatchButton
            GetContent
            tmpargument = tmpargument -1	//Reduce ammo with 1
            SetContent			        //Remember that he has used a spear
            tmpargument = 50
            SetTime

    tmpx = targetdistance
    tmpy = 800
    IfXIsMoreThanY		//Enemy too far away?
      tmpx = targetx
      tmpy = targety
      ClearWaypoints
      AddWaypoint		//Move towards enemy

//------------------------------------------------------------------------------
//Return attack
IfAttacked
  SetTargetToWhoeverAttacked
  IfTargetIsOnHatedTeam
    //Backstabbed!
    tmpargument = [DISA]	//TODO: [STAB]
    IfTargetHasSkillID
      IfHitFromBehind
        tmpargument = 2
        SendMessageNear
        tmpargument = 15
        tmpdistance = EXPMURDER
        GiveExperienceToTarget
        SetTargetToSelf
        KillTarget
  Else
    SetTargetToOldTarget

  //Say ouch
  tmpargument = rand & 2 + 5
  PlaySound

//------------------------------------------------------------------------------
//Dodge bullets?
IfTargetIsAttacking
  IfTargetIsOnHatedTeam
    tmpx = targetdistance
    tmpy = 300
    IfXIsMoreThanY			//Target doing ranged attack?
      tmpargument = 1
      IfArmorIs				//Elites always dodge
        tmpx = 1
      Else
        tmpturn = selfdex - targetdex
        tmpx = 30 + tmpturn > 8
        tmpy = rand % 100
        IfXIsMoreThanY			//Others must make a dex check
          tmpx = 1
        Else
          tmpx = 0			//Failed!
      tmpy = 1
      IfXIsEqualToY			//Success!
        tmpx = rand & 1023 + selfx - 512
        tmpy = rand & 1023 + selfy - 512
        tmpturn = rand
        tmpdistance = 1500
        Compass
        ClearWaypoints
        AddWaypoint
        tmpargument = 100
        SetTime
        tmpargument = LATCHJUMP
        PressLatchButton

//------------------------------------------------------------------------------
//We dont like water =(
tmpargument = 0
IfStateIsNot
  IfOverWater

    //Jump over?
    IfTargetIsOnHatedTeam
      tmpx = targetdistance
      tmpy = 128*4            //Try to jump over up to 4 water tiles
      IfXIsLessThanY
        tmpx = targetx
        tmpy = targety
        tmpdistance = 1500
        tmpturn = targetturnto
        Compass
        ClearWaypoints
        AddWaypoint
        tmpargument = LATCHJUMP		//Leap!
        PressLatchButton
      Else
        tmpx = selfx			//eew! water!
        tmpy = selfy
        tmpdistance = -300
        tmpturn = targetturnto
        Compass
        tmpargument = 150
        SetTime
      ClearWaypoints
      AddWaypoint

//------------------------------------------------------------------------------
//Claw attacks for last resort
tmpargument = 0
IfStateIsNot
  IfUnarmed
    SetTargetToWideEnemy
      tmpx = targetdistance
      tmpy = 100
      IfXIsLessThanY				//Scratch!
        IfFacingTarget
          tmpargument = rand & 1 + LATCHLEFT
          PressLatchButton
      Else
        tmpy = 400
        IfXIsLessThanY				//Move in closer
          tmpx = targetx
          tmpy = targety
          tmpdistance = 0-50
          tmpturn = targetturnto
          Compass
          ClearWaypoints
          AddWaypoint

//------------------------------------------------------------------------------
//More realistic behaviour
IfDazed
  SetTurnModeToVelocity
IfGrogged
  SetTurnModeToVelocity

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