public void GetOrderedEffectsByCapabilityTest()
        {
            CapabilityValue target = new CapabilityValue(); // TODO: Initialize to an appropriate value

            CapabilityValue.Effect ef = new CapabilityValue.Effect("Capability1", 1000.0, 10, 1);
            target.effects.Add(ef);
            ef = new CapabilityValue.Effect("Capability2", 2000.0, 20, 1);
            target.effects.Add(ef);
            ef = new CapabilityValue.Effect("Capability1", 2000.0, 20, 1);
            target.effects.Add(ef);
            ef = new CapabilityValue.Effect("Capability1", 500.0, 5, 0.5);
            target.effects.Add(ef);
            ef = new CapabilityValue.Effect("Capability2", 555.0, 6, 0.6);
            target.effects.Add(ef);
            ef = new CapabilityValue.Effect("Capability1", 5000.0, 50, 1);
            target.effects.Add(ef);

            //this shows that the function returns the correctly ordered list for Capability1
            List <CapabilityValue.Effect> actual;

            actual = target.GetOrderedEffectsByCapability("Capability1");
            Assert.AreEqual(actual.Count, 4);
            Assert.AreEqual(actual[0].range, 500.0);
            Assert.AreEqual(actual[1].range, 1000.0);
            Assert.AreEqual(actual[0].intensity, 5);
            Assert.AreEqual(actual[1].intensity, 10);

            //this shows that the function returns the correctly ordered list for Capability2
            actual = target.GetOrderedEffectsByCapability("Capability2");
            Assert.AreEqual(actual.Count, 2);
            Assert.AreEqual(actual[0].range, 555.0);
            Assert.AreEqual(actual[1].range, 2000.0);
            Assert.AreEqual(actual[0].intensity, 6);
            Assert.AreEqual(actual[1].intensity, 20);

//this shows that removal from the returned list doesn't affect the structure of the internal list.
            actual.RemoveAt(1);

            actual = target.effects;
            Assert.AreEqual(actual.Count, 6);
            Assert.AreEqual(actual[0].range, 1000.0);
            Assert.AreEqual(actual[1].range, 2000.0);
            Assert.AreEqual(actual[0].intensity, 10);
            Assert.AreEqual(actual[1].intensity, 20);
        }
        public void TestAttackProcessorSimAttackObjectLogic()
        {
            //SETUP
            CapabilityValue target = new CapabilityValue();

            CapabilityValue.Effect ef = new CapabilityValue.Effect("Capability1", 1000.0, 10, 1);
            target.effects.Add(ef);
            ef = new CapabilityValue.Effect("Capability2", 2000.0, 20, 1);
            target.effects.Add(ef);
            ef = new CapabilityValue.Effect("Capability1", 2000.0, 20, 1);
            target.effects.Add(ef);
            ef = new CapabilityValue.Effect("Capability1", 500.0, 5, 0.5);
            target.effects.Add(ef);
            ef = new CapabilityValue.Effect("Capability2", 555.0, 6, 0.6);
            target.effects.Add(ef);
            ef = new CapabilityValue.Effect("Capability1", 5000.0, 50, 1);
            target.effects.Add(ef);

            double[] ranges = { 200.0, 800.0, 1500.0, 5000.0 };
            int[]    expectedIntensities = { 5, 10, 20, 50 };

            //TEST
            for (int x = 0; x < ranges.Length; x++)//double distance in ranges)
            {
                List <CapabilityValue.Effect> capabilities = target.GetOrderedEffectsByCapability("Capability1");
                bool testComplete = false;
                foreach (CapabilityValue.Effect eff in capabilities)
                {
                    if (eff.name == "Capability1" && ranges[x] <= eff.range)
                    {
                        //int r = random.Next(0, 100);
                        //if (r <= ((int)(eff.probability * 100)))
                        //{
                        //appliedIntensity = (int)(eff.intensity * newAttack.percentageApplied / 100);
                        //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.
                        Assert.AreEqual(eff.intensity, expectedIntensities[x]);
                        testComplete = true;
                        break; //break because we only want to apply the first range found that satisfied the distance restraint
                    }
                }
                Assert.IsTrue(testComplete, System.String.Format("Incorrect value when testing for range {0}", ranges[x]));
            }
            List <CapabilityValue.Effect> capability = target.GetOrderedEffectsByCapability("Capability1");
            bool testCompleted = false;

            foreach (CapabilityValue.Effect eff in capability)
            {
                if (eff.name == "Capability1" && 6000 <= eff.range)
                {
                    //int r = random.Next(0, 100);
                    //if (r <= ((int)(eff.probability * 100)))
                    //{
                    //appliedIntensity = (int)(eff.intensity * newAttack.percentageApplied / 100);
                    //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.

                    testCompleted = true;
                    break; //break because we only want to apply the first range found that satisfied the distance restraint
                }
            }
            Assert.IsFalse(testCompleted, System.String.Format("Incorrect value when testing for range {0}", 6000));
        }
Exemple #3
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));
        }