feat: villagers contract: population in heads with idle and reserved, offers and queued orders carry the villagers they need, the villagers blocked reason; the mock sends them and refuses with villagers
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
0310b664b2
commit
5b4af38229
|
|
@ -315,6 +315,17 @@ function assignWorkers(v: MockVillage) {
|
|||
return assigned
|
||||
}
|
||||
|
||||
/** Idle villagers a level needs: the workers it adds over the level below; farms need none. */
|
||||
const extraWorkers = (type: BuildingType, level: number) => (type === 'farm' || level < 1 ? 0 : CATALOG[type].workers)
|
||||
|
||||
/** What the queue holds back for the levels it will add; demolitions hold nothing. */
|
||||
const reservedWorkers = (v: MockVillage) =>
|
||||
v.orders.filter((o) => o.action !== 'demolish').reduce((sum, o) => sum + extraWorkers(o.type, o.target_level), 0)
|
||||
|
||||
/** Villagers free for a new level: not drafted, not needed by the buildings, not held for the queue. */
|
||||
const idleVillagers = (v: MockVillage) =>
|
||||
Math.max(0, villagers(v) - militia(v) - v.buildings.reduce((sum, b) => sum + requiredWorkers(b), 0) - reservedWorkers(v))
|
||||
|
||||
const staffing = (b: MockBuilding, assigned: Map<number, number>) => {
|
||||
const required = requiredWorkers(b)
|
||||
return required === 0 ? 1 : (assigned.get(b.id) ?? 0) / required
|
||||
|
|
@ -448,7 +459,8 @@ function upgradeOffer(v: MockVillage, b: MockBuilding): Offer | null {
|
|||
if (current < 1 || current >= MAX_LEVEL) return null
|
||||
const target = current + 1
|
||||
const cost = mapCost(levelCost(b.type, target), (n) => Math.round(n / instanceShare(v, b)))
|
||||
return { target_level: target, cost, seconds: levelSeconds(b.type, target), blocked: null }
|
||||
const needed = extraWorkers(b.type, target)
|
||||
return { target_level: target, cost, seconds: levelSeconds(b.type, target), villagers: needed, blocked: needed > idleVillagers(v) ? 'villagers' : null }
|
||||
}
|
||||
|
||||
function demolishOffer(v: MockVillage, b: MockBuilding) {
|
||||
|
|
@ -470,8 +482,10 @@ function buildOptions(v: MockVillage): BuildOption[] {
|
|||
? 'max_per_village'
|
||||
: !spaceFits(v, type)
|
||||
? 'no_space'
|
||||
: null
|
||||
return { type, space: entry.space, target_level: 1, cost, seconds: levelSeconds(type, 1), blocked, requires: [], instance_share: share }
|
||||
: extraWorkers(type, 1) > idleVillagers(v)
|
||||
? 'villagers'
|
||||
: null
|
||||
return { type, space: entry.space, target_level: 1, cost, seconds: levelSeconds(type, 1), villagers: extraWorkers(type, 1), blocked, requires: [], instance_share: share }
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -647,6 +661,8 @@ function view(state: MockState, v: MockVillage, now: number): VillageState {
|
|||
upkeep: pop * VILLAGER_UPKEEP + soldierUpkeep(v),
|
||||
villagers: pop,
|
||||
workers: [...assigned.values()].reduce((a, b) => a + b, 0),
|
||||
idle: idleVillagers(v),
|
||||
reserved: reservedWorkers(v),
|
||||
soldiers: Object.values(v.troops ?? {}).reduce((a, b) => a + (b ?? 0), 0),
|
||||
training: (v.recruits ?? []).reduce((sum, o) => sum + o.count - o.done, 0),
|
||||
},
|
||||
|
|
@ -712,6 +728,7 @@ function view(state: MockState, v: MockVillage, now: number): VillageState {
|
|||
target_level: o.target_level,
|
||||
space: CATALOG[o.type].space,
|
||||
cost: o.cost,
|
||||
villagers: o.action === 'demolish' ? 0 : extraWorkers(o.type, o.target_level),
|
||||
position: i + 1,
|
||||
builder: o.builder,
|
||||
starts_at: o.starts_at === null ? null : iso(o.starts_at),
|
||||
|
|
@ -1393,6 +1410,7 @@ const handlers: Record<string, Handler> = {
|
|||
const entry = CATALOG[type] ?? fail('not_found')
|
||||
if (entry.max_per_village !== null && instances(v, type) >= entry.max_per_village) fail('max_per_village')
|
||||
if (!spaceFits(v, type)) fail('no_space')
|
||||
if (extraWorkers(type, 1) > idleVillagers(v)) fail('villagers')
|
||||
const { cost } = newInstance(v, type)
|
||||
pay(v, cost)
|
||||
enqueue(state, v, { building_id: null, type, action: 'build', target_level: 1, cost, seconds: levelSeconds(type, 1) }, now)
|
||||
|
|
@ -1403,6 +1421,7 @@ const handlers: Record<string, Handler> = {
|
|||
const v = ownVillage(state, user, data.village_id, now)
|
||||
const b = ownBuilding(v, data.building_id)
|
||||
const offer = upgradeOffer(v, b) ?? fail('max_level')
|
||||
if (offer.blocked === 'villagers') fail('villagers')
|
||||
pay(v, offer.cost)
|
||||
enqueue(state, v, { building_id: b.id, type: b.type, action: 'upgrade', target_level: offer.target_level, cost: offer.cost, seconds: offer.seconds }, now)
|
||||
return view(state, v, now)
|
||||
|
|
|
|||
|
|
@ -40,8 +40,11 @@ export type Units = Partial<Record<UnitType, number>>
|
|||
export type MovementType = 'attack' | 'support' | 'return'
|
||||
export type TravelMode = 'sneaky' | 'cautious' | 'normal' | 'fast' | 'forced'
|
||||
|
||||
/** Why the server refuses an offer. The client shows `village.blocked.<reason>`; no_space means not enough free building space. */
|
||||
export type BlockedReason = 'max_per_village' | 'no_space' | 'max_level' | 'requires'
|
||||
/**
|
||||
* Why the server refuses an offer. The client shows `village.blocked.<reason>`; no_space means
|
||||
* not enough free building space, villagers not enough idle villagers for the workers it adds.
|
||||
*/
|
||||
export type BlockedReason = 'max_per_village' | 'no_space' | 'max_level' | 'requires' | 'villagers'
|
||||
|
||||
export interface AuthUser {
|
||||
id: number
|
||||
|
|
@ -180,9 +183,10 @@ export interface VillageState {
|
|||
/** Amounts stored at `resources_at`. */
|
||||
resources: Resources
|
||||
resources_at: string
|
||||
/** Net change per hour. Food is farm output minus upkeep, so it can be negative. */
|
||||
/** Net change per hour at world speed. Food is farm output minus upkeep, so it can be negative. */
|
||||
rates: Resources
|
||||
storage_cap: number
|
||||
/** In heads, the same on every world speed; `rates.food` is the food per hour. */
|
||||
population: Population
|
||||
/** Villagers drafted as militia: they eat but don't work until `until`. */
|
||||
levy: Levy
|
||||
|
|
@ -478,14 +482,22 @@ export interface ReportPage {
|
|||
unread: number
|
||||
}
|
||||
|
||||
/**
|
||||
* Counted in heads: a head eats one food per hour at world speed 1, world speed times that
|
||||
* on a faster world. So a village holds the same heads on every world speed.
|
||||
*/
|
||||
export interface Population {
|
||||
/** Food per hour the village can feed. */
|
||||
/** Heads the farms feed. */
|
||||
food_budget: number
|
||||
/** Food per hour eaten by villagers and soldiers. */
|
||||
/** Heads that eat: villagers, soldiers, support stationed here, units in training, the wounded. */
|
||||
upkeep: number
|
||||
villagers: number
|
||||
/** Villagers assigned to buildings. */
|
||||
workers: number
|
||||
/** Villagers free for a new building level: not drafted, not needed by the buildings, not held back by the queue. */
|
||||
idle: number
|
||||
/** Villagers the build queue holds back for the levels it will add. */
|
||||
reserved: number
|
||||
soldiers: number
|
||||
/** Units queued but not trained yet; they already eat instead of working. */
|
||||
training: number
|
||||
|
|
@ -525,7 +537,7 @@ export interface RecruitOption {
|
|||
cost: Cost
|
||||
/** Per unit at this building, from its level and staffing. */
|
||||
seconds: number
|
||||
/** Food per hour one unit eats. */
|
||||
/** Heads one unit takes: the food per hour it eats at world speed 1. */
|
||||
upkeep: number
|
||||
/** Most units that resources and villagers allowed when the snapshot was taken. */
|
||||
max: number
|
||||
|
|
@ -582,6 +594,8 @@ export interface Offer {
|
|||
target_level: number
|
||||
cost: Cost
|
||||
seconds: number
|
||||
/** Idle villagers the level needs as extra workers; 0 for farms, which bring their own. */
|
||||
villagers: number
|
||||
blocked: BlockedReason | null
|
||||
}
|
||||
|
||||
|
|
@ -604,6 +618,8 @@ export interface BuildOrder {
|
|||
target_level: number
|
||||
space: number
|
||||
cost: Cost
|
||||
/** Idle villagers the order holds back until it finishes; 0 for demolitions and farms. */
|
||||
villagers: number
|
||||
position: number
|
||||
/** Builder slot working on it, or null while it waits. */
|
||||
builder: number | null
|
||||
|
|
|
|||
Loading…
Reference in New Issue