//The different states:
//0: Wait for enemy
//1: Get in position
//2: Attack
//3: Casting poison cloud


//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
  // Spit out text
  tmpargument = 0
  IfTargetIsOnSameTeam
    tmpargument = 1
    IfTargetIsSelf
      tmpargument =3
  SendMessage

  // Drop goodies
  tmpargument = 65535
  DropMoney
  DropKeys

  // 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

  //Award quest xp
  tmpargument = 200
  tmpdistance = EXPQUEST
  GiveExperienceToGoodTeam

  tmpargument = [BEAT]
  AddIDSZ
  EnableExport
  BeatModule

  //Victory tune
  tmpargument = 14
  tmpdistance = 0
  PlayMusic

  //Drop random magic loot (Note: Cant be burnable)
  tmpx = rand & 4
  tmpy = 0
  IfXIsEqualToY
    tmpargument = 65	//Rune of Restoration

  tmpy = 1
  IfXIsEqualToY
    tmpargument = 66	//Shield of Healing

  tmpy = 2
  IfXIsEqualToY
    tmpargument = 69	//Elixir of Life

  tmpy = 3
  IfXIsEqualToY
    tmpargument = 67	//Firebow

  tmpy = 4
  IfXIsEqualToY
    tmpargument = 68	//Bracelet of Acrobacy

  tmpx = selfx
  tmpy = selfy
  tmpdistance = selfz + 75
  SpawnExactCharacterXYZ

//--------------------------------------------------------
//What to do if enemies are defeated
IfTargetKilled
  SetTargetToNearestEnemy	//Goto next
    IfTargetIsAlive
      tmpargument = 1
    Else
      tmpargument = 0		//They are all dead
  Else
    tmpargument = 0		//No enemies left

  SetState	//Do it

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

  //Growl sometimes
  tmpx = rand & 30000
  tmpy = 29997
  IfXIsMoreThanY
    tmpargument = rand & 2 + 9
    PlaySound

  // Wait for the good guys to barge in...
  IfStateIs0
    tmpx = selfspawnx
    tmpy = selfspawny
    ClearWaypoints
    AddWaypoint
    tmpargument = 10 + rand & 30
    SetTime
    tmpdistance = 500000
    SetTargetToDistantEnemy
      tmpargument = 1
      SetState				//Combat mode
      tmpargument = rand & 2 + 9
      PlaySound				//Growl
      tmpargument = 3			//Tell the players what they have done
      SendMessageNear
    Else
      tmpargument = 40			//Nobody here, wait some more
      SetTime

  //Move towards enemy
  IfStateIs1
    Run
    tmpargument = 20 + rand & 15
    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
  SetTargetToWhoeverAttacked
  IfTargetIsOnHatedTeam
    Run
    tmpargument = 1		//Move toward enemy
    SetState
    tmpargument = rand % 4 + 2
    PlaySound
  Else
    tmpargument = 3		//Friendly fire
    PlaySound   
    SetTargetToOldTarget 

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

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