//------------------------------------------------------------------------------
IfSpawned
  tmpargument = 30
  SetTime


//------------------------------------------------------------------------------
// ZZ> Run away if too close to killer
IfOrdered
  tmpx = selforder
  tmpy = 0
  IfXIsEqualToY
    // Someone said that there's an enemy around, so look for 'im
    SetTargetToWideEnemy
  Else
    // Someone died, so make Scrub Lumpkins run away
    tmpargument = 0
    IfArmorIs
      // It's a Scrub, so check distance
      tmpx = selforder > 8
      tmpy = selforder & 255
      tmpx = selfx > 7 - tmpx
      tmpy = selfy > 7 - tmpy
      tmpturn = xydistance
      tmpdistance = 5  // Number of tiles

      IfDistanceIsMoreThanTurn
        tmpargument = 2
        SendMessageNear
        tmpargument = rand & 3 + 7
        SetContent
        tmpargument = STATERETREAT
        SetState



//------------------------------------------------------------------------------
// ZZ> Handle death by sending a message and telling allies to run away
IfKilled
  // Drop goodies
  tmpargument = 65535
  DropMoney
  DropKeys

  // Make the character body
  tmpargument = 45
  SetBumpHeight

  // Tell friends to run away
  tmpx = targetx > 7
  tmpy = targety > 7
  tmpargument = tmpx < 8 + tmpy
  IssueOrder



  // Send the message
  IfTargetIsOnOtherTeam
    tmpargument = MESSAGEDEATH
  Else
    tmpargument = 1
  SendMessageNear



  // Scream
  tmpdistance = rand & 2047 + 11000
  tmpargument = 2
  PlaySound



//------------------------------------------------------------------------------
// ZZ> Handle being bumped by countering or wandering
IfBumped
  IfStateIsRetreat
    GetState // NOP
  Else
    SetTargetToWhoeverBumped
    IfTargetIsOnHatedTeam
      tmpargument = [LUMP]
      IfTargetHasID
        SetTargetToOldTarget
      Else
        tmpargument = STATEPARRY
        SetState
    Else
      SetTargetToOldTarget



//------------------------------------------------------------------------------
// ZZ> Handle being attacked by blocking or countering or running away
IfAttacked
  // Scream
  tmpdistance = rand & 2047 + 11000
  tmpargument = 1
  PlaySound
  // Pick either 0 or 7, Parry or Combat
  SetTargetToWhoeverAttacked
  IfStateIsRetreat
    GetState  // NOP
  Else
    IfTargetIsOnHatedTeam
      tmpargument = [LUMP]
      IfTargetHasID
        tmpargument = 0
        IssueOrder
      tmpargument = rand & 1
      tmpargument = tmpargument < 3 - tmpargument
      // Check remaining life and remaining friends
      tmpx = selfmorale < 8 + selflife
      tmpy = 800
      IfXIsLessThanY
        tmpargument = rand & 3 + 7
        SetContent
        tmpargument = STATERETREAT
      SetState



//------------------------------------------------------------------------------
// ZZ> Handle being healed by spitting out a message and stopping retreat
IfHealed
  IfStateIsRetreat
    tmpargument = STATECHARGE
    SetState
    tmpargument = 5
  Else
    tmpargument = 4
  SendMessageNear



//------------------------------------------------------------------------------
// ZZ> Handle blocking an attack by countering
IfBlocked
  IfStateIsRetreat
    GetState  // NOP
  Else
    tmpargument = STATECOMBAT
    SetState


//------------------------------------------------------------------------------
// ZZ> Handle an aggressive enemy by checking dexterity to block
IfTargetIsAttacking
  // If it's not a threat, circle around the enemy
  IfStateIsParry
    GetState  // NOP
  Else
    IfStateIsRetreat
      GetState // NOP
    Else
      tmpargument = STATECOMBAT
      tmpx = targetdistance
      tmpy = 600
      IfXIsLessThanY    
        // Are the character and enemy facing opposite directions?
        tmpturn = targetturn - selfturn
        tmpx = tmpturn
        tmpy = 16000
        IfXIsMoreThanY
          tmpy = 48000
          IfXIsLessThanY
            // Does it pass the Dex check...  8191 is perfect
            tmpx = selfdex
            tmpy = rand & 16383
            IfXIsMoreThanY
              // Is the enemy facing the character?
              tmpx = targetturnto - targetturn
              tmpy = 30000
              IfXIsMoreThanY
                tmpargument = rand & 15 + 15
                SetTime
                tmpargument = STATEPARRY
              tmpy = 0 - 30000
              IfXIsLessThanY
                tmpargument = rand & 15 + 15
                SetTime
                tmpargument = STATEPARRY
      SetState



