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

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

  //Turn to gass to escape?
  tmpx = selflife
  tmpy = 10*256
  IfXIsLessThanY	//Turn to gass if less than 10 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 % 60 + 30
      SetTime
      tmpx = selfspawnx
      tmpy = selfspawny
      ClearWaypoints
      AddWaypoint

  //Combat mode
  IfStateIs1
    tmpargument = 20
    SetTime
    
    //Attack?
    IfTargetIsOnHatedTeam
      tmpx = targetdistance
      tmpy = 160
      IfXIsLessThanY			//Close enough?
        IfFacingTarget			//Are we looking at the enemy?
          //If the target defends, either wait or find new target
          IfTargetIsFacingSelf
            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
              tmpargument = LATCHRIGHT
              PressLatchButton
          Else				//Attack the enemy
            tmpargument = LATCHRIGHT
            PressLatchButton
    
    //Try to find new enemy
    Else
      tmpdistance = 128*10
      SetTargetToDistantEnemy
        DoNothing
      Else
        tmpargument = 0     //return to spawn
        SetState
    
    //Charge    
    Run
    tmpx = targetx
    tmpy = targety
    tmpdistance = 800
    tmpturn = targetturnto
    Compass
    ClearWaypoints
    AddWaypoint
    
    //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
      tmpargument = selfcontent -1
      SetContent			//Reduce cooldown timer

    //Combat theme
    tmpdistance = 0
    tmpargument = 20
    PlayMusic

  //Feared
  IfStateIs2
    Run
    tmpx = targetx
    tmpy = targety
    tmpdistance = 1200
    Compass
    tmpturn = targetturnfrom	//Run away
    ClearWaypoints
    AddWaypoint
    tmpargument = 400   //8 seconds
    SetTime
    tmpargument = 0
    SetState

  //Gass cloud
  IfStateIs3
    tmpargument = 125
    SetTime
    tmpx = selflife
    tmpy = 256*25				//About 25 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

    tmpdistance = 128*5
    SetTargetToDistantEnemy		//Laugh and taunt
      tmpargument = rand & 1 + 6
      PlaySound

//------------------------------------------------------------------------------
//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
    tmpx = 2			//Only if idle of in combat mode
    tmpy = selfstate
    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
    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
      SetOldTarget
      SetTargetToSelf
      tmpargument = rand % 1500 + 2000
      tmpdistance = DAMAGEFIRE
      DamageTarget
      SetTargetToOldTarget
      tmpargument = 4     //Play feared ouch sound

    GetDamageType
    tmpx = tmpargument
    tmpy = DAMAGEHOLY
    IfXIsEqualToY
      tmpargument = 50
      tmpdistance = EXPREVENGE	//Give bonus xp
      GiveExperienceToTarget
      SetOldTarget
      SetTargetToSelf		//Holy damage is instant kill
      KillTarget
      SetTargetToOldTarget
      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 = 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

  //Award xp and open final door. Play victory tune.
  tmpargument = passage
  OpenPassage
  tmpargument = 75
  tmpdistance = EXPQUEST
  GiveExperienceToGoodTeam
  tmpargument = 14
  PlayMusic
  
  //Add the end text
  ClearEndMessage
  tmpargument = 8
  AddEndMessage
  tmpargument = 9
  AddEndMessage

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