Ejemplo n.º 1
0
        public void ProjectObjectLocations()
        {
            DateTime currenTime = DateTime.Now;
            TimeSpan ts         = currenTime - m_lastLocationUpdateTime;

            m_lastLocationUpdateTime = currenTime;

            foreach (SimObject ob in AllObjects.Values)
            {
                if (ob.ShouldProject)
                {
                    ob.Location.X = ob.Location.X + (ob.Velocity.VX * (Double)ts.Milliseconds / 1000);
                    ob.Location.Y = ob.Location.Y + (ob.Velocity.VY * (Double)ts.Milliseconds / 1000);
                    ob.Location.Z = ob.Location.Z + (ob.Velocity.VZ * (Double)ts.Milliseconds / 1000);
                    //if (System.Double.IsNaN(ob.Location.X))
                    //{
                    //    ob.ID = ob.ID;
                    //}
                    if (BehaviorHelper.LocationIsEqual(ob.Location, ob.DestinationLocation))
                    {
                        ob.ShouldProject = false;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        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.
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public void Start(DDDServerConnection serverConnection, DMView dmView)
        {
            m_dmView = dmView;
            //Console.Out.WriteLine(String.Format("MoveWithAvoidanceBehavior.Start(){0}",m_thisID));
            LogWriter.Write(m_dmView.DecisionMakerID + "_" + m_thisID, dmView.SimTime, String.Format("MoveWithAvoidanceBehavior.Start,{0},{1}", m_destLocation.ToXML(), m_throttle));
            m_wasBlocked = false;


            SimObject me = dmView.AllObjects[m_thisID];
            //SimObject dest = dmView.AllObjects[m_destID];

            LocationValue myLocation = me.Location;

            //LocationValue destLocation = dest.Location;


            if (BehaviorHelper.LocationIsEqual(myLocation, m_destLocation))
            {
                m_done = true;
                return;
            }

            if (!IsBlocked2())
            {
                m_wasBlocked = false;
                serverConnection.SendMoveObjectRequest(m_thisID, me.Owner, m_destLocation, m_throttle);
            }
            else
            {
                m_wasBlocked = true;
            }
        }
Ejemplo n.º 4
0
        void ContinueLoiter(DDDServerConnection serverConnection, DMView dmView)
        {
            SimObject     me            = dmView.AllObjects[m_thisID];
            LocationValue myLocation    = me.Location;
            SimObject     track         = dmView.AllObjects[m_targetID];
            LocationValue trackLocation = track.Location;

            if (BehaviorHelper.LocationIsEqual(myLocation, m_destWaypoint.Location))
            {
                m_destWaypoint = m_absoluteLoiterPattern.GetNextWaypoint();
                m_relativeLoiterPattern.NextWaypointIndex    = m_absoluteLoiterPattern.NextWaypointIndex;
                m_relativeLoiterPattern.CurrentWaypointIndex = m_absoluteLoiterPattern.CurrentWaypointIndex;
                serverConnection.SendMoveObjectRequest(m_thisID, m_destWaypoint.Location, 1);
            }
        }
Ejemplo n.º 5
0
        public void Update(DDDServerConnection serverConnection, DMView dmView)
        {
            if (m_done)
            {
                return;
            }
            SimObject     me         = dmView.AllObjects[m_thisID];
            LocationValue myLocation = me.Location;


            if (BehaviorHelper.LocationIsEqual(myLocation, m_destWaypoint.Location))
            {
                m_destWaypoint = m_absoluteLoiterPattern.GetNextWaypoint();
                serverConnection.SendMoveObjectRequest(m_thisID, m_destWaypoint.Location, 1);
            }
        }
Ejemplo n.º 6
0
        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);
            }
        }
Ejemplo n.º 7
0
        internal void ViewProMotionUpdate(SimulationEvent ev)
        {
            String id = ((StringValue)ev["ObjectID"]).value;

            if (AllObjects.ContainsKey(id))
            {
                SimObject ob = AllObjects[id];
                ob.ShouldProject       = true;
                ob.Location            = ((LocationValue)ev["Location"]);
                ob.DestinationLocation = ((LocationValue)ev["DestinationLocation"]);
                ob.Throttle            = ((DoubleValue)ev["Throttle"]).value;
                ob.Velocity            = BehaviorHelper.ComputeVelocityVector(ob.Location, ob.DestinationLocation, ob.MaximumSpeed, ob.Throttle);
            }
            if (PlayerAgent != null)
            {
                PlayerAgent.ViewProMotionUpdate(ev);
            }
        }
Ejemplo n.º 8
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);
            }
        }
Ejemplo n.º 9
0
        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);
        }
Ejemplo n.º 10
0
        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;
            }
        }
