Esempio n. 1
0
        public SurvivalToolAssignment MakeNewSurvivalToolAssignment()
        {
            int uniqueId = survivalToolAssignments.Any() ? survivalToolAssignments.Max(a => a.uniqueId) + 1 : 1;
            SurvivalToolAssignment toolAssignment = new SurvivalToolAssignment(uniqueId, $"{"SurvivalToolAssignment".Translate()} {uniqueId}");

            toolAssignment.filter.SetAllow(ST_ThingCategoryDefOf.SurvivalTools, true);
            survivalToolAssignments.Add(toolAssignment);
            return(toolAssignment);
        }
Esempio n. 2
0
 public Dialog_ManageSurvivalToolAssignments(SurvivalToolAssignment selectedToolAssignment)
 {
     forcePause              = true;
     doCloseX                = true;
     doCloseButton           = true;
     closeOnClickedOutside   = true;
     absorbInputAroundWindow = true;
     if (survivalToolGlobalFilter == null)
     {
         survivalToolGlobalFilter = new ThingFilter();
         survivalToolGlobalFilter.SetAllow(ST_ThingCategoryDefOf.SurvivalTools, true);
     }
     SelectedSurvivalToolAssignment = selectedToolAssignment;
 }
Esempio n. 3
0
        private void GenerateStartingSurvivalToolAssignments()
        {
            SurvivalToolAssignment staAnything = MakeNewSurvivalToolAssignment();

            staAnything.label = "OutfitAnything".Translate();

            SurvivalToolAssignment staConstructor = MakeNewSurvivalToolAssignment();

            staConstructor.label = "SurvivalToolAssignmentConstructor".Translate();
            staConstructor.filter.SetDisallowAll(null, null);

            SurvivalToolAssignment staMiner = MakeNewSurvivalToolAssignment();

            staMiner.label = "SurvivalToolAssignmentMiner".Translate();
            staMiner.filter.SetDisallowAll(null, null);

            SurvivalToolAssignment staPlantWorker = MakeNewSurvivalToolAssignment();

            staPlantWorker.label = "SurvivalToolAssignmentPlantWorker".Translate();
            staPlantWorker.filter.SetDisallowAll(null, null);

            foreach (ThingDef tDef in DefDatabase <ThingDef> .AllDefs)
            {
                SurvivalToolProperties toolProps = tDef.GetModExtension <SurvivalToolProperties>();
                if (toolProps == null)
                {
                    continue;
                }
                else
                {
                    if (toolProps.defaultSurvivalToolAssignmentTags.Contains("Constructor"))
                    {
                        staConstructor.filter.SetAllow(tDef, true);
                    }
                    if (toolProps.defaultSurvivalToolAssignmentTags.Contains("Miner"))
                    {
                        staMiner.filter.SetAllow(tDef, true);
                    }
                    if (toolProps.defaultSurvivalToolAssignmentTags.Contains("PlantWorker"))
                    {
                        staPlantWorker.filter.SetAllow(tDef, true);
                    }
                }
            }

            SurvivalToolAssignment staNothing = MakeNewSurvivalToolAssignment();

            staNothing.label = "FoodRestrictionNothing".Translate();
            staNothing.filter.SetDisallowAll(null, null);
        }
Esempio n. 4
0
 private IEnumerable <Widgets.DropdownMenuElement <SurvivalToolAssignment> > Button_GenerateMenu(Pawn pawn)
 {
     using (List <SurvivalToolAssignment> .Enumerator enumerator = Current.Game.GetComponent <SurvivalToolAssignmentDatabase>().AllSurvivalToolAssignments.GetEnumerator())
     {
         while (enumerator.MoveNext())
         {
             SurvivalToolAssignment survivalToolAssignment = enumerator.Current;
             yield return(new Widgets.DropdownMenuElement <SurvivalToolAssignment>
             {
                 option = new FloatMenuOption(survivalToolAssignment.label,
                                              () => pawn.TryGetComp <Pawn_SurvivalToolAssignmentTracker>().CurrentSurvivalToolAssignment = survivalToolAssignment),
                 payload = survivalToolAssignment
             });
         }
     }
 }
