//Randomize skin
IfSpawned
  tmpargument = rand % 4
  ChangeArmor


  //Moving guard 1
  tmpargument = 1
  IfContentIs
    tmpargument = 2		
    SetState

    tmpx = 2235
    tmpy = 4801
    AddWaypoint

    tmpx = 5245
    tmpy = 4801
    AddWaypoint


  //Moving guard 2
  tmpargument = 2
  IfContentIs
    tmpargument = 2		
    SetState

    tmpx = 2235
    tmpy = 4801
    AddWaypoint

    tmpx = 2235
    tmpy = 6478
    AddWaypoint

  //Sleeping Guard
  tmpargument = 3
  IfContentIs
    tmpargument = 3		
    SetState

  //Moving inner guard 3
  tmpargument = 4
  IfContentIs
    tmpargument = 2		
    SetState

    tmpx = 5320
    tmpy = 6478
    AddWaypoint

    tmpx = 5320
    tmpy = 7479
    AddWaypoint

  //Moving inner guard 4
  tmpargument = 5
  IfContentIs
    tmpargument = 2		
    SetState

    tmpx = 5569
    tmpy = 7491
    AddWaypoint

    tmpx = 5569
    tmpy = 6456
    AddWaypoint

//Someone sounded the alarm
IfOrdered
  tmpx = selforder
  tmpy = [HELP]
  IfXIsEqualToY
    tmpargument = 1
    SetState
    SetTargetToNearestEnemy
    tmpargument = 0
    SetTime

//---------------------------------------------------------
//Better fighting tactics
IfBumped
  SetTargetToWhoeverBumped
  IfTargetIsAMount
    tmpargument = LATCHJUMP
    PressLatchButton
    tmpargument = 1
    SetState
  IfTargetIsOnHatedTeam
    tmpargument = 1
    IfStateIsNot		//Sound the alarm!
      tmpargument = 1
      SetState
      tmpargument = [HELP]
      IssueOrder
      tmpargument = 6
      PlayFullSound
      SendMessageNear
    IfFacingTarget
      tmpargument = LATCHRIGHT
      PressLatchButton
    Else
      tmpargument = 1
      SetState
      SetTargetToNearestEnemy
      tmpargument = 0
      SetTime
  Else
    SetTargetToOldTarget


//------------------------------------------------------------------------------
//Use shield
IfTargetIsAttacking
  IfTargetIsOnHatedTeam   
    IfFacingTarget
      IfHoldingShield
        tmpx = selfdex
        tmpy = rand & targetdex*2
        IfXIsMoreThanY			//Check dexterity skill to block
          tmpargument = LATCHLEFT
          PressLatchButton

