// Show the character's location
tmpx = selfx
tmpy = selfy
tmpargument = BLUE
ShowBlipXY

IfSpawned
  tmpargument = 2
  ClosePassage

//------------------------------------------------------------------------------
// ZZ> Jump over corpses
IfBumped
  SetTargetToWhoeverBumped
  IfTargetIsAlive
    tmpargument = [CHES]
    IfTargetHasID
      tmpargument = LATCHJUMP
      PressLatchButton
  Else
    tmpargument = LATCHJUMP
    PressLatchButton
  SetTargetToOldTarget

//------------------------------------------------------------------------------
//Wait for a rouge to bump
IfStateIs0
  IfBumped
    SetTargetToWhoeverBumped
    tmpargument = [ROGU]		//Only rogues
    IfTargetHasID
      AddStat
      tmpargument = 1
      SetState
      tmpargument = 3
      SendMessageNear
      tmpargument = 6
      PlaySound				//Ill watch ya back!
      tmpargument = 2
      OpenPassage				//Open start door
    Else
      tmpargument = 5
      SendMessageNear
      tmpargument = 2
      SetState
      tmpargument = 50
      SetTime
IfStateIs3
  IfBumped
    SetTargetToWhoeverBumped
    IfTargetIsOnSameTeam
      tmpargument = 6
      SendMessageNear
      tmpargument = 4
      SetState
      tmpargument = 50
      SetTime

//------------------------------------------------------------------------------
//Update check
IfTimeOut

  IfStateIs2			//Wait before talking
    tmpargument = 0
    SetState

  IfStateIs4			//Wait before talking
    tmpargument = 3
    SetState

  IfStateIs0
    SetTargetToNearestFriend
      // Approach the player
      tmpx = targetx
      tmpy = targety
      tmpturn = targetturnto
      tmpdistance = 0
      Compass
      ClearWaypoints
      AddWaypoint
      tmpargument = 20
      SetTime
    Else
      // Walk around in a circle
      tmpx = selfx
      tmpy = selfy
      tmpturn = selfturn + 8192
      tmpdistance = 200
      Compass
      ClearWaypoints
      AddWaypoint
      tmpargument = 20
      SetTime

  //Follow and combat
  IfStateIs1

    //Draw weapons
    tmpargument = 2
    IfContentIs
      DoNothing
    Else
      SetTargetToSelf
      SetTargetToTargetLeftHand
        SetTargetToSelf
        SetTargetToTargetRightHand
          DoNothing
        Else
          tmpargument = LATCHPACKRIGHT
          PressLatchButton
      Else
        tmpargument = LATCHPACKLEFT
        PressLatchButton
        GetContent
        tmpargument = tmpargument +1
        SetContent

    // Get in a fight
    tmpdistance = 128*4
    SetTargetToDistantEnemy
      // Charge!
      tmpx = targetx
      tmpy = targety
      tmpdistance = 300
      tmpturn = targetturnto
      Compass
      ClearWaypoints
      AddWaypoint
      // Take a stab?
      tmpx = targetdistance
      tmpy = 120
      IfXIsLessThanY
        IfFacingTarget
          tmpargument = rand & 1 + 1
          PressLatchButton
      tmpargument = 10
      SetTime

    Else
      // Follow the Player around
      SetTargetToLeader
      tmpx = targetdistance
      tmpy = 200
      IfXIsLessThanY
        tmpx = targetx
        tmpy = targety
        tmpturn = targetturnto + 32768
        tmpdistance = 100
        Compass
        ClearWaypoints
        AddWaypoint
        tmpargument = 20
        SetTime
      Else
        // Run after the player
        tmpx = targetx
        tmpy = targety
        tmpturn = targetturnto
        tmpdistance = 400
        Compass
        ClearWaypoints
        AddWaypoint
        tmpargument = 20
        SetTime

      //Handle jumping
      tmpx = targetaltitude
      tmpy = selfz + 25
      IfXIsMoreThanY
        tmpargument = LATCHJUMP
        PressLatchButton

      //K'nife is in the assigned passage
      tmpargument = passage
      tmpdistance = [THIE]
      SetTargetToPassageID
        tmpdistance = [KEYA]
        SetTargetToPassageID
          tmpargument = 3
          SetState			//Stand on button
          tmpargument = 6
          SendMessageNear		//I'll wait here!
          tmpargument = 5
          PlaySound
        Else
          tmpargument = 4
          SendMessageNear		//We need a key!
          tmpargument = 5
          PlaySound
          tmpargument = 100
          SetTime

  //Stand on button state
  IfStateIs3
    SetTurnModeToVelocity
    tmpargument = 20
    SetTime
    SetTargetToWideEnemy
      tmpargument = 1
      SetState		//Return to follow
    Else
      tmpx = 3522
      tmpy = 3651	//Coordinates for the button
      ClearWaypoints
      AddWaypoint
      tmpx = gotodistance
      tmpy = 60
      IfXIsMoreThanY
        tmpx = gotodistance		//Close enough to jump?
        tmpy = 150
        IfXIsLessThanY
          tmpargument = LATCHJUMP	//Jump on the button
          PressLatchButton
      Else
        SetTargetToLeader		//Turn to the player when standing on the button
        SetTurnModeToWatchTarget

//------------------------------------------------------------------------------
// ZZ> Handle death by sending a message and other stuff
IfKilled
  tmpargument = 2
  PlaySound
  tmpargument = 0
  IfTargetIsOnSameTeam
    tmpargument = 1
  SendMessage

  // Drop goodies
  tmpargument = selfmoney
  DropMoney
  DropItems
  DropKeys

  // Make the character body
  tmpargument = 45
  SetBumpHeight

//------------------------------------------------------------------------------
// ZZ> Handle being attacked by blocking or countering or running away
IfAttacked
  SetTargetToWhoeverAttacked
  IfTargetIsOnSameTeam
    tmpargument = 5
    PlaySound
    tmpargument = 2
    SendMessageNear

    //Get unfriendly
    IfStateIs0
      JoinEvilTeam
      tmpargument = 1
      SetState
    
  Else
    tmpargument = rand & 1 + 3
    PlaySound
  SetTargetToOldTarget

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