Jump to content

[solved]iterating over all connected blocks


jordan30001

Recommended Posts

I am having trouble Iterating over all connected blocks of the same block/material

 

How would I go about doing this?

 

Here is my failed attempt at doing what I want (Only gets blocks from y - highest y block)

 

harvests in +x, y, z only

 

https://www.dropbox.com/s/u9b8i307ki4xccl/iteration.png

http://paste.minecraftforge.net/view/f5083f46

Link to comment
Share on other sites

I'll try write it in pseudo-code:

void floodFill(coordinates c, int depth){
  if depth > 50 then return;

  for each neighbour{
    if neighbour is blockIron{
      floodFill(neighbour's coords, depth+1);
    }
  }

  // iron block processing
  replace block at my coordinates (param c)
}

and this method would be called with coordinates of the first iron block (in real implementation you'll probably need more parameters to do the replacement, like passing an instance of World) and zero depth.

mnn.getNativeLang() != English

If I helped you please click on the "thank you" button.

Link to comment
Share on other sites

Edit: That was much simpler when you explained it in pseudo code :D

 

Here is my current code: (how would I expand this to the 3D(y coordinate) plane?

 

public static void floodFill(World w, int x, int y, int z, EntityPlayer p, int depth)//depth starts at 0
{

	  if(depth > 50) return;

	  if(w.getBlockMaterial(x + 1, y, z) == Material.wood)//east + 1
	  {
		  doEntityDropAndDestroyBlock(p, w, x + 1, y, z);
		  w.setBlock(x + 1, y, z, 0);
		  floodFill(w,x + 1,y,z,p,depth + 1);
	  }
	  if(w.getBlockMaterial(x - 1, y, z) == Material.wood) //south - 1
	  {
		  doEntityDropAndDestroyBlock(p, w, x - 1, y, z);
		  w.setBlock(x - 1, y, z, 0);
		  floodFill(w,x-1,y,z,p,depth + 1);
	  }
	  if(w.getBlockMaterial(x, y, z + 1) == Material.wood)//North + 1
	  {
		  doEntityDropAndDestroyBlock(p, w, x, y, z + 1);
		  w.setBlock(x, y, z + 1, 0);
		  floodFill(w,x,y,z+1,p,depth + 1);
	  }
	  if(w.getBlockMaterial(x, y, z - 1) == Material.wood) //north - 1
	  {
		  doEntityDropAndDestroyBlock(p, w, x, y, z - 1);
		  w.setBlock(x, y, z - 1, 0);
		  floodFill(w,x,y,z-1,p,depth + 1);
	  }
}

 

Edit: I tried the following code but it didn't full work


public static void floodFill(World w, int x, int y, int z, EntityPlayer p, int depth)//depth starts at 0
{

	  if(depth > 900) return;

	  if(w.getBlockMaterial(x + 1, y, z) == Material.wood)//east + 1
	  {
		  doEntityDropAndDestroyBlock(p, w, x + 1, y, z);
		  w.setBlock(x + 1, y, z, 0);
		  if(w.getBlockMaterial(x + 1, y + 1, z) == Material.wood)//east + 1
		  {
			  doEntityDropAndDestroyBlock(p, w, x, y + 1, z);
			  w.setBlock(x + 1, y + 1, z, 0);
			  floodFill(w, x + 1, y + 1, z, p,depth + 1);
		  }
		  if(w.getBlockMaterial(x + 1, y - 1, z) == Material.wood)//east + 1
		  {
			  doEntityDropAndDestroyBlock(p, w, x, y - 1, z);
			  w.setBlock(x + 1, y - 1, z, 0);
			  floodFill(w, x + 1, y - 1, z, p,depth + 1);
		  }
		  floodFill(w,x + 1,y,z,p,depth + 1);
	  }
	  if(w.getBlockMaterial(x - 1, y, z) == Material.wood) //south - 1
	  {
		  doEntityDropAndDestroyBlock(p, w, x - 1, y, z);
		  w.setBlock(x - 1, y, z, 0);
		  if(w.getBlockMaterial(x - 1, y + 1, z) == Material.wood)//east + 1
		  {
			  doEntityDropAndDestroyBlock(p, w, x - 1, y + 1, z);
			  w.setBlock(x - 1, y + 1, z, 0);
			  floodFill(w, x - 1, y + 1, z, p,depth + 1);
		  }
		  if(w.getBlockMaterial(x - 1, y - 1, z) == Material.wood)//east + 1
		  {
			  doEntityDropAndDestroyBlock(p, w, x, y - 1, z);
			  w.setBlock(x, y - 1, z, 0);
			  floodFill(w, x, y - 1, z, p,depth + 1);
		  }
		  floodFill(w,x-1,y,z,p,depth + 1);
	  }
	  if(w.getBlockMaterial(x, y, z + 1) == Material.wood)//North + 1
	  {
		  doEntityDropAndDestroyBlock(p, w, x, y, z + 1);
		  w.setBlock(x, y, z + 1, 0);
		  if(w.getBlockMaterial(x, y + 1, z) == Material.wood)//east + 1
		  {
			  doEntityDropAndDestroyBlock(p, w, x, y + 1, z + 1);
			  w.setBlock(x, y + 1, z + 1, 0);
			  floodFill(w, x, y + 1, z + 1, p,depth + 1);
		  }
		  if(w.getBlockMaterial(x, y - 1, z) == Material.wood)//east + 1
		  {
			  doEntityDropAndDestroyBlock(p, w, x, y - 1, z + 1);
			  w.setBlock(x, y - 1, z + 1, 0);
			  floodFill(w, x, y - 1, z + 1, p,depth + 1);
		  }
		  floodFill(w,x,y,z+1,p,depth + 1);
	  }
	  if(w.getBlockMaterial(x, y, z - 1) == Material.wood) //north - 1
	  {
		  doEntityDropAndDestroyBlock(p, w, x, y, z - 1);
		  w.setBlock(x, y, z - 1, 0);
		  if(w.getBlockMaterial(x, y + 1, z) == Material.wood)//east + 1
		  {
			  doEntityDropAndDestroyBlock(p, w, x, y + 1, z - 1);
			  w.setBlock(x, y + 1, z - 1, 0);
			  floodFill(w, x, y + 1, z - 1, p,depth + 1);
		  }
		  if(w.getBlockMaterial(x, y - 1, z) == Material.wood)//east + 1
		  {
			  doEntityDropAndDestroyBlock(p, w, x, y - 1, z - 1);
			  w.setBlock(x, y - 1, z - 1, 0);
			  floodFill(w, x, y - 1, z - 1, p,depth + 1);
		  }
		  floodFill(w,x,y,z-1,p,depth + 1);
	  }
}

Link to comment
Share on other sites

you ended up with weirdly much code :D, try using loops.

maybe something like this?:

floodFill(World w, int x, int y, int z, EntityPlayer p, int depth){ //
if(depth > 50) return;
for(int sx=-1;sx<=1;sx++){
  for(int sy=-1;sy<=1;sy++){
    for(int sz=-1;sz<=1;sz++){
      int nx=x+sx, ny=y+sy, nz=z+sz;
      if(w.getBlockMaterial(nx, ny, nz) == Material.wood){
        doEntityDropAndDestroyBlock(p, w, nx, ny, nz);
        w.setBlock(nx, ny, nz, 0);
        floodFill(w,nx, ny, nz,p,depth + 1);
      }
    }
  }
}
} //

mnn.getNativeLang() != English

If I helped you please click on the "thank you" button.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.