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

//------------------------------------------------------------------------------
// ZZ> Handle death by sending a message and telling allies to run away
IfKilled

  // Drop goodies
  tmpargument = selfmoney
  DropMoney
  DropKeys
  DropItems

  //Award some bonus xp
  tmpargument = 50
  tmpdistance = EXPQUEST
  GiveExperienceToGoodTeam

  // 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
  tmpargument = 3
  PlaySound

  //Bludy body
  tmpargument = 1
  ChangeArmor

//------------------------------------------------------------------------------
// ZZ> Handle being bumped by countering or wandering
IfBumped
  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
  IfTargetIsOnHatedTeam
    tmpargument = [LUMP]
    IfTargetHasID
      tmpargument = 0
      IssueOrder
    tmpargument = rand & 1
    tmpargument = tmpargument < 3 - tmpargument
    SetState

//------------------------------------------------------------------------------
//Handle being healed by spitting out a message
IfHealed
  tmpargument = 4
  SendMessageNear

//------------------------------------------------------------------------------
// ZZ> Handle blocking an attack by countering
IfBlocked
  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
    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
            IfTargetIsFacingSelf      // Is the enemy facing the character?
              tmpargument = rand & 15 + 15
              SetTime
              tmpargument = STATEPARRY
    SetState

//------------------------------------------------------------------------------
// ZZ> This is the state chooser and interpreter
IfTimeOut

  //Check Gonne is out of ammo!
  IfHoldingMeleeWeapon				//Already changed to melee
    DoNothing
  Else
    IfHoldingRangedWeapon			//Still some ammo left
      DoNothing
    Else
      IfUnarmed					//Drop the gonne and draw steel!
        tmpargument = LATCHPACKRIGHT
        PressLatchButton
      Else
        DropWeapons
 
  // 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

    // Lower shield and attack
    IfStateIsParry
      tmpargument = STATECOMBAT

  // No enemy in general area, so return to the guard post
  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

  // 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 = 300
        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
        SetTurnModeToWatchTarget
        tmpx = targetx
        tmpy = targety
        tmpdistance = 0 - 100
        Compass
      Else
        // Find a weapon!!!
        tmpx = rand & 255 - 128 + selfx
        tmpy = rand & 255 - 128 + selfy
    tmpargument = rand & 7 + 8
  ClearWaypoints
  AddWaypoint
  SetTime

  //Heal nearby friends
  SetOldTarget
  SetTargetToNearestFriend
    tmpx = selfmana
    tmpy = 255
    IfXIsMoreThanY				//enough mana?
      IfTargetIsHurt
        IfFacingTarget
          tmpx = targetdistance
          tmpy = 350
          IfXIsLessThanY			//close enough?
            tmpargument = ACTIONMC
            DoAction				//cast the spell!
              tmpx = targetx
              tmpy = targety
              tmpdistance = targetz
              tmpargument = 4
              SpawnExactParticle
              tmpargument = rand & 511 + 512	//2-4
              HealTarget
              tmpargument = rand & 1
              PlaySound
              tmpargument = 2
              SendMessageNear
        tmpx = targetx
        tmpy = targety
        ClearWaypoints
        AddWaypoint				//move closer to heal
  SetTargetToOldTarget

Else
  // Hide behind shield until timer runs out
  IfStateIsParry
    IfHoldingShield
      PressLatchButton
      IfTargetIsAttacking
        tmpargument = 5
        SetTime
    Else
      tmpargument = 0
      SetTime

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