//The different states:
//0: Wait for enemy
//1: Get in position
//2: Attack
//3: Casting poison cloud
//Content is the spell cast charge timer

//------------------------------------------------------------------------------
//Dont cling into other friends
IfBumped
  SetTargetToWhoeverBumped
  IfTargetIsOnHatedTeam
    DoNothing
  Else
    tmpx = rand & 511 + targetx - 255
    tmpy = rand & 511 + targety - 255
    tmpdistance = 0 - 500
    tmpturn = targetturnto
    Compass
    ClearWaypoints
    AddWaypoint
  SetTargetToOldTarget

//------------------------------------------------------------------------------
//This manages the melee attack speed
IfUsed
  tmpargument = 40
  SetReloadTime

//------------------------------------------------------------------------------
//Backstab deals extra damage
IfBackstabbed
  SetTargetToWhoeverAttacked
  GetDamageType
  tmpdistance = tmpargument       //Same damage type as the stab
  tmpargument = targetdex         //+1 damage per dex
  TargetDamageSelf

//------------------------------------------------------------------------------
//Leader AI
IfSpawned
  BecomeLeader

//------------------------------------------------------------------------------
//Summoning AI (Tell the wings do the summoning part)
IfStateIs0
  SetTargetToRider	//No need to summon, not in combat
  tmpargument = [WAND]
  OrderTarget
  SetTargetToOldTarget
Else
  SetTargetToRider	//Tell them we are in battle
  tmpargument = [COMB]
  OrderTarget
  SetTargetToOldTarget

//------------------------------------------------------------------------------
//Nasty sounds
IfBored
  tmpargument = 9
  PlaySound

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

  //Boss monster, award xp
  tmpargument = 400
  tmpdistance = EXPQUEST
  GiveExperienceToGoodTeam

  // Spit out text
  tmpargument = 0
  IfTargetIsOnSameTeam
    tmpargument = 1
    IfTargetIsSelf
      tmpargument = 3
  SendMessage

  // Tell the wings to stop flappin'
  SetTargetToRider // The wings are on gripleft, so this works
  PoofTarget
  SetTargetToOldTarget

  // Drop goodies
  tmpargument = selfmoney
  DropMoney
  DropKeys
  DropItems

  // Make the character body
  tmpargument = 45
  SetBumpHeight

  // Stop flying
  tmpargument = 0
  SetFlyHeight

  //Sound
  tmpargument = 1
  PlaySound

  // Tell the wings to stop flappin'
  SetTargetToRider // The wings are on gripleft, so this works
  tmpargument = [DEAD]
  OrderTarget
  SetTargetToOldTarget
  
  //End text (depending on difficulty)
  ClearEndMessage
  tmpx = difficulty
  tmpy = 0
  IfXIsEqualToY
    EnableExport
    BeatModule
    tmpargument = 5
    AddEndMessage
  Else
    //Spawn the portal to the lower levels of the abyss!
    tmpargument = 101
    tmpx = selfspawnx
    tmpy = selfspawny
    tmpdistance = selfz
    SpawnExactCharacterXYZ
  
//------------------------------------------------------------------------------
//What to do if enemies are defeated
IfTargetKilled
  SetTargetToNearestEnemy	//Goto next
    tmpargument = 1
  Else
    tmpargument = 3		//No enemies left
    tmpdistance = 0
    PlayMusic
    tmpargument = 0

  SetState	//Do it

