// Wander around start position
IfSpawned
  tmpargument = 0
  tmpx = selfx
  tmpy = selfy
  SetXY
  tmpargument = STATEGUARD
  SetState

// Get mean if characters wanna be bad
IfAttacked
  SetTargetToWhoeverAttacked
    IfTargetIsAPlayer
      CallForHelp
      tmpargument = TEAME
      TargetJoinTeam
      DisableExport
  SetTargetToOldTarget
IfCalledForHelp
  SetTargetToWhoeverCalledForHelp
    tmpargument = 0
    
    //Get new guard area
    tmpx = targetx
    tmpy = targety
    SetXY
    tmpargument = STATEGUARD
    SetState

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

//----------------------------------------------------------------------------------
//Use shield
IfTargetIsOnHatedTeam
  IfTargetIsAttacking
    IfFacingTarget
      IfHoldingShield
        PressLatchButton
        
//----------------------------------------------------------------------------------
IfKilled				// This reduces the height of the char
  SetTargetToWhoeverAttacked
  CallForHelp			//Alert friends
  
  IfTargetIsAPlayer
    tmpargument = TEAME
    TargetJoinTeam

  IfTargetIsOnSameTeam			  // Fragged!
    tmpargument = 3			    //
    IfTargetIsSelf			    // No, just a damage tile
      tmpargument = 4			      //
  SendMessage				  //

  tmpargument = selfmoney			  // Drop money
  DropMoney				  //

  tmpargument = 45			  //
  SetBumpHeight				  //

//----------------------------------------------------------------------------------
IfAttacked				// Attacked
  SetTargetToWhoeverAttacked		  //
    IfTargetIsAlive			    //
      IfTargetIsOnHatedTeam
        tmpargument = 1			        //
        IfHoldingRangedWeapon
          tmpargument = 2
        SetState			        //  
      tmpargument = rand & 1 + 4	// Ouch!
      PlaySound			        //
    Else				    //Attacker died already
      SetTargetToOldTarget		      //

//----------------------------------------------------------------------------------
IfTimeOut				// This is done every so often
  SetTargetToWideEnemy		  // Look out for enemies
    tmpargument = STATECOMBAT
    SetState				      // Change to combat mode
  Else
    tmpargument = STATEGUARD
    SetState
    
  IfStateIsGuard
    Sneak
    
    // Try again later
    tmpargument = 50			  
    SetTime

    //Move around guard area
    tmpargument = 0
    GetXY
    tmpx = rand & 1023 + tmpx - 512
    tmpy = rand & 1023 + tmpy - 512
    ClearWaypoints
    AddWaypoint
    
    SetTargetToWideEnemy
      tmpargument = STATECOMBAT
      SetState
    
  IfStateIsCombat
    Run
    
    // Try again later
    tmpargument = rand & 15 + 20 
    SetTime
    
    //Melee AI
    IfHoldingMeleeWeapon
      tmpx = targetdistance		    // Close enough to attack?
      tmpy = 200
      IfXIsLessThanY
        IfFacingTarget
          IfHoldingMeleeWeapon
            PressLatchButton
      tmpx = rand & 255 + targetx - 128			    // Move towards enemy
      tmpy = rand & 255 + targety - 128
      tmpturn = targetturnto
      tmpdistance = 600
      Compass
      ClearWaypoints
      AddWaypoint
      
    //Ranged AI
    IfHoldingRangedWeapon
      tmpx = targetdistance
      tmpy = 800
      IfXIsMoreThanY
        tmpx = targetx			    // Move towards enemy
        tmpy = targety			    //
      Else
        tmpy = 300
        IfXIsLessThanY
          tmpx = rand & 2046 + selfx - 1023			    // Move away from enemy
          tmpy = rand & 2046 + selfy - 1023			    //
        Else
          tmpx = selfx        //We are okay
          tmpy = selfy
      tmpdistance = 50
      tmpturn = targetturnto
      Compass
      ClearWaypoints
      AddWaypoint
      
      //Shoot, but try not to hit friend
      IfFacingTarget
        SetOldTarget
        tmpx = targetdistance
        SetTargetToNearestFriend
        IfFacingTarget              //Dont shoot if we are looking at a friend
          tmpy = targetdistance     //who is closer than our target
        Else
          tmpx = 0
          tmpy = 1
        SetTargetToOldTarget
        IfXIsLessThanY
          IfHoldingRangedWeapon
            PressLatchButton
        
        //Try to get a better aim
        Else
          tmpx = rand & 1023 + targetx - 512
          tmpy = rand & 1023 + targety - 512
          tmpturn = targetturnto + 16000
          tmpdistance = 100
          Compass
          ClearWaypoints
          AddWaypoint
          


//----------------------------------------------------------------------------------
//Bumped
IfBumped				
  SetTargetToWhoeverBumped		  //
    IfTargetIsOnHatedTeam
      tmpargument = STATECOMBAT
      SetState
    Else
      SetTargetToOldTarget
      
      //Get out of the way
      tmpx = rand & 511 + selfx - 256		    //
      tmpy = rand & 511 + selfy - 256		    //
      ClearWaypoints			    //
      AddWaypoint				    //
      tmpargument = 40			    
      SetTime				    //

//----------------------------------------------------------------------------------
End					// All done
//----------------------------------------------------------------------------------
