From 01df1924c06fbf24cef2ff5f6aeea9058b99e71e Mon Sep 17 00:00:00 2001 From: rope Date: Wed, 4 Nov 2026 15:42:17 +0000 Subject: [PATCH] Regions: each new town favours its gathering trades, common materials most Co-Authored-By: Claude Opus 5.5 --- app.py | 4 +++- game.py | 45 +++++++++++++++++++++++++++++++++------ static/app.js | 15 ++++++++++--- tests/test_game.py | 53 ++++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 102 insertions(+), 15 deletions(-) diff --git a/app.py b/app.py index 1f5178f..f230cd9 100644 --- a/app.py +++ b/app.py @@ -152,6 +152,8 @@ def player_view(world: dict, player: dict, now: float) -> dict: view["recon_gain"] = {action["id"]: game.recon_gain(world, player, action) for action in game.ACTIONS if action.get("special") == "recon"} level = game.skill_level(player, "firekeeping") + view["region_yield"] = {action["id"]: round(game.region_yield(world, action) - 1, 3) for action in game.ACTIONS + if game.region_yield(world, action) > 1} view["firekeeping"] = {"minutes": game.fire_minutes(level), "bank": game.fire_bank(level)} # Inputs as the fires leave them: a lit fire pays the log. view["fuelled"] = {action["id"]: game.fire_inputs(world, action) for action in game.ACTIONS if action.get("fire")} @@ -363,7 +365,7 @@ class Handler(BaseHTTPRequestHandler): pouch = body.get("pouch") return game.travel(world, player, str(body.get("to")), pouch if isinstance(pouch, dict) else {}, now) if path == "/api/charter": - return game.propose_charter(world, player, body.get("name"), now) + return game.propose_charter(world, player, body.get("name"), body.get("region"), now) if path == "/api/charter/sign": return game.sign_charter(world, player, now) if path == "/api/charter/cancel": diff --git a/game.py b/game.py index 99a4948..2e911a8 100644 --- a/game.py +++ b/game.py @@ -1690,7 +1690,17 @@ CITY_KEYS = ("events", "next_event_at", "next_minor_at", "next_hardship_at", "ne TRAVEL_TIME = 12 * 60 # game seconds on the road between two towns POUCH_SLOTS = 6 # kinds of item you can carry on the road... POUCH_STACK = 100 # ...and how many of each -MAX_CITIES = 3 +# Where a town stands: one town to a region, between them every gathering trade. Each trip out brings +# back more, most of all of the commonest stuff, and more as the town grows. XP stays the same. +REGIONS = { + "mountains": {"name": "Mountains", "icon": "⛰️", "skills": ["mining"]}, + "forest": {"name": "Forest", "icon": "🌲", "skills": ["woodcutting", "hunting"]}, + "coast": {"name": "Coast", "icon": "🌊", "skills": ["fishing"]}, + "plains": {"name": "Plains", "icon": "🌾", "skills": ["farming"]}, +} +REGION_YIELD = [1.0, 0.75, 0.5, 0.3, 0.2, 0.1] # more per trip at each material tier, for a full city +TOWN_SCALE = [0.5, 0.7, 0.85, 1.0] # how much of that a hamlet, village, town and city get +MAX_CITIES = 1 + len(REGIONS) FOUNDING = {"settlers": 20, "gold": 5000, "provisions": 200} # what a new town takes from the one it leaves FOUNDING_GRACE = 24 # game hours a new town gets before its first threat lands @@ -4427,9 +4437,13 @@ def _store(world: dict, order: dict, qty: int, now: float) -> None: def _hand_over(world: dict, order: dict, owner: dict, now: float) -> None: - """Put delivered items in the requester's bank; what doesn't fit waits on the order, - as it does while they're in another town.""" + """Put delivered items in the requester's bank in the order's town, whether or not they're + there; if they are, what doesn't fit waits on the order.""" if owner["at"] != world.here: + bank = owner["banks"].setdefault(world.here, {}) + bank[order["item"]] = bank.get(order["item"], 0) + order["held"] + note(world, owner, now, "Received", order["item"], order["held"], label=f"Work order · {world['city']['name']}") + order["held"] = 0 return fits = min(order["held"], bank_room(world, owner, order["item"])) if fits: @@ -4583,11 +4597,15 @@ def arrive(world: World, player: dict) -> None: note(world, player, trip["arrive"], "Arrived", label=world["city"]["name"]) -def propose_charter(world: World, player: dict, name, now: float) -> str | None: +def propose_charter(world: World, player: dict, name, region, now: float) -> str | None: """Ask the guild to found a new town. A second founder has to sign before it happens.""" name = str(name or "").strip() if not 3 <= len(name) <= 24: return "A town's name takes 3 to 24 letters" + if region not in REGIONS: + return "Pick a region" + if any(city.get("region") == region for city in world.doc["cities"].values()): + return f"There's already a town in the {REGIONS[region]['name'].lower()}" if world["charter"]: return "A charter is already waiting to be signed" if len(world.doc["cities"]) >= MAX_CITIES: @@ -4597,7 +4615,7 @@ def propose_charter(world: World, player: dict, name, now: float) -> str | None: error = _founding_short(world) if error: return error - world["charter"] = {"name": name, "by": player["id"], "from": world.here, "at": now} + world["charter"] = {"name": name, "region": region, "by": player["id"], "from": world.here, "at": now} log(world, now, "tier", f"{player['name']} drew up a charter for a new town: {name}") return None @@ -4633,6 +4651,7 @@ def sign_charter(world: World, player: dict, now: float) -> str | None: del giving["stockpile"]["provisions"] city = _new_city(now, speed(world), charter["name"]) city["population"] = float(FOUNDING["settlers"]) + city["region"] = charter["region"] city["stockpile"]["provisions"] = FOUNDING["provisions"] city["next_event_at"] = now world.doc["cities"][_slug(charter["name"])] = city @@ -4653,12 +4672,14 @@ def kingdom_view(world: World, now: float) -> dict: cities = [] for key, city in world.doc["cities"].items(): people = [p for p in world["players"].values() if p["at"] == key and not p["travel"]] - cities.append({"id": key, "name": city["name"], "population": int(city["population"]), + cities.append({"id": key, "name": city["name"], "region": city.get("region"), "town_scale": TOWN_SCALE[city["tier"]], + "population": int(city["population"]), "tier": TIERS[city["tier"]]["name"], "repelled": city["repelled"], "people": [p["id"] for p in people], "founded": city["founded"]}) charter = world["charter"] return {"cities": cities, "charter": charter, "pouch": {"slots": POUCH_SLOTS, "stack": POUCH_STACK}, "founding": FOUNDING, "max": MAX_CITIES, "short": _founding_short(world), + "regions": REGIONS, "region_yield": REGION_YIELD, "road": [{"id": p["id"], **{k: p["travel"][k] for k in ("from", "to", "arrive")}} for p in world["players"].values() if p["travel"]]} @@ -5944,10 +5965,20 @@ def _modifier_sources(world: dict) -> list[dict]: return sources +def region_yield(world: dict, action: dict) -> float: + """How much more this gathering brings back in this town, for standing where it does.""" + city = world["city"] + region = REGIONS.get(city.get("region")) + if not region or action["skill"] not in region["skills"] or not action["outputs"]: + return 1.0 + tier = sum(action["level"] >= level for level in TIER_LEVELS) - 1 + return 1 + REGION_YIELD[tier] * TOWN_SCALE[city["tier"]] + + def skill_mods(world: dict, action: dict) -> dict: """How weather, happenings and problems change this action right now.""" skill = action["skill"] - mods = {"speed": 1.0, "yield": 1.0, "xp": 1.0, "blocked": None} + mods = {"speed": 1.0, "yield": region_yield(world, action), "xp": 1.0, "blocked": None} for source in _modifier_sources(world): mods["speed"] *= source.get("speed", {}).get(skill, 1) mods["yield"] *= source.get("yield", {}).get(skill, 1) diff --git a/static/app.js b/static/app.js index 2934c38..77c3b7c 100644 --- a/static/app.js +++ b/static/app.js @@ -935,6 +935,11 @@ function pageHtml() { // --- kingdom ---------------------------------------------------------------- let pouch = {}; +function regionPerks(region, scale) { + const y = S.kingdom.region_yield; + return `${region.skills.map((id) => skillOf(id).icon).join("")} +${pct(y[0] * scale)} → +${pct(y.at(-1) * scale)}`; +} + function townName(id) { return S.kingdom.cities.find((c) => c.id === id)?.name || id; } @@ -944,9 +949,11 @@ function kingdom() { const towns = k.cities.map((c) => { const here = c.id === me.at && !me.travel; const road = k.road.filter((r) => r.to === c.id).length; + const region = k.regions[c.region]; return `

