feat: food warnings: a strip when the stock drains, an alert with the desertion countdown when starving

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-10-28 22:05:45 +01:00
parent 9878fb0527
commit bb3796f22b
6 changed files with 76 additions and 1 deletions

View File

@ -0,0 +1,42 @@
<script setup lang="ts">
import type { Levy, Starvation } from '~/types/game'
defineProps<{
starvation: Starvation
levy: Levy
secondsUntil: (iso: string) => number
}>()
</script>
<!-- A flat strip under the village header while the food stock drains or is empty. -->
<template>
<div
v-if="starvation.since"
class="flex flex-wrap items-center gap-x-3 gap-y-1 border-b-2 border-wood-700 bg-blood-700 px-4 py-2 text-sm text-parchment-50 sm:px-6"
role="alert"
>
<IconResource resource="food" class="size-5" />
<span class="font-semibold">{{ $t('village.food.starving') }}</span>
<span v-if="starvation.desertion_at">
{{ $t('village.food.desertion_in') }}
<span class="font-semibold tabular-nums">{{ formatDuration(secondsUntil(starvation.desertion_at)) }}</span>
</span>
<span class="w-full text-xs text-parchment-200 sm:ml-auto sm:w-auto">
{{ levy.militia ? $t('village.food.fix_levy') : $t('village.food.fix') }}
</span>
</div>
<div
v-else-if="starvation.food_empty_at"
class="flex flex-wrap items-center gap-x-3 gap-y-1 border-b-2 border-wood-700 bg-gold-400 px-4 py-2 text-sm text-ink-900 sm:px-6"
role="status"
>
<IconResource resource="food" class="size-5" />
<span>
{{ $t('village.food.runs_out_in') }}
<span class="font-semibold tabular-nums">{{ formatDuration(secondsUntil(starvation.food_empty_at)) }}</span>
</span>
<span class="w-full text-xs text-ink-700 sm:ml-auto sm:w-auto">
{{ levy.militia ? $t('village.food.fix_levy') : $t('village.food.then') }}
</span>
</div>
</template>

View File

@ -58,7 +58,7 @@ export function useVillage(villageId: number) {
if (!v) return null if (!v) return null
const times = [ const times = [
...v.queue.flatMap((o) => (o.finishes_at ? [Date.parse(o.finishes_at)] : [])), ...v.queue.flatMap((o) => (o.finishes_at ? [Date.parse(o.finishes_at)] : [])),
...(v.levy.until ? [Date.parse(v.levy.until)] : []), ...[v.levy.until, v.starvation.food_empty_at, v.starvation.desertion_at].flatMap((at) => (at ? [Date.parse(at)] : [])),
...v.military.flatMap((m) => ...v.military.flatMap((m) =>
m.queue.filter((o) => o.done < o.count).map((o) => Date.parse(o.starts_at) + (o.done + 1) * o.seconds_per_unit * 1000), m.queue.filter((o) => o.done < o.count).map((o) => Date.parse(o.starts_at) + (o.done + 1) * o.seconds_per_unit * 1000),
), ),

View File

@ -430,6 +430,17 @@ function enqueue(state: MockState, v: MockVillage, order: Omit<MockOrder, 'id' |
startWaiting(v, now) startWaiting(v, now)
} }
/** Only the warnings: the mock never makes soldiers desert. */
function starvation(v: MockVillage) {
const rate = rates(v).food
const empty = v.resources.food <= 0.001
return {
food_empty_at: rate < 0 && !empty ? iso(v.resources_at + (v.resources.food / -rate) * 3_600_000) : null,
since: rate < 0 && empty ? iso(v.resources_at) : null,
desertion_at: null,
}
}
function view(v: MockVillage, now: number): VillageState { function view(v: MockVillage, now: number): VillageState {
const assigned = assignWorkers(v) const assigned = assignWorkers(v)
const pop = villagers(v) const pop = villagers(v)
@ -455,6 +466,7 @@ function view(v: MockVillage, now: number): VillageState {
training: (v.recruits ?? []).reduce((sum, o) => sum + o.count - o.done, 0), training: (v.recruits ?? []).reduce((sum, o) => sum + o.count - o.done, 0),
}, },
levy: { militia: militia(v), until: v.levy_until ? iso(v.levy_until) : null, seconds: LEVY_SECONDS }, levy: { militia: militia(v), until: v.levy_until ? iso(v.levy_until) : null, seconds: LEVY_SECONDS },
starvation: starvation(v),
troops: Object.fromEntries(Object.entries(v.troops ?? {}).filter(([, n]) => n)), troops: Object.fromEntries(Object.entries(v.troops ?? {}).filter(([, n]) => n)),
military: military(v, now), military: military(v, now),
builders: BUILDERS, builders: BUILDERS,

View File

@ -67,6 +67,8 @@ function openRecruit(buildingId?: number) {
</div> </div>
</div> </div>
<VillageFoodWarning :starvation="village.starvation" :levy="village.levy" :seconds-until="v.secondsUntil" />
<div class="relative"> <div class="relative">
<div class="h-[clamp(260px,82vw,460px)] sm:h-[clamp(340px,62vh,760px)]"> <div class="h-[clamp(260px,82vw,460px)] sm:h-[clamp(340px,62vh,760px)]">
<VillageDiorama v-model:selected="selected" :village="village" :seconds-until="v.secondsUntil" /> <VillageDiorama v-model:selected="selected" :village="village" :seconds-until="v.secondsUntil" />

View File

@ -76,6 +76,7 @@ export interface VillageState {
population: Population population: Population
/** Villagers drafted as militia: they eat but don't work until `until`. */ /** Villagers drafted as militia: they eat but don't work until `until`. */
levy: Levy levy: Levy
starvation: Starvation
/** Own units standing in the village. */ /** Own units standing in the village. */
troops: Partial<Record<UnitType, number>> troops: Partial<Record<UnitType, number>>
/** One entry per building that trains units, in building order. */ /** One entry per building that trains units, in building order. */
@ -110,6 +111,16 @@ export interface Levy {
seconds: number seconds: number
} }
/** Upkeep above income drains the food stock; once it is empty, soldiers desert after a grace time. */
export interface Starvation {
/** When the stock runs out at the current rates; null while food isn't draining. */
food_empty_at: string | null
/** Set while the stock is empty and the village eats more than it harvests. */
since: string | null
/** The next desertion wave, while starving. */
desertion_at: string | null
}
/** A barracks, stable, workshop or academy: what it trains and its own queue. */ /** A barracks, stable, workshop or academy: what it trains and its own queue. */
export interface MilitaryBuilding { export interface MilitaryBuilding {
building_id: number building_id: number

View File

@ -64,6 +64,14 @@ export default {
recruit: 'Recruit', recruit: 'Recruit',
hint: 'Build barracks to recruit units.', hint: 'Build barracks to recruit units.',
}, },
food: {
runs_out_in: 'Your village eats more than it harvests. Food runs out in',
then: 'Once the stores are empty, soldiers start to desert after a grace period.',
starving: 'Your village is starving.',
desertion_in: 'Soldiers desert in',
fix: 'More farms or fewer soldiers stop it.',
fix_levy: 'The drafted militia leave the farms unworked: ending the levy helps.',
},
levy: { levy: {
title: 'Levy', title: 'Levy',
militia: 'Militia', militia: 'Militia',