//Attack the enemy if he is approaching
IfBumped
  SetTargetToWhoeverBumped
  IfTargetIsOnHatedTeam
    IfFacingTarget
      IfHoldingMeleeWeapon
        PressLatchButton
    Else
        tmpargument = 1
        SetState
  Else
    SetTargetToOldTarget
    
//Anti heal sound
IfUsed
  tmpargument = 6
  PlaySound

//------------------------------------------------------------------------------
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 = 100
    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
    Else
      tmpargument = 0
      SetState
    //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

  // Rush the enemy
  IfStateIs1
    SetTargetToWideEnemy
      DoNothing
    Else
      tmpargument = 0
      SetState
    
    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		

  //Close Combat
  IfStateIs2
    SetTargetToWideEnemy
      DoNothing
    Else
      tmpargument = 0
      SetState
    Walk				//50% speed
    SetTurnModeToVelocity
    tmpargument = rand & 31 + 30
    SetTime
    tmpx = targetdistance
    tmpy = 200
    IfXIsLessThanY			// Close enough to attack
      tmpdistance = 1
      IfHoldingShield               //If the shield is lost, use magic!
        DoNothing
      Else
        tmpx = selfmana
        tmpy = 256
        IfXIsMoreThanY
          tmpdistance = 0
          tmpargument = LATCHLEFT
          PressLatchButton              //Use evil voodoo!!
      
      //Did not use voodoo, attack normally instead
      tmpturn = 0
      IfDistanceIsMoreThanTurn
        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 = 500
    tmpx = targetx
    tmpy = targety
    tmpturn = targetturnto
    Compass
    ClearWaypoints
    AddWaypoint

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

//------------------------------------------------------------------------------
//How to react if damaged
IfAttacked
  SetTargetToWhoeverAttacked		//Always target attackers
  tmpargument = rand & 1 + 2
  PlaySound
  
//Get rid of the shield
IfHoldingShield
  tmpx = selflife > 8
  tmpy = 6
  IfXIsLessThanY
    tmpargument = LATCHALTLEFT
    PressLatchButton
      IfAttacked
        tmpargument = 4
        SendMessage

//------------------------------------------------------------------------------
//Use shield
IfTargetIsAttacking
  IfFacingTarget
    IfHoldingShield
      tmpx = selfdex			//Check dexterity to block, 10 is average
      tmpy = rand & 7120
      IfXIsMoreThanY
        PressLatchButton

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