Example #1
0
        private bool TryFindAmmoInInventory(out Thing ammoThing)
        {
            ammoThing = null;
            if (compInventory == null)
            {
                return(false);
            }

            // Try finding suitable ammoThing for currently set ammo first
            ammoThing = compInventory.ammoList.Find(thing => thing.def == selectedAmmo);
            if (ammoThing != null)
            {
                return(true);
            }

            // Try finding ammo from different type
            foreach (AmmoDef ammoDef in Props.ammoSet.ammoTypes)
            {
                ammoThing = compInventory.ammoList.Find(thing => thing.def == ammoDef);
                if (ammoThing != null)
                {
                    selectedAmmo = ammoDef;
                    return(true);
                }
            }
            return(false);
        }
Example #2
0
        public override void Initialize(CompProperties vprops)
        {
            base.Initialize(vprops);

            curMagCountInt = Props.spawnUnloaded ? 0 : Props.magazineSize;

            // Initialize ammo with default if none is set
            if (useAmmo)
            {
                if (Props.ammoSet.ammoTypes.NullOrEmpty())
                {
                    Log.Error(this.parent.Label + " has no available ammo types");
                }
                else
                {
                    if (currentAmmoInt == null)
                    {
                        currentAmmoInt = (AmmoDef)Props.ammoSet.ammoTypes[0];
                    }
                    if (selectedAmmo == null)
                    {
                        selectedAmmo = currentAmmoInt;
                    }
                }
            }
        }
Example #3
0
        protected override float GetWeightForDef(ThingDef def)
        {
            float   weight = 1;
            AmmoDef ammo   = def as AmmoDef;

            if (ammo != null && ammo.ammoClass.advanced)
            {
                weight *= 0.2f;
            }
            return(weight);
        }
Example #4
0
        private FloatMenu MakeAmmoMenu()
        {
            List <ThingDef> ammoList = new List <ThingDef>();      // List of all ammo types the gun can use and the pawn has in his inventory

            if (compAmmo.turret != null)
            {
                // If we have no inventory available (e.g. manned turret), add all possible ammo types to the selection
                ammoList.AddRange(compAmmo.Props.ammoSet.ammoTypes);
            }
            else
            {
                // Iterate through all suitable ammo types and check if they're in our inventory
                foreach (ThingDef curAmmoDef in compAmmo.Props.ammoSet.ammoTypes)
                {
                    if (compAmmo.compInventory.ammoList.Any(x => x.def == curAmmoDef))
                    {
                        ammoList.Add(curAmmoDef);
                    }
                }
            }

            // Append float menu options for every available ammo type
            List <FloatMenuOption> floatOptionList = new List <FloatMenuOption>();

            if (ammoList.NullOrEmpty())
            {
                floatOptionList.Add(new FloatMenuOption("CR_OutOfAmmo".Translate(), null));
            }
            else
            {
                foreach (ThingDef curDef in ammoList)
                {
                    AmmoDef ammoDef = (AmmoDef)curDef;
                    floatOptionList.Add(new FloatMenuOption(ammoDef.ammoClass.LabelCap, new Action(delegate { compAmmo.selectedAmmo = ammoDef; })));
                }
            }
            return(new FloatMenu(floatOptionList));
        }
Example #5
0
        public static bool InjectAmmos()
        {
            // Initialize list of all weapons so we don't have to iterate through all the defs, all the time
            CR_Utility.allWeaponDefs.Clear();
            foreach (ThingDef def in DefDatabase <ThingDef> .AllDefsListForReading)
            {
                if (def.IsWeapon && (def.canBeSpawningInventory || def.tradeability == Tradeability.Stockable || def.weaponTags.Contains("TurretGun")))
                {
                    CR_Utility.allWeaponDefs.Add(def);
                }
            }
            if (CR_Utility.allWeaponDefs.NullOrEmpty())
            {
                Log.Warning("CR Ammo Injector found no weapon defs");
                return(true);
            }

            // Find all ammo using guns
            foreach (ThingDef weaponDef in CR_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("CR ammo injector found no recipe named Make" + ammoDef.defName);
                                }
                                else
                                {
                                    if (ammoCraftingStation == null)
                                    {
                                        Log.ErrorOnce("CR ammo injector crafting station is null", 84653201);
                                    }
                                    else
                                    {
                                        if (!recipe.recipeUsers.Contains(ammoCraftingStation))
                                        {
                                            recipe.recipeUsers.Add(ammoCraftingStation);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(true);
        }
Example #6
0
        public void LoadAmmo(Thing ammo = null)
        {
            if (wielder == null && turret == null)
            {
                Log.Error(parent.ToString() + " tried loading ammo with no owner");
                return;
            }

            int newMagCount;

            if (useAmmo)
            {
                Thing ammoThing;
                bool  ammoFromInventory = false;
                if (ammo == null)
                {
                    if (!TryFindAmmoInInventory(out ammoThing))
                    {
                        this.DoOutOfAmmoAction();
                        return;
                    }
                    ammoFromInventory = true;
                }
                else
                {
                    ammoThing = ammo;
                }
                currentAmmoInt = (AmmoDef)ammoThing.def;
                if (Props.magazineSize < ammoThing.stackCount)
                {
                    newMagCount           = Props.magazineSize;
                    ammoThing.stackCount -= Props.magazineSize;
                    if (compInventory != null)
                    {
                        compInventory.UpdateInventory();
                    }
                }
                else
                {
                    newMagCount = ammoThing.stackCount;
                    if (ammoFromInventory)
                    {
                        compInventory.container.Remove(ammoThing);
                    }
                    else if (!ammoThing.Destroyed)
                    {
                        ammoThing.Destroy();
                    }
                }
            }
            else
            {
                newMagCount = Props.magazineSize;
            }
            curMagCountInt = newMagCount;
            if (turret != null)
            {
                turret.isReloading = false;
            }
            if (parent.def.soundInteract != null)
            {
                parent.def.soundInteract.PlayOneShot(SoundInfo.InWorld(position));
            }
            if (Props.throwMote)
            {
                MoteThrower.ThrowText(position.ToVector3Shifted(), "CR_ReloadedMote".Translate());
            }
        }