public override void CalculateSubPosition(IAreaJob rawJob, ref Vector3Int positionSub) { if (rawJob != null) { AppleFarmerJob job = (AppleFarmerJob)rawJob; Vector3Int min = job.Minimum; Vector3Int max = job.Maximum; Logger.Log("Min/Max {0}/{1}", job.Minimum, job.Maximum); Logger.Log("Saplings available {0}", job.NPC.Colony.UsedStockpile.Contains(saplingIndex)); if (job.checkMissingBushes && job.NPC.Colony.UsedStockpile.Contains(saplingIndex)) { // remove legacy positions for (int x = min.x + 1; x <= max.x; x += 3) { for (int z = min.z; z <= max.z; z += 3) { ushort type; Vector3Int possiblePositionSub = new Vector3Int(x, min.y, z); if (!World.TryGetTypeAt(possiblePositionSub, out type)) { return; } if (type == leaveIndex) { job.removingOldBush = true; job.bushLocation = possiblePositionSub; positionSub = Server.AI.AIManager.ClosestPosition(job.bushLocation, job.NPC.Position); return; } } } // place new positions for (int x = min.x; x <= max.x; x += 3) { for (int z = min.z; z <= max.z; z += 3) { ushort type; Vector3Int possiblePositionSub = new Vector3Int(x, min.y, z); if (!World.TryGetTypeAt(possiblePositionSub, out type)) { return; } if (type == 0) { job.placingMissingBush = true; job.bushLocation = possiblePositionSub; positionSub = Server.AI.AIManager.ClosestPositionNotAt(job.bushLocation, job.NPC.Position); return; } } } job.checkMissingBushes = false; } positionSub = min; positionSub.x += Random.Next(0, (max.x - min.x) / 3 + 1) * 3; positionSub.z += Random.Next(0, (max.z - min.z) / 3 + 1) * 3; } else { Logger.Log("rawJob equals null"); } }
public override void OnNPCAtJob(IAreaJob rawJob, ref Vector3Int positionSub, ref NPCBase.NPCState state, ref bool shouldDumpInventory) { AppleFarmerJob job = (AppleFarmerJob)rawJob; state.JobIsDone = true; if (positionSub.IsValid) { ushort type; if (job.placingMissingBush) { if (job.NPC.Colony.UsedStockpile.TryRemove(saplingIndex)) { job.placingMissingBush = false; ServerManager.TryChangeBlock(job.bushLocation, saplingIndex, rawJob.Owner, ServerManager.SetBlockFlags.DefaultAudio); state.SetCooldown(2.0); } else { state.SetIndicator(new Shared.IndicatorState(Random.NextFloat(8f, 14f), saplingIndex, true, false)); } } else if (job.removingOldBush) { if (ServerManager.TryChangeBlock(job.bushLocation, 0, rawJob.Owner, ServerManager.SetBlockFlags.DefaultAudio)) { job.NPC.Colony.UsedStockpile.Add(saplingIndex); job.removingOldBush = false; } state.SetCooldown(2.0); } else if (World.TryGetTypeAt(positionSub, out type)) { if (type == 0) { job.checkMissingBushes = true; state.SetCooldown(1.0, 4.0); } else if (World.TryGetTypeAt(positionSub.Add(0, 3, 0), out type) && type == leaveIndex) { GatherResults.Clear(); GatherResults.Add(new ItemTypes.ItemTypeDrops(fruitIndex, 1, 1.0)); GatherResults.Add(new ItemTypes.ItemTypeDrops(saplingIndex, 1, 0.1)); ModLoader.TriggerCallbacks(ModLoader.EModCallbackType.OnNPCGathered, rawJob as IJob, positionSub, GatherResults); InventoryItem toShow = ItemTypes.ItemTypeDrops.GetWeightedRandom(GatherResults); if (toShow.Amount > 0) { state.SetIndicator(new Shared.IndicatorState(8.5f, toShow.Type)); } else { state.SetCooldown(8.5); } job.NPC.Inventory.Add(GatherResults); } else { state.SetIndicator(new Shared.IndicatorState(Random.NextFloat(8f, 14f), BuiltinBlocks.ErrorMissing)); } } else { state.SetCooldown(Random.NextFloat(3f, 6f)); } positionSub = Vector3Int.invalidPos; } else { state.SetCooldown(10.0); } }