//------------------------------------------------------------------------------------
IfSpawned
  JoinEvilTeam
  BecomeLeader

//------------------------------------------------------------------------------------
//Deader than dead
IfKilled				// This reduces the height of the char
  SetTargetToWhoeverAttacked
  tmpargument = 0			    //
  SendMessageNear			    //
  tmpargument = 40			  //
  SetBumpSize				  //
  tmpargument = 45			  //
  SetBumpHeight				  //
  tmpargument = 65535			  // Drop money
  DropMoney				  //
  DropKeys
  tmpargument = 2			  //
  PlaySound				  //
  tmpargument = 1
  ChangeArmor

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

  //Run away!
  tmpargument = 2
  IfStateIsNot
    GetContent
    tmpx = tmpargument
    tmpy = 0
    IfXIsMoreThanY
      tmpargument = 3
      SetState

//Run away!
IfBumped
  SetTargetToWhoeverBumped
  IfTargetIsOnHatedTeam
    tmpargument = 3
    SetState			  
  Else
    SetTargetToOldTarget

//------------------------------------------------------------------------------------
//The main AI loop
IfTimeOut			// This is done every so often	
  tmpargument = rand & 15 + 30
  SetTime		    

  // Play a random grunt?
  tmpx = rand & 255			  
  tmpy = 15				  
  IfXIsLessThanY			  
    tmpargument = 5			    
    PlaySound	

  //State 0 ( Wander )
  IfStateIs0	
    SetTargetToNearbyEnemy		  // Look for enemies
      tmpargument = 1			    // Enter combat mode
      SetState				    
      BecomeLeader
    Else				//No enemies?
      tmpargument = 0			//Then wander around again
      SetState			    			    
      tmpx = rand & 1023 - 512 + selfspawnx
      tmpy = rand & 1023 - 512 + selfspawny
      ClearWaypoints			  
      AddWaypoint				  

  // State 1 ( Combat )
  IfStateIs1
    SetTargetToNearestEnemy
    tmpx = targetdistance
    tmpy = 1500
    IfXIsLessThanY
      tmpx = 450
      IfXIsLessThanY			//Charge or maneouver?
        tmpdistance = 200
        Walk
        GetContent
        tmpx = tmpargument
        tmpy = 0
        IfXIsEqualToY
          tmpargument = 2
          SetState
      Else
        tmpdistance = 1500		//Move fast if target is far away
        Run 
      tmpx = rand & 255 + targetx - 128                  //Always get in combat position
      tmpy = rand & 255 + targety - 128      
      tmpturn = targetturnto
      Compass			
      ClearWaypoints			  
      AddWaypoint
    Else				//Target too far away
      tmpargument = 0
      SetState

  //State 3 - Retreat!
  IfStateIs3
    tmpargument = 150
    SetTime
    tmpx = rand & 255 + targetx - 128                  //Always get in combat position     
    tmpy = rand & 255 + targety - 128      
    tmpturn = targetturnto
    tmpdistance = 0 - 1500
    Compass			
    ClearWaypoints			  
    AddWaypoint
    tmpargument = 1
    SetState
    
//State 2 - Use breath attack
IfStateIs2
  SetTargetToNearbyEnemy
    SetTurnModeToWatchTarget
    tmpy = targetdistance
    tmpx = 150
    IfXIsLessThanY			//Close enough for breath attack?
      Run
      tmpx = selfx                  //Breath attack position
      tmpy = selfy      
      tmpturn = targetturnto
      tmpdistance = 0
      Compass			
      ClearWaypoints			  
      AddWaypoint

      IfFacingTarget
        GetContent
        tmpx = tmpargument
        tmpy = 50
        IfXIsLessThanY	     //This stuff makes the breath correctly
          tmpdistance = selfz     //appear out of the lizards mouth
          tmpturn = targetturnto
          tmpx = selfx
          tmpy = selfy 
          Compass    
          tmpargument = 0
          SpawnExactParticle
          tmpargument = 0
          IfContentIs
            tmpargument = 3
            PlaySound
            tmpargument = 4
            PlaySound
          GetContent
          tmpargument = tmpargument + 1
          SetContent      		//Recharge breath attack
          Stop
          tmpargument = ACTIONUA
          DoAction
        Else
          tmpargument = 3
          SetState				//Retreat to recharge
    Else
      tmpx = rand & 255 + targetx - 128                  //Breath attack position
      tmpy = rand & 255 + targety - 128      
      tmpturn = targetturnto
      tmpdistance = 0 - 50
      Compass			
      ClearWaypoints			  
      AddWaypoint
  Else                    
    tmpargument = 1
    SetState				//Enemy too far away to breathe at
Else
  SetTurnModeToVelocity

//Recharge breath attack
tmpx = rand & 3
tmpy = 0
IfXIsEqualToY
  GetContent
  tmpx = tmpargument
  tmpy = 0
  IfXIsMoreThanY
    tmpargument = tmpargument - 1
    SetContent				

End					// Finished with this character
