protected override TKey LookUp(long id) { if (!dict.ContainsKey(id)) { dict[id] = (TKey)Galaxy.Current.GetReferrable(id); } return(dict[id]); }
protected override TKey LookUp(string id) { if (!dict.ContainsKey(id)) { dict[id] = (TKey)Mod.Current.Find <TKey>(id); } return(dict[id]); }
/// <summary> /// Connects two nodes. /// </summary> /// <param name="start"></param> /// <param name="end"></param> /// <param name="twoWay">Connect both ways?</param> public void Connect(T start, T end, bool twoWay = false) { if (!connections.ContainsKey(start) || !connections[start].Contains(end)) { var sub1 = Subgraphs.Single(s => s.Contains(start)); var sub2 = Subgraphs.Single(s => s.Contains(end)); if (sub1 != sub2) { var sub3 = new ConnectivityGraph <T>(sub1, sub2); subgraphs.Remove(sub1); subgraphs.Remove(sub2); subgraphs.Add(sub3); } connections[start].Add(end); singletons.Remove(start); singletons.Remove(end); } if (twoWay) { Connect(end, start, false); } }
public static IEnumerable <PathfinderNode <T> > CreateDijkstraMap <T>(T start, T end, ConnectivityGraph <T> graph) { // pathfind! // step 1: empty priority queue with cost to reach each node var queue = new List <PathfinderNode <T> >(); // step 2: empty set of previously visited nodes, along with costs and previous-node references var visited = new SafeDictionary <T, PathfinderNode <T> >(); // step 3: add start node and cost queue.Add(new PathfinderNode <T>(start, 0, null)); // step 4: quit if there are no nodes (all paths exhausted without finding goal) bool success = false; while (queue.Any() && !success) { // step 5: take lowest cost node out of queue // also prefer straight line movement to diagonal var minCost = queue.Min(n => n.Cost); var node = queue.Where(n => n.Cost == minCost).First(); queue.Remove(node); // step 6: if node is the goal, stop - success! if (node.Location.Equals(end)) { success = true; } // step 7: check possible moves var moves = graph.GetExits(node.Location); // step 7a: remove blocked points (aka calculate cost) // nothing to do here // step 7b: update priority queue foreach (var move in moves) { if (!visited.ContainsKey(move)) { // didn't visit yet var newnode = new PathfinderNode <T>(move, node.Cost + 1, node); queue.Add(newnode); visited.Add(move, newnode); } else { // already visited - but is it by a longer path? var items = queue.Where(n => n.Location.Equals(move) && n.Cost > node.Cost + 1); if (items.Any()) { foreach (var old in items.ToArray()) { queue.Remove(old); } var newnode = new PathfinderNode <T>(move, node.Cost + 1, node); queue.Add(newnode); visited.Add(move, newnode); } } } } return(visited.Values); }