//Spawn the Evil glow
tmpargument = 4
tmpx = selfx
tmpy = selfy
tmpdistance = selfz
SpawnExactParticle

//Attack the enemy if he is approaching
IfBumped
  SetTargetToWhoeverBumped
  IfTargetIsOnHatedTeam
    IfFacingTarget
      tmpargument = LATCHRIGHT
      PressLatchButton
    Else
        tmpargument = 1
        SetState
  Else
    SetTargetToOldTarget

//------------------------------------------------------------------------------
IfTargetKilled
  SetTargetToNearestEnemy
    tmpargument = 1
    SetState
  Else
    tmpargument = 0
    SetState
    SetContent		//Reset battlecry (Enemies are defeated or fled)


//------------------------------------------------------------------------------
// ZZ> Handle death by sending a message and other stuff
IfKilled

  // Drop goodies
  tmpargument = 65535
  DropMoney
  DropKeys
  DropItems

  //Death message and sound
  tmpargument = 0
  SendMessageNear
  tmpargument = 4
  PlaySound

  // Make the character body
  tmpargument = 25
  SetBumpHeight

  // Boss stuff...
  BeatModule
  EnableExport
  tmpargument = [BEAT]
  AddIDSZ
  ClearEndMessage

  tmpargument = [LORD]
  tmpdistance = 6
  SetTargetToNearestBlahID	//Did lord hagen survive?
    tmpargument = 4
    AddEndMessage
    tmpargument = 5
    AddEndMessage		//TODO end message
    tmpargument = 50
    tmpdistance = EXPQUEST
    GiveExperienceToGoodTeam
    tmpargument = [LORD]
    AddIDSZ			//Special ending
  Else
    tmpargument = 4
    AddEndMessage
    tmpargument = 5
    AddEndMessage
    tmpargument = 20
    tmpdistance = EXPQUEST
    GiveExperienceToGoodTeam



//------------------------------------------------------------------------------
//Always target bumpers
IfBumped
  SetTargetToWhoeverBumped
  IfTargetIsOnHatedTeam
    tmpargument = 0
    IfStateIsNot
      tmpargument = 2
      SetState
  Else
    SetTargetToOldTarget


//------------------------------------------------------------------------------
//0 Wait for good guys
//1 Charge enemy
//2 Close combat

//Special order when the boss door is opened
IfOrdered
  tmpx = selforder
  tmpy = [ATTA]
  IfXIsEqualToY
    SetTargetToNearestEnemy
    tmpargument = 1		//Charge nearest enemy
    SetState
    SetContent			//Dont do more warcries this combat

    //Do the warcry
    tmpargument = 2
    SendMessageNear
    tmpargument = 5
    PlaySound

IfTimeOut

  // Wait for the good guys to barge in...
  IfStateIs0
    SetTargetToNearbyEnemy
      tmpargument = 0
      IfContentIs			//Do the battlecry
        tmpargument = 1
        SetContent
        tmpargument = 2
        SendMessageNear
        tmpargument = 5
        PlaySound
      tmpargument = 1			//Charge target
      SetState
      tmpargument = rand & 20
      SetTime
    Else
      Sneak				//33% speed
      tmpx = selfspawnx
      tmpy = selfspawny
      ClearWaypoints
      AddWaypoint			//Return to start
      tmpargument = rand & 15 + 50
      SetTime


  //Close Combat
  IfStateIs2
    Walk				//50% speed
    SetTurnModeToVelocity
    tmpargument = rand & 31 + 30
    SetTime
    tmpx = targetdistance
    tmpy = 180
    IfXIsLessThanY			// Close enough to swing...
      IfHoldingMeleeWeapon
        IfFacingTarget
          tmpargument = LATCHRIGHT
          PressLatchButton

    //Keep in combat position
    tmpx = targetdistance
    tmpy = 250
    IfXIsMoreThanY			//Keep a tactical distance
      SetTurnModeToWatchTarget
      tmpdistance = 0 - 200
    Else				//Move in for attack
      SetTurnModeToVelocity
      tmpdistance = 200
    tmpx = targetx
    tmpy = targety
    tmpturn = targetturnto
    Compass
    ClearWaypoints
    AddWaypoint

    //Is the target too far away?
    tmpx = targetdistance
    tmpy = 500
    IfXIsMoreThanY
      tmpargument = 1
      SetState
      SetTime

  // Rush the enemy
  IfStateIs1
    Run					//100% speed
    SetTurnModeToWatchTarget

    //Target is far away, pick the closest one
    tmpx = targetdistance
    tmpy = 1400
    IfXIsMoreThanY			
      SetTargetToNearestEnemy

    // Charge the enemy
    tmpx = targetx
    tmpy = targety
    tmpturn = targetturnto
    tmpdistance = 700
    Compass
    ClearWaypoints
    AddWaypoint
    tmpargument = 50
    SetTime

    //Close enough to chop?
    tmpx = targetdistance
    tmpy = 500
    IfXIsLessThanY
      tmpargument = 2
      SetState		


//------------------------------------------------------------------------------
//How to react if damaged
IfAttacked
  Run
  SetTurnModeToVelocity
  SetTargetToWhoeverAttacked		//Always target attackers
  tmpargument = rand & 1 + 2
  PlaySound

//------------------------------------------------------------------------------
//Use shield
IfTargetIsAttacking
  IfFacingTarget
    tmpargument = [SHIE]
    IfHoldingItemID
      tmpx = selfdex			//Check dexterity to block, 10 is average
      tmpy = rand & 7120
      IfXIsMoreThanY
        tmpargument = LATCHLEFT
        PressLatchButton

//------------------------------------------------------------------------------
End
//------------------------------------------------------------------------------
