Exemple #1
0
        //Find obstructions
        //public static List<string> FindObstructions(List<string> allObstructions, Vec3D sensorPoint, Vec3D emitterPoint)
        //{//should pass a list of SimulationObjectProxies?

        //    List<string> obstructionList = new List<string>();
        //    SimulationObject obj;
        //    //do obstruction calculations
        //    foreach (string region in allObstructions)
        //    {
        //        obj = bbc
        //        Polygon3D reg = new Polygon3D(
        //    }

        //    return obstructionList;
        //    //return a list of ACVs?
        //}



        /// <summary>
        /// This method determines if 2 objects are within a certain range.  Returns true if
        /// the distance between the two objects is less than the range, otherwise returns false.  Offers easy
        /// calculation of two LocationValue objects.
        /// </summary>
        /// <param name="range">The max distance that can be between the two objects while still returning true.</param>
        /// <param name="objectA">The first object's location as a LocationValue.</param>
        /// <param name="objectB">The second object's location as a LocationValue.</param>
        /// <returns></returns>
        public static bool IsWithinRange(double range, LocationValue objectA, LocationValue objectB)
        {
            Vec3D objA = new Vec3D(objectA);
            Vec3D objB = new Vec3D(objectB);

            if (range > objA.ScalerDistanceTo(objB))
            {
                return(true);
            }

            return(false);
        }
Exemple #2
0
        //Find obstructions
        //public static List<string> FindObstructions(List<string> allObstructions, Vec3D sensorPoint, Vec3D emitterPoint)
        //{//should pass a list of SimulationObjectProxies?

        //    List<string> obstructionList = new List<string>();
        //    SimulationObject obj;
        //    //do obstruction calculations
        //    foreach (string region in allObstructions)
        //    { 
        //        obj = bbc
        //        Polygon3D reg = new Polygon3D(
        //    }

        //    return obstructionList;
        //    //return a list of ACVs?
        //}


        
        /// <summary>
        /// This method determines if 2 objects are within a certain range.  Returns true if
        /// the distance between the two objects is less than the range, otherwise returns false.  Offers easy
        /// calculation of two LocationValue objects.
        /// </summary>
        /// <param name="range">The max distance that can be between the two objects while still returning true.</param>
        /// <param name="objectA">The first object's location as a LocationValue.</param>
        /// <param name="objectB">The second object's location as a LocationValue.</param>
        /// <returns></returns>
        public static bool IsWithinRange(double range, LocationValue objectA, LocationValue objectB)
        {
            Vec3D objA = new Vec3D(objectA);
            Vec3D objB = new Vec3D(objectB);
            
            if (range > objA.ScalerDistanceTo(objB))
            {
                return true;
            }

            return false;
        }
Exemple #3
0
        //detection
        public static DetectedAttributeValue Detection(LocationValue sensingLocation,
                                                       LocationValue emittingLocation,
                                                       DataValue emittingObjectAttribute,
                                                       List <ConeValue> sensingCones,
                                                       Dictionary <string, double> emitters,
                                                       List <SimulationObjectProxy> obstructions,
                                                       ref Random random)
        {
            DetectedAttributeValue returnDAV = new DetectedAttributeValue();
            Vec3D  sensingPoint                 = new Vec3D(sensingLocation);
            Vec3D  emittingPoint                = new Vec3D(emittingLocation);
            Vec3D  sensingDirection             = new Vec3D(sensingPoint); //can't set to blank, just ignore.
            double coneAngle                    = 0.0;
            double angleBetweenSensorAndEmitter = 0.0;

            foreach (ConeValue cone in sensingCones)
            {
                sensingDirection.Set(cone.direction);
                Vec3D closestPoint = FindClosestPoint(sensingPoint, sensingDirection, emittingPoint);
                if (cone.extent > sensingPoint.ScalerDistanceTo(closestPoint))
                {                                            //p* is within the cone's extent.
                    //determine if emitting point is within the spread of the cone.
                    coneAngle = cone.spread * Math.PI / 180; // System.Math.Atan(emittingPoint.ScalerDistanceTo(closestPoint) / sensingPoint.ScalerDistanceTo(closestPoint));//should be cone.spread
                    angleBetweenSensorAndEmitter = System.Math.Atan(emittingPoint.ScalerDistanceTo(closestPoint) / sensingPoint.ScalerDistanceTo(closestPoint));

                    if (coneAngle < angleBetweenSensorAndEmitter)
                    {
                        continue; //should go to next cone.
                    }
                    if (emitters.ContainsKey(cone.level))
                    {//emitter has an emission at the same level as the cone.
                        //returnDAV = (DetectedAttributeValue)ObfuscateAttributeValue(emittingObjectAttribute, emitters[cone.level], ref random);
                        returnDAV = (DetectedAttributeValue)FuzzAttributeValue(emittingObjectAttribute, emitters[cone.level], ref random);

                        return(returnDAV);
                    }
                    else
                    { //emitter does not emit the same level as the cone.
                      //should this find next best, or just move on?
                    }
                }
            }

            return(null);
        }
