Exemplo n.º 1
0
 static void Postfix(CompAmmoUser __instance, ref bool __result)
 {
     if (__result == false &&
         (__instance.Wielder != null || __instance.turret != null))
     {
         if (!__instance.HasMagazine && __instance.UseAmmo)
         {
             var def = __instance.GetType().GetField("selectedAmmo", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(__instance) as ThingDef;
             if (CombatExtendedUtil.TryRemoveAmmo(def, 1))
             {
                 //__instance.GetType().GetField("currentAmmoInt", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(__instance, ammo.def as AmmoDef);
                 //__instance.Props.ammoSet
                 __result = true;
             }
         }
         var ammoTypes = __instance.Props?.ammoSet?.ammoTypes;
         if (ammoTypes != null)
         {
             foreach (var t in ammoTypes)
             {
                 if (CombatExtendedUtil.HasAmmo(t.projectile))
                 {
                     __result = true;
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
        public bool UseAmmo(ThingWithComps thingWithComps)
        {
            CompAmmoUser comp = thingWithComps.TryGetComp <CompAmmoUser>();

            if (comp.UseAmmo)
            {
                return(true);
            }
            return(false);
        }
Exemplo n.º 3
0
        public int getAmount(ThingWithComps thingWithComps)
        {
            CompAmmoUser comp         = thingWithComps.TryGetComp <CompAmmoUser>();
            int          amountNeeded = comp.Props.magazineSize;

            if (comp.CurrentAmmo == comp.SelectedAmmo)
            {
                amountNeeded -= comp.CurMagCount;
            }
            return(amountNeeded);
        }
        private CompAmmoUser GetAmmo()
        {
            if (_ammo == null)
            {
                _ammo = GetGun()?.TryGetComp <CompAmmoUser>();
            }
            if (_ammo != null)
            {
                _ammo.turret = this;
            }

            return(_ammo);
        }
 internal static bool HasHuntingWeapon(Pawn p)
 {
     if (p.equipment.Primary != null && p.equipment.Primary.def.IsRangedWeapon)
     {
         CompAmmoUser comp = p.equipment.Primary.TryGetComp <CompAmmoUser>();
         if (comp == null ||
             !comp.useAmmo ||
             (comp.hasMagazine && comp.curMagCount > 0) ||
             comp.hasAmmo)
         {
             return(true);
         }
     }
     return(false);
 }
Exemplo n.º 6
0
 public static void Postfix(ref bool __result, Pawn p)
 {
     if (__result)
     {
         // Check if gun has ammo first
         CompAmmoUser comp = p.equipment.Primary.TryGetComp <CompAmmoUser>();
         __result = comp == null || comp.CanBeFiredNow || comp.HasAmmo;
     }
     else
     {
         // Change result to true if we have melee weapon and melee hunting is allowed in settings
         ThingWithComps eq = p.equipment.Primary;
         __result = eq != null && Controller.settings.AllowMeleeHunting && eq.def.IsMeleeWeapon;
     }
 }
        // Functions like a prefix.  If this has something to do return false. if nothing to do return true.
        static bool CheckReload(Verb __instance)
        {
            if (!(__instance is Verb_ShootCE || __instance is Verb_ShootCEOneUse))
            {
                return(true);                // no work to do as the verb isn't the right kind.
            }
            CompAmmoUser gun = __instance.ownerEquipment.TryGetComp <CompAmmoUser>();

            if (gun == null || !gun.HasMagazine || gun.CurMagCount > 0)
            {
                return(true);                // gun isn't an ammo user that stores ammo internally or isn't out of bullets.
            }
            // we got work to do at this point.
            // Try starting the reload job.
            gun.TryStartReload();
            return(false);
        }
Exemplo n.º 8
0
 static void Prefix(CompAmmoUser __instance)
 {
     if (__instance.turret == null &&
         __instance.Wielder != null &&
         __instance.HasMagazine)
     {
         AmmoDef ammoDef = __instance.SelectedAmmo;//__instance.GetType().GetProperty("CurrentAmmo", BindingFlags.Instance | BindingFlags.Public).GetValue(__instance, null) as Def;
         if (ammoDef != null &&
             CombatExtendedUtil.HasAmmo(ammoDef))
         {
             if (!__instance.TryFindAmmoInInventory(out Thing ammo))
             {
                 int magazineSize = __instance.Props.magazineSize;
                 if (CombatExtendedUtil.TryRemoveAmmo(ammoDef, magazineSize, out ammo))
                 {
                     __instance.CompInventory.ammoList.Add(ammo as ThingWithComps);
                 }
             }
         }
     }
 }
        internal static bool TryStartAttack(this Pawn_EquipmentTracker _this, LocalTargetInfo targ)
        {
            Pawn pawn = (Pawn)pawnFieldInfo.GetValue(_this);

            if (pawn.stances.FullBodyBusy)
            {
                return(false);
            }
            if (pawn.story != null && pawn.story.DisabledWorkTags.Contains(WorkTags.Violent))
            {
                return(false);
            }
            bool allowManualCastWeapons = !pawn.IsColonist;
            Verb verb = pawn.TryGetAttackVerb(allowManualCastWeapons);

            // Check for reload before attacking
            if (_this.PrimaryEq != null && verb != null && verb == _this.PrimaryEq.PrimaryVerb)
            {
                if (_this.Primary != null)
                {
                    CompAmmoUser compAmmo = _this.Primary.TryGetComp <CompAmmoUser>();
                    if (compAmmo != null)
                    {
                        if (!compAmmo.hasMagazine)
                        {
                            if (compAmmo.useAmmo && !compAmmo.hasAmmo)
                            {
                                return(false);
                            }
                        }
                        else if (compAmmo.curMagCount <= 0)
                        {
                            compAmmo.TryStartReload();
                            return(false);
                        }
                    }
                }
            }
            return(verb != null && verb.TryStartCastOn(targ, false, true));
        }
Exemplo n.º 10
0
        static bool ReloadCheck(Verb verb, LocalTargetInfo targ, bool surpriseAttack, bool canFreeIntercept, Pawn_EquipmentTracker __instance)
        {
            // this is no longer valid: ThingWithComps primaryInt = Traverse.Create(__instance).Field("primaryInt").GetValue() as ThingWithComps;
            ThingOwner <ThingWithComps> equipment = Traverse.Create(__instance).Field("equipment").GetValue <ThingOwner <ThingWithComps> >();


            if (equipment != null && __instance.PrimaryEq != null && verb != null && verb == __instance.PrimaryEq.PrimaryVerb)
            {
                if (__instance.Primary != null)
                {
                    CompAmmoUser compAmmo = __instance.Primary.TryGetComp <CompAmmoUser>();
                    if (compAmmo != null && !compAmmo.CanBeFiredNow)
                    {
                        if (compAmmo.HasAmmo)
                        {
                            compAmmo.TryStartReload();
                        }
                        return(false);
                    }
                }
            }
            return(verb != null && verb.TryStartCastOn(targ, surpriseAttack, canFreeIntercept));
        }
Exemplo n.º 11
0
        internal static bool HasHuntingWeapon(Pawn p)
        {
            if (p.equipment.Primary != null)
            {
                CompAmmoUser comp = p.equipment.Primary.TryGetComp <CompAmmoUser>();
                if (comp == null ||
                    !comp.useAmmo ||
                    (comp.hasMagazine && comp.curMagCount > 0) ||
                    comp.hasAmmo)
                {
                    return(true);
                }
            }
            List <Hediff> hediffs = p.health.hediffSet.hediffs;

            for (int i = 0; i < hediffs.Count; i++)
            {
                if (hediffs[i].def.addedPartProps != null && hediffs[i].def.addedPartProps.isGoodWeapon)
                {
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 12
0
 static public bool ShouldReload(Pawn p)
 {
     //For mechanoids replace the check of is p.RaceProps.HumanLike by custom logic
     if (p.RaceProps.IsMechanoid && p.IsHacked())
     {
         //return true when a mechanoid is hacked and does not have much ammo.
         CompInventory  inventory = p.TryGetComp <CompInventory>();
         ThingWithComps primaryEq = p.equipment.Primary;
         if (inventory != null && primaryEq != null)
         {
             CompAmmoUser ammoUser = primaryEq.TryGetComp <CompAmmoUser>();
             if (ammoUser != null)
             {
                 int ammoCount = inventory.AmmoCountOfDef(ammoUser.CurrentAmmo);
                 int minAmmo   = ammoUser.Props.magazineSize == 0 ? 10 : ammoUser.Props.magazineSize; //No magic numbers?
                 if (ammoCount < minAmmo)
                 {
                     return(true);
                 }
             }
         }
     }
     return(p.RaceProps.Humanlike);
 }
Exemplo n.º 13
0
        public bool ammoNeeded(Pawn vehicle, ThingWithComps thingWithComps)
        {
            CompAmmoUser comp = vehicle.equipment.Primary.TryGetComp <CompAmmoUser>();

            if (comp != null)
            {
                if (comp.HasMagazine && !(comp.CurMagCount < comp.Props.magazineSize || comp.SelectedAmmo != comp.CurrentAmmo))
                {
                    return(false);
                }
                if (!comp.UseAmmo)
                {
                    return(true);
                }
                Thing ammo = GenClosest.ClosestThingReachable(vehicle.Position, vehicle.Map,
                                                              ThingRequest.ForDef(comp.SelectedAmmo),
                                                              PathEndMode.ClosestTouch,
                                                              TraverseParms.For(vehicle, Danger.Deadly, TraverseMode.ByPawn),
                                                              80,
                                                              x => !x.IsForbidden(vehicle) && vehicle.CanReserve(x));
                return(ammo != null);
            }
            return(false);
        }
Exemplo n.º 14
0
        public Job returnJob(Thing t, Pawn pawn, Pawn vehicle)
        {
            CompAmmoUser comp = vehicle.equipment.Primary.TryGetComp <CompAmmoUser>();

            if (vehicle == null || comp == null)
            {
                return(null);
            }

            if (!comp.UseAmmo)

            {
                return(new Job(DefDatabase <JobDef> .GetNamed("ReloadWeaponOnVehicle", true), t, null));
            }

            // Iterate through all possible ammo types for NPC's to find whichever is //available, /startingwit /currently /selected
            Thing ammo      = null;
            var   ammoTypes = new List <AmmoDef>();

            try
            {
                foreach (var a in comp.Props.ammoSet.ammoTypes)
                {
                    if (a.ammo != null)
                    {
                        ammoTypes.Add(a.ammo);
                    }
                }
            }
            catch
            {
            }
            var index = ammoTypes.IndexOf(comp.SelectedAmmo);

            for (int i = 0; i < ammoTypes.Count; i++)
            {
                index = (index + i) % ammoTypes.Count;
                ammo  = GenClosest.ClosestThingReachable(pawn.Position, pawn.Map,
                                                         ThingRequest.ForDef(ammoTypes[index]),
                                                         PathEndMode.ClosestTouch,
                                                         TraverseParms.For(pawn, Danger.Deadly, TraverseMode.ByPawn),
                                                         80,
                                                         x => !x.IsForbidden(pawn) && pawn.CanReserve(x));
                if (ammo != null || pawn.Faction == Faction.OfPlayer)
                {
                    break;
                }
            }
            if (ammo == null)
            {
                return(null);
            }

            // Update selected ammo if necessary
            if (ammo.def != comp.SelectedAmmo)
            {
                comp.SelectedAmmo = ammo.def as AmmoDef;
            }

            // Create the actual job
            int amountNeeded = comp.Props.magazineSize;

            if (comp.CurrentAmmo == comp.SelectedAmmo)
            {
                amountNeeded -= comp.CurMagCount;
            }
            return(new Job(DefDatabase <JobDef> .GetNamed("ReloadWeaponOnVehicle"), t, ammo)
            {
                count = Mathf.Min(amountNeeded, ammo.stackCount)
            });
        }