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

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


//------------------------------------------------------------------------------
// ZZ> Move around...  Start at state 0
IfTimeOut
  // Load more bullets
  IfHoldingRangedWeapon
    GetState  // NOP
  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
  IfStateIs5
    // Fire at enemy
    IfTargetIsAlive
      SetTargetToNearestFriend
        IfFacingTarget
          SetTargetToOldTarget
          tmpx = targetdistance
          tmpy = ownerdistance
          IfXIsLessThanY
            // The baddy is in front of the frenny...
            // Check enemy level
            SetTargetToOldTarget
            tmpx = selfz + 50
            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 + 50
          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 + 50
        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
      // The target is dead...  Follow leader
      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 + 50
      IfXIsMoreThanY
        // Leader is too high, so wait...
        tmpx = selfx
        tmpy = selfy
        ClearWaypoints
        AddWaypoint
        tmpargument = 10
      Else
        // Follow the leader
        tmpdistance = 250000  // 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
      SetTargetToOwner
      tmpx = targetdistance
      tmpy = 200
      IfXIsLessThanY
        // "I'll watch your back"
        tmpargument = 6
        tmpdistance = 11025
        PlaySound
        tmpargument = 5
        SendMessageNear
        tmpargument = ACTIONMC
        DoAction
        tmpargument = 4
        SetState
      Else
        // Check for baddies while out
        tmpdistance = 250000  // 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 = 6
    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
    SetTargetToNearestEnemy
      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
        tmpx = 2304
        tmpy = 3550
        ClearWaypoints
        AddWaypoint
        tmpargument = 1
        SetState
        tmpargument = 150
    Else
      // Get near the crate
      tmpx = 2304
      tmpy = 3550
      ClearWaypoints
      AddWaypoint
      tmpargument = 1
      SetState
      tmpargument = 200
  SetTime


//------------------------------------------------------------------------------
// ZZ> Handle death by sending a message and other stuff
IfKilled
  tmpargument = 3
  tmpdistance = rand & 1027 + 11000
  PlaySound
  tmpargument = MESSAGEDEATH
  IfTargetIsOnSameTeam
    tmpargument = MESSAGEFRAG
    IfTargetIsSelf
      tmpargument = MESSAGEACCIDENT
  SendMessage

  // Drop goodies
  tmpargument = 65535
  DropMoney

  // Make the character body
  tmpargument = 45
  SetBumpHeight


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



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