//The different states:
//0: Wandering around looking for enemies (And following the leader)
//1: In combat
//2: Feared
//3: Gass form

//------------------------------------------------------------------------------
//How fast can he attack?
IfUsed
  tmpargument = rand % 50 + 25
  SetReloadTime

//------------------------------------------------------------------------------
//Always have 1 vampire leader
IfSpawned
  BecomeLeader
  tmpargument = STATEWANDER
  SetState
IfLeaderKilled
  BecomeLeader

//------------------------------------------------------------------------------
//Gass cloud effect
tmpargument = 1
IfArmorIs
  GetContent
  tmpx = tmpargument
  tmpy = 7
  IfXIsMoreThanY
    tmpargument = 5
    tmpx = selfx
    tmpy = selfy
    tmpdistance = rand & 100 + selfz
    SpawnExactParticle
    tmpargument = 0
    SetContent
  Else
    GetContent
    tmpargument = tmpargument + 1
    SetContent

//------------------------------------------------------------------------------
// Let the character walk around
IfTimeOut

  //Turn to gass to escape?
  tmpx = selflife
  tmpy = 2048                   //8 or less life
  IfXIsLessThanY	              //Turn to gass if less than 8 life left
    tmpargument = STATEPARRY
    IfStateIsNot	              //Dont become gass if already gas
      tmpargument = 1
      ChangeArmor
      tmpargument = STATEPARRY	//Gass mode
      SetState
      tmpargument = 4	          //Sound effect and message
      SendMessageNear
      tmpargument = 5
      PlaySound
      SetTargetToRider
        tmpargument = [HIDE]
        OrderTarget	            //Hide the wings

  //Wandering around following lead vampire
  IfStateIsWander
    Walk
    SetTargetToWideEnemy		//Found a enemy, enter combat mode
      tmpargument = rand % 20 + 10
      SetTime
      tmpargument = STATECOMBAT
      SetState
      tmpx = targetx
      tmpy = targety
      tmpdistance = 200
      tmpturn = targetturnto
      Compass
      ClearWaypoints
      AddWaypoint
    Else  				//No enemies, wander around
      tmpargument = rand % 75 + 50
      SetTime
      tmpx = rand & 1023 + selfspawnx - 511
      tmpy = rand & 1023 + selfspawny - 511
      Compass
      ClearWaypoints
      AddWaypoint

  //Combat mode
  IfStateIsCombat
    IfTargetIsOnHatedTeam
      DoNothing
    Else
      SetTargetToWideEnemy
        DoNothing
      Else
        tmpargument = STATEWANDER
        SetState
    Run
    tmpargument = 20
    SetTime
    
    //Move it!
    tmpx = targetx
    tmpy = targety
    tmpdistance = 200
    tmpturn = targetturnto
    Compass
    ClearWaypoints
    AddWaypoint
    tmpx = targetdistance
    tmpy = 110
    IfXIsLessThanY			//Close enough?
      IfFacingTarget			//Are we looking at the enemy?

      	//If the target defends, either wait or find new target
        IfTargetIsDefending
          IfAttacked
            SetTargetToWhoeverAttacked
          Else
            SetTargetToNearestEnemy
          tmpx = selfx
          tmpy = selfy
          tmpdistance = 250
          tmpturn = targetturnto
          Compass
          ClearWaypoints
          AddWaypoint
          tmpargument = 5
          SetTime                   //Readied attack time

        Else				//Attack the enemy
          tmpargument = LATCHRIGHT
          PressLatchButton

  //Feared
  IfStateIsRetreat
    Run
    tmpx = rand % 2047 - 1023 + targetx
    tmpy = rand % 2047 - 1023 + targety
    tmpdistance = 600
    tmpturn = rand
    Compass
    ClearWaypoints
    AddWaypoint
    tmpargument = 450
    SetTime
    tmpargument = STATEWANDER
    SetState

  //Gass cloud
  IfStateIsParry
    tmpargument = 450
    SetTime
    tmpx = selflife
    tmpy = 3000				//About 12 life should be enough
    IfXIsMoreThanY			//Do we have enough life to return to combat?
      tmpargument = 0			//If so, become normal again
      ChangeArmor			//Become visible
      tmpargument = STATEWANDER
      SetState  			//Return to leader or wandering mode
      SetTime
      SetTargetToRider
        tmpargument = [HIDE]
        OrderTarget	//Show the wings
      tmpargument = 5			//Sound effect
      PlaySound
    Else				//Nope, still too hurt, hide and heal
      Run
      SetTargetToNearestEnemy
      tmpx = rand % 2047 - 1023 + targetx
      tmpy = rand % 2047 - 1023 + targety
      tmpdistance = 600
      tmpturn = rand		//Run away
      Compass
      ClearWaypoints
      AddWaypoint
      tmpargument = rand % 456 + 356 //Regenerate extra while in gass form
      HealSelf

