//The way to the treasure is closed
IfSpawned
  tmpargument = passage
  ClosePassage
  tmpargument = 100
  SetTime
  
//Backstabbing!
IfBackstabbed
  SetTargetToWhoeverAttacked
  tmpargument = 70
  tmpdistance = EXPMURDER
  GiveExperienceToTarget
      
  SetTargetToSelf
  KillTarget
      
  tmpargument = 1
  SendMessageNear
  
// ZZ> Death and let the player team win
IfKilled
  tmpargument = 0
  SendMessageNear
  PlaySound
  SpawnPoof

  // Award quest experience
  tmpargument = 100
  tmpdistance = EXPQUEST
  GiveExperienceToTargetTeam
  
  //Open gate
  tmpargument = passage
  OpenPassage

  //Victory tune
  tmpargument = 14
  tmpdistance = 0
  PlayMusic

  //Reduce size
  tmpargument = 40			  
  SetBumpSize				  
  tmpargument = 35			  
  SetBumpHeight				  			  

  // Drop money
  tmpargument = selfmoney
  DropMoney

//Ouch
IfAttacked				// Counter attack
  SetTargetToWhoeverAttacked		  //
    IfTargetIsOnHatedTeam		    //
      BecomeLeader			      //
      tmpargument = 1			      // Enter combat mode
      SetState				      //
      SetTurnModeToWatchTarget
    Else				    //
      SetTargetToOldTarget		      //
  tmpargument = rand & 1 + 1		  //
  PlaySound

//----------------------------------------------------------------------
// Return to watch mode
IfTargetKilled
  tmpargument = 0			  //
  SetState				  //

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

//----------------------------------------------------------------------
//Main AI loop
IfTimeOut
  tmpargument = rand & 15 + 30		  // Wait a while and try again
  SetTime				  //
  
  //Guarding
  IfStateIs0
    SetTurnModeToSpin
    tmpx = selfx
    tmpy = selfy
    SetTargetToNearestEnemy
      IfFacingTarget
        SetTurnModeToWatchTarget
        tmpargument = 1
        SetState
        
  //Engaged in combat
  IfStateIs1
  
    //Shoot!
    tmpx = rand & 1
    tmpy = 0
    IfXIsEqualToY
      IfFacingTarget
        tmpargument = 0
        tmpx = selfx
        tmpy = selfy
        tmpdistance = 75
        tmpturn = selfturn
        Compass
        tmpdistance = selfz + 125
        SpawnExactParticle
    
    //Move closer or retreat
    tmpx = targetdistance
    tmpy = 10 < 7             //Up to 10 tiles
    IfXIsLessThanY
      tmpx = targetx
      tmpy = targety
      tmpturn = targetturnto
      tmpdistance = 0-250
      Compass
    Else
      tmpx = selfspawnx
      tmpy = selfspawny
      tmpargument = 0
      SetState
      tmpargument = 100
      SetTime	
  ClearWaypoints			  //
  AddWaypoint				  //

  //Boss music
  IfKilled
    DoNothing
  Else
    SetOldTarget
    SetTargetToWideEnemy
      tmpargument = 6
      tmpdistance = 0
      PlayMusic
    SetTargetToOldTarget
    

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