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