//------------------------------------------------------------------------------
// ZZ> This is the state chooser and interpreter
IfTimeOut
  // Choose a new state, by setting tmpargument
  // Look for enemies
  SetTurnModeToVelocity


  // Find an enemy
  tmpx = 0
  IfTargetIsOnHatedTeam
    // Already have an enemy
    tmpx = targetdistance
    tmpy = 900
    IfXIsMoreThanY
      tmpx = 0
      // Find a new enemy
      tmpargument = [LUMP]
      tmpdistance = 18  // BLAHENEMIES | BLAHINVERTID
      SetTargetToWideBlahID
        tmpx = 1
    Else
      // Keep the current enemy
      tmpx = 1
  Else
    tmpargument = [LUMP]
    tmpdistance = 18  // BLAHENEMIES | BLAHINVERTID
    SetTargetToWideBlahID
      tmpx = 1




  tmpy = 0
  IfXIsMoreThanY
    // Default to previous state
    GetState

    // Switch to combat if close enough
    IfStateIsCharge
      tmpx = targetdistance
      tmpy = 600
      IfXIsLessThanY
        tmpargument = STATECOMBAT

    // Fight 'em if there's nothing better to do
    IfStateIsGuard
      tmpargument = STATECOMBAT

    // Let courage come back ( content is used as a timer )
    IfStateIsRetreat
      GetContent
      tmpargument = tmpargument - 1
      SetContent
      tmpargument = 0
      IfContentIs
        tmpargument = STATECHARGE
      Else
        GetState

    // Lower shield and attack
    IfStateIsParry
      tmpargument = STATECOMBAT


  // No enemy in general area, so return to the guard post
  Else
    IfStateIsRetreat
      GetState
    Else
      tmpargument = STATEGUARD



  // Now set the state to whatever tmpargument is
  SetState


  // Interpret the states and set the latches
  // Defend
  IfStateIsParry
    IfHoldingShield
      PressLatchButton
      tmpx = selfx
      tmpy = selfy
      tmpargument = rand & 31 + 15

  // Return to guard post
  IfStateIsGuard
    tmpx = rand & 511 - 256 + selfspawnx
    tmpy = rand & 511 - 256 + selfspawny
    tmpargument = rand & 63 + 30

  // Run away
  IfStateIsRetreat
    tmpx = selfx
    tmpy = selfy
    tmpdistance = 400
    tmpturn = rand & 32767 + targetturnto + 16384
    Compass
    tmpargument = rand & 15 + 10

  // Approach
  IfStateIsCharge
    tmpx = targetx + selfx > 1
    tmpy = targety + selfy > 1
    tmpargument = rand & 15 + 10

  // Attack with melee weapon, Keep back with ranged
  IfStateIsCombat
    tmpturn = targetturnto
    IfHoldingRangedWeapon
      tmpx = selfz
      tmpy = targetz + 60
      IfSitting
        tmpy = targetz + 110
      IfXIsLessThanY
        IfSitting
          tmpx = tmpturn - selfturn
          tmpy = 6000
          IfXIsLessThanY
            tmpy = 0 - 6000
            IfXIsMoreThanY
              PressLatchButton
          tmpx = targetx
          tmpy = targety
        Else
          SetTurnModeToWatchTarget
          tmpx = tmpturn - selfturn
          tmpy = 6000
          IfXIsLessThanY
            tmpy = 0 - 6000
            IfXIsMoreThanY
              PressLatchButton
          tmpx = selfx
          tmpy = selfy
      Else
        tmpx = rand & 255 - 128 + selfx
        tmpy = rand & 255 - 128 + selfy
    Else
      IfHoldingMeleeWeapon
        tmpx = targetdistance
        tmpy = 200
        IfSitting
          tmpy = 400
        IfXIsLessThanY
          tmpx = tmpturn - selfturn
          tmpy = 6000
          IfXIsLessThanY
            tmpy = 0 - 6000
            IfXIsMoreThanY
              PressLatchButton
        Else
          // Wave weapon around if not mounted
          IfSitting
            GetState
          Else
            tmpx = rand & 1023
            tmpy = 800
            IfXIsMoreThanY
              // Is it caught in a trap?
              tmpx = selfaccel
              tmpy = 1
              IfXIsMoreThanY
                // Nope, so cheer...
                tmpargument = ACTIONMC
                DoAction
                tmpdistance = rand & 2047 + 11000
                tmpargument = 3
                PlaySound
        tmpx = targetx
        tmpy = targety
        tmpdistance = 200
        Compass
      Else
        // Find a weapon!!!
        tmpx = rand & 255 - 128 + selfx
        tmpy = rand & 255 - 128 + selfy
    tmpargument = rand & 7 + 8
  ClearWaypoints
  AddWaypoint
  SetTime
Else
  // Hide behind shield until timer runs out
  IfStateIsParry
    IfHoldingShield
      PressLatchButton
      IfTargetIsAttacking
        tmpargument = 5
        SetTime
    Else
      tmpargument = 0
      SetTime


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