private static void expandNode(WayPoint nextWayPoint, PathStruct currentPoint, WayPoint destination) { if (nextWayPoint != null) { //check if thie new waypoint has already been visited if (!visited.Contains(nextWayPoint.transform.position)) { visited.Add(nextWayPoint.transform.position); //calculate the total costs int totalCosts = currentPoint.cost + pathCost + manhattenDistance(destination.transform.position, nextWayPoint.transform.position); //create PathStruct PathStruct newPathStruct = new PathStruct(currentPoint.cost + pathCost, nextWayPoint, currentPoint); //add the struct to the openlist if (openList.ContainsKey(totalCosts)) { openList[totalCosts].Add(newPathStruct); } else { openList.Add(totalCosts, new List <PathStruct>() { newPathStruct }); } } else { } } else { } // else there is no waypoint upwards. }
private void Listener(PathStruct[] prefixes) { if (!HttpListener.IsSupported) { Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class."); return; } if (prefixes == null || prefixes.Length == 0) throw new ArgumentException("prefixes"); foreach (PathStruct s in prefixes) { listener.Prefixes.Add(s.Uri); } listener.Start(); Console.WriteLine("Listening..."); while (isListen) { HttpListenerContext context = listener.GetContext(); HttpListenerRequest request = context.Request; Console.WriteLine(string.Concat("Запрос к ", request.Url.ToString())); HttpListenerResponse response = context.Response; var match = ""; if (Regex.Match(context.Request.Url.ToString(), @"[\/]$").Success) match = Regex.Match(context.Request.Url.ToString(), @"(?<=[\/]).+(?=[\/]$)").Value + "/index.cs"; else match = Regex.Match(context.Request.Url.ToString(), @"(?<=http:\/).+[^\/]$").Value; var portMatch = Regex.Match(context.Request.Url.ToString(), @":\d+(?=\/)"); if (portMatch.Success) { match = match.Replace(portMatch.Value, ""); } string content = ""; byte[] buffer = new byte[0]; try { string typeMatch = Regex.Match(match, @"(?<=[.])\w+$").Value; switch (typeMatch) { case "cs": { var matches = Regex.Matches(match, @"(?<=\/)[^\/]+"); string path = ""; for (int i = 0; i < matches.Count; i++) if (!(matches.Count > 1 && (i == matches.Count - 1))) path += string.Concat(".", matches[i].Value); else path += string.Concat(".", matches[i].Value.Remove(matches[i].Value.Length - 3, 3)); var indexClass = Type.GetType(string.Concat("http_server.www", path));//TODO content = ((http_server.Page)System.Activator.CreateInstance(indexClass)).PageString(); buffer = System.Text.Encoding.UTF8.GetBytes(content); break; } case "html": { content = System.IO.File.ReadAllText(string.Concat(Environment.CurrentDirectory, "/www", match)); buffer = System.Text.Encoding.UTF8.GetBytes(content); break; } default: { buffer = System.IO.File.ReadAllBytes(string.Concat(Environment.CurrentDirectory, "/www", match)); break; } } response.ContentLength64 = buffer.Length; } catch (FileNotFoundException ex) { response.StatusCode = 404; Console.WriteLine("Не найден файл " + ex.FileName); } catch (DirectoryNotFoundException ex) { response.StatusCode = 404; Console.WriteLine(ex.StackTrace); } catch (ArgumentNullException ex) { response.StatusCode = 404; Console.WriteLine(ex.StackTrace); } System.IO.Stream output = response.OutputStream; try { output.Write(buffer, 0, buffer.Length); } catch(HttpListenerException ex) { response.StatusCode = 404; Console.WriteLine(ex.StackTrace); } finally { output.Close(); } } listener.Stop(); }
//destination: der ZielPunkt (Pacmans Position) //start: aktuelle Positionn des Geistes //die zurückgegebene Liste soll als ersten Wegpunkt den Startpunkt und als letzten Wegpunkt den //Zielpunkt enthalten. public static List <WayPoint> getPath(WayPoint destination, WayPoint start) { List <WayPoint> path = new List <WayPoint>(); //initialize int cost = 0; int heuristicValue = manhattenDistance(destination.transform.position, start.transform.position); //List of visited positions. Needed to ensure we don't revisit places. visited = new List <Vector3>(); //using the key as the cost + heuristic value. Value is a struct with the waypoint and the path to this one. openList = new SortedDictionary <int, List <PathStruct> >(); //insert the starting point PathStruct startPath = new PathStruct(cost, start, null); List <PathStruct> startList = new List <PathStruct>(); startList.Add(startPath); openList.Add((cost + heuristicValue), startList); bool finished = false; PathStruct currentPoint = startPath; //the loop while (!finished && openList.Count > 0) { //get all waypoints with lowest total cost List <PathStruct> lowestCostList = new List <PathStruct>(openList.First().Value); openList.Remove(openList.First().Key); //loop over these points for (int i = 0; i < lowestCostList.Count; i++) { //get the first one currentPoint = lowestCostList[i]; //did we reach the goal? if (currentPoint.wayPoint.transform.position.Equals(destination.transform.position)) { finished = true; break; } else { //try to expand it in all four directions: //up: WayPoint nextWayPoint = currentPoint.wayPoint.upWaypoint; expandNode(nextWayPoint, currentPoint, destination); //down nextWayPoint = currentPoint.wayPoint.downWaypoint; expandNode(nextWayPoint, currentPoint, destination); //left: nextWayPoint = currentPoint.wayPoint.leftWaypoint; expandNode(nextWayPoint, currentPoint, destination); //right: nextWayPoint = currentPoint.wayPoint.rightWaypoint; expandNode(nextWayPoint, currentPoint, destination); } } } if (finished) { while (currentPoint.predecessor != null) { path.Insert(0, currentPoint.wayPoint); currentPoint = currentPoint.predecessor; } path.Insert(0, currentPoint.wayPoint); } return(path); }
public PathStruct(int cost, WayPoint wayPoint, PathStruct predecessor) { this.cost = cost; this.wayPoint = wayPoint; this.predecessor = predecessor; }