//The different states
//0: Watch mode
//1: Combat mode
//3: Backstabbed

//---------------------------------------------------------------------------------
//Unsummon ourselves if ordered to do so
IfOrdered
  tmpx = selforder
  tmpy = [UNDO]
  IfXIsEqualToY
    SetTargetToSelf
    KillTarget

//---------------------------------------------------------------------------------
//What to do if warned
IfCalledForHelp
  SetTargetToWideEnemy
    tmpargument = 3
    IfStateIsNot		//Dont if backstabbed
      tmpargument = 1
      SetState
  Else
    SetTargetToOldTarget
    tmpargument = 0
    SetState

//---------------------------------------------------------------------------------
//New round
IfTimeOut
  tmpargument = rand & 15 + 30
  SetTime

  //WATCH MODE
  IfStateIs0
    Sneak
    SetTargetToWideEnemy		//Enemy nearby?
      IfFacingTarget			//See the enemy
        tmpx = targetz
        tmpy = 300
        IfXIsLessThanY			//Target not flying or jumping out of sight?
          tmpargument = 1		//Enter combat mode
          SetState
    
    //Spin around when we are home
    tmpx = spawndistance
    tmpy = 100
    IfXIsLessThanY
      SetTurnModeToSpin

    //Stay where we spawned
    tmpx = selfspawnx
    tmpy = selfspawny
    
  //COMBAT MODE
  IfStateIs1
    SetTurnModeToVelocity
    Run
    SetTargetToWideEnemy		//Enemy nearby?
      CallForHelp
      
      //shoot eye ray
      IfFacingTarget
        tmpargument = rand & 31 + 50	//Time between shots
        SetTime
        tmpargument = rand % 3 + 1	//Randomize ray
        tmpx = selfx
        tmpy = selfy
        tmpdistance = selfz + 50
        SpawnExactParticle
//        tmpargument = 256		//1 ray costs 1 mana
//        SetTargetToSelf
//        CostTargetMana
        
        //Battlelust!
        tmpargument = 0
        SetContent
        
      //We cant see them... if this goes on for long, then we give up
      Else
        tmpargument = selfcontent + 1
        SetContent
        
      tmpx = targetdistance
      tmpy = 400
      IfXIsLessThanY
        IfFacingTarget
          SetTurnModeToWatchTarget
          tmpx = targetx
          tmpy = targety			//Move towards him or her
          tmpdistance = -400
          tmpturn = targetturnto
          Compass
        Else
          tmpx = targetx
          tmpy = targety			//Move towards him or her
      Else
        tmpx = targetx
        tmpy = targety			//Move towards him or her


    Else				//Enemy ran away
      tmpargument = 0
      SetState
      SetContent
      SetTurnModeToVelocity
      
    //We give up if the player outsmarts us
    tmpargument = 8
    IfContentIs
      tmpargument = 0
      SetState
      SetContent
      SetTurnModeToVelocity
      
  ClearWaypoints			  //
  AddWaypoint				  //

//---------------------------------------------------------------------------------
//Change skin depending on state
tmpargument = selfstate
SetTargetToSelf
IfTargetIsAlive		//and when killed
  DoNothing
Else
  tmpargument = 2
SetTargetToOldTarget
ChangeArmor		//Do it

//---------------------------------------------------------------------------------
//Return to watch mode
IfStateIs1				//Only if already in combat
  IfTargetKilled
    SetTargetToNearestEnemy
      tmpargument = 1
      SetState			//Combat
    Else
      tmpargument = 0			//Watch mode
      SetState

//---------------------------------------------------------------------------------
//How to react if bumped
IfBumped				// Attack whoever it bumped
  SetTargetToWhoeverBumped
  IfTargetIsOnHatedTeam			  // Make sure it's an enemy
    tmpargument = 3
    IfStateIsNot			  //Dont if backstabbed
      tmpargument = 1			  //Enter combat mode
      SetState
      CallForHelp		//Warn others (Only if not backstabbed)
      tmpx = targetx		//Move away from melee
      tmpy = targety
      tmpdistance = 400
      tmpturn = targetturnto + 36000
      Compass
      ClearWaypoints
      AddWaypoint
  Else					    //Move out of the way of friendlies
    tmpx = rand & 255 + targetx - 128
    tmpy = rand & 255 + targety - 128
    ClearWaypoints
    AddWaypoint
    SetTargetToOldTarget

//---------------------------------------------------------------------------------
IfBackstabbed
  SetTargetToSelf
  KillTarget			//Yep, instant kill
  SetTargetToWhoeverAttacked
  tmpargument = 25
  tmpdistance = EXPMURDER
  GiveExperienceToTarget		//Give some bonus xp
  tmpargument = 3
  SetState			//Backstabs are undetected
  SetContent			//For death message
  tmpargument = 1
  SendMessageNear

//How to react if attacked
IfAttacked
  SetTargetToWhoeverAttacked
  IfTargetIsOnHatedTeam
    tmpargument = 3			//Backstabbed?
    IfStateIsNot
      tmpargument = 1			//Nope, enter combat mode
      SetState
      CallForHelp		//Warn others (Only if not backstabbed)
  Else
    SetTargetToOldTarget

  tmpargument = rand % 3		  //Play damaged sound
  PlaySound

//---------------------------------------------------------------------------------
//What to do if killed
IfKilled
  //Messaging
  tmpargument = 3	//Backstabbed?
  IfContentIs
    tmpargument = 1
  Else
    tmpargument = 0	//Normal death

  SendMessageNear

  //Make the body
  tmpargument = 33
  SetBumpSize
  tmpargument = 50
  SetBumpHeight

  tmpargument = 7
  SetShadowSize

  //Drop stash
  tmpargument = selfmoney
  DropMoney
  DropKeys

  //Death sound
  tmpargument = 1
  PlaySound

  //Death stuff
  tmpargument = 0
  SetFlyHeight
  tmpargument = 10
  SetShadowSize
  KeepAction

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