//------------------------------------------------------------------------------
//Make owner same as leader...
IfSpawned
  SetTargetToLeader
  SetOwnerToTarget

  //Play the music
  tmpargument = 5
  tmpdistance = 0
  PlayMusic

//------------------------------------------------------------------------------
//Move around...  Start at state 0
IfTimeOut
  
  // Load more bullets
  IfHoldingRangedWeapon
    DoNothing
  Else
    SetTargetToSelf
    tmpargument = [GONN]
    RestockTargetAmmoIDFirst
      tmpargument = ACTIONMC
      DoAction
      tmpargument = 6
      SendMessageNear
    SetTargetToOldTarget
  SetTurnModeToVelocity
  
  IfStateIs6
    // Kill 'em all!
    IfTargetIsAlive
      // Check enemy level
      tmpx = selfz + 50
      tmpy = targetz
      IfXIsLessThanY
        tmpargument = 4
        SetState
      Else
        // Fire away
        SetTurnModeToWatchTarget
        IfFacingTarget
          IfHoldingRangedWeapon
            tmpargument = LATCHRIGHT
            PressLatchButton
        tmpx = targetx
        tmpy = targety
        ClearWaypoints
        AddWaypoint
        tmpargument = 20
        SetTime
    Else
      tmpargument = 3
      SetState
      
  // Fire at enemy
  IfStateIs5
    IfTargetIsOnHatedTeam
      SetOldTarget
      SetTargetToNearestFriend
        IfFacingTarget
          SetTargetToOldTarget
          tmpx = targetdistance
          tmpy = ownerdistance
          IfXIsLessThanY
            // The baddy is in front of the frenny...
            // Check enemy level
            SetTargetToOldTarget
            tmpx = selfz + 75
            tmpy = targetz
            IfXIsLessThanY
              tmpargument = 4
              SetState
            Else
              // Open fire
              SetTurnModeToWatchTarget
              IfFacingTarget
                IfHoldingRangedWeapon
                  tmpargument = LATCHRIGHT
                  PressLatchButton
                tmpargument = 20
              Else
                tmpx = targetx
                tmpy = targety
                ClearWaypoints
                AddWaypoint
                tmpargument = 10
          Else
            // The shot isn't clear, so close in
            tmpx = targetx
            tmpy = targety
            ClearWaypoints
            AddWaypoint
            tmpargument = 10
        Else
          // Check enemy level
          SetTargetToOldTarget
          tmpx = selfz + 75
          tmpy = targetz
          IfXIsLessThanY
            tmpargument = 4
            SetState
          Else
            // Open fire
            SetTurnModeToWatchTarget
            IfFacingTarget
              IfHoldingRangedWeapon
                tmpargument = LATCHRIGHT
                PressLatchButton
              tmpargument = 20
            Else
              tmpx = targetx
              tmpy = targety
              ClearWaypoints
              AddWaypoint
              tmpargument = 10
      Else
        // Check enemy level
        tmpx = selfz + 75
        tmpy = targetz
        IfXIsLessThanY
          tmpargument = 4
          SetState
        Else
          // Open fire
          SetTurnModeToWatchTarget
          IfFacingTarget
            IfHoldingRangedWeapon
              tmpargument = LATCHRIGHT
              PressLatchButton
            tmpargument = 30
          Else
            tmpx = targetx
            tmpy = targety
            ClearWaypoints
            AddWaypoint
            tmpargument = 10
    Else
      //Not an enemy target
      tmpargument = 4
      SetState
      
  IfStateIs4
    // Check pit
    tmpx = selfz
    tmpy = 50
    IfXIsLessThanY
      // Try to get out of the pit
      tmpargument = 0
      SetState
    Else
      // Check leader height
      SetTargetToLeader
      tmpx = targetz
      tmpy = selfz + 75
      IfXIsMoreThanY
        // Leader is too high, so wait...
        tmpx = selfx
        tmpy = selfy
        ClearWaypoints
        AddWaypoint
        tmpargument = 10
      Else
        // Follow the leader
        tmpdistance = 128*8  // Square of distance to check
        SetTargetToDistantEnemy
          // Go get 'em
          tmpargument = 5
          SetState
          tmpargument = 0
        Else
          // Follow
          tmpx = ownerdistance
          tmpy = 150
          IfXIsMoreThanY
            // Run to catch up
            tmpx = ownerx
            tmpy = ownery
            tmpturn = ownerturnto
            tmpdistance = 200
            Compass
          Else
            // Give 'im some room
            tmpx = ownerx
            tmpy = ownery
            tmpturn = ownerturnto + 32768
            tmpdistance = 120
            Compass
          ClearWaypoints
          AddWaypoint
          tmpargument = 20

  IfStateIs3
    // Am I out?
    tmpx = selfz
    tmpy = 50
    IfXIsLessThanY
      // Fell back in...  Try again
      tmpargument = 0
      SetState
    Else
      // Wait for the player to catch up
      tmpx = ownerdistance
      tmpy = 200
      IfXIsLessThanY
        // "I'll watch your back"
        tmpargument = 6
        PlaySound
        tmpargument = 5
        SendMessageNear
        tmpargument = ACTIONMC
        DoAction
        tmpargument = 4
        SetState
      Else
        // Check for baddies while out
        tmpdistance = 128*8  // Square of distance to check
        SetTargetToDistantEnemy
          // Go berserk
          tmpx = targetx
          tmpy = targety
          tmpargument = 6
          SetState
        Else
          tmpx = selfx
          tmpy = 3900
          ClearWaypoints
          AddWaypoint
    tmpargument = 10
    
  IfStateIs2
    // Jump out of the pit
    tmpx = selfx
    tmpy = 3968
    ClearWaypoints
    AddWaypoint
    
    // Help a little...
    tmpy = 12
    tmpx = 0
    SetTargetToSelf
    AccelerateTarget
    tmpargument = LATCHJUMP
    PressLatchButton
    tmpargument = 3
    SetState
    tmpargument = 40

  IfStateIs1
    // Jump onto the crate
    tmpargument = [CRAT]
    tmpdistance = BLAHITEMS + BLAHFRIENDS + BLAHENEMIES
    SetTargetToNearestBlahID
      tmpx = targetdistance
      tmpy = 100
      IfXIsLessThanY
        tmpx = targetx
        tmpy = targety
        tmpdistance = 300
        tmpturn = targetturnto
        Compass
        ClearWaypoints
        AddWaypoint
        tmpargument = LATCHJUMP
        PressLatchButton
        tmpargument = 2
        SetState
        tmpargument = 40
      Else
        // Walk to the crate
        tmpx = targetx
        tmpy = targety
        ClearWaypoints
        AddWaypoint
        tmpargument = 5
    Else
      // Walk back to center if there weren't any crates
      tmpx = 2070
      tmpy = 3200
      ClearWaypoints
      AddWaypoint
      tmpargument = 1
      SetState
      tmpargument = 40
      
  IfStateIs0
    // Check for enemies in the pit
    SetTargetToWideEnemy
      tmpx = targetz
      tmpy = 50
      IfXIsLessThanY
        // The enemy is in the pit, so go get 'em
        tmpx = targetx
        tmpy = targety
        ClearWaypoints
        AddWaypoint
        tmpargument = 5
        SetState
        tmpargument = 10
      Else
        // Get near the crate
        tmpargument = 1
        SetState
        tmpargument = 15
    Else
      // Get near the crate
      tmpargument = 1
      SetState
      tmpargument = 15
  SetTime

//------------------------------------------------------------------------------
//Handle death by sending a message and other stuff
IfKilled
  tmpargument = 3
  PlaySound
  tmpargument = MESSAGEDEATH
  IfTargetIsOnSameTeam
    tmpargument = MESSAGEFRAG
    IfTargetIsSelf
      tmpargument = MESSAGEACCIDENT
  SendMessage

  // Drop goodies
  tmpargument = selfmoney
  DropMoney

  // Make the character body
  tmpargument = 45
  SetBumpHeight

//------------------------------------------------------------------------------
//Handle being attacked by blocking or countering or running away
IfAttacked
  SetTargetToWhoeverAttacked
  IfTargetIsOnSameTeam
    tmpargument = 2
    PlaySound
    tmpargument = MESSAGEOUCH
    SendMessageNear
  Else
    tmpargument = rand & 1 + 4
    PlaySound

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