-
Notifications
You must be signed in to change notification settings - Fork 4
/
nw_parser.py
135 lines (109 loc) · 4.47 KB
/
nw_parser.py
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
# Copyright (c) 2016, Aeva M. Palecek
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import re
import os
import sys
import string
from PIL import Image
from parser_common import LevelParser, UnknownFileHeader
BASE64 = string.ascii_uppercase + string.ascii_lowercase + string.digits + "+/"
class DotNWParser(LevelParser):
"""
This class takes a path to a .nw encoded file, decodes it, and
provides a means of easily accessing the contained data.
"""
def file_version(self):
if not self.header == "GLEVNW01":
raise UnknownFileHeader("Unknown header: %s" % self.header)
return 1
def parse(self, text_only = False):
with open(self._uri, "r") as reader:
raw_data = reader.read().replace("\r\n", "\n")
lines = raw_data.split("\n")
pattern = r'BOARD (\d+) (\d+) (\d+) (\d+) ([{0}]+)'.format(BASE64)
for line in lines:
match = re.match(pattern, line)
if match:
x_start, y_start, run, layer, data = match.groups()
y = int(y_start)
for i in range(int(run)*2)[::2]:
x = i/2
encoded = data[i:i+2]
self.board[x][y] = self.decode_tile(encoded)
if not text_only:
self.find_npcs(raw_data)
self.find_links(raw_data)
self.find_baddies(raw_data)
self.find_treasures(raw_data)
self.find_signs(raw_data)
def decode_tile(self, aa):
"""
This method is called by parse_tile, and does not need to be
called directly. This function takes a pair of characters
'XX' which represents a number in base64, and determines the
x,y coordinate encoded in that number.
The return value is a dictionary that describes the tile.
"""
lhs = BASE64.index(aa[0])*64
rhs = BASE64.index(aa[1])
di = lhs + rhs
tx = di % 16
ty = di / 16
bx = ty / 32 * 16 + tx
by = ty % 32
# tile = {
# "code" : aa,
# "index" : di,
# "__t_xy" : (tx, ty),
# "sprite" : (bx, by),
# }
return (bx, by)
def find_npcs(self, raw_data):
pattern = r'^NPC ([^\s]+) (-?\d+) (-?\d+)$(.+?)NPCEND$'
flags = re.DOTALL | re.MULTILINE
npcs = re.findall(pattern, raw_data, flags)
for npc in npcs:
x, y = int(npc[1]), int(npc[2])
img = npc[0] if npc[0] != '-' else None
src = npc[3].strip()
self.add_actor(x, y, img, src)
def find_links(self, raw_data):
pattern = r'^LINK (.+) (\d+) (\d+) (\d+) (\d+) ([^\s]+) ([^\s]+)$'
flags = re.MULTILINE
links = re.findall(pattern, raw_data, flags)
for link_params in links:
self.add_link(*link_params)
def find_baddies(self, raw_data):
pattern = r'^BADDY (-?\d+) (-?\d+) (\d+)\n([^\n]*)\n([^\n]*)\n([^\n]*)\nBADDYEND$'
flags = re.MULTILINE
baddies = re.findall(pattern, raw_data, flags)
for baddy in baddies:
x, y, kind = map(int, baddy[:3])
messages = baddy[3:]
self.add_baddy(x, y, kind, messages)
def find_treasures(self, raw_data):
pattern=r'^CHEST (-?\d+) (-?\d+) (.*) (-?\d+)$'
flags = re.MULTILINE
treasures = re.findall(pattern, raw_data, flags)
for treasure in treasures:
x, y = map(int, treasure[:2])
item = treasure[2]
sign = int(treasure[3])
self.add_treasure(x, y, item, sign)
def find_signs(self, raw_data):
pattern = r'^SIGN (-?\d+) (-?\d+)$(.+?)SIGNEND$'
flags = re.DOTALL | re.MULTILINE
signs = re.findall(pattern, raw_data, flags)
for sign in signs:
x, y = int(sign[0]), int(sign[1])
text = sign[2]
self.add_sign(x, y, text)