//The different States:
//0: Wander around
//1: Combat mode
//------------------------------------------------------------------------------
//Magical creature enchant (Dies if not enchanted)
IfSpawned
  SetTargetToSelf
  EnchantTarget  // Target is self...

//------------------------------------------------------------------------------
//Death effects
IfKilled				// This reduces the height of the char
  tmpargument = 0			    //
  SendMessageNear			    //
  tmpargument = selfmoney			  // Drop money
  DropMoney				  //
  tmpargument = 2			  //
  PlaySound				  //

//------------------------------------------------------------------------------
//This makes stalkers blink into visibility when attacked
IfAttacked
  tmpargument = 50
  SetTime
  SetDamageTime
  tmpargument = 2
  SetState
IfBumped
  SetTargetToWhoeverBumped
  IfTargetIsOnHatedTeam
    tmpargument = 40
    SetTime
    tmpargument = 2
    SetState
  Else
    SetTargetToOldTarget

//------------------------------------------------------------------------------
//Handle invisibility
IfStateIs2
  tmpargument = 75
  SetLight                //Visible
  tmpargument = 255
  SetAlpha
Else
  tmpargument = 0
  SetLight                //Invisible
  tmpargument = 0
  SetAlpha

//------------------------------------------------------------------------------
//Counter attack
IfAttacked
  SetTargetToWhoeverAttacked		  //
    IfTargetIsOnHatedTeam		    //
      tmpargument = 1			      // Enter combat mode
      SetState				      //
    Else				    //
      SetTargetToOldTarget		      //
  tmpargument = rand & 1		  //
  PlaySound				  //

//------------------------------------------------------------------------------
//Enemy is dead
IfTargetKilled				// Return to follow mode
  tmpargument = 0			  //
  SetState				  //

//------------------------------------------------------------------------------
//Somebody bumped
IfBumped				// Attack whoever it bumped
  SetTargetToWhoeverBumped		  //
  IfTargetIsOnHatedTeam			  // Make sure it's an enemy
    IfHoldingMeleeWeapon                    // Got Weapon?
      PressLatchButton			      //
  Else					  // Scooch around friendly
    tmpx = rand & 255 + targetx - 128	    //
    tmpy = rand & 255 + targety - 128	    //
    ClearWaypoints			    //
    AddWaypoint				    //
    SetTargetToOldTarget		    //

//------------------------------------------------------------------------------
IfTimeOut				// This is done every so often

  IfStateIs0				  // State 0 ( Wander )
    SetTargetToWideEnemy			  // Look for enemies
      tmpargument = 1			    // Enter combat mode
      SetState
    tmpx = selfcontent
    tmpy = 0
    IfXIsEqualToY
      tmpx = rand & 1023 - 512 + selfx	    //
      tmpy = rand & 1023 - 512 + selfy	    //
    Else
      // Guard starting area
      tmpx = rand & 511 - 256 + selfspawnx
      tmpy = rand & 511 - 256 + selfspawny
  IfStateIs1					  // State 1 ( Combat )
    tmpx = targetdistance		    // Too far?
    tmpy = 1400				    //
    IfXIsMoreThanY			    //
      tmpargument = 0
      SetState				      // Revert to follow mode
      tmpx = selfx
      tmpy = selfy
    Else
      tmpy = 150
      IfXIsLessThanY
        // Run away
        tmpx = targetx
        tmpy = targety
        tmpturn = targetturnto + 32768
        tmpdistance = 800
        Compass
      Else
        tmpy = 0  // Unarmed...
        IfHoldingMeleeWeapon		      // Got Weapon?
          tmpy = 350
        Else
          IfHoldingRangedWeapon
            tmpy = 800
        IfXIsLessThanY
          tmpx = 800
          IfXIsEqualToY
            // Ranged weapon, so lead the target
            tmpx = targetspeedx * tmpx > 4 + targetx
            tmpy = targetspeedy * tmpx > 4 + targety
            IfFacingTarget
              PressLatchButton
          Else
            // Melee weapon, so run around the target...
            tmpx = rand & 511 - 256 + targetx
            tmpy = rand & 511 - 256 + targety
            IfFacingTarget
              PressLatchButton
              tmpx = targetx
              tmpy = targety
        Else
          // Not close enough, so run at the target
          tmpx = targetx
          tmpy = targety
          tmpturn = targetturnto
          tmpdistance = 400
          Compass
  ClearWaypoints			  //
  AddWaypoint				  //
  tmpargument = rand & 15 + 10		  // Wait a while and try again
  SetTime

  //Become invisible again
  IfStateIs2
    tmpargument = 1
    SetState
//------------------------------------------------------------------------------
End					// Finished with this character
//------------------------------------------------------------------------------
