//------------------------------------------------------------------------------
//Set correct wing colour
IfSpawned
  SetTargetToSelf
  SetTargetToTargetLeftHand	//Target the wing
  tmpargument = 1
  IfArmorIs
    ChangeTargetArmor
  tmpargument = 2
  IfArmorIs
    ChangeTargetArmor
  tmpargument = 3
  IfArmorIs
    ChangeTargetArmor
    SetTargetToSelf
    tmpargument = 1280
    GiveLifeToTarget		//5 bonus hp

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

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

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

  //Guard in a circle
  IfStateIs0
    tmpargument = rand & 31 + 30
    SetTime
    SetTargetToWideEnemy	//Enemy found, target him
      tmpargument = 1 
      SetState 		//and charge
    Else			//Circle around
      tmpx = rand & 511 + selfx - 256
      tmpy = rand & 511 + selfy - 256
      ClearWaypoints
      AddWaypoint		       

  //Move towards enemy
  IfStateIs1
    tmpx = targetdistance
    tmpy = 128*3
    IfXIsLessThanY
      tmpargument = 2
      SetState		//Melee
    Else				//Move towards enemy in a
      tmpx = rand & 1023 + targetx - 511	//random direction pattern
      tmpy = rand & 1023 + targety - 511
      tmpturn = targetturnto
      tmpdistance = rand % 1000 + 500
      Compass
      ClearWaypoints
      AddWaypoint
      tmpargument = 50
      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 = 128*3
    IfXIsMoreThanY    
      tmpargument = 1 		//Retry attack position
      SetState    
      tmpargument = 0
      SetTime

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

  //Dont flock
  IfTargetIsOnHatedTeam
    tmpx = targetdistance
    tmpy = 128
    IfXIsMoreThanY
      SetOldTarget			//Remember enemy
      SetTargetToNearestFriend
        tmpx = targetdistance
        tmpy = 80
        IfXIsLessThanY			//Too close to friend?
          tmpx = targetx
          tmpy = targety
          tmpdistance = 300
          tmpturn = targetturnto + 32768
          Compass
          ClearWaypoints
          AddWaypoint
      SetTargetToOldTarget		//Set target back to enemy

  //Fly up or down depending on opponent altitude
  tmpargument = 20
  IfTargetIsOnHatedTeam
    tmpx = targetz
    tmpy = 20
    IfXIsMoreThanY
      tmpargument = targetaltitude + 20
  SetFlyHeight

//------------------------------------------------------------------------------
//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 blud
  tmpargument = 1
  tmpdistance = rand % 50 + selfz
  tmpx = selfx
  tmpy = selfy
  tmpargument = 0		//Fire mephit
  IfArmorIs
    tmpargument = 1		//Red blud
  Else
    tmpargument = 1		//Ice mephit
    IfArmorIs
      tmpargument = 3		//Blue blud
    Else
      tmpargument = 2		//Moss mephit
      IfArmorIs
        tmpargument = 5		//Green blud
      Else
        tmpargument = 3		//death mephit also has Blue blud
  SpawnExactParticle
  SpawnExactParticle
  SpawnExactParticle
  SpawnExactParticle
  SpawnExactParticle

//------------------------------------------------------------------------------
//Slash and bite (Okay forget the biting part)
IfBumped
  SetTargetToWhoeverBumped
  
  //Fly over obstacles
  IfTargetIsAPlatform
    tmpargument = selfz + 80
    SetFlyHeight
    tmpargument = 50
    SetTime
  Else
    IfTargetIsAlive
      DoNothing
    Else
      tmpargument = selfz + 80
      SetFlyHeight
      tmpargument = 50
      SetTime      

  //Attack or scuttle
  IfTargetIsOnHatedTeam
    IfFacingTarget
      IfTargetIsAlive
        tmpargument = LATCHRIGHT
        PressLatchButton
  Else
    tmpx = rand & 511 + targetx - 256
    tmpy = rand & 511 + targety - 256
    ClearWaypoints
    AddWaypoint
    SetTargetToOldTarget	//Oops, friendly xD

//------------------------------------------------------------------------------
// 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 = 20
    tmpdistance = EXPKILL
    GiveExperienceToTarget

  // Drop goodies
  tmpargument = selfmoney
  DropMoney

  // Make the character body
  tmpargument = 45
  SetBumpHeight

  // Stop flying
  tmpargument = 0
  SetFlyHeight

  //Sound
  tmpargument = 1
  PlaySound

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