Esempio n. 1
0
        /// <summary>
        /// Process weapon by checking if any ammo used by it is AOE
        /// </summary>
        /// <param name="def"></param>
        private static void ProcessWeapons(ThingDef def)
        {
            CompProperties_AmmoUser props = (CompProperties_AmmoUser)(def.comps?.First(c => c.compClass == typeof(CompAmmoUser)) ?? null);

            if (props?.ammoSet != null)
            {
                isAOEArray[def.index] = isAOEArray[props.ammoSet.index];
            }
        }
Esempio n. 2
0
        public static bool InjectAmmos()
        {
            bool enabled = Controller.settings.EnableAmmoSystem;

            if (enabled)
            {
                // Initialize list of all weapons
                CE_Utility.allWeaponDefs.Clear();

                foreach (ThingDef def in DefDatabase <ThingDef> .AllDefsListForReading)
                {
                    if (def.IsWeapon &&
                        (def.generateAllowChance > 0 ||
                         def.tradeability.TraderCanSell() ||
                         (def.weaponTags != null && def.weaponTags.Contains("TurretGun"))))
                    {
                        CE_Utility.allWeaponDefs.Add(def);
                    }
                }
                if (CE_Utility.allWeaponDefs.NullOrEmpty())
                {
                    Log.Warning("CE Ammo Injector found no weapon defs");
                    return(true);
                }
            }
            else
            {
                //If the ammo system is not enabled and it appears that there are no weaponDefs at all ..
                if (CE_Utility.allWeaponDefs.NullOrEmpty())
                {
                    //.. return out of the method early because nothing has to be reversed ..
                    return(true);
                }
                //.. else, continue the method.
            }

            var ammoDefs = new HashSet <ThingDef>();

            // Find all ammo using guns
            foreach (ThingDef weaponDef in CE_Utility.allWeaponDefs)
            {
                CompProperties_AmmoUser props = weaponDef.GetCompProperties <CompProperties_AmmoUser>();
                if (props != null && props.ammoSet != null && !props.ammoSet.ammoTypes.NullOrEmpty())
                {
                    ammoDefs.UnionWith(props.ammoSet.ammoTypes.Select <AmmoLink, ThingDef>(x => x.ammo));
                }
            }

            // Make sure to exclude all ammo things which double as weapons
            ammoDefs.RemoveWhere(x => x.IsWeapon);

            /*
             * bool canCraft = (AmmoCraftingStation != null);
             *
             * if (!canCraft)
             * {
             *  Log.ErrorOnce("CE ammo injector crafting station is null", 84653201);
             * }
             */

            foreach (AmmoDef ammoDef in ammoDefs)
            {
                // Toggle ammo visibility in the debug menu
                ammoDef.menuHidden    = !enabled;
                ammoDef.destroyOnDrop = !enabled;

                // Toggle trading
                if (ammoDef.tradeTags.Contains(enableTradeTag))
                {
                    ammoDef.tradeability = enabled ? Tradeability.All : Tradeability.Sellable;
                }

                // Toggle craftability
                var craftingTags = ammoDef.tradeTags.Where(t => t.StartsWith(enableCraftingTag));
                if (craftingTags.Any())
                {
                    RecipeDef recipe = DefDatabase <RecipeDef> .GetNamed(("Make" + ammoDef.defName), false);

                    if (recipe == null)
                    {
                        Log.Error("CE ammo injector found no recipe named Make" + ammoDef.defName);
                    }
                    else
                    {
                        // Go through all crafting tags and add to the appropriate benches
                        foreach (string curTag in craftingTags)
                        {
                            ThingDef bench;
                            if (curTag == enableCraftingTag)
                            {
                                bench = CE_ThingDefOf.AmmoBench;
                            }
                            else
                            {
                                // Parse tag for bench def
                                if (curTag.Length <= enableCraftingTag.Length + 1)
                                {
                                    Log.Error("CE :: AmmoInjector trying to inject " + ammoDef.ToString() + " but " + curTag + " is not a valid crafting tag, valid formats are: " + enableCraftingTag + " and " + enableCraftingTag + "_defNameOfCraftingBench");
                                    continue;
                                }
                                var benchName = curTag.Remove(0, enableCraftingTag.Length + 1);
                                bench = DefDatabase <ThingDef> .GetNamed(benchName, false);

                                if (bench == null)
                                {
                                    Log.Error("CE :: AmmoInjector trying to inject " + ammoDef.ToString() + " but no crafting bench with defName=" + benchName + " could be found for tag " + curTag);
                                    continue;
                                }
                            }
                            ToggleRecipeOnBench(recipe, bench);

                            /*
                             * // Toggle recipe
                             * if (enabled)
                             * {
                             *  recipe.recipeUsers.Add(bench);
                             * }
                             * else
                             * {
                             *  recipe.recipeUsers.RemoveAll(x => x.defName == bench.defName);
                             * }
                             */
                        }
                    }
                }
            }

            /*
             *  if (canCraft)
             *  {
             *  // Set ammoCraftingStation.AllRecipes to null so it will reset
             *                  _allRecipesCached.SetValue(AmmoCraftingStation, null);
             *
             *                  // Remove all bills which contain removed ammo types
             *                  if (!enabled)
             *                  {
             *      if (Current.Game != null)
             *      {
             *          IEnumerable<Building> enumerable = Find.Maps.SelectMany(x => x.listerBuildings.AllBuildingsColonistOfDef(AmmoCraftingStation));
             *          foreach (Building current in enumerable)
             *          {
             *              var billGiver = current as IBillGiver;
             *              if (billGiver != null)
             *              {
             *                  for (int i = 0; i < billGiver.BillStack.Count; i++)
             *                  {
             *                      Bill bill = billGiver.BillStack[i];
             *                      if (!AmmoCraftingStation.AllRecipes.Exists(r => bill.recipe == r))
             *                      {
             *                          billGiver.BillStack.Delete(bill);
             *                      }
             *                  }
             *              }
             *          }
             *      }
             *
             *          CE_Utility.allWeaponDefs.Clear();
             *                  }
             * }
             */

            return(true);
        }
