0

Below is a JavaScript from a interface in the Customs NPC mod which runs with minecraft. It is suppose to be running a flood fill routine. It only fill correctly to the positive x and positive z of the starting position. In minecraft x & z are in the horizontal plane y is the vertical axis.

var node1 = {
  xy: []
};
var blockp;
//starting block type and position
blockp = world.getBlock(npc.getBlockX(), npc.getBlockY() - 1, npc.getBlockZ() + 1);
node1.xy[0] = npc.getBlockX();
node1.xy[1] = npc.getBlockZ() + 1;
node1.xy[2] = npc.getBlockY() - 1;
//
var floodfill = function(nameb, node) {
  if (nameb == "minecraft:stone" ||
    nameb == null) {
    return;
  }
  var blkname;
  //
  world.setBlock(node.xy[0], node.xy[2], node.xy[1], world.createItem("minecraft:stone", 0, 1));
  //
  node.xy[0] = node.xy[0];
  node.xy[1] = node.xy[1] + 1;
  node.xy[2] = node.xy[2];
  blkname = world.getBlock(node.xy[0], node.xy[2], node.xy[1]).name;
  floodfill(blkname, node);
  //
  node.xy[0] = node.xy[0];
  node.xy[1] = node.xy[1] - 1;
  node.xy[2] = node.xy[2];
  blkname = world.getBlock(node.xy[0], node.xy[2], node.xy[1]).name;
  floodfill(blkname, node);
  //
  node.xy[0] = node.xy[0] + 1;
  node.xy[1] = node.xy[1];
  node.xy[2] = node.xy[2];
  blkname = world.getBlock(node.xy[0], node.xy[2], node.xy[1]).name;
  floodfill(blkname, node);
  //
  node.xy[0] = node.xy[0] - 1;
  node.xy[1] = node.xy[1];
  node.xy[2] = node.xy[2];
  blkname = world.getBlock(node.xy[0], node.xy[2], node.xy[1]).name;
  floodfill(blkname, node);
  //
  return;
}
floodfill(blockp.name, node1);
Unihedron
  • 10,902
  • 13
  • 62
  • 72
TAP
  • 25
  • 9

1 Answers1

0

Basically, it looks like you're decrementing but really you're just resetting to your default values. Take a look at this snippet here:

//
node.xy[0] = node.xy[0];
node.xy[1] = node.xy[1] + 1;
node.xy[2] = node.xy[2];
blkname = world.getBlock(node.xy[0], node.xy[2], node.xy[1]).name;
floodfill(blkname, node);
//
node.xy[0] = node.xy[0];
node.xy[1] = node.xy[1] - 1;
node.xy[2] = node.xy[2];
blkname = world.getBlock(node.xy[0], node.xy[2], node.xy[1]).name;
floodfill(blkname, node);

At first glance it seems like you're moving +1 then -1 from your original position. However, since you're modifying the reference to node.xy[1], you're really setting it to +1 then +0.

Instead you might consider something like this:

var xy0 = node.xy[0];
var xy1 = node.xy[1];
var xy2 = node.xy[2];

...

node.xy[0] = xy0 + 1;
...
node.xy[0] = xy0 - 1;

This way you're not modifying the original reference.

Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
  • Wow - thanks - that did work. Changed all the node.xy[0] and node.xy[1] and node.xy[2] to xy0,xy1,xy2 whether or not it was being incremented or decremented. seem to work perfectly. Now just need to wrap my brain around it! – TAP Dec 01 '15 at 23:11