public static bool Equals(Starlane starlaneA, Starlane starlaneB) { if (starlaneA.planetA.transform.position == starlaneB.planetA.transform.position && starlaneA.planetB.transform.position == starlaneB.planetB.transform.position) { return(true); } else { return(false); } }
// Use this for initialization public static Starlane Create(Planet planetA, Planet planetB) { GameObject obj = new GameObject("starlane", typeof(Starlane)); LineRenderer lr = obj.AddComponent <LineRenderer>(); lr.material = new Material(Shader.Find("Particles/Additive")); lr.startWidth = 0.35f; lr.endWidth = 0.35f; Starlane starlane = obj.GetComponent <Starlane>(); starlane.planetA = planetA; starlane.planetB = planetB; starlane.distance = Vector3.Distance(planetA.transform.position, planetB.transform.position); return(starlane); }
public static Route SolveForPlanetRoute(Planet start, Planet end) { List <Planet> routeFinal = new List <Planet>(); List <Node> closedList = new List <Node>(); List <Node> frontier = new List <Node>(); bool goalAchieved = false; Planet goal = end; Node startNode = new Node(start); Node currentNode = startNode; int iterations = 0; while (!goalAchieved) { if (frontier.Contains(currentNode)) { frontier.Remove(currentNode); } if (!currentNode.expanded) { frontier = SortFrontier(currentNode.Expand(), frontier); currentNode.expanded = true; } closedList.Add(currentNode); currentNode = GetBestNode(currentNode, closedList, goal); goalAchieved = GoalCheck(currentNode.planet, goal) ? true : false; iterations++; if (iterations > 50) //ToDo this needs to adjust for seriously bigger map sizes, for most sizes around 10 planets it works fine { Debug.Log("<GBFS Could Not Find Route!!!>"); //We couldn't find a connection to the goal planet so we should try to create one Starlane starlane = Starlane.Create(currentNode.planet, goal); currentNode.planet.starlanes.Add(starlane); goal.starlanes.Add(starlane); //Throw an error here break; } } //BuildRoute(); return(GetRoute(currentNode)); }
void GenerateLanes() { foreach (Node node in graph.Vertices) { IOrderedEnumerable <Node> orderedNodes = graph.Vertices.OrderBy(nodeToOrder => CartezianPosition.CalculateDistance(node.position, nodeToOrder.position)); IEnumerator <Node> enumeratorNodes = orderedNodes.GetEnumerator(); enumeratorNodes.MoveNext(); enumeratorNodes.MoveNext(); if (CartezianPosition.CalculateDistance(enumeratorNodes.Current.position, node.position) > galaxy.maxDistanceBetweenNodesToConnect) { Node reference = enumeratorNodes.Current; Starlane newEdge = new Starlane(node, reference); graph.AddEdge(newEdge); edgeCosts.Add(newEdge, CartezianPosition.CalculateDistance(enumeratorNodes.Current.position, node.position)); //node.starlanes.Add(new Starlane(new CartezianLine(node.position,orderedNodes.ElementAt(1).position),node, orderedNodes.ElementAt(1))); //orderedNodes.ElementAt(1).starlanes.Add(new Starlane(new CartezianLine(node.position, orderedNodes.ElementAt(1).position), orderedNodes.ElementAt(1), node)); //graph.AddEdge(new Starlane(new CartezianLine(node.position, orderedNodes.ElementAt(1).position), node, orderedNodes.ElementAt(1))); //graph.AddEdge(new Starlane(new CartezianLine(node.position, orderedNodes.ElementAt(1).position), orderedNodes.ElementAt(1), node)); } else { for (int j = 1; j < 6; j++) { if (graph.AdjacentEdges(node).Count() < galaxy.maxConnectionsPerNode && graph.AdjacentEdges(enumeratorNodes.Current).Count() < galaxy.maxConnectionsPerNode) { bool foundIntersection = false; foreach (Node linkNode in graph.AdjacentVertices(enumeratorNodes.Current)) { foreach (Node linkOfLinkNode in graph.AdjacentVertices(linkNode)) { if (CartezianLine.LineSegmentsIntersect(new CartezianLine(node.position, enumeratorNodes.Current.position), new CartezianLine(linkNode.position, linkOfLinkNode.position))) { foundIntersection = true; } } } foreach (Node linkNode in graph.AdjacentVertices(node)) { foreach (Node linkOfLinkNode in graph.AdjacentVertices(linkNode)) { if (CartezianLine.LineSegmentsIntersect(new CartezianLine(node.position, enumeratorNodes.Current.position), new CartezianLine(linkNode.position, linkOfLinkNode.position))) { foundIntersection = true; } } } if (foundIntersection == false) { Node reference = enumeratorNodes.Current; Starlane newEdge = new Starlane(node, reference); graph.AddEdge(newEdge); edgeCosts.Add(newEdge, CartezianPosition.CalculateDistance(enumeratorNodes.Current.position, node.position)); //node.starlanes.Add(new Starlane(new CartezianLine(node.position, orderedNodes.ElementAt(j).position), node, orderedNodes.ElementAt(j))); //orderedNodes.ElementAt(j).starlanes.Add(new Starlane(new CartezianLine(node.position, orderedNodes.ElementAt(j).position), orderedNodes.ElementAt(j), node)); //graph.AddEdge(new Starlane(new CartezianLine(node.position, orderedNodes.ElementAt(j).position), node, orderedNodes.ElementAt(j))); //graph.AddEdge(new Starlane(new CartezianLine(node.position, orderedNodes.ElementAt(j).position), orderedNodes.ElementAt(j), node)); } } enumeratorNodes.MoveNext(); } } } }
public List <Starlane> GetStarlanes(List <Planet> worlds) { List <Starlane> starlanes = new List <Starlane>(); foreach (Planet planet in worlds) { int numConnections = getNumConnections(); //Setup layermasks - NameToLayer is expensive so just use it once here rather than every time we need it LayerMask ignoreLayer = LayerMask.NameToLayer("Ignore Raycast"); LayerMask defaultLayer = LayerMask.NameToLayer("Default"); //Add a new planet until our number of starlanes is the correct value while (planet.starlanes.Count < numConnections) { float closestDistance = 9999999; //Weird hardcode, "it just works" Planet closestPlanet = null; foreach (Planet connectingPlanet in worlds) { bool invalidConnection = false; foreach (Starlane starlane in planet.starlanes) { foreach (Starlane connectingStarlane in connectingPlanet.starlanes) { if (Starlane.Equals(starlane, connectingStarlane)) { invalidConnection = true; } } } Transform planetSphere = planet.transform.GetChild(0); Transform connectingPlanetSphere = connectingPlanet.transform.GetChild(0); Transform planetAura = planetSphere.GetChild(0); Transform connectingPlanetAura = connectingPlanetSphere.GetChild(0); planetSphere.gameObject.layer = ignoreLayer; connectingPlanetSphere.gameObject.layer = ignoreLayer; planetAura.gameObject.layer = ignoreLayer; connectingPlanetAura.gameObject.layer = ignoreLayer; //Returns true if collision with a planet other than the current and connecting planets occurs if (Physics.Linecast(planet.transform.position, connectingPlanet.transform.position)) { invalidConnection = true; } planetSphere.gameObject.layer = defaultLayer; connectingPlanetSphere.gameObject.layer = defaultLayer; planetAura.gameObject.layer = defaultLayer; connectingPlanetAura.gameObject.layer = defaultLayer; float distance = Vector3.Distance(planet.transform.position, connectingPlanet.transform.position); if (distance < closestDistance && !invalidConnection && !planet.Equals(connectingPlanet)) { closestPlanet = connectingPlanet; closestDistance = Vector3.Distance(planet.transform.position, connectingPlanet.transform.position); } } if (closestPlanet != null) { Starlane newStarlane = Starlane.Create(planet, closestPlanet); starlanes.Add(newStarlane); planet.starlanes.Add(newStarlane); closestPlanet.starlanes.Add(newStarlane); } else { Debug.Log("<NO CLOSEST PLANET FOUND!>"); numConnections = -1; break; } } } return(starlanes); }