Example #1
0
        public override void OnNPCAtJob(ref NPCBase.NPCState state)
        {
            state.JobIsDone = true;
            usedNPC.LookAt(position.Vector);
            if (!state.Inventory.IsEmpty)
            {
                usedNPC.Inventory.Dump(blockInventory);
            }
            if (selectedRecipe != null)
            {
                if (recipesToCraft > 0 && selectedRecipe.IsPossible(usedNPC.Colony.UsedStockpile, blockInventory))
                {
                    blockInventory.Remove(selectedRecipe.Requirements);

                    craftingResults.Clear();
                    for (int i = 0; i < selectedRecipe.Results.Count; i++)
                    {
                        craftingResults.Add(selectedRecipe.Results[i]);
                    }
                    ModLoader.TriggerCallbacks(ModLoader.EModCallbackType.OnNPCCraftedRecipe, (IJob)this, selectedRecipe, craftingResults);

                    if (craftingResults.Count > 0)
                    {
                        blockInventory.Add(craftingResults);
                        ushort typeToShow;
                        typeToShow = craftingResults[0].Type;
                        if (craftingResults.Count > 1)
                        {
                            int totalTypes = 0;
                            for (int i = 0; i < craftingResults.Count; i++)
                            {
                                totalTypes += craftingResults[i].Amount;
                            }
                            totalTypes = Random.Next(0, totalTypes + 1);
                            for (int i = 0; i < craftingResults.Count; i++)
                            {
                                totalTypes -= craftingResults[i].Amount;
                                if (totalTypes <= 0)
                                {
                                    typeToShow = craftingResults[i].Type;
                                    break;
                                }
                            }
                        }

                        state.SetIndicator(new Shared.IndicatorState(CraftingCooldown, typeToShow));
                    }
                    else
                    {
                        state.SetIndicator(new Shared.IndicatorState(CraftingCooldown, NPCIndicatorType.None));
                    }

                    state.JobIsDone = false;
                    recipesToCraft--;
                    OnRecipeCrafted();
                }
                else
                {
                    selectedRecipe = null;
                    recipesToCraft = 0;
                    blockInventory.Dump(usedNPC.Inventory);
                    if (!state.Inventory.IsEmpty)
                    {
                        shouldTakeItems = true;
                    }
                    state.SetCooldown(0.1);
                    if (wasCrafting)
                    {
                        OnStopCrafting();
                    }
                }
            }
            else
            {
                var recipeMatch = Recipe.MatchRecipe <Recipe, IList <Recipe> >(GetPossibleRecipes, usedNPC.Colony.UsedStockpile);
                switch (recipeMatch.MatchType)
                {
                case Recipe.RecipeMatchType.AllDone:
                case Recipe.RecipeMatchType.FoundMissingRequirements:
                    if (!state.Inventory.IsEmpty || !blockInventory.IsEmpty)
                    {
                        blockInventory.Dump(usedNPC.Inventory);
                        shouldTakeItems = true;
                        state.SetCooldown(0.3);
                    }
                    else
                    {
                        state.JobIsDone = false;
                        float cooldown = Random.NextFloat(8f, 16f);
                        if (recipeMatch.MatchType == Recipe.RecipeMatchType.AllDone)
                        {
                            state.SetIndicator(new Shared.IndicatorState(cooldown, BlockTypes.Builtin.BuiltinBlocks.ErrorIdle));
                        }
                        else
                        {
                            state.SetIndicator(new Shared.IndicatorState(cooldown, recipeMatch.FoundRecipe.FindMissingType(owner), true, false));
                        }
                    }
                    break;

                case Recipe.RecipeMatchType.FoundCraftable:
                    blockInventory.Dump(usedNPC.Inventory);
                    selectedRecipe  = recipeMatch.FoundRecipe;
                    shouldTakeItems = true;
                    state.SetCooldown(0.3);
                    break;

                default:
                    Assert.IsTrue(false, "Unexpected RecipeMatchType: " + recipeMatch.MatchType);
                    break;
                }
                if (wasCrafting)
                {
                    OnStopCrafting();
                }
            }
        }
        public override void OnNPCDoJob(ref NPCBase.NPCState state)
        {
            state.JobIsDone = true;
            usedNPC.LookAt(position.Vector);
            if (!state.Inventory.IsEmpty)
            {
                storedFuel += state.Inventory.TotalFuel;
                state.Inventory.ClearFuel();
                state.Inventory.Dump(blockInventory);
            }
            if (selectedRecipe != null)
            {
                if (recipesToCraft > 0 && selectedRecipe.IsPossible(usedNPC.Colony.UsedStockpile, blockInventory, storedFuel))
                {
                    storedFuel -= selectedRecipe.FuelPerCraft;
                    blockInventory.Remove(selectedRecipe.Requirements);
                    blockInventory.Add(selectedRecipe.Results);
                    recipesToCraft--;
                    OnLit();
                    state.SetIndicator(NPCIndicatorType.Crafted, TimeBetweenJobs, selectedRecipe.Results[0].Type);
                    state.JobIsDone = false;
                    return;
                }
                else
                {
                    recipesToCraft = 0;
                    selectedRecipe = null;
                    blockInventory.Dump(usedNPC.Inventory);
                    if (!state.Inventory.IsEmpty)
                    {
                        shouldTakeItems = true;
                    }
                    OverrideCooldown(0.1);
                }
            }
            else
            {
                var recipeMatch = Recipe.MatchRecipe(GetPossibleRecipes, usedNPC.Colony.UsedStockpile);
                switch (recipeMatch.MatchType)
                {
                case Recipe.RecipeMatchType.AllDone:
                case Recipe.RecipeMatchType.FoundMissingRequirements:
                    if (!state.Inventory.IsEmpty || !blockInventory.IsEmpty)
                    {
                        blockInventory.Dump(usedNPC.Inventory);
                        shouldTakeItems = true;
                    }
                    else
                    {
                        state.JobIsDone = false;
                        float cooldown = Random.NextFloat(8f, 16f);
                        if (recipeMatch.MatchType == Recipe.RecipeMatchType.AllDone)
                        {
                            state.SetIndicator(NPCIndicatorType.SuccessIdle, cooldown);
                        }
                        else
                        {
                            state.SetIndicator(NPCIndicatorType.MissingItem, cooldown, recipeMatch.FoundRecipe.FindMissingType(owner));
                        }
                        OverrideCooldown(cooldown);
                    }
                    break;

                case Recipe.RecipeMatchType.FoundCraftable:
                    blockInventory.Dump(usedNPC.Inventory);
                    selectedRecipe  = recipeMatch.FoundRecipe;
                    shouldTakeItems = true;
                    OverrideCooldown(0.5);
                    break;
                }
            }
            OnUnlit();
        }
        public override void OnNPCAtJob(ref NPCBase.NPCState state)
        {
            state.JobIsDone = true;
            usedNPC.LookAt(position.Vector);
            if (!state.Inventory.IsEmpty)
            {
                usedNPC.Inventory.Dump(blockInventory);
            }
            if (selectedRecipe != null)
            {
                if (recipesToCraft > 0 && selectedRecipe.IsPossible(usedNPC.Colony.UsedStockpile, blockInventory))
                {
                    blockInventory.Remove(selectedRecipe.Requirements);
                    blockInventory.Add(selectedRecipe.Results);
                    state.SetIndicator(new Shared.IndicatorState(CraftingCooldown, selectedRecipe.Results[0].Type));
                    state.JobIsDone = false;
                    recipesToCraft--;
                    OnRecipeCrafted();
                }
                else
                {
                    selectedRecipe = null;
                    recipesToCraft = 0;
                    blockInventory.Dump(usedNPC.Inventory);
                    if (!state.Inventory.IsEmpty)
                    {
                        shouldTakeItems = true;
                    }
                    state.SetCooldown(0.1);
                    if (wasCrafting)
                    {
                        OnStopCrafting();
                    }
                }
            }
            else
            {
                var recipeMatch = Recipe.MatchRecipe <Recipe, IList <Recipe> >(GetPossibleRecipes, usedNPC.Colony.UsedStockpile);
                switch (recipeMatch.MatchType)
                {
                case Recipe.RecipeMatchType.AllDone:
                case Recipe.RecipeMatchType.FoundMissingRequirements:
                    if (!state.Inventory.IsEmpty || !blockInventory.IsEmpty)
                    {
                        blockInventory.Dump(usedNPC.Inventory);
                        shouldTakeItems = true;
                        state.SetCooldown(0.3);
                    }
                    else
                    {
                        state.JobIsDone = false;
                        float cooldown = Random.NextFloat(8f, 16f);
                        if (recipeMatch.MatchType == Recipe.RecipeMatchType.AllDone)
                        {
                            state.SetIndicator(new Shared.IndicatorState(cooldown, BlockTypes.Builtin.BuiltinBlocks.ErrorIdle));
                        }
                        else
                        {
                            state.SetIndicator(new Shared.IndicatorState(cooldown, recipeMatch.FoundRecipe.FindMissingType(owner), true, false));
                        }
                    }
                    break;

                case Recipe.RecipeMatchType.FoundCraftable:
                    blockInventory.Dump(usedNPC.Inventory);
                    selectedRecipe  = recipeMatch.FoundRecipe;
                    shouldTakeItems = true;
                    state.SetCooldown(0.3);
                    break;

                default:
                    Assert.IsTrue(false, "Unexpected RecipeMatchType: " + recipeMatch.MatchType);
                    break;
                }
                if (wasCrafting)
                {
                    OnStopCrafting();
                }
            }
        }