"""Project 02, Step 3b: the unit (two squads) and travel (Dillon's design, 2026-11-10/11). Dillon: "Each Unit will leave the base at the most of 16 soldiers split between squad 1 and 2 with at most of 8 soldiers each. They will travel from foot, animal, or vehicle which will have thier tiers and types." And 2026-11-11: "remember two squads make a unit." Each rule pinned to a number from data.py; distances pinned to the real map.""" import math import tempfile import unittest from pathlib import Path from p02 import campaign as K from p02 import data as D def fresh(seed=1, soldiers=16): c = K.new_campaign(seed) while len(c.roster) < soldiers: c.roster.append(K.Soldier(f"Extra{len(c.roster)}", "rifleman")) c.supplies = 500 return c def a_mission(c, rid="phelps", group=0, days=8): m = K.Mission(c.next_id, rid, group, "raid", c.day + days, [list(p) for p in D.REGIONS[rid]["groups"][1 - group]["pods"]]) c.next_id += 1 c.missions.append(m) return m def ally(c, unlock): """Make the group that offers this asset your ally in its region.""" rid, g = next((rid, g) for rid, r in D.REGIONS.items() for g, grp in enumerate(r["groups"]) if unlock in grp.get("unlocks", []) and c.regions[rid].owner is None) # a county not taken yet c.regions[rid].owner, c.regions[rid].ally = g, True def names(c, a, b): return [s.name for s in c.roster[a:b]] class TheUnit(unittest.TestCase): def test_two_squads_of_eight_make_a_unit_of_sixteen(self): self.assertEqual((D.SQUADS, D.SQUAD_SIZE), (2, 8)) # Dillon's numbers c = fresh() m = a_mission(c) b = K.launch(c, m.id, [names(c, 0, 8), names(c, 8, 16)]) mine = [u for u in b.units if u.side == "squad"] self.assertEqual(len(mine), 16) self.assertEqual(sorted(u.squad for u in mine), [1] * 8 + [2] * 8) self.assertEqual({u.name for u in mine if u.squad == 2}, set(names(c, 8, 16))) self.assertEqual(len({(u.x, u.y) for u in b.units}), len(b.units)) # nobody stacked def test_one_squad_is_a_unit_too(self): c = fresh() m = a_mission(c) b = K.launch(c, m.id, [names(c, 0, 3), []]) self.assertEqual([u.squad for u in b.units if u.side == "squad"], [1, 1, 1]) def test_what_the_unit_cannot_be(self): c = fresh(soldiers=20) m = a_mission(c) for squads, why in (([names(c, 0, 9)], "nine in a squad"), ([names(c, 0, 8), names(c, 8, 17)], "nine in Squad 2"), ([names(c, 0, 2), names(c, 2, 4), names(c, 4, 6)], "three squads"), ([names(c, 0, 3), names(c, 2, 5)], "one soldier in both"), ([[], []], "nobody")): with self.assertRaises(K.CampaignError, msg=why): K.launch(c, m.id, squads) self.assertEqual((c.day, c.supplies), (1, 500)) # refused: nothing spent class TheRoad(unittest.TestCase): def test_distances_are_the_real_map(self): # Outside facts: Rolla (Phelps) is about 40 km from the fort in a straight line, Jefferson # City (Cole) about 100; St. Clair (Osceola) and Carter (Van Buren) are the far edges. km = {rid: K.region_km(rid) for rid in D.REGIONS} self.assertTrue(30 <= km["phelps"] <= 60, km["phelps"]) self.assertTrue(80 <= km["cole"] <= 115, km["cole"]) self.assertEqual(sorted(sorted(km, key=km.get)[-2:]), ["carter", "stclair"]) def test_days_on_the_road(self): for rid in D.REGIONS: for mode, t in D.TRAVEL.items(): self.assertEqual(K.travel_days(rid, mode), max(1, math.ceil(K.region_km(rid) / t["km_day"]))) self.assertEqual(K.travel_days("benton", "on foot"), 5) # 125 km at 30 a day self.assertEqual(K.travel_days("benton", "trucks"), 1) def test_the_fort_runs_on_while_the_unit_travels(self): c = fresh() m = a_mission(c, "benton") doomed = a_mission(c, "phelps", days=2) # expires on the way lean = c.regions["phelps"].lean K.launch(c, m.id, names(c, 0, 4), "on foot") days = K.travel_days("benton", "on foot") self.assertEqual(c.day, 1 + days - 1) # the last day ends in battle self.assertNotIn(doomed, c.missions) self.assertEqual(c.regions["phelps"].lean, lean - D.LEAN_EXPIRED) def test_the_way_home_keeps_them_tired(self): for mode in ("on foot", "trucks"): c = fresh() ally(c, "trucks") m = a_mission(c, "benton") b = K.launch(c, m.id, names(c, 0, 2), mode) b.outcome = "won" day = c.day K.finish(c, m.id, b) self.assertEqual(c.roster[0].tired_until, day + 1 + D.FATIGUE_DAYS + K.travel_days("benton", mode), mode) self.assertIsNone(c.trip) def test_too_slow_to_get_there(self): c = fresh() m = a_mission(c, "benton", days=K.travel_days("benton", "on foot")) with self.assertRaises(K.CampaignError): K.launch(c, m.id, names(c, 0, 2), "on foot") K.launch(c, m.id, names(c, 0, 2), "forced march") # 3 days: in time def test_gone_when_they_get_there(self): c = fresh() m = a_mission(c, "benton") a_mission(c, "benton", group=0, days=2) # expires on the way: group 1 gains... c.regions["benton"].lean = -(100 - D.LEAN_EXPIRED) # ...and that settles it out = K.launch(c, m.id, names(c, 0, 2), "on foot") self.assertIsNone(out) self.assertIsNotNone(c.regions["benton"].owner) self.assertNotIn(m, c.missions) self.assertGreater(c.roster[0].tired_until, c.day) self.assertIsNone(c.trip) class TheConvoy(unittest.TestCase): """3c part 3: a mount's or machine's own utility slots.""" def test_slots_cost_and_locks(self): c = fresh() with self.assertRaises(K.CampaignError): K.fit_convoy(c, "trucks", 0, "ammo crates") # no trucks yet ally(c, "trucks") K.fit_convoy(c, "trucks", 0, "ammo crates") K.fit_convoy(c, "trucks", 1, "rations") with self.assertRaises(K.CampaignError): K.fit_convoy(c, "trucks", 2, "aid station") # trucks have 2 slots with self.assertRaises(K.CampaignError): K.fit_convoy(c, "on foot", 0, "rations") # walking has none self.assertEqual(c.supplies, 500 - D.CONVOY["ammo crates"]["cost"] - D.CONVOY["rations"]["cost"]) K.fit_convoy(c, "trucks", 0, None) self.assertEqual(c.convoy["trucks"], ["rations"]) def test_only_on_trips_that_way(self): for mode, clip in (("trucks", 1), ("on foot", 0)): c = fresh() ally(c, "trucks") K.fit_convoy(c, "trucks", 0, "ammo crates") b = K.launch(c, a_mission(c).id, names(c, 0, 2), mode) u = b.units[0] base = D.WEAPONS[c.roster[0].gun()]["clip"] self.assertEqual(u.clip, base + clip, mode) def test_aid_station_and_rations(self): c = fresh() ally(c, "trucks") K.fit_convoy(c, "trucks", 0, "aid station") K.fit_convoy(c, "trucks", 1, "rations") m = a_mission(c, "benton") s0, s1 = c.roster[0], c.roster[1] b = K.launch(c, m.id, [s0.name, s1.name], "trucks") b.outcome = "won" hurt = next(u for u in b.units if u.name == s1.name) hurt.hp = hurt.max_hp - 2 day = c.day K.finish(c, m.id, b) road = K.travel_days("benton", "trucks") for item, key, said in (("rations", "rest", "{} days less tired"), ("aid station", "heal", "a quarter")): self.assertIn(said.format(D.CONVOY[item][key]), D.CONVOY[item]["about"]) # says what it does self.assertEqual(D.CONVOY["aid station"]["heal"], 0.75) # "a quarter faster" self.assertGreater(D.CONVOY["rations"]["rest"], 0) self.assertEqual(s0.tired_until, day + 1 + max(0, D.FATIGUE_DAYS + road - D.CONVOY["rations"]["rest"])) self.assertEqual(s1.wounded_until, day + 1 + -(-2 * D.WOUND_DAYS_PER_HP * D.CONVOY["aid station"]["heal"] // 1)) def test_kept_in_the_save(self): c = fresh() ally(c, "boats") K.fit_convoy(c, "boats", 0, "grenade crate") path = Path(tempfile.mkdtemp()) / "s.json" K.save(c, path) self.assertEqual(K.load(path).convoy, {"boats": ["grenade crate"]}) class TheTiers(unittest.TestCase): def test_foot_animal_and_mechanical_lines(self): # Dillon (3c): "from mechanical and animal that will increase in size, mass and its own utility" lines = {} for mode, t in D.TRAVEL.items(): lines.setdefault(t["type"], []).append((t["tier"], mode, t)) self.assertEqual(set(lines), {"foot", "animal", "mechanical"}) self.assertEqual({t for t, *_ in lines["foot"]}, {1, 2}) for kind in ("animal", "mechanical"): line = sorted(lines[kind], key=lambda x: x[0]) self.assertEqual({t for t, *_ in line}, {1, 2, 3}, kind) seats = lambda t: 99 if t["seats"] is None else t["seats"] # noqa: E731 top = max(line, key=lambda x: x[0])[2] for tier, mode, t in line: self.assertLessEqual(t["slots"], top["slots"], mode) self.assertLessEqual(seats(t), seats(top), mode) self.assertGreater(top["slots"], min(t["slots"] for tr, _, t in line if tr == 1), kind) for mode, t in D.TRAVEL.items(): self.assertEqual(t["slots"] == 0, t["type"] == "foot", mode) free = [m for m, t in D.TRAVEL.items() if not t["needs"]] self.assertEqual(sorted(free), ["forced march", "on foot"]) # animals and machines: allies def test_every_asset_for_the_road_is_a_way_to_travel(self): offered = {u for r in D.REGIONS.values() for g in r["groups"] for u in g.get("unlocks", [])} needs = {n for t in D.TRAVEL.values() for n in t["needs"]} self.assertLessEqual({"mules", "horses", "motorbikes", "trucks", "boats"}, needs) self.assertLessEqual(needs, offered) # each one can be won def test_locked_until_an_ally_has_it(self): c = fresh() m = a_mission(c) with self.assertRaises(K.CampaignError): K.launch(c, m.id, names(c, 0, 2), "pack mules") ally(c, "mules") with self.assertRaises(K.CampaignError): K.launch(c, m.id, names(c, 0, 2), "draft wagons") # mules AND horses ally(c, "horses") K.launch(c, m.id, names(c, 0, 2), "draft wagons") def test_it_costs_supplies_per_soldier(self): c = fresh() ally(c, "horses") m = a_mission(c) K.launch(c, m.id, names(c, 0, 6), "horses") self.assertEqual(c.supplies, 500 - 6 * D.TRAVEL["horses"]["cost"]) c2 = fresh() ally(c2, "horses") c2.supplies = 6 * D.TRAVEL["horses"]["cost"] - 1 with self.assertRaises(K.CampaignError): K.launch(c2, a_mission(c2).id, names(c2, 0, 6), "horses") def test_horses_and_motorbikes_carry_one_squad(self): c = fresh() ally(c, "motorbikes") ally(c, "horses") m = a_mission(c) for mode in ("motorbikes", "horses"): with self.assertRaises(K.CampaignError): K.launch(c, m.id, [names(c, 0, 8), names(c, 8, 9)], mode) K.launch(c, m.id, [names(c, 0, 8)], "motorbikes") def test_boats_need_water(self): c = fresh() ally(c, "boats") with self.assertRaises(K.CampaignError): K.launch(c, a_mission(c, "phelps").id, names(c, 0, 2), "boats") K.launch(c, a_mission(c, "camden").id, names(c, 0, 2), "boats") def test_what_the_road_does_to_them_in_battle(self): for mode, stat in (("forced march", "aim"), ("horses", "mobility"), ("armored carriers", "defense")): base, tired = fresh(), fresh() for c in (base, tired): for need in D.TRAVEL[mode]["needs"]: ally(c, need) b0 = K.launch(base, a_mission(base).id, names(base, 0, 2), "on foot") b1 = K.launch(tired, a_mission(tired).id, names(tired, 0, 2), mode) u0 = next(u for u in b0.units if u.side == "squad") u1 = next(u for u in b1.units if u.side == "squad" and u.name == u0.name) self.assertEqual(getattr(u1, stat) - getattr(u0, stat), D.TRAVEL[mode]["extra"][stat], mode) def test_the_trip_survives_a_save(self): c = fresh() m = a_mission(c) b = K.launch(c, m.id, names(c, 0, 2), "on foot") path = Path(tempfile.mkdtemp()) / "save.json" K.save(c, path) c2 = K.load(path) days = K.travel_days("phelps", "on foot") self.assertEqual(c2.trip, {"mission": m.id, "region": "phelps", "travel": "on foot", "heal": 1.0, "rest": 0, "days": days, "squads": [names(c, 0, 2)], "gone": days - 1, "stage": "battle"}) b.outcome = "won" K.finish(c2, m.id, b) self.assertIsNone(c2.trip) if __name__ == "__main__": unittest.main()