//The definition of the diffrent states:
//0: Follow master
//1: Combat


//How often can it attack?
IfUsed
  tmpargument = rand & 50 + 35
  SetReloadTime

IfKilled
  tmpargument = 3
  PlayFullSound
  tmpargument = 0			  // Last words...
  SetState
  SendMessageNear
  GoPoof				//No body


IfAttacked				// Attacked
  SetTargetToWhoeverAttacked		  //
    IfTargetIsAlive			    //
      IfTargetIsOnHatedTeam		      // Go after 'em
        tmpargument = 1			        //
        SetState			        //  
        tmpdistance = rand & 2047 + 11000       // Ouch!
        tmpargument = rand & 1 + 1
        PlaySound			        //
    Else				    //Attacker died already
      SetTargetToOldTarget		      //


IfTargetKilled				// Return to follow mode
  tmpargument = 0			  // State 0 ( Follow )
  SetState				  //


IfTimeOut				// This is done every so often
  SetTargetToWideEnemy		  // Look out for enemies
    tmpargument = 1			      //
    SetState				      // Change to combat mode
    tmpargument = selfhateid		      //
    IfTargetHasID			      // Battle cry versus hate group
      IfTargetIsOldTarget		        //
        DoNothing
      Else				        //
        tmpargument = 2			          //
        SendMessageNear			          //
        tmpargument = 1			          //
        tmpdistance = rand & 2047 + 11000         //
        PlaySound			          //

  IfStateIs1				  // State 1 ( Combat )

    //!!!SPECIAL AI FOR WIZARD STARTER!!!
    SetOldTarget			//Remember our enemy
    tmpargument = [LICH]
    tmpdistance = 2
    SetTargetToNearestBlahID		//Find the boss lich
      tmpx = targetdistance
      tmpy = 520
      IfXIsMoreThanY			//Boss lich is too far away
        SetTargetToOldTarget
    Else
      SetTargetToOldTarget		//Not found

    //BITE ATTACK
    tmpx = targetdistance		    // Close enough to attack?
    tmpy = 200				    //
    IfXIsLessThanY			    //
      tmpargument = LATCHRIGHT		      // Right Attack == 2
      PressLatchButton	

    //LIGHTNING POWER
    IfFacingTarget			//Do we fire a lightning bolt?
      tmpx = rand & 50
      tmpy = 0				//2% chance each check
      IfXIsEqualToY
        tmpx = selfmana
        tmpy = 1280			//Need at least 5 mana
        IfXIsMoreThanY
          tmpargument = 7         //part0.txt
          tmpx = selfx            // x
          tmpy = selfy            // y
          tmpdistance = selfz     // z
          SpawnExactParticle      //Unleash the power!!
          SetTargetToSelf
          tmpargument = 1792		//7 mana
          CostTargetMana
          SetTargetToOldTarget

    //NOVA POWER
    tmpx = targetdistance		    // Close enough to use nova power?
    tmpy = 450				    
    IfXIsLessThanY			    
      tmpx = rand & 50			    //2% chance each check
      tmpy = 0
      IfXIsEqualToY
        tmpx = selfmana
        tmpy = 1792
        IfXIsMoreThanY			//Enough mana?
          tmpargument = 0         //part0.txt
          tmpx = selfx            // x
          tmpy = selfy            // y
          tmpdistance = selfz     // z
          SpawnExactParticle      //Unleash the power!!
          tmpargument = 3
          SendMessageNear	  //Tell them what happened
          SetTargetToSelf
          tmpargument = 1792		//7 mana
          CostTargetMana
          SetTargetToOldTarget		//Target enemy again

    tmpx = targetdistance
    tmpy = 700
    IfXIsMoreThanY			//Target ran away
      tmpargument = 0
      SetState
    tmpx = leaderdistance
    IfXIsMoreThanY			//Leader ran away
      tmpargument = 0
      SetState

    IfStateIs1				// Move towards enemy
      tmpx = targetx			    
      tmpy = targety
      tmpturn = targetturnto		    
      tmpdistance = 200			    
      Compass		    
      ClearWaypoints			  
      AddWaypoint		


  IfStateIs0				  // State 0 ( Follow )
    tmpx = leaderx			    //
    tmpy = leadery			    //
    tmpturn = 16384 + leaderturn	    // Stand to side
    tmpdistance = 100			    //
    Compass				    //
    ClearWaypoints			  
    AddWaypoint		  

  tmpargument = rand & 20 + 15		 // Try again later
  SetTime


IfBumped				// Bumped
  SetTargetToWhoeverBumped		  //
  IfTargetIsOnHatedTeam			  // Try to counter attack
    tmpargument = LATCHRIGHT		    //
    PressLatchButton			    //
  Else					  // Get around other character
    tmpargument = 0			    // Return to follow state
    SetTargetToNearbyEnemy		    // Look out for enemies
      tmpargument = 1			      // Stay in combat state
    Else				    // Revert
      SetTargetToOldTarget		      //
    SetState				    //
    tmpx = rand&511+selfx-256		    //
    tmpy = rand&511+selfy-256		    //
    ClearWaypoints			    //
    AddWaypoint				    //
    tmpargument = 40			    // Try again soon
    SetTime				    //


//Manage some flashy start things and set some values
IfSpawned
  tmpargument = 0       //part0.txt
  tmpx = selfx            // x
  tmpy = selfy            // y
  tmpdistance = selfz     // z
  SpawnExactParticle      //Unleash the power!!
  SpawnExactParticle      //Unleash the power - twice!!
  tmpargument = 3
  PlaySound
  JoinGoodTeam
  
End					// Finished with this character
