Unit Summary Page Generator

This script will generate the content for the Unit Summary Table page.

from pyquery import PyQuery as pq
import os

basePath = "/home/jonathan/programming/0ad_tech/binaries/data/mods/public/simulation/templates/"

def loadData(fileName, basePath, general, cost, attackRanged, attackMelee, armour):
	d = pq(filename=basePath + fileName)
	
	if d("Entity").attr("parent") != None:
		unitInfo = loadData(d("Entity").attr("parent")+".xml", basePath, general, cost, attackRanged, attackMelee, armour)
	else:
		unitInfo = {'general':{}, 'cost':[0,0,0,0,0], 'attackRanged':[0,0,0,0], 'attackMelee':[0,0,0], 'armour':[0,0,0]}
	
	for attr in general:
		if d(attr).text() != None:
			unitInfo['general'][attr] = d(attr).text()
	
	for i in range(len(cost)):
		if d(cost[i]).text() != None:
			unitInfo['cost'][i] = float(d(cost[i]).text())
	
	for i in range(len(armour)):
		if d(armour[i]).text() != None:
			unitInfo['armour'][i] = float(d(armour[i]).text())
	
	for i in range(len(attackRanged)):
		if d(attackRanged[i]).text() != None:
			unitInfo['attackRanged'][i] = float(d(attackRanged[i]).text())
	
	for i in range(len(attackMelee)):
		if d(attackMelee[i]).text() != None:
			unitInfo['attackMelee'][i] = float(d(attackMelee[i]).text())
	
	return unitInfo

def printUnit(fileName, basePath):
	general = ["GenericName", "SpecificName", "Vision > Range", "Health > Max", "WalkSpeed"]
	cost = ["Cost > * > food", "Cost > * > wood", "Cost > * > metal", "Cost > * > stone", "Cost > BuildTime"]
	attackRanged = ["Attack > Ranged > Pierce", "Attack > Ranged > Hack", "Attack > Ranged > Crush", "Attack > Ranged > MaxRange"]
	attackMelee = ["Attack > Melee > Pierce", "Attack > Melee > Hack", "Attack > Melee > Crush"]
	armour = ["Armour > Pierce", "Armour > Hack", "Armour > Crush"]
	generalTitles = ["Unit", "Specific Name", "Line of Sight", "Health"]
	costTitles = ["F", "W", "G", "S", "T"]
	aTitles = ["P", "H", "C"]
	
	unitStuff = loadData(fileName, basePath, general, cost, attackRanged, attackMelee, armour)
	
	if fileName.endswith("_b.xml"):
		unitStuff["general"]["GenericName"] += " (basic)"
	if fileName.endswith("_a.xml"):
		unitStuff["general"]["GenericName"] += " (advanced)"
	if fileName.endswith("_e.xml"):
		unitStuff["general"]["GenericName"] += " (elite)"

	outString = "|| "
	for t in general:
		if t in unitStuff["general"]:
			outString += unitStuff["general"][t] + " || "
		else:
			outString += " || "

	for i in range(len(unitStuff["cost"])):
		if unitStuff["cost"][i] != 0:
			outString += costTitles[i] + "{0:g} ".format(unitStuff["cost"][i])
	outString += "|| "
	
	if sum(unitStuff["attackRanged"]) > 0:
		for i in range(3):
			if unitStuff["attackRanged"][i] != 0:
				outString += aTitles[i] + "{0:g} ".format(unitStuff["attackRanged"][i])
		outString += "(Range {0:g}) ".format(unitStuff["attackRanged"][3])
	
	if sum(unitStuff["attackMelee"]) > 0:
		for i in range(3):
			if unitStuff["attackMelee"][i] != 0:
				outString += aTitles[i] + "{0:g} ".format(unitStuff["attackMelee"][i])
		outString += "(Melee) "
	outString += "|| "
	
	for i in range(3):
		if unitStuff["armour"][i] != 0:
			outString += aTitles[i] + "{0:g} ".format(unitStuff["armour"][i])

	outString += "||"

	return outString

header = "||'''Common Name'''||'''Specific Name'''||'''Line of Sight'''||'''Health'''||'''Speed'''||'''Cost'''||'''Attack'''||'''Armour'''||\n"

f = open('unit_summary_table', 'w')

f.write("= Unit Summary Table =\n\n")
f.write("A more detailed description of the units is available on the [wiki:Manual_Units units page].\n\n")
f.write("The following abbreviations are used in this page\n\n")
f.write("  F = [wiki:Manual_Terminology#Food food], W = [wiki:Manual_Terminology#Wood wood], M = [wiki:Manual_Terminology#Metal metal], S = stone, T = training time (seconds), H = Hack, P = Pierce, C = Crush\n\n")
f.write("The data is based on the basic form for all units that can be promoted.\n\n")

prefixes = ["hele", "spar", "athe", "mace", "rome", "cart", "pers", "maur", "theb", "gaul", "brit", "iber","viking"]
expanded = ["Hellene", "Spartan", "Athenian", "Macedonian", "Roman", "Cathaginian", "Persian", "Mauryan", "Theban", "Gaul", "British", "Iberian","Viking"]
prefixes.sort()
expanded.sort()

f.write("== Contents ==\n")
for i in range(len(prefixes)):
	f.write("* [#" + prefixes[i] + " " + expanded[i] + " Units ]\n")

f.write("\n")

lastCiv = "none"

os.chdir(basePath + "units")
fileList = os.listdir(".")
fileList.sort()

# Hack to order basic units before advanced
for i in range(len(fileList) - 1):
	if fileList[i].endswith("_a.xml"):
		if fileList[i+1].endswith("_b.xml"):
			tmp = fileList[i]
			fileList[i] = fileList[i+1]
			fileList[i+1] = tmp

for files in fileList:
	if files.endswith(".xml") and not files.endswith("packed.xml") and not files.endswith("unpacked.xml"):
		if not files.startswith(lastCiv):
			for prefix in prefixes:
				if files.startswith(prefix):
					i = prefixes.index(prefix)
					civName = expanded[i]
					f.write("== " + civName + " Units == #" + prefix + "\n")
					f.write(header)
					lastCiv = prefix
		f.write(printUnit("units/" + files, basePath).encode("utf-8")+"\n")


f.write("\n\nThis page was generated using this [wiki:Unit_Summary_Table_Generator unit summary table generator script]."+"\n")
Last modified 11 years ago Last modified on Jul 5, 2013, 1:44:25 PM
Note: See TracWiki for help on using the wiki.