${esc(c.name)} ${here ? `📍 Here` : ""}

${esc(c.tier)} · 👥 ${fmt(c.population)} · 🛡️ ${fmt(c.repelled)}${road ? ` · 🛤️ ${road}` : ""}

+ ${region ? `

${region.icon} ${esc(region.name)} · ${regionPerks(region, c.town_scale)}

` : ""}

${c.people.length ? c.people.map((uid) => esc(playerName(uid))).join(", ") : `Nobody here`}

${here || me.travel ? "" : ``}
`; @@ -963,13 +970,15 @@ function kingdom() { `; const charter = k.charter; const full = k.cities.length >= k.max; + const open = Object.entries(k.regions).filter(([id]) => !k.cities.some((c) => c.region === id)); const cost = `${COIN} ${fmt(S.city.treasury)} / ${fmt(k.founding.gold)} · ${itemChip("provisions", S.city.stockpile.provisions || 0)} / ${fmt(k.founding.provisions)} · 👥 ${k.founding.settlers}`; const founding = charter - ? `

📜 ${esc(charter.name)} · ${esc(playerName(charter.by))}

+ ? `

📜 ${esc(charter.name)} · ${k.regions[charter.region].icon} ${esc(k.regions[charter.region].name)} · ${esc(playerName(charter.by))}