Ejemplo n.º 11
0
        //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);
            }
        }
Ejemplo n.º 12
0
        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;
            }
        }
Ejemplo n.º 13
0
        //Boolean IsBlocked()
        //{

        //    if (Paused)
        //    {
        //        m_blockedBy = "PAUSED";
        //        m_isBlocked = true;
        //        return m_isBlocked;
        //    }

        //    SimObject me = m_dmView.AllObjects[m_thisID];


        //    //SimObject dest = m_dmView.AllObjects[m_destID];

        //    LocationValue myLoc = me.Location;
        //    //LocationValue destLoc = dest.Location;
        //    LocationValue otherLocation = null;
        //    Vec2D myVec = new Vec2D(myLoc);
        //    Vec2D otherVec = null;
        //    Vec2D destVec = new Vec2D(m_destLocation);

        //    Polygon2D poly = GetPoly(myVec, destVec);

        //    foreach (String id in m_dmView.AllObjects.Keys)
        //    {
        //        if (id == m_thisID)
        //        {
        //            continue;
        //        }

        //        if (!m_objectClassesToAvoid.Contains(m_dmView.AllObjects[id].ClassName))
        //        {
        //            continue;
        //        }
        //        otherLocation = m_dmView.AllObjects[id].Location;
        //        otherVec = new Vec2D(otherLocation);

        //        if (Polygon2D.IsPointInside(poly, otherVec))
        //        {
        //            if (myVec.ScalerDistanceTo(otherVec) < m_avoidRange)
        //            {
        //                m_blockedBy = id;
        //                m_isBlocked = true;
        //                return m_isBlocked;
        //            }
        //        }
        //    }


        //    if (m_holdWP != null)
        //    {
        //        otherVec = new Vec2D(m_holdWP.Location);
        //        if (Polygon2D.IsPointInside(poly, otherVec))
        //        {

        //            if (myVec.ScalerDistanceTo(otherVec) < m_avoidRange)
        //            {
        //                //Console.Out.WriteLine(String.Format("MoveWithAvoidanceBehavior.IsBlocked() {0} true", m_thisID));
        //                m_blockedBy = "HOLD";
        //                m_isBlocked = true;
        //                return m_isBlocked;
        //            }
        //        }
        //    }

        //    //Console.Out.WriteLine(String.Format("MoveWithAvoidanceBehavior.IsBlocked() {0} false", m_thisID));
        //    m_blockedBy = String.Empty;
        //    m_isBlocked = false;
        //    return m_isBlocked;
        //}


        Boolean IsBlocked2()
        {
            if (Paused)
            {
                m_blockedBy = "PAUSED";
                m_isBlocked = true;
                return(m_isBlocked);
            }

            if (!ShouldAvoid)
            {
                m_blockedBy = String.Empty;;
                m_isBlocked = false;
                return(m_isBlocked);
            }

            SimObject     me            = m_dmView.AllObjects[m_thisID];
            LocationValue myLoc         = me.Location;
            LocationValue otherLocation = null;
            Vec2D         myVec         = new Vec2D(myLoc);
            Vec2D         otherVec      = null;
            Vec2D         destVec       = new Vec2D(m_destLocation);


            Vec2D farPoint = ProjectPointOnLine(myVec, destVec, m_avoidRange);

            //Polygon2D poly = GetPoly(myVec, destVec);

            foreach (String id in m_dmView.AllObjects.Keys)
            {
                if (id == m_thisID)
                {
                    continue;
                }

                if (!m_objectClassesToAvoid.Contains("ALL"))
                {
                    if (!m_objectClassesToAvoid.Contains(m_dmView.AllObjects[id].ClassName))
                    {
                        continue;
                    }
                }
                otherLocation = m_dmView.AllObjects[id].Location;
                otherVec      = new Vec2D(otherLocation);

                double d = BehaviorHelper.DistanceFromPointToLineSegment(otherVec, myVec, farPoint);

                if (d < (m_avoidPathWidth / 2))
                {
                    m_blockedBy = id;
                    m_isBlocked = true;
                    return(m_isBlocked);
                }
            }


            if (m_holdWP != null)
            {
                otherVec = new Vec2D(m_holdWP.Location);
                double d2 = BehaviorHelper.DistanceFromPointToLineSegment(otherVec, myVec, farPoint);

                if (d2 < (m_avoidPathWidth / 2))
                {
                    m_blockedBy = "HOLD";
                    m_isBlocked = true;
                    return(m_isBlocked);
                }
            }

            //Console.Out.WriteLine(String.Format("MoveWithAvoidanceBehavior.IsBlocked() {0} false", m_thisID));
            m_blockedBy = String.Empty;
            m_isBlocked = false;
            return(m_isBlocked);
        }