Exemple #4
0
        /*
        private CapabilityValue.Effect FindCapabilityEffect(CapabilityValue cap, VulnerabilityValue vuln)
        {
            List<string> vulnNames = new List<string>();

            foreach (VulnerabilityValue.Transition t in vuln.transitions)
            {
                foreach (VulnerabilityValue.TransitionCondition tc in t.conditions)
                {
                    if (!vulnNames.Contains(tc.capability))
                    {
                        vulnNames.Add(tc.capability);
                    }
                }
            }

            foreach (CapabilityValue.Effect e in cap.effects)
            {
                if (vulnNames.Contains(e.name))
                {
                    return e;
                }
            }
            return null;
        }*/

        private void TimeTick(SimulationEvent e)
        {
            if (objectProxies == null)
            {
                objectProxies = bbClient.GetObjectProxies();
            }
            time = ((IntegerValue)e["Time"]).value;

            DataValue dv = null;

            SimulationObjectProxy obProx = null, targetProx = null;

            Vec3D myLoc;
            Vec3D targetLoc;
            string targetID;
            bool isWeapon = false;
            bool pursueStarted;

            CapabilityValue capabilities;
            VulnerabilityValue vulnerabilities;
            CapabilityValue.Effect effect;
            double distance;
            LocationValue location;
            
            foreach (string id in objectProxies.Keys)
            {
                obProx = objectProxies[id];

                // update the StateDB info

                if (StateDB.physicalObjects[id].ownerID == "")
                {
                    string owner = ((StringValue)obProx["OwnerID"].GetDataValue()).value;
                    if (StateDB.decisionMakers.ContainsKey(owner))
                    {
                        StateDB.physicalObjects[id].ownerID = owner;
                    }
                }

                if (StateDB.physicalObjects[id].speciesName == "")
                {
                    string speciesName = ((StringValue)obProx["ClassName"].GetDataValue()).value;
                    StateDB.physicalObjects[id].speciesName = speciesName;
                }

                if (StateDB.physicalObjects[id].ownerID != "" && StateDB.physicalObjects[id].teamName == "")
                {
                    if (StateDB.decisionMakers[StateDB.physicalObjects[id].ownerID].team != null)
                    {
                        StateDB.physicalObjects[id].teamName = StateDB.decisionMakers[StateDB.physicalObjects[id].ownerID].team.id;
                    }
                }

                if (((StringValue)obProx["AttackState"].GetDataValue()).value == "BEING_ATTACKED")
                {
                    continue;
                }

                AttackCollectionValue attCV = (AttackCollectionValue)obProx["CurrentAttacks"].GetDataValue();
                if (attCV.GetCurrentSelfDefenseAttacks().Count == 0)
                //if (((StringValue)obProx["AttackTargetID"].GetDataValue()).value == "")
                {
                    dv = obProx["SelfDefenseStartAttack"].GetDataValue();
                    ((BooleanValue)dv).value = false;
                    obProx["SelfDefenseStartAttack"].SetDataValue(dv);

                    dv = obProx["SelfDefenseCapability"].GetDataValue();
                    ((StringValue)dv).value = "";
                    obProx["SelfDefenseCapability"].SetDataValue(dv);

                    dv = obProx["SelfDefenseTargetID"].GetDataValue();
                    ((StringValue)dv).value = "";
                    obProx["SelfDefenseTargetID"].SetDataValue(dv);
                }

                if (((BooleanValue)obProx["SelfDefenseStartAttack"].GetDataValue()).value == true)
                {
                    continue;
                }

                isWeapon = ((BooleanValue)obProx["IsWeapon"].GetDataValue()).value;
                pursueStarted = ((BooleanValue)obProx["PursueStarted"].GetDataValue()).value;
                targetID = ((StringValue)obProx["PursueTargetID"].GetDataValue()).value;

                capabilities = (CapabilityValue)obProx["Capability"].GetDataValue();

                if (isWeapon && pursueStarted && objectProxies.ContainsKey(targetID))
                {
                    targetProx = objectProxies[targetID];

                    location = (LocationValue)obProx["Location"].GetDataValue();
                    if (!location.exists)
                    {
                        continue;
                    }
                    myLoc = new Vec3D(location);

                    location = (LocationValue)targetProx["Location"].GetDataValue();
                    if (!location.exists)
                    {
                        continue;
                    }
                    targetLoc = new Vec3D(location);

                    distance = myLoc.ScalerDistanceTo(targetLoc);

                    
                    vulnerabilities = (VulnerabilityValue)targetProx["Vulnerability"].GetDataValue();

                    //string cap = FindCapability(capabilities, vulnerabilities);
                    effect = SimUtility.FindCapabilityEffect(capabilities, vulnerabilities);
                    if (effect != null && distance < effect.range)
                    {
                        dv = obProx["SelfDefenseStartAttack"].GetDataValue();
                        ((BooleanValue)dv).value = true;
                        obProx["SelfDefenseStartAttack"].SetDataValue(dv);

                        dv = obProx["SelfDefenseCapability"].GetDataValue();
                        ((StringValue)dv).value = effect.name;
                        obProx["SelfDefenseCapability"].SetDataValue(dv);

                        dv = obProx["SelfDefenseTargetID"].GetDataValue();
                        ((StringValue)dv).value = targetID;
                        obProx["SelfDefenseTargetID"].SetDataValue(dv);

                    }
                    else if (effect == null && distance < 1)
                    {
                        distClient.PutEvent(SimUtility.BuildStateChangeEvent(ref simModel, time, id, "Dead"));
                    }

                    continue;
                }

                string ownerID = ((StringValue)obProx["OwnerID"].GetDataValue()).value;
                if (StateDB.decisionMakers.ContainsKey(ownerID))
                {
                    StateDB.DecisionMaker thisDM = StateDB.decisionMakers[ownerID];
                    List<string> targets = ((StringListValue)obProx["TargetsInRange"].GetDataValue()).strings;

                    //if (targets.Count == 0)
                    //{
                    //    continue;
                    //}
                    foreach (string target in targets)
                    {
                        SimulationObjectProxy obProx2 = objectProxies[target];
                        string ownerID2 = ((StringValue)obProx2["OwnerID"].GetDataValue()).value;
                        if (!StateDB.decisionMakers.ContainsKey(ownerID2))
                        {
                            continue;
                        }
                        StateDB.DecisionMaker thatDM = StateDB.decisionMakers[ownerID2];
                        vulnerabilities = (VulnerabilityValue)obProx2["Vulnerability"].GetDataValue();
                        effect = SimUtility.FindCapabilityEffect(capabilities, vulnerabilities);
                        
                        if (effect != null && thisDM.isHostile(thatDM) && !thisDM.isHuman)
                        {
                            dv = obProx["SelfDefenseStartAttack"].GetDataValue();
                            ((BooleanValue)dv).value = true;
                            obProx["SelfDefenseStartAttack"].SetDataValue(dv);

                            dv = obProx["SelfDefenseCapability"].GetDataValue();
                            ((StringValue)dv).value = effect.name;
                            obProx["SelfDefenseCapability"].SetDataValue(dv);

                            dv = obProx["SelfDefenseTargetID"].GetDataValue();
                            ((StringValue)dv).value = target;
                            obProx["SelfDefenseTargetID"].SetDataValue(dv);
                            break;
                        }
                        

                    }

                    
                }

            }
        }
