IfSpawned
  MakeCrushValid

IfCrushed
  tmpx = selfx
  tmpy = selfy
  tmpdistance = selfz
  tmpargument = 1
  SpawnExactParticle
  SpawnExactParticle
  GoPoof
  tmpargument = 1
  SendMessageNear

IfDropped
  // Popped out of a chest
  tmpargument = 3
  PlaySound

IfKilled
  IfTargetIsSelf
    tmpargument = 2
  Else
    tmpargument = 0
  SendMessageNear
  tmpargument = 40
  SetBumpHeight
  tmpargument = 1
  PlaySound

IfAttacked
  SetTargetToWhoeverAttacked
    tmpargument = 0
    PlaySound

IfBumped
  SetTargetToWhoeverBumped
  IfTargetIsOnHatedTeam
    IfFacingTarget
      tmpargument = LATCHLEFT
      PressLatchButton
  Else
    tmpx = rand & 1023 - 512 + selfx
    tmpy = rand & 1023 - 512 + selfy
    ClearWaypoints
    AddWaypoint
    SetTargetToOldTarget

// Look for enemies
IfTimeOut
  tmpdistance = 128*8
  SetTargetToDistantEnemy
    IfFacingTarget
      //Growl
      tmpx = rand & 2047
      tmpy = 0
      IfXIsEqualToY
        tmpargument = 3
        PlaySound

      //Attack
      tmpx = targetdistance
      tmpy = 450
      IfXIsLessThanY
        tmpx = selfaccel
        tmpy = 1
        IfXIsMoreThanY
          tmpargument = LATCHJUMP
          PressLatchButton
          tmpx = targetx - selfx > 4
          tmpy = targety - selfy > 4
          SetOldTarget
          SetTargetToSelf
          AccelerateTarget
          SetTargetToOldTarget
          
          //Spawn bite particle
          tmpargument = 5
          tmpdistance = SPAWNLAST
          SpawnAttachedParticle
          
          // Play the attack sound
          tmpargument = 2
          PlaySound

      tmpx = targetx
      tmpy = targety
      tmpturn = targetturnto
      tmpdistance = 1500
      Compass
    Else
      tmpx = rand & 511 + targetx - 256
      tmpy = rand & 511 + targety - 256
      tmpturn = targetturnto
      tmpdistance = -256
      Compass
    ClearWaypoints
    AddWaypoint
    
  //No enemies around.... wander
  Else
    tmpx = selfx
    tmpy = selfy
    tmpturn = selfturn
    tmpdistance = 500
    Compass
    tmpturn = rand & 1023 - 512 + selfspawnx
    tmpx = tmpx + tmpturn + tmpturn + tmpturn > 2
    tmpturn = rand & 1023 - 512 + selfspawny
    tmpy = tmpy + tmpturn + tmpturn + tmpturn > 2
    ClearWaypoints
    AddWaypoint
  tmpargument = rand & 31 + 25
  SetTime

// All done
End
