static public List <ComponentLocator> GetWeaponComponentLocatorList(Mech m)
        {
            List <ComponentLocator> compLocList = new List <ComponentLocator>();

            foreach (ChassisLocations loc in System.Enum.GetValues(typeof(ChassisLocations)))
            {
                if ((loc == ChassisLocations.All) ||
                    (loc == ChassisLocations.Arms) ||
                    (loc == ChassisLocations.Legs) ||
                    (loc == ChassisLocations.Torso) ||
                    (loc == ChassisLocations.None))
                {
                    continue;
                }

                int inventorySlotsCount = m.MechDef.GetChassisLocationDef(loc).InventorySlots;
                if (inventorySlotsCount == 0)
                {
                    continue;
                }

                for (int slotIndex = 0; slotIndex < inventorySlotsCount; ++slotIndex)
                {
                    MechComponent component = m.GetComponentInSlot(loc, slotIndex);
                    if (component is Weapon)
                    {
                        ComponentLocator compLoc = new ComponentLocator(m, loc, slotIndex);
                        compLocList.Add(compLoc);
                    }
                }
            }
            return(compLocList);
        }
        static Dictionary <ComponentLocator, float> getComponentDictionary(Mech targetMech, ChassisLocations chassisLoc)
        {
            Dictionary <ComponentLocator, float> componentDict = new Dictionary <ComponentLocator, float>();

            int inventorySlotsCount = targetMech.MechDef.GetChassisLocationDef(chassisLoc).InventorySlots;

            if (inventorySlotsCount == 0)
            {
                return(componentDict);
            }

            float slotProbability = 1.0f / inventorySlotsCount;

            for (int slotIndex = 0; slotIndex < inventorySlotsCount; ++slotIndex)
            {
                MechComponent component = targetMech.GetComponentInSlot(chassisLoc, slotIndex);
                if (component != null)
                {
                    ComponentLocator compLoc = new ComponentLocator(targetMech, chassisLoc, slotIndex);
                    componentDict[compLoc] = slotProbability;
                }
            }

            return(componentDict);
        }
        public void AddComponentDamage(float damage, ComponentLocator componentLoc)
        {
            float existingDamage = 0.0f;

            if (componentDamageDictionary.ContainsKey(componentLoc))
            {
                existingDamage = componentDamageDictionary[componentLoc];
            }
            componentDamageDictionary[componentLoc] = existingDamage + damage;
        }
        static void NukeMechLocation(ref DamageExpectationRecord damageExpectationRecord, Mech targetMech, ChassisLocations loc)
        {
            // foreach component, destroy it
            Dictionary <ComponentLocator, float> componentDict = getComponentDictionary(targetMech, loc);

            foreach (KeyValuePair <ComponentLocator, float> componentKVP in componentDict)
            {
                ComponentLocator compLoc = componentKVP.Key;
                damageExpectationRecord.AddComponentDamage(2.0f, compLoc);
            }
        }
        static void NukeUnit(ref DamageExpectationRecord damageExpectationRecord, Turret targetTurret)
        {
            damageExpectationRecord.lethalProbability = 1.0f;

            List <Weapon> weaponList = targetTurret.Weapons;

            for (int slotIndex = 0; slotIndex < weaponList.Count; ++slotIndex)
            {
                ComponentLocator compLoc = new ComponentLocator(targetTurret, slotIndex);
                damageExpectationRecord.AddComponentDamage(2.0f, compLoc);
            }
        }
        static public List <ComponentLocator> GetWeaponComponentLocatorList(Turret t)
        {
            List <ComponentLocator> compLocList = new List <ComponentLocator>();

            List <Weapon> weaponList = t.Weapons;

            for (int i = 0; i < weaponList.Count; ++i)
            {
                ComponentLocator loc = new ComponentLocator(t, i);
                compLocList.Add(loc);
            }
            return(compLocList);
        }
        static public float EvaluateFirepowerReductionFromAttack(AbstractActor attacker, Vector3 attackerPosition, ICombatant target, Vector3 targetPosition, Quaternion targetRotation, List <Weapon> weapons, MeleeAttackType attackType)
        {
            AbstractActor actor = target as AbstractActor;

            if (actor == null)
            {
                return(0.0f);
            }

            DamageExpectationRecord damageExpectationRecord = EvaluateAttack(attacker, attackerPosition, target, targetPosition, targetRotation, weapons, attackType);

            float dmg = 0.0f;
            List <ComponentLocator> weaponList = GetWeaponComponentLocatorList(actor);

            for (int weaponIndex = 0; weaponIndex < weaponList.Count; ++weaponIndex)
            {
                ComponentLocator compLoc  = weaponList[weaponIndex];
                MechComponent    mechComp = compLoc.GetComponent();
                Weapon           w        = mechComp as Weapon;

                if (w.CanFire)
                {
                    float weaponBaseDamage = w.ShotsWhenFired * w.DamagePerShot;
                    if (w.DamageLevel == ComponentDamageLevel.Functional)
                    {
                        int expDmg = Mathf.RoundToInt(damageExpectationRecord.GetComponentDamageForLocation(compLoc));
                        if (expDmg == 1)
                        {
                            // that's like half damage
                            dmg += weaponBaseDamage * 0.5f;
                        }
                        else if (expDmg > 1)
                        {
                            dmg += weaponBaseDamage;
                        }
                    }
                    else if (w.DamageLevel == ComponentDamageLevel.Penalized)
                    {
                        int expDmg = Mathf.RoundToInt(damageExpectationRecord.GetComponentDamageForLocation(compLoc));
                        if (expDmg >= 1)
                        {
                            dmg += weaponBaseDamage;
                        }
                    }
                }
            }
            return(dmg);
        }
        public float GetComponentDamageForLocation(ComponentLocator compLoc)
        {
            float dmg = 0.0f;

            if (componentDamageDictionary.ContainsKey(compLoc))
            {
                dmg = componentDamageDictionary[compLoc];
            }

            for (int childIndex = 0; childIndex < children.Count; ++childIndex)
            {
                ChildWithProbability c = children[childIndex];
                dmg += c.DamageExpectationRecord.GetComponentDamageForLocation(compLoc) * c.Probability;
            }
            return(dmg);
        }
        public void flattenInto(DamageExpectationRecord target, DamageExpectationRecord source, float probability)
        {
            foreach (KeyValuePair <ComponentLocator, float> kvp in source.componentDamageDictionary)
            {
                ComponentLocator key = kvp.Key;
                float            dmg = kvp.Value;
                target.AddComponentDamage(dmg * probability, key);
            }

            foreach (ChassisLocations loc in source.chassisLocationDictionary.Keys)
            {
                float dmg = source.chassisLocationDictionary[loc];
                target.AddStructureDamage(dmg * probability, loc);
            }

            foreach (ArmorLocation loc in source.armorLocationDictionary.Keys)
            {
                float dmg = source.armorLocationDictionary[loc];
                target.AddArmorDamage(dmg * probability, loc);
            }

            foreach (VehicleChassisLocations loc in source.vehicleChassisLocationDictionary.Keys)
            {
                float dmg = source.vehicleChassisLocationDictionary[loc];
                target.AddVehicleStructureDamage(dmg * probability, loc);
            }

            foreach (VehicleChassisLocations loc in source.vehicleArmorLocationDictionary.Keys)
            {
                float dmg = source.vehicleArmorLocationDictionary[loc];
                target.AddVehicleArmorDamage(dmg * probability, loc);
            }

            target.AddPilotDamage(pilotDamage * probability);
            target.lethalProbability += lethalProbability * probability;

            for (int childIndex = 0; childIndex < source.children.Count; ++childIndex)
            {
                ChildWithProbability c = source.children[childIndex];
                flattenInto(target, c.DamageExpectationRecord, c.Probability);
            }
        }
        static void evaluateWeaponAttackOnMech(float expectedDamage, Weapon w, ref DamageExpectationRecord damageExpectationRecord, Vector3 attackerPosition, Mech targetMech, Vector3 targetPosition, Quaternion targetRotation)
        {
            // use hit table to figure out where this will go
            Dictionary <ArmorLocation, float> locations = GetLocationDictionary(attackerPosition, targetMech, targetPosition, targetRotation);

            foreach (KeyValuePair <ArmorLocation, float> locKVP in locations)
            {
                ArmorLocation loc         = locKVP.Key;
                float         probability = locKVP.Value;

                DamageExpectationRecord locRecord = new DamageExpectationRecord();
                damageExpectationRecord.AddChildRecord(probability, locRecord);

                float existingArmor          = targetMech.ArmorForLocation((int)loc);
                float armorThatWillBeRemoved = Mathf.Min(existingArmor, expectedDamage);
                float damageRemaining        = expectedDamage - existingArmor;
                locRecord.AddArmorDamage(armorThatWillBeRemoved, loc);

                ChassisLocations sLoc = MechStructureRules.GetChassisLocationFromArmorLocation((ArmorLocation)loc);

                // there's a chance this hit will be a critical hit
                if (!targetMech.IsLocationDestroyed(sLoc))
                {
                    float critChance = targetMech.Combat.CritChance.GetCritChance(targetMech, sLoc, w);

                    if (critChance > 0)
                    {
                        DamageExpectationRecord critRecord = new DamageExpectationRecord();
                        locRecord.AddChildRecord(critChance, critRecord);

                        // iterate over components, apply one point of damage to each location.

                        Dictionary <ComponentLocator, float> componentDict = getComponentDictionary(targetMech, sLoc);

                        float probOfHittingAmmo = 0.0f;
                        foreach (KeyValuePair <ComponentLocator, float> componentKVP in componentDict)
                        {
                            ComponentLocator compLoc              = componentKVP.Key;
                            MechComponent    component            = compLoc.GetComponent();
                            float            componentProbability = componentKVP.Value;

                            DamageExpectationRecord componentRecord = new DamageExpectationRecord();
                            critRecord.AddChildRecord(componentProbability, componentRecord);

                            componentRecord.AddComponentDamage(1.0f, compLoc);

                            // if this component is ammo, there's a chance we could lose this location and all child locations
                            if (component.componentType == ComponentType.AmmunitionBox)
                            {
                                AmmunitionBox abComponent   = component as AmmunitionBox;
                                int           remainingAmmo = abComponent.CurrentAmmo;
                                int           capacity      = abComponent.ammunitionBoxDef.Capacity;
                                float         percentage    = ((float)remainingAmmo) / ((float)capacity);

                                if (percentage > 0.5f)
                                {
                                    probOfHittingAmmo += componentProbability;
                                }
                            }
                        }
                        if (probOfHittingAmmo > 0.0f)
                        {
                            DamageExpectationRecord ammoBlownRecord = new DamageExpectationRecord();
                            locRecord.AddChildRecord(probOfHittingAmmo, ammoBlownRecord);

                            foreach (KeyValuePair <ComponentLocator, float> componentKVP in componentDict)
                            {
                                ComponentLocator compLoc = componentKVP.Key;
                                ammoBlownRecord.AddComponentDamage(2.0f, compLoc);
                            }
                        }
                    }
                }

                if (damageRemaining > 0)
                {
                    // some goes in to the structure
                    float currentStructure = targetMech.GetCurrentStructure(sLoc);

                    float structureDamage      = Mathf.Min(damageRemaining, currentStructure);
                    float damageAfterStructure = damageRemaining - structureDamage;

                    locRecord.AddStructureDamage(structureDamage, sLoc);

                    if (damageAfterStructure > 0)
                    {
                        // some hits a component
                        Dictionary <ComponentLocator, float> componentDict = getComponentDictionary(targetMech, sLoc);

                        float probOfHittingAmmo = 0.0f;

                        foreach (KeyValuePair <ComponentLocator, float> componentKVP in componentDict)
                        {
                            ComponentLocator compLoc              = componentKVP.Key;
                            MechComponent    component            = compLoc.GetComponent();
                            float            componentProbability = componentKVP.Value;

                            DamageExpectationRecord componentRecord = new DamageExpectationRecord();
                            locRecord.AddChildRecord(componentProbability, componentRecord);

                            componentRecord.AddComponentDamage(1.0f, compLoc);

                            // if this component is ammo, there's a chance we could lose this location and all child locations
                            if (component.componentType == ComponentType.AmmunitionBox)
                            {
                                AmmunitionBox abComponent   = component as AmmunitionBox;
                                int           remainingAmmo = abComponent.CurrentAmmo;
                                int           capacity      = abComponent.ammunitionBoxDef.Capacity;
                                float         percentage    = ((float)remainingAmmo) / ((float)capacity);

                                if (percentage > 0.5f)
                                {
                                    probOfHittingAmmo += componentProbability;
                                }
                            }
                        }

                        if (probOfHittingAmmo > 0)
                        {
                            DamageExpectationRecord ammoBlownRecord = new DamageExpectationRecord();
                            locRecord.AddChildRecord(probOfHittingAmmo, ammoBlownRecord);

                            foreach (KeyValuePair <ComponentLocator, float> componentKVP in componentDict)
                            {
                                ComponentLocator compLoc = componentKVP.Key;
                                ammoBlownRecord.AddComponentDamage(2.0f, compLoc);
                            }
                        }
                    }
                }
            }
        }
 public void DestroyComponent(ComponentLocator compLoc)
 {
     componentDamageDictionary[compLoc] = MAX_COMPONENT_DAMAGE;
 }