// ZZ> This function makes the character wanders around its enemy

IfSpawned
  MakeCrushValid
  SetTargetToWhoeverIsHolding
    SetOwnerToTarget
    JoinTargetTeam
    MakeNameKnown
    
IfGrabbed
  MakeCrushInvalid
  IfNameIsKnown
    SetTargetToWhoeverIsHolding
    IfTargetIsAPlayer
      SetOldTarget
      SetTargetToOwner
      IfTargetIsOldTarget
        tmpargument = 5
      Else
        tmpargument = 6
      SendMessageNear
  Else
    SetTargetToWhoeverIsHolding
    SetOwnerToTarget
    JoinTargetTeam
    MakeNameKnown
    tmpargument = 3
    SendMessageNear
    tmpargument = 4
    SendMessageNear
  SetTargetToSelf
  SetTargetToTargetLeftHand
    DropItems
    tmpargument = 16
    SendMessageNear
    
IfDropped
  SetTargetToSelf
  StopTargetMovement
  UnkeepAction
  
IfUsed
  SetTargetToWhoeverIsHolding
  tmpargument = 25
  SetTargetReloadTime
  IfTargetIsAPlayer
    // =Cuddles=
    SetTargetToSelf
    tmpargument = 7
    SendMessageNear

    // Level
    tmpx = targetlevel
    tmpy = 2
    IfXIsMoreThanY
      tmpx = 2
    tmpargument = 8 + tmpx
    SendMessageNear


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

    // Stats
    tmpx = selfint > 8
    tmpy = selfdex > 8
    tmpdistance = targetexp
    tmpargument = 15
    SendMessageNear
    
IfCrushed
  tmpx = selfx
  tmpy = selfy
  tmpdistance = selfz
  tmpargument = 4
  SpawnExactParticle
  SpawnExactParticle
  SpawnExactParticle
  SpawnExactParticle
  GoPoof
  tmpargument = 2
  SendMessageNear
  
IfCleanedUp				// Respawn
  IfTargetIsAlive			  // Make sure killer is away
    tmpx = 1100
    tmpy = targetdistance
    IfXIsLessThanY
      RespawnCharacter
  Else					  // Killer is far away
    RespawnCharacter
    
IfKilled				// This reduces the height of the char
  IfNameIsKnown
    SetOldTarget
    SetTargetToOwner
    IfTargetIsAlive
      tmpargument = 0
      SendMessageNear
    SetTargetToOldTarget
  Else
    tmpargument = 1
    SendMessageNear
  tmpargument = 30			  // Dead height
  SetBumpHeight
  tmpargument = 5			  // Sound
  tmpdistance = rand & 2047 + 12000
  PlaySound
  
IfAttacked				// Don't take kindly to attackers
  SetTargetToWhoeverAttacked
    tmpargument = 4			    // Sound
    tmpdistance = rand & 2047 + 9300
    PlaySound
    
IfBumped				// Scooch around
  tmpx = rand & 1023 + targetx - 512
  tmpy = rand & 1023 + targety - 512
  ClearWaypoints
  AddWaypoint
  
IfTimeOut				// This is done every so often
  // Do clucking
  GetContent
  tmpx = tmpargument
  tmpy = 0
  IfXIsMoreThanY
    tmpargument = tmpargument - 1
    SetContent
    tmpargument = rand & 3
    tmpdistance = rand & 2047 + 10000
    PlaySound
    tmpargument = rand & 15 + 15
  Else
    tmpx = rand & 15
    tmpy = 7
    IfXIsMoreThanY
      tmpargument = tmpx > 1
      SetContent
    tmpargument = rand & 15 + 30
  SetTime

  IfHeld
    // Don't bother moving around if held
    GetState
  Else
    SetTargetToOwner
    IfTargetIsSelf
      tmpx = rand & 1023 - 512 + selfspawnx
      tmpy = rand & 1023 - 512 + selfspawny
    Else
      SetTargetToNearbyEnemy
        tmpx = targetdistance
        tmpy = 150
        IfXIsLessThanY
          IfFacingTarget
            // Attack the enemy...
            DropItems
            tmpargument = ACTIONUA
            DoAction

            // Play the sound
            tmpargument = 6
            tmpdistance = rand & 1023 + 8000
            PlaySound
        tmpx = targetx
        tmpy = targety
        tmpturn = targetturnto
        tmpdistance = 200
        Compass
      Else
        // Is it carrying something?
        SetTargetToSelf
        SetTargetToTargetLeftHand
          // Has an item, so bring it to owner
          tmpx = rand & 511 - 256 + ownerx
          tmpy = rand & 511 - 256 + ownery
        Else
          // No item, so find one
          tmpargument = [NONE]
          tmpdistance = BLAHITEMS         //Any item will do
          SetTargetToWideBlahID
            // Don't grab kursed items
            IfTargetIsKursed
              DoNothing
            Else
              // Get closer?
              tmpx = targetdistance
              tmpy = 20
              IfXIsLessThanY
                // Pick up the item
                tmpargument = LATCHALTLEFT
                PressLatchButton
              tmpx = targetx
              tmpy = targety
              tmpturn = targetturnto
              tmpdistance = 200
              Compass
            Else
              // The item is bad, so follow the owner
              tmpx = ownerx
              tmpy = ownery
          Else
            // No items around, so follow owner
            tmpx = ownerx
            tmpy = ownery
    ClearWaypoints
    AddWaypoint
    
End					// Finished with this character