0
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
1 #
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
2 # Map
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
3 #
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
4
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
5 import sys
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
6 import string
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
7 import struct
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
8 import os
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
9 import functools
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
10
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
11 class MapDefinition:
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
12 Parse = functools.partial(string.split, sep=',')
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
13
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
14 def __init__(self, size, position, direction, fcount, functions, grid):
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
15 self.Size = self._ParseVector(size)
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
16 self.Position = self._ParseVector(position)
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
17 self.Direction = self._ParseVector(direction)
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
18 self.Count = self._ParseVector(fcount)
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
19 self.Functions = self._ParseList(functions)
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
20 self.Grid = self._ParseList(grid)
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
21
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
22 def Write(self, filename):
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
23 with open(filename, 'wb') as handle:
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
24 handle.write(struct.pack('i' * len(self.Size), *self.Size)) # 8
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
25 handle.write(struct.pack('i' * len(self.Position), *self.Position)) # 8
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
26 handle.write(struct.pack('i' * len(self.Direction), *self.Direction)) # 4
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
27 handle.write(struct.pack('i' * len(self.Count), *self.Count)) # 4
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
28
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
29 for function in self.Functions:
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
30 handle.write(struct.pack('i' * len(function), *function)) # 4 * Count
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
31
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
32 for tower in self.Grid:
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
33 handle.write(struct.pack('i' * len(tower), *tower)) # 8 * Size.X * Size.Y
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
34
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
35 def _ParseVector(self, line):
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
36 return map(int, self.Parse(line))
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
37
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
38 def _ParseList(self, lines):
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
39 return [self._ParseVector(line) for line in lines]
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
40
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
41 def ReadDefinition(filename):
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
42 def FilterLines(lines):
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
43 for line in map(string.strip, lines):
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
44 if line and not line.startswith('#'):
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
45 yield line.split('#')[0]
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
46
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
47 with open(filename, 'rt') as handle:
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
48 lines = handle.readlines()
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
49
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
50 definition = tuple(FilterLines(lines))
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
51
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
52 offset = 0
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
53 size = definition[offset]
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
54 offset += 1
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
55 position = definition[offset]
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
56 offset += 1
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
57 direction = definition[offset]
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
58 offset += 1
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
59 fcount = definition[offset]
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
60 offset += 1
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
61 length = int(fcount)
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
62 functions = definition[offset:offset+length]
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
63 offset += length
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
64 grid = definition[offset:]
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
65
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
66 return MapDefinition(size, position, direction, fcount, functions, grid)
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
67
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
68 if __name__ == '__main__':
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
69 if len(sys.argv) < 2:
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
70 print 'Usage: Map <map> [output]'
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
71 sys.exit()
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
72
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
73 path_input = os.path.abspath(sys.argv[1])
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
74 path_output = os.path.splitext(path_input)[0] + '.map'
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
75
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
76 if len(sys.argv) > 2:
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
77 path_output = os.path.abspath(sys.argv[2])
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
78 if os.path.isdir(path_output):
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
79 path_output += os.path.splitext(os.path.basename(path_input))[0] + '.map'
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
80
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
81 definition = ReadDefinition(path_input)
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
82 if definition:
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
83 print 'Building %s from %s' % (os.path.basename(path_output), os.path.basename(path_input))
|
koryspansel <koryspansel@bendbroadband.com>
parents:
diff
changeset
|
84 definition.Write(path_output) |