//Betray the suckers!!
IfOrdered
  tmpx = selforder
  tmpy = [FREE]
  IfXIsEqualToY
    SetTargetToNearestEnemy
    tmpargument = 3
    IfStateIsNot
      JoinEvilTeam
      tmpargument = 8
      PlayFullSound
      SendMessage
      tmpargument = 3
      SetState
      tmpargument = 70
      tmpdistance = EXPDARE
      GiveExperienceToGoodTeam

//Main AI loop
IfTimeOut
  //This makes the betrayer teleport behind enemy defences
  IfTargetIsDefending
    IfStateIs3
      //Do we have enough mana?
      tmpx = selfmana
      tmpy = 700
      IfXIsMoreThanY
        tmpx = targetx
        tmpy = targety
        ClearWaypoints
        AddWaypoint
        tmpdistance = 150
        tmpturn = targetturnto
        Compass
        Teleport
          tmpargument = 3
          tmpdistance = SPAWNORIGIN
          SpawnParticle
          SpawnParticle
          SpawnParticle
          tmpargument = 10		//Spelly effects
          PlaySound
          tmpargument = 700		//Cost some mana
          SetTargetToSelf
          CostTargetMana
          SetTargetToOldTarget

        // Is the enemy facing the character?
        IfTargetIsFacingSelf
          tmpx = targetx
          tmpy = targety
          Teleport
            tmpargument = 3
            tmpdistance = SPAWNORIGIN
            SpawnParticle
            SpawnParticle
            SpawnParticle
            tmpargument = 10		//Spelly effects
            PlaySound
            tmpargument = 700		//Cost some mana
            SetTargetToSelf
            CostTargetMana
            SetTargetToOldTarget

  //Lure the player into releasing him
  tmpargument = 3
  IfStateIsNot
    tmpargument = 250
    SetTime
    tmpdistance = 128*7
    SetTargetToDistantEnemy                //SetTargetToWhoeverIsInPassage
      IfTargetIsAPlayer
        IfStateIs0
          tmpargument = 7
          PlayFullSound
          SendMessage
          tmpargument = 1
          SetState
        Else
          IfStateIs1
            tmpargument = rand & 2 + 4
            PlayFullSound
            SendMessage
            tmpargument = 0
            SetState
    Else
      IfStateIs1
        tmpargument = 9
        PlayFullSound
        SendMessage
        tmpargument = 0
        SetState

    SetTurnModeToWatchTarget

  //Betrayal mode
  Else
    tmpargument = rand % 15 + 35
    SetTime
    SetTargetToNearestEnemy
      tmpx = targetx
      tmpy = targety
      tmpdistance = 700
      tmpturn = targetturnto
      Compass
      ClearWaypoints
      AddWaypoint

      //Attack!
      IfHoldingMeleeWeapon
        IfFacingTarget
          tmpx = targetdistance
          tmpy = 200
          IfXIsLessThanY
            PressLatchButton
      Else
        tmpargument = LATCHPACKRIGHT		//uh oh, not holding a weapon
        PressLatchButton
    Else
      GoPoof					//Player lost the battle, dissapear
      tmpargument = 3
      SendMessage
      tmpdistance = SPAWNORIGIN
      SpawnParticle
      SpawnParticle
      SpawnParticle
      tmpargument = 10		//Spelly effects
      PlaySound

//------------------------------------------------------------------------------
// ZZ> Handle death by sending a message and other stuff
IfKilled
  tmpargument = 1
  PlaySound			//death cry

  tmpargument = 0
  IfTargetIsOnSameTeam
    tmpargument = 1
    IfTargetIsSelf
      tmpargument = 2
  SendMessage

  // Drop goodies
  tmpargument = selfmoney
  DropItems
  DropMoney
  DropKeys

  //Create a reward chest =)
  tmpx = selfspawnx
  tmpy = selfspawny
  tmpdistance = 700
  tmpargument = 86
  SpawnExactCharacterXYZ

  tmpargument = 2		//Randomize the chest type
  tmpx = rand % 100		//55% chance for artifact
  tmpy = 55
  IfXIsMoreThanY		//35% for armor chest
    tmpargument = 1
    tmpy = 90
    IfXIsMoreThanY		//10% for mere gold & treasure
      tmpargument = 0
  SetChildState

  // Make the character body
  tmpargument = 45
  SetBumpHeight

//Spawn some fancy sparklies
tmpargument = 2
tmpx = selfx
tmpy = selfy
tmpdistance = selfz
SpawnExactParticle

//------------------------------------------------------------------------------
//Say ouch
IfAttacked
  SetTargetToWhoeverAttacked
  tmpargument = rand & 1 + 1
  PlaySound

//------------------------------------------------------------------------------
//Released out of the cage somehow and attacked or bumped (disenchant)
tmpargument = 3
IfStateIsNot
  IfBumped
    tmpargument = [FREE]
    OrderTarget

  IfAttacked
    SetTargetToSelf
    tmpargument = [FREE]
    OrderTarget

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