fix(butler): the cook's lost meat, the furnace that starved, the coal with nowhere to go

Three tester reports against the Bone Servant, all with the same shape:
the servant was doing what the code said and the code said the wrong
thing. Two vanilla methods were re-read off the IL rather than trusted.

RPC_RemoveDoneItem empties ONE slot per call - it finds the first Done
slot, spawns its item, clears it and returns - not every slot, as the
comment on PullOutput claimed. DoCollect also swept the ground BEFORE
pulling, so each visit collected the previous visit's item and dropped a
fresh one; since Plan() diverts to PlanDelivery the moment the payload is
non-empty, the servant then left for the chest and abandoned what it had
just pulled. A working hearth therefore always had exactly one cooked
meat lying beside it. Now it pulls first, in a loop bounded by a new
ButlerStation.OutputPulls - the slot count for a cooking station, 1
everywhere else, because the fermenter's Ready survives its own tap for
the length of the animation and a bare while(HasOutput) would spam
RPC_Tap.

PlanFuel sat last, behind PlanLoad, and FreeSlots goes back above zero
the instant a furnace eats one ore - so loading always had an answer and
refuelling never got a turn. The blast furnace burned down to nothing.
PlanFuel now runs ahead of PlanLoad, which cannot starve loading in
return because FuelWanted is 0 for a station with no work, and it scores
candidates by the walk the way PlanLoad does now that the choice matters.

PlanDelivery offers the load to the nearest station that will CONSUME it,
as material or as fuel, after the chest search fails and before dropping
it on the floor. UpdateSmelter only calls SpawnProcessed once the ore
queue empties, so the charcoal kiln releases its whole batch at once -
exactly the delivery a nearly full chest cannot absorb. The coal now
walks into the furnace beside it instead of onto the ground.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Ferdinando 2026-08-29 08:45:09 +02:00
commit 03c1006511
2 changed files with 124 additions and 21 deletions

View file

@ -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
}
/// <summary>
/// 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.
/// </summary>
@ -495,9 +501,20 @@ namespace RacesOfValheim
};
}
/// <summary>4. REFUEL. Only stations that actually have something to work on.</summary>
/// <summary>
/// 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.
/// </summary>
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,
};
}
/// <summary>Where the load in hand is going: on to its station, or back into a chest.</summary>
@ -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
};
}
/// <summary>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.</summary>
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.
/// </summary>
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