void ComputeIntersections(WaypointRoute route1, WaypointRoute route2) { List <LineSegment> segments1 = route1.ToLineSegments(); List <LineSegment> segments2 = route2.ToLineSegments(); Double THRESHOLD = 1.0; foreach (LineSegment ls1 in segments1) { foreach (LineSegment ls2 in segments2) { LocationValue intersect = BehaviorHelper.LineIntersect(ls1.First.Location, ls1.Second.Location, ls2.First.Location, ls2.Second.Location); if (intersect != null) { // if the intersect is really close to one of the route verticies, than don't do anything. // otherwise, insert the intersect into the routes if (BehaviorHelper.Distance(intersect, ls1.First.Location) > THRESHOLD && BehaviorHelper.Distance(intersect, ls1.Second.Location) > THRESHOLD) { int pos = route1.IndexOf(ls1.Second); route1.Insert(pos, new Waypoint(String.Format("between-{0}-{1}", ls1.First.Name, ls1.Second.Name), intersect)); } if (BehaviorHelper.Distance(intersect, ls2.First.Location) > THRESHOLD && BehaviorHelper.Distance(intersect, ls2.Second.Location) > THRESHOLD) { int pos = route2.IndexOf(ls2.Second); route2.Insert(pos, new Waypoint(String.Format("between-{0}-{1}", ls2.First.Name, ls2.Second.Name), intersect)); } return; // we only allow for one intersection between the two routes. } } } }
public int Compare(ActiveRegionWaypoint obj1, ActiveRegionWaypoint obj2) { double dis1 = BehaviorHelper.Distance(obj1.Location, m_refPoint.Location); double dis2 = BehaviorHelper.Distance(obj2.Location, m_refPoint.Location); if (dis1 < dis2) { return(-1); } else if (dis1 > dis2) { return(1); } else { return(0); } }
bool ShouldTrack(DDDServerConnection serverConnection, DMView dmView) { SimObject me = dmView.AllObjects[m_thisID]; LocationValue myLocation = me.Location; SimObject track = dmView.AllObjects[m_targetID]; LocationValue trackLocation = track.Location; WaypointSequence absoluteLoiterPattern = m_relativeLoiterPattern.ToAbsolute(trackLocation); Waypoint destWaypoint = absoluteLoiterPattern.GetWaypointClosestTo(myLocation); Double myDis = BehaviorHelper.Distance(myLocation, trackLocation); Double destDis = BehaviorHelper.Distance(destWaypoint.Location, trackLocation); if (myDis > (destDis * 2)) { return(true); } else { return(false); } }
static public LocationGraph GenerateRouteGraph(String startName, LocationValue startLocation, String endName, LocationValue endLocation, List <WaypointRoute> routes) { LocationGraph result = new LocationGraph(); double distanceThreshold = 1.0; //List<String> routeNames = new List<string>(this.Keys); String name; for (int i = 0; i < routes.Count; i++) { name = routes[i].Name; WaypointRoute route = routes[i]; LocationGraph.LocationNode lastNode = null; foreach (Waypoint wp in route) { LocationGraph.LocationNode node = new LocationGraph.LocationNode(wp.Name, wp.Location); result.AddNode(node); if (lastNode != null) { result.BiConnect(lastNode, node); } lastNode = node; } if (i > 0) { WaypointRoute lastRoute = routes[i - 1]; Boolean done = false; for (int j = 1; j < lastRoute.Count; j++) { if (done) { break; } for (int k = 1; k < route.Count; k++) { if (done) { break; } Waypoint lastRouteP1 = lastRoute[j - 1]; Waypoint lastRouteP2 = lastRoute[j]; Waypoint nextRouteP1 = route[k - 1]; Waypoint nextRouteP2 = route[k]; LocationValue intersect = BehaviorHelper.LineIntersect(lastRouteP1.Location, lastRouteP2.Location, nextRouteP1.Location, nextRouteP2.Location); /*if (intersect != null) * { * string newName = String.Format("Intersection_{0}_{1}_{2}_{3}", lastRouteP1.Name, lastRouteP2.Name, nextRouteP1.Name, nextRouteP2.Name); * LocationGraph.LocationNode intersectNode = new LocationGraph.LocationNode(newName, intersect); * result.AddNode(intersectNode); * result.BiConnect(intersectNode.Name, lastRouteP1.Name); * result.BiConnect(intersectNode.Name, lastRouteP2.Name); * result.BiConnect(intersectNode.Name, nextRouteP1.Name); * result.BiConnect(intersectNode.Name, nextRouteP2.Name); * } * else*/ if (BehaviorHelper.Distance(lastRouteP1.Location, nextRouteP1.Location) < distanceThreshold) { result.BiConnect(lastRouteP1.Name, nextRouteP1.Name); done = true; } else if (BehaviorHelper.Distance(lastRouteP1.Location, nextRouteP2.Location) < distanceThreshold) { result.BiConnect(lastRouteP1.Name, nextRouteP2.Name); done = true; } else if (BehaviorHelper.Distance(lastRouteP2.Location, nextRouteP1.Location) < distanceThreshold) { result.BiConnect(lastRouteP2.Name, nextRouteP1.Name); done = true; } else if (BehaviorHelper.Distance(lastRouteP2.Location, nextRouteP2.Location) < distanceThreshold) { result.BiConnect(lastRouteP2.Name, nextRouteP2.Name); done = true; } } } } } //result.ProximityConnect(10); //result.IntersectConnect(); LocationNode startNode = new LocationNode(startName, startLocation); LocationNode endNode = new LocationNode(endName, endLocation); result.InsertByProximity(startNode); result.InsertByProximity(endNode); return(result); }
public void FindShortestPath(List <String> path, String startName, String endName, out Double out_distance, out List <String> out_path) { out_path = new List <string>(); out_distance = -1; path = new List <string>(path); path.Add(startName); LocationNode startNode = m_nodes[startName]; List <LocationNode> childNodes = new List <LocationNode>(startNode.ConnectedNodes); List <String> bestPath = null; LocationNode bestNode = null; Double bestDistance = -1; foreach (LocationNode child in childNodes) { if (path.Contains(child.Name)) // ignore children that we have already been to { continue; } if (child.Name == endName) // we've found the destination! { out_path.Add(endName); out_distance = BehaviorHelper.Distance(startNode.Location, child.Location); return; } else { List <String> tempPath = null; Double tempDistance = -1; FindShortestPath(path, child.Name, endName, out tempDistance, out tempPath); if (tempDistance < 0) { continue; } else if (bestDistance < 0) { bestDistance = tempDistance; bestPath = tempPath; bestNode = child; } else if (tempDistance < bestDistance) { bestDistance = tempDistance; bestPath = tempPath; bestNode = child; } } } if (bestDistance < 0) { return; } else { out_distance = BehaviorHelper.Distance(startNode.Location, m_nodes[bestPath[0]].Location) + bestDistance; //bestPath.Insert(0, startNode.Name); bestPath.Insert(0, bestNode.Name); out_path = bestPath; } }
//public void ProximityConnect(Double distanceThreshold) //{ // List<String> workingList = new List<string>(m_nodes.Keys); // while (workingList.Count > 0) // { // String currentNodeName = workingList[0]; // workingList.Remove(currentNodeName); // foreach (String workingNodeName in workingList) // { // if (BehaviorHelper.LocationIsEqual(m_nodes[currentNodeName].Location, m_nodes[workingNodeName].Location, distanceThreshold)) // { // BiConnect(currentNodeName, workingNodeName); // } // } // } //} //public void IntersectConnect() //{ // List<String> workingList = new List<string>(m_nodes.Keys); // while (workingList.Count > 0) // { // String currentNodeName = workingList[0]; // LocationNode currentNode = m_nodes[currentNodeName]; // workingList.Remove(currentNodeName); // List<LocationNode> connectedNodes = new List<LocationNode>(currentNode.ConnectedNodes); // foreach (LocationNode connectedNode in connectedNodes) // { // foreach (String workingNodeName in workingList) // { // LocationNode workingNode = m_nodes[workingNodeName]; // List<LocationNode> connectedNodes2 = new List<LocationNode>(workingNode.ConnectedNodes); // foreach (LocationNode workingConnectedNode in connectedNodes2) // { // LocationValue intersectLoc = BehaviorHelper.LineIntersect(currentNode.Location, connectedNode.Location, // workingNode.Location, workingConnectedNode.Location); // if (intersectLoc != null) // { // LocationNode newNode = new LocationNode(String.Format("{0}_{1}_{2}_{3}",currentNode.Name,connectedNode.Name, // workingNode.Name, workingConnectedNode.Name), // intersectLoc); // AddNode(newNode); // BiConnect(currentNode, newNode); // BiConnect(connectedNode, newNode); // BiConnect(workingNode, newNode); // BiConnect(workingConnectedNode, newNode); // } // } // } // } // } //} void InsertByProximity(LocationNode newNode) { //find closest node List <String> workingList = new List <string>(m_nodes.Keys); String closest = workingList[0]; double distance = BehaviorHelper.Distance(newNode.Location, m_nodes[closest].Location); workingList.Remove(closest); foreach (String n in workingList) { double thisDistance = BehaviorHelper.Distance(newNode.Location, m_nodes[n].Location); if (thisDistance < distance) { closest = n; distance = thisDistance; } } LocationNode closestNode = m_nodes[closest]; LocationNode otherNode = null; LocationValue pointOnLine = null; double distanceToPointOnLine = -1; foreach (LocationNode node in closestNode.ConnectedNodes) { if (pointOnLine == null) { otherNode = node; pointOnLine = BehaviorHelper.ClosestPointOnLine(newNode.Location, closestNode.Location, node.Location); distanceToPointOnLine = BehaviorHelper.Distance(newNode.Location, pointOnLine); } else { LocationValue l = BehaviorHelper.ClosestPointOnLine(newNode.Location, closestNode.Location, node.Location); double d = BehaviorHelper.Distance(newNode.Location, l); if (d < distanceToPointOnLine) { otherNode = node; pointOnLine = l; distanceToPointOnLine = d; } } } if (distanceToPointOnLine > 5 || true) { LocationNode insertNode = new LocationNode(String.Format("{0}_{1}_{2}", newNode.Name, closestNode.Name, otherNode.Name), pointOnLine); AddNode(insertNode); BiConnect(insertNode.Name, closestNode.Name); BiConnect(insertNode.Name, otherNode.Name); AddNode(newNode); BiConnect(newNode.Name, insertNode.Name); } else { AddNode(newNode); BiConnect(newNode.Name, closestNode.Name); BiConnect(newNode.Name, otherNode.Name); } }
public void Update(DDDServerConnection serverConnection, DMView dmView) { if (m_done) { return; } SimObject me = dmView.AllObjects[m_thisID]; LocationValue myLocation = me.Location; SimObject track = dmView.AllObjects[m_targetID]; LocationValue trackLocation = track.Location; m_absoluteLoiterPattern = m_relativeLoiterPattern.ToAbsolute(trackLocation); switch (m_engagementState) { case EngagementState.Tracking: if (ShouldTrack(serverConnection, dmView)) { serverConnection.SendMoveObjectRequest(m_thisID, trackLocation, 1); } else { m_engagementState = EngagementState.Attacking; StartLoiter(serverConnection, dmView); } break; case EngagementState.Attacking: ContinueLoiter(serverConnection, dmView); if (!m_attackInProcess) // start the attack { // start with weapons if (me.DockedWeapons.Count > 0) { m_attackWeaponID = me.DockedWeapons[0]; serverConnection.SendWeaponLaunchRequest(m_thisID, m_attackWeaponID, m_targetID); m_attackEndTime = dmView.SimTime + 12000; // give a two minute time window to start, AttackUpdate will modify this m_attackInProcess = true; m_attackIsWeapon = true; } else // use native capabilities { // figure out capability/vulnerability match up String cap = DetermineCapability(me.CapabilityList, track.VulnerabilityList); if (cap != String.Empty) { serverConnection.SendAttackObjectRequest(m_thisID, m_targetID, cap); m_attackInProcess = true; m_attackIsWeapon = false; m_attackEndTime = dmView.SimTime + 12000; } else // I don't have the right capabilities, finish up { ResetAttack(); if (m_returnAfter) { m_engagementState = EngagementState.Returning; m_dddServer.SendMoveObjectRequest(m_thisID, m_originalLocation, 1); } else { m_done = true; } } } } else // check to see if the attack was succesful { // if we are still in attack mode 2 seconds after attack was supposed to end // start another attack if (dmView.SimTime > m_attackEndTime + 2000) { ResetAttack(); } } break; case EngagementState.Returning: if (BehaviorHelper.Distance(myLocation, m_originalLocation) < 1) { m_done = true; } break; } }