diff --git a/JotunnModStub/Butler/BoneServantAI.cs b/JotunnModStub/Butler/BoneServantAI.cs index 922cb36..e5c1894 100644 --- a/JotunnModStub/Butler/BoneServantAI.cs +++ b/JotunnModStub/Butler/BoneServantAI.cs @@ -295,8 +295,14 @@ namespace RacesOfValheim // 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 // simplification the split bought. + // REFUEL BEFORE LOAD, and that order is the whole of the blast furnace fix. FreeSlots goes + // back above zero the instant a furnace eats one ore, so while there is scrap left in any + // chest PlanLoad always has an answer and PlanFuel, sitting behind it, never got a turn - + // the furnace was topped up forever and burned down to nothing. Fuel cannot starve loading + // 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() ?? PlanLoad() ?? PlanFuel(); + job = job ?? PlanCollect() ?? PlanFuel() ?? PlanLoad(); if (job != null && (m_job == null || m_job.Kind != job.Kind || m_job.Target != job.Target)) { @@ -449,7 +455,7 @@ namespace RacesOfValheim } /// - /// 3. LOAD. Urgency, then distance - and the urgency of a station with nothing wrong with it is + /// 4. LOAD. Urgency, then distance - and the urgency of a station with nothing wrong with it is /// how long it has been waiting. A metre of walking is worth four seconds of a station standing /// idle, so the far furnace wins eventually instead of never. /// @@ -495,9 +501,20 @@ namespace RacesOfValheim }; } - /// 4. REFUEL. Only stations that actually have something to work on. + /// + /// 3. REFUEL. Only stations that actually have something to work on. + /// + /// Scored by the walk, the same way PlanLoad is, and for a reason that only appeared once this + /// moved up the list: taking the first station in scan order was harmless while refuelling was + /// the job of last resort, but ahead of loading it lets one furnace at the edge of the circle + /// hold the servant while a cold one two metres away waits. + /// private Job PlanFuel() { + Container bestChest = null; + string bestFuel = null; + float bestScore = float.MaxValue; + foreach (ButlerStation station in m_stations) { if (!station.Alive || IsPenalised(station.Id)) continue; @@ -512,16 +529,24 @@ namespace RacesOfValheim Container chest = FindChestWith(fuel); if (chest == null) continue; - return new Job - { - Kind = "fuel", - Target = ZdoOf(chest), - Point = chest.transform.position, - Label = "Fetching " + fuel, - }; + float score = Vector3.Distance(transform.position, chest.transform.position) + + Vector3.Distance(chest.transform.position, station.Position); + if (score >= bestScore) continue; + + bestScore = score; + bestChest = chest; + bestFuel = fuel; } - return null; + if (bestChest == null) return null; + + return new Job + { + Kind = "fuel", + Target = ZdoOf(bestChest), + Point = bestChest.transform.position, + Label = "Fetching " + bestFuel, + }; } /// Where the load in hand is going: on to its station, or back into a chest. @@ -563,8 +588,31 @@ namespace RacesOfValheim }; } - // Every sealed chest is full. Beside the table it goes: a stalled chain is worse than a - // tidy floor, and nothing may vanish (sec. 7.3). + // Every sealed chest is full - but a chest is not the only place a thing can go. A station + // that BURNS what is in hand is a destination too, and for the smith it is usually the + // right one: the charcoal kiln releases its whole batch in one go (verified in the IL - + // UpdateSmelter only calls SpawnProcessed once the ore queue empties), which is exactly the + // delivery a nearly full chest cannot absorb, and the coal it makes is what the furnace + // beside it is waiting for. Offering the load to a consumer before giving up is what turns + // "nowhere to put 20x Coal" into a full blast furnace. + // + // It comes AFTER the chest and not before, so a product still goes home to the chest that + // paid for it whenever there is room; this is the step before the floor, not instead of + // storing. + ButlerStation consumer = StationWanting(); + if (consumer != null) + { + return new Job + { + Kind = "deposit_station", + Target = consumer.Id, + Point = consumer.Position, + Label = "Loading " + Describe(), + }; + } + + // Nothing will take it. Beside the table it goes: a stalled chain is worse than a tidy + // floor, and nothing may vanish (sec. 7.3). return new Job { Kind = "drop", @@ -574,6 +622,29 @@ namespace RacesOfValheim }; } + /// The nearest station that will take something out of the payload, as material or as + /// fuel. No FreeSlots check, for the reason spelled out above the KeyTarget branch: fuel does + /// not go into a slot, and AcceptsAnything already asks the right question of both doors. + private ButlerStation StationWanting() + { + ButlerStation best = null; + float bestDistance = float.MaxValue; + + foreach (ButlerStation station in m_stations) + { + if (!station.Alive || IsPenalised(station.Id)) continue; + if (!station.Operable || !AcceptsAnything(station)) continue; + + float distance = Vector3.Distance(transform.position, station.Position); + if (distance >= bestDistance) continue; + + bestDistance = distance; + best = station; + } + + return best; + } + // ================================================================ execution private void Execute(float dt) @@ -691,23 +762,35 @@ namespace RacesOfValheim /// At a station with something finished. Pull first, sweep second - and the sweep is the only /// place the servant ever picks anything up off the ground, which is what keeps it from tidying /// away things the player left lying about on purpose. + /// + /// THE ORDER IS THE POINT, and it used to be the other way round. Sweeping first meant every + /// visit collected what the LAST visit had thrown on the floor and then threw a fresh one down; + /// since Plan() hands the servant to PlanDelivery the moment it is carrying anything, it walked + /// off to the chest the instant the sweep landed and abandoned the item it had just pulled. + /// While a hearth kept working there was therefore always exactly one cooked meat lying on the + /// ground - the "loses one per batch, whether that is two or five" the testers reported. + /// + /// The loop is the other half of it. RPC_RemoveDoneItem empties ONE slot per call, so a hearth + /// with five done slots needs five calls; one per visit turned one batch into five round trips + /// to the chest. It runs against OutputPulls rather than against HasOutput alone because the + /// fermenter's Ready survives its own tap for the length of the animation. /// private bool DoCollect() { ButlerStation station = StationById(m_job.Target); if (station == null || !station.Alive) return false; - int before = PayloadCount(); - SweepDrops(station.Position); - bool swept = PayloadCount() > before; - bool pulled = false; - if (station.HasOutput) + for (int i = 0; i < station.OutputPulls && station.HasOutput; i++) { station.PullOutput(transform.position); pulled = true; } + int before = PayloadCount(); + SweepDrops(station.Position); + bool swept = PayloadCount() > before; + if (swept) { // The product belongs to whoever fed the station. If nothing was written there - the diff --git a/JotunnModStub/Butler/ButlerStation.cs b/JotunnModStub/Butler/ButlerStation.cs index 2b47de5..be76226 100644 --- a/JotunnModStub/Butler/ButlerStation.cs +++ b/JotunnModStub/Butler/ButlerStation.cs @@ -155,6 +155,17 @@ namespace RacesOfValheim /// pickup routine will find them. public abstract void PullOutput(Vector3 at); + /// + /// How many times PullOutput is worth calling in a single visit, which is not the same question + /// as how much is finished. + /// + /// One, everywhere except the hearth. The Fermenter is the reason this is not simply "keep + /// pulling while HasOutput": tapping does not clear Ready straight away - DelayedTap waits + /// m_tapDelay for the animation - so a loop on HasOutput would pull the same tap over and over + /// in one frame. + /// + public virtual int OutputPulls { get { return 1; } } + /// Seconds until something in here is ruined. float.MaxValue for the two families that /// cannot spoil, which is what keeps the urgency comparison honest without special cases. public virtual float SecondsToSpoil { get { return float.MaxValue; } } @@ -394,9 +405,14 @@ namespace RacesOfValheim public override bool HasOutput { get { return Vanilla.Call(Vanilla.CookHaveDone, m_cooking, false); } } /// - /// ONE per done slot, and the 1 is not a stylistic choice. RPC_RemoveDoneItem spawns the - /// item amount times PER SLOT (verified in the IL: the inner loop runs to the - /// argument), so passing anything above 1 would not collect faster - it would print money. + /// ONE CALL, ONE SLOT - and the 1 is the count, not the slot. + /// + /// Read again off the IL after the cook was reported losing a meat per batch, because the + /// first reading of it was wrong in a way that mattered. RPC_RemoveDoneItem walks the slots, + /// finds the FIRST one that is Done, spawns its item amount times, clears that slot, + /// sends RPC_SetSlotVisual and RETURNS. It does not go on to the other slots. So the + /// argument is a multiplier - anything above 1 prints money - and a hearth with five cooked + /// slots needs five calls, which is what OutputPulls below is for. /// public override void PullOutput(Vector3 at) { @@ -404,6 +420,10 @@ namespace RacesOfValheim Nview.InvokeRPC("RPC_RemoveDoneItem", at, 1); } + /// One call per slot, so a full hearth is emptied in one visit instead of one item + /// per errand. The loop that uses this stops on HasOutput anyway; this is only its bound. + public override int OutputPulls { get { return Mathf.Max(1, SlotCount); } } + /// /// The clock this whole feature exists for. Done at m_cookTime, Burnt at 2x - so what is /// left is 2*cookTime - cookedTime, and a slot already Done is the urgent one because its