//------------------------------------------------------------------------------
//What to do if enemy is dead
IfTargetKilled
  tmpargument = STATEPARRY
  IfStateIsNot			//Only do if not gass form
    SetTargetToNearbyEnemy
      tmpargument = STATECOMBAT		//Engage nearby enemy
    Else
      tmpargument = STATEWANDER		//Return to leader
    SetState

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

  //Engage the attacker
  SetTargetToWhoeverAttacked
  IfTargetIsOnHatedTeam
    tmpx = 1
    tmpargument = STATECOMBAT			//Only if idle of in combat mode
    IfStateIs
      tmpx = 0
    Else
      tmpargument = STATEWANDER			//Only if idle of in combat mode
      IfStateIs
        tmpx = 0
    tmpy = 1
    IfXIsLessThanY
      tmpx = targetx		        //Else - counter attack
      tmpy = targety
      tmpdistance = 200
      tmpturn = targetturnto
      Compass
      ClearWaypoints
      AddWaypoint
      tmpargument = 30
      SetTime
  Else				//Whoops, friendly fire
    SetTargetToOldTarget

  //Cover in fear if damaged by holy
  GetDamageType
  tmpx = tmpargument
  tmpy = DAMAGEHOLY
  IfXIsEqualToY
    tmpargument = 1
    SendMessageNear
    tmpargument = STATEPARRY
    IfStateIsNot	//Dont do this if in gass form
      tmpargument = STATERETREAT	//Become Feared
      SetState
    tmpargument = 4     //Play feared ouch sound
  Else
    tmpargument = rand & 1 + 1	//Play normal ouch sound

  //Silver weapons are super effective
  IfHitVulnerable
    SetTargetToSelf
    tmpargument = rand % 1280 + 1024  //5-9 damage
    tmpdistance = DAMAGEFIRE
    DamageTarget
    SetTargetToOldTarget
    tmpargument = 4    //Play feared ouch sound

  //In gass form the vampire has weakness to fire and holy damage is instant kill
  IfStateIsParry
    GetDamageType
    tmpx = tmpargument
    tmpy = DAMAGEFIRE
    IfXIsEqualToY
      SetTargetToSelf
      tmpargument = rand % 1280 + 1792      //5-12 damage
      tmpdistance = DAMAGEFIRE
      DamageTarget
      SetTargetToOldTarget
      tmpargument = 4     //Play feared ouch sound

    GetDamageType
    tmpx = tmpargument
    tmpy = DAMAGEHOLY
    IfXIsEqualToY
      SetTargetToWhoeverAttacked
      tmpargument = 30
      tmpdistance = EXPREVENGE	//Give bonus xp
      GiveExperienceToTarget
      SetTargetToSelf		//Holy damage is instant kill
      KillTarget
      tmpargument = 5
      SendMessageNear

  PlaySound


//------------------------------------------------------------------------------
//Always attack enemies who bump
IfBumped
  SetTargetToWhoeverBumped
  IfTargetIsOnHatedTeam
    DoNothing
  Else
    SetTargetToOldTarget

//------------------------------------------------------------------------------
//Handle death by sending a message and other stuff
IfKilled
  SetTargetToWhoeverAttacked
  tmpargument = 0
  IfTargetIsOnSameTeam
    tmpargument = 2
    IfTargetIsSelf
      tmpargument = 3
  SendMessageNear

  tmpargument = 3	//Play death sound
  PlaySound

  //If gass form, become normal
  tmpargument = 1
  IfArmorIs
    tmpargument = 0
    ChangeArmor
    SetTargetToRider
    tmpargument = [HIDE]	//Show the wings
    OrderTarget

  // Drop goodies
  tmpargument = selfmoney
  DropMoney
  DropKeys

  // Make the character body
  tmpargument = 45
  SetBumpHeight

  // Stop flying
  tmpargument = 0
  SetFlyHeight

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

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