//The different states:
//0 = Bored
//1 = Follow master
//-------------------------------------------------------------------------------
IfSpawned				// Item
  MakeNameKnown
  MakeCrushValid
  KeepAction
  IfNameIsKnown			//It has a owner from the start
    SetTargetToWhoeverIsHolding
    tmpargument = [GNOM]
    IfTargetHasID
      tmpargument = 1
      SetState
      SetOwnerToTarget
      JoinTargetTeam

//-------------------------------------------------------------------------------
IfCrushed
  GoPoof
  SpawnPoof
  SpawnPoof
  SpawnPoof
  tmpargument = 4
  SendMessageNear

//-------------------------------------------------------------------------------
//Spawns smoke puffs when in booster mode
tmpargument = [POWR]
IfContentIs
  tmpx = rand % 4
  tmpy = 0
  IfXIsEqualToY
    SpawnPoof
  tmpx = ownerx
  tmpy = ownery
  ClearWaypoints
  AddWaypoint
  
//-------------------------------------------------------------------------------
//Show stats
IfUsed
  tmpargument = 2
  PlaySound
  tmpargument = 75
  SetReloadTime
  
  //Name
  tmpargument = 5
  SendMessageNear
  
  //Display level
  tmpx = selflevel
  tmpy = 1
  IfXIsMoreThanY
    tmpargument = 9
  Else
    tmpargument = 6 + selflevel
  SendMessageNear

  //Stats with life  
  tmpx = targetlife > 9
  tmpy = 2
  IfXIsMoreThanY
    tmpx = 3
  tmpargument = 10 + tmpx
  tmpx = selfstr > 8
  tmpy = selfwis > 8
  SendMessageNear

  // Stats
  tmpx = selfint > 8
  tmpy = selfdex > 8
  tmpdistance = targetexp
  tmpargument = 14
  SendMessageNear
  
  //Special abilities
  tmpx = 1
  tmpy = selflevel
  IfXIsLessThanY        //Level 3 or higher gives boost
    tmpdistance = 1
  Else
    tmpdistance = 0
  tmpx = 0
  IfXIsMoreThanY        //Level 2 or higher gives shock
    tmpx = 1
  tmpy = 1              //Level 1 or higher gives alarm
  tmpargument = 15
  SendMessageNear

