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


//------------------------------------------------------------------------------
//Death effects
IfKilled				// This reduces the height of the char
  tmpargument = 0			    //
  SendMessageNear			    //
  tmpargument = 40			  //
  SetBumpSize				  //
  tmpargument = 45			  //
  SetBumpHeight				  //
  tmpargument = 65535			  // Drop money
  DropMoney				  //
  tmpargument = 2			  //
  tmpdistance = rand & 2047 + 6000	  //
  PlaySound				  //


//------------------------------------------------------------------------------
//Counter attack
IfAttacked				
  SetTargetToWhoeverAttacked		  //
    IfTargetIsOnHatedTeam		    //
      tmpargument = 1			      // Enter combat mode
      SetState				      //
    Else				    //
      SetTargetToOldTarget		      //
  tmpargument = rand & 1		  //
  tmpdistance = rand & 2047 + 6000	  //
  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
  SetTargetToWideEnemy			  // Look for enemies
    tmpargument = 1			    // Enter combat mode
    SetState				    //
  IfStateIs0				  // State 0 ( Wander )
    GetContent
    tmpx = tmpargument
    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
  Else					  // 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				  //

//------------------------------------------------------------------------------
End					// Finished with this character
//------------------------------------------------------------------------------
