//Set correct wing colour
IfSpawned
  SetTargetToRider
  tmpargument = 1
  IfArmorIs
    ChangeTargetArmor
  tmpargument = 2
  IfArmorIs
    ChangeTargetArmor
  tmpargument = 3
  IfArmorIs
    ChangeTargetArmor
    SetTargetToSelf
    tmpargument = 1024
    GiveLifeToTarget		//2 bonus hp

//------------------------------------------------------------------------------
//Death Mephit poison attack
IfScoredAHit
  tmpargument = 3
  IfArmorIs
    SetTargetToSelf
    SetOwnerToTarget
    SetTargetToWhoeverWasHit
    EnchantTarget

//------------------------------------------------------------------------------
// ZZ> Handle death by sending a message and other stuff
IfKilled
  // Spit out text
  tmpargument = 0
  IfArmorIs
    DoNothing			//Fire mephit death
  Else
    tmpargument = 1
    IfArmorIs
      tmpargument = 4		//Ice mephit death
    Else
      tmpargument = 2		//moss mephit death
      IfArmorIs
        tmpargument = 5
      Else
        tmpargument = 6		//Death mephit death

  IfTargetIsOnSameTeam
    tmpargument = 1		//Friendly fire
    IfTargetIsSelf
      tmpargument =3		//Sucide!
  SendMessageNear

  //Award bonus xp for death mephits
  tmpargument = 3
  IfArmorIs
    tmpargument = 15
    tmpdistance = EXPKILL
    GiveExperienceToTarget

  // Drop goodies
  tmpargument = 65535
  DropMoney

  // Make the character body
  tmpargument = 45
  SetBumpHeight

  // Stop flying
  tmpargument = 0
  SetFlyHeight

  //Sound
  tmpargument = 1
  PlaySound

  //Spawn different types of blood
  tmpargument = 1
  tmpdistance = rand & 50 + selfz
  tmpx = selfx
  tmpy = selfy
  tmpargument = 0		//Fire mephit
  IfArmorIs
    tmpargument = 1		//Red blood
  Else
    tmpargument = 1		//Ice mephit
    IfArmorIs
      tmpargument = 3		//Blue blood
    Else
      tmpargument = 2		//Moss mephit
      IfArmorIs
        tmpargument = 5		//Green blood
      Else
        tmpargument = 3		//death mephit also has Blue blood
  SpawnExactParticle
  SpawnExactParticle
  SpawnExactParticle
  SpawnExactParticle
  SpawnExactParticle

  // Tell the wings to stop flappin'
  SetTargetToRider // The wings are on gripleft, so this works
  tmpargument = [DEAD]
  OrderTarget

//------------------------------------------------------------------------------
//Enemy target has been defeated
IfTargetKilled
  SetTargetToNearestEnemy
    tmpargument = 1 //Follow
    SetState
  Else
    tmpargument = 0 //Guard
    SetState

//------------------------------------------------------------------------------
IfTimeOut

  //Guard in a circle
  IfStateIs0
    tmpargument = 20
    SetTime    
    SetTargetToWideEnemy	//Enemy found, target him
      tmpargument = 1 
      SetState 		//and charge
    Else			//Circle around
      Run
      SetTurnModeToWatch
      tmpx = selfx
      tmpy = selfy
      tmpturn = selfturn + 6192
      tmpdistance = 200
      Compass
      ClearWaypoints
      AddWaypoint		       

  //Move towards enemy
  IfStateIs1
    SetTurnModeToVelocity
    tmpx = targetdistance
    tmpy = 250
    IfXIsLessThanY
      tmpargument = 2
      SetState		//Melee
      tmpargument = 5
      SetTime
    Else				//Move towards enemy in a
      tmpx = rand & 340 + targetx - 170	//random direction pattern
      tmpy = rand & 340 + targety - 170
      tmpturn = targetturnto
      tmpdistance = 500
      Compass
      ClearWaypoints
      AddWaypoint
      tmpargument = 35
      SetTime
  SetTargetToWideEnemy	//Enemy to far away?
    DoNothing		//Nope
  Else
    tmpargument = 0 		//Yep return to guarding
    SetState    
    tmpargument = 20
    SetTime

  //Combat
  IfStateIs2
    tmpx = targetdistance
    tmpy = 170
    IfXIsLessThanY             // Close enough to swing...
      IfFacingTarget            // If he is facing his enemy
        tmpargument = LATCHRIGHT
        PressLatchButton
        tmpargument = 10
        SetTime

    //Enemy to far away?
    tmpx = targetdistance
    tmpy = 250
    IfXIsMoreThanY    
       tmpargument = 1 		//Retry attack position
       SetState    
       tmpargument = 0
       SetTime

    //Move directly towards target
    tmpx = targetx
    tmpy = targety
    tmpdistance = 300
    tmpturn = targetturnto
    Compass
    ClearWaypoints
    AddWaypoint

  //Dont flock
  SetOldTarget			//Remember enemy
  SetTargetToNearestFriend
  tmpx = targetdistance
  tmpy = 150
  IfXIsLessThanY			//To close to friend?
    tmpx = rand & 500 + targetx - 250
    tmpy = rand & 500 + targety - 250
    SetTargetToOldTarget		//Enemy
    tmpturn = targetturnto
    tmpdistance = rand & 1000
    Compass
    ClearWaypoints
    AddWaypoint			//Move randomly away from him
    tmpargument = rand & 30 + 30
    SetTime
  Else
    SetTargetToOldTarget		//Set target back to enemy

//------------------------------------------------------------------------------
//Handle being attacked by countering
IfAttacked
  SetTargetToWhoeverAttacked
  IfTargetIsOnHatedTeam
    Run
    IfStateIs0		//If not already, engage in combat
      tmpargument = 1
      SetState
    tmpargument = 2
    PlaySound		//Ouch sound
  Else
    SetTargetToOldTarget
  
  //Spawn different types of blood
  tmpargument = 1
  tmpdistance = rand & 50 + selfz
  tmpx = selfx
  tmpy = selfy
  tmpargument = 0		//Fire mephit
  IfArmorIs
    tmpargument = 1		//Red blood
  Else
    tmpargument = 1		//Ice mephit
    IfArmorIs
      tmpargument = 3		//Blue blood
    Else
      tmpargument = 2		//Moss mephit
      IfArmorIs
        tmpargument = 5		//Green blood
      Else
        tmpargument = 3		//death mephit also has Blue blood
  SpawnExactParticle
  SpawnExactParticle
  SpawnExactParticle
  SpawnExactParticle
  SpawnExactParticle

//------------------------------------------------------------------------------
//Slash and bite (Okay forget the biting part)
IfBumped
  SetTargetToWhoeverBumped
  IfTargetIsOnHatedTeam
    IfFacingTarget
      tmpargument = LATCHRIGHT
      PressLatchButton
  Else
    SetTargetToOldTarget	//Oops, friendly xD

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