//Bludy Hawk, the Assassin wanted. He's basically just
//a strong bandit with a different look that causes the
//module to be beaten if you defeat him.

//Zefz: Improved the AI, gave hime some dangerous dodging
//abilities like the Jungle Cats.

//------------------------------------------------------------------------------
IfBumped
  SetTargetToWhoeverBumped
    IfTargetIsOnHatedTeam
      tmpargument = 1
      SetState
    Else
      // Jump over barriers, onto mounts
      IfTargetIsAlive
        IfTargetIsAPlatform
          // Jump over some stuff
          tmpargument = LATCHJUMP
          PressLatchButton
        IfTargetIsAMount
          // Jump onto mounts
          tmpx = targetx
          tmpy = targety
          ClearWaypoints
          AddWaypoint

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

//------------------------------------------------------------------------------
//Dodge attacks
IfTargetIsAttacking
  IfTargetIsOnHatedTeam
    tmpx = targetdistance
    tmpy = 150
    IfXIsMoreThanY
      tmpargument = LATCHJUMP
      PressLatchButton
      PressLatchButton
      tmpx = rand & 511 + targetx - 256
      tmpy = rand & 511 + targety - 256
      tmpturn = targetturnto
      tmpdistance = 2000
      Compass
      ClearWaypoints
      AddWaypoint

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

  // Just to be sure
  tmpargument = ACTIONKA
  DoActionOverride
  KeepAction

  //Death sound
  tmpargument = rand % 3 + 5
  PlaySound

  // Drop goodies
  tmpargument = selfmoney
  DropMoney
  DropItems
  DropKeys

  // Make the character body
  tmpargument = 40
  SetBumpHeight

  // Finish it up
  BeatModule
  EnableExport

  // Beat it...
  tmpargument = [BENE]      //Which quest
  tmpdistance = 1           //Quest level
  AddQuestAllPlayers

  //Victory tune
  tmpargument = 14		//Music track
  tmpdistance = 0		//Fade in time
  PlayMusic
  
  //Create the heirloom!
  tmpargument = 38
  tmpdistance = selfz
  tmpx = selfspawnx
  tmpy = selfspawny
  SpawnExactCharacterXYZ
  
  //Do the end text
  ClearEndMessage
  tmpargument = 3
  AddEndMessage
  tmpargument = 4
  AddEndMessage
  tmpargument = 5
  AddEndMessage

//------------------------------------------------------------------------------
IfAttacked
  SetTargetToWhoeverAttacked
    IfTargetIsOnHatedTeam
      DoNothing
    Else
      // Friendly fire, get out of the way
      tmpx = rand & 511 - 256 + selfx
      tmpy = rand & 511 - 256 + selfy
      ClearWaypoints
      AddWaypoint
      SetTargetToOldTarget

    tmpargument = rand % 3 + 2
    PlaySound

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

//------------------------------------------------------------------------------
IfTimeOut
  //STATE 0 - WANDER
  IfStateIs0
    tmpargument = rand & 15 + 15
    SetTime
    SetTurnModeToVelocity
    SetTargetToWideEnemy
      tmpargument = 6		//Boss music!
      tmpdistance = 0		//Fade in time
      PlayMusic

      //Warriors, attack!
      tmpargument = 1
      PlaySound
      tmpargument = 3
      SendMessageNear

      //Find out which weapon to use
      IfHoldingRangedWeapon
        tmpx = targetdistance
        tmpy = 128*2        //2 tiles
        IfXIsMoreThanY
          tmpargument = 2
        Else
          tmpargument = 1
      Else
        tmpargument = 1
      SetState
    Else
      tmpx = rand & 1023 - 512 + selfspawnx
      tmpy = rand & 1023 - 512 + selfspawny
      ClearWaypoints
      AddWaypoint

  //STATE 1 - MELEE
  IfStateIs1
    tmpargument = rand % 5 + 10 // Time
    SetTime

    // Charge and attack the enemy
    SetTargetToNearestEnemy
    tmpx = targetdistance
    tmpy = 128*15           //15 tiles
    IfXIsLessThanY
      tmpy = 128*4          //4 tiles
      IfXIsLessThanY
        tmpx = rand & 511 + targetx - 256
        tmpy = rand & 511 + targety - 256
        tmpturn = targetturnto
        tmpdistance = 600
        Compass
        SetTurnModeToWatchTarget
      Else
        IfHoldingRangedWeapon
          tmpargument = 2
          SetState
        Else                    //Leap attack
          tmpargument = LATCHJUMP
          PressLatchButton
          PressLatchButton
          tmpx = rand & 511 + targetx - 256
          tmpy = rand & 511 + targety - 256
          tmpturn = targetturnto
          tmpdistance = 2500
          Compass
        SetTurnModeToVelocity
      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
      // Draw a new weapon
      tmpargument = LATCHPACKRIGHT
      PressLatchButton

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

    SetTargetToNearestEnemy
    SetTurnModeToWatchTarget
    tmpx = targetdistance
    tmpy = 2500
    IfXIsLessThanY
      tmpx = targetx
      tmpy = targety
      tmpturn = targetturnto
      tmpdistance = 600
      Compass
      ClearWaypoints
      AddWaypoint

      //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 + 20
    SetTime
    IfHoldingRangedWeapon
      PressLatchButton			//return to ranged movement
    Else					//ammo out
      // Put the musketoon away
      tmpargument = LATCHPACKLEFT
      PressLatchButton
      // Now using melee weapons
      tmpargument = 1
      SetState

    SetTargetToNearestEnemy
      IfFacingTarget
        tmpx = targetdistance
        tmpy = 900
        IfXIsMoreThanY
          tmpargument = 2
          SetState
        tmpy = 300
        IfXIsLessThanY
          tmpargument = 1
          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

      //Grab a weapon
      Else
        tmpargument = [SWOR]
        tmpdistance = BLAHITEMS		//Look for items
        tmpx = 0
        SetTargetToWideBlahID
          tmpx = 1				//Found a sword
        Else
          tmpargument = [MACE]
          SetTargetToWideBlahID
            tmpx = 1				//Found a mace
          Else
            tmpargument = [AXEE]
            SetTargetToWideBlahID
              tmpx = 1			//Found a axe
            Else
              tmpargument = [CLAW]
              SetTargetToWideBlahID
                tmpx = 1			//Found a claw
              Else
                tmpargument = [HAMM]
                SetTargetToWideBlahID
                  tmpx = 1			//Found a hammer
                Else
                  tmpargument = [POLE]
                  SetTargetToWideBlahID
                    tmpx = 1			//Found a spear
        tmpy = 1
        IfXIsEqualToY				//tmpx = 1 means something was found
          tmpx = targetdistance
          tmpy = 50
          IfXIsLessThanY
            tmpargument = LATCHALTRIGHT
            PressLatchButton
          tmpx = targetx
          tmpy = targety
          tmpturn = targetturnto
          tmpdistance = 100
          Compass
          ClearWaypoints
          AddWaypoint

//------------------------------------------------------------------------------
IfHealed
  tmpargument = 7
  SendMessageNear

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