public int getManhattan(SparseGrid.Voxel source, SparseGrid.Voxel target) { int manhattan = 0; int X = (int)(target.Center.x - source.Center.x); int Y = (int)(target.Center.y - source.Center.y); int Z = (int)(target.Center.z - source.Center.z); manhattan = X + Y + Z; return(manhattan); }
//apply once upon instantiation public void AppendGO(SparseGrid.Voxel voxel) { if ((voxel.Index.x + voxel.Index.y + voxel.Index.z) % 2 == 0) { GameObject go = Instantiate(_A, voxel.Center, Quaternion.identity); voxel.DisplayCell = go; voxel.SwitchOff(); } else { GameObject go = Instantiate(_B, voxel.Center, Quaternion.identity); voxel.DisplayCell = go; voxel.SwitchOff(); } }
//gets the index of the best neighbour public bool SecondBestMove(SparseGrid.Voxel voxel, out Vector3Int outIndex) { var neighbours = voxel.GetFaceNeighbours().Where(n => n.On == false); Vector3Int target = new Vector3Int(0, 0, 0); if (voxel.Job != null) { if (voxel.Job.indices.Count > 0 || voxel.Job.indices != null) { target = voxel.Job.indices[0]; } else { target = hqBestVacant; } } outIndex = new Vector3Int(); bool worked = false; if (grid.Voxels.TryGetValue(target, out var end)) { var neighRanking = new List <VoxelScore>(); foreach (var n in neighbours) { neighRanking.Add(new VoxelScore(n.Index, getManhattan(n, end))); } var sortedRank = neighRanking.OrderBy(s => s.score); var secondBest = sortedRank.First().index; outIndex = secondBest; worked = true; } return(worked); }
//checks to see if there is an empty space below, if so, it moves there public void CheckBottom(SparseGrid.Voxel voxel) { if (voxel.AtWork == false) { var neighbourLow = voxel.GetBottomNeighbour(); if (neighbourLow != null) { if (neighbourLow.On == false) { neighbourLow.SwitchOn(); neighbourLow.Job = voxel.Job; neighbourLow.AtWork = voxel.AtWork; neighbourLow.Idle = voxel.Idle; neighbourLow.Commute = voxel.Commute; voxel.SwitchOff(); voxel.Job = null; voxel.AtWork = false; voxel.Idle = false; voxel.Commute = false; } } } }
public IEnumerator MoveDudes() { for (int c = 0; c < 100000; c++) { UpdateLandlocked(); if (!((grid.GetVoxels().Where(v => v.On).Where(v => v.IsMovable).Where(v => v.Commute) == null || grid.GetVoxels().Where(v => v.On).Where(v => v.IsMovable).Where(v => v.Commute).Count() == 0))) { var opVox = grid.GetVoxels().Where(v => v.On).Where(v => v.IsMovable).Where(v => v.Commute); var opVoxCount = opVox.Count(); var index = Random.Range(0, opVoxCount - 1); var start = opVox.ElementAt(index); Vector3Int endIndex = new Vector3Int(); if (start.Job == null || start.Job.indices.Count < 1) { endIndex = hqBestVacant; } else { endIndex = start.Job.indices[0]; } if (grid.Voxels.TryGetValue(endIndex, out var end)) { var shortest = graph.ShortestPathsDijkstra(_ => 1, start); SparseGrid.Voxel next = null; List <SparseGrid.Voxel> pathList = new List <SparseGrid.Voxel>(); if (shortest(end, out var path)) { var current = start; foreach (var edge in path) { current = edge.GetOtherVertex(current); pathList.Add(current); } var indNex = pathList.Count() <= 10 ? pathList.Count() - 1 : (int)(pathList.Count() * 0.5f); if (indNex <= 10) { next = end; } else { next = pathList.ElementAt(indNex); } } else { Debug.Log("shortest path not found"); continue; } if (next.On == false) { next.SwitchOn(); next.Job = start.Job; next.AtWork = start.AtWork; next.Idle = start.Idle; next.Commute = start.Commute; start.SwitchOff(); start.Job = null; start.AtWork = false; start.Idle = false; start.Commute = false; } UpdateEmptyHQIndex(); UpdateBottoms(); UpdateArmyVacancyRate(); } } yield return(new WaitForSeconds(0.0001f)); } }