${charter.by === me.id ? `` : ``}` : full ? `

${k.cities.length} / ${k.max} towns

` : `

${cost}

${S.city.next_tier ? `

${esc(k.short)}

` : `
+
`}`; return `
${towns}
@@ -1948,7 +1957,7 @@ function actionCard(a) { return `
${icon}
-
${esc(a.name)} ${Object.keys(a.outputs).map((id) => needTag(id) + fittedTag(id)).join("")}
+
${esc(a.name)} ${Object.keys(a.outputs).map((id) => needTag(id) + fittedTag(id)).join("")}${me.region_yield[a.id] ? ` +${pct(me.region_yield[a.id])}` : ""}
Level ${a.level} · ${dur(time)} · ${a.xp} XP
${orderable ? `` : ""}${Object.keys(a.inputs).length ? `` : ""}
${io ? `
${io}
` : ""} @@ -3351,7 +3360,7 @@ document.addEventListener("click", (e) => { if (d.pouchDrop) { delete pouch[d.pouchDrop]; return render(); } if ("charterNew" in d) { if (!confirm(`Draw up a charter? Once signed, ${S.kingdom.founding.settlers} settlers, gold and provisions leave this town.`)) return; - return act("/api/charter", { name: $("[data-keep=charter-name]").value }); + return act("/api/charter", { name: $("[data-keep=charter-name]").value, region: $("[data-keep=charter-region]").value }); } if ("charterSign" in d) { if (!confirm(`Sign the charter for ${S.kingdom.charter.name}? The town is founded at once.`)) return; diff --git a/tests/test_game.py b/tests/test_game.py index b296c94..6e00102 100644 --- a/tests/test_game.py +++ b/tests/test_game.py @@ -3607,8 +3607,8 @@ class KingdomTest(unittest.TestCase): city["treasury"] = 10000.0 city["stockpile"]["provisions"] = 500 - def found(self, name="Saltmere", now=T0): - self.assertIsNone(game.propose_charter(self.world, self.a, name, now)) + def found(self, name="Saltmere", now=T0, region="coast"): + self.assertIsNone(game.propose_charter(self.world, self.a, name, region, now)) self.assertIsNone(game.sign_charter(self.world, self.b, now)) return self.world.at(game._slug(name)) @@ -3617,7 +3617,7 @@ class KingdomTest(unittest.TestCase): return now + game.TRAVEL_TIME def test_a_charter_needs_a_second_founder(self): - game.propose_charter(self.world, self.a, "Saltmere", T0) + game.propose_charter(self.world, self.a, "Saltmere", "coast", T0) self.assertEqual(len(self.world.doc["cities"]), 1) self.assertIsNotNone(game.sign_charter(self.world, self.a, T0)) self.assertIsNone(game.sign_charter(self.world, self.b, T0)) @@ -3634,7 +3634,7 @@ class KingdomTest(unittest.TestCase): def test_only_a_full_city_can_found(self): self.world["city"]["tier"] = 2 - self.assertIn("city", game.propose_charter(self.world, self.a, "Saltmere", T0)) + self.assertIn("city", game.propose_charter(self.world, self.a, "Saltmere", "coast", T0)) def test_travel_pauses_work_and_arrives_with_the_pouch(self): new = self.found() @@ -3781,6 +3781,51 @@ class KingdomTest(unittest.TestCase): self.assertEqual(self.a["banks"][game.HOME_CITY]["stone"], 5) self.assertNotIn("stone", self.a["bank"]) + def test_every_gathering_trade_has_a_region(self): + self.assertEqual({skill for region in game.REGIONS.values() for skill in region["skills"]}, game.GATHERING) + + def test_one_town_to_a_region(self): + self.found() + self.assertIn("already", game.propose_charter(self.world, self.a, "Brine", "coast", T0)) + self.assertIsNone(game.propose_charter(self.world, self.a, "Brine", "forest", T0)) + + def test_a_region_favours_its_own_trades_and_common_stuff(self): + hills = self.found(region="mountains") + action = game.ACTION_BY_ID + self.assertEqual(game.region_yield(hills, action["mine_stone"]), 1.5) + self.assertEqual(game.region_yield(hills, action["mine_copper"]), 1.5) + self.assertEqual(game.region_yield(hills, action["mine_runite"]), 1.05) + self.assertEqual(game.region_yield(hills, action["cut_logs"] if "cut_logs" in action else next(a for a in game.ACTIONS if a["skill"] == "woodcutting")), 1.0) + self.assertEqual(game.region_yield(self.world, action["mine_copper"]), 1.0) + + def test_the_bonus_grows_with_the_town(self): + hills = self.found(region="mountains") + hills["city"]["tier"] = len(game.TIERS) - 1 + self.assertEqual(game.region_yield(hills, game.ACTION_BY_ID["mine_copper"]), 2.0) + + def test_a_region_pays_more_ore_but_the_same_xp(self): + hills = self.found(region="mountains") + hills["city"]["tier"] = len(game.TIERS) - 1 + self.a["cls"] = "builder" + home = game.new_player("c", "Cy", "builder", T0) + self.world["players"]["c"] = home + self.a["at"] = hills.here + action = game.ACTION_BY_ID["mine_copper"] + for player, world in ((self.a, hills), (home, self.world)): + for _ in range(10): + game._finish_action(world, player, action, T0, random.Random(3), game.empty_summary()) + self.assertEqual(self.a["bank"]["copper_ore"], 2 * home["bank"]["copper_ore"]) + self.assertEqual(self.a["xp"]["mining"], home["xp"]["mining"]) + + def test_a_filled_order_goes_to_your_bank_in_its_town(self): + new = self.found() + order = {"id": 1, "by": "a", "item": "stone", "held": 40} + self.a["at"] = new.here + game._hand_over(self.world, order, self.a, T0) + self.assertEqual(order["held"], 0) + self.assertEqual(self.a["banks"][game.HOME_CITY]["stone"], 40) + self.assertNotIn("stone", self.a["bank"]) + def test_old_saves_gain_towns(self): doc = json.loads(json.dumps(self.world.doc)) doc["version"] = 39