locbit.py: Can't set a FORCED attribute in the YAML...
[open-adventure.git] / locbit.py
1 #!/usr/bin/env python3
2 #
3 # Enhance adventure.yaml entries with explicit properties based on Section 9
4 # of adventure.text and the kludgy macro definitions in advent.h.
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 import sys, yaml
12
13 # This is the original location-attribute data from section 9 of adventure.text
14 # Bit indices are the first element of each tuple; the remaining numbers are
15 # indices of locations with that bit set.
16 section12 = (
17     (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
18     (0, 100, 115, 116, 126, 145, 146, 147, 148, 149, 150),
19     (0, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160),
20     (0, 161, 162, 163, 164, 165, 166, 167),
21     (1, 24),
22     (2, 1, 3, 4, 7, 38, 95, 113, 24, 168, 169),
23     (3, 46, 47, 48, 54, 56, 58, 82, 85, 86),
24     (3, 122, 123, 124, 125, 126, 127, 128, 129, 130),
25     (4, 6, 145, 146, 147, 148, 149, 150, 151, 152),
26     (4, 153, 154, 155, 156, 157, 158, 159, 160, 161),
27     (4, 162, 163, 164, 165, 166, 42, 43, 44, 45),
28     (4, 49, 50, 51, 52, 53, 55, 57, 80, 83),
29     (4, 84, 87, 107, 112, 131, 132, 133, 134, 135),
30     (4, 136, 137, 138, 139, 108),
31     (11, 8),
32     (12, 13),
33     (13, 19),
34     (14, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51),
35     (14, 52, 53, 54, 55, 56, 80, 81, 82, 86, 87),
36     (15, 99, 100, 101),
37     (16, 108),
38     (17, 6),
39     (18, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154),
40     (18, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164),
41     (18, 165, 166),
42     (19, 143),
43     (20, 8, 15, 64, 109, 126),
44 )
45
46 # Names for attribute bits
47 attrnames = (
48     "LIT",      # 0
49     "OILY",     # 1
50     "FLUID",    # 2
51     "NOARRR",   # 3
52     "NOBACK",   # 4
53     "",         # 5
54     "FOREST",   # 6     # New
55     "ABOVE",    # 7     # New
56     "DEEP",     # 8     # New
57     "",         # 9
58     "HBASE",    # 10
59     "HCAVE",    # 11
60     "HBIRD",    # 12
61     "HSNAKE",   # 13
62     "HMAZE",    # 14
63     "HDARK",    # 15
64     "HWITT",    # 16
65     "HCLIFF",   # 17
66     "HWOODS",   # 18
67     "HOGRE",    # 19
68     "HJADE",    # 20
69 )
70
71 nlocs = 184
72 grate = 8
73 misthall = 15
74 sapphireloc = 167
75
76 # For reference from advent.h:
77 #
78 # define FORCED(LOC)    (COND[LOC] == 2)
79 # define FOREST(LOC)    ((LOC) >= LOC_FOREST1 && (LOC) <= LOC_FOREST22)
80 #
81 #/*  The following two functions were added to fix a bug (game.clock1 decremented
82 # *  while in forest).  They should probably be replaced by using another
83 # *  "cond" bit.  For now, however, a quick fix...  OUTSID(LOC) is true if
84 # *  LOC is outside, INDEEP(LOC) is true if LOC is "deep" in the cave (hall
85 # *  of mists or deeper).  Note special kludges for "Foof!" locs. */
86 # define OUTSID(LOC)    ((LOC) <= LOC_GRATE || FOREST(LOC) || (LOC) == PLAC[SAPPH] || (LOC) == LOC_FOOF2 || (LOC) == LOC_FOOF4)
87 # define INDEEP(LOC)    ((LOC) >= LOC_MISTHALL && !OUTSID(LOC) && (LOC) != LOC_FOOF1)
88
89 def genline(loc):
90     attrs = []
91     name = locnames[loc]
92     for props in section12:
93         if loc in props[1:]:
94             if props[0] not in attrs:
95                 attrs.append(props[0])
96     # Adod new attributes.  These are computed the same way as the
97     # INDEEP(), OUTSID(), and FOREST() macros in advent.h.
98     if "FOREST" in name:
99         attrs.append(6) # FOREST
100     # 167 is the sapphire's start location
101     if loc in range(1, grate+1) or name in ("FOOF2", "FOOF4") or name == sapphireloc:
102         attrs.append(7) # ABOVE
103     if not loc in range(0, misthall+1) and name != "FOOF1" and 6 not in attrs:
104         attrs.append(8) # DEEP
105     names = str([attrnames[n] for n in attrs]).replace("'", "")
106     return "    conditions: %s\n" % (names,)
107
108 if __name__ == "__main__":
109     with open("adventure.yaml", "r") as fp:
110         db = yaml.load(fp)
111         fp.seek(0)
112         locnames = [el[0] for el in db["locations"]]
113         ln = -1
114         while True:
115             line = fp.readline()
116             if not line:
117                 break
118             if line.startswith("- LOC"):
119                 if ln > -1:
120                     sys.stdout.write(genline(ln))
121                 ln += 1
122             sys.stdout.write(line)
123
124 # end