A step forward in definining YAML for the travel array.
[open-adventure.git] / travel.py
1 #!/usr/bin/env python3
2 #
3 # Enhance adventure.yaml entries with explicit properties based on Section 3
4 # of adventure.text.
5 #
6 # This script is meant to be gotten right, used once, and then discarded.
7 # We'll leave a copy in the repository history for reference 
8 #
9 # When in doubt, make the code dumber and the data smarter.
10 #
11 # Here's the original format description:
12 #
13 #  Section 3: Travel table.  Each line contains a location number (X), a second
14 #       location number (Y), and a list of motion numbers (see section 4).
15 #       each motion represents a verb which will go to Y if currently at X.
16 #       Y, in turn, is interpreted as follows.  Let M=Y/1000, N=Y mod 1000.
17 #               If N<=300       it is the location to go to.
18 #               If 300<N<=500   N-300 is used in a computed goto to
19 #                                       a section of special code.
20 #               If N>500        message N-500 from section 6 is printed,
21 #                                       and he stays wherever he is.
22 #       Meanwhile, M specifies the conditions on the motion.
23 #               If M=0          it's unconditional.
24 #               If 0<M<100      it is done with M% probability.
25 #               If M=100        unconditional, but forbidden to dwarves.
26 #               If 100<M<=200   he must be carrying object M-100.
27 #               If 200<M<=300   must be carrying or in same room as M-200.
28 #               If 300<M<=400   game.prop(M % 100) must *not* be 0.
29 #               If 400<M<=500   game.prop(M % 100) must *not* be 1.
30 #               If 500<M<=600   game.prop(M % 100) must *not* be 2, etc.
31 #       If the condition (if any) is not met, then the next different
32 #       "destination" value is used (unless it fails to meet *its* conditions,
33 #       in which case the next is found, etc.).  Typically, the next dest will
34 #       be for one of the same verbs, so that its only use is as the alternate
35 #       destination for those verbs.  For instance:
36 #               15      110022  29      31      34      35      23      43
37 #               15      14      29
38 #       This says that, from loc 15, any of the verbs 29, 31, etc., will take
39 #       him to 22 if he's carrying object 10, and otherwise will go to 14.
40 #               11      303008  49
41 #               11      9       50
42 #       This says that, from 11, 49 takes him to 8 unless game.prop(3)=0, in which
43 #       case he goes to 9.  Verb 50 takes him to 9 regardless of game.prop(3).
44 #
45 # In addition, it looks as though the action verb value 1 is a sentinel used
46 # when a table entry would have no motions at all in it.
47 #
48 import sys, yaml
49
50 # This is the original travel table from section 3 of adventure.text
51 section3 = (
52     (1, 2, 2, 44, 29),
53     (1, 3, 3, 12, 19, 43),
54     (1, 4, 5, 13, 14, 46, 30),
55     (1, 145, 6, 45),
56     (1, 8, 63),
57     (2, 1, 12, 43),
58     (2, 5, 44),
59     (2, 164, 45),
60     (2, 157, 46, 6),
61     (2, 580, 30),
62     (3, 1, 11, 32, 44),
63     (3, 179, 62),
64     (3, 181, 65),
65     (3, 79, 5, 14),
66     (4, 1, 4, 12, 45),
67     (4, 150, 43, 6),
68     (4, 156, 44),
69     (4, 7, 5, 46, 30),
70     (4, 8, 63),
71     (4, 745, 14),
72     (5, 2, 2, 43, 29),
73     (5, 1, 12),
74     (5, 158, 46, 6),
75     (5, 159, 44),
76     (5, 165, 45),
77     (6, 161, 46, 6),
78     (6, 163, 43),
79     (6, 21, 39),
80     (7, 1, 12),
81     (7, 4, 4, 45),
82     (7, 150, 43, 6),
83     (7, 154, 44),
84     (7, 8, 5, 16, 46, 63),
85     (7, 595, 60, 14, 30, 19, 3),
86     (8, 151, 43, 6),
87     (8, 154, 46),
88     (8, 153, 44),
89     (8, 1, 12),
90     (8, 7, 4, 13, 45),
91     (8, 303009, 3, 19, 30),
92     (8, 593, 3),
93     (9, 303008, 11, 29),
94     (9, 593, 11),
95     (9, 10, 17, 18, 19, 44),
96     (9, 14, 31),
97     (9, 11, 51),
98     (10, 9, 11, 20, 21, 43),
99     (10, 11, 19, 22, 44, 51),
100     (10, 14, 31),
101     (11, 303008, 63),
102     (11, 9, 64),
103     (11, 10, 17, 18, 23, 24, 43),
104     (11, 12, 25, 19, 29, 44),
105     (11, 180, 62),
106     (11, 14, 31),
107     (12, 303008, 63),
108     (12, 9, 64),
109     (12, 11, 30, 43, 51),
110     (12, 13, 19, 29, 44),
111     (12, 14, 31),
112     (13, 303008, 63),
113     (13, 9, 64),
114     (13, 11, 51),
115     (13, 12, 25, 43),
116     (13, 14, 23, 31, 44),
117     (14, 303008, 63),
118     (14, 9, 64),
119     (14, 11, 51),
120     (14, 13, 23, 43),
121     (14, 150020, 30, 31, 34),
122     (14, 15, 30),
123     (14, 16, 33, 44),
124     (15, 18, 36, 46),
125     (15, 17, 7, 38, 44),
126     (15, 19, 10, 30, 45),
127     (15, 150022, 29, 31, 34, 35, 23, 43),
128     (15, 14, 29),
129     (15, 34, 55),
130     (16, 14, 1),
131     (17, 15, 38, 43),
132     (17, 312596, 39),
133     (17, 412021, 7),
134     (17, 412597, 41, 42, 44, 69),
135     (17, 27, 41),
136     (18, 15, 38, 11, 45),
137     (19, 15, 10, 29, 43),
138     (19, 311028, 45, 37),
139     (19, 311029, 46, 36),
140     (19, 311030, 44, 7),
141     (19, 32, 45),
142     (19, 35074, 49),
143     (19, 211032, 49),
144     (19, 74, 66),
145     (20, 0, 1),
146     (21, 0, 1),
147     (22, 15, 1),
148     (23, 67, 43, 42),
149     (23, 68, 44, 61),
150     (23, 25, 30, 31),
151     (23, 648, 52),
152     (24, 67, 29, 11),
153     (25, 23, 29, 11),
154     (25, 524031, 56),
155     (25, 26, 56),
156     (26, 88, 1),
157     (27, 312596, 39),
158     (27, 412021, 7),
159     (27, 412597, 41, 42, 43, 69),
160     (27, 17, 41),
161     (27, 40, 45),
162     (27, 41, 44),
163     (28, 19, 38, 11, 46),
164     (28, 33, 45, 55),
165     (28, 36, 30, 52),
166     (29, 19, 38, 11, 45),
167     (30, 19, 38, 11, 43),
168     (30, 62, 44, 29),
169     (31, 424089, 1),
170     (31, 90, 1),
171     (32, 19, 1),
172     (33, 182, 65),
173     (33, 28, 46),
174     (33, 34, 43, 53, 54),
175     (33, 35, 44),
176     (33, 159302, 71),
177     (33, 183, 71),
178     (34, 33, 30, 55),
179     (34, 15, 29),
180     (35, 33, 43, 55),
181     (35, 20, 39),
182     (36, 37, 43, 17),
183     (36, 28, 29, 52),
184     (36, 39, 44),
185     (36, 65, 70),
186     (37, 36, 44, 17),
187     (37, 38, 30, 31, 56),
188     (38, 37, 56, 29, 11),
189     (38, 595, 60, 14, 30, 4, 5, 3, 19),
190     (39, 36, 43, 23),
191     (39, 64, 30, 52, 58),
192     (39, 65, 70),
193     (40, 41, 1),
194     (41, 42, 46, 29, 23, 56),
195     (41, 27, 43),
196     (41, 59, 45),
197     (41, 60, 44, 17),
198     (42, 41, 29),
199     (42, 42, 45),
200     (42, 43, 43),
201     (42, 45, 46),
202     (42, 80, 44),
203     (43, 42, 44),
204     (43, 44, 46),
205     (43, 45, 43),
206     (44, 43, 43),
207     (44, 48, 30),
208     (44, 50, 46),
209     (44, 82, 45),
210     (45, 42, 44),
211     (45, 43, 45),
212     (45, 46, 43),
213     (45, 47, 46),
214     (45, 87, 29, 30),
215     (46, 45, 44, 11),
216     (47, 45, 43, 11),
217     (48, 44, 29, 11),
218     (49, 50, 43),
219     (49, 51, 44),
220     (50, 44, 43),
221     (50, 49, 44),
222     (50, 51, 30),
223     (50, 52, 46),
224     (51, 49, 44),
225     (51, 50, 29),
226     (51, 52, 43),
227     (51, 53, 46),
228     (52, 50, 44),
229     (52, 51, 43),
230     (52, 52, 46),
231     (52, 53, 29),
232     (52, 55, 45),
233     (52, 86, 30),
234     (53, 51, 44),
235     (53, 52, 45),
236     (53, 54, 46),
237     (54, 53, 44, 11),
238     (55, 52, 44),
239     (55, 55, 45),
240     (55, 56, 30),
241     (55, 57, 43),
242     (56, 55, 29, 11),
243     (57, 13, 30, 56),
244     (57, 55, 44),
245     (57, 58, 46),
246     (57, 83, 45),
247     (57, 84, 43),
248     (58, 57, 43, 11),
249     (59, 27, 1),
250     (60, 41, 43, 29, 17),
251     (60, 61, 44),
252     (60, 62, 45, 30, 52),
253     (61, 60, 43),
254     (61, 62, 45),
255     (61, 100107, 46),
256     (62, 60, 44),
257     (62, 63, 45),
258     (62, 30, 43),
259     (62, 61, 46),
260     (63, 62, 46, 11),
261     (64, 39, 29, 56, 59),
262     (64, 65, 44, 70),
263     (64, 103, 45, 74),
264     (64, 106, 43),
265     (65, 64, 43),
266     (65, 66, 44),
267     (65, 65556, 46),
268     (65, 68, 61),
269     (65, 60556, 29),
270     (65, 70070, 29),
271     (65, 39, 29),
272     (65, 50556, 45),
273     (65, 75072, 45),
274     (65, 71, 45),
275     (65, 65556, 30),
276     (65, 106, 30),
277     (66, 65, 47),
278     (66, 67, 44),
279     (66, 80556, 46),
280     (66, 77, 25),
281     (66, 96, 43),
282     (66, 50556, 50),
283     (66, 97, 72),
284     (67, 66, 43),
285     (67, 23, 44, 42),
286     (67, 24, 30, 31),
287     (68, 23, 46),
288     (68, 69, 29, 56),
289     (68, 65, 45),
290     (69, 68, 30, 61),
291     (69, 331120, 46),
292     (69, 119, 46),
293     (69, 109, 45),
294     (69, 113, 75),
295     (70, 71, 45),
296     (70, 65, 30, 23),
297     (70, 111, 46),
298     (71, 65, 48),
299     (71, 70, 46),
300     (71, 110, 45),
301     (72, 65, 70),
302     (72, 118, 49),
303     (72, 73, 45),
304     (72, 97, 48, 72),
305     (73, 72, 46, 17, 11),
306     (74, 19, 43),
307     (74, 331120, 44),
308     (74, 121, 44),
309     (74, 75, 30),
310     (75, 76, 46),
311     (75, 77, 45),
312     (76, 75, 45),
313     (77, 75, 43),
314     (77, 78, 44),
315     (77, 66, 45, 17),
316     (78, 77, 46),
317     (79, 3, 1),
318     (80, 42, 45),
319     (80, 80, 44),
320     (80, 80, 46),
321     (80, 81, 43),
322     (81, 80, 44, 11),
323     (82, 44, 46, 11),
324     (83, 57, 46),
325     (83, 84, 43),
326     (83, 85, 44),
327     (84, 57, 45),
328     (84, 83, 44),
329     (84, 114, 50),
330     (85, 83, 43, 11),
331     (86, 52, 29, 11),
332     (87, 45, 29, 30),
333     (88, 25, 30, 56, 43),
334     (88, 20, 39),
335     (88, 92, 44, 27),
336     (89, 25, 1),
337     (90, 23, 1),
338     (91, 95, 45, 73, 23),
339     (91, 72, 30, 56),
340     (92, 88, 46),
341     (92, 93, 43),
342     (92, 94, 45),
343     (93, 92, 46, 27, 11),
344     (94, 92, 46, 27, 23),
345     (94, 309095, 45, 3, 73),
346     (94, 611, 45),
347     (95, 94, 46, 11),
348     (95, 92, 27),
349     (95, 91, 44),
350     (96, 66, 44, 11),
351     (97, 66, 48),
352     (97, 72, 44, 17),
353     (97, 98, 29, 45, 73),
354     (98, 97, 46, 72),
355     (98, 99, 44),
356     (99, 98, 50, 73),
357     (99, 301, 43, 23),
358     (99, 100, 43),
359     (100, 301, 44, 23, 11),
360     (100, 99, 44),
361     (100, 159302, 71),
362     (100, 184, 71),
363     (100, 101, 47, 22),
364     (101, 100, 46, 71, 11),
365     (102, 103, 30, 74, 11),
366     (103, 102, 29, 38),
367     (103, 104, 30),
368     (103, 114618, 46),
369     (103, 115619, 46),
370     (103, 64, 46),
371     (104, 103, 29, 74),
372     (104, 105, 30),
373     (105, 104, 29, 11),
374     (105, 103, 74),
375     (106, 64, 29),
376     (106, 65, 44),
377     (106, 108, 43),
378     (107, 131, 46),
379     (107, 132, 49),
380     (107, 133, 47),
381     (107, 134, 48),
382     (107, 135, 29),
383     (107, 136, 50),
384     (107, 137, 43),
385     (107, 138, 44),
386     (107, 139, 45),
387     (107, 61, 30),
388     (108, 95556, 43, 45, 46, 47, 48, 49, 50, 29, 30),
389     (108, 106, 43),
390     (108, 626, 44),
391     (109, 69, 46),
392     (109, 113, 45, 75),
393     (110, 71, 44),
394     (110, 20, 39),
395     (111, 70, 45),
396     (111, 40050, 30, 39, 56),
397     (111, 50053, 30),
398     (111, 45, 30),
399     (112, 131, 49),
400     (112, 132, 45),
401     (112, 133, 43),
402     (112, 134, 50),
403     (112, 135, 48),
404     (112, 136, 47),
405     (112, 137, 44),
406     (112, 138, 30),
407     (112, 139, 29),
408     (112, 140, 46),
409     (113, 109, 46, 11),
410     (113, 445552, 45, 42, 69),
411     (113, 168, 45),
412     (114, 84, 48),
413     (115, 116, 49),
414     (116, 115, 47),
415     (116, 593, 30),
416     (117, 118, 49),
417     (117, 233660, 41, 42, 69, 47),
418     (117, 332661, 41),
419     (117, 303, 41),
420     (117, 332021, 39),
421     (117, 596, 39),
422     (118, 72, 30),
423     (118, 117, 29),
424     (119, 69, 45, 11),
425     (119, 653, 43, 7),
426     (120, 69, 45),
427     (120, 74, 43),
428     (121, 74, 43, 11),
429     (121, 653, 45, 7),
430     (122, 123, 47),
431     (122, 233660, 41, 42, 69, 49),
432     (122, 303, 41),
433     (122, 596, 39),
434     (122, 124, 15),
435     (122, 126, 28),
436     (122, 129, 40),
437     (123, 122, 44),
438     (123, 124, 43, 15),
439     (123, 126, 28),
440     (123, 129, 40),
441     (124, 123, 44),
442     (124, 125, 47, 36),
443     (124, 128, 48, 37, 30),
444     (124, 126, 28),
445     (124, 129, 40),
446     (125, 124, 46, 15),
447     (125, 126, 45, 28),
448     (125, 127, 43, 17),
449     (126, 125, 46, 23, 11),
450     (126, 124, 15),
451     (126, 610, 30),
452     (126, 178, 39),
453     (127, 125, 44, 11, 17),
454     (127, 124, 15),
455     (127, 126, 28),
456     (128, 124, 45, 29, 15),
457     (128, 129, 46, 30, 40),
458     (128, 126, 28),
459     (129, 128, 44, 29),
460     (129, 124, 15),
461     (129, 130, 43, 19, 40, 3),
462     (129, 126, 28),
463     (130, 129, 44, 11),
464     (130, 124, 15),
465     (130, 126, 28),
466     (131, 107, 44),
467     (131, 132, 48),
468     (131, 133, 50),
469     (131, 134, 49),
470     (131, 135, 47),
471     (131, 136, 29),
472     (131, 137, 30),
473     (131, 138, 45),
474     (131, 139, 46),
475     (131, 112, 43),
476     (132, 107, 50),
477     (132, 131, 29),
478     (132, 133, 45),
479     (132, 134, 46),
480     (132, 135, 44),
481     (132, 136, 49),
482     (132, 137, 47),
483     (132, 138, 43),
484     (132, 139, 30),
485     (132, 112, 48),
486     (133, 107, 29),
487     (133, 131, 30),
488     (133, 132, 44),
489     (133, 134, 47),
490     (133, 135, 49),
491     (133, 136, 43),
492     (133, 137, 45),
493     (133, 138, 50),
494     (133, 139, 48),
495     (133, 112, 46),
496     (134, 107, 47),
497     (134, 131, 45),
498     (134, 132, 50),
499     (134, 133, 48),
500     (134, 135, 43),
501     (134, 136, 30),
502     (134, 137, 46),
503     (134, 138, 29),
504     (134, 139, 44),
505     (134, 112, 49),
506     (135, 107, 45),
507     (135, 131, 48),
508     (135, 132, 30),
509     (135, 133, 46),
510     (135, 134, 43),
511     (135, 136, 44),
512     (135, 137, 49),
513     (135, 138, 47),
514     (135, 139, 50),
515     (135, 112, 29),
516     (136, 107, 43),
517     (136, 131, 44),
518     (136, 132, 29),
519     (136, 133, 49),
520     (136, 134, 30),
521     (136, 135, 46),
522     (136, 137, 50),
523     (136, 138, 48),
524     (136, 139, 47),
525     (136, 112, 45),
526     (137, 107, 48),
527     (137, 131, 47),
528     (137, 132, 46),
529     (137, 133, 30),
530     (137, 134, 29),
531     (137, 135, 50),
532     (137, 136, 45),
533     (137, 138, 49),
534     (137, 139, 43),
535     (137, 112, 44),
536     (138, 107, 30),
537     (138, 131, 43),
538     (138, 132, 47),
539     (138, 133, 29),
540     (138, 134, 44),
541     (138, 135, 45),
542     (138, 136, 46),
543     (138, 137, 48),
544     (138, 139, 49),
545     (138, 112, 50),
546     (139, 107, 49),
547     (139, 131, 50),
548     (139, 132, 43),
549     (139, 133, 44),
550     (139, 134, 45),
551     (139, 135, 30),
552     (139, 136, 48),
553     (139, 137, 29),
554     (139, 138, 46),
555     (139, 112, 47),
556     (140, 112, 45, 11),
557     (140, 338141, 46),
558     (140, 142, 46),
559     (141, 140, 45),
560     (141, 143, 46),
561     (142, 140, 1),
562     (143, 141, 44),
563     (143, 241560, 45),
564     (143, 144, 45),
565     (144, 143, 46, 11),
566     (145, 1, 43),
567     (145, 157, 44),
568     (145, 146, 45),
569     (145, 147, 46),
570     (146, 145, 43),
571     (146, 163, 44),
572     (146, 147, 45),
573     (146, 162, 46),
574     (147, 148, 43, 44),
575     (147, 146, 45),
576     (147, 145, 46),
577     (148, 147, 43, 45),
578     (148, 149, 44, 46),
579     (149, 148, 43, 45),
580     (149, 151, 44),
581     (149, 150, 46),
582     (150, 149, 43),
583     (150, 151, 44),
584     (150, 4, 45),
585     (150, 7, 46),
586     (151, 149, 43),
587     (151, 150, 44),
588     (151, 8, 45),
589     (151, 152, 46),
590     (152, 153, 43),
591     (152, 155, 44),
592     (152, 166, 45),
593     (152, 151, 46),
594     (153, 155, 43),
595     (153, 152, 44),
596     (153, 154, 45),
597     (153, 8, 46),
598     (154, 7, 43),
599     (154, 155, 44),
600     (154, 153, 45),
601     (154, 8, 46),
602     (155, 154, 43),
603     (155, 152, 44),
604     (155, 166, 45),
605     (155, 153, 46),
606     (156, 157, 43),
607     (156, 158, 44),
608     (156, 166, 45),
609     (156, 4, 46),
610     (157, 145, 43),
611     (157, 156, 44),
612     (157, 164, 45),
613     (157, 2, 46),
614     (158, 5, 43),
615     (158, 160, 44),
616     (158, 159, 45),
617     (158, 156, 46),
618     (159, 160, 43),
619     (159, 166, 44),
620     (159, 5, 45),
621     (159, 158, 46),
622     (160, 161, 43, 45),
623     (160, 158, 44),
624     (160, 159, 46),
625     (161, 162, 43),
626     (161, 160, 44, 46),
627     (161, 6, 45),
628     (162, 163, 43),
629     (162, 161, 44),
630     (162, 146, 45),
631     (162, 165, 46),
632     (163, 146, 43),
633     (163, 162, 44),
634     (163, 6, 45),
635     (163, 164, 46),
636     (164, 2, 43),
637     (164, 165, 44),
638     (164, 163, 45),
639     (164, 157, 46),
640     (165, 164, 43),
641     (165, 5, 44),
642     (165, 162, 45),
643     (165, 165, 46),
644     (166, 152, 43),
645     (166, 155, 44),
646     (166, 159, 45),
647     (166, 156, 46),
648     (167, 21, 39),
649     (168, 169, 45),
650     (168, 113, 46),
651     (169, 445552, 46, 42, 69),
652     (169, 168, 46),
653     (169, 170, 50, 29, 11),
654     (170, 171, 29, 50),
655     (170, 169, 30, 48),
656     (171, 170, 30, 48),
657     (171, 172, 29, 50),
658     (172, 171, 30, 48),
659     (172, 173, 29, 56),
660     (173, 172, 30),
661     (173, 146175, 29),
662     (173, 174, 29),
663     (174, 0, 1),
664     (175, 176, 1),
665     (176, 173, 56, 30),
666     (176, 177, 47, 17),
667     (177, 176, 49, 11, 17),
668     (178, 0, 1),
669     (179, 11, 1),
670     (180, 3, 1),
671     (181, 33, 1),
672     (182, 3, 1),
673     (183, 100, 1),
674     (184, 33, 1),
675     )
676
677 def destdecode(dest):
678     "Decode a destinatio nnumber"
679     if dest <= 300:
680         return '["move", %s]' % locnames[dest]
681     elif dest <= 500:
682         return '["goto", %s]' % (dest - 300)
683     else:
684         return '["speak", %s]' % (msgnames[dest - 500])
685
686 def genline(loc):
687     attrs = []
688     sys.stdout.write("    travel: {\n")
689     for t in section3:
690         t = list(t)
691         src = t.pop(0)
692         if src == loc:
693             dest = t.pop(0)
694             try:
695                 if t.index(1) == len(t) - 1:
696                     t.pop()
697                 else:
698                     sys.stderr.write("%s (%d): action value 1 in unexpected place\n" %\
699                                      (locnames[loc], loc))
700                     sys.exit(1)
701             except ValueError:
702                 pass
703             cond = dest // 1000
704             dest = dest % 1000
705             t = [verbs[e] for e in t]
706             sys.stdout.write("      %s %s %s,\n" % (destdecode(dest), cond, t))
707     sys.stdout.write("    }\n")
708         
709     
710
711 if __name__ == "__main__":
712     with open("adventure.yaml", "r") as fp:
713         db = yaml.load(fp)
714         fp.seek(0)
715         locnames = [el[0] for el in db["locations"]]
716         msgnames = [el[0] for el in db["arbitrary_messages"]]
717         verbs = {}
718         for entry in db["vocabulary"]:
719             if entry["type"] == "motion" and entry["value"] not in verbs:
720                 verbs[entry["value"]] = entry["word"]
721         ln = -1
722         while True:
723             line = fp.readline()
724             if not line:
725                 break
726             if line.startswith("- LOC"):
727                 if ln > -1:
728                     genline(ln)
729                 ln += 1
730             sys.stdout.write(line)
731
732 # end