Esempio n. 5
0
 public AcceptanceReport TryDelete(SurvivalToolAssignment toolAssignment)
 {
     foreach (Pawn pawn in PawnsFinder.AllMapsCaravansAndTravelingTransportPods_Alive)
     {
         if (pawn.TryGetComp <Pawn_SurvivalToolAssignmentTracker>()?.CurrentSurvivalToolAssignment == toolAssignment)
         {
             return(new AcceptanceReport("SurvivalToolAssignmentInUse".Translate(pawn)));
         }
     }
     foreach (Pawn pawn2 in PawnsFinder.AllMapsWorldAndTemporary_AliveOrDead)
     {
         if (pawn2.TryGetComp <Pawn_SurvivalToolAssignmentTracker>() is Pawn_SurvivalToolAssignmentTracker toolAssignmentTracker &&
             toolAssignmentTracker.CurrentSurvivalToolAssignment == toolAssignment)
         {
             toolAssignmentTracker.CurrentSurvivalToolAssignment = null;
         }
     }
     survivalToolAssignments.Remove(toolAssignment);
     return(AcceptanceReport.WasAccepted);
 }
        // This is a janky mess and a half, but works!
        protected override Job TryGiveJob(Pawn pawn)
        {
            if (SurvivalToolsSettings.toolOptimization)
            {
                Pawn_SurvivalToolAssignmentTracker assignmentTracker = pawn.TryGetComp <Pawn_SurvivalToolAssignmentTracker>();

                // Pawn can't use tools, lacks a tool assignment tracker or it isn't yet time to re-optimise tools
                if (!pawn.CanUseSurvivalTools() || assignmentTracker == null || Find.TickManager.TicksGame < assignmentTracker.nextSurvivalToolOptimizeTick)
                {
                    return(null);
                }

                // Check if current tool assignment allows for each tool, auto-removing those that aren't allowed.
                SurvivalToolAssignment curAssignment = assignmentTracker.CurrentSurvivalToolAssignment;
                List <Thing>           heldTools     = pawn.GetHeldSurvivalTools().ToList();
                foreach (Thing tool in heldTools)
                {
                    if ((!curAssignment.filter.Allows(tool) || !pawn.NeedsSurvivalTool((SurvivalTool)tool)) && assignmentTracker.forcedHandler.AllowedToAutomaticallyDrop(tool))
                    {
                        return(pawn.DequipAndTryStoreSurvivalTool(tool));
                    }
                }

                // Look for better alternative tools to what the colonist currently has, based on what stats are relevant to the work types the colonist is assigned to
                List <Thing>   heldUsableTools   = heldTools.Where(t => heldTools.IndexOf(t).IsUnderSurvivalToolCarryLimitFor(pawn)).ToList();
                List <Thing>   mapTools          = pawn.MapHeld.listerThings.AllThings.Where(t => t is SurvivalTool).ToList();
                List <StatDef> workRelevantStats = pawn.AssignedToolRelevantWorkGiversStatDefs();
                Thing          curTool           = null;
                Thing          newTool           = null;
                float          optimality        = 0f;
                foreach (StatDef stat in workRelevantStats)
                {
                    curTool    = pawn.GetBestSurvivalTool(stat);
                    optimality = SurvivalToolScore(curTool, workRelevantStats);
                    foreach (Thing potentialToolThing in mapTools)
                    {
                        SurvivalTool potentialTool = (SurvivalTool)potentialToolThing;
                        if (StatUtility.StatListContains(potentialTool.WorkStatFactors.ToList(), stat) && curAssignment.filter.Allows(potentialTool) && potentialTool.BetterThanWorkingToollessFor(stat) &&
                            pawn.CanUseSurvivalTool(potentialTool.def) && potentialTool.IsInAnyStorage() && !potentialTool.IsForbidden(pawn) && !potentialTool.IsBurning())
                        {
                            float potentialOptimality = SurvivalToolScore(potentialTool, workRelevantStats);
                            float delta = potentialOptimality - optimality;
                            if (delta > 0f && pawn.CanReserveAndReach(potentialTool, PathEndMode.OnCell, pawn.NormalMaxDanger()))
                            {
                                newTool    = potentialTool;
                                optimality = potentialOptimality;
                            }
                        }
                    }
                    if (newTool != null)
                    {
                        break;
                    }
                }

                // Return a job based on whether or not a better tool was located

                // Failure
                if (newTool == null)
                {
                    SetNextOptimizeTick(pawn);
                    return(null);
                }

                // Success
                int heldToolOffset = 0;
                if (curTool != null && assignmentTracker.forcedHandler.AllowedToAutomaticallyDrop(curTool))
                {
                    pawn.jobs.jobQueue.EnqueueFirst(pawn.DequipAndTryStoreSurvivalTool(curTool, false));
                    heldToolOffset = -1;
                }
                if (pawn.CanCarryAnyMoreSurvivalTools(heldToolOffset))
                {
                    Job pickupJob = new Job(JobDefOf.TakeInventory, newTool)
                    {
                        count = 1
                    };
                    return(pickupJob);
                }

                // Final failure state
                SetNextOptimizeTick(pawn);
            }

            return(null);
        }
