public static Task RequestTask(Dwarf dwarf) { _requestedTasksMutex.WaitOne(); // Check if previous requested task is ready if (_requestedTasks.ContainsKey(dwarf.Id)) { _requestedTasksMutex.ReleaseMutex(); return null; } else if (_readyTasks.ContainsKey(dwarf.Id) == true) { Task t = _readyTasks[dwarf.Id]; _readyTasks.Remove(dwarf.Id); _requestedTasksMutex.ReleaseMutex(); return t; } // Finds a task for the dwarf, and ques it for processing if (NumberOfOpenTasks() > 0) { _requestedTasks.Add(dwarf.Id, new TaskRequest() { TaskType = BestTaskTypeForDwarf(dwarf), Dwarf = dwarf }); } _requestedTasksMutex.ReleaseMutex(); return null; }
public void CalculatedBestPathToDwarf(Dwarf dwarf) { Path bestPath = null; if (Element.WorldMap.GetMapElement(Element.Position.X + 1, Element.Position.Y, Element.Position.Z) == null || Element.WorldMap.GetMapElement(Element.Position.X + 1, Element.Position.Y, Element.Position.Z).Solid == false) { Path path = Element.WorldMap.GetPath( new Point((int)dwarf.Position.X, (int)dwarf.Position.Y), new Point(Element.Position.X + 1, Element.Position.Y)); if (path != null) { if (bestPath == null) bestPath = path; else if (path.IsShorterThan(bestPath)) bestPath = path; } } if (Element.WorldMap.GetMapElement(Element.Position.X - 1, Element.Position.Y, Element.Position.Z) == null || Element.WorldMap.GetMapElement(Element.Position.X - 1, Element.Position.Y, Element.Position.Z).Solid == false) { Path path = Element.WorldMap.GetPath( new Point((int)dwarf.Position.X, (int)dwarf.Position.Y), new Point(Element.Position.X - 1, Element.Position.Y)); if (path != null) { if (bestPath == null) bestPath = path; else if (path.IsShorterThan(bestPath)) bestPath = path; } } if (Element.WorldMap.GetMapElement(Element.Position.X, Element.Position.Y + 1, Element.Position.Z) == null || Element.WorldMap.GetMapElement(Element.Position.X, Element.Position.Y + 1, Element.Position.Z).Solid == false) { Path path = Element.WorldMap.GetPath( new Point((int)dwarf.Position.X, (int)dwarf.Position.Y), new Point(Element.Position.X , Element.Position.Y+1)); if (path != null) { if (bestPath == null) bestPath = path; else if (path.IsShorterThan(bestPath)) bestPath = path; } } if (Element.WorldMap.GetMapElement(Element.Position.X, Element.Position.Y - 1, Element.Position.Z) == null || Element.WorldMap.GetMapElement(Element.Position.X, Element.Position.Y - 1, Element.Position.Z).Solid == false) { Path path = Element.WorldMap.GetPath( new Point((int)dwarf.Position.X, (int)dwarf.Position.Y), new Point(Element.Position.X, Element.Position.Y - 1)); if (path != null) { if (bestPath == null) bestPath = path; else if (path.IsShorterThan(bestPath)) bestPath = path; } } //if (bestPath == null) // throw new Exception("No path found"); Path = bestPath; }
private static TaskType BestTaskTypeForDwarf(Dwarf dwarf) { // TODO: Add logic return TaskType.Mining; }