//-------------------------------------------------------------------------------
//This function makes Jimju cheer when idle
IfTimeOut				// Wait a bit
  
  //Reset speed and timer
  tmpargument = [POWR]
  IfContentIs
    Run
    tmpargument = 0
    SetContent
  tmpargument = rand & 63 + 10
  SetTime				  

  //Dont run AI loop if held
  IfHeld
    DoNothing
  Else
  
    //Tic tac sounds
    tmpx = rand & 255
    tmpy = 200
    IfXIsMoreThanY
      tmpargument = 2
      PlaySound
    
    //Initiate warnings
    IfNameIsKnown
      tmpdistance = selflevel*192 + 896 //7 tiles +1.5 tiles per level
      SetTargetToDistantEnemy
        tmpargument = 255
        FlashVariable
        //Light effects
        tmpargument = 4
        tmpx = 0
        tmpy = 0
        tmpdistance = SPAWNORIGIN
        SpawnParticle
        tmpargument = 0
        IfContentIs
          tmpargument = 2
          SendMessageNear			//Warn the player
          tmpargument = 1
          SetContent
          
          //Flash warning
          tmpargument = 2
          SetBlueShift		
          SetGreenShift
          
          //Alarm sound
          tmpargument = 3
          PlaySound
          
      Else
        tmpargument = 0
        SetContent
        SetBlueShift			//Stop warning
        SetGreenShift

    // State 0 ( Bored )
    SetTargetToOwner
    IfStateIs0				  
      tmpargument = ACTIONMC
      DoAction
      tmpx = selfx			//Walk in a circle
      tmpy = selfy
      tmpturn = selfturn + 8192
      tmpdistance = 200
      Compass
      ClearWaypoints
      AddWaypoint
      
      //Follow owner
      tmpx = ownerdistance
      tmpy = 128*5
      IfXIsLessThanY
        tmpargument = 1
        SetState
      
    // State 1 ( Follow )
    IfStateIs1
      tmpx = 128*12
      tmpy = ownerdistance
      IfXIsLessThanY			    // Become bored if owner is away
        tmpargument = 0
        SetState
        tmpx = selfx
        tmpy = selfy
        ClearWaypoints			      // Dont move
        AddWaypoint
      Else				    // Follow the owner
        tmpx = selflevel
        tmpy = 1
        IfXIsMoreThanY          //Enable booster abillity at level 3
          tmpx = 128*5
          IfXIsLessThanY
            tmpargument = 300
            SetSpeedPercent
            tmpargument = 150     //Do this for 3 seconds
            SetTime
            tmpargument = [POWR]  //Code for extra speed
            SetContent
            tmpargument = 1
            PlaySound
            tmpx = ownerx
            tmpy = ownery
          Else
            tmpx = rand & 255 - 128 + ownerx	      
            tmpy = rand & 255 - 128 + ownery	      
        Else
          tmpx = rand & 255 - 128 + ownerx	      
          tmpy = rand & 255 - 128 + ownery	      
        ClearWaypoints			      
        AddWaypoint	      

    //Shock enemies
    tmpx = selflevel
    tmpy = 0
    IfXIsMoreThanY          //Enable shock abillity at level 2
      tmpargument = 1
      IfContentIs
        SetTargetToNearbyEnemy
          tmpx = targetdistance
          tmpy = 512
          IfXIsLessThanY
            tmpx = targetx
            tmpy = targety
            tmpdistance = 1500
            tmpturn = targetturnto
            Compass
            ClearWaypoints
            AddWaypoint
            tmpx = targetdistance
            tmpy = 90
            IfXIsLessThanY
              tmpdistance = SPAWNORIGIN
              tmpargument = 0
              SpawnAttachedParticle
              tmpargument = 5			//Recharge
              SetContent
 
  //Start recharging
  tmpargument = [POWR]
  IfContentIs
    DoNothing
  Else
    tmpx = selfcontent
    tmpy = 1
    IfXIsMoreThanY
      tmpargument = selfcontent -1
      SetContent

  //This doesnt work for some reason... (Zap when in water)
  IfOverWater
    SetTargetToSelf
    SpawnPoof
    tmpargument = 256
    tmpdistance = DAMAGEZAP
    DamageTarget


//-------------------------------------------------------------------------------
//What to do if picked up...
IfGrabbed				// Stop cheering
  SetTargetToWhoeverIsHolding
    tmpargument = 0
    SetContent
    SetBlueShift			//Stop warning
    SetGreenShift  
    Run
  tmpargument = [GNOM]
  IfTargetHasID
    IfTargetIsOwner			  // Say hi to old owner
      tmpargument = 1
      SendMessageNear
    Else					  // Say hi to new owner
      tmpargument = 0
      SendMessageNear
      SetOwnerToTarget
      MakeNameKnown
      JoinTargetTeam
  Else
    tmpargument = 3			    //Doesn't know what it is
    SendMessageNear			    //
  tmpargument = 1			  // Set that state
  SetState
  UnkeepAction				  // Make it animate

  //Ready electric charge
  tmpargument = 2
  IfContentIs
    tmpargument = 1
    SetContent

//-------------------------------------------------------------------------------
//Follow master
IfDropped				// Stop cheering
  tmpargument = 1			  // Set that state
  SetState				  //

//-------------------------------------------------------------------------------
//Death
IfKilled
  tmpargument = 4
  SendMessageNear
  tmpargument = 5
  PlaySound
  
  //Cannot be ressurected!
  JoinNullTeam

//-------------------------------------------------------------------------------
//Ouchies
IfAttacked
  tmpargument = 4
  PlaySound


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

