-
Notifications
You must be signed in to change notification settings - Fork 2
/
cfg_parser.py
executable file
·69 lines (43 loc) · 1.26 KB
/
cfg_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
# import cPickle as pickle
import pickle
import tiles
def parse(data):
# turn tile into a transport tile (used for storage)
# duplicate map
m = [x[:] for x in data['map']]
# parse it
for x in xrange(data['width']-1, -1, -1):
for y in xrange(0, data['height']):
m[x][y] = tile_trans(m[x][y])
data['map'] = m
# return it, pickled
return pickle.dumps(data, -1)
# load map data
def load(world, s, data):
data = pickle.loads(data)
dmap = []
for x in xrange(data['width']-1, -1, -1):
# for x in xrange(0, data['width']):
dmap.append([])
for y in xrange(0, data['height']):
# for y in xrange(data['height']-1, -1, -1):
dmap[-1].append( data['map'][x][y].unpack(world, s) )
data['map'] = dmap
return data
# used to store a tile's values
class tile_trans(object):
def __init__(self, tile):
self.__dict__ = tile.__dict__.copy()
self._TYPE = tile.__class__.__name__
# delete problems
self.BLOCK = []
del self.parent
def unpack(self, world, s):
# repack the temp storage into an actual tile
# what tile we need to create
i = getattr(tiles, self._TYPE)
n = i(world, self.x, self.y, self.color, self.s)
n.__dict__ = self.__dict__.copy()
n.parent = world
n.s = s
return n