feat: building copies: each instance shows the share it works at (amber chip on copy rows, the building header and the upgrade card), the build row and plot card of a new copy warn it works at 50% and costs more; the mock ranks copies by level and scales their output and cost

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-10-31 10:54:49 +01:00
parent eb799666cf
commit b746bb9b00
10 changed files with 87 additions and 29 deletions

View File

@ -170,14 +170,18 @@ const demolishable = computed(() =>
{{ row.name }}
</button>
<p v-else class="truncate font-semibold">{{ row.name }}</p>
<p class="text-xs text-ink-500">
{{
row.kind === 'upgrade'
? $t('village.buildings.level_n', { level: row.building.level })
: row.another
? $t('village.build.another_hint')
: $t('village.build.not_built')
}}
<!-- A copy works at a share of the first: one amber chip, from the server's share. -->
<p class="flex items-center gap-1.5 text-xs whitespace-nowrap text-ink-500">
<template v-if="row.kind === 'upgrade'">
{{ $t('village.buildings.level_n', { level: row.building.level }) }}
<span v-if="isCopy(row.building.instance_share)" class="badge badge-gold">
{{ $t('village.copy.share', { share: sharePercent(row.building.instance_share) }) }}
</span>
</template>
<span v-else-if="isCopy(row.option.instance_share)" class="badge badge-gold">
{{ $t('village.copy.new', { share: sharePercent(row.option.instance_share) }) }}
</span>
<template v-else>{{ row.another ? $t('village.build.another_hint') : $t('village.build.not_built') }}</template>
</p>
</div>
</div>

View File

@ -69,6 +69,9 @@ function value(e: BuildingEffect) {
<h1 class="font-display text-lg leading-tight font-bold text-wood-950">
{{ title }} <span class="font-normal">({{ $t('village.buildings.level_n', { level: building.level }) }})</span>
</h1>
<p v-if="isCopy(building.instance_share)" class="mt-0.5">
<span class="badge badge-gold">{{ $t('village.copy.this', { share: sharePercent(building.instance_share) }) }}</span>
</p>
<p class="mt-0.5 text-ink-700">{{ $t(`village.building_about.${building.type}`) }}</p>
</div>

View File

@ -158,10 +158,8 @@ const badges = computed(() => {
const tip = ref<{ id: number; x: number; y: number } | null>(null)
const tipOffer = computed(() => (tip.value ? (badges.value.find((p) => p.b.id === tip.value!.id)?.offer ?? null) : null))
/** Which building the card is about: "Hiding place 2" when there are several. */
const tipName = computed(() => {
const b = tip.value ? props.village.buildings.find((x) => x.id === tip.value!.id) : null
return b ? buildingName(props.village, b, t) : ''
})
const tipBuilding = computed(() => (tip.value ? (props.village.buildings.find((x) => x.id === tip.value!.id) ?? null) : null))
const tipName = computed(() => (tipBuilding.value ? buildingName(props.village, tipBuilding.value, t) : ''))
function showTip(id: number, e: Event) {
const r = (e.currentTarget as Element).getBoundingClientRect()
tip.value = { id, x: r.left + r.width / 2, y: r.top }
@ -369,6 +367,9 @@ const timers = computed(() => {
<UiCost :cost="tipOffer.cost" :have="resources ?? undefined" nowrap />
<span class="inline-flex items-center gap-1 text-ink-700 tabular-nums"><IconUi name="clock" class="size-3.5" />{{ formatDuration(tipOffer.seconds) }}</span>
</div>
<p v-if="tipBuilding && isCopy(tipBuilding.instance_share)" class="mt-1">
<span class="badge badge-gold">{{ $t('village.copy.this', { share: sharePercent(tipBuilding.instance_share) }) }}</span>
</p>
<p v-if="tipOffer.blocked" class="mt-1 text-xs font-semibold text-blood-700">{{ $t(`village.blocked.${tipOffer.blocked}`) }}</p>
<p v-else-if="!affordable(tipOffer)" class="mt-1 text-xs font-semibold text-blood-700">{{ $t('village.blocked.resources') }}</p>
</div>

View File

@ -47,7 +47,11 @@ const reason = computed(() => {
{{ $t('village.build.requirement', { type: $t(`buildings.${r.type}`), level: r.level }) }}
</p>
</div>
<div v-else class="space-y-1.5 rounded-sm border border-wood-600/50 bg-parchment-100 p-3">
<!-- A copy works at a share of the first and costs more: say so before the cost. -->
<p v-if="isCopy(option.instance_share)" class="border border-gold-600 bg-gold-400 px-2 py-0.5 text-xs text-ink-900">
{{ $t('village.copy.offer', { share: sharePercent(option.instance_share) }) }}
</p>
<div v-if="!option.requires.length" class="space-y-1.5 rounded-sm border border-wood-600/50 bg-parchment-100 p-3">
<div class="flex items-center justify-between gap-2 text-xs text-ink-500">
<UiCost :cost="option.cost" :have="resources" />
<span class="flex items-center gap-1 tabular-nums">

View File

@ -98,3 +98,6 @@ export const DETECTION = 2
export const CANCEL_WINDOW = 600
/** Wood, clay and iron each in every barbarian village; it never runs out. */
export const BARBARIAN_STOCK = 1000
/** Each copy of a building type works at this share of the one ranked above it, and costs its inverse as much. */
export const COPY_SHARE = 0.5

View File

@ -51,6 +51,7 @@ import {
BUILDING_TYPES,
CANCEL_WINDOW,
CATALOG,
COPY_SHARE,
DEFAULT_MODE,
DEFAULT_PRIORITY,
DEMOLISH_REFUND,
@ -242,10 +243,28 @@ const levelCost = (type: BuildingType, level: number) => mapCost(CATALOG[type].c
const levelSeconds = (type: BuildingType, level: number) => atLevel(CATALOG[type].seconds, level)
const requiredWorkers = (b: MockBuilding) => atLevel(CATALOG[b.type].workers, b.level)
/**
* The share an instance works at: the first of its type (by level, then age) fully, each copy
* COPY_SHARE of the one ranked above it. A crude stand-in for the backend's copy rule.
*/
function instanceShare(v: MockVillage, b: MockBuilding) {
const rank = v.buildings
.filter((o) => o.type === b.type)
.sort((p, q) => q.level - p.level || p.id - q.id)
.indexOf(b)
return COPY_SHARE ** Math.max(0, rank)
}
/** What another instance of a type would cost and work at; unique types have no copies. */
function newInstance(v: MockVillage, type: BuildingType) {
const share = CATALOG[type].max_per_village === 1 ? 1 : COPY_SHARE ** instances(v, type)
return { share, cost: mapCost(levelCost(type, 1), (n) => Math.round(n / share)) }
}
function foodBudget(v: MockVillage) {
return v.buildings
.filter((b) => b.type === 'farm')
.reduce((sum, b) => sum + atLevel(CATALOG.farm.production!.per_hour, b.level), 0)
.reduce((sum, b) => sum + atLevel(CATALOG.farm.production!.per_hour, b.level) * instanceShare(v, b), 0)
}
/** Known unit types with a positive count. */
@ -297,17 +316,17 @@ const staffing = (b: MockBuilding, assigned: Map<number, number>) => {
/** Linear stand-in for the real staffing curve: empty buildings still make 10%. */
const outputFactor = (ratio: number) => 0.1 + 0.9 * ratio
function production(b: MockBuilding, assigned: Map<number, number>) {
function production(v: MockVillage, b: MockBuilding, assigned: Map<number, number>) {
const prod = CATALOG[b.type].production
if (!prod) return null
return { resource: prod.resource, per_hour: atLevel(prod.per_hour, b.level) * outputFactor(staffing(b, assigned)) }
return { resource: prod.resource, per_hour: atLevel(prod.per_hour, b.level) * outputFactor(staffing(b, assigned)) * instanceShare(v, b) }
}
function rates(v: MockVillage): Resources {
const assigned = assignWorkers(v)
const r: Resources = { wood: BASE_TRICKLE, clay: BASE_TRICKLE, iron: BASE_TRICKLE, food: 0 }
for (const b of v.buildings) {
const p = production(b, assigned)
const p = production(v, b, assigned)
if (p) r[p.resource] += p.per_hour
}
r.food -= villagers(v) * VILLAGER_UPKEEP + soldierUpkeep(v)
@ -317,7 +336,7 @@ function rates(v: MockVillage): Resources {
function storageCap(v: MockVillage) {
return v.buildings
.filter((b) => b.type === 'storage')
.reduce((sum, b) => sum + atLevel(CATALOG.storage.capacity!, b.level), 0)
.reduce((sum, b) => sum + atLevel(CATALOG.storage.capacity!, b.level) * instanceShare(v, b), 0)
}
/** stored + rate × elapsed, written back into the village. */
@ -421,7 +440,7 @@ function upgradeOffer(v: MockVillage, b: MockBuilding): Offer | null {
const current = queuedLevel(v, b)
if (current < 1 || current >= MAX_LEVEL) return null
const target = current + 1
const cost = levelCost(b.type, target)
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 }
}
@ -438,14 +457,14 @@ function demolishOffer(v: MockVillage, b: MockBuilding) {
function buildOptions(v: MockVillage): BuildOption[] {
return BUILDING_TYPES.map((type) => {
const entry = CATALOG[type]
const cost = levelCost(type, 1)
const { cost, share } = newInstance(v, type)
const blocked =
entry.max_per_village !== null && instances(v, type) >= entry.max_per_village
? 'max_per_village'
: !spaceFits(v, type)
? 'no_space'
: null
return { type, space: entry.space, target_level: 1, cost, seconds: levelSeconds(type, 1), blocked, requires: [] }
return { type, space: entry.space, target_level: 1, cost, seconds: levelSeconds(type, 1), blocked, requires: [], instance_share: share }
})
}
@ -513,7 +532,8 @@ function missingRequirements(v: MockVillage, unit: UnitType) {
.map(([type, level]) => ({ type: type as BuildingType, level: level ?? 0 }))
}
const secondsPerUnit = (b: MockBuilding, unit: UnitType) => Math.max(1, Math.ceil(UNITS[unit].seconds / (1 + 0.05 * Math.max(0, b.level - 1))))
const secondsPerUnit = (v: MockVillage, b: MockBuilding, unit: UnitType) =>
Math.max(1, Math.ceil(UNITS[unit].seconds / (1 + 0.05 * Math.max(0, b.level - 1)) / instanceShare(v, b)))
function military(v: MockVillage, at: number): MilitaryBuilding[] {
const pop = villagers(v) - militia(v)
@ -530,7 +550,7 @@ function military(v: MockVillage, at: number): MilitaryBuilding[] {
return {
unit,
cost,
seconds: secondsPerUnit(b, unit),
seconds: secondsPerUnit(v, b, unit),
upkeep,
max: requires.length ? 0 : Math.max(0, Math.min(byResources, Math.floor(pop / upkeep))),
blocked: requires.length ? ('requires' as const) : null,
@ -647,7 +667,7 @@ function view(state: MockState, v: MockVillage, now: number): VillageState {
builders: BUILDERS,
buildings: v.buildings.map(
(b): Building => {
const p = production(b, assigned)
const p = production(v, b, assigned)
return {
id: b.id,
type: b.type,
@ -662,6 +682,7 @@ function view(state: MockState, v: MockVillage, now: number): VillageState {
// Placeholder: production only; the backend lists every effect.
effects: p ? [{ key: 'production', value: round(p.per_hour), resource: p.resource }] : [],
next_effects: null,
instance_share: instanceShare(v, b),
}
},
),
@ -804,7 +825,7 @@ const travelMode = (mode: unknown) => TRAVEL_MODES.find((m) => m.mode === mode)
function hospital(v: MockVillage) {
const wounded = (v.wounded ?? []).filter((w) => w.heals_at > Date.now())
return {
beds: v.buildings.filter((b) => b.type === 'hospital').reduce((n, b) => n + 20 * b.level, 0),
beds: v.buildings.filter((b) => b.type === 'hospital').reduce((n, b) => n + Math.floor(20 * b.level * instanceShare(v, b)), 0),
occupied: wounded.reduce((n, w) => n + Object.values(w.units).reduce((a, c) => a + (c ?? 0), 0), 0),
share: 0.5,
heal_seconds: 14400,
@ -1285,7 +1306,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')
const cost = levelCost(type, 1)
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)
return view(state, v, now)
@ -1340,7 +1361,7 @@ const handlers: Record<string, Handler> = {
if (missingRequirements(v, unit).length) fail('requires')
if (count * entry.upkeep > villagers(v) - militia(v)) fail('villagers')
pay(v, mapCost(entry.cost, (n) => n * count))
const seconds = secondsPerUnit(b, unit)
const seconds = secondsPerUnit(v, b, unit)
const last = (v.recruits ?? []).filter((o) => o.building_id === b.id).at(-1)
const starts = last && last.finishes_at > now ? last.finishes_at : now
v.recruits = [

View File

@ -539,9 +539,15 @@ export interface Building {
upgrade: Offer | null
/** null when the building cannot go lower (core buildings at level 1); target level 0 removes it. */
demolish: { target_level: number; refund: Cost; seconds: number } | null
/** What one instance does at its level, and one level up (null at max level). */
/** What one instance does at its level, and one level up (null at max level); a copy's come scaled by `instance_share`. */
effects: BuildingEffect[]
next_effects: BuildingEffect[] | null
/**
* Share of full output this instance works at (production, capacity, hides, beds, training
* speed): 1 for the only or first instance of its type, then 0.5, 0.25, … for the copies,
* ranked by level. Copies cost more too; the server's costs include it.
*/
instance_share: number
}
/** One line of what a building does: production at full staffing (per hour), percentages as whole numbers. */
@ -564,6 +570,8 @@ export interface BuildOption extends Offer {
space: number
/** Requirements no building meets yet; only advanced buildings have any. */
requires: { type: BuildingType; level: number }[]
/** Share the new instance would work at: 1 for the first of its type, below 1 for a copy (whose `cost` is already raised). */
instance_share: number
}
export interface BuildOrder {

8
app/utils/copies.ts Normal file
View File

@ -0,0 +1,8 @@
// Building copies: the server sends each instance's `instance_share` (1 for the first of a
// type, 0.5, 0.25, … for the copies); the client only shows it as a percentage.
/** A copy works below full output. Missing shares (older servers) count as full. */
export const isCopy = (share: number | undefined) => share !== undefined && share < 1
/** 0.5 → 50, 0.125 → 12.5. */
export const sharePercent = (share: number) => Math.round(share * 1000) / 10

View File

@ -15,7 +15,7 @@ export function demoVillage(): VillageState {
]
const buildings = levels.map(([type, level], i): Building => ({
id: i + 1, type, level, space: SPACE[type] ?? 4, priority: 5,
workers: { assigned: 4, required: 4 }, staffing: 1, production: null, upgrade: null, demolish: null, effects: [], next_effects: null,
workers: { assigned: 4, required: 4 }, staffing: 1, production: null, upgrade: null, demolish: null, effects: [], next_effects: null, instance_share: 1,
}))
return {
id: 0, points: 2400, buildings, queue: [], troops: { spear: 160, sword: 90, light_cavalry: 30 },

View File

@ -337,6 +337,12 @@ export default {
free_space: '{free} space free',
submit: 'Build',
},
copy: {
share: 'works at {share}%',
this: 'Copy: works at {share}%',
new: 'New copy: works at {share}%, costs more',
offer: 'This copy would work at {share}% and cost more than the first.',
},
queue: {
title: 'Build queue',
builders: 'Builders: {count}',