Esempio n. 1
0
 static void Postfix(Pawn_EquipmentTracker __instance, ThingWithComps newEq)
 {
     if (__instance.pawn.Faction == Faction.OfPlayerSilentFail && WorldComp.HasStorages() &&
         WorldComp.CreateOrGetAssignedWeapons(__instance.pawn, out AssignedWeaponContainer aw))
     {
         aw.Add(newEq);
     }
 }
Esempio n. 2
0
            static void Postfix(ref bool __result, Bill bill, Pawn pawn, Thing billGiver, List <ThingCount> chosen)
            {
                if (bill.Map == null)
                {
                    Log.Error("Bill's map is null");
                    return;
                }

                if (__result == true || !WorldComp.HasStorages(bill.Map) || bill.Map != pawn.Map)
                {
                    return;
                }

#if DEBUG || DROP_DEBUG || BILL_DEBUG
                Log.Warning("TryFindBestBillIngredients.Postfix __result: " + __result);
#endif
                Dictionary <ThingDef, int> chosenAmounts = new Dictionary <ThingDef, int>();
                foreach (ThingCount c in chosen)
                {
                    int count;
                    if (chosenAmounts.TryGetValue(c.Thing.def, out count))
                    {
                        count += c.Count;
                    }
                    else
                    {
                        count = c.Count;
                    }
                    chosenAmounts[c.Thing.def] = count;
                }

#if DEBUG || DROP_DEBUG || BILL_DEBUG
                Log.Warning("    ChosenAmounts:");
                //foreach (KeyValuePair<ThingLookup, int> kv in chosenAmounts)
                {
                    //    Log.Warning("        " + kv.Key.Def.label + " - " + kv.Value);
                }
#endif

                LinkedList <NeededIngrediants> neededIngs = new LinkedList <NeededIngrediants>();
                foreach (IngredientCount ing in bill.recipe.ingredients)
                {
                    bool found = false;
                    foreach (KeyValuePair <ThingDef, int> kv in chosenAmounts)
                    {
                        if ((int)ing.GetBaseCount() == kv.Value)
                        {
#if DEBUG || DROP_DEBUG || BILL_DEBUG
                            Log.Warning("    Needed Ing population count is the same");
#endif
                            if (ing.filter.Allows(kv.Key))
                            {
#if DEBUG || DROP_DEBUG || BILL_DEBUG
                                Log.Warning("    Needed Ing population found: " + kv.Key.label + " count: " + kv.Value);
#endif
                                found = true;
                                break;
                            }
                        }
                    }
                    if (!found)
                    {
#if DEBUG || DROP_DEBUG || BILL_DEBUG
                        Log.Warning("    Needed Ing population not found");
#endif
                        neededIngs.AddLast(new NeededIngrediants(ing.filter, (int)ing.GetBaseCount()));
                    }
                }

#if DEBUG || DROP_DEBUG || BILL_DEBUG
                Log.Warning("    Needed Ings:");
                foreach (NeededIngrediants ings in neededIngs)
                {
                    Log.Warning("        " + ings.Count);
                }
#endif

                List <WeaponsToUse> weaponsToUse = new List <WeaponsToUse>();
                foreach (Building_WeaponStorage storage in WorldComp.GetWeaponStorages(bill.Map))
                {
                    if ((float)(storage.Position - billGiver.Position).LengthHorizontalSquared < Math.Pow(bill.ingredientSearchRadius, 2))
                    {
                        LinkedListNode <NeededIngrediants> n = neededIngs.First;
                        while (n != null)
                        {
                            var next = n.Next;
                            NeededIngrediants neededIng = n.Value;

                            List <ThingWithComps> gotten;
                            if (storage.TryGetFilteredWeapons(bill, neededIng.Filter, out gotten))
                            {
                                foreach (ThingWithComps got in gotten)
                                {
                                    neededIng.Add(new StoredWeapons(storage, got));
                                }
                                if (neededIng.CountReached())
                                {
                                    weaponsToUse.Add(new WeaponsToUse(neededIng.GetFoundThings(), neededIng.Count));
                                    neededIng.Clear();
                                    neededIngs.Remove(n);
                                }
                            }
                            n = next;
                        }
                    }
                }

#if DEBUG || DROP_DEBUG || BILL_DEBUG
                Log.Warning("    neededIngs.count: " + neededIngs.Count);
#endif

                if (neededIngs.Count == 0)
                {
                    __result = true;
                    foreach (WeaponsToUse ttu in weaponsToUse)
                    {
                        int count = ttu.Count;
                        foreach (StoredWeapons sa in ttu.Weapons)
                        {
                            if (count <= 0)
                            {
                                break;
                            }

                            if (sa.Storage.Remove(sa.Weapon))
                            {
                                count -= sa.Weapon.stackCount;
                                chosen.Add(new ThingCount(sa.Weapon, sa.Weapon.stackCount));
                            }
                        }
                    }
                }

                weaponsToUse.Clear();
                foreach (NeededIngrediants n in neededIngs)
                {
                    n.Clear();
                }
                neededIngs.Clear();
                chosenAmounts.Clear();
            }
Esempio n. 3
0
            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"));
                                    }