public PandaBlockFarmAreaJob(
     BlockFarmAreaJobDefinition def,
     Colony owner,
     Vector3Int min,
     Vector3Int max,
     int npcID = 0)
     : base(def, owner, min, max, npcID)
     => this.CraftingGroupID = owner.RecipeData.RecipeSettingsDefault.ID;
 public PandaBlockFarmAreaJob(
     BlockFarmAreaJobDefinition def,
     Colony owner,
     Vector3Int min,
     Vector3Int max,
     int npcID,
     RecipeSettingsGroup.GroupID npcGroupID)
     : base(def, owner, min, max, npcID)
     => this.CraftingGroupID = npcGroupID;
Esempio n. 3
0
        public void PerformGoal(ref NPCBase.NPCState state)
        {
            ThreadManager.AssertIsMainThread();
            state.JobIsDone = true;

            if (!FarmingJob.PositionSub.IsValid)
            {
                state.SetCooldown(8.0, 12.0);
            }
            else
            {
                BlockFarmAreaJobDefinition definition = (BlockFarmAreaJobDefinition)this.Definition;
                Vector3Int vector3Int = FarmingJob.BlockLocation.IsValid ? FarmingJob.BlockLocation : FarmingJob.PositionSub;
                FarmingJob.PositionSub = Vector3Int.invalidPos;
                ItemTypes.ItemType val1;
                if (!World.TryGetTypeAt(vector3Int, out val1))
                {
                    state.SetCooldown(8.0, 12.0);
                }
                else
                {
                    if (val1 == definition.PlacedBlockType)
                    {
                        RecipeMatch = Recipe.MatchRecipe(FarmingJob.Owner.RecipeData.GetAvailableRecipes(definition.NPCTypeString), FarmingJob.Owner, FarmingJob.Owner.RecipeData.GetRecipeGroup(FarmingJob.CraftingGroupID));
                        switch (RecipeMatch.MatchType)
                        {
                        case Recipe.RecipeMatchType.FoundCraftable:
                            Recipe foundRecipe = RecipeMatch.FoundRecipe;
                            if (FarmingJob.NPC.Inventory.TryRemove(foundRecipe.Requirements))
                            {
                                CraftingResults.Clear();

                                for (int index = 0; index < foundRecipe.Results.Count; ++index)
                                {
                                    CraftingResults.Add(foundRecipe.Results[index]);
                                }

                                ModLoader.Callbacks.OnNPCCraftedRecipe.Invoke(Job, foundRecipe, CraftingResults);
                                RecipeResult weightedRandom = RecipeResult.GetWeightedRandom(CraftingResults);
                                float        timeToShow     = definition.Cooldown * Pipliz.Random.NextFloat(0.9f, 1.1f);

                                if (weightedRandom.Amount > 0)
                                {
                                    state.SetIndicator(new IndicatorState(timeToShow, weightedRandom.Type));
                                }
                                else
                                {
                                    state.SetCooldown(timeToShow);
                                }

                                FarmingJob.NPC.Inventory.Add(CraftingResults);
                                ++FarmingJob.GatherCount;

                                if (FarmingJob.GatherCount < definition.MaxGathersPerRun)
                                {
                                    return;
                                }

                                FarmingJob.GatherCount = 0;
                                PutItemsInCrate(ref state);
                                return;
                            }
                            else
                            {
                                PandaJobFactory.SetActiveGoal(Job, new GetItemsFromCrateGoal(Job, FarmingJob.KeyLocation, this, foundRecipe.Requirements, this), ref state);
                                FarmingJob.NPC.Inventory.Add(foundRecipe.Requirements, RecipeMatch.FoundRecipeCount);
                            }
                            state.SetCooldown(0.4, 0.6);
                            return;

                        case Recipe.RecipeMatchType.FoundMissingRequirements:
                        case Recipe.RecipeMatchType.AllDone:
                            if (state.Inventory.IsEmpty)
                            {
                                state.JobIsDone = true;
                                float cooldown = definition.Cooldown;
                                if (RecipeMatch.MatchType == Recipe.RecipeMatchType.AllDone)
                                {
                                    state.SetIndicator(new IndicatorState(cooldown, BuiltinBlocks.Indices.erroridle));
                                }
                                else
                                {
                                    state.SetIndicator(new IndicatorState(cooldown, RecipeMatch.FoundRecipe.FindMissingType(FarmingJob.Owner.Stockpile), true, false));
                                }
                                FarmingJob.Owner.Stats.RecordNPCIdleSeconds(FarmingJob.NPCType, cooldown);
                                return;
                            }
                            PutItemsInCrate(ref state);
                            return;
                        }
                    }
                    if (val1 == BuiltinBlocks.Types.air)
                    {
                        ItemTypes.ItemType val2;
                        if (World.TryGetTypeAt(vector3Int.Add(0, -1, 0), out val2))
                        {
                            if (!definition.PlacedBlockType.RequiresFertileBelow || val2.IsFertile)
                            {
                                if (definition.RequiredBlockItem.Amount != 0 && Job.NPC.Colony.Stockpile.TryRemove(definition.RequiredBlockItem))
                                {
                                    state.SetCooldown(1.5, 2.5);
                                    PandaJobFactory.SetActiveGoal(Job, new GetItemsFromCrateGoal(Job, FarmingJob.KeyLocation, this, new[] { new StoredItem(definition.RequiredBlockItem) }, this), ref state);
                                    Job.NPC.Inventory.Add(definition.RequiredBlockItem);
                                }

                                if (definition.RequiredBlockItem.Amount == 0)
                                {
                                    ServerManager.TryChangeBlock(vector3Int, BuiltinBlocks.Types.air, definition.PlacedBlockType, Job.Owner, ESetBlockFlags.DefaultAudio);
                                    state.SetCooldown(1.5, 2.5);
                                    return;
                                }
                                state.SetIndicator(new IndicatorState(Pipliz.Random.NextFloat(8f, 14f), definition.RequiredBlockItem.Type, true, false));
                                return;
                            }
                        }
                        else
                        {
                            state.SetCooldown(8.0, 12.0);
                            return;
                        }
                    }
                    state.SetCooldown((double)Pipliz.Random.NextFloat(3f, 6f));
                }
            }
        }