Esempio n. 3
0
        public static bool InjectAmmos()
        {
            bool enabled = Controller.settings.EnableAmmoSystem;

            if (enabled)
            {
                // Initialize list of all weapons
                CE_Utility.allWeaponDefs.Clear();

                foreach (ThingDef def in DefDatabase <ThingDef> .AllDefsListForReading)
                {
                    if (def.IsWeapon &&
                        (def.generateAllowChance > 0 ||
                         def.tradeability.TraderCanSell() ||
                         (def.weaponTags != null && def.weaponTags.Contains("TurretGun"))))
                    {
                        CE_Utility.allWeaponDefs.Add(def);
                    }
                }
                if (CE_Utility.allWeaponDefs.NullOrEmpty())
                {
                    Log.Warning("CE Ammo Injector found no weapon defs");
                    return(true);
                }
            }
            else
            {
                //If the ammo system is not enabled and it appears that there are no weaponDefs at all ..
                if (CE_Utility.allWeaponDefs.NullOrEmpty())
                {
                    //.. return out of the method early because nothing has to be reversed ..
                    return(true);
                }
                //.. else, continue the method.
            }

            AddRemoveCaliberFromGunRecipes();

            var ammoDefs = new HashSet <ThingDef>();

            // Find all ammo using guns
            foreach (ThingDef weaponDef in CE_Utility.allWeaponDefs)
            {
                CompProperties_AmmoUser props = weaponDef.GetCompProperties <CompProperties_AmmoUser>();
                if (props != null && props.ammoSet != null && !props.ammoSet.ammoTypes.NullOrEmpty())
                {
                    // Union their ammoTypes -- since ammoDefs is a HashSet, duplicates are automatically removed
                    ammoDefs.UnionWith(props.ammoSet.ammoTypes.Select <AmmoLink, ThingDef>(x => x.ammo));
                }
            }

            /*
             * bool canCraft = (AmmoCraftingStation != null);
             *
             * if (!canCraft)
             * {
             *  Log.ErrorOnce("CE ammo injector crafting station is null", 84653201);
             * }
             */

            // Loop through all weaponDef's unique ammoType.ammo values
            foreach (AmmoDef ammoDef in ammoDefs)
            {
                //AFTER CE_Utility.allWeaponDefs is initiated, this sets each ammo to list its users & special effects in its DEF DESCRIPTION rather than its THING DESCRIPTION.
                //This is because the THING description ISN'T available during crafting - so people can now figure out what's different between ammo types.
                ammoDef.AddDescriptionParts();

                if (ammoDef.tradeTags != null)
                {
                    if (ammoDef.tradeTags.Contains(destroyWithAmmoDisabledTag))
                    {
                        // Toggle ammo visibility in the debug menu
                        ammoDef.menuHidden    = !enabled;
                        ammoDef.destroyOnDrop = !enabled;
                    }

                    //Weapon defs aren't changed w.r.t crafting, trading, destruction on drop -- but the description is still added to the recipe
                    if (ammoDef.IsWeapon)
                    {
                        continue;
                    }

                    //  LX7: Commented this out for now as it's preventing mechanoid ammo from being sold.
                    //  If this is needed for something that can't be accomplished via XML, update it with mech ammo sellability in mind.
                    //if (!ammoDef.Users                                                                          //If there exists NO gun..
                    //    .Any(x => !x.destroyOnDrop                                                              //.. which DOESN'T destroy on drop (e.g all guns destroy on drop)
                    //                || (x.weaponTags != null && x.weaponTags.Contains("TurretGun")              //.. or IS part of a Turret..
                    //                    && DefDatabase<ThingDef>.AllDefs.Where(y => y.building?.turretGunDef == x)                //.. as long as ALL turrets using the gun are non-mechcluster turrets
                    //                        .All(y => !y.building?.buildingTags?.Contains(MechClusterGenerator.MechClusterMemberTag) ?? true))))
                    //    continue;                                                                               //Then this ammo's tradeability and craftability are ignored

                    // Toggle trading
                    var tradingTags = ammoDef.tradeTags.Where(t => t.StartsWith(enableTradeTag));
                    if (tradingTags.Any())
                    {
                        var curTag = tradingTags.First();

                        if (curTag == enableTradeTag)
                        {
                            ammoDef.tradeability = enabled ? Tradeability.All : Tradeability.None;
                        }
                        else
                        {
                            if (curTag.Length <= enableTradeTag.Length + 1)
                            {
                                Log.Error("Combat Extended :: AmmoInjector trying to inject " + ammoDef.ToString() + " but " + curTag + " is not a valid trading tag, valid formats are: " + enableTradeTag + " and " + enableTradeTag + "_levelOfTradeability");
                            }
                            else
                            {
                                var tradeabilityName = curTag.Remove(0, enableTradeTag.Length + 1);

                                ammoDef.tradeability = enabled
                                    ? (Tradeability)Enum.Parse(typeof(Tradeability), tradeabilityName, true)
                                    : Tradeability.None;
                            }
                        }
                    }

                    // Toggle craftability
                    var craftingTags = ammoDef.tradeTags.Where(t => t.StartsWith(enableCraftingTag));
                    if (craftingTags.Any())
                    {
                        RecipeDef recipe = DefDatabase <RecipeDef> .GetNamed(("Make" + ammoDef.defName), false);

                        if (recipe == null)
                        {
                            Log.Error("CE ammo injector found no recipe named Make" + ammoDef.defName);
                        }
                        else
                        {
                            // Go through all crafting tags and add to the appropriate benches
                            foreach (string curTag in craftingTags)
                            {
                                ThingDef bench;
                                if (curTag == enableCraftingTag)
                                {
                                    bench = CE_ThingDefOf.AmmoBench;
                                }
                                else
                                {
                                    // Parse tag for bench def
                                    if (curTag.Length <= enableCraftingTag.Length + 1)
                                    {
                                        Log.Error("Combat Extended :: AmmoInjector trying to inject " + ammoDef.ToString() + " but " + curTag + " is not a valid crafting tag, valid formats are: " + enableCraftingTag + " and " + enableCraftingTag + "_defNameOfCraftingBench");
                                        continue;
                                    }
                                    var benchName = curTag.Remove(0, enableCraftingTag.Length + 1);
                                    bench = DefDatabase <ThingDef> .GetNamed(benchName, false);

                                    if (bench == null)
                                    {
                                        Log.Error("Combat Extended :: AmmoInjector trying to inject " + ammoDef.ToString() + " but no crafting bench with defName=" + benchName + " could be found for tag " + curTag);
                                        continue;
                                    }
                                }
                                ToggleRecipeOnBench(recipe, bench);

                                /*
                                 * // Toggle recipe
                                 * if (enabled)
                                 * {
                                 *  recipe.recipeUsers.Add(bench);
                                 * }
                                 * else
                                 * {
                                 *  recipe.recipeUsers.RemoveAll(x => x.defName == bench.defName);
                                 * }
                                 */
                            }
                        }
                    }
                }
            }

            /*
             *  if (canCraft)
             *  {
             *  // Set ammoCraftingStation.AllRecipes to null so it will reset
             *                  _allRecipesCached.SetValue(AmmoCraftingStation, null);
             *
             *                  // Remove all bills which contain removed ammo types
             *                  if (!enabled)
             *                  {
             *      if (Current.Game != null)
             *      {
             *          IEnumerable<Building> enumerable = Find.Maps.SelectMany(x => x.listerBuildings.AllBuildingsColonistOfDef(AmmoCraftingStation));
             *          foreach (Building current in enumerable)
             *          {
             *              var billGiver = current as IBillGiver;
             *              if (billGiver != null)
             *              {
             *                  for (int i = 0; i < billGiver.BillStack.Count; i++)
             *                  {
             *                      Bill bill = billGiver.BillStack[i];
             *                      if (!AmmoCraftingStation.AllRecipes.Exists(r => bill.recipe == r))
             *                      {
             *                          billGiver.BillStack.Delete(bill);
             *                      }
             *                  }
             *              }
             *          }
             *      }
             *
             *          CE_Utility.allWeaponDefs.Clear();
             *                  }
             * }
             */

            return(true);
        }
