//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 + 50
  SetReloadTime

//------------------------------------------------------------------------------
//Always have 1 vampire leader
IfSpawned
  BecomeLeader
  MakeCrushValid
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

//Vampire is staked in spiked door (Lured by player)
IfCrushed
  SetTargetToSelf
  KillTarget
  tmpargument = 10
  PlayFullSound
  tmpargument = 8		//Tell em
  SendMessage
  tmpargument = 50		//A nifty bonus xp for this solution
  tmpdistance = EXPSECRET
  GiveExperienceToGoodTeam

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

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

  //Waiting for the player
  IfStateIs0
    Walk
    SetTargetToWideEnemy		//Found a enemy, enter combat mode
      tmpargument = rand & 20 + 10
      SetTime
      tmpargument = 1
      SetState
      tmpx = targetx
      tmpy = targety
      tmpdistance = 200
      tmpturn = targetturnto
      Compass
      ClearWaypoints
      AddWaypoint
      tmpargument = rand & 1 + 6	//Laugh
      PlaySound
      tmpargument = 7
      SendMessageNear
    Else  				//No enemies, wander around
      tmpargument = rand & 61 + 30
      SetTime
      tmpx = selfspawnx
      tmpy = selfspawny
      ClearWaypoints
      AddWaypoint

  //Combat mode
  IfStateIs1
    Run
    tmpargument = 20
    SetTime
    tmpargument = 1
    SetState
    tmpx = targetx
    tmpy = targety
    tmpdistance = 1600
    tmpturn = targetturnto
    Compass
    ClearWaypoints
    AddWaypoint    
    tmpx = targetdistance
    tmpy = 160
    IfXIsLessThanY			//Close enough?
      IfFacingTarget			//Are we looking at the enemy?
        //If the target defends, either wait or find new target
        tmpx = targetturn
        tmpy = selfturn
        IfXIsLessThanY		//Target is facing self
          IfTargetIsDefending
            SetTargetToNearestEnemy
            IfAttacked
              SetTargetToWhoeverAttacked
            Sneak
            tmpx = rand & 511 + selfx - 256
            tmpy = rand & 511 + selfy - 256
            tmpdistance = 250
            tmpturn = targetturnto
            Compass
            ClearWaypoints
            AddWaypoint   
          Else				//Attack the enemy
            IfTargetIsOnHatedTeam	//Only if he is a enemy
              tmpargument = LATCHRIGHT
              PressLatchButton    
        Else				//Attack the enemy
          IfTargetIsOnHatedTeam		//Only if he is a enemy
            tmpargument = LATCHRIGHT
            PressLatchButton
    Else
    //Do spells and special abillities
    tmpargument = 0
    IfContentIs
      IfFacingTarget
        tmpx = selfmana
        tmpy = 511
        IfXIsMoreThanY
          tmpx = rand & 1
          tmpy = 0
          IfXIsEqualToY
            tmpargument = 6	//Blue attack
          Else
            tmpargument = 3	//Red attack
          tmpx = selfx
          tmpy = selfy
          tmpdistance = selfz + 70
          SpawnExactParticle
          tmpargument = ACTIONBC	//Cast Spell
          DoAction
            tmpargument = 512
            SetTargetToSelf
            CostTargetMana		//Pay 2 mana
            SetTargetToOldTarget
            tmpargument = rand & 4 + 3		//Cooldown time
            SetContent
    Else
      GetContent
      tmpargument = tmpargument -1
      SetContent			//Reduce cooldown timer

    //Combat theme
    tmpdistance = 0
    tmpargument = 16
    PlayMusic


  //Feared
  IfStateIs2
    Run
    tmpx = targetx
    tmpy = targety
    tmpdistance = 1200
    Compass
    tmpturn = targetturnaway	//Run away
    ClearWaypoints
    AddWaypoint   
    tmpargument = 450
    SetTime
    tmpargument = 0
    SetState

  //Gass cloud
  IfStateIs3     
    tmpargument = 125
    SetTime
    tmpx = selflife
    tmpy = 7240				//About 40 life should be enough
    IfXIsMoreThanY			//Do we have enough life to return to combat?
      tmpargument = 0			//If so, become normal again  
      ChangeArmor			//Become visible
      SetState  			//Return to leader waiting mode
      SetTime
      SetTargetToRider
        tmpargument = [HIDE]
        OrderTarget	//Show the wings
      tmpargument = 5			//Sound effect
      PlaySound
    Else				//Nope, still too hurt
      Run
      SetTargetToNearestEnemy
      tmpx = selfspawnx
      tmpy = selfspawny
      ClearWaypoints
      AddWaypoint  

    SetTargetToNearestEnemy		//Laugh and taunt
      tmpx = targetdistance
      tmpy = 350
      IfXIsLessThanY
        tmpargument = rand & 1 + 6
        PlaySound

//Don't just stand still
IfAtLastWaypoint
  tmpargument = 3	//Don't do this if gass cloud
  IfStateIsNot
    tmpargument = 0
    SetState

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


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

  //Engage the attacker
  SetTargetToWhoeverAttacked
  IfTargetIsOnHatedTeam
    GetState
    tmpx = 2			//Only if idle of in combat mode
    tmpy = tmpargument
    IfXIsLessThanY
      tmpx = targetx		//Else - counter attack
      tmpy = targety
      tmpdistance = 200
      tmpturn = targetturnto
      Compass
      ClearWaypoints
      AddWaypoint
      tmpargument = 30
      SetTime
  Else				//Whoops, friendly fire
    SetTargetToNearestEnemy
  
  //Cover in fear if damaged by holy
  GetDamageType
  tmpx = tmpargument
  tmpy = DAMAGEHOLY
  IfXIsEqualToY
    tmpargument = 1
    SendMessageNear
    tmpargument = 3
    IfStateIsNot	//Dont do this if in gass form
      tmpargument = 2	//Become Feared
      SetState
    tmpargument = 4     //Play feared ouch sound
  Else
    tmpargument = rand & 2 + 1	//Play normal ouch sound

  //Silver weapons are super effective
  IfHitVulnerable
    SetTargetToSelf
    tmpargument = rand & 1500 + 1000
    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
  IfStateIs3
    GetDamageType
    tmpx = tmpargument
    tmpy = DAMAGEFIRE
    IfXIsEqualToY
      SetTargetToSelf
      tmpargument = rand & 1500 + 2000
      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
  tmpargument = 0
  IfTargetIsOnSameTeam
    tmpargument = 2
    IfTargetIsSelf
      tmpargument = 3
  SendMessageNear

  tmpargument = 10	//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 = 65535
  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


  //Award xp and open final door. Play victory tune.
  tmpargument = passage
  OpenPassage
  tmpargument = 75
  tmpdistance = EXPQUEST
  GiveExperienceToGoodTeam
  tmpargument = 14
  PlayMusic

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