Esempio n. 7
0
        // This is a janky mess and a half, but works!
        protected override Job TryGiveJob(Pawn pawn)
        {
            Pawn_SurvivalToolAssignmentTracker assignmentTracker = pawn.TryGetComp <Pawn_SurvivalToolAssignmentTracker>();

            // Pawn can't use tools, lacks a tool assignment tracker or it isn't yet time to re-optimise tools
            if (!pawn.CanUseSurvivalTools() || assignmentTracker == null || Find.TickManager.TicksGame < assignmentTracker.nextSurvivalToolOptimizeTick)
            {
                return(null);
            }

            if (SurvivalToolsSettings.toolAutoDropExcess)
            {
                assignmentTracker.CheckToolsInUse();

                // Check if current tool assignment allows for each tool, auto-removing those that aren't allowed.
                SurvivalToolAssignment curAssignment = assignmentTracker.CurrentSurvivalToolAssignment;
                List <SurvivalTool>    heldTools     = pawn.GetHeldSurvivalTools();
                foreach (SurvivalTool tool in heldTools)
                {
                    if ((!curAssignment.filter.Allows(tool) || !pawn.NeedsSurvivalTool(tool) || !tool.InUse) && !tool.Forced &&
                        StoreUtility.TryFindBestBetterStoreCellFor(tool, pawn, pawn.Map, StoreUtility.CurrentStoragePriorityOf(tool), pawn.Faction, out IntVec3 c))
                    {
                        return(pawn.DequipAndTryStoreSurvivalTool(tool, true, c));
                    }
                }
            }
            if (SurvivalToolsSettings.toolOptimization)
            {
                SurvivalToolAssignment curAssignment     = assignmentTracker.CurrentSurvivalToolAssignment;
                List <StatDef>         workRelevantStats = pawn.AssignedToolRelevantWorkGiversStatDefs();
                List <Thing>           mapTools          = pawn.MapHeld.listerThings.AllThings.Where(t => t is SurvivalTool).ToList();

                SurvivalTool curTool    = null;
                SurvivalTool newTool    = null;
                float        optimality = 0f;
                foreach (StatDef stat in workRelevantStats)
                {
                    curTool    = pawn.GetBestSurvivalTool(stat);
                    optimality = SurvivalToolScore(curTool, workRelevantStats);
                    foreach (SurvivalTool potentialTool in mapTools)
                    {
                        if (StatUtility.StatListContains(potentialTool.WorkStatFactors.ToList(), stat) && curAssignment.filter.Allows(potentialTool) && potentialTool.BetterThanWorkingToollessFor(stat) &&
                            pawn.CanUseSurvivalTool(potentialTool.def) && potentialTool.IsInAnyStorage() && !potentialTool.IsForbidden(pawn) && !potentialTool.IsBurning())
                        {
                            float potentialOptimality = SurvivalToolScore(potentialTool, workRelevantStats);
                            float delta = potentialOptimality - optimality;
                            if (delta > 0f && pawn.CanReserveAndReach(potentialTool, PathEndMode.OnCell, pawn.NormalMaxDanger()))
                            {
                                newTool    = potentialTool;
                                optimality = potentialOptimality;
                            }
                        }
                    }
                    if (newTool != null)
                    {
                        break;
                    }
                }

                // Return a job based on whether or not a better tool was located

                // Failure
                if (newTool == null)
                {
                    SetNextOptimizeTick(pawn);
                    return(null);
                }

                // Success
                int heldToolOffset = 0;
                if (curTool != null && !curTool.Forced)
                {
                    pawn.jobs.jobQueue.EnqueueFirst(pawn.DequipAndTryStoreSurvivalTool(curTool, false));
                    heldToolOffset = -1;
                }
                if (pawn.CanCarryAnyMoreSurvivalTools(heldToolOffset))
                {
                    Job pickupJob = new Job(JobDefOf.TakeInventory, newTool)
                    {
                        count = 1
                    };
                    return(pickupJob);
                }
            }

            // Final failure state
            SetNextOptimizeTick(pawn);
            return(null);
        }