Example #1
0
        public void RemoveAttackTest1()
        {
            AttackCollectionValue target = new AttackCollectionValue();

            target.FromXML(LoadableXML);
            AttackCollectionValue.AttackValue attack = null;
            bool expected = true;
            bool actual;
            int  remainingCapabilityPercentage = 40;

            attack = target.GetCurrentAttacks()[1];
            actual = target.RemoveAttack(attack);
            Assert.AreEqual(expected, actual);
            Assert.AreEqual(remainingCapabilityPercentage, target.GetCurrentAttacks()[1].percentageApplied);
        }
Example #2
0
        private void CancelCapabilityAttack(string capabilityName, string attackingObjectID)
        {
            SimulationObjectProxy attacker = objectProxies[attackingObjectID];

            if (attacker == null)
            {
                return;
            }

            List <AttackCollectionValue.AttackValue> attacksToRemove = new List <AttackCollectionValue.AttackValue>();
            AttackCollectionValue currentAttacks = attacker["CurrentAttacks"].GetDataValue() as AttackCollectionValue;

            foreach (AttackCollectionValue.AttackValue av in currentAttacks.GetCurrentAttacks())
            {
                if (av.capabilityName == capabilityName && attackingObjectID == av.attackingObjectId)
                {
                    attacksToRemove.Add(av);
                }
            }

            foreach (AttackCollectionValue.AttackValue av in attacksToRemove)
            {
                RemoveIntensityFromTargetsVulnerability(av.targetObjectId, av.capabilityName, av.appliedIntensity);
                currentAttacks.RemoveAttack(av);
            }

            attacker["CurrentAttacks"].SetDataValue(currentAttacks);
            attacksToRemove.Clear();
        }
Example #3
0
        public void FromXMLTest()
        {
            AttackCollectionValue target = new AttackCollectionValue();
            bool caughtException         = false;

            try
            {
                target.FromXML(LoadableXML);
            }
            catch (System.Exception ex)
            {
                caughtException = true;
            }
            Assert.IsTrue(!caughtException, "Error creating FromXML");
            Assert.IsTrue(target.GetCurrentAttacks().Count == 3, "Incorrect Amount of attacks populated");
            Assert.IsTrue(target.GetCurrentPercentageApplied("Missile") == 100, "Incorrect Percentage Applied for Missile");

            target.FromXML("");
            Assert.IsTrue(target.GetCurrentAttacks().Count == 0, "Incorrect Amount of attacks populated");
            Assert.IsTrue(target.GetCurrentPercentageApplied("Missile") == 0, "Incorrect Percentage Applied for Missile");
        }
