diff --git a/JotunnModStub/Alfar/AlfarDroneAI.cs b/JotunnModStub/Alfar/AlfarDroneAI.cs index 4cb5f86..b319ceb 100644 --- a/JotunnModStub/Alfar/AlfarDroneAI.cs +++ b/JotunnModStub/Alfar/AlfarDroneAI.cs @@ -448,15 +448,31 @@ namespace RacesOfValheim CheckIdleKiln(); // A harvest is in progress: wait for the drops to land, then gather them and replant. + Job job; if (m_farmSpot != null) { - m_job = Time.time < m_farmReadyAt + job = Time.time < m_farmReadyAt ? new Job { Kind = "wait", Point = transform.position, Label = "Waiting" } : new Job { Kind = "farm_finish", Point = m_farmSpot.Value, Label = "Gathering" }; - return; } + // A delivery is a job like any other and gets its own clock. It used to return early here + // and inherit the load's deadline: a slow trip, or a fight on the way, left it already late, + // so it timed out on the first tick, was planned again, timed out again, every frame - the + // drone froze with wood in hand next to the kiln (10790 timeouts in one player's log). + else if (m_payload.Count > 0) job = PlanDelivery(anchor.Value); + else job = PlanWork(anchor.Value); - if (m_payload.Count > 0) { m_job = PlanDelivery(anchor.Value); return; } + if (job != null && (m_job == null || m_job.Kind != job.Kind || m_job.Target != job.Target)) + { + m_jobDeadline = Time.time + JobTimeout; + } + if (m_job != null && !ReferenceEquals(m_job, job)) Release(m_job.Claim); + if (job != null) Claim(job.Claim); + m_job = job; + } + + private Job PlanWork(Vector3 anchor) + { // The forge has no clock: a smelter holds its ore indefinitely and puts its own output on // the floor, so PlanRescue would ask a question whose answer is always no. The smith is a @@ -472,15 +488,7 @@ namespace RacesOfValheim // base: repairs, the fields, and last the tidying of the entry chest. Job job = PlanRescue(); job = job ?? PlanCollect() ?? PlanFuel() ?? PlanLoad(); - job = job ?? PlanRepair(anchor.Value) ?? PlanFarm(anchor.Value) ?? PlanSort(); - - if (job != null && (m_job == null || m_job.Kind != job.Kind || m_job.Target != job.Target)) - { - m_jobDeadline = Time.time + JobTimeout; - } - if (m_job != null && !ReferenceEquals(m_job, job)) Release(m_job.Claim); - if (job != null) Claim(job.Claim); - m_job = job; + return job ?? PlanRepair(anchor) ?? PlanFarm(anchor) ?? PlanSort(); } /// @@ -609,7 +617,7 @@ namespace RacesOfValheim Kind = "collect", Target = best.Id, Claim = best.Id, - Point = best.Position, + Point = ApproachPoint(best), Hurry = true, Label = "Saving the food", }; @@ -640,7 +648,7 @@ namespace RacesOfValheim Kind = "collect", Target = best.Id, Claim = best.Id, - Point = best.Position, + Point = ApproachPoint(best), Label = "Collecting", }; } @@ -770,20 +778,37 @@ namespace RacesOfValheim // coal, so gating the delivery on free slots sent the servant back to the chest with // the coal it had just fetched - and then PlanFuel sent it out for the same coal again, // forever. AcceptsAnything already asks the right question of both doors. + // A station it could not reach is in the penalty box: asking it again straight away + // is the same failed trip. + bool unreachable = IsPenalised(target); ButlerStation station = StationById(target); - if (station != null && station.Alive && station.Operable && AcceptsAnything(station)) + if (station != null && station.Alive && station.Operable && AcceptsAnything(station) && !unreachable) { return new Job { Kind = "deposit_station", Target = target, - Point = station.Position, + Point = ApproachPoint(station), Label = "Loading " + Describe(), }; } // The station is gone, full or cold. The goods are ours to put back, not to hold. m_nview.GetZDO().Set(KeyTarget, ZDOID.None); + + // Out of reach, the load goes back where it came from: the Eldividarkista's wood stays + // fuel instead of landing in the entry chest and being sorted away into storage. + Container source = unreachable ? ContainerById(m_nview.GetZDO().GetZDOID(KeySource)) : null; + if (source != null && !IsMarkedOff(source) && !IsPenalised(ZdoOf(source)) && HasRoomFor(source)) + { + return new Job + { + Kind = "deposit", + Target = ZdoOf(source), + Point = source.transform.position, + Label = "Storing " + Describe(), + }; + } } Container chest = ChestForProduct(); @@ -816,7 +841,7 @@ namespace RacesOfValheim { Kind = "deposit_station", Target = consumer.Id, - Point = consumer.Position, + Point = ApproachPoint(consumer), Label = "Loading " + Describe(), }; } @@ -1664,10 +1689,22 @@ namespace RacesOfValheim lines.Add($"[RoV drone] {distance:0}m: '{drone.Status}' job {(drone.m_job != null ? drone.m_job.Kind : "none")} " + $"carry {drone.PayloadCount()} fight {drone.m_fighting} vanilla target {targetText} " + $"alerted {drone.m_ai.IsAlerted()} flying {drone.m_character.IsFlying()} owner {drone.m_nview.IsOwner()}"); + + // The other half of what holds a fight: the 12 m sweep, and how long the linger has left. + Character near = BaseAI.FindClosestEnemy(drone.m_character, drone.transform.position, FightRange); + string nearText = near != null + ? $"{Utils.GetPrefabName(near.gameObject)} {Vector3.Distance(near.transform.position, drone.transform.position):0}m" + : "none"; + lines.Add($"[RoV drone] fight left {Mathf.Max(0f, drone.m_fightUntil - Time.time):0.0}s, " + + $"closest enemy {nearText}, in reach {(target != null && drone.InFightReach(target))}"); } if (nearest == null) lines.Add("[RoV drone] no worker drone within 60m"); - else lines.AddRange(nearest.SortReport()); + else + { + lines.AddRange(nearest.SortReport()); + lines.AddRange(nearest.StationReport()); + } foreach (string line in lines) { @@ -1676,6 +1713,59 @@ namespace RacesOfValheim } } + /// Why a station is (or is not) fed: what the last scan saw, the wood chest, and every + /// smelter, hearth or fermenter within the radius - and the ones just past it, by how much. + private IEnumerable StationReport() + { + Vector3? anchor = Anchor; + if (anchor == null) { yield return "[RoV station] no anchor"; yield break; } + + float radius = AlfarNexusConfig.NexusRadius.Value + m_radiusBonus; + yield return $"[RoV station] anchor {Vector3.Distance(transform.position, anchor.Value):0}m from drone, " + + $"radius {radius:0}, scanned {Time.time - m_scannedAt:0}s ago, {m_stations.Count} stations, {m_chests.Count} chests"; + + if (m_wood == null) yield return "[RoV station] no Eldividarkista in range (or it is open, or holds the sigil)"; + else + { + var wood = new List(); + Inventory inventory = m_wood.GetInventory(); + if (inventory != null) + { + foreach (ItemDrop.ItemData item in inventory.GetAllItems()) + { + wood.Add(item.m_dropPrefab != null ? $"{item.m_dropPrefab.name}x{item.m_stack}" : $"{item.m_shared.m_name}(no prefab)"); + } + } + yield return $"[RoV station] Eldividarkista {Vector3.Distance(m_wood.transform.position, anchor.Value):0}m from anchor, " + + $"open {m_wood.IsInUse()}, penalty {IsPenalised(ZdoOf(m_wood))}, holds [{string.Join(", ", wood.ToArray())}]"; + } + + foreach (ButlerStation station in m_stations) + { + if (!station.Alive) { yield return "[RoV station] (a station that is gone)"; continue; } + + Container chest; + string material = FindMaterialFor(station, out chest); + string from = material == null ? "nothing" : $"{material} from {(chest == m_wood ? "Eldividarkista" : "entry")}"; + yield return $"[RoV station] {Utils.GetPrefabName(station.Behaviour.gameObject)} " + + $"{Vector3.Distance(station.Position, anchor.Value):0}m: operable {station.Operable} free {station.FreeSlots} " + + $"work {station.HasWork} wood {station.Accepts("Wood")} penalty {IsPenalised(station.Id)} " + + $"claimed {ClaimedByOther(station.Id)} fuel {station.FuelPrefab ?? "-"}/{station.FuelWanted} | load: {from}"; + } + + // A station the scan cannot see because it stands past the edge. + List pieces = AllPiecesField != null ? AllPiecesField.GetValue(null) as List : null; + if (pieces == null) yield break; + foreach (Piece piece in pieces) + { + if (piece == null) continue; + float distance = Vector3.Distance(piece.transform.position, anchor.Value); + if (distance <= radius || distance > radius + 15f) continue; + if (ButlerStation.From(piece.gameObject) == null) continue; + yield return $"[RoV station] {Utils.GetPrefabName(piece.gameObject)} {distance:0}m: OUTSIDE the radius by {distance - radius:0.0}m"; + } + } + private IEnumerable SortReport() { if (m_entry == null) { yield return "[RoV sort] no entry chest in range"; yield break; } @@ -1708,6 +1798,38 @@ namespace RacesOfValheim } } + /// + /// Where the drone flies to serve a station: beside it, a metre past its colliders on the side + /// the drone comes from. Vanilla lifts a flyer's goal to m_flyAltitudeMin over the ground under + /// it (BaseAI.MoveTo, IL), and under a charcoal kiln's origin that is inside the dome: the drone + /// pressed against the stone, never came within ArriveDistance, and the kiln went into the + /// penalty box (snakey's log, "Could not reach collect target"). Beside it is open air, and it + /// stays under a roof indoors, where aiming over the top would not. The loading and the sweep + /// still work from the station's own position. + /// + private Vector3 ApproachPoint(ButlerStation station) + { + Vector3 centre = station.Position; + bool any = false; + Bounds bounds = new Bounds(centre, Vector3.zero); + foreach (Collider collider in station.Behaviour.GetComponentsInChildren()) + { + if (collider == null || !collider.enabled || collider.isTrigger) continue; + if (any) bounds.Encapsulate(collider.bounds); + else { bounds = collider.bounds; any = true; } + } + if (!any) return centre; + + Vector3 away = transform.position - bounds.center; + away.y = 0f; + if (away.sqrMagnitude < 0.01f) away = station.Transform.forward; + float reach = Mathf.Max(bounds.extents.x, bounds.extents.z) + 1f; + + Vector3 point = bounds.center + away.normalized * reach; + point.y = centre.y; + return point; + } + private bool MoveToPoint(float dt, Vector3 point, float distance, bool run) { if (m_ai == null || MoveToMethod == null) return true; // cannot walk: act in place diff --git a/JotunnModStub/Butler/BoneServantAI.cs b/JotunnModStub/Butler/BoneServantAI.cs index 073d718..2c165db 100644 --- a/JotunnModStub/Butler/BoneServantAI.cs +++ b/JotunnModStub/Butler/BoneServantAI.cs @@ -363,8 +363,22 @@ namespace RacesOfValheim Scan(anchor.Value); ExpirePenalties(); - if (m_payload.Count > 0) { m_job = PlanDelivery(anchor.Value); return; } + // A delivery gets its own clock like any other job. Returning early here left it with the + // load's deadline: a slow trip, or a fight on the way, made it late before it started, and + // it timed out and was planned again on every tick, frozen with the goods in hand. + Job job; + if (m_payload.Count > 0) job = PlanDelivery(anchor.Value); + else job = PlanWork(); + if (job != null && (m_job == null || m_job.Kind != job.Kind || m_job.Target != job.Target)) + { + m_jobDeadline = Time.time + JobTimeout; + } + m_job = job; + } + + private Job PlanWork() + { // The forge has no clock: a smelter holds its ore indefinitely and puts its own output on // the floor, so PlanRescue would ask a question whose answer is always no. The smith is a // four-state machine and the cook is a five-state one, and that is the whole of the @@ -376,13 +390,7 @@ namespace RacesOfValheim // in return, because FuelWanted is zero for a station with no work: an empty furnace asks // for no coal, gets filled, and only then becomes worth stoking. Job job = m_family == ButlerFamily.Kitchen ? PlanRescue() : null; - job = job ?? PlanCollect() ?? PlanFuel() ?? PlanLoad(); - - if (job != null && (m_job == null || m_job.Kind != job.Kind || m_job.Target != job.Target)) - { - m_jobDeadline = Time.time + JobTimeout; - } - m_job = job; + return job ?? PlanCollect() ?? PlanFuel() ?? PlanLoad(); } /// @@ -634,8 +642,10 @@ namespace RacesOfValheim // coal, so gating the delivery on free slots sent the servant back to the chest with // the coal it had just fetched - and then PlanFuel sent it out for the same coal again, // forever. AcceptsAnything already asks the right question of both doors. + // A station it could not reach is in the penalty box: asking it again straight away + // is the same failed trip. ButlerStation station = StationById(target); - if (station != null && station.Alive && station.Operable && AcceptsAnything(station)) + if (station != null && station.Alive && station.Operable && AcceptsAnything(station) && !IsPenalised(target)) { return new Job {