Liquid

From Sandboxels Wiki
Jump to navigation Jump to search
A puddle of water
Water is an example of a liquid.

Liquid is a behavior that makes the element act like a liquid. The element falls down chaotically in the air and moves left and right on ground. Liquids are mostly in the Liquids category. Water is an example of a liquid.

Technical

The LIQUID behavior in Sandboxels' source code is described as a function:

LIQUID: function(pixel) {
  if (pixel.start === pixelTicks) {return}
  if (pixel.charge && elements[pixel.element].behaviorOn) {
    pixelTick(pixel)
  }
  if (elements[pixel.element].viscosity && (!((Math.random()*100) < 100 / Math.pow(elements[pixel.element].viscosity, 0.25)))) {
    var move1Spots = [
      [pixel.x, pixel.y+1]
    ]
  }
  else {
    var move1Spots = [
      [pixel.x+1, pixel.y+1],
      [pixel.x, pixel.y+1],
      [pixel.x-1, pixel.y+1],
    ]
  }
  var moved = false;
  for (var i = 0; i < move1Spots.length; i++) {
    var coords = move1Spots[Math.floor(Math.random()*move1Spots.length)];
    if (tryMove(pixel, coords[0], coords[1])) { moved = true; break; }
    else { move1Spots.splice(move1Spots.indexOf(coords), 1); }
  }
  if (!moved) {
    if (elements[pixel.element].viscosity===undefined || !(!((Math.random()*100) < 100 / Math.pow(elements[pixel.element].viscosity, 0.25)))) {
      if (Math.random() < 0.5) {
        if (!tryMove(pixel, pixel.x+1, pixel.y)) {
          tryMove(pixel, pixel.x-1, pixel.y);
        }
      } else {
        if (!tryMove(pixel, pixel.x-1, pixel.y)) {
          tryMove(pixel, pixel.x+1, pixel.y);
        }
      }
    }
  }
  doDefaults(pixel);
},

This is functionally identical to the LIQUID_OLD behavior, which is coded in a grid style:

LIQUID_OLD: [
  "XX|XX|XX",
  "M2|XX|M2",
  "M1|M1|M1",
],