-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgenerate_adventure_data.py
More file actions
1099 lines (902 loc) · 39.1 KB
/
Copy pathgenerate_adventure_data.py
File metadata and controls
1099 lines (902 loc) · 39.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
generate_adventure_data.py
Generate game data for the C++ Standard Adventure Game.
This script extracts sections from markdown files and generates JSON data files
for the adventure game: world map, NPCs, items, quests, and puzzles.
Uses the cpp_std_labels.lua file generated during markdown conversion to get
the correct label→chapter mappings (stable names to markdown file stems).
Usage:
python3 generate_adventure_data.py [--output DIR] [--versions DIR...]
"""
import argparse
import json
import re
import shutil
import subprocess
import sys
from pathlib import Path
from typing import Any
import yaml
from adventure_content_validator import validate_content
# Add src to path so we can import from cpp_std_converter
sys.path.insert(0, str(Path(__file__).parent / "src"))
from cpp_std_converter.utils import iter_section_headings
from cpp_std_converter.library_indexer import LibraryIndexer
def get_cppstdmd_sha() -> dict[str, str]:
"""Get current SHA of the cppstdmd repository.
Returns:
Dict with 'sha' (full) and 'short_sha' (7 chars) keys.
Returns empty strings if git command fails.
"""
try:
result = subprocess.run(
["git", "rev-parse", "HEAD"],
capture_output=True,
text=True,
cwd=Path(__file__).parent,
)
if result.returncode == 0:
sha = result.stdout.strip()
return {"sha": sha, "short_sha": sha[:7]}
except Exception:
pass
return {"sha": "", "short_sha": ""}
# Realm theming - maps stable name prefix to (display_name, description, theme)
REALM_THEMES: dict[str, tuple[str, str, str]] = {
"intro": ("The Grand Entrance", "Where all journeys through the Standard begin", "welcoming"),
"lex": ("Lexicon Tower", "A tall library of lexical knowledge and tokens", "scholarly"),
"basic": ("The Foundations", "Ancient temple ruins inscribed with fundamental laws", "ancient"),
"expr": (
"Expression Fields",
"Open testing grounds for expressions and operators",
"experimental",
),
"stmt": ("Statement Sanctum", "Control flow chambers with branching paths", "labyrinthine"),
"dcl": ("Declaration Domain", "Bureaucratic halls of formal declarations", "governmental"),
"class": ("Class Citadel", "Medieval castle with towers of inheritance", "medieval"),
"over": ("Overload Observatory", "Where function signatures are matched", "observatory"),
"temp": ("Template Tundra", "Frozen crystalline structures of abstract patterns", "arctic"),
"except": ("Exception Caverns", "Underground tunnels with try-catch bridges", "subterranean"),
"cpp": ("Preprocessor Catacombs", "Ancient tunnels of macro magic", "underground"),
"module": ("Module Mountains", "Modern peaks of code organization", "mountainous"),
"concepts": ("Concept Cathedral", "Grand cathedral of constraints and requirements", "gothic"),
"library": ("The Standard Library", "Grand entrance hall to the library realms", "grand"),
"support": ("Support Chambers", "Foundation utilities and type support", "utility"),
"concepts.lib": ("Library Concepts Hall", "Where library concepts are defined", "academic"),
"diagnostics": ("Diagnostics Den", "Where errors and exceptions are catalogued", "medical"),
"mem": ("Memory Depths", "Deep caverns of memory management", "cavernous"),
"utilities": ("Utility Workshop", "Inventor's workshop with tools", "workshop"),
"strings": ("String Springs", "Flowing waters of text manipulation", "aquatic"),
"containers": ("Container Harbor", "Docks with different container ships", "nautical"),
"iterators": ("Iterator Isle", "Bridge-connected islands of traversal", "archipelago"),
"ranges": ("Range Frontier", "New frontier territory of composable views", "frontier"),
"algorithms": ("Algorithm Academy", "Training grounds for algorithmic arts", "athletic"),
"numerics": ("Numerics Nexus", "Mathematical computation center", "mathematical"),
"time": ("Chrono Clocktower", "Giant clockwork tower of time utilities", "clockwork"),
"locales": ("Locale Lands", "International territories of localization", "international"),
"input": ("Input Streams", "Rivers flowing into the program", "riverside"),
"output": ("Output Streams", "Rivers flowing out to the world", "riverside"),
"re": ("Regex Realm", "Pattern matching territories", "mystical"),
"atomics": ("Atomic Arenas", "Where indivisible operations occur", "futuristic"),
"thread": ("Thread Nexus", "Multi-dimensional concurrent hub", "futuristic"),
"exec": ("Execution Engine", "Where senders and receivers connect", "mechanical"),
}
# Era mappings
ERA_NAMES: dict[str, str] = {
"n3337": "C++11",
"n4140": "C++14",
"n4659": "C++17",
"n4861": "C++20",
"n4950": "C++23",
"trunk": "C++26",
}
def load_label_to_chapter_from_lua(md_dir: Path) -> dict[str, str]:
"""
Load the label→chapter mapping from cpp_std_labels.lua in the markdown directory.
This file is generated during markdown conversion and contains the correct
mapping of stable names to markdown file stems (not LaTeX filenames).
Args:
md_dir: Path to the markdown output directory (e.g., n4950/)
Returns:
Dict mapping stable name → markdown file stem (e.g., "dcl.dcl" → "dcl")
"""
lua_file = md_dir / "cpp_std_labels.lua"
if not lua_file.exists():
return {}
content = lua_file.read_text(encoding="utf-8")
# Parse the Lua table format: return { ["key"] = "value", ... }
result = {}
for match in re.finditer(r'\["([^"]+)"\]\s*=\s*"([^"]+)"', content):
label, chapter = match.groups()
result[label] = chapter
return result
def extract_sections_from_markdown(
md_dir: Path, label_to_chapter: dict[str, str] | None = None
) -> dict[str, dict[str, Any]]:
"""Extract all sections with metadata from markdown files.
Uses the centralized iter_section_headings() from utils.py to correctly
handle titles containing < characters (like `<initializer_list>`).
"""
sections: dict[str, dict[str, Any]] = {}
for md_file in sorted(md_dir.glob("*.md")):
chapter = md_file.stem
content = md_file.read_text(encoding="utf-8")
# Use centralized heading parser from utils.py
headings = list(iter_section_headings(content))
for i, heading in enumerate(headings):
# Extract section content (up to next section)
section_start = heading.match_end
if i + 1 < len(headings):
section_end = headings[i + 1].match_start
else:
section_end = len(content)
section_content = content[section_start:section_end]
# Find cross-references in content
cross_refs = list(set(re.findall(r"\[\[([^\]]+)\]\]", section_content)))
# Use LabelIndexer mapping if available, fallback to current file
actual_chapter = chapter
if label_to_chapter and heading.stable_name in label_to_chapter:
actual_chapter = label_to_chapter[heading.stable_name]
sections[heading.stable_name] = {
"chapter": actual_chapter,
"title": heading.title,
"stableName": heading.stable_name,
"headingLevel": heading.level,
"documentOrder": i, # Position within chapter for ordering
"crossReferences": cross_refs,
"contentLength": len(section_content),
}
return sections
def build_hierarchy_from_levels(sections: dict[str, dict[str, Any]]) -> dict[str, dict[str, Any]]:
"""Build parent-child relationships using actual heading levels.
This correctly handles cases where a child's stable name doesn't start with
the parent's prefix (e.g., basic.lval under expr.prop).
"""
from collections import defaultdict
# Group sections by chapter and sort by document order
by_chapter: dict[str, list[tuple[str, dict]]] = defaultdict(list)
for name, data in sections.items():
by_chapter[data["chapter"]].append((name, data))
for chapter, chapter_sections in by_chapter.items():
# Sort by document order within chapter
chapter_sections.sort(key=lambda x: x[1]["documentOrder"])
# Use a stack to track the hierarchy based on heading levels
# Stack contains (stable_name, level) tuples
stack: list[tuple[str, int]] = []
for name, data in chapter_sections:
level = data["headingLevel"]
# Pop stack until we find a parent (level < current level)
while stack and stack[-1][1] >= level:
stack.pop()
# Parent is top of stack (or None if empty)
parent = stack[-1][0] if stack else None
data["parent"] = parent
# Initialize children list
data["children"] = []
# Add ourselves to parent's children list
if parent and parent in sections:
sections[parent]["children"].append(name)
# Push ourselves onto the stack
stack.append((name, level))
return sections
def infer_hierarchy(sections: dict[str, dict[str, Any]]) -> dict[str, dict[str, Any]]:
"""Infer parent-child relationships - delegates to build_hierarchy_from_levels."""
return build_hierarchy_from_levels(sections)
def generate_connections(
sections: dict[str, dict[str, Any]], chapter_order: list[str] | None = None
) -> dict[str, dict[str, Any]]:
"""Generate north/south/east/west connections between sections.
Navigation semantics:
- North/South: Previous/next sibling (same parent, same level)
- East/West: Previous/next chapter (based on std.tex order)
- Up (parent field): Go to parent section
- Down (children[0]): Go to first child
Args:
sections: Section data with parent/children already populated
chapter_order: List of chapter names in std.tex order (for E/W navigation)
"""
# Build chapter → root section mapping for E/W navigation
chapter_roots: dict[str, str] = {}
for name, data in sections.items():
if data.get("parent") is None:
# This is a root section (no parent within this chapter)
chapter = data["chapter"]
# First root section in document order becomes the chapter entry point
if chapter not in chapter_roots:
chapter_roots[chapter] = name
# Generate connections for each section
for name, data in sections.items():
parent = data.get("parent")
chapter = data["chapter"]
# Get siblings (sections with same parent)
if parent and parent in sections:
siblings = sections[parent].get("children", [])
else:
# Top-level: no siblings for N/S (only E/W for chapter navigation)
siblings = []
# Find position among siblings
try:
idx = siblings.index(name)
except ValueError:
idx = -1
# North/South: sibling navigation (null at chapter root level)
north = siblings[idx - 1] if idx > 0 else None
south = siblings[idx + 1] if 0 <= idx < len(siblings) - 1 else None
# East/West: chapter navigation
east = None
west = None
if chapter_order:
try:
chapter_idx = chapter_order.index(chapter)
# West = previous chapter's root section
if chapter_idx > 0:
prev_chapter = chapter_order[chapter_idx - 1]
west = chapter_roots.get(prev_chapter)
# East = next chapter's root section
if chapter_idx < len(chapter_order) - 1:
next_chapter = chapter_order[chapter_idx + 1]
east = chapter_roots.get(next_chapter)
except ValueError:
pass # Chapter not in order list
data["connections"] = {
"north": north,
"south": south,
"east": east,
"west": west,
}
return sections
def get_chapter_order_from_std_tex(draft_dir: Path) -> list[str]:
"""Extract chapter order from std.tex file.
Returns list of chapter stable names (markdown file stems) in document order.
"""
std_tex = draft_dir / "std.tex"
if not std_tex.exists():
return []
content = std_tex.read_text(encoding="utf-8")
# Extract \include{filename} in order
includes = re.findall(r"\\include\{([^}]+)\}", content)
# Map LaTeX filenames to markdown chapter names
# Most map directly, but some have special handling
latex_to_md = {
"expressions": "expr",
"statements": "stmt",
"declarations": "dcl",
"classes": "class",
"overloading": "over",
"templates": "temp",
"exceptions": "except",
"preprocessor": "cpp",
"modules": "module",
"lib-intro": "library",
"concepts": "concepts",
"diagnostics": "diagnostics",
"memory": "mem",
"utilities": "utilities",
"strings": "strings",
"containers": "containers",
"iterators": "iterators",
"ranges": "ranges",
"algorithms": "algorithms",
"numerics": "numerics",
"locales": "locales",
"iostreams": "input", # input.output -> input
"regex": "re",
"threads": "thread",
"grammar": "grammar",
"limits": "limits",
"compatibility": "compatibility",
"future": "future",
# These are their own names
"intro": "intro",
"lex": "lex",
"basic": "basic",
"support": "support",
"meta": "meta",
"time": "time",
"uax31": "uax31",
"front": "front",
"back": "back",
}
chapter_order = []
for inc in includes:
md_name = latex_to_md.get(inc, inc)
chapter_order.append(md_name)
return chapter_order
def get_realm_info(stable_name: str) -> tuple[str, str, str]:
"""Get realm display name, description, and theme for a stable name."""
parts = stable_name.split(".")
realm_key = parts[0]
# Try increasingly specific prefixes
for i in range(len(parts), 0, -1):
prefix = ".".join(parts[:i])
if prefix in REALM_THEMES:
return REALM_THEMES[prefix]
# Fallback
if realm_key in REALM_THEMES:
return REALM_THEMES[realm_key]
return (realm_key.title(), f"The {realm_key} area", "default")
def generate_display_name(title: str, stable_name: str) -> str:
"""Generate a thematic display name for a section."""
# Map common terms to thematic alternatives
theme_map = {
"overview": "Observation Deck",
"introduction": "Welcome Chamber",
"preamble": "Entry Hall",
"requirements": "Standards Hall",
"constructors": "Constructor Forge",
"destructor": "Destructor Crypt",
"members": "Member Hall",
"copy": "Duplication Chamber",
"move": "Transfer Station",
"assignment": "Assignment Alcove",
"virtual": "Phantom Tower",
"derived": "Inheritance Gates",
"access": "Access Control",
"special": "Special Functions Chamber",
"operators": "Operator Arena",
"declarations": "Declaration Hall",
"definitions": "Definition Chamber",
"scope": "Scope Tower",
"lookup": "Lookup Library",
"templates": "Template Workshop",
"concepts": "Concept Shrine",
"constraints": "Constraint Chamber",
}
# Special handling for .general sections - include parent context
if stable_name.endswith(".general"):
# Extract parent from stable name (e.g., "alg.sorting" from "alg.sorting.general")
parent_parts = stable_name.rsplit(".", 2)
if len(parent_parts) >= 2:
parent = parent_parts[-2].replace(".", " ").title()
return f"{parent} Main Hall"
return "Main Hall"
title_lower = title.lower()
for key, value in theme_map.items():
if key in title_lower:
return value
# Default: use title directly
return title
def detect_era_availability(stable_name: str, version_dirs: list[Path]) -> list[str]:
"""Detect which C++ versions contain this section."""
available = []
for version_dir in version_dirs:
if not version_dir.exists():
continue
# Check if any markdown file in this version contains the stable name
for md_file in version_dir.glob("*.md"):
content = md_file.read_text(encoding="utf-8")
if f'id="{stable_name}"' in content:
available.append(version_dir.name)
break
return available
def generate_world_map(
primary_version_dir: Path,
all_version_dirs: list[Path],
draft_dir: Path | None = None,
) -> dict[str, Any]:
"""Generate the complete world map from markdown content."""
# Get label→chapter mapping from the generated Lua file
label_to_chapter = load_label_to_chapter_from_lua(primary_version_dir)
if label_to_chapter:
print(f" Loaded {len(label_to_chapter)} label mappings from cpp_std_labels.lua")
sections = extract_sections_from_markdown(primary_version_dir, label_to_chapter)
sections = infer_hierarchy(sections)
# Get chapter order for E/W navigation
chapter_order = []
if draft_dir:
chapter_order = get_chapter_order_from_std_tex(draft_dir)
if chapter_order:
print(f" Loaded chapter order from std.tex ({len(chapter_order)} chapters)")
sections = generate_connections(sections, chapter_order)
world_map: dict[str, Any] = {
"version": "1.0.0",
"primaryEra": primary_version_dir.name,
"eras": ERA_NAMES,
"stableNameAliases": {}, # Will be populated later from aliases data
"realms": {},
"sections": {},
}
for stable_name, data in sections.items():
realm_key = stable_name.split(".")[0]
# Initialize realm if needed
if realm_key not in world_map["realms"]:
realm_name, realm_desc, realm_theme = get_realm_info(realm_key)
world_map["realms"][realm_key] = {
"name": realm_name,
"description": realm_desc,
"theme": realm_theme,
"sections": [],
}
world_map["realms"][realm_key]["sections"].append(stable_name)
# Build section entry
world_map["sections"][stable_name] = {
"stableName": stable_name,
"displayName": generate_display_name(data["title"], stable_name),
"title": data["title"],
"realm": realm_key,
"chapter": data["chapter"],
"parent": data.get("parent"),
"children": data.get("children", []),
"connections": data.get("connections", {}),
"availableIn": detect_era_availability(stable_name, all_version_dirs),
"description": f"Section covering {data['title'].lower()}.",
"npcs": [],
"items": [],
"puzzles": [],
}
return world_map
def load_yaml_content(game_content_dir: Path) -> dict[str, Any]:
"""Load hand-crafted content from YAML files if they exist."""
content: dict[str, Any] = {
"npcs": [],
"quests": [],
"items": [],
"puzzles": [],
"realms": {},
}
if not game_content_dir.exists():
return content
# Load NPCs
npcs_dir = game_content_dir / "npcs"
if npcs_dir.exists():
for yaml_file in npcs_dir.glob("*.yaml"):
try:
data = yaml.safe_load(yaml_file.read_text())
if data and "npcs" in data:
content["npcs"].extend(data["npcs"])
except Exception as e:
print(f"Warning: Failed to load {yaml_file}: {e}")
# Load quests
quests_dir = game_content_dir / "quests"
if quests_dir.exists():
for yaml_file in quests_dir.rglob("*.yaml"):
try:
data = yaml.safe_load(yaml_file.read_text())
if data:
# Handle both formats: {id: ..., ...} and {quests: [...]}
if "quests" in data:
content["quests"].extend(data["quests"])
elif "id" in data:
content["quests"].append(data)
except Exception as e:
print(f"Warning: Failed to load {yaml_file}: {e}")
# Load items
items_dir = game_content_dir / "items"
if items_dir.exists():
for yaml_file in items_dir.glob("*.yaml"):
try:
data = yaml.safe_load(yaml_file.read_text())
if data and "items" in data:
content["items"].extend(data["items"])
except Exception as e:
print(f"Warning: Failed to load {yaml_file}: {e}")
# Load puzzles
puzzles_dir = game_content_dir / "puzzles"
if puzzles_dir.exists():
for yaml_file in puzzles_dir.rglob("*.yaml"):
try:
data = yaml.safe_load(yaml_file.read_text())
if data:
# Handle both formats: {id: ..., ...} and {puzzles: [...]}
if "puzzles" in data:
content["puzzles"].extend(data["puzzles"])
elif "id" in data:
content["puzzles"].append(data)
except Exception as e:
print(f"Warning: Failed to load {yaml_file}: {e}")
# Load realm overrides
realms_file = game_content_dir / "realms.yaml"
if realms_file.exists():
try:
data = yaml.safe_load(realms_file.read_text())
if data and "realms" in data:
content["realms"] = data["realms"]
except Exception as e:
print(f"Warning: Failed to load {realms_file}: {e}")
return content
def merge_content(world_map: dict[str, Any], yaml_content: dict[str, Any]) -> dict[str, Any]:
"""Merge hand-crafted YAML content into the world map."""
# Override realm info from YAML
for realm_key, realm_data in yaml_content.get("realms", {}).items():
if realm_key in world_map["realms"]:
world_map["realms"][realm_key].update(realm_data)
# Add NPCs to their locations
for npc in yaml_content.get("npcs", []):
npc_id = npc.get("id")
locations = npc.get("locations", [])
for loc in locations:
if loc == "*":
# NPC appears everywhere - handled by game client
continue
if loc in world_map["sections"]:
if npc_id not in world_map["sections"][loc]["npcs"]:
world_map["sections"][loc]["npcs"].append(npc_id)
# Add items to their source sections
for item in yaml_content.get("items", []):
source = item.get("sourceSection")
if source and source in world_map["sections"]:
item_id = item.get("id")
if item_id not in world_map["sections"][source]["items"]:
world_map["sections"][source]["items"].append(item_id)
# Add puzzles to their locations
for puzzle in yaml_content.get("puzzles", []):
location = puzzle.get("location")
if location and location in world_map["sections"]:
puzzle_id = puzzle.get("id")
if puzzle_id not in world_map["sections"][location]["puzzles"]:
world_map["sections"][location]["puzzles"].append(puzzle_id)
return world_map
def copy_source_files(source_dir: Path, output_dir: Path) -> None:
"""Copy adventure game source files to output directory."""
if not source_dir.exists():
print(f" Warning: Source directory not found: {source_dir}")
return
# Copy HTML
html_src = source_dir / "index.html"
if html_src.exists():
html_dst = output_dir / "adventure"
html_dst.mkdir(parents=True, exist_ok=True)
shutil.copy2(html_src, html_dst / "index.html")
# Copy JavaScript
js_src = source_dir / "js"
if js_src.exists():
js_dst = output_dir / "js" / "adventure"
js_dst.mkdir(parents=True, exist_ok=True)
for js_file in js_src.glob("*.js"):
shutil.copy2(js_file, js_dst / js_file.name)
# Copy CSS
css_src = source_dir / "css"
if css_src.exists():
css_dst = output_dir / "css"
css_dst.mkdir(parents=True, exist_ok=True)
for css_file in css_src.glob("*.css"):
shutil.copy2(css_file, css_dst / css_file.name)
def copy_version_markdown(version_dirs: list[Path], output_dir: Path) -> None:
"""Copy markdown files from version directories to output for deployment.
This replaces symlinks with actual file copies so the site is fully deployable.
Only copies .md and .lua files needed by the adventure game.
"""
for version_dir in version_dirs:
if not version_dir.exists():
continue
version_name = version_dir.name
dst_dir = output_dir / version_name
# Remove existing symlink if present
if dst_dir.is_symlink():
dst_dir.unlink()
# Create directory
dst_dir.mkdir(parents=True, exist_ok=True)
# Copy markdown files
md_count = 0
for md_file in version_dir.glob("*.md"):
shutil.copy2(md_file, dst_dir / md_file.name)
md_count += 1
# Copy lua files (cpp_std_labels.lua)
for lua_file in version_dir.glob("*.lua"):
shutil.copy2(lua_file, dst_dir / lua_file.name)
print(f" Copied {md_count} markdown files to {version_name}/")
def load_or_generate_aliases(
game_data_dir: Path, base_dir: Path, versions: list[str]
) -> dict[str, Any]:
"""Load existing aliases or generate new ones.
Returns the aliases data structure with forward_aliases and bidirectional maps.
"""
alias_file = game_data_dir / "stable-name-aliases.json"
# Import the generator function
from generate_stable_name_aliases import generate_aliases
# Generate aliases (will write to file)
return generate_aliases(
base_dir=base_dir,
output_file=alias_file,
versions=versions,
)
def load_episode_correlations(game_data_dir: Path) -> dict[str, list[dict]]:
"""Load C++ Weekly episode correlations if available.
Looks for episode-correlations.json generated by the correlation_db system.
This file maps stable names to related C++ Weekly episodes.
Returns:
Dict mapping stable_name -> list of episode info dicts
"""
correlations_file = game_data_dir / "episode-correlations.json"
if not correlations_file.exists():
return {}
try:
with open(correlations_file) as f:
correlations = json.load(f)
print(f" Loaded episode correlations for {len(correlations)} sections")
return correlations
except Exception as e:
print(f" Warning: Failed to load episode correlations: {e}")
return {}
def add_episode_links_to_world_map(
world_map: dict[str, Any],
episode_correlations: dict[str, list[dict]],
) -> dict[str, Any]:
"""Add C++ Weekly episode links to world map sections.
Args:
world_map: The generated world map data
episode_correlations: Mapping of stable_name -> episode list
Returns:
Updated world map with episode links added to sections
"""
if not episode_correlations:
return world_map
episodes_added = 0
for stable_name, section in world_map.get("sections", {}).items():
episodes = episode_correlations.get(stable_name, [])
if episodes:
# Add episodes to section data
section["cppWeeklyEpisodes"] = [
{
"episode": ep["episode"],
"title": ep["title"],
"youtubeUrl": ep["youtube_url"],
"confidence": ep["confidence"],
}
for ep in episodes
]
episodes_added += len(episodes)
if episodes_added > 0:
print(f" Added {episodes_added} C++ Weekly episode links to sections")
return world_map
def generate_adventure_data(
output_dir: Path,
primary_version: str = "n4950",
versions: list[str] | None = None,
game_content_dir: Path | None = None,
) -> None:
"""Main entry point: generate all adventure game data files."""
game_data_dir = output_dir / "data" / "game"
game_data_dir.mkdir(parents=True, exist_ok=True)
# Determine version directories
base_dir = Path(__file__).parent
if versions is None:
versions = ["n3337", "n4140", "n4659", "n4861", "n4950", "trunk"]
version_dirs = [base_dir / v for v in versions]
primary_dir = base_dir / primary_version
if not primary_dir.exists():
print(f"Error: Primary version directory not found: {primary_dir}")
sys.exit(1)
print("Generating adventure game data...")
print(f" Primary version: {primary_version}")
print(f" Output directory: {game_data_dir}")
# Generate stable name aliases
print("\n Generating stable name aliases...")
aliases_data = load_or_generate_aliases(game_data_dir, base_dir, versions)
# Find draft directory for std.tex (chapter ordering)
draft_dir = base_dir / "cplusplus-draft" / "source"
if not draft_dir.exists():
draft_dir = None
print(" Warning: cplusplus-draft/source not found, chapter order unavailable")
# Generate world map
world_map = generate_world_map(primary_dir, version_dirs, draft_dir)
print(f" Extracted {len(world_map['sections'])} sections from markdown")
# Add stable name aliases to world map for timeshift navigation
world_map["stableNameAliases"] = aliases_data.get("bidirectional", {})
# Load hand-crafted YAML content
if game_content_dir is None:
# Try default locations
for candidate in [
base_dir / "adventure-content",
base_dir / "game-content",
]:
if candidate.exists():
game_content_dir = candidate
break
yaml_content: dict[str, Any] = {
"npcs": [],
"quests": [],
"items": [],
"puzzles": [],
"realms": {},
}
if game_content_dir and game_content_dir.exists():
yaml_content = load_yaml_content(game_content_dir)
print(f" Loaded YAML content from: {game_content_dir}")
print(f" NPCs: {len(yaml_content['npcs'])}")
print(f" Quests: {len(yaml_content['quests'])}")
print(f" Items: {len(yaml_content['items'])}")
print(f" Puzzles: {len(yaml_content['puzzles'])}")
# Merge content
world_map = merge_content(world_map, yaml_content)
# Load and add C++ Weekly episode correlations
episode_correlations = load_episode_correlations(game_data_dir)
world_map = add_episode_links_to_world_map(world_map, episode_correlations)
# Add cppstdmd repository SHA for GitHub links
sha_info = get_cppstdmd_sha()
world_map["cppstdmdSha"] = sha_info["sha"]
world_map["cppstdmdShortSha"] = sha_info["short_sha"]
# Validate content before writing
# world_map sections already contain availableIn field for era validation
success, errors, warnings = validate_content(
quests=yaml_content["quests"],
npcs=yaml_content["npcs"],
items=yaml_content["items"],
puzzles=yaml_content["puzzles"],
world_map=world_map,
)
if warnings:
print("\n Validation warnings:")
for warning in warnings:
print(f" WARNING: {warning}")
if not success:
print("\n Content validation FAILED:")
for error in errors:
print(f" ERROR: {error}")
sys.exit(1)
print(" Content validation passed")
# Write output files
with open(game_data_dir / "world-map.json", "w") as f:
json.dump(world_map, f, indent=2)
print(f" Generated world-map.json ({len(world_map['sections'])} sections)")
with open(game_data_dir / "npcs.json", "w") as f:
json.dump(yaml_content["npcs"], f, indent=2)
print(f" Generated npcs.json ({len(yaml_content['npcs'])} NPCs)")
with open(game_data_dir / "items.json", "w") as f:
json.dump(yaml_content["items"], f, indent=2)
print(f" Generated items.json ({len(yaml_content['items'])} items)")
with open(game_data_dir / "quests.json", "w") as f:
json.dump(yaml_content["quests"], f, indent=2)
print(f" Generated quests.json ({len(yaml_content['quests'])} quests)")
with open(game_data_dir / "puzzles.json", "w") as f:
json.dump(yaml_content["puzzles"], f, indent=2)
print(f" Generated puzzles.json ({len(yaml_content['puzzles'])} puzzles)")
# Generate library entity index from LaTeX source
# Try versioned worktree first (for primary version), fall back to main source
latex_source_dir = base_dir / "cplusplus-draft" / "worktrees" / primary_version / "source"
if not latex_source_dir.exists():
latex_source_dir = base_dir / "cplusplus-draft" / "source"
if latex_source_dir.exists():
indexer = LibraryIndexer()
library_index = indexer.build_index(latex_source_dir)
with open(game_data_dir / "library-index.json", "w") as f:
json.dump(library_index, f)
print(f" Generated library-index.json ({len(library_index)} entities)")
else:
print(" Warning: LaTeX source not found, library index not generated")
# Copy source files (HTML, JS, CSS) from adventure-src to output
adventure_src_dir = base_dir / "adventure-src"
if adventure_src_dir.exists():
copy_source_files(adventure_src_dir, output_dir)
print(f" Copied source files from {adventure_src_dir}")
# Copy markdown files from version directories (replaces symlinks for deployment)
existing_version_dirs = [d for d in version_dirs if d.exists()]
if existing_version_dirs:
print(" Copying markdown files for deployment...")
copy_version_markdown(existing_version_dirs, output_dir)
print("\nAdventure game data generation complete!")
def should_regenerate(output_dir: Path, input_dirs: list[Path], force: bool = False) -> bool:
"""Check if output needs regeneration based on input mtimes.
Args:
output_dir: Directory containing output JSON files
input_dirs: List of directories containing input files (markdown, YAML)
force: If True, always regenerate
Returns:
True if regeneration is needed, False otherwise
"""
if force:
return True
# Check if key output files exist
game_data_dir = output_dir / "data" / "game"
world_map_file = game_data_dir / "world-map.json"
if not world_map_file.exists():
return True
output_mtime = world_map_file.stat().st_mtime
# Check all input directories for newer files
for input_dir in input_dirs:
if not input_dir.exists():
continue
# Check markdown files
for md_file in input_dir.rglob("*.md"):
if md_file.stat().st_mtime > output_mtime:
return True
# Check YAML files (in game-content directory)
for yaml_file in input_dir.rglob("*.yaml"):
if yaml_file.stat().st_mtime > output_mtime:
return True
# Check Lua files (label mappings)
for lua_file in input_dir.rglob("*.lua"):
if lua_file.stat().st_mtime > output_mtime:
return True
# Check HTML files (adventure-src)
for html_file in input_dir.rglob("*.html"):
if html_file.stat().st_mtime > output_mtime:
return True
# Check JavaScript files (adventure-src)
for js_file in input_dir.rglob("*.js"):
if js_file.stat().st_mtime > output_mtime:
return True
# Check CSS files (adventure-src)
for css_file in input_dir.rglob("*.css"):
if css_file.stat().st_mtime > output_mtime:
return True
# Check LaTeX files (cplusplus-draft source for library index)
for tex_file in input_dir.rglob("*.tex"):
if tex_file.stat().st_mtime > output_mtime:
return True
# Check for updated correlation data (external JSON input)