//Randomize skin, each skin has one elemental resistance
IfSpawned
  tmpargument = rand % 4
  ChangeArmor
  tmpargument = STATEGUARD
  SetState

//---------------------------------------------------------------------
//Paralysing effect
IfScoredAHit
  SetTargetToWhoeverWasHit
  IfTargetIsAlive
    tmpx = targetlife > 8
    tmpy = selfstr > 8
    tmpy = 25 + tmpy - tmpx
    tmpx = rand % 100
    IfXIsLessThanY
      tmpargument = selflevel*15 + 150	//3 seconds +0.3 per level
      SetTargetReloadTime
      tmpargument = 2
      SendMessageNear
      tmpargument = 3
      PlaySound
    tmpargument = 80
    SetReloadTime
  
  //Keep distance
  tmpargument = STATESURROUND 
  SetState

//---------------------------------------------------------------------
//Die
IfKilled				// This reduces the height of the char
  IfTargetIsOnHatedTeam			  // Final words
    tmpargument = 0			    //
  Else
    tmpargument = 1
  SendMessageNear
  tmpargument = 20			  // Dead height
  SetBumpHeight				  //
  tmpargument = 2			  // Sound
  PlaySound				  //


//---------------------------------------------------------------------
//Say ouch
IfAttacked				// Don't take kindly to attackers
  SetTargetToWhoeverAttacked		  //
  IfTargetIsOnHatedTeam
    tmpargument = STATECOMBAT
    SetState
  Else
    SetTargetToOldTarget
    
  tmpargument = rand & 1			    // Sound
  PlaySound				    //
    
//---------------------------------------------------------------------
// Attack whoever it bumped
IfBumped				
  SetTargetToWhoeverBumped		  //
  IfTargetIsOnHatedTeam			  // Make sure it's an enemy
    tmpargument = STATECOMBAT
    SetState
  Else					  // Scooch around friendly
    tmpx = rand & 1023 + targetx - 512      //
    tmpy = rand & 1023 + targety - 512      //
    ClearWaypoints			    //
    AddWaypoint				    //
    SetTargetToOldTarget		    //

//---------------------------------------------------------------------
//Main AI loop
IfTimeOut
  tmpargument = rand & 31 + 20		  // Wait a while and try again
  SetTime
  
  IfStateIsSurround
    SetTurnModeToWatchTarget
    tmpx = rand & 511 + targetx - 256
    tmpy = rand & 511 + targety - 256
    tmpdistance = 0 - 400
    tmpturn = targetturnto
    Compass
    ClearWaypoints
    AddWaypoint
    
    //Engage randomly
    tmpx = rand & 255
    tmpy = 30
    IfXIsLessThanY
      tmpargument = STATECOMBAT
      SetState
      tmpargument = 5
      PlaySound

  IfStateIsCombat
    tmpx = spawndistance
    tmpy = 128*10
    IfXIsMoreThanY
      tmpx = targetdistance
      tmpy = 512
      IfXIsMoreThanY
        tmpargument = STATEGUARD      //enemy ran away
        SetState
        
    tmpx = targetx
    tmpy = 400
    IfXIsMoreThanY
      tmpx = targetx
      tmpy = targety
      tmpdistance = 1500        //catch up fast
      tmpturn = targetturnto
      Compass
      ClearWaypoints			  //
      AddWaypoint				  //
    Else
      tmpx = rand & 511 - 256 + targetx	    //
      tmpy = rand & 511 - 256 + targety	    // attack from random direction!
      tmpturn = targetturnto
      tmpdistance = 400
      Compass
      ClearWaypoints			  //
      AddWaypoint				  //
      
    //Attack!
    IfTargetIsOnHatedTeam
      IfFacingTarget
        tmpx = targetdistance
        tmpy = 150
        IfXIsLessThanY
          tmpargument = LATCHLEFT		    // Bite 'em
          PressLatchButton

  IfStateIsGuard
    SetTurnModeToVelocity
    // Wander around home position
    tmpx = rand & 1023 - 512 + selfspawnx   //
    tmpy = rand & 1023 - 512 + selfspawny   //
    ClearWaypoints			  //
    AddWaypoint				  //
    
    SetTargetToNearbyEnemy
      tmpargument = STATESURROUND
      SetState
      tmpargument = 5
      PlaySound
      CallForHelp           //Alert the others

//---------------------------------------------------------------------
//Someone screamed for help
IfCalledForHelp
  SetTargetToWhoeverCalledForHelp
  tmpx = targetdistance
  tmpy = 128*8
  IfXIsLessThanY                //close enough to hear it?
    SetTargetToNearestEnemy
    tmpargument = STATESURROUND
    SetState                    //help em!
    
//---------------------------------------------------------------------
//Dont attack dead stuff
IfTargetKilled
  SetTargetToWideEnemy
    tmpargument = STATECOMBAT
  Else
    tmpargument = STATEGUARD
  SetState
	
//---------------------------------------------------------------------
End					// Finished with this character
//---------------------------------------------------------------------