Example #4
0
        public void RemoveAttackTest()
        {
            AttackCollectionValue target = new AttackCollectionValue();

            target.FromXML(LoadableXML);
            string capabilityName                = "Missile";
            string targetObjectId                = "MyTarget";
            string attackingObjectId             = "MyAttacker";
            int    attackStartTime               = 8;
            int    remainingCapabilityPercentage = 40;
            bool   expected = true;
            bool   actual;

            actual = target.RemoveAttack(capabilityName, targetObjectId, attackingObjectId, attackStartTime);
            Assert.AreEqual(expected, actual);
            Assert.AreEqual(remainingCapabilityPercentage, target.GetCurrentAttacks()[1].percentageApplied);
        }
        private void CompareNewDMViewWithPrevious(string dm, Dictionary <string, AttributeCollectionValue> singleDMView, ref List <string> recentDiscoveries)
        {
            if (!dmViews.ContainsKey(dm))
            {
                return;
            }
            ObjectsAttributeCollection prevDMView     = dmViews[dm];
            List <string> objectsToRemove             = new List <string>();
            List <string> objectsToAdd                = new List <string>();
            List <string> previouslyNotVisibleObjects = new List <string>();

            foreach (string obj in prevDMView.GetObjectKeys())
            {
                if (!singleDMView.ContainsKey(obj))
                {
                    objectsToRemove.Add(obj);
                }
                if (prevDMView[obj].attributes.ContainsKey("Location"))
                {
                    if (!((LocationValue)((DetectedAttributeValue)prevDMView[obj]["Location"]).value).exists)
                    {
                        previouslyNotVisibleObjects.Add(obj);
                    }
                }
            }
            foreach (string obj in singleDMView.Keys)
            {
                if (!prevDMView.ContainsObject(obj) && singleDMView[obj].attributes.ContainsKey("Location"))
                {
                    objectsToAdd.Add(obj);
                }
            }
            AttributeCollectionValue acv = new AttributeCollectionValue();

            //acv.attributes.Add("ID", new StringValue() as DataValue);
            foreach (string obj in objectsToAdd.ToArray())
            {
                //
                acv = singleDMView[obj]; //singleDMView stores DetectedValues, which need to be
                //stripped before sending to SendVPIO
                if (acv.attributes.ContainsKey("Location"))
                {
                    if (!((LocationValue)((DetectedAttributeValue)acv["Location"]).value).exists)
                    {
                        objectsToRemove.Add(obj);
                        objectsToAdd.Remove(obj);
                        continue;
                    }
                    else
                    {
                        //if (!previouslyNotVisibleObjects.Contains(obj))
                        //    continue;
                        //objectsToAdd.Add(obj);
                        //objectsToRemove.Remove(obj);
                    }
                }
                if (!acv.attributes.ContainsKey("ID"))
                {
                    acv["ID"] = DataValueFactory.BuildDetectedValue(DataValueFactory.BuildString(obj), 100);
                }
                if (!acv.attributes.ContainsKey("OwnerID"))
                {
                    acv["OwnerID"] = DataValueFactory.BuildDetectedValue(objectProxies[obj]["OwnerID"].GetDataValue(), 100);
                }
                SendViewProInitializeObject(dm, ExtractDetectedValuesFromACV(acv));
            }

            foreach (string obj in objectsToRemove)
            {
                SendRemoveObjectEvent(obj, dm);
                prevDMView.RemoveObject(obj);
            }

            bool anyAttributeChanged = false;

            acv = null;

            foreach (string obj in objectsToAdd)
            {
                //AttributeCollectionValue atts = new AttributeCollectionValue(); //prevDMView.UpdateObject(obj, singleDMView[obj]);
                //SendViewProAttributeUpdate(dm, atts);
                prevDMView.UpdateObject(obj, new AttributeCollectionValue());
                if (!movingObjects.Contains(obj))
                {
                    continue;
                }
                if (!activeDMs.Contains(dm))
                {
                    continue;
                }
                if (!recentDiscoveries.Contains(obj))
                {
                    recentDiscoveries.Add(obj);
                }
                //atts["ObjectID"] = DataValueFactory.BuildString(obj);//this is needed as the
                //                                                     //above Update call will
                //                                                     //add "ID", not "ObjectID"
                //SendViewProMotionUpdate(atts);
            }
            foreach (string obj in prevDMView.GetObjectKeys())
            {
                acv = prevDMView.UpdateObject(obj, singleDMView[obj]);
                //add recent tags
                if (recentUnitTags != null)
                {
                    if (recentUnitTags.ContainsKey(obj))
                    {
                        if (recentUnitTags[obj].ContainsKey(dm))
                        {
                            if (acv == null)
                            {
                                acv = new AttributeCollectionValue();
                            }
                            acv.attributes["InitialTag"] = DataValueFactory.BuildString(recentUnitTags[obj][dm]);
                            recentUnitTags[obj].Remove(dm); //remove once the DM has been notified.
                            acv.attributes["ID"]      = DataValueFactory.BuildString(obj);
                            acv.attributes["OwnerID"] = prevDMView[obj]["OwnerID"];
                        }
                    }
                }

                String classification = GetChangedClassificationForDM(obj, dm);
                if (classification != null)
                {
                    if (acv == null)
                    {
                        acv = new AttributeCollectionValue();
                        acv.attributes["ID"]      = DataValueFactory.BuildString(obj);
                        acv.attributes["OwnerID"] = prevDMView[obj]["OwnerID"];
                    }
                    acv.attributes["CurrentClassification"] = DataValueFactory.BuildString(classification);
                }

                if (acv == null)
                {
                    continue;
                }
                if (acv.attributes.ContainsKey("Location"))
                {
                    if (((LocationValue)((DetectedAttributeValue)acv["Location"]).value).exists)
                    {
                        //SendViewProInitializeObject(dm, ExtractDetectedValuesFromACV(prevDMView[obj]));
                    }
                }

                SendViewProAttributeUpdate(dm, acv);
                if (acv.attributes.ContainsKey("OwnerID") && acv.attributes.ContainsKey("CurrentAttacks"))
                {
                    if (((StringValue)((DetectedAttributeValue)acv.attributes["OwnerID"]).value).value == dm)
                    {
                        AttackCollectionValue attacks = (AttackCollectionValue)((DetectedAttributeValue)acv.attributes["CurrentAttacks"]).value;
                        foreach (AttackCollectionValue.AttackValue av in attacks.GetCurrentAttacks())
                        {
                            foreach (Attack a in currentAttacks)
                            {
                                if (a.attacker == av.attackingObjectId && a.target == av.targetObjectId && a.capabilityName == av.capabilityName)
                                {
                                    a.SetTimes(av.attackStartTime, av.attackTimeWindow);
                                }
                            }
                        }
                    }
                }
                if (acv.attributes.ContainsKey("DestinationLocation") ||
                    acv.attributes.ContainsKey("Throttle"))
                {
                    if (!recentDiscoveries.Contains(obj))
                    {
                        recentDiscoveries.Add(obj);
                    }
                    //acv["ObjectID"] = acv["ID"];
                    //SendViewProMotionUpdate(ExtractDetectedValuesFromACV(acv));
                }
                acv = null;
            }
        }
