1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
#! /usr/bin/env python
# ____ _ __
# / __ )____ _____ | | / /___ ___________
# / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
# / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
# /_____/\____/____/ |__/|__/\__,_/_/ /____/
#
# A futuristic real-time strategy game.
# This file is part of Bos Wars.
#
# Script that generates all the distribution packages.
# (c) Copyright 2007 by Francois Beerten
#
# Bos Wars is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; only version 2 of the License.
#
# Bos Wars is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
import os
import sys
import csv
import re
importantkeys = ['Name',
'EnergyValue', 'MagmaValue',
'MaxEnergyUtilizationRate', 'MaxMagmaUtilizationRate',
'EnergyProductionRate', 'MagmaProductionRate',
'EnergyStorageCapacity', 'MagmaStorageCapacity',
'HitPoints', 'SightRange', 'Armor', 'BasicDamage',
'PiercingDamage', 'MaxAttackRange', 'MovementDelay',
'AttackDelay']
def findunits():
u = os.listdir('units')
u = [x for x in u if not x.startswith('.')]
return u
def findunitscripts(directory):
base = 'units/' + directory + '/'
s = os.listdir(base)
s = [base + x for x in s if x.endswith('.lua')]
return s
def findallscripts():
scripts = []
units = findunits()
for u in units:
scripts.extend(findunitscripts(u))
return scripts
def parseKvList(kvlist):
d = {}
key = kvlist[0]
for i in kvlist[1]:
if key:
d[key] = i
key = None
else:
key = i
return key
def replaceTabs(s):
return s.replace('\t', ' ' * 4)
class ParsedScript:
def __init__(self, path):
self.path = path
self.units = []
def regenerate(self, f):
f.write(self.head)
for unit in self.units:
unit.regenerate(f)
class ParsedUnit:
def __init__(self):
self.orderedkeys = []
self.stats = {}
def regenerateStats(self, f):
keys = self.stats.keys()
keys.sort()
for k in self.orderedkeys[:-1]:
f.write(' ' + k + ' = ' + self.stats[k] + ',\n')
k = self.orderedkeys[-1]
f.write(' ' + k + ' = ' + self.stats[k] + '\n')
def regenerate(self, f):
f.write('DefineUnitType("' + self.internalname + '", {\n')
self.regenerateStats(f)
f.write('})')
f.write(self.rest)
def writeCsv(self, statsfile):
statsfile.writerow(self.stats)
def stripComments(s):
s = s.split('\n')
new = []
for i in s:
i = i.split('--', 1)
new.append(i[0])
return '\n'.join(new)
def parseDefineUnitType(text):
unit = ParsedUnit()
body, rest = text.split('})', 1)
unit.rest = replaceTabs(rest)
body = stripComments(body)
internalname, stats = body.split(', {', 1)
unit.internalname = internalname.strip().strip('"')
stats = stats.split('=')
prevKey = stats[0].strip()
for x in stats[1:-1]:
x = x.split(',')
unit.stats[prevKey] = replaceTabs(','.join(x[:-1]).strip())
unit.orderedkeys.append(prevKey)
prevKey = x[-1].strip()
if len(stats) > 2:
unit.orderedkeys.append(prevKey)
unit.stats[prevKey] = replaceTabs(stats[-1].strip())
return unit
def parseWaits(text, section):
section += ' ='
if section not in text:
return 0
_, text = text.split(section, 1)
text,_ = text.split('}',1)
if 'unbreakable' in text:
_, text, _ = text.split('"unbreakable', 2)
m =re.findall(r'"wait \d+"', text)
waits = 0
for i in m:
v = int(i[6:-1])
waits += v
return waits
def parseScript(path):
parsedscript = ParsedScript(path)
s = file(path, 'rt').read()
elements = s.split('DefineUnitType(')
parsedscript.head = elements[0]
movement = parseWaits(s, 'Move')
attackwait = parseWaits(s, 'Attack')
for e in elements[1:]:
unit = parseDefineUnitType(e)
unit.stats['MovementDelay'] = movement
unit.stats['AttackDelay'] = attackwait
parsedscript.units.append(unit)
return parsedscript
def parseAllScripts():
units = []
scriptpaths = findallscripts()
scripts = []
for i in scriptpaths:
if 'crystal' not in i:
parsedscript = parseScript(i)
scripts.append(parsedscript)
units.extend(parsedscript.units)
return units, scripts
def generateStatsFile(units):
rawcsvfile = file('unitstats.csv', 'wb')
statsfile = csv.DictWriter(rawcsvfile, importantkeys, extrasaction='ignore',
delimiter=';', quotechar="'")
title = {}
for i in importantkeys:
title[i]=i
statsfile.writerow(title)
for unit in units:
unit.writeCsv(statsfile)
def regenerateScripts(scripts):
for i in scripts:
f = file(i.path, 'wt')
i.regenerate(f)
def readUnitStats():
rawcsvfile = file('unitstats.csv', 'rb')
stats = csv.DictReader(rawcsvfile, delimiter=';', quotechar="'")
newstats = {}
for r in stats:
newstats['"%s"' % r['Name']] = r
return newstats
def removeCommas(old):
return ''.join(old.split(','))
def updateUnitStats(units):
stats = readUnitStats()
for unit in units:
name = unit.stats['Name']
if stats.has_key(name):
up = stats[name]
for k in up.keys():
if unit.stats.has_key(k) and k != 'Name':
unit.stats[k] = removeCommas(up[k])
Usage = """
Unit stats generation tool.
Usage: %s <command>
Command:
csv
regenerate
update
When updating, the unitstats.csv file should use the semicolon (;) as
delimiter and single quote (') as string quote.
"""
def printUsage(args):
print Usage % args[0]
def main(args):
if len(args) == 1:
printUsage(args)
return
units, scripts = parseAllScripts()
if args[1] == 'csv':
generateStatsFile(units)
elif args[1] == 'regenerate':
regenerateScripts(scripts)
elif args[1] == 'update':
updateUnitStats(units)
regenerateScripts(scripts)
elif args[1] == 'tupdate':
updateUnitStats([units[0]])
regenerateScripts(scripts)
else:
printUsage(args)
return
if __name__ == '__main__':
main(sys.argv)
|