| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
"""Parse the original PDP ``advent.dat`` file. |
| 4 |
|
| 5 |
Copyright 2010-2015 Brandon Rhodes. Licensed as free software under the |
| 6 |
Apache License, Version 2.0 as detailed in the accompanying README.txt. |
| 7 |
|
| 8 |
""" |
| 9 |
from operator import attrgetter |
| 10 |
from .model import Hint, Message, Move, Object, Room, Word |
| 11 |
|
| 12 |
# The Adventure data file knows only the first five characters of each |
| 13 |
# word in the game, so we have to know the full verion of each word. |
| 14 |
|
| 15 |
long_words = { w[:5]: w for w in """upstream downstream forest |
| 16 |
forward continue onward return retreat valley staircase outside building stream |
| 17 |
cobble inward inside surface nowhere passage tunnel canyon awkward |
| 18 |
upward ascend downward descend outdoors barren across debris broken |
| 19 |
examine describe slabroom depression entrance secret bedquilt plover |
| 20 |
oriental cavern reservoir office headlamp lantern pillow velvet fissure tablet |
| 21 |
oyster magazine spelunker dwarves knives rations bottle mirror beanstalk |
| 22 |
stalactite shadow figure drawings pirate dragon message volcano geyser |
| 23 |
machine vending batteries carpet nuggets diamonds silver jewelry treasure |
| 24 |
trident shards pottery emerald platinum pyramid pearl persian spices capture |
| 25 |
release discard mumble unlock nothing extinguish placate travel proceed |
| 26 |
continue explore follow attack strike devour inventory detonate ignite |
| 27 |
blowup peruse shatter disturb suspend sesame opensesame abracadabra |
| 28 |
shazam excavate information""".split() } |
| 29 |
|
| 30 |
class Data(object): |
| 31 |
def __init__(self): |
| 32 |
self.rooms = {} |
| 33 |
self.vocabulary = {} |
| 34 |
self.objects = {} |
| 35 |
self.messages = {} |
| 36 |
self.class_messages = [] |
| 37 |
self.hints = {} |
| 38 |
self.magic_messages = {} |
| 39 |
|
| 40 |
def referent(self, word): |
| 41 |
if word.kind == 'noun': |
| 42 |
return self.objects[word.n % 1000] |
| 43 |
|
| 44 |
# Helper functions. |
| 45 |
|
| 46 |
def make_object(dictionary, klass, n): |
| 47 |
if n not in dictionary: |
| 48 |
dictionary[n] = obj = klass() |
| 49 |
obj.n = n |
| 50 |
return dictionary[n] |
| 51 |
|
| 52 |
def expand_tabs(segments): |
| 53 |
it = iter(segments) |
| 54 |
line = next(it) |
| 55 |
for segment in it: |
| 56 |
spaces = 8 - len(line) % 8 |
| 57 |
line += ' ' * spaces + segment |
| 58 |
return line |
| 59 |
|
| 60 |
def accumulate_message(dictionary, n, line): |
| 61 |
dictionary[n] = dictionary.get(n, '') + line + '\n' |
| 62 |
|
| 63 |
# Knowledge of what each section contains. |
| 64 |
|
| 65 |
def section1(data, n, *etc): |
| 66 |
"""Handle record from “Section 1: long form descriptions”. |
| 67 |
|
| 68 |
Section 1: long form descriptions. Each line contains a location |
| 69 |
number, a TAB, and a line of text. The set of (necessarily adjacent) |
| 70 |
lines whose numbers are X form the long description of location X. |
| 71 |
|
| 72 |
""" |
| 73 |
room = make_object(data.rooms, Room, n) |
| 74 |
if not etc[0].startswith('>$<'): |
| 75 |
room.long_description += expand_tabs(etc) + '\n' |
| 76 |
|
| 77 |
def section2(data, n, line): |
| 78 |
"""Handle record from “Section 2: short form descriptions”. |
| 79 |
|
| 80 |
Section 2: short form descriptions. Same format as long form. Not |
| 81 |
all places have short descriptions. |
| 82 |
|
| 83 |
""" |
| 84 |
make_object(data.rooms, Room, n).short_description += line + '\n' |
| 85 |
|
| 86 |
def section3(data, x, y, *verbs): |
| 87 |
"""Handle record from “Section 3: travel table”. |
| 88 |
|
| 89 |
Section 3: travel table. Each line contains a location number (X), a |
| 90 |
second location number (Y), and a list of motion numbers (see |
| 91 |
section 4). |
| 92 |
|
| 93 |
Each motion represents a verb which will go to Y if currently at |
| 94 |
X. Y, in turn, is interpreted as follows. Let M=Y/1000, N=Y MOD |
| 95 |
1000. |
| 96 |
|
| 97 |
If N<=300: it is the location to go to. |
| 98 |
|
| 99 |
If 300<N<=500: N-300 is used in a computed GOTO to a |
| 100 |
section of special code. |
| 101 |
|
| 102 |
If N>500: message N-500 from section 6 is printed, and he |
| 103 |
stays wherever he is. |
| 104 |
|
| 105 |
Meanwhile, M specifies the conditions on the motion. |
| 106 |
|
| 107 |
If M=0: it's unconditional. |
| 108 |
If 0<M<100: it is done with M% probability. |
| 109 |
If M=100: unconditional, but forbidden to dwarves. |
| 110 |
If 100<M<=200: he must be carrying object M-100. |
| 111 |
If 200<M<=300: must be carrying or in same room as M-200. |
| 112 |
If 300<M<=400: PROP(M MOD 100) must *not* be 0. |
| 113 |
If 400<M<=500: PROP(M MOD 100) must *not* be 1. |
| 114 |
If 500<M<=600: PROP(M MOD 100) must *not* be 2, etc. |
| 115 |
|
| 116 |
If the condition (if any) is not met, then the next *different* |
| 117 |
"destination" value is used (unless it fails to meet *its* |
| 118 |
conditions, in which case the next is found, etc.). Typically, the |
| 119 |
next dest will be for one of the same verbs, so that its only use is |
| 120 |
as the alternate destination for those verbs. For instance: |
| 121 |
|
| 122 |
15 110022 29 31 34 35 23 43 |
| 123 |
15 14 29 |
| 124 |
|
| 125 |
This says that, from LOC 15, any of the verbs 29, 31, etc., will |
| 126 |
take him to 22 if he's carrying object 10, and otherwise will go to |
| 127 |
14. |
| 128 |
|
| 129 |
11 303008 49 |
| 130 |
11 9 50 |
| 131 |
|
| 132 |
This says that, from 11, 49 takes him to 8 unless PROP(3)=0, in |
| 133 |
which case he goes to 9. Verb 50 takes him to 9 regardless of |
| 134 |
PROP(3). |
| 135 |
|
| 136 |
""" |
| 137 |
last_travel = data._last_travel |
| 138 |
if last_travel[0] == x and last_travel[1][0] == verbs[0]: |
| 139 |
verbs = last_travel[1] # same first verb implies use whole list |
| 140 |
else: |
| 141 |
data._last_travel = [x, verbs] |
| 142 |
|
| 143 |
m, n = divmod(y, 1000) |
| 144 |
mh, mm = divmod(m, 100) |
| 145 |
|
| 146 |
if m == 0: |
| 147 |
condition = (None,) |
| 148 |
elif 0 < m < 100: |
| 149 |
condition = ('%', m) |
| 150 |
elif m == 100: |
| 151 |
condition = ('not_dwarf',) |
| 152 |
elif 100 < m <= 200: |
| 153 |
condition = ('carrying', mm) |
| 154 |
elif 200 < m <= 300: |
| 155 |
condition = ('carrying_or_in_room_with', mm) |
| 156 |
elif 300 < m: |
| 157 |
condition = ('prop!=', mm, mh - 3) |
| 158 |
|
| 159 |
if n <= 300: |
| 160 |
action = make_object(data.rooms, Room, n) |
| 161 |
elif 300 < n <= 500: |
| 162 |
action = n # special computed goto |
| 163 |
else: |
| 164 |
action = make_object(data.messages, Message, n - 500) |
| 165 |
|
| 166 |
move = Move() |
| 167 |
if len(verbs) == 1 and verbs[0] == 1: |
| 168 |
move.is_forced = True |
| 169 |
else: |
| 170 |
move.verbs = [ make_object(data.vocabulary, Word, verb_n) |
| 171 |
for verb_n in verbs if verb_n < 100 ] # skip bad "109" |
| 172 |
move.condition = condition |
| 173 |
move.action = action |
| 174 |
data.rooms[x].travel_table.append(move) |
| 175 |
|
| 176 |
def section4(data, n, text, *etc): |
| 177 |
"""Handle record from “Section 4: vocabulary”. |
| 178 |
|
| 179 |
Section 4: vocabulary. Each line contains a number (N), a TAB, and a |
| 180 |
five-letter word. Call M=N/1000. If M=0, then the word is a motion |
| 181 |
verb for use in travelling (see section 3). Else, if M=1, the word |
| 182 |
is an object. Else, if M=2, the word is an action verb (such as |
| 183 |
"CARRY" or "ATTACK"). Else, if M=3, the word is a special case verb |
| 184 |
(such as "DIG") and N MOD 1000 is an index into section 6. Objects |
| 185 |
from 50 to (currently, anyway) 79 are considered treasures (for |
| 186 |
pirate, closeout). |
| 187 |
|
| 188 |
""" |
| 189 |
text = text.lower() |
| 190 |
text = long_words.get(text, text) |
| 191 |
word = make_object(data.vocabulary, Word, n) |
| 192 |
if word.text is None: # this is the first word with index "n" |
| 193 |
word.text = text |
| 194 |
else: # there is already a word sitting at "n", so create a synonym |
| 195 |
original = word |
| 196 |
word = Word() |
| 197 |
word.n = n |
| 198 |
word.text = text |
| 199 |
original.add_synonym(word) |
| 200 |
word.kind = ['travel', 'noun', 'verb', 'snappy_comeback'][n // 1000] |
| 201 |
if word.kind == 'noun': |
| 202 |
n %= 1000 |
| 203 |
obj = make_object(data.objects, Object, n) |
| 204 |
obj.names.append(text) |
| 205 |
obj.is_treasure = (n >= 50) |
| 206 |
data.objects[text] = obj |
| 207 |
if text not in data.vocabulary: # since duplicate names exist |
| 208 |
data.vocabulary[text] = word |
| 209 |
|
| 210 |
def section5(data, n, *etc): |
| 211 |
"""Handle record from “Section 5: object descriptions”. |
| 212 |
|
| 213 |
Section 5: object descriptions. Each line contains a number (N), a |
| 214 |
TAB, and a message. If N is from 1 to 100, the message is the |
| 215 |
"inventory" message for object N. Otherwise, N should be 000, 100, |
| 216 |
200, etc., and the message should be the description of the |
| 217 |
preceding object when its PROP value is N/100. The N/100 is used |
| 218 |
only to distinguish multiple messages from multi-line messages; the |
| 219 |
PROP info actually requires all messages for an object to be present |
| 220 |
and consecutive. Properties which produce no message should be |
| 221 |
given the message ">$<". |
| 222 |
|
| 223 |
""" |
| 224 |
if 1 <= n <= 99: |
| 225 |
data._object = make_object(data.objects, Object, n) |
| 226 |
data._object.inventory_message = expand_tabs(etc) |
| 227 |
else: |
| 228 |
n //= 100 |
| 229 |
messages = data._object.messages |
| 230 |
if etc[0].startswith('>$<'): |
| 231 |
more = '' |
| 232 |
else: |
| 233 |
more = expand_tabs(etc) + '\n' |
| 234 |
messages[n] = messages.get(n, '') + more |
| 235 |
|
| 236 |
def section6(data, n, *etc): |
| 237 |
"""Handle record from “Section 6: arbitrary messages”. |
| 238 |
|
| 239 |
Section 6: arbitrary messages. Same format as sections 1, 2, and 5, |
| 240 |
except the numbers bear no relation to anything (except for special |
| 241 |
verbs in section 4). |
| 242 |
|
| 243 |
""" |
| 244 |
message = make_object(data.messages, Message, n) |
| 245 |
message.text += expand_tabs(etc) + '\n' |
| 246 |
|
| 247 |
def section7(data, n, room_n, fixed=None): |
| 248 |
"""Handle record from “Section 7: object locations”. |
| 249 |
|
| 250 |
Section 7: object locations. Each line contains an object number and |
| 251 |
its initial location (zero (or omitted) if none). If the object is |
| 252 |
immovable, the location is followed by a "-1". If it has two |
| 253 |
locations (e.g. the grate) the first location is followed with the |
| 254 |
second, and the object is assumed to be immovable. |
| 255 |
|
| 256 |
""" |
| 257 |
obj = make_object(data.objects, Object, n) |
| 258 |
if room_n: |
| 259 |
room = make_object(data.rooms, Room, room_n) |
| 260 |
obj.drop(room) |
| 261 |
if fixed is not None: |
| 262 |
if fixed == -1: |
| 263 |
obj.is_fixed = True |
| 264 |
else: |
| 265 |
room2 = make_object(data.rooms, Room, fixed) |
| 266 |
obj.rooms.append(room2) # exists two places, like grate |
| 267 |
obj.starting_rooms = list(obj.rooms) # remember where things started |
| 268 |
|
| 269 |
def section8(data, word_n, message_n): |
| 270 |
"""Handle record from “Section 8: action defaults”. |
| 271 |
|
| 272 |
Section 8: action defaults. Each line contains an "action-verb" |
| 273 |
number and the index (in section 6) of the default message for the |
| 274 |
verb. |
| 275 |
|
| 276 |
""" |
| 277 |
if not message_n: |
| 278 |
return |
| 279 |
word = make_object(data.vocabulary, Word, word_n + 2000) |
| 280 |
message = make_object(data.messages, Message, message_n) |
| 281 |
for word2 in word.synonyms: |
| 282 |
word2.default_message = message |
| 283 |
|
| 284 |
def section9(data, bit, *nlist): |
| 285 |
"""Handle record from “Section 9: liquid assets, etc.”. |
| 286 |
|
| 287 |
Section 9: liquid assets, etc. Each line contains a number (N) and |
| 288 |
up to 20 location numbers. Bit N (where 0 is the units bit) is set |
| 289 |
in COND(LOC) for each LOC given. The COND bits currently assigned |
| 290 |
are: |
| 291 |
|
| 292 |
0: light |
| 293 |
1: if bit 2 is on: on for oil, off for water |
| 294 |
2: liquid asset, see bit 1 |
| 295 |
3: pirate doesn't go here unless following player |
| 296 |
|
| 297 |
Other bits are used to indicate areas of interest to "hint" |
| 298 |
routines: |
| 299 |
|
| 300 |
4: trying to get into cave |
| 301 |
5: trying to catch bird |
| 302 |
6: trying to deal with snake |
| 303 |
7: lost in maze |
| 304 |
8: pondering dark room |
| 305 |
9: at Witt's End |
| 306 |
|
| 307 |
COND(LOC) is set to 2, overriding all other bits, if LOC has forced |
| 308 |
motion. |
| 309 |
|
| 310 |
""" |
| 311 |
for n in nlist: |
| 312 |
room = make_object(data.rooms, Room, n) |
| 313 |
if bit == 0: |
| 314 |
room.is_light = True |
| 315 |
elif bit == 1: |
| 316 |
room.liquid = make_object(data.objects, Object, 22) #oil |
| 317 |
elif bit == 2: |
| 318 |
room.liquid = make_object(data.objects, Object, 21) #water |
| 319 |
elif bit == 3: |
| 320 |
room.is_forbidden_to_pirate = True |
| 321 |
else: |
| 322 |
hint = make_object(data.hints, Hint, bit) |
| 323 |
hint.rooms.append(room) |
| 324 |
|
| 325 |
def section10(data, score, line, *etc): |
| 326 |
"""Handle record from “Section 10: class messages”. |
| 327 |
|
| 328 |
Section 10: class messages. Each line contains a number (N), a TAB, |
| 329 |
and a message describing a classification of player. The scoring |
| 330 |
section selects the appropriate message, where each message is |
| 331 |
considered to apply to players whose scores are higher than the |
| 332 |
previous N but not higher than this N. Note that these scores |
| 333 |
probably change with every modification (and particularly expansion) |
| 334 |
of the program. |
| 335 |
|
| 336 |
""" |
| 337 |
data.class_messages.append((score, line)) |
| 338 |
|
| 339 |
def section11(data, n, turns_needed, penalty, question_n, message_n): |
| 340 |
"""Handle record from “Section 11: hints”. |
| 341 |
|
| 342 |
Section 11: hints. Each line contains a hint number (corresponding |
| 343 |
to a COND bit, see section 9), the number of turns he must be at the |
| 344 |
right LOC(s) before triggering the hint, the points deducted for |
| 345 |
taking the hint, the message number (section 6) of the question, and |
| 346 |
the message number of the hint. These values are stashed in the |
| 347 |
"HINTS" array. |
| 348 |
|
| 349 |
HNTMAX is set to the max hint number (<= HNTSIZ). Numbers 1-3 are |
| 350 |
unusable since COND bits are otherwise assigned, so 2 is used to |
| 351 |
remember if he's read the clue in the repository, and 3 is used to |
| 352 |
remember whether he asked for instructions (gets more turns, but |
| 353 |
loses points). |
| 354 |
|
| 355 |
""" |
| 356 |
hint = make_object(data.hints, Hint, n) |
| 357 |
hint.turns_needed = turns_needed |
| 358 |
hint.penalty = penalty |
| 359 |
hint.question = make_object(data.messages, Message, question_n) |
| 360 |
hint.message = make_object(data.messages, Message, message_n) |
| 361 |
|
| 362 |
def section12(data, n, line): |
| 363 |
"""Handle record from “Section 12: magic messages”. |
| 364 |
|
| 365 |
Section 12: magic messages. Identical to section 6 except put in a |
| 366 |
separate section for easier reference. Magic messages are used by |
| 367 |
the startup, maintenance mode, and related routines. |
| 368 |
|
| 369 |
""" |
| 370 |
accumulate_message(data.magic_messages, n, line) |
| 371 |
|
| 372 |
# Process every section of the file in turn. |
| 373 |
|
| 374 |
def parse(data, datafile): |
| 375 |
"""Read the Adventure data file and return a ``Data`` object.""" |
| 376 |
data._last_travel = [0, [0]] # x and verbs used by section 3 |
| 377 |
|
| 378 |
while True: |
| 379 |
section_number = int(datafile.readline()) |
| 380 |
if not section_number: # no further sections |
| 381 |
break |
| 382 |
store = globals().get('section%d' % section_number) |
| 383 |
while True: |
| 384 |
fields = [ (int(field) if field.lstrip('-').isdigit() else field) |
| 385 |
for field in datafile.readline().strip().split('\t') ] |
| 386 |
if fields[0] == -1: # end-of-section marker |
| 387 |
break |
| 388 |
store(data, *fields) |
| 389 |
|
| 390 |
del data._last_travel # state used by section 3 |
| 391 |
del data._object # state used by section 5 |
| 392 |
|
| 393 |
data.object_list = sorted(set(data.objects.values()), key=attrgetter('n')) |
| 394 |
#data.room_list = sorted(set(data.rooms.values()), key=attrgetter('n')) |
| 395 |
for obj in data.object_list: |
| 396 |
name = obj.names[0] |
| 397 |
if hasattr(data, name): |
| 398 |
name = name + '2' # create identifiers like ROD2, PLANT2 |
| 399 |
setattr(data, name, obj) |
| 400 |
|
| 401 |
return data |