Пример #1
0
            public static void Postfix(Toil __instance)
            {
                if (__instance == null)
                {
                    return;
                }

                __instance.AddPreInitAction(delegate
                {
                    Pawn pawn = __instance.GetActor();

                    if (pawn == null || pawn.Dead || pawn.equipment == null || pawn.inventory == null || !pawn.RaceProps.Humanlike)
                    {
                        return;
                    }

                    if (pawn.Drafted)
                    {
                        GrabYourToolMod.Instance.ClearMemory(pawn);
                        return;
                    }

                    SkillDef activeSkill = pawn.CurJob?.RecipeDef?.workSkill;
                    if (__instance.activeSkill != null && __instance.activeSkill() != null)
                    {
                        activeSkill = __instance.activeSkill();
                    }

                    if (activeSkill != null)
                    {
                        ToolMemory memory = GrabYourToolMod.Instance.GetMemory(pawn);

                        if (!memory.UpdateSkill(activeSkill))
                        {
                            return;
                        }

                        // Don't do it if this job uses weapons (i.e. hunting)
                        if (activeSkill == SkillDefOf.Shooting || activeSkill == SkillDefOf.Melee)
                        {
                            memory.UpdateUsingTool(null, false);
                        }
                        // Check currently equipped item
                        else if (pawn.equipment.Primary != null && ToolMemoryTracker.HasReleventStatModifiers(pawn.equipment.Primary, activeSkill))
                        {
                            memory.UpdateUsingTool(null, true);
                        }
                        // Try and find something else in inventory
                        else
                        {
                            memory.UpdateUsingTool(pawn.equipment.Primary, ToolMemoryTracker.EquipAppropriateWeapon(pawn, activeSkill));
                        }
                    }
                    else
                    {
                        GrabYourToolMod.Instance.ClearMemory(pawn);
                    }
                });
            }
        public ToolMemory GetMemory(Pawn pawn)
        {
            CheckToolMemories();

            ToolMemory toolMemory = toolMemories.Find(tm => tm != null && tm.pawn == pawn);

            if (toolMemory == null)
            {
                toolMemory      = new ToolMemory();
                toolMemory.pawn = pawn;

                toolMemories.Add(toolMemory);
            }

            return(toolMemory);
        }
        public void ClearMemory(Pawn pawn)
        {
            CheckToolMemories();

            ToolMemory toolMemory = toolMemories.Find(tm => tm != null && tm.pawn == pawn);

            if (toolMemory != null)
            {
                Thing previouslyEquipped = toolMemory.PreviousEquipped;
                if (previouslyEquipped != null && pawn.inventory != null && pawn.inventory.GetDirectlyHeldThings() != null && pawn.inventory.GetDirectlyHeldThings().Contains(previouslyEquipped))
                {
                    TryEquipWeapon(pawn, previouslyEquipped as ThingWithComps, false);
                }

                toolMemories.Remove(toolMemory);
            }
        }