comparison LightTools/build.py @ 51:efd2b1ca5b77

Clean up gui
author koryspansel <koryspansel@bendbroadband.com>
date Tue, 27 Sep 2011 09:42:01 -0700
parents a379bce1aeb1
children 2444937929ae
comparison
equal deleted inserted replaced
50:7ff46a00bcd3 51:efd2b1ca5b77
5 import sys 5 import sys
6 import string 6 import string
7 import struct 7 import struct
8 import os 8 import os
9 import functools 9 import functools
10 import re
11
12 _pattern = re.compile('\(\s*(\d+)\s*,\s*(\d+)\s*\)')
10 13
11 class MapDefinition: 14 class MapDefinition:
12 Parse = functools.partial(string.split, sep=',') 15 Parse = functools.partial(string.split, sep=',')
13 16
14 def __init__(self, size, position, direction, fcount, functions, grid): 17 def __init__(self, size, position, direction, grid):
15 self.Size = self._ParseVector(size) 18 self.Size = self._ParseVector(size)
16 self.Position = self._ParseVector(position) 19 self.Position = self._ParseVector(position)
17 self.Direction = self._ParseVector(direction) 20 self.Direction = self._ParseVector(direction)
18 self.Count = self._ParseVector(fcount)
19 self.Functions = self._ParseList(functions)
20 self.Grid = self._ParseList(grid) 21 self.Grid = self._ParseList(grid)
21 22
22 def Write(self, filename): 23 def Write(self, filename):
23 with open(filename, 'wb') as handle: 24 with open(filename, 'wb') as handle:
24 handle.write(struct.pack('i' * len(self.Size), *self.Size)) # 8 25 handle.write(struct.pack('i' * len(self.Size), *self.Size)) # 8
25 handle.write(struct.pack('i' * len(self.Position), *self.Position)) # 8 26 handle.write(struct.pack('i' * len(self.Position), *self.Position)) # 8
26 handle.write(struct.pack('i' * len(self.Direction), *self.Direction)) # 4 27 handle.write(struct.pack('i' * len(self.Direction), *self.Direction)) # 4
27 handle.write(struct.pack('i' * len(self.Count), *self.Count)) # 4
28
29 for function in self.Functions:
30 handle.write(struct.pack('i' * len(function), *function)) # 4 * Count
31 28
32 for tower in self.Grid: 29 for tower in self.Grid:
33 handle.write(struct.pack('i' * len(tower), *tower)) # 8 * Size.X * Size.Y 30 handle.write(struct.pack('i' * len(tower), *tower)) # 8 * Size.X * Size.Y
34 31
35 def _ParseVector(self, line): 32 def _ParseVector(self, line):
36 return map(int, self.Parse(line)) 33 return map(int, self.Parse(line))
37 34
38 def _ParseList(self, lines): 35 def _ParseList(self, lines):
39 return [self._ParseVector(line) for line in lines] 36 #return [self._ParseVector(line) for line in lines]
37 tuples = []
38
39 #print ''.join(lines)
40 #print _pattern.split(''.join(lines))
41
42 for item in _pattern.finditer(''.join(lines)):
43 #print item.groups()
44 #tuples.append((int(item.group(1)), int(item.group(2))))
45 tuples.append(map(int, item.groups()))
46
47 #print tuples
48 return tuples
49 #print tuples
50
51 #return [map(int, item.groups()) for item in _pattern.finditer(''.join(lines))]
40 52
41 def ReadDefinition(filename): 53 def ReadDefinition(filename):
42 def FilterLines(lines): 54 def FilterLines(lines):
43 for line in map(string.strip, lines): 55 for line in map(string.strip, lines):
44 if line and not line.startswith('#'): 56 if line and not line.startswith('#'):
47 with open(filename, 'rt') as handle: 59 with open(filename, 'rt') as handle:
48 lines = handle.readlines() 60 lines = handle.readlines()
49 61
50 definition = tuple(FilterLines(lines)) 62 definition = tuple(FilterLines(lines))
51 63
52 offset = 0 64 size = definition[0]
53 size = definition[offset] 65 position = definition[1]
54 offset += 1 66 direction = definition[2]
55 position = definition[offset] 67 grid = definition[3:]
56 offset += 1
57 direction = definition[offset]
58 offset += 1
59 fcount = definition[offset]
60 offset += 1
61 length = int(fcount)
62 functions = definition[offset:offset+length]
63 offset += length
64 grid = definition[offset:]
65 68
66 return MapDefinition(size, position, direction, fcount, functions, grid) 69 return MapDefinition(size, position, direction, grid)
67 70
68 if __name__ == '__main__': 71 if __name__ == '__main__':
69 path_root = os.path.dirname(sys.argv[0]) 72 path_root = os.path.dirname(sys.argv[0])
70 path_assets = os.path.abspath(os.path.join(path_root, '..', 'Assets', 'Maps')) 73 path_assets = os.path.abspath(os.path.join(path_root, '..', 'Assets', 'Maps'))
71 path_build = os.path.abspath(os.path.join(path_root, '..', 'Data', 'Maps')) 74 path_build = os.path.abspath(os.path.join(path_root, '..', 'Data', 'Maps'))
72 75