stackoverflow, minecraft terrain, finding neightbours

So… I have map like minecraft, but im making three dimension strategy game. Game start and generate map. Then player can change where he want his base. When player set his first base, its meant that the game will make area around base flat. And there is a problem. :slight_smile:

public World w;
public bool CanGoDown;
public int UpLevel = 0;

There is code variables.
-World w is base code of world.
-I will explain other later.

public Vector3[] GetBlockNeighbours(int x, int y, int z, bool OnlyUp){

		List<Vector3> delete = new List<Vector3> ();

		if (!OnlyUp) {
			if (w.GetBlock (x, y - 1, z - 1).GetType () == typeof(BlockAir) ||
			    w.GetBlock (x - 1, y - 1, z - 1).GetType () == typeof(BlockAir) ||
			    w.GetBlock (x - 1, y - 1, z).GetType () == typeof(BlockAir) ||
			    w.GetBlock (x - 1, y - 1, z + 1).GetType () == typeof(BlockAir) ||
			    w.GetBlock (x, y - 1, z + 1).GetType () == typeof(BlockAir) ||
			    w.GetBlock (x + 1, y - 1, z + 1).GetType () == typeof(BlockAir) ||
			    w.GetBlock (x + 1, y - 1, z).GetType () == typeof(BlockAir) ||
			    w.GetBlock (x + 1, y - 1, z - 1).GetType () == typeof(BlockAir)) {

				if (CanGoDown == false) {
					return delete.ToArray ();
				}

				Vector3[] pos = MakeNewRequest (x, y - 1, z, false);
				if (pos.Length != 0) {
					foreach (Vector3 p in pos) {
						delete.Add (p);
					}
				}
				delete.Add (new Vector3 (x, y, z));

			} else

This is the part game call from World script!
So… If there is no ground next where player want but his base the code will go one level down.
If it go down Code will call MakeNewRequest(…)…

Vector3[] MakeNewRequest(int x, int y, int z, bool OnlyUp){
		GrindMapBase g = new GrindMapBase ();
		Vector3[] pos;
		g.w = w;
		g.CanGoDown = false;
		if (OnlyUp)
			g.UpLevel += 1;
		if (UpLevel < 4)
			pos = g.GetBlockNeighbours (x, y, z, OnlyUp);
		else
			pos = new Vector3[0];

		return pos;

	}

So now CanGoDown is false, and next time if code find no ground next to base, code return delete.ToArray(), And i think the delete will be empty. UpLevel wont yeat rise, becouse (bool) OnlyUp was false. If code find ground next to base, code will skip going down part and it start really finding pieces next to it.

else {
				if (w.GetBlock (x, y, z - 1).GetType () != typeof(BlockAir)) {
					delete.Add (new Vector3 (x, y, z - 1));
				}
				Vector3[] pos = MakeNewRequest (x, y, z - 1, true);
				if (pos.Length != 0) {
					foreach (Vector3 p in pos) {
						delete.Add (p);
					}
				}
				if (w.GetBlock (x - 1, y, z - 1).GetType () != typeof(BlockAir)) {
					delete.Add (new Vector3 (x - 1, y, z - 1));
				}
				pos = MakeNewRequest (x - 1, y, z - 1, true);
				if (pos.Length != 0) {
					foreach (Vector3 p in pos) {
						delete.Add (p);
					}
				}

Now every time code find neighbour code will add it in to delete.(). After that code will call MakeNewRequest(…), but this time OnlyUp(boolean) is true.

So now UpLevel int will grow and MakeNewRequest just cancel making loop when UpLevel is 3…

And end of Code

return delete.ToArray ();

Stackoverflow error say error is when i call GetBlockNeighbours from MakeNewRequest.

And sorry my english skills :slight_smile:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GrindMapBase{

	public World w;
	public bool CanGoDown;
	public int UpLevel = 0;

	Vector3[] MakeNewRequest(int x, int y, int z, bool OnlyUp){
		GrindMapBase g = new GrindMapBase ();
		Vector3[] pos;
		g.w = w;
		g.CanGoDown = false;
		if (OnlyUp)
			g.UpLevel += 1;
		if (UpLevel < 4)
			pos = g.GetBlockNeighbours (x, y, z, OnlyUp);
		else
			pos = new Vector3[0];

		return pos;

	}

	public Vector3[] GetBlockNeighbours(int x, int y, int z, bool OnlyUp){

		List<Vector3> delete = new List<Vector3> ();

		if (!OnlyUp) {
			if (w.GetBlock (x, y - 1, z - 1).GetType () == typeof(BlockAir) ||
			    w.GetBlock (x - 1, y - 1, z - 1).GetType () == typeof(BlockAir) ||
			    w.GetBlock (x - 1, y - 1, z).GetType () == typeof(BlockAir) ||
			    w.GetBlock (x - 1, y - 1, z + 1).GetType () == typeof(BlockAir) ||
			    w.GetBlock (x, y - 1, z + 1).GetType () == typeof(BlockAir) ||
			    w.GetBlock (x + 1, y - 1, z + 1).GetType () == typeof(BlockAir) ||
			    w.GetBlock (x + 1, y - 1, z).GetType () == typeof(BlockAir) ||
			    w.GetBlock (x + 1, y - 1, z - 1).GetType () == typeof(BlockAir)) {

				if (CanGoDown == false) {
					return delete.ToArray ();
				}

				Vector3[] pos = MakeNewRequest (x, y - 1, z, false);
				if (pos.Length != 0) {
					foreach (Vector3 p in pos) {
						delete.Add (p);
					}
				}
				delete.Add (new Vector3 (x, y, z));

			} else {
				if (w.GetBlock (x, y, z - 1).GetType () != typeof(BlockAir)) {
					delete.Add (new Vector3 (x, y, z - 1));
				}
				Vector3[] pos = MakeNewRequest (x, y, z - 1, true);
				if (pos.Length != 0) {
					foreach (Vector3 p in pos) {
						delete.Add (p);
					}
				}
				if (w.GetBlock (x - 1, y, z - 1).GetType () != typeof(BlockAir)) {
					delete.Add (new Vector3 (x - 1, y, z - 1));
				}
				pos = MakeNewRequest (x - 1, y, z - 1, true);
				if (pos.Length != 0) {
					foreach (Vector3 p in pos) {
						delete.Add (p);
					}
				}
				if (w.GetBlock (x - 1, y, z).GetType () != typeof(BlockAir)) {
					delete.Add (new Vector3 (x - 1, y, z));
				}
				pos = MakeNewRequest (x - 1, y, z, true);
				if (pos.Length != 0) {
					foreach (Vector3 p in pos) {
						delete.Add (p);
					}
				}
				if (w.GetBlock (x - 1, y, z + 1).GetType () != typeof(BlockAir)) {
					delete.Add (new Vector3 (x - 1, y, z + 1));
				}
				pos = MakeNewRequest (x - 1, y, z + 1, true);
				if (pos.Length != 0) {
					foreach (Vector3 p in pos) {
						delete.Add (p);
					}
				}
				if (w.GetBlock (x, y, z + 1).GetType () != typeof(BlockAir)) {
					delete.Add (new Vector3 (x, y, z + 1));
				}
				pos = MakeNewRequest (x, y, z + 1, true);
				if (pos.Length != 0) {
					foreach (Vector3 p in pos) {
						delete.Add (p);
					}
				}
				if (w.GetBlock (x + 1, y, z + 1).GetType () != typeof(BlockAir)) {
					delete.Add (new Vector3 (x + 1, y, z + 1));
				}
				pos = MakeNewRequest (x + 1, y, z + 1, true);
				if (pos.Length != 0) {
					foreach (Vector3 p in pos) {
						delete.Add (p);
					}
				}
				if (w.GetBlock (x + 1, y, z).GetType () != typeof(BlockAir)) {
					delete.Add (new Vector3 (x + 1, y, z));
				}
				pos = MakeNewRequest (x + 1, y, z, true);
				if (pos.Length != 0) {
					foreach (Vector3 p in pos) {
						delete.Add (p);
					}
				}
				if (w.GetBlock (x + 1, y - 1, z).GetType () != typeof(BlockAir)) {
					delete.Add (new Vector3 (x + 1, y - 1, z));
				}
				pos = MakeNewRequest (x + 1, y, z - 1, true);
				if (pos.Length != 0) {
					foreach (Vector3 p in pos) {
						delete.Add (p);
					}
				}

			}
		} else {
			if (w.GetBlock (x, y + 1, z).GetType () != typeof(BlockAir)) {
				delete.Add (new Vector3(x, y + 1, z));
			}
			Vector3[] pos = MakeNewRequest (x, y + 1, z, true);
			if (pos.Length != 0) {
				foreach (Vector3 p in pos) {
					delete.Add (p);
				}
			}
		}

		return delete.ToArray ();
	}

}

It’s hart do understand what your code acutally does or should do. Flatten the terrain in a certain area is just a couple of loops and block checks which can be done all in one method as long as you know the shape / size of your base. I can’t really follow your logic here. If “UpOnly” is false you only check the neighbors that are below the block except the block directly below.

Since you do a recursive search here, you would need a way to remember which blocks you have already checked. Though it doesn’t seem you have anything like that.

Also keep in mind that here:

     if (OnlyUp)
         g.UpLevel += 1;
     if (UpLevel < 4)

You “raise” UpLevel in your newly created “GrindMapBase” but you are checking UpLevel from the current GrindMapBase instance. When you create a new GrindMapBase you don’t seem to initialize UpLevel, so it would starts with 0 in each new instance you create. If that’s the only place where you increase the UpLevel, it will never be greater than “1” in each GrindMapBase instance.

Since we don’t really understand what this code should do in the end i would suggest you rethink the task you’re trying to complete here.

Apart from that you should always name your methods after what they actually do. GetBlockNeighbours does not simply return the neighbors of the block you pass as parameter but returns other blocks as well since you do a recursive search through the neighbors as well. So the method name is misleading. “MakeNewRequest” is also a bad name since it doesn’t say anything about want it actually does. It’s like naming it “Do()” or “A()” ^^. Most IDEs have refactoring tools for renaming methods easily. It’s often the case that you have to rename a method or class a couple of times as it evolves when the concept isn’t clear from the start.