Example #6
0
        private void TimeTick(SimulationEvent e)
        {
            time = ((IntegerValue)e["Time"]).value;

            DataValue dv = null;

            SimulationObjectProxy targetProx = null;
            Vec3D targetLoc = new Vec3D(0, 0, 0);
            SimulationObjectProxy obProx = null;

            bool   selfDefenseStartAttack;
            string selfDefenseCapability;
            string selfDefenseTargetID;
            Dictionary <string, Dictionary <string, List <AttackCollectionValue.AttackValue> > > currentAttackCollection = new Dictionary <string, Dictionary <string, List <AttackCollectionValue.AttackValue> > >();
            //[ [TargetID]/[ CapabilityUsed]/[List of Attacks] ]
            Dictionary <string, List <AttackCollectionValue.AttackValue> > attacksToRemove = new Dictionary <string, List <AttackCollectionValue.AttackValue> >();

            // [AttackerID]/[List of attacks to remove]
            //as you clean up attacks, add them to this list.  once done iterating over targets, go through this list and update the attacks in the keys.
            foreach (string id in objectProxies.Keys)
            {
                obProx = objectProxies[id];
                //Generate Attack dictionary
                AttackCollectionValue attacks = (AttackCollectionValue)obProx["CurrentAttacks"].GetDataValue();

                foreach (AttackCollectionValue.AttackValue av in attacks.GetCurrentAttacks())
                {
                    if (!currentAttackCollection.ContainsKey(av.targetObjectId))
                    {
                        currentAttackCollection.Add(av.targetObjectId, new Dictionary <string, List <AttackCollectionValue.AttackValue> >());
                    }
                    if (!currentAttackCollection[av.targetObjectId].ContainsKey(av.capabilityName))
                    {
                        currentAttackCollection[av.targetObjectId].Add(av.capabilityName, new List <AttackCollectionValue.AttackValue>());
                    }
                    currentAttackCollection[av.targetObjectId][av.capabilityName].Add(av); //store pointer
                }

                selfDefenseStartAttack = ((BooleanValue)obProx["SelfDefenseStartAttack"].GetDataValue()).value;

                if (selfDefenseStartAttack)
                {
                    selfDefenseCapability = ((StringValue)obProx["SelfDefenseCapability"].GetDataValue()).value;
                    selfDefenseTargetID   = ((StringValue)obProx["SelfDefenseTargetID"].GetDataValue()).value;

                    targetProx = objectProxies[selfDefenseTargetID];

                    if (((AttackCollectionValue)obProx["CurrentAttacks"].GetDataValue()).GetCurrentAttacks().Count == 0 &&
                        ((StringValue)obProx["State"].GetDataValue()).value != "Dead" &&
                        ((StringValue)targetProx["State"].GetDataValue()).value != "Dead")
                    {
                        AttackObject(id, selfDefenseTargetID, selfDefenseCapability, 100, true);
                        if (((StringValue)obProx["AttackState"].GetDataValue()).value == "")
                        {
                            SendSelfDefenseAttackStarted(id, selfDefenseTargetID);
                        }
                    }
                }
            }

            foreach (string targetID in objectProxies.Keys)
            {
                targetProx = objectProxies[targetID];
                string currentState = ((StringValue)objectProxies[targetID]["State"].GetDataValue()).value;
                dv = targetProx["AttackState"].GetDataValue();
                if (((StringValue)dv).value == "BEING_ATTACKED")
                {
                    if (!currentAttackCollection.ContainsKey(targetID))
                    {
                        currentAttackCollection.Add(targetID, new Dictionary <string, List <AttackCollectionValue.AttackValue> >());
                        //this should not happen, or we're in trouble
                    }
                    int capabilitiesCompleted = 0; //this gets incremented as you add to attacksToRemove
                    foreach (String capability in currentAttackCollection[targetID].Keys)
                    {
                        //update attack windows for each attack object?
                        int attackEndTime = -1;
                        foreach (AttackCollectionValue.AttackValue av in currentAttackCollection[targetID][capability])
                        {
                            if (attackEndTime == -1)
                            {
                                attackEndTime = av.attackStartTime + av.attackTimeWindow;
                            }
                            else
                            {
                                attackEndTime = Math.Min(attackEndTime, av.attackStartTime + av.attackTimeWindow);
                            }
                        }
                        int newDuration = attackEndTime - time;
                        foreach (AttackCollectionValue.AttackValue av in currentAttackCollection[targetID][capability])
                        {
                            av.attackTimeWindow = attackEndTime - av.attackStartTime;// newDuration;
                        }

                        //check attack window vs current time
                        if (time >= attackEndTime)
                        {
                            //cleanup if needed

                            //add attacks to remove list

                            foreach (AttackCollectionValue.AttackValue av in currentAttackCollection[targetID][capability])
                            {
                                if (!attacksToRemove.ContainsKey(av.attackingObjectId))
                                {
                                    attacksToRemove.Add(av.attackingObjectId, new List <AttackCollectionValue.AttackValue>());
                                }
                                attacksToRemove[av.attackingObjectId].Add(av);
                            }

                            //check vulnerabilities
                            VulnerabilityValue targetVul = (VulnerabilityValue)targetProx["Vulnerability"].GetDataValue();
                            bool          attackSuccess  = false;
                            List <string> capabilitiesApplied;
                            List <string> attackers = new List <string>();

                            foreach (VulnerabilityValue.Transition t in targetVul.transitions)
                            {
                                foreach (String cap in t.GetAppliedCapabilities())
                                {
                                    if (!currentAttackCollection[targetID].ContainsKey(cap))
                                    {
                                        continue; //workaround for issue at USF; for some reason capability was not added to current attack collection.
                                    }
                                    foreach (AttackCollectionValue.AttackValue val in currentAttackCollection[targetID][cap])
                                    {
                                        string attackerID = val.attackingObjectId;
                                        if (!attackers.Contains(attackerID))
                                        {
                                            attackers.Add(attackerID);
                                        }
                                    }
                                }
                                if (t.ConditionsMet())
                                {
                                    capabilitiesApplied = t.GetAppliedCapabilities();
                                    // Send state change
                                    string          newState = t.state;
                                    SimulationEvent sc       = SimulationEventFactory.BuildEvent(ref simModel, "StateChange");
                                    ((StringValue)sc["ObjectID"]).value = targetID;
                                    ((StringValue)sc["NewState"]).value = newState;
                                    ((IntegerValue)sc["Time"]).value    = time;
                                    distClient.PutEvent(sc);
                                    foreach (string attackerID in attackers)
                                    {
                                        distClient.PutEvent(SimUtility.BuildSystemMessageEvent(ref simModel,
                                                                                               time,
                                                                                               ((StringValue)(objectProxies[attackerID]["OwnerID"].GetDataValue())).value,
                                                                                               attackerID + " has succesfully engaged " + targetID));
                                        distClient.PutEvent(SimUtility.BuildSystemMessageEvent(ref simModel,
                                                                                               time,
                                                                                               ((StringValue)(targetProx["OwnerID"].GetDataValue())).value,
                                                                                               targetID + " has been succesfully engaged by " + attackerID));

                                        ScoringDB.UpdateScore_StateChange(new ScoringDB.ActorFrame(attackerID,
                                                                                                   StateDB.physicalObjects[attackerID].speciesName,
                                                                                                   StateDB.physicalObjects[attackerID].ownerID,
                                                                                                   StateDB.physicalObjects[attackerID].activeRegions),
                                                                          currentState,
                                                                          t.state,
                                                                          new ScoringDB.ActorFrame(targetID,
                                                                                                   StateDB.physicalObjects[targetID].speciesName,
                                                                                                   StateDB.physicalObjects[targetID].ownerID,
                                                                                                   StateDB.physicalObjects[targetID].activeRegions));
                                    }


                                    t.ClearAppliedEffects();

                                    distClient.PutEvent(SimUtility.BuildHistory_AttackedObjectReportEvent(ref simModel,
                                                                                                          time,
                                                                                                          targetID,
                                                                                                          targetLoc,
                                                                                                          true,
                                                                                                          t.state));
                                    distClient.PutEvent(SimUtility.BuildAttackSucceededEvent(ref simModel, time, attackers[0], targetID, newState, capabilitiesApplied));
                                    attackSuccess = true;
                                    break;
                                }
                            }
                            //send messages
                            if (!attackSuccess)
                            {
                                foreach (String attackerID in attackers)
                                {
                                    distClient.PutEvent(SimUtility.BuildSystemMessageEvent(ref simModel,
                                                                                           time,
                                                                                           ((StringValue)(objectProxies[attackerID]["OwnerID"].GetDataValue())).value,
                                                                                           attackerID + " has failed in engagement of " + targetID));
                                    distClient.PutEvent(SimUtility.BuildSystemMessageEvent(ref simModel,
                                                                                           time,
                                                                                           ((StringValue)(targetProx["OwnerID"].GetDataValue())).value,
                                                                                           targetID + " has been unsuccesfully engaged by " + attackerID));
                                }
                                foreach (VulnerabilityValue.Transition t in targetVul.transitions)
                                {
                                    t.ClearAppliedEffects();
                                }

                                distClient.PutEvent(SimUtility.BuildHistory_AttackedObjectReportEvent(ref simModel,
                                                                                                      time,
                                                                                                      targetID,
                                                                                                      targetLoc,
                                                                                                      false,
                                                                                                      ""));
                            }

                            capabilitiesCompleted++;
                            //if there are more capabilities being applied than this one, don't remove target's attack state.
                            if (currentAttackCollection[targetID].Count - capabilitiesCompleted == 0)
                            {// this occurs when all attacks will be removed in this loop
                                dv = targetProx["AttackState"].GetDataValue();
                                ((StringValue)dv).value = "";
                                targetProx["AttackState"].SetDataValue(dv);
                            }
                        }
                    }

                    foreach (string attackerID in attacksToRemove.Keys)
                    {
                        SimulationObjectProxy attackerProxy = objectProxies[attackerID];
                        if (attackerProxy != null)
                        {
                            AttackCollectionValue acv = attackerProxy["CurrentAttacks"].GetDataValue() as AttackCollectionValue;
                            foreach (AttackCollectionValue.AttackValue attackVal in attacksToRemove[attackerID])
                            {
                                if (!acv.RemoveAttack(attackVal))
                                {
                                    acv.RemoveAttack(attackVal.capabilityName, attackVal.targetObjectId, attackVal.attackingObjectId, attackVal.attackStartTime);
                                }
                            }

                            attackerProxy["CurrentAttacks"].SetDataValue(acv);
                            if (((BooleanValue)attackerProxy["IsWeapon"].GetDataValue()).value)
                            {
                                distClient.PutEvent(SimUtility.BuildStateChangeEvent(ref simModel, time, attackerID, "Dead"));
                            }
                        }
                    }

                    if (capabilitiesCompleted > 0)
                    {//some attacks were removed, actually remove them from the currentAttackCollection, update attacker list.
                     //update attack lists (this will require some iteration over the attacks.
                        List <string> attackers = null;
                        dv        = targetProx["AttackerList"].GetDataValue();
                        attackers = ((StringListValue)dv).strings;
                        attackers.Clear();

                        foreach (String capability in currentAttackCollection[targetID].Keys)
                        {
                            foreach (AttackCollectionValue.AttackValue av in currentAttackCollection[targetID][capability])
                            {
                                if (!attackers.Contains(av.attackingObjectId))
                                {
                                    attackers.Add(av.attackingObjectId);
                                }
                            }
                        }

                        ((StringListValue)dv).strings = attackers;
                        targetProx["AttackerList"].SetDataValue(dv);
                    }
                }
            }
        }
