private IEnumerable <Widgets.DropdownMenuElement <WeaponPreset> > Button_GenerateMenu(Pawn pawn)
        {
            Pawn_WeaponPresetTracker pawn_WeaponPresetTracker = pawn.TryGetComp <Pawn_WeaponPresetTracker>();

            using (List <WeaponPreset> .Enumerator enumerator = Current.Game.GetComponent <WeaponPresetDatabase>().AllWeaponPresets.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    WeaponPreset weaponPreset = enumerator.Current;
                    yield return(new Widgets.DropdownMenuElement <WeaponPreset>
                    {
                        option = new FloatMenuOption(weaponPreset.label, delegate()
                        {
                            pawn_WeaponPresetTracker.CurrentWeaponPreset = weaponPreset;
                        }, MenuOptionPriority.Default, null, null, 0f, null, null),
                        payload = weaponPreset
                    });
                }
            }
            yield break;
        }
        public override void DoCell(Rect rect, Pawn pawn, PawnTable table)
        {
            Pawn_WeaponPresetTracker pawn_WeaponPresetTracker = pawn.TryGetComp <Pawn_WeaponPresetTracker>();

            if (pawn_WeaponPresetTracker == null)
            {
                return;
            }
            int   num  = Mathf.FloorToInt((rect.width - 4f) * 0.714285731f);
            int   num2 = Mathf.FloorToInt((rect.width - 4f) * 0.2857143f);
            float num3 = rect.x;


            Rect rect2 = new Rect(num3, rect.y + 2f, (float)num, rect.height - 4f);



            Rect rect3 = rect2;
            Pawn pawn2 = pawn;
            Func <Pawn, WeaponPreset> getPayload = (Pawn p) => p.TryGetComp <Pawn_WeaponPresetTracker>().CurrentWeaponPreset;
            Func <Pawn, IEnumerable <Widgets.DropdownMenuElement <WeaponPreset> > > menuGenerator = new Func <Pawn, IEnumerable <Widgets.DropdownMenuElement <WeaponPreset> > >(this.Button_GenerateMenu);
            string buttonLabel = pawn_WeaponPresetTracker.CurrentWeaponPreset.label.Truncate(rect2.width, null);
            string label       = pawn_WeaponPresetTracker.CurrentWeaponPreset.label;

            Widgets.Dropdown <Pawn, WeaponPreset>(rect3, pawn2, getPayload, menuGenerator, buttonLabel, null, label, null, null, true);
            num3 += rect2.width;
            num3 += 4f;
            Rect rect4 = new Rect(num3, rect.y + 2f, (float)num2, rect.height - 4f);

            Rect rect5 = new Rect(num3, rect.y + 2f, (float)num2, rect.height - 4f);

            if (Widgets.ButtonText(rect5, "AssignTabEdit".Translate(), true, false, true)) //Edit...
            {
                Find.WindowStack.Add(new Dialog_ManageWeapons(pawn_WeaponPresetTracker.CurrentWeaponPreset));
            }
            num3 += (float)num2;
        }
        protected override Job TryGiveJob(Pawn pawn)
        {
            Pawn_WeaponPresetTracker pawn_WeaponPresetTracker = pawn.TryGetComp <Pawn_WeaponPresetTracker>();

            if (pawn_WeaponPresetTracker == null)
            {
                return(null);
            }
            if (pawn.Faction != Faction.OfPlayer)
            {
                return(null);
            }
            if (!DebugViewSettings.debugApparelOptimize)
            {
                if (Find.TickManager.TicksGame < pawn_WeaponPresetTracker.nextWeaponPresetOptimizeTick)
                {
                    return(null);
                }
            }

            WeaponPreset   CurweaponPreset  = pawn_WeaponPresetTracker.CurrentWeaponPreset;
            ThingFilter    _equipmentFilter = CurweaponPreset.filter;
            ThingWithComps thingWithComps   = pawn.equipment?.Primary;

            bool HasWeaponEquipped = true;  //true = Has got  a weapon equipped
            bool HasNonePreset     = false; //false = Not using "NONE" PRESET
            bool WeaponNotAllowed  = false; //false = The weapon is not allowed by the preset

            if (thingWithComps is null)
            {
                HasWeaponEquipped = false;
            }
            if (CurweaponPreset.filter.AllowedDefCount == 0)
            {
                HasNonePreset = true;
            }
            if (thingWithComps != null && !_equipmentFilter.Allows(thingWithComps.def))
            {
                WeaponNotAllowed = true;
            }

            Log.Message(pawn + " HasWeaponEquipped = " + HasWeaponEquipped.ToString(), true);
            Log.Message(pawn + " HasNonePreset = " + HasNonePreset.ToString(), true);
            Log.Message(pawn + " WeaponNotAllowed = " + WeaponNotAllowed.ToString(), true);


            Log.Message(pawn + " Has this equipped " + thingWithComps, true);



            if ((!HasWeaponEquipped && !HasNonePreset) || (!HasNonePreset && WeaponNotAllowed))

            {
                Log.Warning(pawn + " Will Optimize weapons", true);
                Thing        newWeapon = null;
                List <Thing> list      = pawn.Map.listerThings.ThingsInGroup(ThingRequestGroup.Weapon);

                if (list.Count == 0)
                {
                    this.SetNextOptimizeTick(pawn);
                    return(null);
                }

                // Top 3 Guns chosen by the pawn first
                bool found = false;
                for (int j = 0; j < 3; j++) //it's 3 right now
                {
                    if (!(CurweaponPreset.SearchPriorityDefnames[j].NullOrEmpty()))
                    {
                        for (int i = 0; i < list.Count; i++)
                        {
                            Thing searchedWeapon = list[i];

                            if (CurweaponPreset.SearchPriorityDefnames[j] == searchedWeapon.def.defName && searchedWeapon.IsInAnyStorage() && !searchedWeapon.IsForbidden(pawn) && !searchedWeapon.IsBurning() && pawn.CanReserveAndReach(searchedWeapon, PathEndMode.OnCell, pawn.NormalMaxDanger(), 1, -1, null, false))
                            {
                                newWeapon = searchedWeapon;
                                found     = true;
                                break;
                            }
                        }
                    }
                    if (found)
                    {
                        break;
                    }
                }
                Log.Warning(pawn + " found weapon = " + found.ToString(), true);
                //else just keep browsing the other filter stuff
                if (!found)
                {
                    Log.Message(pawn + " Looking in the rest of the non priorityized weapons ", true);

                    for (int j = 0; j < list.Count; j++)
                    {
                        Thing searchedWeapon = list[j];

                        if (_equipmentFilter.Allows(searchedWeapon.def) && searchedWeapon.IsInAnyStorage() && !searchedWeapon.IsForbidden(pawn) && !searchedWeapon.IsBurning() && pawn.CanReserveAndReach(searchedWeapon, PathEndMode.OnCell, pawn.NormalMaxDanger(), 1, -1, null, false))
                        {
                            newWeapon = searchedWeapon;
                            found     = true;
                            break;
                        }
                    }
                }

                if (newWeapon == null)
                {
                    this.SetNextOptimizeTick(pawn);
                    return(null);
                }


                return(new Job(JobDefOf.Equip, newWeapon));
            }


            else
            {
                Log.Warning("Not doing anything, no need to optimize", true);
                this.SetNextOptimizeTick(pawn);
                return(null);
            }
        }
        private int GetValueToCompare(Pawn pawn)
        {
            Pawn_WeaponPresetTracker pawn_WeaponPresetTracker = pawn.TryGetComp <Pawn_WeaponPresetTracker>();

            return((pawn_WeaponPresetTracker != null && pawn_WeaponPresetTracker.CurrentWeaponPreset != null) ? pawn_WeaponPresetTracker.CurrentWeaponPreset.uniqueId : int.MinValue);
        }