//------------------------------------------------------------------------------
//What to do each round
IfTimeOut

  // Wait for the good guys to barge in...
  IfStateIs0
    tmpx = selfspawnx
    tmpy = selfspawny
    ClearWaypoints
    AddWaypoint
    tmpargument = rand % 30 + 10
    SetTime
    tmpdistance = 128*6
    SetTargetToDistantEnemy
      tmpargument = 1
      SetState				//Combat mode
      tmpargument = rand & 1 + 10
      PlayFullSound				//Growl
      tmpargument = 3			//Tell the players what they have done
      SendMessageNear
    Else
      tmpargument = 40			//Nobody here, wait some more
      SetTime
  Else
    //Growl sometimes
    tmpx = rand & 255
    tmpy = 250
    IfXIsMoreThanY
      tmpargument = rand & 7 + 9
      PlayFullSound

  //Move towards enemy
  IfStateIs1
    Run
    tmpargument = rand & 15 + 20
    SetTime
    tmpx = targetdistance
    tmpy = 290
    IfXIsMoreThanY		//Move closer?
      Run
      tmpx = targetx
      tmpy = targety
      ClearWaypoints
      AddWaypoint
      tmpargument = 40
      SetTime
      IfFacingTarget		//Shall we cast a spell?
        SetTargetToSelf
        tmpx = targetmana
        tmpy = 550
        SetTargetToOldTarget
        IfXIsMoreThanY		//Only if enough mana
          tmpx = rand & 100	//70% chance not to cast a spell this time
          tmpy = 92
          IfXIsMoreThanY       	  //Cast a fireball spell (8% chance)
            tmpargument = 1
            tmpx = selfx
            tmpy = selfy
            tmpdistance = selfz + 70
            SpawnExactParticle
            tmpargument = ACTIONBC	//Cast Spell
            DoAction
            tmpargument = 500	//Costs 2 points of mana
            SetTargetToSelf
            CostTargetMana
            SetTargetToOldTarget
          Else
            tmpx = rand & 100
            tmpy = 97
            IfXIsMoreThanY	//Cast Poison cloud spell (3% chance if last failed)
              tmpargument = ACTIONCC	//Cast Spell
              DoAction
              tmpx = 0
              tmpy = 0
              tmpdistance = GRIPRIGHT
              tmpargument = 5
              SpawnAttachedParticle
              SetTargetToSelf		//Costs 3 points of mana
              tmpargument = 800
              CostTargetMana
              SetTargetToOldTarget
              tmpargument =  3	//Set to cast poison cloud
              SetState
    Else			//Close enough - attack
      IfFacingTarget
        IfTargetIsDefending
          tmpargument = ACTIONCC	//Cast Spell if he uses shield
          DoAction
          tmpx = 0
          tmpy = 0
          tmpdistance = GRIPRIGHT
          tmpargument = 5
          SpawnAttachedParticle

          //Costs 3 points of mana
          SetTargetToSelf
          tmpargument = 800
          CostTargetMana
          SetTargetToOldTarget

          tmpargument =  3	//Set to cast poison cloud
          SetState
        Else
          tmpargument = LATCHRIGHT	//Attack the enemy
          PressLatchButton
      Else
        Walk
        tmpturn = targetturnto
        Compass
        ClearWaypoints
        AddWaypoint
        tmpargument = 5
        SetTime

  //Casting posion cloud
  IfStateIs3
    tmpargument = 10
    SetTime

    //Always look at the enemy while casting spell
    tmpx = selfx
    tmpy = selfy
    tmpturn = targetturnto
    Compass
    ClearWaypoints
    AddWaypoint

    GetContent
    tmpargument = tmpargument +1	//Another round has passed
    SetContent
    tmpx = tmpargument
    tmpy = 15
    IfXIsMoreThanY		//Finished casting poison cloud?
      tmpargument = 0
      SetContent		//Reset poison timer
      SetTargetToNearestEnemy
        Run
        tmpx = targetx
        tmpy = targety
        tmpturn = targetturnto
        tmpdistance = 250
        Compass
        ClearWaypoints		//Charge enemy
        AddWaypoint
        tmpargument = 1
        SetState
      Else
        tmpargument = 0		//He died, lets wait again
        SetState

//------------------------------------------------------------------------------
//Spawn fire around daemon (Fiery shield)
tmpx = selfx
tmpy = selfy
tmpdistance = selfz
tmpargument = 4
SpawnExactParticle

//------------------------------------------------------------------------------
// ZZ> Handle being attacked by blocking or countering or running away
IfAttacked

  //Handle being damaged by weakness
  IfHitVulnerable  	 		//Silver
    SetTargetToSelf
    tmpargument = rand & 255 + 512	//1-3
    tmpargument = 0 - tmpargument
    GiveLifeToTarget
  GetDamageType
  tmpx = tmpargument
  tmpy = DAMAGEHOLY
  IfXIsEqualToY				//Holy
    SetTargetToSelf
    tmpargument = rand & 255 + 512	//1-3
    tmpargument = 0 - tmpargument
    GiveLifeToTarget

  SetTargetToWhoeverAttacked
  IfTargetIsOnHatedTeam
    Run
    tmpargument = 1		//Move toward enemy
    SetState
    tmpargument = rand & 3 + 2
    PlaySound
  Else
    tmpargument = 3		//Friendly fire
    PlaySound
    SetTargetToOldTarget

//------------------------------------------------------------------------------
//Last, check if our target is really a enemy (Just in case)
tmpargument = 0
IfStateIsNot
  IfTargetIsOnHatedTeam
    DoNothing
  Else
    SetTargetToNearestEnemy

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