//------------------------------------------------------------------------------
IfSpawned
  tmpargument = 30
  SetTime
  
//------------------------------------------------------------------------------
//Remove alert
IfTargetKilled
  IfTargetIsOnHatedTeam
    tmpargument = 0
    tmpx = 0
    tmpy = 0
    SetXY
    
  //Dont attack dead bodies
  IfStateIsRetreat
    DoNothing
  Else
    tmpargument = STATEGUARD
    SetState

//------------------------------------------------------------------------------
//Alert countdown
tmpargument = 0
GetXY
IfXIsMoreThanY
  tmpx = tmpx - 1
  SetXY

//------------------------------------------------------------------------------
// ZZ> Run away if too close to killer
IfOrdered
  
  // Someone said that there's an enemy around, so look for 'im
  tmpx = selforder
  tmpy = [FAER]
  IfXIsEqualToY
    tmpargument = 0
    tmpx = 50*25      //Stay alert for 25 seconds
    tmpy = 0
    SetXY
  
  // Someone died, so make Scrub Lumpkins run away
  tmpy = [RUNN]
  IfXIsEqualToY
    tmpargument = 3
    IfArmorIs
      DoNothing
    Else
      // It's a Scrub, so check distance
      SetTargetToNearestEnemy
      tmpargument = [LUMP]
      IfTargetHasID
        DoNothing
      Else
        tmpx = 10*128  // Number of tiles
        tmpy = targetdistance
        IfXIsMoreThanY
          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 = selfmoney
  DropMoney
  DropKeys

  // Make the character body
  tmpargument = 45
  SetBumpHeight

  // Tell friends to run away
  tmpargument = [RUNN]
  IssueOrder

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

  // Scream
  tmpargument = 3
  PlaySound

//------------------------------------------------------------------------------
// ZZ> Handle being bumped by countering or wandering
IfBumped
  IfStateIsRetreat
    DoNothing
  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
  tmpargument = 2
  PlaySound
  
  // Pick either 0 or 7, Parry or Combat
  SetTargetToWhoeverAttacked
  IfStateIsRetreat
    DoNothing
  Else
    IfTargetIsOnHatedTeam
      tmpargument = [LUMP]
      IfTargetHasID
        tmpargument = [FAER]
        IssueOrder
        tmpargument = 0
        tmpx = 25*50    //25 seconds
        tmpy = 0
        SetXY
      
      tmpargument = rand & 1
      tmpargument = tmpargument < 3 - tmpargument
      
      // Check remaining life and remaining friends
      tmpx = selfmorale < 8 + selflife
      tmpy = 1024
      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   //Get our morale back!
    SetState
    tmpargument = 5
  Else
    tmpargument = 4
  SendMessageNear

//------------------------------------------------------------------------------
//Handle blocking an attack by countering
IfBlocked
  IfStateIsRetreat
    DoNothing
  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
    DoNothing
  Else
    IfStateIsRetreat
      DoNothing
    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
  tmpargument = 0
  GetXY
  IfXIsMoreThanY
    SetTargetToWideEnemy          //If we are alert, target even morphed characters
  Else
    tmpx = 0
    IfTargetIsOnHatedTeam
      IfTargetIsAlive
        // Already have an enemy
        tmpx = targetdistance
        tmpy = 900
        IfXIsMoreThanY
          tmpx = 0
          // Find a new enemy
          tmpargument = [LUMP]
          tmpdistance = BLAHENEMIES + BLAHINVERTID
          SetTargetToWideBlahID
            tmpx = 1
        Else
          // Keep the current enemy
          tmpx = 1
      Else
        tmpargument = [LUMP]
        tmpdistance = BLAHENEMIES + BLAHINVERTID
        SetTargetToWideBlahID
          tmpx = 1    
    Else
      tmpargument = [LUMP]
      tmpdistance = 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
            DoNothing
          Else
            tmpx = rand & 1023
            tmpy = 800
            IfXIsMoreThanY
              // Is it caught in a trap?
              tmpx = selfaccel
              tmpy = 1
              IfXIsMoreThanY
                // Nope, so cheer...
                tmpargument = ACTIONMC
                DoAction
                tmpargument = tmpdistance & 1
                PlaySound
        tmpx = targetx
        tmpy = targety
        tmpdistance = 200
        Compass
      Else
        // Find a weapon!!!
        tmpx = 0
        tmpargument = [GONN]      //Gonne
        tmpdistance = BLAHITEMS
        SetTargetToWideBlahID
          tmpx = 1
        Else
          tmpargument = [CLAW]    //Claw
          SetTargetToWideBlahID
            tmpx = 1
          Else
            tmpargument = [SWOR]  //Sword
            SetTargetToWideBlahID
              tmpx = 1
            Else
              tmpargument = [TORC]  //Torch
              SetTargetToWideBlahID
                tmpx = 1
        tmpy = 1
        IfXIsEqualToY
          tmpx = targetdistance
          tmpy = 70
          IfXIsLessThanY
            tmpargument = rand & 1 + LATCHALTLEFT   //randomize left or right grab
            PressLatchButton
          tmpx = targetx
          tmpy = targety
        Else
          tmpx = rand & 1023 + selfx - 512
          tmpy = rand & 1023 + selfy - 512
    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
//------------------------------------------------------------------------------
