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.
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
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 ();
}
}