/// <summary> /// Find path. /// </summary> public static List <Field> FindPath(WorldManager worldManager, Field startField, Field destinationField, bool isCrew, CrewStation ownCrewStation) { List <Field> path = new List <Field>(); List <Field> FieldsTocheck = new List <Field>(); Dictionary <Field, float> costDictionary = new Dictionary <Field, float>(); Dictionary <Field, float> priorityDictionary = new Dictionary <Field, float>(); Dictionary <Field, Field> parentsDictionary = new Dictionary <Field, Field>(); FieldsTocheck.Add(startField); priorityDictionary.Add(startField, 0); costDictionary.Add(startField, 0); parentsDictionary.Add(startField, null); while (FieldsTocheck.Count > 0) { Field currentField = GetClosestVertex(FieldsTocheck, priorityDictionary); FieldsTocheck.Remove(currentField); if (currentField.Equals(destinationField)) { path = GeneratePath(parentsDictionary, currentField); return(path); } foreach (Field walkabledjacentField in worldManager.GetWalkableAdjacentFields(currentField, isCrew, ownCrewStation)) { float newCost = costDictionary[currentField] + worldManager.GetCostOfEnteringField(walkabledjacentField); if (!costDictionary.ContainsKey(walkabledjacentField) || newCost < costDictionary[walkabledjacentField]) { costDictionary[walkabledjacentField] = newCost; float priority = newCost + GetManhattanDistance(destinationField, walkabledjacentField); FieldsTocheck.Add(walkabledjacentField); priorityDictionary[walkabledjacentField] = priority; parentsDictionary[walkabledjacentField] = currentField; } } } return(path); }
/// <summary> /// Set crew station. /// </summary> public void SetCrewStation(CrewStation crewStation) { this.crewStation = crewStation; }