public void buildIslandsAndBridges(List<string> islandNames) { string prev = ""; string cur = ""; while (islandNames.Count != 0) { prev = cur; cur = islandNames[0]; islandNames.RemoveAt(0); // first island if (prev == "") { AreaNode n = new AreaNode(); n.name = cur; n.loc = new Point(0, 0); n.type = AREA_TYPE.ISLAND; IslandMap.Add(n.name, n); WorldMap.Add(n.loc, n); } else { List<Point> bridge = search(prev, cur); foreach (Point p in bridge) { AreaNode n = new AreaNode(); n.name = prev + ":" + cur; n.loc = p; n.type = AREA_TYPE.BRIDGE; //IslandMap.Add(n.name, n); WorldMap.Add(n.loc, n); } } } }
// pseudo-random search to build a bridge between two islands already on map private List<Point> search(AreaNode startIsland, AreaNode endIsland) { throw new Exception("not impled"); }
// psuedo-random search to build a bridge to a new island to be placed on map private List<Point> search(AreaNode startIsland, string destIsland) { //AreaNode destNode = new AreaNode(); //destNode.name = destIsland; SortedList<float, SearchNode> openList = new SortedList<float, SearchNode>(); SearchNode searchNode = new SearchNode(); searchNode.loc = startIsland.loc; searchNode.wt = 0; searchNode.prev = new SearchNode(); // prevents null ptr errors searchNode.prev.loc = searchNode.loc; // shouldn't affect result const int THRESHOLD = 6; while (evaluateIslandPos(searchNode.loc) + rand.Next(3) < THRESHOLD) { foreach (SearchNode x in expand(searchNode)) { openList.Add(x.wt + (float)rand.NextDouble(), x); } if (openList.Count == 0) throw new Exception("Map construction failure... not good"); searchNode = openList[openList.Keys[0]]; openList.Remove(openList.Keys[0]); } List<Point> finalPath = new List<Point>(); while (searchNode.loc != startIsland.loc) { finalPath.Insert(0, searchNode.loc); searchNode = searchNode.prev; } return finalPath; }
public List<Point> search(string startIsland, string destIsland) { if (IslandMap.ContainsKey(destIsland)) { return search(IslandMap[startIsland], IslandMap[destIsland]); } List<Point> bridgePlusDest = search(IslandMap[startIsland], destIsland); Point destPt = bridgePlusDest[bridgePlusDest.Count - 1]; bridgePlusDest.RemoveAt(bridgePlusDest.Count - 1); AreaNode n = new AreaNode(); n.name = destIsland; n.loc = destPt; n.type = AREA_TYPE.ISLAND; IslandMap.Add(n.name, n); WorldMap.Add(n.loc, n); return bridgePlusDest; }