static void Postfix(Pawn_MeleeVerbs __instance, Thing target) { if (Settings.AutoSwitchMelee == false) { return; } Pawn pawn = __instance.Pawn; if (pawn != null && WorldComp.TryGetAssignedWeapons(pawn, out AssignedWeaponContainer c)) { Verb attackVerb = pawn.TryGetAttackVerb(target, !pawn.IsColonist); if (attackVerb != null && attackVerb.verbProps.IsMeleeAttack) { float blunt = target.GetStatValue(StatDefOf.ArmorRating_Blunt); float sharp = target.GetStatValue(StatDefOf.ArmorRating_Sharp); DamageType dt = (blunt > sharp) ? DamageType.Sharp : DamageType.Blunt; if (blunt == sharp) { if (Settings.PreferredDamageType == PreferredDamageTypeEnum.WeaponStorage_None) { return; } dt = (Settings.PreferredDamageType == PreferredDamageTypeEnum.ArmorBlunt) ? DamageType.Blunt : DamageType.Sharp; } if (TryGetBestWeapon(dt, pawn.equipment.Primary, c, out ThingWithComps bestWeapon)) { HarmonyPatchUtil.EquipWeapon(bestWeapon, pawn, c); } } } }
static void Postfix(ref bool __result, ref Pawn __state, ThingWithComps eq) { if (__state != null) { if (WorldComp.TryGetAssignedWeapons(__state, out AssignedWeaponContainer c) && c.Contains(eq)) { if (!Settings.AllowPawnsToDropWeapon) { if (!WorldComp.Add(eq)) { Log.Warning($"unable to find weapon storage that can hold {eq.ThingID} so it will be dropped."); WorldComp.Drop(eq); } } else { if (c.Remove(eq)) { if (eq.def.IsRangedWeapon) { if (!HarmonyPatchUtil.EquipRanged(c)) { HarmonyPatchUtil.EquipMelee(c); } } else { if (!HarmonyPatchUtil.EquipMelee(c)) { HarmonyPatchUtil.EquipRanged(c); } } } if (Settings.PlaceDroppedWeaponsInStorage) { if (!WorldComp.Add(eq)) { Log.Warning($"unable to find weapon storage that can hold {eq.ThingID} so it will be dropped."); WorldComp.Drop(eq); } } } } else { foreach (SharedWeaponFilter swf in WorldComp.SharedWeaponFilter) { if (swf.Allows(eq) && !WorldComp.Add(eq)) { Log.Warning($"unable to find weapon storage that can hold {eq.ThingID} so it will be dropped."); WorldComp.Drop(eq); break; } } } } }
static void Postfix(Pawn_DraftController __instance) { Pawn pawn = __instance.pawn; if (WorldComp.TryGetAssignedWeapons(pawn, out AssignedWeaponContainer weapons) && weapons.TryGetLastThingUsed(pawn, out ThingWithComps w)) { HarmonyPatchUtil.EquipWeapon(w, pawn, weapons); } }
static void Prefix(Pawn_HealthTracker __instance, ref ThingWithComps __state) { if (Settings.EnableAssignWeapons && pawnFI.GetValue(__instance) is Pawn pawn && !__instance.Downed && pawn.IsColonist && pawn.equipment?.Primary != null) { if (WorldComp.TryGetAssignedWeapons(pawn, out AssignedWeaponContainer c)) { HarmonyPatchUtil.UnequipPrimaryWeapon(pawn, c); } } }
static void Prefix(Pawn_HealthTracker __instance) { if (!Settings.EnableAssignWeapons) { return; } Pawn pawn = (Pawn)__instance.GetType().GetField( "pawn", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(__instance); if (pawn != null && pawn.Faction == Faction.OfPlayer && pawn.def.race.Humanlike) { if (WorldComp.TryGetAssignedWeapons(pawn, out AssignedWeaponContainer c) && pawn.equipment?.Primary == null) { if (c.TryGetLastThingUsed(pawn, out ThingWithComps w)) { HarmonyPatchUtil.EquipWeapon(w, pawn, c); } } } }
static void Postfix(Pawn_DraftController __instance, ref IEnumerable <Gizmo> __result) { try { Pawn pawn = __instance.pawn; if (pawn.Faction == Faction.OfPlayer) { List <Gizmo> l = null; if (Settings.ShowWeaponStorageButtonForPawns && WorldComp.HasStorages()) { l = new List <Gizmo>() { new Command_Action { icon = AssignUI.weaponStorageTexture, defaultLabel = "WeaponStorage.UseWeaponStorage".Translate(), activateSound = SoundDef.Named("Click"), action = delegate { Find.WindowStack.Add(new AssignUI(null, pawn)); } } }; l.AddRange(__result); } if (WorldComp.TryGetAssignedWeapons(__instance.pawn, out AssignedWeaponContainer weapons)) { if (l == null) { l = new List <Gizmo>(); l.AddRange(__result); } //if (pawn.equipment.Primary != null) // l.Add(CreateUnequipGizmo(pawn, weapons)); foreach (ThingWithComps weapon in weapons.Weapons) { bool isTool = Settings.IsTool(weapon); bool show = false; if (pawn.Drafted) { show = true; } else // Not drafted { if (isTool || Settings.ShowWeaponsWhenNotDrafted) { show = true; } } if (show) { show = pawn.equipment.Primary != weapon; } if (show) { l.Add(CreateEquipWeaponGizmo(weapon.def, pawn, delegate { HarmonyPatchUtil.EquipWeapon(weapon, pawn, weapons); weapons.SetLastThingUsed(pawn, weapon, false); })); } } } foreach (SharedWeaponFilter f in WorldComp.SharedWeaponFilter) { if (l == null) { l = new List <Gizmo>(); l.AddRange(__result); } f.UpdateFoundDefCache(); if (f.AssignedPawns.Contains(pawn)) { if (l == null) { l = new List <Gizmo>(); if (__result != null) { l.AddRange(__result); } } foreach (ThingDef d in f.AllowedDefs) { if (d != pawn.equipment.Primary?.def && f.FoundDefCacheContains(d)) { l.Add(CreateEquipWeaponGizmo(d, pawn, delegate { if (WorldComp.TryRemoveWeapon(d, f, false, out ThingWithComps weapon)) { HarmonyPatchUtil.EquipWeapon(weapon, pawn); f.UpdateDefCache(d); } }, "WeaponStorage.EquipShared")); }