//------------------------------------------------------------------------------
// Spawn the chain particles every frame
IfCrushed
  tmpx = selfx
  tmpy = selfy
  tmpdistance = selfz
  tmpargument = 2
  SpawnExactParticle
  SpawnExactParticle
  SpawnExactParticle
  SpawnExactParticle
  GoPoof
  tmpargument = 4
  SendMessageNear
  
IfTargetIsSelf
  DoNothing
Else
  // First figure out where to draw them

  // Chain link nearest to target, only move if out of range
  tmpargument = 3
  GetXY
  tmpx = targetx - tmpx
  tmpy = targety - tmpy
  tmpdistance = xydistance
  tmpturn = 128
  IfDistanceIsMoreThanTurn
    tmpdistance = 128
  tmpx = tmpx * tmpdistance > 7
  tmpy = tmpy * tmpdistance > 7
  AddXY
  tmpargument = 2
  GetXY
  tmpdistance = tmpx
  tmpturn = tmpy
  tmpargument = 3
  GetXY
  tmpx = tmpdistance - tmpx
  tmpy = tmpturn - tmpy
  tmpdistance = xydistance
  tmpturn = 256
  IfDistanceIsMoreThanTurn
    tmpdistance = 256
  tmpx = tmpx * tmpdistance > 8
  tmpy = tmpy * tmpdistance > 8
  AddXY

  // Next one
  tmpargument = 3
  GetXY
  tmpdistance = tmpx
  tmpturn = tmpy
  tmpargument = 2
  GetXY
  tmpx = tmpdistance - tmpx
  tmpy = tmpturn - tmpy
  tmpdistance = xydistance
  tmpturn = 128
  IfDistanceIsMoreThanTurn
    tmpdistance = 128
  tmpx = tmpx * tmpdistance > 7
  tmpy = tmpy * tmpdistance > 7
  AddXY
  tmpargument = 1
  GetXY
  tmpdistance = tmpx
  tmpturn = tmpy
  tmpargument = 2
  GetXY
  tmpx = tmpdistance - tmpx
  tmpy = tmpturn - tmpy
  tmpdistance = xydistance
  tmpturn = 256
  IfDistanceIsMoreThanTurn
    tmpdistance = 256
  tmpx = tmpx * tmpdistance > 8
  tmpy = tmpy * tmpdistance > 8
  AddXY

  // Next one
  tmpargument = 2
  GetXY
  tmpdistance = tmpx
  tmpturn = tmpy
  tmpargument = 1
  GetXY
  tmpx = tmpdistance - tmpx
  tmpy = tmpturn - tmpy
  tmpdistance = xydistance
  tmpturn = 128
  IfDistanceIsMoreThanTurn
    tmpdistance = 128
  tmpx = tmpx * tmpdistance > 7
  tmpy = tmpy * tmpdistance > 7
  AddXY
  tmpargument = 0
  GetXY
  tmpdistance = tmpx
  tmpturn = tmpy
  tmpargument = 1
  GetXY
  tmpx = tmpdistance - tmpx
  tmpy = tmpturn - tmpy
  tmpdistance = xydistance
  tmpturn = 256
  IfDistanceIsMoreThanTurn
    tmpdistance = 256
  tmpx = tmpx * tmpdistance > 8
  tmpy = tmpy * tmpdistance > 8
  AddXY

  // Link nearest to ball
  tmpargument = 1
  GetXY
  tmpdistance = tmpx
  tmpturn = tmpy
  tmpargument = 0
  GetXY
  tmpx = tmpdistance - tmpx
  tmpy = tmpturn - tmpy
  tmpdistance = xydistance
  tmpturn = 128
  IfDistanceIsMoreThanTurn
    tmpdistance = 128
  tmpx = tmpx * tmpdistance > 7
  tmpy = tmpy * tmpdistance > 7
  AddXY
  tmpx = selfx
  tmpy = selfy
  tmpdistance = tmpx
  tmpturn = tmpy
  tmpargument = 0
  GetXY
  tmpx = tmpdistance - tmpx
  tmpy = tmpturn - tmpy
  tmpdistance = xydistance
  tmpturn = 256
  IfDistanceIsMoreThanTurn
    tmpdistance = 256
  tmpx = tmpx * tmpdistance > 8
  tmpy = tmpy * tmpdistance > 8
  AddXY

  // Don't allow jumping
  tmpx = targetaltitude
  tmpy = 256
  IfXIsMoreThanY
    StopTargetMovement

  // Now check for pulled chain
  tmpx = targetx - selfx
  tmpy = targety - selfy
  tmpdistance = xydistance
  tmpturn = 180
  IfDistanceIsMoreThanTurn
    tmpturn = 225
    IfDistanceIsMoreThanTurn
      tmpx = targetx
      tmpy = targety
      Teleport
    Else
      tmpx = selfx - targetx > 6
      tmpy = selfy - targety > 6
      SetTargetToLowestTarget
      AccelerateTarget
      SetTargetToSelf
      SetTargetToLowestTarget
      tmpx = -tmpx
      tmpy = -tmpy
      IfTargetIsSelf
        IfTimeOut
          tmpargument = 1
          PlaySound
          tmpargument = rand & 7 + 18
          SetTime
      AccelerateTarget
      SetTargetToOldTarget

  // Check for a crushed ball
  tmpturn = 0
  IfCrushed
    tmpturn = 1

  // Now draw them all
  tmpargument = 3
  GetXY
  tmpargument = tmpturn
  tmpdistance = targetaltitude > 1
  tmpdistance = targetz - tmpdistance
  SpawnExactParticle

  tmpdistance = 0
  tmpargument = 2
  GetXY
  tmpargument = tmpturn
  SpawnExactParticle

  tmpargument = 1
  GetXY
  tmpargument = tmpturn
  SpawnExactParticle

  tmpargument = 0
  GetXY
  tmpargument = tmpturn
  tmpdistance = selfaltitude > 1
  tmpdistance = selfz - tmpdistance
  SpawnExactParticle

//------------------------------------------------------------------------------
//This function...  Does stuff
IfSpawned				// Make it able to be crushed in doors
  MakeCrushValid			  //
  tmpargument = 15
  SetTime
IfStateIs0
  // Drop it when first appearing
  IfTimeOut
    tmpargument = 1
    SetState
    DetachFromHolder
IfDropped
  UndoEnchant
IfUsed
  tmpargument = 50
  SetReloadTime
IfHitGround				// Make a sound
  tmpargument = 0			  //
  PlaySound				  //
  
//IfNotPutAway              //Say oops
//  tmpargument = 5         //No longer needed, this is handled by the sourcecode now...
//  SendMessageNear

IfGrabbed				// Tell them what they've won...
  IfTargetIsSelf			  //
    tmpx = selfx			    // Clear out the storage values
    tmpy = selfy			    //
    tmpargument = 0			    //
    SetXY				    //
    tmpargument = 1			    //
    SetXY				    //
    tmpargument = 2			    //
    SetXY				    //
    tmpargument = 3			    //
    SetXY				    //
    SetTargetToWhoeverIsHolding		    //
    IfTargetIsAPlayer			    //
      tmpargument = 0			      //
      SendMessageNear			      //
  Else					  //
    SetTargetToWhoeverIsHolding		    //
    IfTargetIsOldTarget			    //
      tmpargument = 1			      //
    Else				    //
      SetTargetToOldTarget		      //
      tmpargument = 2			      //
    SendMessageNear			    //
  EnchantTarget
End					// All done
