Posted February 26, 201312 yr 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
February 26, 201312 yr https://en.wikipedia.org/wiki/Flood_fill ? if you'll implemented it using recursion don't forget to add a limit or it can crash when recursion goes to deep . mnn.getNativeLang() != English If I helped you please click on the "thank you" button.
February 26, 201312 yr Author https://en.wikipedia.org/wiki/Flood_fill ? if you'll implemented it using recursion don't forget to add a limit or it can crash when recursion goes to deep . I took a look at that but I don't totally understand how I could implement that
February 26, 201312 yr 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.
February 26, 201312 yr Author Edit: That was much simpler when you explained it in pseudo code 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); } }
February 26, 201312 yr you ended up with weirdly much code , 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.
February 26, 201312 yr Author you ended up with weirdly much code , try using loops. maybe something like this?: Thanks it worked perfectly not to self: dont set depth to 10000 and go on a flatworld thats made of the material bad things happen
February 27, 201312 yr for higher depths surely. for anyone interested read the wiki - https://en.wikipedia.org/wiki/Flood_fill#Alternative_implementations (and as said use queue not stack - with queue it should behave like BFS, with stack probably like DFS). mnn.getNativeLang() != English If I helped you please click on the "thank you" button.
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.