"""Project 02, Step 3c part 1: weapons (Dillon, 2026-11-11: "Weapons are to be similar to long war of xcom"). Families, four tiers, an armory, and a pistol for everyone. Our own names and numbers.""" import tempfile import unittest from pathlib import Path from p02 import campaign as K from p02 import data as D from p02 import tactics as T def fresh(seed=1, supplies=1000): c = K.new_campaign(seed) c.supplies = supplies return c def ally_with(c, asset): rid, g = next((rid, g) for rid, r in D.REGIONS.items() for g, grp in enumerate(r["groups"]) if asset in grp.get("unlocks", [])) c.regions[rid].owner, c.regions[rid].ally = g, True def of(c, cls): return next(s for s in c.roster if s.cls == cls) class TheGuns(unittest.TestCase): def test_what_everyone_starts_with_is_unchanged(self): # The numbers Steps 1-3b were balanced on, written out: a tier-0 class weapon must be them. before = {"rifle": ((3, 5), 4, 10, (10, 0, -10)), "shotgun": ((4, 6), 3, 20, (25, -5, -40)), "machine gun": ((4, 6), 5, 5, (0, 0, -15)), "sniper rifle": ((4, 6), 3, 25, (-15, 10, 15))} for cls, c in D.CLASSES.items(): w = D.WEAPONS[D.weapon_key(c["weapon"])] self.assertEqual((w["damage"], w["clip"], w["crit"], w["range"]), before[c["weapon"]], cls) self.assertEqual((w["aim"], w.get("mobility", 0)), (0, 0), cls) self.assertEqual(D.CLASS_WEAPONS[cls][0], c["weapon"], cls) def test_families_and_four_tiers(self): self.assertEqual(len(D.WEAPON_TIERS), 4) self.assertEqual(set(D.FAMILIES), {"pistol", "SMG", "carbine", "rifle", "shotgun", "machine gun", "marksman rifle", "sniper rifle"}) for family in D.FAMILIES: ws = [D.WEAPONS[D.weapon_key(family, t)] for t in range(4)] for lo, hi in zip(ws, ws[1:]): self.assertLessEqual(lo["damage"], hi["damage"], family) self.assertLessEqual((lo["aim"], lo["crit"]), (hi["aim"], hi["crit"]), family) self.assertNotEqual((lo["damage"], lo["aim"], lo["crit"]), (hi["damage"], hi["aim"], hi["crit"])) costs = [t["cost"] for t in D.WEAPON_TIERS] self.assertEqual(costs, sorted(set(costs))) for fam in D.CLASS_WEAPONS.values(): self.assertNotIn("pistol", fam) # the pistol is everyone's sidearm self.assertEqual({f for fs in D.CLASS_WEAPONS.values() for f in fs} | {"pistol"}, set(D.FAMILIES)) def test_a_tier_adds_exactly_its_numbers(self): for family, base in D.FAMILIES.items(): for t, tier in enumerate(D.WEAPON_TIERS): w = D.WEAPONS[D.weapon_key(family, t)] self.assertEqual(w["damage"], (base["damage"][0] + tier["damage"], base["damage"][1] + tier["damage"]), (family, t)) self.assertEqual((w["aim"], w["crit"], w["clip"], w["range"]), (tier["aim"], base["crit"] + tier["crit"], base["clip"], base["range"])) def test_every_tier_can_be_won(self): offered = {u for r in D.REGIONS.values() for g in r["groups"] for u in g.get("unlocks", [])} for t in D.WEAPON_TIERS: self.assertTrue(not t["needs"] or set(t["needs"]) & offered, t["name"]) class TheArmory(unittest.TestCase): def test_buying_a_weapon(self): c = fresh() s = of(c, "scout") K.arm(c, s.name, "SMG", 1) self.assertEqual((s.gun(), c.supplies), ("pre-war surplus SMG", 1000 - D.WEAPON_TIERS[1]["cost"])) with self.assertRaises(K.CampaignError): K.arm(c, s.name, "SMG", 1) # already carrying it with self.assertRaises(K.CampaignError): K.arm(c, s.name, "sniper rifle", 0) # not a scout's K.arm(c, s.name, "shotgun", 0) # back to scrap: free self.assertEqual(c.supplies, 1000 - D.WEAPON_TIERS[1]["cost"]) def test_tiers_wait_for_allies(self): c = fresh() s = of(c, "rifleman") for tier in (2, 3): with self.assertRaises(K.CampaignError): K.arm(c, s.name, "rifle", tier) ally_with(c, D.WEAPON_TIERS[2]["needs"][1]) # either asset will do K.arm(c, s.name, "rifle", 2) ally_with(c, D.WEAPON_TIERS[3]["needs"][0]) K.arm(c, s.name, "rifle", 3) self.assertEqual(s.gun(), "Army arsenal rifle") def test_supplies(self): c = fresh(supplies=D.WEAPON_TIERS[1]["cost"] - 1) with self.assertRaises(K.CampaignError): K.arm(c, c.roster[0].name, D.CLASSES[c.roster[0].cls]["weapon"], 1) K.arm(c, c.roster[0].name, "pistol", 1) # a pistol is cheaper self.assertEqual(c.supplies, D.WEAPON_TIERS[1]["cost"] - 1 - K.weapon_cost("pistol", 1)) self.assertEqual(K.weapon_cost("pistol", 1), -(-D.WEAPON_TIERS[1]["cost"] * D.PISTOL_COST // 1)) def test_the_armory_list(self): c = fresh() s = of(c, "gunner") rows = K.armory(c, s) self.assertEqual(len(rows), (len(D.CLASS_WEAPONS["gunner"]) + 1) * 4) why = {(f, t): w for f, t, _, w in rows} self.assertEqual(why[("machine gun", 0)], "carrying it") self.assertIsNone(why[("rifle", 1)]) self.assertIn("needs an ally", why[("rifle", 2)]) def test_kept_in_the_save(self): c = fresh() K.arm(c, c.roster[0].name, "SMG" if c.roster[0].cls in ("rifleman", "scout", "medic") else D.CLASS_WEAPONS[c.roster[0].cls][1], 1) K.arm(c, c.roster[0].name, "pistol", 1) path = Path(tempfile.mkdtemp()) / "s.json" K.save(c, path) s2 = K.load(path).roster[0] self.assertEqual((s2.gun(), s2.pistol()), (c.roster[0].gun(), c.roster[0].pistol())) class InBattle(unittest.TestCase): def setUp(self): self.c = fresh() ally_with(self.c, "posse rifles") self.s = of(self.c, "rifleman") rid = next(r for r, reg in self.c.regions.items() if reg.owner is None) self.m = K.Mission(99, rid, 0, "raid", self.c.day + 20, [["militiaman"]]) self.c.missions.append(self.m) def unit(self, b): return next(u for u in b.units if u.name == self.s.name) def test_they_carry_what_they_bought(self): K.arm(self.c, self.s.name, "SMG", 2) u = self.unit(K.launch(self.c, self.m.id, [self.s.name])) self.assertEqual((u.weapon, u.sidearm), ("military SMG", "scrap-built pistol")) self.assertEqual(u.mobility, D.CLASSES["rifleman"]["mobility"] + D.FAMILIES["SMG"]["mobility"]) self.assertEqual((u.clip, u.ammo), (D.FAMILIES["SMG"]["clip"],) * 2) def test_a_bought_pistol_goes_too(self): gun = self.s.gun() K.arm(self.c, self.s.name, "pistol", 2) self.assertEqual((self.s.gun(), self.s.pistol()), (gun, "military pistol")) u = self.unit(K.launch(self.c, self.m.id, [self.s.name])) self.assertEqual((u.weapon, u.sidearm), (gun, "military pistol")) def test_a_better_tier_hits_more_often(self): b = T.Battle.new(3, squad=[("A", "rifleman", None, 1, D.weapon_key("rifle", 0)), ("B", "rifleman", None, 1, D.weapon_key("rifle", 2))], pods=[["militiaman"]]) a, bb = b.units[0], b.units[1] t = b.units[2] h0, c0 = b.odds(a, t, 5, 5) h2, c2 = b.odds(bb, t, 5, 5) self.assertEqual(h2 - h0, D.WEAPON_TIERS[2]["aim"]) self.assertEqual(c2 - c0, D.WEAPON_TIERS[2]["crit"]) def test_the_pistol_never_runs_dry(self): b = T.Battle.new(3, squad=[("A", "rifleman", None)], pods=[["bruiser"]]) a, t = b.units[0], b.units[1] t.x, t.y = a.x + 1, a.y # point blank, in plain sight for c in [(a.x + 1, a.y), (a.x, a.y)]: b.obst.pop(c, None) a.ammo = 0 self.assertFalse(b.can_shoot(a, t)) self.assertTrue(b.can_shoot(a, t, sidearm=True)) hp = t.hp b.rng.seed(1) while t.hp == hp: a.actions = 2 b.shoot(a, t, sidearm=True) self.assertEqual(a.ammo, 0) p = D.WEAPONS[a.sidearm] dealt = hp - t.hp self.assertTrue(1 <= dealt <= -(-(p["damage"][1] - t.armor) * 3 // 2), dealt) self.assertEqual(b.odds(a, t, sidearm=True)[0], min(100, a.aim + p["aim"] + p["range"][0] - t.defense - b.cover(t, t.x, t.y, a.x, a.y))) def test_out_of_ammo_with_one_action_the_ai_draws_its_pistol(self): b = T.Battle.new(3, squad=[("A", "rifleman", None)], pods=[["bruiser"]]) a, t = b.units[0], b.units[1] t.x, t.y, t.awake = a.x + 2, a.y, True for c in [(a.x + 1, a.y), (a.x + 2, a.y)]: b.obst.pop(c, None) a.ammo, a.actions = 0, 1 b._act(a) self.assertEqual((a.ammo, a.actions), (0, 0)) # shot with the pistol, didn't reload self.assertTrue(any("A hits" in l or "A misses" in l for l in b.log)) if __name__ == "__main__": unittest.main()