"""Drive the base and the war room's trip with its real windows on a virtual display (Steps 3e, 3f). Not part of the unit suite (it needs a display and Panda3D): xvfb-run -a -s "-screen 0 1280x800x24" ~/venvs/p02-3d/bin/python tests/warroom_smoke.py Opens the room on a seeded campaign, clicks a mission's flag, opens the plan (Tk, inside the Panda3D process), presses Go, lets the days play out on the table, plays the battle out by AI, comes home. Prints one line per check and exits non-zero on the first failure. """ import math import sys import tempfile from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) from panda3d.core import loadPrcFileData # noqa: E402 loadPrcFileData("", "window-type offscreen\nwin-size 640 400\naudio-library-name null\n") from direct.showbase.ShowBase import ShowBase # noqa: E402 from p02 import campaign as K # noqa: E402 from p02 import data as D # noqa: E402 from p02 import cutaway as CW # noqa: E402 from p02 import warroom as W # noqa: E402 def check(ok, what): print(("ok " if ok else "FAIL ") + what) if not ok: sys.exit(1) base = ShowBase() save = Path(tempfile.mkdtemp()) / "save.json" c = K.new_campaign(4) c.intel = 100 while len(c.missions) < 2: K.scout(c) m = max(c.missions, key=lambda x: K.travel_days(x.region, "on foot")) m.expires = c.day + 30 g = CW.Cutaway(base, c, save=lambda c: K.save(c, save)) room = g.room t = W.INTRO_SECONDS + 1 def run(seconds, step=0.05): global t end = t + seconds while t < end: t += step g.at(t) if room.tk is not None: room.tk.update() base.graphicsEngine.renderFrame() run(0.1) check(g.place == "base" and room.root.isHidden(), "opens on the whole base") g.tabs["command"]["command"](*g.tabs["command"]["extraArgs"]) run(CW.GLIDE_SECONDS + W.INTRO_SECONDS + 0.1) check(g.place == "command" and not room.root.isHidden() and room.in_map(), "the Command tab: at the table") flag = room.pieces[f"mission:{m.id}"].find("**/flag") from panda3d.core import Point2 # noqa: E402 lo, hi = flag.getTightBounds(base.cam) p = Point2() base.camLens.project((lo + hi) / 2, p) room.show(room.pick(p.x, p.y)) check(room.chosen == f"mission:{m.id}" and not room.plan_btn.isHidden(), "clicked the flag: Plan the unit shows") room.plan_btn["command"]() run(0.2) d = room.planner check(d.top.winfo_exists() and sum(v.get() for v in d.picks.values()) > 0, "the plan opened (Tk, in the room)") check(str(d.go_btn.cget("state")) == "disabled" and len(d.sides) == 2, "an incursion: Go waits for a side") d.sides[0].invoke() run(0.1) check(str(d.go_btn.cget("state")) == "normal", "picked a side: Go is live") day = c.day d.go_btn.invoke() run(0.1) check(not d.top.winfo_exists(), "the plan closed on Go") days = c.trip["days"] check(room.journey and room.journey["phase"] == "out" and c.day == day, f"on the road ({days} days), no day yet") run(days * W.DAY_SECONDS + W.LEAN_SECONDS + 0.2) check(c.day == day + days - 1 and K.load(save).trip["stage"] == "battle", "the days passed and were saved") bv = getattr(room, "battle_view", None) check(bv is not None and bv.root.winfo_exists(), "the battle opened") bv.on_auto_all() run(0.1) bv.on_back() run(0.1) check(c.trip is None and room.journey["phase"] == "home", f"settled ({bv.b.outcome}), coming home") run(W.HOME_SECONDS + 0.2) u = room.pieces["unit"].getPos() check(room.journey is None and math.dist((u.x, u.y), room.unit_home) < 1e-4, "home at the fort") check(K.load(save).day == c.day and not any(x.id == m.id for x in K.load(save).missions), "the save has it") # Step 3f: the Barracks tab, its window (Tk), the memorial, back to the table by its tab day = c.day g.tabs["barracks"]["command"](*g.tabs["barracks"]["extraArgs"]) run(CW.GLIDE_SECONDS + 0.2) check(g.place == "room" and g.key == "barracks" and g.use_btn["text"] == "The roster", "the Barracks tab: in") g.use_btn["command"]() run(0.2) check(g.desks.roster_top.winfo_exists() and len(g.desks.roster.get_children()) == len(c.roster), "the roster window opened (Tk, in the base)") g.desks.roster_top.destroy() g.tabs["rec"]["command"](*g.tabs["rec"]["extraArgs"]) run(CW.GLIDE_SECONDS + 0.2) g.use_btn["command"]() run(0.2) check(g.desks.memorial_top.winfo_exists(), "the memorial opened (" + str(sum(s.dead for s in c.roster)) + " fallen)") from p02 import briefs as B # noqa: E402 for x in B.fallen(c): print(" " + B.memorial_entry(x).replace("\n", "\n ")) check(all(x.fate and ", turn " in x.fate["story"] for x in B.fallen(c)), "each of the fallen: how, from the battle") g.desks.memorial_top.destroy() g.tabs["command"]["command"](*g.tabs["command"]["extraArgs"]) run(CW.GLIDE_SECONDS + 0.2) check(g.place == "command" and not room.root.isHidden() and c.day == day, "back at the table, no day gone") # Step 4 part 3: a caravan from the garage walks the table a day at a time, and comes home loaded c.affinity["phelps:0"] = 50 pile = c.pile("phelps", 0) weapon = next(i for i in D.REGIONS["phelps"]["groups"][0]["gear"] if i in D.WEAPONS) pile["recruits"], pile["items"] = 1, {weapon: 1} g.tabs["garage"]["command"](*g.tabs["garage"]["extraArgs"]) run(CW.GLIDE_SECONDS + 0.2) g.use_btn["command"]() run(0.1) g.desks.open_caravans() run(0.1) g.desks.caravan_buttons[("phelps", 0)].invoke() run(0.1) cp = g.desks.caravan_plan for n, v in cp.picks.items(): v.set(1 if n == list(cp.picks)[0] else 0) cp.changed() cp.go_btn.invoke() run(0.1) check(c.trip and c.trip.get("caravan") == ["phelps", 0] and room.journey and room.journey["phase"] == "out", f"a caravan out to Rolla ({c.trip['days']} days), its piece on the table") check("caravan is on the road to Rolla" in room.hud_text.getText(), "the news says so") people = len(c.roster) run(c.trip["days"] * W.DAY_SECONDS + W.LEAN_SECONDS + W.HOME_SECONDS + 0.4) check(c.trip is None and room.journey is None and c.store.get(weapon, 0) >= 1 and len(c.roster) >= people + 1, f"the caravan is home: {c.store.get(weapon)} {weapon} in the store, {len(c.roster) - people} recruits " "(and what the week put by on the way)") # Step 4 part 3b: raided on the war table: the battle opens, and when it's over the piece comes home D.RAID_CHANCE = 1.0 c.pile("phelps", 0)["recruits"] = 1 g.desks.open_caravans() run(0.1) g.desks.caravan_buttons[("phelps", 0)].invoke() run(0.1) cp = g.desks.caravan_plan for n, v in cp.picks.items(): v.set(1 if n in [s.name for s in c.ready()[:3]] else 0) cp.changed() cp.go_btn.invoke() run(c.trip["days"] * W.DAY_SECONDS + W.LEAN_SECONDS + 0.3) bv = getattr(room, "battle_view", None) check(bv is not None and bv.root.winfo_exists() and c.trip and c.trip["stage"] == "battle", "raided: the battle opened") bv.on_auto_all() run(0.1) bv.on_back() run(W.HOME_SECONDS + 0.3) check(c.trip is None and room.journey is None, f"the raid settled ({bv.b.outcome}), the piece home") print("WAR ROOM SMOKE PASSED")