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
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
1 #
46
a379bce1aeb1 Rename map.py to build.py; Add release script
koryspansel
parents: 45
diff changeset
2 # build
0
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
51
efd2b1ca5b77 Clean up gui
koryspansel <koryspansel@bendbroadband.com>
parents: 46
diff changeset
10 import re
efd2b1ca5b77 Clean up gui
koryspansel <koryspansel@bendbroadband.com>
parents: 46
diff changeset
11
efd2b1ca5b77 Clean up gui
koryspansel <koryspansel@bendbroadband.com>
parents: 46
diff changeset
12 _pattern = re.compile('\(\s*(\d+)\s*,\s*(\d+)\s*\)')
0
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
13
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
14 class MapDefinition:
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
15 Parse = functools.partial(string.split, sep=',')
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
16
62
6d4437a24aeb Added camera position to level definition
koryspansel
parents: 52
diff changeset
17 def __init__(self, size, position, direction, distance, yaw, pitch, grid):
0
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
18 self.Size = self._ParseVector(size)
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
19 self.Position = self._ParseVector(position)
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
20 self.Direction = self._ParseVector(direction)
62
6d4437a24aeb Added camera position to level definition
koryspansel
parents: 52
diff changeset
21 self.Distance = self._ParseVectorF(distance)
6d4437a24aeb Added camera position to level definition
koryspansel
parents: 52
diff changeset
22 self.Yaw = self._ParseVectorF(yaw)
6d4437a24aeb Added camera position to level definition
koryspansel
parents: 52
diff changeset
23 self.Pitch = self._ParseVectorF(pitch)
0
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
24 self.Grid = self._ParseList(grid)
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
25
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
26 def Write(self, filename):
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
27 with open(filename, 'wb') as handle:
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
28 handle.write(struct.pack('i' * len(self.Size), *self.Size)) # 8
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
29 handle.write(struct.pack('i' * len(self.Position), *self.Position)) # 8
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
30 handle.write(struct.pack('i' * len(self.Direction), *self.Direction)) # 4
62
6d4437a24aeb Added camera position to level definition
koryspansel
parents: 52
diff changeset
31 handle.write(struct.pack('f' * len(self.Distance), *self.Distance)) # 4
6d4437a24aeb Added camera position to level definition
koryspansel
parents: 52
diff changeset
32 handle.write(struct.pack('f' * len(self.Yaw), *self.Yaw)) # 4
6d4437a24aeb Added camera position to level definition
koryspansel
parents: 52
diff changeset
33 handle.write(struct.pack('f' * len(self.Pitch), *self.Pitch)) # 4
0
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
34
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
35 for tower in self.Grid:
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
36 handle.write(struct.pack('i' * len(tower), *tower)) # 8 * Size.X * Size.Y
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
37
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
38 def _ParseVector(self, line):
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
39 return map(int, self.Parse(line))
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
40
62
6d4437a24aeb Added camera position to level definition
koryspansel
parents: 52
diff changeset
41 def _ParseVectorF(self, line):
6d4437a24aeb Added camera position to level definition
koryspansel
parents: 52
diff changeset
42 return map(float, self.Parse(line))
6d4437a24aeb Added camera position to level definition
koryspansel
parents: 52
diff changeset
43
0
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
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
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
46
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
47 def ReadDefinition(filename):
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
48 def FilterLines(lines):
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
49 for line in map(string.strip, lines):
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
50 if line and not line.startswith('#'):
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
51 yield line.split('#')[0]
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
52
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
53 with open(filename, 'rt') as handle:
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
54 lines = handle.readlines()
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
55
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
56 definition = tuple(FilterLines(lines))
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
57
51
efd2b1ca5b77 Clean up gui
koryspansel <koryspansel@bendbroadband.com>
parents: 46
diff changeset
58 size = definition[0]
efd2b1ca5b77 Clean up gui
koryspansel <koryspansel@bendbroadband.com>
parents: 46
diff changeset
59 position = definition[1]
efd2b1ca5b77 Clean up gui
koryspansel <koryspansel@bendbroadband.com>
parents: 46
diff changeset
60 direction = definition[2]
62
6d4437a24aeb Added camera position to level definition
koryspansel
parents: 52
diff changeset
61 distance = definition[3]
6d4437a24aeb Added camera position to level definition
koryspansel
parents: 52
diff changeset
62 yaw = definition[4]
6d4437a24aeb Added camera position to level definition
koryspansel
parents: 52
diff changeset
63 pitch = definition[5]
6d4437a24aeb Added camera position to level definition
koryspansel
parents: 52
diff changeset
64 grid = definition[6:]
0
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
65
62
6d4437a24aeb Added camera position to level definition
koryspansel
parents: 52
diff changeset
66 return MapDefinition(size, position, direction, distance, yaw, pitch, grid)
51
efd2b1ca5b77 Clean up gui
koryspansel <koryspansel@bendbroadband.com>
parents: 46
diff changeset
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
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
83 if __name__ == '__main__':
45
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
84 path_root = os.path.dirname(sys.argv[0])
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
85 path_assets = os.path.abspath(os.path.join(path_root, '..', 'Assets', 'Maps'))
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
86 path_build = os.path.abspath(os.path.join(path_root, '..', 'Data', 'Maps'))
0
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
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
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
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
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
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'))