diff --git a/cw4/Ant.cs b/cw4/Ant.cs index a03a6fb2..adb944b6 100644 --- a/cw4/Ant.cs +++ b/cw4/Ant.cs @@ -9,6 +9,7 @@ public class Ant : MonoBehaviour public sbyte foodSearch = -1; // 1 it has food, -1 it searches for food public VoxelSpace voxelSpace; + private static readonly System.Random random = new System.Random(); public void sniff() { @@ -23,17 +24,22 @@ public class Ant : MonoBehaviour hasFood = false; foodSearch = -1; } - else if (voxelSpace.positionInVoxelSpace(transform.position).x == voxelSpace.sizeX - 1 || voxelSpace.positionInVoxelSpace(transform.position).z == voxelSpace.sizeY - 1) + else if ( + voxelSpace.positionInVoxelSpace(transform.position).x == voxelSpace.sizeX - 1 + || voxelSpace.positionInVoxelSpace(transform.position).z == voxelSpace.sizeY - 1) { foodSearch = 1; } - var pos = voxelSpace.positionInVoxelSpace(transform.position); - if ((foodSearch == -1 && pos.x < voxelSpace.voxels.GetLength(0) -1 && pos.z < voxelSpace.voxels.GetLength(1) -1 && voxelSpace.voxels[pos.x+1, pos.z] == voxelSpace.voxels[pos.x, pos.z+1]) || (foodSearch == 1 && pos.x > 0 && pos.z > 0 && voxelSpace.voxels[pos.x-1, pos.z] == voxelSpace.voxels[pos.x, pos.z-1])) + Debug.Log(pos); + var nextX = pos.x < voxelSpace.sizeX - 1 ? voxelSpace.voxels[pos.x + 1, pos.z] : -1; + var nextZ = pos.z < voxelSpace.sizeY - 1 ? voxelSpace.voxels[pos.x, pos.z + 1] : -1; + var previousX = pos.x > 0 ? voxelSpace.voxels[pos.x - 1, pos.z] : -1; + var previuosZ = pos.z > 0 ? voxelSpace.voxels[pos.x, pos.z - 1] : -1; + if ((foodSearch == -1 && nextX == nextZ) || (foodSearch == 1 && previousX == previuosZ)) { - var random = new System.Random(); - var next = random.Next(0, 1); - if (next == 0) + var next = random.Next(0, 2); + if (next == 0) { turnHorizontaly(); } @@ -42,18 +48,18 @@ public class Ant : MonoBehaviour turnVerticaly(); } } - else if ((foodSearch == -1 && pos.x < voxelSpace.voxels.GetLength(0) -1 && pos.z < voxelSpace.voxels.GetLength(1) -1 && voxelSpace.voxels[pos.x+1, pos.z] > voxelSpace.voxels[pos.x, pos.z+1]) || (foodSearch == 1 && pos.x > 0 && pos.z > 0 && voxelSpace.voxels[pos.x-1, pos.z] < voxelSpace.voxels[pos.x, pos.z-1])) - { - turnVerticaly(); - } - else if ((foodSearch == -1 && pos.x < voxelSpace.voxels.GetLength(0) -1 && pos.z < voxelSpace.voxels.GetLength(1) -1 && voxelSpace.voxels[pos.x+1, pos.z] < voxelSpace.voxels[pos.x, pos.z+1]) || (foodSearch == 1 && pos.x > 0 && pos.z > 0 && voxelSpace.voxels[pos.x-1, pos.z] > voxelSpace.voxels[pos.x, pos.z-1])) - { - turnVerticaly(); - } - if (hasFood) - { - voxelSpace.voxels[pos.x, pos.z] = (byte)(voxelSpace.voxels[pos.x, pos.z] + 40); - } + else if ((foodSearch == -1 && nextX > nextZ) || (foodSearch == 1 && previousX < previuosZ)) + { + turnHorizontaly(); + } + else if ((foodSearch == -1 && nextX < nextZ) || (foodSearch == 1 && previousX > previuosZ)) + { + turnVerticaly(); + } + if (hasFood) + { + voxelSpace.voxels[pos.x, pos.z] = (byte)(voxelSpace.voxels[pos.x, pos.z] + 40); + } go(); } diff --git a/cw4/VoxelSpace.cs b/cw4/VoxelSpace.cs index 905ab544..ba16a956 100644 --- a/cw4/VoxelSpace.cs +++ b/cw4/VoxelSpace.cs @@ -101,7 +101,7 @@ public class VoxelSpace : MonoBehaviour frame = 0; setTableScale(); createVoxelSpace(); - // placeFood(); + placeFood(); addAnts(); } @@ -109,13 +109,6 @@ public class VoxelSpace : MonoBehaviour // Update is called once per frame void Update() { - Debug.Log("max=" + maxVoxel()); - Debug.Log("start: " + (new Vector3(3.5f, 5.5f, 4.5f))); - Debug.Log("midle: " + (positionInVoxelSpace(new Vector3(3.5f, 5.5f, 4.5f)))); - Debug.Log("endin: " + (positionInWorld(positionInVoxelSpace(new Vector3(3.5f, 5.5f, 4.5f))))); - Debug.Log("zeroo: " + (positionInWorld(new Vector3(0, 0, 0)))); - - if (frame % animationFrames == 0) { foreach (Ant ant in ants) @@ -159,8 +152,6 @@ public class VoxelSpace : MonoBehaviour var a = positionInWorld(new Vector3(i, 0, j)); var b = positionInVoxelSpace(a); var c = getPheromoneAt(a.x, a.z); - - Debug.Log(i + "x" + j + "=" + m + " posW " + a + " posV " + b + " phs " + c); } } } diff --git a/cw4/export/voxelSpace_Data/Managed/Assembly-CSharp.dll b/cw4/export/voxelSpace_Data/Managed/Assembly-CSharp.dll index 651ce1cb..55ccbd80 100644 Binary files a/cw4/export/voxelSpace_Data/Managed/Assembly-CSharp.dll and b/cw4/export/voxelSpace_Data/Managed/Assembly-CSharp.dll differ diff --git a/cw4/export/voxelSpace_Data/globalgamemanagers b/cw4/export/voxelSpace_Data/globalgamemanagers index 79d689d3..aee6322e 100644 Binary files a/cw4/export/voxelSpace_Data/globalgamemanagers and b/cw4/export/voxelSpace_Data/globalgamemanagers differ diff --git a/cw4/export/voxelSpace_Data/globalgamemanagers.assets b/cw4/export/voxelSpace_Data/globalgamemanagers.assets index 77c04f79..276bd2ec 100644 Binary files a/cw4/export/voxelSpace_Data/globalgamemanagers.assets and b/cw4/export/voxelSpace_Data/globalgamemanagers.assets differ