//------------------------------------------------------------------------------
//Pick next enemy
IfTargetKilled
  SetTargetToNearestEnemy
    tmpargument = 1
    SetState
  Else
    tmpargument = 0
    SetState


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


  // Drop goodies
  tmpargument = 65535
  DropMoney
  DropKeys
  DropItems

  // Make the character body
  tmpargument = 15
  SetBumpHeight

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

  //Standing Guard
  IfStateIs0
    SetTurnModeToVelocity
    tmpargument = rand & 25 + 20
    SetTime
    SetTargetToWideEnemy
      IfFacingTarget
        tmpx = targetdistance
        tmpy = rand & 250 + 650
        IfXIsLessThanY			//Close enough to see?
          tmpargument = 1
          SetState
          tmpargument = [HELP]
          IssueOrder                      //Sound the alarm!
          tmpargument = 6
          PlayFullSound
          SendMessageNear
    Else
      Walk
      tmpx = selfspawnx   
      tmpy = selfspawny    
      ClearWaypoints			    
      AddWaypoint				 


  //Combat mode
  IfStateIs1
    tmpdistance = 128000
    SetTargetToDistantEnemy
    IfTargetIsOnHatedTeam
      DoNothing
    Else
      SetTargetToNearestEnemy
    Run
    tmpargument = rand & 21 + 15
    SetTime

    //Draw weapons if unarmed
    IfUnarmed
      tmpargument = LATCHPACKRIGHT
      PressLatchButton

    //Melee combat
    IfHoldingMeleeWeapon
      tmpdistance = 200
      IfFacingTarget
        tmpx = targetdistance			//Close enough?
        tmpy = 250
        IfXIsLessThanY
          IfTargetIsOnHatedTeam
            tmpargument = LATCHRIGHT		//Attack!
            PressLatchButton
        Else
          tmpdistance = 1500			//Charge attack
      IfHoldingShield
        DoNothing
      Else
        tmpargument = LATCHPACKLEFT
        PressLatchButton

      tmpx = targetx
      tmpy = targety
      tmpturn = targetturnto
      Compass
      ClearWaypoints
      AddWaypoint

    //Ranged combat
    IfHoldingRangedWeapon
      IfFacingTarget			//Dont shoot if not facing or the target is facing self with shield
        tmpx = targetturn
        tmpy = selfturn
        IfXIsLessThanY
          IfTargetIsDefending
            DoNothing

          //Shoot!
          Else
            IfTargetIsOnHatedTeam
              SetTargetToNearestFriend		//Try not to hit friends
                IfFacingTarget
                  tmpx = targetdistance
                  tmpy = 450
                  IfXIsMoreThanY		//Friend is far away
                    SetTargetToOldTarget
                    tmpargument = LATCHRIGHT
                    PressLatchButton
                  Else
                    SetTargetToOldTarget
                    tmpy = targetdistance
                    IfXIsMoreThanY		//Enemy is closer than friend
                      tmpargument = LATCHRIGHT
                      PressLatchButton
                Else
                  SetTargetToOldTarget		//Not facing friend
                  tmpargument = LATCHRIGHT
                  PressLatchButton
              Else
                SetTargetToOldTarget		//No friends nearby
                tmpargument = LATCHRIGHT
                PressLatchButton

        //Shoot!
        Else
          IfTargetIsOnHatedTeam
            SetTargetToNearestFriend		//Try not to hit friends
              IfFacingTarget
                tmpx = targetdistance
                tmpy = 450
                IfXIsMoreThanY		//Friend is far away
                  SetTargetToOldTarget
                  tmpargument = LATCHRIGHT
                  PressLatchButton
                Else
                  SetTargetToOldTarget
                  tmpy = targetdistance
                  IfXIsMoreThanY		//Enemy is closer than friend
                    tmpargument = LATCHRIGHT
                    PressLatchButton
              Else
                SetTargetToOldTarget		//Not facing friend
                tmpargument = LATCHRIGHT
                PressLatchButton
            Else
              SetTargetToOldTarget		//No friends nearby
              tmpargument = LATCHRIGHT
              PressLatchButton
 
      SetTargetToNearestEnemy
      SetTurnModeToWatchTarget
      tmpx = targetdistance
      tmpy = 350
      IfXIsLessThanY			//Keep distance
        tmpdistance = 0 - 400
        tmpx = rand & 350 + targetx - 175
        tmpy = rand & 350 + targety - 175
      Else
        tmpx = targetdistance
        tmpy = 650
        IfXIsMoreThanY			//Target too far away?
          tmpdistance = 0 - 400
          tmpx = targetx
          tmpy = targety
        Else
          tmpx = selfx			//No, this position is fine
          tmpy = selfy

      ClearWaypoints
      AddWaypoint

      //Try to avoid hitting friends
      SetTargetToNearestFriend
        IfFacingTarget
          tmpx = targetdistance
          tmpy = 300
          IfXIsLessThanY
            tmpx = rand & 512 + selfx - 256
            tmpy = rand & 512 + selfy - 256
            ClearWaypoints
            AddWaypoint
        SetTargetToOldTarget

      //Check for ammo if we need a reload
      SetOldTarget
      SetTargetToSelf
      SetTargetToTargetRightHand
      tmpx = targetammo
      tmpy = 10
      IfXIsLessThanY		//Yep
        tmpargument = 9
        PlaySound
        tmpargument = 7
        SendMessageNear
        tmpargument = [LBOW]
        SetTargetToSelf
        RestockTargetAmmoIDAll 
      SetTargetToOldTarget


  //Moving Guard
  IfStateIs2
    Sneak
    SetTurnModeToVelocity
    tmpargument = 10
    SetTime

    //Watch out for enemies
    SetTargetToWideEnemy
      IfFacingTarget
        tmpx = targetdistance
        tmpy = rand & 250 + 470
        IfXIsLessThanY			//Close enough to see?
          tmpargument = 1
          SetState
          SetTime
          tmpargument = [HELP]
          IssueOrder                      //Sound the alarm!
          tmpargument = 6
          PlayFullSound
          SendMessageNear

  //Sleepy guard
  IfStateIs3
    tmpargument = rand & 100 + 175
    SetTime
    tmpx = selfx
    tmpy = selfy
    tmpargument = 2
    tmpdistance = selfz
    SpawnExactParticle
    SpawnExactParticle
    tmpargument = 8
    PlaySound


//------------------------------------------------------------------------------
// ZZ> Handle being attacked by blocking or countering or running away
IfAttacked
  SetTargetToWhoeverAttacked
  IfTargetIsOnHatedTeam
    IfHitFromBehind
      tmpargument = 1
      IfStateIsNot			//Only if not in combat already
        tmpargument = [DISA]
        IfTargetHasSkillID                   //Can the target backstab?
          tmpargument = 5
          SendMessageNear
          tmpargument = 15
          tmpdistance = EXPMURDER
          GiveExperienceToTarget            //Award some xp for backstabbing
          SetTargetToSelf
          KillTarget                        //Killed!
  Else
    IfTargetIsOnSameTeam
      tmpargument = 1
      PlaySound				//Hey! careful there 
    SetTargetToOldTarget

IfAttacked
  SetTargetToWhoeverAttacked
  IfTargetIsOnHatedTeam
    tmpargument = 1
    IfStateIsNot
      tmpargument = [HELP]
      IssueOrder                      //Sound the alarm!
      tmpargument = 1
      SetState
      SetTime
      tmpargument = 6
      PlayFullSound
      SendMessageNear
    tmpargument = 4		//Ouch!
    PlaySound
  Else
    SetTargetToOldTarget

//Stand still few seconds before turning to start again
IfAtLastWaypoint
  IfStateIs2
    tmpargument = rand & 100 + 175
    SetTime
    Stop

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




