Skip to content

A town simulation in pale isometric line art, implemented with Ritmo ECS and PixiJs 3D

Works cancelled with their building give back what they took; one works per building a step

SandroMaglione1dd1c862060f
diff --git a/src/present.ts b/src/present.ts
index 2c19716..5657dd3 100644
--- a/src/present.ts
+++ b/src/present.ts
@@ -91,7 +91,7 @@ import { blockers as blockersOf, streetAdvice, streetShort, whyUnmet, type InTow
import { upgradeChanges, upgradePrice } from "./rules/upgrades.ts"
import { breadBonus, moreAdmin, shortOf, streetTax, suppliedTaxOf, taxOutlook, type TaxedHome } from "./rules/households.ts"
import { approvalTarget } from "./rules/approval.ts"
-import { salvageOf, salvageText } from "./rules/salvage.ts"
+import { salvageOf, salvageText, withWorks } from "./rules/salvage.ts"
import { rockBlocks, type RockPart } from "./render3d/rock-blocks.ts"
import { courseCount, GUIDE_QUIET, guideDone, guideStep, guideSteps, lockedNote, upcoming, type GuideFacts, type GuideFocus } from "./rules/guide.ts"
import { TAX_STEPS, ticks } from "./content.ts"
@@ -1028,8 +1028,8 @@ export const Interact = Game.System(
}
/** What pulling a building or home down would cost and give back, for the bulldozer and the panel. */
- const salvageFor = (cost: Kind["cost"], site: { readonly owed: Amounts } | undefined) => {
- const s = salvageOf(cost, site?.owed ?? null)
+ const salvageFor = (cost: Kind["cost"], site: { readonly owed: Amounts } | undefined, works?: { readonly kind: Kind; readonly owed: Amounts; readonly level: number }) => {
+ const s = withWorks(salvageOf(cost, site?.owed ?? null), works?.kind ?? {}, works)
const said = salvageText(s)
return `${site ? "cancel" : "pull down"}${said ? `: ${said}` : ""}`
}
@@ -1044,7 +1044,7 @@ export const Interact = Game.System(
const level = maybe(hoveredBuilding.data.upgraded)?.level ?? 0, works = maybe(hoveredBuilding.data.works)
const title = (upgradeAt(kind, level)?.title ?? kind.title).toLowerCase()
preview.tooltip = ui.tool === "bulldoze"
- ? `${title} · ${hoveredBuilding.data.hall.present ? "the town hall stays" : salvageFor(builtCost(kind, level), maybe(hoveredBuilding.data.site))}`
+ ? `${title} · ${hoveredBuilding.data.hall.present ? "the town hall stays" : salvageFor(builtCost(kind, level), maybe(hoveredBuilding.data.site), works && { kind, ...works })}`
: `${title} · ${problem ?? status}${works ? ` · becoming a ${upgradeAt(kind, works.level)!.title.toLowerCase()}, ${Math.round(worksShare(works)! * 100)}% built` : ""}`
} else if (hoveredHouse) {
const h = hoveredHouse.data.house.get()
@@ -1115,7 +1115,7 @@ export const Interact = Game.System(
groups,
pausable,
paused: pickedBuilding.data.paused.present,
- salvage: salvageFor(builtCost(kind, level), maybe(pickedBuilding.data.site)),
+ salvage: salvageFor(builtCost(kind, level), maybe(pickedBuilding.data.site), works && { kind, ...works }),
level,
upgraded: level > 0,
upgrade: upgradeOffer,
diff --git a/src/rules/salvage.ts b/src/rules/salvage.ts
index e415edf..e95edad 100644
--- a/src/rules/salvage.ts
+++ b/src/rules/salvage.ts
@@ -1,4 +1,4 @@
-import type { GoodId, Kind } from "../content.ts"
+import { upgradeAt, type GoodId, type Kind } from "../content.ts"
import { shippedRules, type Rules } from "./config.ts"
/** What pulling something down costs, and what comes back into storage. */
@@ -23,6 +23,19 @@ export const salvageOf = (cost: Kind["cost"], owed: Partial<Record<GoodId, numbe
return { fee: owed ? -cost.coins : Math.ceil(cost.coins * rules.salvage.fee), back }
}
+/**
+ * A building pulled down with works under way (see `Works`): its own salvage,
+ * and the works cancelled as a site is, their coins and what was already
+ * brought to them back whole.
+ */
+export const withWorks = (s: Salvage, kind: Pick<Kind, "upgrade">, works: { readonly owed: Partial<Record<GoodId, number>>; readonly level: number } | undefined, rules: Rules = shippedRules): Salvage => {
+ const up = works ? upgradeAt(kind, works.level) : undefined
+ if (!works || !up) return s
+ const w = salvageOf(up.cost, works.owed, rules), back = { ...s.back }
+ for (const [g, n] of Object.entries(w.back) as Array<[GoodId, number]>) back[g] = (back[g] ?? 0) + n
+ return { fee: s.fee + w.fee, back }
+}
+
/** "−25 coins · 2 planks, 3 stone back", for the bulldozer's tooltip and the toast. */
export const salvageText = (s: Salvage): string => {
const goods = Object.entries(s.back).map(([g, n]) => `${n} ${g}`).join(", ")
diff --git a/src/sim/edits.ts b/src/sim/edits.ts
index a18d7f7..b21e7c0 100644
--- a/src/sim/edits.ts
+++ b/src/sim/edits.ts
@@ -13,7 +13,7 @@ import { canPlace, cellsOf, GridBuilder, ownerAt, useAt } from "../grid.ts"
import { inside } from "../iso.ts"
import { generate } from "../mapgen.ts"
import { Appeal, Building, Census, Charter, Chronicle, Crown, Fate, Fires, Game, Guide, Rules, History, House, Inventory, Land, Ledger, Market, maybe, Mood, Pantry, Park, Paved, Paused, Policy, Rising, Rng, Rock, Service, Site, Status, Stock, Storage, Time, TownHall, Treasury, Ui, Upgraded, Works } from "../schema.ts"
-import { salvageOf, salvageText } from "../rules/salvage.ts"
+import { salvageOf, salvageText, withWorks } from "../rules/salvage.ts"
import { elapsed, rollWinters, termTicks, TileRead, useOf, write } from "./shared.ts"
import type { Rules as RuleInputs } from "../rules/config.ts"
import { crownGap, foundedFate } from "./term.ts"
@@ -45,7 +45,7 @@ export const ApplyEdits = Game.System(
halls: Game.Query({ selection: { hall: Game.Query.read(TownHall) } }),
/** Whatever was built at a cost, standing or a site: what pulling it down gives back. */
built: Game.Query({
- selection: { building: Game.Query.optional(Building), house: Game.Query.optional(House), site: Game.Query.optional(Site), paused: Game.Query.optional(Paused), upgraded: Game.Query.optional(Upgraded) }
+ selection: { building: Game.Query.optional(Building), house: Game.Query.optional(House), site: Game.Query.optional(Site), paused: Game.Query.optional(Paused), upgraded: Game.Query.optional(Upgraded), works: Game.Query.optional(Works) }
}),
stores: Game.Query({ selection: { inventory: Game.Query.write(Inventory) }, with: [Storage] }),
rocks: Game.Query({ selection: { rock: Game.Query.read(Rock) } }),
@@ -99,6 +99,8 @@ export const ApplyEdits = Game.System(
}
const halls = new Set<number>(queries.halls.each().map((row) => row.entity.id))
let coins = resources.treasury.get().coins
+ // Works begun in this step: they are in the world only once it ends, so a second click on the same building is refused here.
+ const begun = new Set<number>()
// What is left to spend this step: placing several things at once draws on the same stock.
const left: Record<string, number> = { ...resources.stock.get() }
@@ -308,7 +310,8 @@ export const ApplyEdits = Game.System(
const built = queries.built.get(owner)
if (built.ok && (built.value.data.building.present || built.value.data.house.present)) {
const b = maybe(built.value.data.building)
- const s = salvageOf(b ? builtCost(kinds[b.kind], maybe(built.value.data.upgraded)?.level ?? 0) : home.cost, maybe(built.value.data.site)?.owed ?? null, rules)
+ const own = salvageOf(b ? builtCost(kinds[b.kind], maybe(built.value.data.upgraded)?.level ?? 0) : home.cost, maybe(built.value.data.site)?.owed ?? null, rules)
+ const s = b ? withWorks(own, kinds[b.kind], maybe(built.value.data.works), rules) : own
if (s.fee > 0 && coins < s.fee) {
ui.toast(`Pulling it down costs ${s.fee} coins`)
break
@@ -407,7 +410,7 @@ export const ApplyEdits = Game.System(
const kind = kinds[data.building.get().kind]
const level = maybe(data.upgraded)?.level ?? 0
const up = nextUpgrade(kind, level)
- if (!up || data.works.present || data.site.present || data.rising.present) break
+ if (!up || data.works.present || begun.has(row.value.entity.id) || data.site.present || data.rising.present) break
const blocked = upgradeBlock(up, resources.fate.get().rank, rules)
if (blocked) {
ui.toast(`${up.title} ${blocked}`)
@@ -418,6 +421,7 @@ export const ApplyEdits = Game.System(
break
}
coins -= up.cost.coins
+ begun.add(row.value.entity.id)
const owed = amounts(up.cost.materials)
commands.insert(row.value.entity.id, Game.Command.entry(Works, { owed, total: total(owed), done: 0, level: (level + 1) as 1 | 2 }))
const was = (upgradeAt(kind, level)?.title ?? kind.title).toLowerCase()
diff --git a/test/endless.test.ts b/test/endless.test.ts
index b73ff79..9228d27 100644
--- a/test/endless.test.ts
+++ b/test/endless.test.ts
@@ -255,3 +255,43 @@ test("pulling down a market hall gives back part of the upgrade too", () => {
assert(back > Math.floor(base / 2), `${back} stone back, more than the market's own share`)
assert(back <= base + up)
})
+
+test("pulling down a building with works under way gives back the works' coins and what was brought, as a cancelled site does", () => {
+ const t = headless(charter)
+ t.run(1)
+ let cell = { x: 0, y: 0 }
+ t.change((s) => {
+ cell = market(s)
+ s.resources["Town/Fate"].rank = 2
+ })
+ t.edit({ type: "upgrade", cell })
+ t.run(1)
+ const begun = t.snapshot(), works = begun.entities.find((e) => e.components["Town/Building"]?.kind === "market")!.components["Town/Works"]
+ assert(works, "the works began")
+ const coins = begun.resources["Town/Treasury"].coins, stone = stockOf(begun, "stone")
+ const up = kinds.market.upgrade!
+ const brought = up.cost.materials.stone! - works.owed.stone
+ t.edit({ type: "remove", cell })
+ t.run(1)
+ const after = t.snapshot()
+ assert.equal(after.entities.some((e) => e.components["Town/Building"]?.kind === "market"), false)
+ // The market's own fee is a small share of its coins: the works' coins come back whole on top.
+ assert(after.resources["Town/Treasury"].coins >= coins + up.cost.coins - kinds.market.cost.coins, `${after.resources["Town/Treasury"].coins} from ${coins}`)
+ assert(stockOf(after, "stone") >= stone + brought)
+})
+
+test("two clicks to upgrade the same building in one step begin one works, and pay once", () => {
+ const t = headless(charter)
+ t.run(1)
+ let cell = { x: 0, y: 0 }
+ t.change((s) => {
+ cell = market(s)
+ s.resources["Town/Fate"].rank = 2
+ s.resources["Town/Treasury"].coins = 2000
+ })
+ t.edit({ type: "upgrade", cell }, { type: "upgrade", cell })
+ t.run(1)
+ const s = t.snapshot()
+ assert("Town/Works" in s.entities.find((e) => e.components["Town/Building"]?.kind === "market")!.components)
+ assert(s.resources["Town/Treasury"].coins > 2000 - 2 * kinds.market.upgrade!.cost.coins, String(s.resources["Town/Treasury"].coins))
+})