//The diffrent States
//State 0: Talk
//State 1: Remain Silent
//State 2: Walk around
//State 3: Wait before walking again
//State 4: Sleeping
//State 5: Looking for trouble
//State 6: In combat


//------------------------------------------------------
//Handle behaviour
IfSpawned
  tmpargument = 0	 
  IfContentIs
    tmpargument = 2	//Character is walking around
    SetState

  tmpargument = 2	 
  IfContentIs
    tmpargument = 4	//Character is sleeping
    SetState

IfBumped
  SetTargetToWhoeverBumped
  IfTargetIsAPlayer
    IfStateIs0
      tmpargument = rand % 3
      SendMessageNear	//Say something random
      PlaySound
      tmpargument = 1
      SetState
      tmpargument = 200
      SetTime

    IfStateIs2
      tmpargument = rand % 4
      SendMessageNear	//Say something random
      PlaySound
      tmpargument = 3
      SetState

    IfStateIs4
      tmpargument = 2
      SetState
      tmpargument = 5
      SendMessageNear	//Tell the players the pirate woke up
      tmpargument = 0
      PlaySound
      tmpargument = 40
      SetTime


IfTimeOut		//Actions when timeout
			      // Change to combat mode

  //PIRATE CAN TALK AGAIN
  IfStateIs1
    tmpargument = 0	
    SetState

  //WANDER AROUND
  IfStateIs2			
    tmpx = rand&511+selfx-256
    tmpy = rand&511+selfy-256
    ClearWaypoints
    AddWaypoint
    tmpargument = 3	//Wait before walking around again
    SetState
    tmpargument = 200
    SetTime

   //WANDER AROUND
   IfStateIs3
    tmpx = rand&511+selfx-256
    tmpy = rand&511+selfy-256
    ClearWaypoints
    AddWaypoint
    tmpargument = 2	//Wait before walking around again
    SetState
    tmpargument = 200
    SetTime

   //SLEEPING
   IfStateIs4
     tmpdistance = SPAWNORIGIN
     tmpargument = 2
     tmpx = selfx            // x
     tmpy = selfy            // y
     tmpdistance = selfz     // z
     SpawnExactParticle
     SpawnExactParticle
     tmpargument = 8
     PlaySound
     tmpargument = 300
     SetTime

   //LOOKING FOR TROUBLE
   IfStateIs5
     SetTargetToWideEnemy
       tmpargument = 6			//Enemy found, enter combat
       SetState

       //Draw weapon
       IfUnarmed		//Armed?
         tmpargument = LATCHPACKRIGHT	//Nop, draw weapon
         PressLatchButton
     Else
       tmpx = rand & 511 + selfx - 256	//No enemies, wander around    
       tmpy = rand & 511 + selfy - 256
       Compass
       ClearWaypoints
       AddWaypoint
       tmpargument = 20 + rand & 60
       SetTime
       IfHoldingMeleeWeapon
         tmpargument = LATCHPACKRIGHT	//Seathe weapon
         PressLatchButton


   //IN COMBAT
   IfStateIs6
    IfUnarmed
      tmpargument = LATCHPACKRIGHT	//Draw weapon
      PressLatchButton
     tmpargument = 40
     SetTime
     tmpx = targetdistance
     tmpy = 166
     IfXIsLessThanY
       IfFacingTarget
         IfTargetIsAlive
           IfTargetIsOnHatedTeam
             tmpargument = LATCHRIGHT
             PressLatchButton
     SetTargetToNearestEnemy
       tmpx = targetx
       tmpy = targety
       tmpturn = targetturnto
       Compass
       ClearWaypoints
       AddWaypoint
     Else
       tmpargument = 5
       SetState

//Jumping
IfBumped
  SetTargetToWhoeverBumped
  tmpargument = [CHES]
  IfTargetHasID
    tmpargument = LATCHJUMP
    PressLatchButton
  IfTargetIsOnHatedTeam
    IfFacingTarget
      IfTargetIsAlive
        tmpargument = LATCHRIGHT
        PressLatchButton
  Else					//Dont flock
    tmpx = rand & 511 + targetx - 256	    
    tmpy = rand & 511 + targety - 256
    tmpturn = selfturn
    tmpdistance = rand & 250
    Compass		    
    ClearWaypoints			    
    AddWaypoint
    tmpargument = rand & 40 + 20			     
    SetTime

//What to do if killed
IfKilled	
  CallForHelp			//Warn friends
  tmpargument = 6
  PlaySound			//Play a sound when killed
  tmpargument = 65000
  DropMoney			//Drop loot
  DropKeys
  DropItems
  tmpargument = 45			  
  SetBumpHeight				  

// Get mean if characters wanna be bad
IfAttacked
  SetTargetToWhoeverAttacked
  tmpargument = 5
  SetState
  SetTime
  IfTargetIsAPlayer
    CallForHelp
    JoinEvilTeam
  Else
    SetTargetToOldTarget

IfCalledForHelp
  SetTargetToWhoeverCalledForHelp
    SetOwnerToTarget
    tmpargument=5
    SetState
    JoinEvilTeam

End	//The end