Example #7
0
        private void ChangeObjectVulnerability(VulnerabilityValue newVulnerabilitySet, string objectID)
        {
            SimulationObjectProxy obj = objectProxies[objectID];

            if (obj == null)
            {
                return;
            }

            VulnerabilityValue previousVulnerabilityValue = obj["Vulnerability"].GetDataValue() as VulnerabilityValue;
            List <string>      previousVulnerabilityNames = new List <string>();

            foreach (VulnerabilityValue.Transition tr in previousVulnerabilityValue.transitions)
            {
                foreach (VulnerabilityValue.TransitionCondition tc in tr.conditions)
                {
                    if (!previousVulnerabilityNames.Contains(tc.capability))
                    {
                        previousVulnerabilityNames.Add(tc.capability);
                    }
                }
            }
            List <string> newVulnerabilityNames = new List <string>();

            foreach (VulnerabilityValue.Transition tr in newVulnerabilitySet.transitions)
            {
                foreach (VulnerabilityValue.TransitionCondition tc in tr.conditions)
                {
                    if (!newVulnerabilityNames.Contains(tc.capability))
                    {
                        newVulnerabilityNames.Add(tc.capability);
                    }
                }
            }
            List <string> missingVulnerabilityNames = new List <string>();

            foreach (string capName in previousVulnerabilityNames)
            {
                if (!newVulnerabilityNames.Contains(capName))
                {
                    if (!missingVulnerabilityNames.Contains(capName))
                    {
                        missingVulnerabilityNames.Add(capName);
                    }
                }
            }
            List <string> attackerList        = ((StringListValue)obj["AttackerList"].GetDataValue()).strings;
            List <string> updatedAttackerList = new List <string>();

            foreach (string attacker in attackerList)
            {
                //get attacker's current attacks
                AttackCollectionValue currentAttacks = objectProxies[attacker]["CurrentAttacks"].GetDataValue() as AttackCollectionValue;
                List <AttackCollectionValue.AttackValue> attacksOnTarget = currentAttacks.GetCurrentAttacksOnTarget(objectID);
                List <AttackCollectionValue.AttackValue> allAttacks      = currentAttacks.GetCurrentAttacks();

                //foreach attack
                foreach (AttackCollectionValue.AttackValue av in attacksOnTarget)
                {
                    if (missingVulnerabilityNames.Contains(av.capabilityName))
                    {
//if this attack is targetting my object with a capability that's in missingVulnerabilities, cancel the attack
                        CancelAttack(attacker, objectID, av.capabilityName);
                    }
                }
                if (allAttacks.Count > attacksOnTarget.Count)
                {
                    updatedAttackerList.Add(attacker);
                }
            }
            obj["AttackerList"].SetDataValue(DataValueFactory.BuildStringList(updatedAttackerList));
            obj["Vulnerability"].SetDataValue(newVulnerabilitySet);
            previousVulnerabilityNames.Clear();
            newVulnerabilityNames.Clear();
        }