annotate LightTools/Map.py @ 45:50def8c971d9

Move build functionality into map.py
author koryspansel
date Thu, 22 Sep 2011 13:41:29 -0700
parents 7e3a0ae9c016
children
rev   line source
0
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
1 #
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
2 # Map
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
3 #
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
4
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
5 import sys
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
6 import string
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
7 import struct
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
8 import os
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
9 import functools
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
10
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
11 class MapDefinition:
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
12 Parse = functools.partial(string.split, sep=',')
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
13
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
14 def __init__(self, size, position, direction, fcount, functions, grid):
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
15 self.Size = self._ParseVector(size)
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
16 self.Position = self._ParseVector(position)
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
17 self.Direction = self._ParseVector(direction)
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
18 self.Count = self._ParseVector(fcount)
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
19 self.Functions = self._ParseList(functions)
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
20 self.Grid = self._ParseList(grid)
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
21
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
22 def Write(self, filename):
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
23 with open(filename, 'wb') as handle:
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
24 handle.write(struct.pack('i' * len(self.Size), *self.Size)) # 8
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
25 handle.write(struct.pack('i' * len(self.Position), *self.Position)) # 8
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
26 handle.write(struct.pack('i' * len(self.Direction), *self.Direction)) # 4
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
27 handle.write(struct.pack('i' * len(self.Count), *self.Count)) # 4
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
28
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
29 for function in self.Functions:
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
30 handle.write(struct.pack('i' * len(function), *function)) # 4 * Count
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
31
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
32 for tower in self.Grid:
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
33 handle.write(struct.pack('i' * len(tower), *tower)) # 8 * Size.X * Size.Y
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
34
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
35 def _ParseVector(self, line):
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
36 return map(int, self.Parse(line))
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
37
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
38 def _ParseList(self, lines):
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
39 return [self._ParseVector(line) for line in lines]
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
40
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
41 def ReadDefinition(filename):
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
42 def FilterLines(lines):
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
43 for line in map(string.strip, lines):
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
44 if line and not line.startswith('#'):
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
45 yield line.split('#')[0]
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
46
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
47 with open(filename, 'rt') as handle:
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
48 lines = handle.readlines()
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
49
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
50 definition = tuple(FilterLines(lines))
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
51
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
52 offset = 0
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
53 size = definition[offset]
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
54 offset += 1
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
55 position = definition[offset]
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
56 offset += 1
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
57 direction = definition[offset]
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
58 offset += 1
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
59 fcount = definition[offset]
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
60 offset += 1
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
61 length = int(fcount)
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
62 functions = definition[offset:offset+length]
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
63 offset += length
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
64 grid = definition[offset:]
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
65
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
66 return MapDefinition(size, position, direction, fcount, functions, grid)
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
67
45
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
68 #if __name__ == '__main__':
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
69 # if len(sys.argv) < 2:
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
70 # print 'Usage: Map <map> [output]'
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
71 # sys.exit()
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
72 #
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
73 # path_input = os.path.abspath(sys.argv[1])
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
74 # path_output = os.path.splitext(path_input)[0] + '.map'
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
75 #
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
76 # if len(sys.argv) > 2:
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
77 # path_output = os.path.abspath(sys.argv[2])
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
78 # if os.path.isdir(path_output):
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
79 # path_output += os.path.splitext(os.path.basename(path_input))[0] + '.map'
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
80 #
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
81 # definition = ReadDefinition(path_input)
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
82 # if definition:
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
83 # print 'Building %s from %s' % (os.path.basename(path_output), os.path.basename(path_input))
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
84 # definition.Write(path_output)
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
85
0
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
86 if __name__ == '__main__':
45
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
87 path_root = os.path.dirname(sys.argv[0])
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
88 path_assets = os.path.abspath(os.path.join(path_root, '..', 'Assets', 'Maps'))
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
89 path_build = os.path.abspath(os.path.join(path_root, '..', 'Data', 'Maps'))
0
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
90
45
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
91 if os.path.isdir(path_assets):
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
92 for filename in os.listdir(path_assets):
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
93 if not filename.endswith('.def'):
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
94 continue
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
95
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
96 definition = ReadDefinition(os.path.join(path_assets, filename))
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
97 if not definition:
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
98 continue
0
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
99
45
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
100 output = os.path.join(path_build, os.path.splitext(filename)[0] + '.map')
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
101 folder = os.path.dirname(output)
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
102
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
103 if not os.path.isdir(folder):
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
104 os.makedirs(folder)
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
105
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
106 print 'Building %s from %s' % (os.path.basename(output), os.path.basename(filename))
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
107 definition.Write(output)