Esempio n. 4
0
        public static bool InjectAmmos()
        {
            // Initialize list of all weapons so we don't have to iterate through all the defs, all the time
            CE_Utility.allWeaponDefs.Clear();
            foreach (ThingDef def in DefDatabase <ThingDef> .AllDefsListForReading)
            {
                if (def.IsWeapon && (def.canBeSpawningInventory || def.tradeability == Tradeability.Stockable || def.weaponTags.Contains("TurretGun")))
                {
                    CE_Utility.allWeaponDefs.Add(def);
                }
            }
            if (CE_Utility.allWeaponDefs.NullOrEmpty())
            {
                Log.Warning("CE Ammo Injector found no weapon defs");
                return(true);
            }

            // Find all ammo using guns
            foreach (ThingDef weaponDef in CE_Utility.allWeaponDefs)
            {
                CompProperties_AmmoUser props = weaponDef.GetCompProperties <CompProperties_AmmoUser>();
                if (props != null && props.ammoSet != null && !props.ammoSet.ammoTypes.NullOrEmpty())
                {
                    foreach (ThingDef curDef in props.ammoSet.ammoTypes)
                    {
                        AmmoDef ammoDef = curDef as AmmoDef;
                        if (ammoDef != null)
                        {
                            // Enable trading
                            if (ammoDef.tradeTags.Contains(enableTradeTag))
                            {
                                ammoDef.tradeability = Tradeability.Stockable;
                            }

                            // Enable recipe
                            if (ammoDef.tradeTags.Contains(enableCraftingTag))
                            {
                                RecipeDef recipe = DefDatabase <RecipeDef> .GetNamed(("Make" + ammoDef.defName), false);

                                if (recipe == null)
                                {
                                    Log.Error("CE ammo injector found no recipe named Make" + ammoDef.defName);
                                }
                                else
                                {
                                    if (ammoCraftingStation == null)
                                    {
                                        Log.ErrorOnce("CE ammo injector crafting station is null", 84653201);
                                    }
                                    else
                                    {
                                        if (!recipe.recipeUsers.Contains(ammoCraftingStation))
                                        {
                                            recipe.recipeUsers.Add(ammoCraftingStation);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(true);
        }