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

//------------------------------------------------------------------------------
//Follow the leader!
IfSpawned
  tmpargument = STATEFOLLOW
  SetState

//------------------------------------------------------------------------------
IfKilled				// This reduces the height of the char
  tmpargument = 1
  PlaySound

  //Last words
  tmpargument = 0
  IfTargetIsOnSameTeam			  // Fragged!
    tmpargument = 3
    IfTargetIsSelf			    // No, just a damage tile
      tmpargument = 4
  SendMessage
  
  tmpargument = selfmoney			  // Drop money
  DropMoney
  tmpargument = 45
  SetBumpHeight

//------------------------------------------------------------------------------
IfTimeOut				// This is done every so often
  tmpargument = rand & 15 + 15		  // Try again later
  SetTime				  //

  //Find someone to heal
  tmpargument = STATEGUARD
  IfStateIsNot
    tmpx = selfmana
    tmpy = 260
    IfXIsMoreThanY				// Enough mana to heal?
      SetTargetToNearestFriend
        IfTargetIsHurt
          tmpx = targetdistance
          tmpy = 400
          IfXIsLessThanY
            tmpargument = STATEGUARD
            SetState
      
    //move towards target
    tmpx = targetx
    tmpy = targety
    ClearWaypoints
    AddWaypoint
      
      
  IfStateIsFollow
    SetTargetToLeader
    IfTargetIsAlive

      //Follow the player
      tmpx = leaderdistance
      tmpy = 128*6
      IfXIsMoreThanY
        tmpargument = 133        //Catch up fast!
        SetSpeedPercent
      Else
        tmpargument = 100        //Walk normal
        SetSpeedPercent
        
      tmpdistance = 100         
      tmpx = leaderx
      tmpy = leadery
      tmpturn = 49152 + leaderturn	    // Stand to side
      Compass
      ClearWaypoints
      AddWaypoint
      
      //Find someone to bash
      tmpx = leaderdistance
      tmpy = 128*6
      IfXIsLessThanY
        SetTargetToWideEnemy
          tmpargument = STATECOMBAT
          SetState
    Else
      tmpargument = STATEWANDER
      SetState
      
  IfStateIsCombat
  
    tmpargument = [LOCK]
    tmpdistance = BLAHENEMIES + BLAHINVERTID        //Enemies that are not a mimic
    SetTargetToWideBlahID
      tmpx = targetdistance		    // Close enough to attack?
      tmpy = 300
      IfXIsLessThanY
        IfFacingTarget
          IfHoldingMeleeWeapon
            PressLatchButton

      //Retreat if leader is running and not in combat
      tmpx = leaderdistance
      tmpy = 128*6
      IfXIsMoreThanY
        tmpx = targetdistance
        tmpy = 200
        IfXIsMoreThanY
          tmpargument = STATEFOLLOW
          SetState
      
      //Special movement in case its the jelly cube
      tmpargument = [CUBE]
      IfTargetHasID
        tmpargument = [HUMA]
        IfTargetHoldingItemID
          tmpdistance = 0         //Go save the humanoid who is trapped inside!
        Else
          tmpdistance = -400     //Keep away from the gelfeet
      Else
        tmpdistance = 0			//Else move in to attack
      
      tmpx = targetx			    
      tmpy = targety			    
      tmpturn = targetturnto
      Compass				    
      ClearWaypoints
      AddWaypoint
    
    //No enemies around, return to follow
    Else
      tmpargument = STATEFOLLOW
      SetState
  
  
  IfStateIsWander
    tmpx = rand & 511 + selfx - 256
    tmpy = rand & 511 + selfy - 256
    ClearWaypoints
    AddWaypoint

    //Follow the leader if he has respawned
    SetTargetToLeader
      IfTargetIsAlive
        tmpargument = STATEFOLLOW
        SetState
        
    //Look out for enemies
    SetTargetToNearbyEnemy		  
      tmpargument = STATECOMBAT
      SetState				      // Change to combat mode

  //Healing state
  IfStateIsGuard    
    tmpargument = STATEFOLLOW
    SetState
    
    //Heal closest friend
    SetTargetToNearestFriend
      IfTargetIsHurt			    // Heal wounded only

        //Enough mana to cast?
        tmpx = selfmana
        tmpy = 260
        IfXIsMoreThanY				
      
          //Only if he didn't run away
          tmpx = targetdistance
          tmpy = 128*6
          IfXIsLessThanY
            tmpargument = STATEGUARD
            SetState
          
            //Close enough to cast spell?
            tmpx = targetdistance
            tmpy = 300
            IfXIsLessThanY
              IfFacingTarget
                tmpargument = LATCHLEFT	          // Left Attack == 1
                PressLatchButton

//------------------------------------------------------------------------------
// Heal sound and effect
IfUsed  
  //Heal friendlies
  IfTargetIsOnSameTeam
    tmpx = targetx
    tmpy = targety
    tmpdistance = targetz
    tmpargument = 0
    SpawnExactParticle

    tmpargument = rand & 511 + 512
    HealTarget

    //Make a sound
    tmpargument = 0
    PlaySound
    tmpargument = 50
    SetReloadTime
      
//------------------------------------------------------------------------------
IfAttacked				// Counter attack if not healing
  SetTargetToWhoeverAttacked		    //
    IfTargetIsAlive			      //
      IfTargetIsOnHatedTeam		        // Go get 'em
        tmpargument = 1		          //
        SetState			          //
        tmpargument = rand & 1 + 2
        PlaySound
      IfTargetIsOnSameTeam		        // Yell at 'em
        tmpargument = MESSAGEOUCH	          //
        SendMessageNear		          //
        SetTargetToOldTarget		          //
        tmpargument = 4
        PlaySound
    Else				      // Attacker dead already
      SetTargetToOldTarget		        //  

//------------------------------------------------------------------------------
IfBumped
  SetTargetToWhoeverBumped
  IfTargetIsAlive
    
    // Heal 'em
    IfTargetIsOnSameTeam
      IfTargetIsHurt			  
        tmpargument = STATEGUARD
        SetState
        SetTargetToOldTarget
      Else
        tmpargument = STATECOMBAT
        IfStateIsNot
          //Move out of the way
          tmpx = rand & 511 + selfx - 256
          tmpy = rand & 511 + selfy - 256
          ClearWaypoints
          AddWaypoint
          tmpargument = 40			  // Try again soon
          SetTime

    //Bash 'em
    IfTargetIsOnHatedTeam
      IfFacingTarget
        IfHoldingMeleeWeapon
          PressLatchButton
          
  //Jump over bodies
  Else
    tmpargument = LATCHJUMP
    PressLatchButton
    

//------------------------------------------------------------------------------
IfTargetKilled				// Mode switch
  tmpargument = STATEFOLLOW			  // Return to follow mode
  SetState

//------------------------------------------------------------------------------
End					// Finished with this character
//------------------------------------------------------------------------------
