I am using a byte array to describe the terrain in my game, each byte value represents a different block type
I created a grid to simulate the water spread, I'm giving a byte value to the amount of water on a block and then I am spreading it by increasing the value of one block which causes it to spread to blocks that have less water, I made a little video of it to show: ( value 55 is a wall, 0 air, 1 to 4 water, its a top down perspective )
https://www.youtube.com/watch?v=SOmnejfmPe0
However I am having trouble finishing it, I dont want the water level to rise above 4, when a block that is already at value 4 goes to 5 I want it to flow through all the other blocks until all the contained water is at value 4. However with my current code if I set the condition so that the water flows no matter what if it has value of 4 or more it ends in infinite loop.
here is the current relevant code:
public Vector3Int PosToCheck;
public IEnumerator FlowRoutine(int x, int y, int z)
{
PosToCheck = KeepPosInBounds(new Vector3Int(x - 1, y, z));
if ( GridArray[x,y,z] > GridArray[PosToCheck.x,PosToCheck.y,PosToCheck.z] + 1 && GridArray[PosToCheck.x, PosToCheck.y, PosToCheck.z] != 55)
{
GridArray[x, y, z] -= 1;
GridArray[PosToCheck.x, PosToCheck.y, PosToCheck.z] += 1;
yield return StartCoroutine(FlowRoutine(PosToCheck.x, PosToCheck.y, PosToCheck.z));
}
else
{
PosToCheck = KeepPosInBounds(new Vector3Int(x, y + 1, z));
if (GridArray[x,y,z] > GridArray[PosToCheck.x,PosToCheck.y,PosToCheck.z] + 1 && GridArray[PosToCheck.x, PosToCheck.y, PosToCheck.z] != 55)
{
GridArray[x, y, z] -= 1;
GridArray[PosToCheck.x, PosToCheck.y, PosToCheck.z] += 1;
yield return StartCoroutine(FlowRoutine(PosToCheck.x, PosToCheck.y, PosToCheck.z));
}
else
{
PosToCheck = KeepPosInBounds(new Vector3Int(x + 1, y, z));
if (GridArray[x,y,z] > GridArray[PosToCheck.x,PosToCheck.y,PosToCheck.z] + 1 && GridArray[PosToCheck.x, PosToCheck.y, PosToCheck.z] != 55)
{
GridArray[x, y, z] -= 1;
GridArray[PosToCheck.x, PosToCheck.y, PosToCheck.z] += 1;
yield return StartCoroutine(FlowRoutine(PosToCheck.x, PosToCheck.y, PosToCheck.z));
}
else
{
PosToCheck = KeepPosInBounds(new Vector3Int(x , y-1, z));
if (GridArray[x,y,z] > GridArray[PosToCheck.x,PosToCheck.y,PosToCheck.z] + 1 && GridArray[PosToCheck.x, PosToCheck.y, PosToCheck.z] != 55)
{
GridArray[x, y, z] -= 1;
GridArray[PosToCheck.x, PosToCheck.y, PosToCheck.z] += 1;
yield return StartCoroutine(FlowRoutine(PosToCheck.x, PosToCheck.y, PosToCheck.z));
}
}
}
}
UpdateAllButtons();
yield return null;
}
public Vector3Int KeepPosInBounds(Vector3Int newPos)
{
if (newPos.x < 0) { newPos.x = XGridSize - 1; }
if (newPos.y < 0) { newPos.y = YGridSize - 1; }
if (newPos.z < 0) { newPos.z = ZGridSize - 1; }
if (newPos.x >= XGridSize) { newPos.x = 0; }
if (newPos.y >= YGridSize) { newPos.y = 0; }
if (newPos.z >= ZGridSize) { newPos.z = 0; }
return newPos;
}
and this is the condition I was trying to add but it breaks before all available blocks are at 4, ( water gets caught in an infinite loop between 2 blocks that pass water to each other infinitely)
if ((GridArray[x,y,z] > GridArray[PosToCheck.x,PosToCheck.y,PosToCheck.z] + 1 || GridArray[x, y, z]>=WaterCap) && GridArray[PosToCheck.x, PosToCheck.y, PosToCheck.z] != 55)
Would appreciate advice on how to tackle this problem, Thanks!