Exemple #5
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="allObjectViews"></param>
        private void SenseAllObjects(ref Dictionary<string, Dictionary<string, AttributeCollectionValue>> allObjectsViews)
        {
            SimulationObjectProxy sensingProxy = null;
            SimulationObjectProxy targetProxy = null;
            Vec3D sensingLocation = null;
            Vec3D targetsLocation = null;
            LocationValue senLocation = null;
            LocationValue tarLocation = null;
            double distance = 0.0;
            SensorArrayValue sav = null;
            DataValue targetsAttribute = null;
            string ownerID = null;
            string objectType = null;
            DetectedAttributeValue detectedAttribute;
            AttributeCollectionValue singleAttributeCollection = null;
            bool isSensed = false;

            // The dictionary is constantly created in this method //
            Dictionary<string, AttributeCollectionValue> singleObjectView;

            //AD to improve this later:
            // for each object in the visible ObjectDistances collection
            //    for each object after the current index
            //       check for the index object's view of the nested object
            //       check for the nested object's view of the index object
            //Possible hangup is the "All" emitter.





            int x = 0;
            //Each object senses each other object
            //foreach (string sensorObjectID in listOfObjectIDs)
            foreach (KeyValuePair<string, List<string>> networks in networkRosters)
            {
                if (!activeSensorNetworks[networks.Key])
                    continue;
                foreach (string sensorObjectID in networkObjects[networks.Key])
                {
                    sensingProxy = objectProxies[sensorObjectID];
                    objectType = sensingProxy.GetObjectType();
                    senLocation = sensingProxy["Location"].GetDataValue() as LocationValue;
                    if (senLocation.exists)
                    {
                        ownerID = ((StringValue)sensingProxy["OwnerID"].GetDataValue()).value;
                        singleObjectView = new Dictionary<string, AttributeCollectionValue>();
                        x++;
                        //foreach (string targetObjectID in listOfObjectIDs)
                        foreach (List<string> objects in networkObjects.Values)
                        {
                            if (objects.Contains(sensorObjectID))
                            {
                                if (((EmitterValue)sensingProxy["Emitters"].GetDataValue()).emitters.ContainsKey("Invisible"))
                                {
                                    continue;
                                }
                                string target = sensorObjectID;

                                singleAttributeCollection = new AttributeCollectionValue();
                                foreach (KeyValuePair<string, AttributeInfo> simModelAtt in simModel.objectModel.objects[objectType].attributes)
                                {
                                    if (!simModelAtt.Value.ownerObservable == true)
                                        continue;
                                    //if (!sensingProxy.GetKeys().Contains(simModelAtt.Key))
                                    //    continue;
                                    DataValue t = sensingProxy[simModelAtt.Key].GetDataValue();
                                    if (t.dataType == "CustomAttributesType")
                                    {
                                        Dictionary<string, DataValue> copiedDict = CopyFromCustomAttributes(((CustomAttributesValue)t).attributes);
                                        t = new CustomAttributesValue();
                                        ((CustomAttributesValue)t).attributes = copiedDict;
                                    }
                                    detectedAttribute = new DetectedAttributeValue();
                                    detectedAttribute.stdDev = 100;
                                    detectedAttribute.value = DataValueFactory.BuildFromDataValue(t);
                                    AddAttributeToACV(ref singleAttributeCollection, simModelAtt.Key, detectedAttribute);
                                }//end foreach sensable attribute

                                if (!allObjectsViews.ContainsKey(sensorObjectID))
                                {
                                    allObjectsViews.Add(sensorObjectID, new Dictionary<string, AttributeCollectionValue>());
                                }
                                if (!objectViews.ContainsKey(sensorObjectID))
                                {
                                    objectViews.Add(sensorObjectID, new ObjectsAttributeCollection());
                                }
                                //update the global "allObjectViews".  The return from UpdateObject is the collection of attributes
                                //that have changed.  These attributes are stored in allObjectsViews and then sent out
                                //to users.
                                AttributeCollectionValue changedAttributes = objectViews[sensorObjectID].UpdateObject(target, singleAttributeCollection);

                                //if (changedAttributes != null && selectedRangeRingLevel != RangeRingLevels.DISABLED)
                                //{
                                //    CalculateRangeRings(ref changedAttributes, ref sensingProxy);
                                //}

                                if (changedAttributes != null)
                                {
                                    allObjectsViews[sensorObjectID].Add(target, changedAttributes);
                                }
                                else//this is so there is at least the empty entry for the detected object so 
                                //you still know it is detected.
                                {
                                    if (!allObjectsViews[sensorObjectID].ContainsKey(target))
                                    {
                                        allObjectsViews[sensorObjectID].Add(target, new AttributeCollectionValue());
                                    }
                                }
                            }
                            else
                            {
                                foreach (string targetObjectID in objects)
                                {//sensing and target objects are not the same 
                                    targetProxy = objectProxies[targetObjectID];
                                    tarLocation = targetProxy["Location"].GetDataValue() as LocationValue;
                                    if (tarLocation.exists)
                                    {
                                        EmitterValue emitters = targetProxy["Emitters"].GetDataValue() as EmitterValue;
                                        if (emitters.emitters.ContainsKey("All"))
                                        {
                                            //if an object has an all emitter, retrieve all attributes without sensing algorithm
                                            AttributeCollectionValue atts = new AttributeCollectionValue();
                                            if (singleObjectView.ContainsKey(targetObjectID))
                                            {
                                                atts = singleObjectView[targetObjectID];
                                            }
                                            SenseAllAttributes(ref atts, targetProxy);
                                            singleObjectView[targetObjectID] = atts;
                                            isSensed = true;

                                            if (!allObjectsViews.ContainsKey(sensorObjectID))
                                            {
                                                allObjectsViews.Add(sensorObjectID, new Dictionary<string, AttributeCollectionValue>());
                                            }
                                            if (!objectViews.ContainsKey(sensorObjectID))
                                            {
                                                objectViews.Add(sensorObjectID, new ObjectsAttributeCollection());
                                            }
                                            //update the global "allObjectViews".  The return from UpdateObject is the collection of attributes
                                            //that have changed.  These attributes are stored in allObjectsViews and then sent out
                                            //to users.
                                            AttributeCollectionValue changedAttributes = objectViews[sensorObjectID].UpdateObject(targetObjectID, atts);

                                            //if (changedAttributes != null && selectedRangeRingLevel == RangeRingLevels.FULL)
                                            //{//the only way to sense a "target" here is for FULL ring display
                                            //    CalculateRangeRings(ref changedAttributes, ref targetProxy);
                                            //}

                                            if (changedAttributes != null)
                                            {
                                                allObjectsViews[sensorObjectID].Add(targetObjectID, changedAttributes);
                                            }
                                            else//this is so there is at least the empty entry for the detected object so 
                                            //you still know it is detected.
                                            {
                                                if (!allObjectsViews[sensorObjectID].ContainsKey(targetObjectID))
                                                {
                                                    allObjectsViews[sensorObjectID].Add(targetObjectID, new AttributeCollectionValue());
                                                }
                                            }
                                        }
                                        else if (emitters.emitters.ContainsKey("Invisible"))
                                        {
                                            continue;
                                        }
                                        else
                                        {//object does not have an all emitter, continue to check each emitter value 
                                            AttributeCollectionValue atts = new AttributeCollectionValue();
                                            if (Omniscience)
                                            {
                                                if (singleObjectView.ContainsKey(targetObjectID))
                                                {
                                                    atts = singleObjectView[targetObjectID];
                                                }
                                                SenseAllAttributes(ref atts, targetProxy);
                                                singleObjectView[targetObjectID] = atts;
                                                isSensed = true;
                                                if (!allObjectsViews.ContainsKey(sensorObjectID))
                                                {
                                                    allObjectsViews.Add(sensorObjectID, new Dictionary<string, AttributeCollectionValue>());
                                                }
                                                if (!objectViews.ContainsKey(sensorObjectID))
                                                {
                                                    objectViews.Add(sensorObjectID, new ObjectsAttributeCollection());
                                                }
                                                //update the global "allObjectViews".  The return from UpdateObject is the collection of attributes
                                                //that have changed.  These attributes are stored in allObjectsViews and then sent out
                                                //to users.
                                                AttributeCollectionValue changedAttributes = objectViews[sensorObjectID].UpdateObject(targetObjectID, atts);

                                                //if (changedAttributes != null && selectedRangeRingLevel == RangeRingLevels.FULL)
                                                //{//the only way to sense a "target" here is for FULL ring display
                                                //    CalculateRangeRings(ref changedAttributes, ref targetProxy);
                                                //}

                                                if (changedAttributes != null)
                                                {
                                                    allObjectsViews[sensorObjectID].Add(targetObjectID, changedAttributes);
                                                }
                                                else//this is so there is at least the empty entry for the detected object so 
                                                //you still know it is detected.
                                                {
                                                    if (!allObjectsViews[sensorObjectID].ContainsKey(targetObjectID))
                                                    {
                                                        allObjectsViews[sensorObjectID].Add(targetObjectID, new AttributeCollectionValue());
                                                    }
                                                }
                                                continue;
                                            }

                                            sensingLocation = new Vec3D(senLocation);
                                            targetsLocation = new Vec3D(tarLocation);
                                            isSensed = false;
                                            distance = sensingLocation.ScalerDistanceTo(targetsLocation);
                                            sav = sensingProxy["Sensors"].GetDataValue() as SensorArrayValue;

                                            foreach (SensorValue sv in sav.sensors)
                                            {
                                                if (distance < sv.maxRange)
                                                {
                                                    //Find obstructions
                                                    List<SimulationObjectProxy> obstructions = FindObstructions(sensingLocation, targetsLocation);

                                                    foreach (KeyValuePair<string, List<ConeValue>> kvp in sv.ranges)
                                                    { //Key is the attribute being sensed, value is a list of cones
                                                        if (kvp.Key == "All")
                                                        {
                                                            atts = new AttributeCollectionValue();
                                                            if (singleObjectView.ContainsKey(targetObjectID))
                                                            {
                                                                atts = singleObjectView[targetObjectID];
                                                            }
                                                            SenseAllAttributes(ref atts, targetProxy);
                                                            singleObjectView[targetObjectID] = atts;
                                                            isSensed = true;
                                                        }
                                                        else //not an All sensor, so run detection algorithm
                                                        {//Added in main-line
                                                            if (!emitters.emitters.ContainsKey(kvp.Key))
                                                            {
                                                                continue;
                                                            }
                                                            //Custom attributes fix:
                                                            DataValue currentDataValue;
                                                            try
                                                            {
                                                                currentDataValue = targetProxy[kvp.Key].GetDataValue();
                                                            }
                                                            catch
                                                            {
                                                                currentDataValue = targetProxy["CustomAttributes"].GetDataValue();
                                                                currentDataValue = ((CustomAttributesValue)currentDataValue)[kvp.Key];
                                                                //will throw an error here if object doesn't contain custom atts, or 
                                                                //if it doesnt contain the specified custom att.
                                                            }

                                                            bool isObstructed = false;
                                                            foreach (SimulationObjectProxy reg in obstructions)
                                                            {
                                                                foreach (string attributeBlocked in ((StringListValue)reg["BlocksSensorTypes"].GetDataValue()).strings)
                                                                {
                                                                    if (kvp.Key == attributeBlocked)
                                                                    {
                                                                        isObstructed = true;
                                                                    }
                                                                }
                                                            }
                                                            if (isObstructed)
                                                            {
                                                                detectedAttribute = new DetectedAttributeValue();
                                                                detectedAttribute.value = DataValueFactory.BuildValue(currentDataValue.dataType);
                                                                detectedAttribute.confidence = 0;
                                                                //continue;
                                                            }
                                                            else
                                                            {
                                                                targetsAttribute = DataValueFactory.BuildFromDataValue(currentDataValue);
                                                                //ev is emitters
                                                                Dictionary<string, double> emitterCollection = emitters.emitters[kvp.Key];

                                                                detectedAttribute = new DetectedAttributeValue();
                                                                detectedAttribute = ObjectMath.Detection(senLocation, tarLocation, targetsAttribute, kvp.Value, emitterCollection, obstructions, ref randomGenerator);
                                                            }
                                                            if (detectedAttribute != null)
                                                            {
                                                                singleAttributeCollection = new AttributeCollectionValue();
                                                                if (singleObjectView.ContainsKey(targetObjectID))
                                                                {
                                                                    singleAttributeCollection = singleObjectView[targetObjectID];
                                                                }
                                                                AddAttributeToACV(ref singleAttributeCollection, kvp.Key, detectedAttribute);
                                                                isSensed = true;
                                                            }
                                                        }
                                                    }//end foreach attribute in sensor

                                                    //if the object has any attributes sensed, fill in some other info for the object's view.
                                                    if (isSensed)
                                                    {
                                                        detectedAttribute = new DetectedAttributeValue();
                                                        detectedAttribute.stdDev = 100;
                                                        detectedAttribute.value = targetProxy["ID"].GetDataValue();
                                                        if (singleAttributeCollection == null)
                                                        {
                                                            singleAttributeCollection = new AttributeCollectionValue();
                                                        }
                                                        if (!singleObjectView.ContainsKey(targetObjectID))
                                                        {
                                                            singleObjectView.Add(targetObjectID, singleAttributeCollection);
                                                        }
                                                        singleAttributeCollection = singleObjectView[targetObjectID];
                                                        AddAttributeToACV(ref singleAttributeCollection, "ID", detectedAttribute);

                                                        detectedAttribute = new DetectedAttributeValue();
                                                        detectedAttribute.stdDev = 100;
                                                        detectedAttribute.value = targetProxy["OwnerID"].GetDataValue();
                                                        AddAttributeToACV(ref singleAttributeCollection, "OwnerID", detectedAttribute);
                                                    }//end if isSensed.
                                                }
                                            }//end foreach sensor in sensor array

                                            if (singleObjectView.ContainsKey(targetObjectID))
                                            {
                                                AttributeCollectionValue attr = singleObjectView[targetObjectID];
                                                if (!allObjectsViews.ContainsKey(sensorObjectID))
                                                {
                                                    allObjectsViews.Add(sensorObjectID, new Dictionary<string, AttributeCollectionValue>());
                                                }
                                                if (!objectViews.ContainsKey(sensorObjectID))
                                                {
                                                    objectViews.Add(sensorObjectID, new ObjectsAttributeCollection());
                                                }

                                                if (attr.attributes.ContainsKey("CustomAttributes"))
                                                {
                                                    DataValue t = ((DetectedAttributeValue)attr["CustomAttributes"]).value;
                                                    double conf = ((DetectedAttributeValue)attr["CustomAttributes"]).confidence;
                                                    Dictionary<string, DataValue> copiedDict = CopyFromCustomAttributes(((CustomAttributesValue)t).attributes);
                                                    t = new CustomAttributesValue();
                                                    ((CustomAttributesValue)t).attributes = copiedDict;
                                                    attr.attributes.Remove("CustomAttributes");
                                                    attr.attributes.Add("CustomAttributes", DataValueFactory.BuildDetectedValue(t, Convert.ToInt32(conf)));
                                                }
                                                //update the global "allObjectViews".  The return from UpdateObject is the collection of attributes
                                                //that have changed.  These attributes are stored in allObjectsViews and then sent out
                                                //to users.
                                                AttributeCollectionValue changedAttributes = objectViews[sensorObjectID].UpdateObject(targetObjectID, attr);

                                                //if (changedAttributes != null && selectedRangeRingLevel == RangeRingLevels.FULL)
                                                //{//the only way to sense a "target" here is for FULL ring display
                                                //    CalculateRangeRings(ref changedAttributes, ref targetProxy);
                                                //}

                                                if (changedAttributes != null)
                                                {
                                                    allObjectsViews[sensorObjectID].Add(targetObjectID, changedAttributes);
                                                }
                                                else//this is so there is at least the empty entry for the detected object so 
                                                //you still know it is detected.
                                                {
                                                    if (!allObjectsViews[sensorObjectID].ContainsKey(targetObjectID))
                                                    {
                                                        allObjectsViews[sensorObjectID].Add(targetObjectID, new AttributeCollectionValue());
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                if (objectViews.ContainsKey(sensorObjectID))
                                                {
                                                    if (objectViews[sensorObjectID].ContainsObject(targetObjectID))
                                                    {
                                                        objectViews[sensorObjectID].RemoveObject(targetObjectID);
                                                        //if you once knew of this object, and now don't, remove it from object's view.
                                                    }
                                                }
                                            }
                                        }//end emitter detection
                                    }//end if target is visible
                                }
                            }
                        }//end foreach target object
                    }//end if sensor is visible
                }//end of foreach sensing object
            }//end of Foreach sensor network
            //if (x > 0)
            //  Console.Out.WriteLine("ViewPro: {0} objects were sensing at time {1}.", x, currentTick / 1000);
        }
Exemple #6
0
        //detection
        public static DetectedAttributeValue Detection(LocationValue sensingLocation,
                                                        LocationValue emittingLocation,
                                                        DataValue emittingObjectAttribute,
                                                        List<ConeValue> sensingCones,
                                                        Dictionary<string, double> emitters,
                                                        List<SimulationObjectProxy> obstructions,
                                                        ref Random random)
        {
            DetectedAttributeValue returnDAV = new DetectedAttributeValue();
            Vec3D sensingPoint = new Vec3D(sensingLocation);
            Vec3D emittingPoint = new Vec3D(emittingLocation);
            Vec3D sensingDirection = new Vec3D(sensingPoint); //can't set to blank, just ignore.
            double coneAngle = 0.0;
            double angleBetweenSensorAndEmitter = 0.0;

            foreach(ConeValue cone in sensingCones)
            {
                sensingDirection.Set(cone.direction);
                Vec3D closestPoint = FindClosestPoint(sensingPoint, sensingDirection, emittingPoint);
                if (cone.extent > sensingPoint.ScalerDistanceTo(closestPoint))
                {//p* is within the cone's extent. 
                    
                    //determine if emitting point is within the spread of the cone.
                    coneAngle = cone.spread * Math.PI / 180;// System.Math.Atan(emittingPoint.ScalerDistanceTo(closestPoint) / sensingPoint.ScalerDistanceTo(closestPoint));//should be cone.spread
                    angleBetweenSensorAndEmitter = System.Math.Atan(emittingPoint.ScalerDistanceTo(closestPoint) / sensingPoint.ScalerDistanceTo(closestPoint));

                    if (coneAngle < angleBetweenSensorAndEmitter)
                    {
                        continue; //should go to next cone.
                    }
                    if (emitters.ContainsKey(cone.level))
                    {//emitter has an emission at the same level as the cone.
                        //returnDAV = (DetectedAttributeValue)ObfuscateAttributeValue(emittingObjectAttribute, emitters[cone.level], ref random);
                        returnDAV = (DetectedAttributeValue)FuzzAttributeValue(emittingObjectAttribute, emitters[cone.level], ref random);
    
                        return returnDAV;
                    }
                    else
                    { //emitter does not emit the same level as the cone.
                        //should this find next best, or just move on?
                    }
                }
            }

            return null;
        }
Exemple #7
0
        /*
        Vec3D FindLandBoundaryIntersection(Vec3D fromP, Vec3D toP)
        {
            Vec2D r = null;

            Vec2D myFromP = new Vec2D(fromP.X, fromP.Y);
            Vec2D myToP = new Vec2D(toP.X, toP.Y);

            List<Vec2D> points = new List<Vec2D>();
            foreach (LandRegion reg in landRegions.Values)
            {
                r = Polygon2D.FindIntersect(reg.poly, myFromP, myToP);
                if (r != null)
                {
                    points.Add(r);
                }
            }

            if (points.Count == 0)
            {
                return null;
            }
            Vec2D closest = new Vec2D(points[0]);

            foreach (Vec2D p in points)
            {
                if (myFromP.ScalerDistanceTo(p) < myFromP.ScalerDistanceTo(closest))
                {
                    closest.Set(p);
                }
            }

            // return point on from side of intersection.
            if (myFromP.X < closest.X)
            {
                closest.X -= 1;
            }
            else if (myFromP.X > closest.X)
            {
                closest.X += 1;
            }
            if (myFromP.Y < closest.Y)
            {
                closest.Y -= 1;
            }
            else if (myFromP.Y > closest.Y)
            {
                closest.Y += 1;
            }

            return new Vec3D(closest.X, closest.Y, fromP.Z);
        }
        */
        /*
        Vec3D FindObstructionBoundaryIntersection(Vec3D fromP, Vec3D toP)
        {
            Vec3D r = null;

            List<Vec3D> points = new List<Vec3D>();
            foreach (BlockingRegion reg in blockingRegions.Values)
            {
                r = Polygon3D.FindIntersect(reg.poly, fromP, toP);
                if (r != null)
                {
                    points.Add(r);
                }
            }

            if (points.Count == 0)
            {
                return null;
            }
            Vec3D closest = new Vec3D(points[0]);

            foreach (Vec3D p in points)
            {
                if (fromP.ScalerDistanceTo(p) < fromP.ScalerDistanceTo(closest))
                {
                    closest.Set(p);
                }
            }

            // return point on from side of intersection.
            if (fromP.X < closest.X)
            {
                closest.X -= 1;
            }
            else if (fromP.X > closest.X)
            {
                closest.X += 1;
            }
            if (fromP.Y < closest.Y)
            {
                closest.Y -= 1;
            }
            else if (fromP.Y > closest.Y)
            {
                closest.Y += 1;
            }

            return closest;
        }
        */
        private void TimeTick(SimulationEvent e)
        {
            int oldTime = time;
            DataValue dv = null;

            Vec3D landIntersect;
            Vec3D obstructionIntersect;

            Vec3D curLocVec = new Vec3D(0, 0, 0);
            Vec3D newLocVec = new Vec3D(0, 0, 0);
            Vec3D destVec = new Vec3D(0, 0, 0);
            Vec3D velVec = new Vec3D(0, 0, 0);

            dv = e["Time"];
            time = ((IntegerValue)dv).value;

            double dTime = ((double)(time - oldTime)) / 1000;
            SimulationObjectProxy obProx = null;
            SimulationObjectProxy parentProx = null;

            bool launchStarted, launchDone;
            int launchEndTime;

            bool pursueStarted;
            string pursueTargetID;
            string parentObjectID;

            ScoringDB.ActorFrame actorFrame = new ScoringDB.ActorFrame();
            foreach (string id in objectProxies.Keys)
            {
                obProx = objectProxies[id];


                //*********************************
                // Launch movement logic 
                //*********************************

                launchStarted = ((BooleanValue)obProx["LaunchStarted"].GetDataValue()).value;
                launchDone = ((BooleanValue)obProx["LaunchDone"].GetDataValue()).value;

                pursueStarted = ((BooleanValue)obProx["PursueStarted"].GetDataValue()).value;
                pursueTargetID = ((StringValue)obProx["PursueTargetID"].GetDataValue()).value;

                if (launchStarted == false && launchDone == true)
                {
                    dv = obProx["LaunchDone"].GetDataValue();
                    ((BooleanValue)dv).value = false;
                    obProx["LaunchDone"].SetDataValue(dv);
                    launchDone = false;
                }
                if (launchStarted == true && launchDone == false)
                {
                    parentObjectID = ((StringValue)obProx["ParentObjectID"].GetDataValue()).value;
                    parentProx = objectProxies[parentObjectID];
                    launchEndTime = ((IntegerValue)obProx["LaunchEndTime"].GetDataValue()).value;
                    if (time >= launchEndTime)
                    {
                        Vec3D parentVec = new Vec3D((LocationValue)parentProx["Location"].GetDataValue());
                        dv = obProx["LaunchDestinationLocation"].GetDataValue();
                        Vec3D launchDestVec = new Vec3D((LocationValue)dv);
                        bool sendMoveRequest = ((LocationValue)dv).exists;

                        obProx["Location"].SetDataValue(parentVec.ToLocationValue());
                        //Now that location has been updated, set in helper library:
                        ObjectDistances.UpdateObjectLocation(id, parentVec);

                        dv = obProx["LaunchDone"].GetDataValue();
                        ((BooleanValue)dv).value = true;
                        obProx["LaunchDone"].SetDataValue(dv);

                        if (sendMoveRequest)
                        {
                            string userID = ((StringValue)obProx["OwnerID"].GetDataValue()).value;
                            distClient.PutEvent(SimUtility.BuildMoveObjectRequestEvent(ref simModel, time, userID, id, launchDestVec, 1.0));
                        }

                        if (((BooleanValue)obProx["LaunchIsWeapon"].GetDataValue()).value == true)
                        {
                            dv = obProx["PursueStarted"].GetDataValue();
                            ((BooleanValue)dv).value = true;
                            obProx["PursueStarted"].SetDataValue(dv);
                            pursueStarted = true;

                            dv = obProx["LaunchWeaponTargetID"].GetDataValue();
                            pursueTargetID = ((StringValue)dv).value;

                            dv = obProx["PursueTargetID"].GetDataValue();
                            ((StringValue)dv).value = pursueTargetID;
                            obProx["PursueTargetID"].SetDataValue(dv);

                            distClient.PutEvent(SimUtility.BuildSystemMessageEvent(ref simModel,
                                                           time,
                                                           ((StringValue)(obProx["OwnerID"].GetDataValue())).value,
                                                           id + " has been launched from " + parentObjectID + " at " + pursueTargetID));
                            distClient.PutEvent(SimUtility.BuildHistory_SubplatformLaunchEvent(ref simModel,
                                                                                               time,
                                                                                               id,
                                                                                               parentObjectID,
                                                                                               parentVec,
                                                                                               launchDestVec,
                                                                                               true,
                                                                                               pursueTargetID));
                        }
                        else
                        {
                            distClient.PutEvent(SimUtility.BuildSystemMessageEvent(ref simModel,
                                                           time,
                                                           ((StringValue)(obProx["OwnerID"].GetDataValue())).value,
                                                           id + " has been undocked from " + parentObjectID));
                            distClient.PutEvent(SimUtility.BuildHistory_SubplatformLaunchEvent(ref simModel,
                                                                                               time,
                                                                                               id,
                                                                                               parentObjectID,
                                                                                               parentVec,
                                                                                               launchDestVec,
                                                                                               false,
                                                                                               ""));
                        }
                    }
                }

                if (pursueStarted)
                {
                    if (objectProxies.ContainsKey(pursueTargetID))
                    {
                        dv = objectProxies[pursueTargetID]["Location"].GetDataValue();

                        String targetState = ((StringValue)objectProxies[pursueTargetID]["State"].GetDataValue()).value;
                        if (!((LocationValue)dv).exists || targetState == "Dead")
                        {
                            //if my target is dead or not existant, kill this weapon too.

                            obProx["PursueStarted"].SetDataValue(DataValueFactory.BuildBoolean(false));
                            obProx["PursueTargetID"].SetDataValue(DataValueFactory.BuildString(String.Empty));
                            distClient.PutEvent(SimUtility.BuildStateChangeEvent(ref simModel, time, id, "Dead"));
                            continue;
                        }

                        destVec.Set((LocationValue)dv);
                        obProx["DestinationLocation"].SetDataValue(destVec.ToLocationValue());

                        dv = obProx["Throttle"].GetDataValue();
                        ((DoubleValue)dv).value = 1;
                        obProx["Throttle"].SetDataValue(dv);

                        distClient.PutEvent(SimUtility.BuildHistory_PursueEvent(ref simModel,
                                                                                time,
                                                                                id,
                                                                                curLocVec,
                                                                                pursueTargetID,
                                                                                destVec));
                    }
                }


                //*********************************
                //   MoveObject logic
                //*********************************

                //
                // Ignore objects that don't have a location
                //
                dv = obProx["Location"].GetDataValue();
                if (!((LocationValue)dv).exists)
                {
                    continue;
                }
                curLocVec.Set((LocationValue)dv);
                

                //dv = obProx["Velocity"].GetDataValue();
                //velVec.Set((VelocityValue)dv);

                dv = obProx["DestinationLocation"].GetDataValue();
                if (!((LocationValue)dv).exists)
                {
                    destVec.Set(curLocVec);
                    obProx["DestinationLocation"].SetDataValue(destVec.ToLocationValue());
                }
                else
                {
                    destVec.Set((LocationValue)dv);
                }

                dv = obProx["Throttle"].GetDataValue();
                double throttle = ((DoubleValue)dv).value;

                dv = obProx["MaximumSpeed"].GetDataValue();
                double maxSpeed = ((DoubleValue)dv).value;




                //
                // If the object's throttle or maximum speed are zero, set velocity to (0,0,0) and 
                // set the destination to the current location
                //
                if (throttle == 0 || maxSpeed == 0 || ((StringValue)obProx["State"].GetDataValue()).value == "Dead")
                {
                    velVec.Set(0, 0, 0);
                    obProx["Velocity"].SetDataValue(velVec.ToVelocityValue());

                    destVec.Set(curLocVec);
                    obProx["DestinationLocation"].SetDataValue(destVec.ToLocationValue());

                    dv = obProx["Throttle"].GetDataValue();
                    ((DoubleValue)dv).value = 0;
                    obProx["Throttle"].SetDataValue(dv);

                }


                //
                // Update what active regions the object is in.
                //
                StateDB.physicalObjects[id].activeRegions = GetActiveRegions(curLocVec);
                dv = obProx["InActiveRegions"].GetDataValue();
                StateDB.physicalObjects[id].activeRegions.Remove(id);// dont want to be blocked by itself.
                ((StringListValue)dv).strings = StateDB.physicalObjects[id].activeRegions;
                obProx["InActiveRegions"].SetDataValue(dv);


                double speedMultiplier = GetActiveRegionSpeedMultiplier(StateDB.physicalObjects[id].activeRegions);
                dv = obProx["ActiveRegionSpeedMultiplier"].GetDataValue();
                if (((DoubleValue)dv).value != speedMultiplier)
                {
                    ((DoubleValue)dv).value = speedMultiplier;
                    obProx["ActiveRegionSpeedMultiplier"].SetDataValue(dv);
                    //distClient.PutEvent(SimUtility.BuildActiveRegionSpeedMultiplierUpdateEvent(ref simModel, time, id));
                    distributor.viewProBackChannelEvents.Add(SimUtility.BuildActiveRegionSpeedMultiplierUpdateEvent(ref simModel, time, id));
                }

                if (time % 1000 == 0)
                {
                    actorFrame.objectID = id;
                    actorFrame.speciesName = StateDB.physicalObjects[id].speciesName;
                    actorFrame.ownerID = StateDB.physicalObjects[id].ownerID;
                    actorFrame.activeRegions = StateDB.physicalObjects[id].activeRegions;

                    ScoringDB.UpdateScore_ObjectExists(actorFrame);
                }

                //
                // Update the object's velocity based on its destinations, throttle, and maximum speed.
                //

                if (curLocVec.ScalerDistanceTo(destVec) > 0.01)
                {
                    if (curLocVec.ScalerDistanceTo(destVec) >= (maxSpeed * throttle * dTime * speedMultiplier))
                    {
                        velVec = curLocVec.VectorDistanceTo(destVec);
                        velVec.Normalize();
                        velVec = velVec.Multiply(maxSpeed * throttle * speedMultiplier);
                    }
                    else
                    {
                        velVec = destVec.Subtract(curLocVec);
                    }

                    obProx["Velocity"].SetDataValue(velVec.ToVelocityValue());
                    //
                    // Update the object's location based on its velocity
                    //
                    newLocVec.X = curLocVec.X + (velVec.X * dTime);
                    newLocVec.Y = curLocVec.Y + (velVec.Y * dTime);
                    newLocVec.Z = curLocVec.Z + (velVec.Z * dTime);





                    // TODO: Do obstruction check here
                    /*
                    obstructionIntersect = FindObstructionBoundaryIntersection(curLocVec, newLocVec);
                    landIntersect = FindLandBoundaryIntersection(curLocVec, newLocVec);
                    if (obstructionIntersect != null)
                    {
                        curLocVec.Set(obstructionIntersect);
                        obProx["Location"].SetDataValue(curLocVec.ToLocationValue());
                        destVec.Set(curLocVec);
                        obProx["DestinationLocation"].SetDataValue(destVec.ToLocationValue());
                        distClient.PutEvent(SimUtility.BuildMoveDoneEvent(ref simModel, time, id));
                        dv = obProx["Throttle"].GetDataValue();
                        ((DoubleValue)dv).value = 0;
                        obProx["Throttle"].SetDataValue(dv);
                    }
                    else if (landIntersect != null && (obProx.GetObjectType() != "AirObject"))
                    {
                        curLocVec.Set(landIntersect);
                        obProx["Location"].SetDataValue(curLocVec.ToLocationValue());
                        destVec.Set(curLocVec);
                        obProx["DestinationLocation"].SetDataValue(destVec.ToLocationValue());
                        distClient.PutEvent(SimUtility.BuildMoveDoneEvent(ref simModel, time, id));
                        dv = obProx["Throttle"].GetDataValue();
                        ((DoubleValue)dv).value = 0;
                        obProx["Throttle"].SetDataValue(dv);
                    }
                    else
                    {
                        curLocVec.Set(newLocVec);
                        obProx["Location"].SetDataValue(curLocVec.ToLocationValue());
                    } */

                    bool stopMotion = false;
                    switch (obProx.GetObjectType())
                    {
                        case "SeaObject":
                            if (!IsOnSea(newLocVec))
                            {
                                stopMotion = true;
                                distClient.PutEvent(SimUtility.BuildSystemMessageEvent(ref simModel,
                                            time,
                                            ((StringValue)(obProx["OwnerID"].GetDataValue())).value,
                                            id + " has been blocked by land"));
                            }
                            break;
                        case "LandObject":
                            if (!IsOnLand(newLocVec))
                            {
                                stopMotion = true;
                                distClient.PutEvent(SimUtility.BuildSystemMessageEvent(ref simModel,
                                            time,
                                            ((StringValue)(obProx["OwnerID"].GetDataValue())).value,
                                            id + " has been blocked by sea"));
                            }
                            break;
                        default:
                            break;
                    }

                    if (IsObstructed(curLocVec, newLocVec,id))
                    {
                        stopMotion = true;
                        //distClient.PutEvent(SimUtility.BuildSystemMessageEvent(ref simModel,
                        //                    time,
                        //                    ((StringValue)(obProx["OwnerID"].GetDataValue())).value,
                        //                    id + " has been blocked by an obstruction"));
                    }

                    if (!stopMotion)
                    {
                        curLocVec.Set(newLocVec);
                        obProx["Location"].SetDataValue(curLocVec.ToLocationValue());
                        //Now that location has been updated, set in helper library:
                        ObjectDistances.UpdateObjectLocation(id, curLocVec);
                    }
                    else
                    {
                        obProx["Location"].SetDataValue(curLocVec.ToLocationValue());
                        //Now that location has been updated, set in helper library:
                        ObjectDistances.UpdateObjectLocation(id, curLocVec);
                        destVec.Set(curLocVec);
                        obProx["DestinationLocation"].SetDataValue(destVec.ToLocationValue());
                        distClient.PutEvent(SimUtility.BuildMoveDoneEvent(ref simModel, time, id, "Obstruction"));
                        distributor.viewProBackChannelEvents.Add(SimUtility.BuildMoveDoneEvent(ref simModel, time, id, "Obstruction"));
                        dv = obProx["Throttle"].GetDataValue();
                        ((DoubleValue)dv).value = 0;
                        obProx["Throttle"].SetDataValue(dv);
                        distClient.PutEvent(SimUtility.BuildSystemMessageEvent(ref simModel,
                                            time,
                                            ((StringValue)(obProx["OwnerID"].GetDataValue())).value,
                                            id + " was stopped by an obstruction"));
                    }

                }
                else // if the object has reached its destination and is not pursuing, send MoveDone
                {


                    velVec.Set(0, 0, 0);
                    newLocVec.Set(destVec);
                    obProx["Location"].SetDataValue(newLocVec.ToLocationValue());
                    //Now that location has been updated, set in helper library:
                    ObjectDistances.UpdateObjectLocation(id, newLocVec);
                    obProx["Velocity"].SetDataValue(velVec.ToVelocityValue());


                    if (!pursueStarted && throttle > 0)
                    {
                        /* TODO: Make sure MoveDone is being sent correctly.*/

                        distClient.PutEvent(SimUtility.BuildMoveDoneEvent(ref simModel, time, id, "ReachedDestination"));
                        distributor.viewProBackChannelEvents.Add(SimUtility.BuildMoveDoneEvent(ref simModel, time, id, "ReachedDestination"));
                        dv = obProx["Throttle"].GetDataValue();
                        ((DoubleValue)dv).value = 0;
                        obProx["Throttle"].SetDataValue(dv);

                        distClient.PutEvent(SimUtility.BuildSystemMessageEvent(ref simModel,
                                            time,
                                            ((StringValue)(obProx["OwnerID"].GetDataValue())).value,
                                            id + " has reached its destination"));
                    }



                }

                

                /* TODO: Ignore obstruction code for now */
                /*
                if (((StringValue)obProx["State"].GetDataValue()).value != "Dead" &&
                    ((obProx.GetObjectType() == "LandObject" && !IsOnLand(newLocVec)) ||
                    (obProx.GetObjectType() == "SeaObject" && !IsOnSea(newLocVec)) ||
                    IsObstructed(curLocVec,newLocVec) || IsInObstruction(newLocVec)))
                {
                    velVec.Set(0, 0, 0);
                    dv = obProx["Velocity"].GetDataValue();

                    obProx["Velocity"].SetDataValue(velVec.ToVelocityValue());
                    SimulationEvent done = SimulationEventFactory.BuildEvent(ref simModel, "MoveDone");
                    ((IntegerValue)done["Time"]).value = time;
                    ((StringValue)done["ObjectID"]).value = id;
                    distClient.PutEvent(done);
                    System.Console.WriteLine("Motion: object {0} stopped by land boundery", id);

                    dv = obProx["Throttle"].GetDataValue();
                    ((DoubleValue)dv).value = 0;
                    obProx["Throttle"].SetDataValue(dv);

                    if (((obProx.GetObjectType() == "LandObject" && !IsOnLand(curLocVec)) ||
                        obProx.GetObjectType() == "SeaObject" && !IsOnSea(curLocVec)) ||
                        IsInObstruction(curLocVec))
                    {
                        SimulationEvent dead = SimulationEventFactory.BuildEvent(ref simModel, "StateChange");
                        ((IntegerValue)dead["Time"]).value = time;
                        ((StringValue)dead["ObjectID"]).value = id;
                        ((StringValue)dead["NewState"]).value = "Dead";
                        distClient.PutEvent(dead);
                    }

                    return;
                }
                 * */

            }
        }
Exemple #8
0
        private void AttackObject(string attackerID, string targetID, string capabilityName, int percentageApplied, bool isSelfDefense)
        {
            DataValue dv = null;


            //AD Note: on 9/23/09, we decided that Vulnerabilities would now have an "EngagementDuration", which is the Vulnerability
            // version of Capability's "AttackDuration".  If an EngagementDuration exists, it will override a Capability's AttackDuration.

            SimulationObjectProxy attackerProx = objectProxies[attackerID];
            SimulationObjectProxy targetProx = objectProxies[targetID];

            CapabilityValue attackerCap = (CapabilityValue)attackerProx["Capability"].GetDataValue();
            VulnerabilityValue targetVul = (VulnerabilityValue)targetProx["Vulnerability"].GetDataValue();

            // Check to see if this attack can start

            if (((StringValue)attackerProx["AttackState"].GetDataValue()).value == "BEING_ATTACKED")
            {
                distClient.PutEvent(SimUtility.BuildSystemMessageEvent(ref simModel,
                                                                       time,
                                                                       ((StringValue)(attackerProx["OwnerID"].GetDataValue())).value,
                                                                       attackerID + " is being engaged -- it can't initiate an engagement."));

                return;
            }
            if (targetVul.transitions.Count == 0)
            {
                distClient.PutEvent(SimUtility.BuildSystemMessageEvent(ref simModel,
                                                                       time,
                                                                       ((StringValue)(attackerProx["OwnerID"].GetDataValue())).value,
                                                                       attackerID + " can't engage " + targetID + " -- object has no vulnerabilities."));
                return;
            }

            //AD 11/16/2009: New Attack Logic: Allow for partial capabilities to be applied to attacks.

            AttackCollectionValue attackCollection = (AttackCollectionValue)attackerProx["CurrentAttacks"].GetDataValue();
            int attackDuration = -1;

            AttackCollectionValue.AttackValue newAttack = new AttackCollectionValue.AttackValue(time, attackDuration, targetID, attackerID, capabilityName, percentageApplied, isSelfDefense);
            string errMsg = String.Empty;

            attackDuration = ((IntegerValue)attackerProx["AttackDuration"].GetDataValue()).value;
            int defenseDuration = ((IntegerValue)targetProx["EngagementDuration"].GetDataValue()).value;
            int attackTimeWindow = attackDuration;
            bool attackSuccess = true;
            if (defenseDuration > 0)
            {
                attackTimeWindow = defenseDuration;
            }
            Console.WriteLine("AttackObject: Attack duration for " + attackerID + " is " + attackDuration.ToString());
            Console.WriteLine("AttackObject: Attack duration for attack is " + attackTimeWindow.ToString());
            newAttack.attackTimeWindow = attackTimeWindow;

            if (attackCollection.AddAttack(newAttack, out errMsg) == false)
            {
                string msg = "The attack between " + attackerID + " and " + targetID + " encountered the following problem: " + errMsg;
                distClient.PutEvent(SimUtility.BuildSystemMessageEvent(ref simModel,
                                                                   time,
                                                                   ((StringValue)(attackerProx["OwnerID"].GetDataValue())).value,
                                                                   msg));
                attackSuccess = false;
            }
            else if (errMsg != String.Empty)
            {
                string msg = "The attack between " + attackerID + " and " + targetID + " encountered the following problem: " + errMsg;
                distClient.PutEvent(SimUtility.BuildSystemMessageEvent(ref simModel,
                                                                   time,
                                                                   ((StringValue)(attackerProx["OwnerID"].GetDataValue())).value,
                                                                   msg));
            }
            else //success
            {
                dv = targetProx["AttackState"].GetDataValue();
                ((StringValue)dv).value = "BEING_ATTACKED";
                targetProx["AttackState"].SetDataValue(dv);

                dv = targetProx["AttackerList"].GetDataValue();
                ((StringListValue)dv).strings.Add(attackerID);
                targetProx["AttackerList"].SetDataValue(dv);
            }

            

            if (!attackSuccess)
            {
                attackerProx["CurrentAttacks"].SetDataValue(attackCollection); //set below if attackSuccess is true
                return;
            }


            //End new attack logic.


            //
            Vec3D attackerPosition = new Vec3D((LocationValue)attackerProx["Location"].GetDataValue());
            Vec3D targetPosition = new Vec3D((LocationValue)targetProx["Location"].GetDataValue());
            double distance = attackerPosition.ScalerDistanceTo(targetPosition); //FIX find actual diatance

            int appliedIntensity = -1;
            List<CapabilityValue.Effect> capabilities = attackerCap.GetOrderedEffectsByCapability(capabilityName);
            foreach (CapabilityValue.Effect eff in capabilities)
            {
                if (eff.name == capabilityName && distance <= eff.range)
                {
                    int r = random.Next(0, 100);
                    if (r <= ((int)(eff.probability * 100)))
                    {
                        appliedIntensity = Convert.ToInt32(Math.Round(((double)eff.intensity * (double)newAttack.percentageApplied / 100), MidpointRounding.AwayFromZero));
                        newAttack.appliedIntensity = appliedIntensity;
                        targetVul.ApplyEffect(eff.name, appliedIntensity, distance, ref random);
                        
                    }
                    //break outside of the if because if the probability failed, you dont want a second chance with a different range.
                    break; //break because we only want to apply the first range found that satisfied the distance restraint
                }
            }
            attackerProx["CurrentAttacks"].SetDataValue(attackCollection);
            targetProx["Vulnerability"].SetDataValue((DataValue)targetVul);


            distClient.PutEvent(SimUtility.BuildSystemMessageEvent(ref simModel,
                                                                   time,
                                                                   ((StringValue)(attackerProx["OwnerID"].GetDataValue())).value,
                                                                   attackerID + " has engaged " + targetID));
            distClient.PutEvent(SimUtility.BuildSystemMessageEvent(ref simModel,
                                                                   time,
                                                                   ((StringValue)(targetProx["OwnerID"].GetDataValue())).value,
                                                                   targetID + " has been engaged by " + attackerID));

            distClient.PutEvent(SimUtility.BuildHistory_AttackerObjectReportEvent(ref simModel,
                                                                                  time,
                                                                                  attackerID,
                                                                                  attackerPosition,
                                                                                  targetID,
                                                                                  targetPosition,
                                                                                  capabilityName,
                                                                                  appliedIntensity));


        }
Exemple #9
0
 public double Distance(String ob1, String ob2)
 {
     if (!AllObjects.ContainsKey(ob1) || !AllObjects.ContainsKey(ob2))
     {
         return -1;
     }
     Vec3D ob1V = new Vec3D(AllObjects[ob1].Location);
     Vec3D ob2V = new Vec3D(AllObjects[ob2].Location);
     return ob1V.ScalerDistanceTo(ob2V);
 }
        private void TimeTick(SimulationEvent e)
        {
            int oldTime = time;
            DataValue dv = null;


            //Vec3D curVec = new Vec3D(0, 0, 0);
            //Vec3D destVec = new Vec3D(0, 0, 0);
            //Vec3D velVec = new Vec3D(0, 0, 0);

            Vec3D loc1 = new Vec3D(0, 0, 0);
            Vec3D loc2 = new Vec3D(0, 0, 0);
            Vec3D next1 = new Vec3D(0, 0, 0);
            Vec3D next2 = new Vec3D(0, 0, 0);

            Vec3D vel1 = new Vec3D(0, 0, 0);
            Vec3D vel2 = new Vec3D(0, 0, 0);

            dv = e["Time"];
            time = ((IntegerValue)dv).value;

            double dTime = ((double)(time - oldTime)) / 1000;
            SimulationObjectProxy obProx1 = null;
            SimulationObjectProxy obProx2 = null;
            

            List<string> ids = new List<string>(objectProxies.Keys);

            string id1;

            SimulationEvent collision = null;
            double distance;
            double? d;
            while (ids.Count > 0)
            {
                id1 = ids[0];
                ids.Remove(id1);
                

                foreach (string id2 in ids)
                {
                    d = ObjectDistances.GetScalarDistanceBetweenObjects(id1, id2);

                    if (d == null)
                    { 
                    // Don't look for collisions if they aren't on the screen
                        continue;
                    }
                    distance = d.Value;

                    double objectSize1 = 0;
                    double objectSize2 = 0;
                    obProx1 = objectProxies[id1];
                    obProx2 = objectProxies[id2];


                    //// Don't look for collisions if they aren't on the screen

                    dv = obProx1["Location"].GetDataValue();
                    //if (!((LocationValue)dv).exists)
                    //{
                    //    continue;
                    //}
                    loc1.Set((LocationValue)dv);
                    dv = obProx2["Location"].GetDataValue();
                    //if (!((LocationValue)dv).exists)
                    //{
                    //    continue;
                    //}
                    loc2.Set((LocationValue)dv);

                    if (((BooleanValue)obProx1["DockedToParent"].GetDataValue()).value)
                    {
                        continue;
                    }
                    if (((BooleanValue)obProx2["DockedToParent"].GetDataValue()).value)
                    {
                        continue;
                    }

                    // Don't look for collisions if they are owned by the same player
                    if (StateDB.physicalObjects[id1].ownerID == StateDB.physicalObjects[id2].ownerID)
                    {
                        continue;
                    }
                    // Don't look for collisions if they are on the same team
                    if (StateDB.physicalObjects[id1].teamName == StateDB.physicalObjects[id2].teamName)
                    {
                        continue;
                    }
                    //Don't look for collisions if they are not hostile
                    if (StateDB.teams.ContainsKey(StateDB.physicalObjects[id1].teamName) &&
                        StateDB.teams.ContainsKey(StateDB.physicalObjects[id2].teamName))
                    {
                        if (!StateDB.teams[StateDB.physicalObjects[id1].teamName].hostility.Contains(StateDB.teams[StateDB.physicalObjects[id2].teamName].id) &&
                            !StateDB.teams[StateDB.physicalObjects[id2].teamName].hostility.Contains(StateDB.teams[StateDB.physicalObjects[id1].teamName].id))
                        {//only continue if both teams are not hostile to one another
                            continue;
                        }
                    }

                    CapabilityValue.Effect eff = null;

                    // check ranges and add objects to target lists for self defense sim
                    CapabilityValue cap1 = ((CapabilityValue)obProx1["Capability"].GetDataValue());
                    VulnerabilityValue vul1 = ((VulnerabilityValue)obProx1["Vulnerability"].GetDataValue());
                    CapabilityValue cap2 = ((CapabilityValue)obProx2["Capability"].GetDataValue());
                    VulnerabilityValue vul2 = ((VulnerabilityValue)obProx2["Vulnerability"].GetDataValue());
                    //double distance = loc1.ScalerDistanceTo(loc2);

                    
                    eff = SimUtility.FindCapabilityEffect(cap1, vul2);
                    dv = obProx1["TargetsInRange"].GetDataValue();
                    //AD: TODO need a "TargetsInSensorRange"? which can drive the ViewPro loops
                    if (eff != null && distance <= eff.range &&
                        ((StringValue)obProx2["State"].GetDataValue()).value != "Dead")
                    {
                        if (!((StringListValue)dv).strings.Contains(id2))
                        {
                            ((StringListValue)dv).strings.Add(id2);
                        }
                    }
                    else
                    {
                        if (((StringListValue)dv).strings.Contains(id2))
                        {
                            ((StringListValue)dv).strings.Remove(id2);
                        }
                    }
                    obProx1["TargetsInRange"].SetDataValue(dv);

                    eff = SimUtility.FindCapabilityEffect(cap2, vul1);
                    dv = obProx2["TargetsInRange"].GetDataValue();
                    if (eff != null && distance <= eff.range &&
                        ((StringValue)obProx1["State"].GetDataValue()).value != "Dead")
                    {
                        if (!((StringListValue)dv).strings.Contains(id1))
                        {
                            ((StringListValue)dv).strings.Add(id1);
                        }
                    }
                    else
                    {
                        if (((StringListValue)dv).strings.Contains(id1))
                        {
                            ((StringListValue)dv).strings.Remove(id1);
                        }
                    }
                    obProx2["TargetsInRange"].SetDataValue(dv);


                    // Don't look for collisions if they are dead

                    if (((StringValue)obProx1["State"].GetDataValue()).value == "Dead")
                    {
                        continue;
                    }
                    if (((StringValue)obProx2["State"].GetDataValue()).value == "Dead")
                    {
                        continue;
                    }


                    // Don't look for collisions if they are too small to collide

                    dv = obProx1["Size"].GetDataValue();
                    objectSize1 = ((DoubleValue)dv).value;
                    if (objectSize1 <= 0.000001)
                    {
                        continue;
                    }
                    dv = obProx2["Size"].GetDataValue();
                    objectSize2 = ((DoubleValue)dv).value;
                    if (objectSize2 <= 0.000001)
                    {
                        continue;
                    }

                    dv = obProx1["Velocity"].GetDataValue();
                    vel1.Set((VelocityValue)dv);
                    dv = obProx2["Velocity"].GetDataValue();
                    vel2.Set((VelocityValue)dv);
                    if (vel1.X == 0 && vel2.X == 0 && vel1.Y == 0 && vel2.Y == 0 && vel1.Z == 0 && vel2.Z == 0)
                        continue;

                    next1 = loc1.Add(vel1.Multiply(dTime));
                    next2 = loc2.Add(vel2.Multiply(dTime));

                    if (next1.ScalerDistanceTo(next2) < (objectSize1 + objectSize2))
                    {
                        collision = SimulationEventFactory.BuildEvent(ref simModel, "ObjectCollision");
                        ((StringValue)collision["ObjectID1"]).value = id1;
                        ((StringValue)collision["ObjectID2"]).value = id2;
                        ((IntegerValue)collision["Time"]).value = time;
                        distClient.PutEvent(collision);
                    }
                }
            }

            /*foreach (string id1 in objectProxies.Keys)
            {
                foreach (string id2 in objectProxies.Keys)
                {
                    if (id1 == id2)
                    {
                        continue;
                    }
                    obProx1 = objectProxies[id1];
                    obProx2 = objectProxies[id1];

                    dv = obProx1["Location"].GetDataValue();
                    loc1.Set((LocationValue)dv);
                    dv = obProx2["Location"].GetDataValue();
                    loc2.Set((LocationValue)dv);

                }

            }*/
        }
Exemple #11
0
 static public Double Distance(LocationValue l1, LocationValue l2)
 {
     Vec3D v1 = new Vec3D(l1);
     Vec3D v2 = new Vec3D(l2);
     return v1.ScalerDistanceTo(v2);
 }