Mercurial > LightClone
annotate LightTools/build.py @ 75:57c0ce406a68 tip
Add main menu
author | koryspansel <koryspansel@bendbroadband.com> |
---|---|
date | Tue, 18 Oct 2011 17:08:17 -0700 |
parents | 6d4437a24aeb |
children |
rev | line source |
---|---|
0 | 1 # |
46 | 2 # build |
0 | 3 # |
4 | |
5 import sys | |
6 import string | |
7 import struct | |
8 import os | |
9 import functools | |
51 | 10 import re |
11 | |
12 _pattern = re.compile('\(\s*(\d+)\s*,\s*(\d+)\s*\)') | |
0 | 13 |
14 class MapDefinition: | |
15 Parse = functools.partial(string.split, sep=',') | |
16 | |
62 | 17 def __init__(self, size, position, direction, distance, yaw, pitch, grid): |
0 | 18 self.Size = self._ParseVector(size) |
19 self.Position = self._ParseVector(position) | |
20 self.Direction = self._ParseVector(direction) | |
62 | 21 self.Distance = self._ParseVectorF(distance) |
22 self.Yaw = self._ParseVectorF(yaw) | |
23 self.Pitch = self._ParseVectorF(pitch) | |
0 | 24 self.Grid = self._ParseList(grid) |
25 | |
26 def Write(self, filename): | |
27 with open(filename, 'wb') as handle: | |
28 handle.write(struct.pack('i' * len(self.Size), *self.Size)) # 8 | |
29 handle.write(struct.pack('i' * len(self.Position), *self.Position)) # 8 | |
30 handle.write(struct.pack('i' * len(self.Direction), *self.Direction)) # 4 | |
62 | 31 handle.write(struct.pack('f' * len(self.Distance), *self.Distance)) # 4 |
32 handle.write(struct.pack('f' * len(self.Yaw), *self.Yaw)) # 4 | |
33 handle.write(struct.pack('f' * len(self.Pitch), *self.Pitch)) # 4 | |
0 | 34 |
35 for tower in self.Grid: | |
36 handle.write(struct.pack('i' * len(tower), *tower)) # 8 * Size.X * Size.Y | |
37 | |
38 def _ParseVector(self, line): | |
39 return map(int, self.Parse(line)) | |
40 | |
62 | 41 def _ParseVectorF(self, line): |
42 return map(float, self.Parse(line)) | |
43 | |
0 | 44 def _ParseList(self, lines): |
52
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
45 return [map(int, item.groups()) for item in _pattern.finditer(''.join(lines))] |
0 | 46 |
47 def ReadDefinition(filename): | |
48 def FilterLines(lines): | |
49 for line in map(string.strip, lines): | |
50 if line and not line.startswith('#'): | |
51 yield line.split('#')[0] | |
52 | |
53 with open(filename, 'rt') as handle: | |
54 lines = handle.readlines() | |
55 | |
56 definition = tuple(FilterLines(lines)) | |
57 | |
51 | 58 size = definition[0] |
59 position = definition[1] | |
60 direction = definition[2] | |
62 | 61 distance = definition[3] |
62 yaw = definition[4] | |
63 pitch = definition[5] | |
64 grid = definition[6:] | |
0 | 65 |
62 | 66 return MapDefinition(size, position, direction, distance, yaw, pitch, grid) |
51 | 67 |
52
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
68 def Compile(input_map, output_map): |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
69 if os.path.isfile(input_map): |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
70 definition = ReadDefinition(input_map) |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
71 if not definition: |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
72 print 'Failed to read definition "%s"' % input_map |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
73 |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
74 else: |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
75 folder = os.path.dirname(output_map) |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
76 |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
77 if not os.path.isdir(folder): |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
78 os.makedirs(folder) |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
79 |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
80 print 'Building %s from %s' % (os.path.basename(output_map), os.path.basename(input_map)) |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
81 definition.Write(output_map) |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
82 |
0 | 83 if __name__ == '__main__': |
45 | 84 path_root = os.path.dirname(sys.argv[0]) |
85 path_assets = os.path.abspath(os.path.join(path_root, '..', 'Assets', 'Maps')) | |
86 path_build = os.path.abspath(os.path.join(path_root, '..', 'Data', 'Maps')) | |
0 | 87 |
52
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
88 if len(sys.argv) > 1: |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
89 Compile(os.path.join(path_assets, sys.argv[1]), os.path.join(path_build, os.path.splitext(sys.argv[1])[0] + '.map')) |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
90 |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
91 else: |
45 | 92 |
52
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
93 if os.path.isdir(path_assets): |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
94 for filename in os.listdir(path_assets): |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
95 if not filename.endswith('.def'): |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
96 continue |
0 | 97 |
52
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
98 Compile(os.path.join(path_assets, filename), os.path.join(path_build, os.path.splitext(filename)[0] + '.map')) |