import xml.etree.ElementTree as ET import os import glob # What data to use AttackTypes = ["Hack","Pierce","Crush"] Resources = ["food", "wood", "stone", "metal"] #Generic templates to load Class = ["_champion",""] Types = ["infantry","cavalry"]; Subtypes = ["spearman","pikeman","swordsman", "archer","javelinist","slinger"]; Fix = ["melee_","ranged_",""] AddedTemplates = ["template_unit_champion_elephant_melee.xml"] # Those describe Civs to analyze and buildings to consider. # That way you can restrict to some buildings specifically. Civs = ["athen", "mace", "spart", "cart", "rome", "pers", "maur", "brit", "gaul", "iber"] #Civs = ["rome","pers"] CivBuildings = ["civil_centre", "barracks","gymnasion", "stables", "elephant_stables", "fortress", "embassy_celtic", "embassy_italiote", "embassy_iberian"] #CivBuildings = ["civil_centre", "barracks"] # Remote Civ templates with those strings in their name. FilterOut = ["hero", "mecha", "support", "barracks"] # Graphic parameters: affects only how the data is shown ComparativeSortByCav = True ComparativeSortByChamp = True SortTypes = ["Support","Pike","Spear","Sword", "Archer","Javelin","Sling","Elephant"]; # Classes ShowCivLists = False # Parameters: affects the data paramIncludePopCost = False paramFactorCounters = True; # False means attack bonuses are not counted. paramDPSImp = 5 paramHPImp = 3 # Decrease to make more important paramRessImp = { "food" : 1.0, "wood" : 1.0, "stone" : 2.0, "metal" : 1.5 } paramBuildTimeImp = 0.5 # Sane values are 0-1, though more is OK. paramSpeedImp = 0.2 # Sane values are 0-1, anything else will be weird. paramRangedCoverage = 1.0 # Sane values are 0-1, though more is OK. paramRangedMode = "Average" #Anything but "Average" is "Max" paramMicroAutoSpeed = 2.5 # Give a small advantage to ranged units faster than melee units (by at least this parameter). Making this too high will disable this advantage. This simulates the advantage a bit of micro would give paramMicroPerfectSpeed = 5.0 # Likewise, only this assumes perfect micro. Thus a ranged unit a lot faster is invincible. For example chariot v Elephant is a large win for elephants without this, but a large win for chariots with, since chariots can easily outmaneuver elephants if microed perfectly. # This is the path to the /templates/ folder to consider. Change this for mod support. basePath = os.path.realpath(__file__).replace("unitTables.py","") + "../../../binaries/data/mods/public/simulation/templates/" def htbout(file, balise, value): file.write("<" + balise + ">" + value + "\n" ) def htout(file, value): file.write("

" + value + "

\n" ) def CalcUnit(UnitName, existingUnit = None): unit = { 'HP' : "0", "BuildTime" : "0", "Cost" : { 'food' : "0", "wood" : "0", "stone" : "0", "metal" : "0", "population" : "0"}, 'Attack' : { "Melee" : { "Hack" : 0, "Pierce" : 0, "Crush" : 0 }, "Ranged" : { "Hack" : 0, "Pierce" : 0, "Crush" : 0 } }, 'RepeatRate' : {"Melee" : "0", "Ranged" : "0"},'PrepRate' : {"Melee" : "0", "Ranged" : "0"}, "Armour" : {}, "Ranged" : "false", "Classes" : [], "AttackBonuses" : {}, "Restricted" : [], "Civ" : None }; if (existingUnit != None): unit = existingUnit Template = ET.parse(UnitName) # Recursively get data from our parent which we'll override. if (Template.getroot().get("parent") != None): unit = CalcUnit(Template.getroot().get("parent") + ".xml", unit) unit["Parent"] = Template.getroot().get("parent") + ".xml" if (Template.find("./Identity/Civ") != None): unit['Civ'] = Template.find("./Identity/Civ").text if (Template.find("./Health/Max") != None): unit['HP'] = Template.find("./Health/Max").text if (Template.find("./Cost/BuildTime") != None): unit['BuildTime'] = Template.find("./Cost/BuildTime").text if (Template.find("./Cost/Resources") != None): for type in list(Template.find("./Cost/Resources")): unit['Cost'][type.tag] = type.text if (Template.find("./Attack/Melee") != None): if (Template.find("./Attack/Melee/RepeatTime") != None): unit['RepeatRate']["Melee"] = Template.find("./Attack/Melee/RepeatTime").text if (Template.find("./Attack/Melee/PrepareTime") != None): unit['PrepRate']["Melee"] = Template.find("./Attack/Melee/PrepareTime").text for atttype in AttackTypes: if (Template.find("./Attack/Melee/"+atttype) != None): unit['Attack']['Melee'][atttype] = Template.find("./Attack/Melee/"+atttype).text if (Template.find("./Attack/Melee/Bonuses") != None): for Bonus in Template.find("./Attack/Melee/Bonuses"): Against = [] CivAg = [] if (Bonus.find("Classes") != None and Bonus.find("Classes").text != None): Against = Bonus.find("Classes").text.split(" ") if (Bonus.find("Civ") != None and Bonus.find("Civ").text != None): CivAg = Bonus.find("Civ").text.split(" ") Val = float(Bonus.find("Multiplier").text) unit["AttackBonuses"][Bonus.tag] = {"Classes" : Against, "Civs" : CivAg, "Multiplier" : Val} if (Template.find("./Attack/Melee/RestrictedClasses") != None): newClasses = Template.find("./Attack/Melee/RestrictedClasses").text.split(" ") for elem in newClasses: if (elem.find("-") != -1): newClasses.pop(newClasses.index(elem)) if elem in unit["Restricted"]: unit["Restricted"].pop(newClasses.index(elem)) unit["Restricted"] += newClasses if (Template.find("./Attack/Ranged") != None): unit['Ranged'] = "true" if (Template.find("./Attack/Ranged/MaxRange") != None): unit['Range'] = Template.find("./Attack/Ranged/MaxRange").text if (Template.find("./Attack/Ranged/RepeatTime") != None): unit['RepeatRate']["Ranged"] = Template.find("./Attack/Ranged/RepeatTime").text if (Template.find("./Attack/Ranged/PrepareTime") != None): unit['PrepRate']["Ranged"] = Template.find("./Attack/Ranged/PrepareTime").text for atttype in AttackTypes: if (Template.find("./Attack/Ranged/"+atttype) != None): unit['Attack']['Ranged'][atttype] = Template.find("./Attack/Ranged/"+atttype).text if (Template.find("./Attack/Ranged/Bonuses") != None): for Bonus in Template.find("./Attack/Ranged/Bonuses"): Against = [] CivAg = [] if (Bonus.find("Classes") != None and Bonus.find("Classes").text != None): Against = Bonus.find("Classes").text.split(" ") if (Bonus.find("Civ") != None and Bonus.find("Civ").text != None): CivAg = Bonus.find("Civ").text.split(" ") Val = float(Bonus.find("Multiplier").text) unit["AttackBonuses"][Bonus.tag] = {"Classes" : Against, "Civs" : CivAg, "Multiplier" : Val} if (Template.find("./Attack/Melee/RestrictedClasses") != None): newClasses = Template.find("./Attack/Melee/RestrictedClasses").text.split(" ") for elem in newClasses: if (elem.find("-") != -1): newClasses.pop(newClasses.index(elem)) if elem in unit["Restricted"]: unit["Restricted"].pop(newClasses.index(elem)) unit["Restricted"] += newClasses if (Template.find("./Armour") != None): for atttype in AttackTypes: if (Template.find("./Armour/"+atttype) != None): unit['Armour'][atttype] = Template.find("./Armour/"+atttype).text if (Template.find("./UnitMotion") != None): if (Template.find("./UnitMotion/WalkSpeed") != None): unit['WalkSpeed'] = Template.find("./UnitMotion/WalkSpeed").text if (Template.find("./Identity/VisibleClasses") != None): newClasses = Template.find("./Identity/VisibleClasses").text.split(" ") for elem in newClasses: if (elem.find("-") != -1): newClasses.pop(newClasses.index(elem)) if elem in unit["Classes"]: unit["Classes"].pop(newClasses.index(elem)) unit["Classes"] += newClasses if (Template.find("./Identity/Classes") != None): newClasses = Template.find("./Identity/Classes").text.split(" ") for elem in newClasses: if (elem.find("-") != -1): newClasses.pop(newClasses.index(elem)) if elem in unit["Classes"]: unit["Classes"].pop(newClasses.index(elem)) unit["Classes"] += newClasses return unit def WriteUnit(Name, UnitDict): rstr = "" rstr += "" + Name + "\n" rstr += "" + UnitDict["HP"] + "\n" rstr += "" + UnitDict["BuildTime"] + "\n" rstr += "" + UnitDict["WalkSpeed"] + "\n" rstr += "" + UnitDict["Cost"]["food"] + "F / " + UnitDict["Cost"]["wood"] + "W / " + UnitDict["Cost"]["stone"] + "S / " + UnitDict["Cost"]["metal"] + "M\n" if UnitDict["Ranged"] == "True": rstr += "" + str(UnitDict["Attack"]["Ranged"]["Hack"]) + " / " + str(UnitDict["Attack"]["Ranged"]["Pierce"]) + " / " + str(UnitDict["Attack"]["Ranged"]["Crush"]) + "\n" else: rstr += "" + str(UnitDict["Attack"]["Melee"]["Hack"]) + " / " + str(UnitDict["Attack"]["Melee"]["Pierce"]) + " / " + str(UnitDict["Attack"]["Melee"]["Crush"]) + "\n" rstr += "" + str(UnitDict["Armour"]["Hack"]) + " / " + str(UnitDict["Armour"]["Pierce"]) + " / " + str(UnitDict["Armour"]["Crush"]) + "\n" rstr += "" for classe in UnitDict["Classes"]: rstr += classe + " " rstr += "" rstr += "" for Bonus in UnitDict["AttackBonuses"]: rstr += "[" for classe in UnitDict["AttackBonuses"][Bonus]["Classes"]: rstr += classe + " " rstr += ': ' + str(UnitDict["AttackBonuses"][Bonus]["Multiplier"]) + "] " rstr += "" rstr += "" for classe in UnitDict["Restricted"]: rstr += classe + " " rstr += "" return "" + rstr def CalcValue(UnitDict): Worth = {}; # Try to estimate how "worth it" a unit is. # We can't really differentiate between attack types and armor types and counters here, so values might fluctuate # But this can be used to quickly estimate if some units are too costly. Attack = "Melee" if (UnitDict["Ranged"] == "true"): Attack = "Ranged" DPS = 0 for att in AttackTypes: DPS += float(UnitDict['Attack'][Attack][att]) if (Attack == "Ranged"): DPS += float(UnitDict["Range"]) / 10 DPS /= (float(UnitDict['RepeatRate'][Attack])+float(UnitDict['PrepRate'][Attack])+1)/1000 Worth["Attack"] = DPS * paramDPSImp; if "Infantry" in UnitDict["Restricted"]: Worth["Attack"] /= 5.0; if "Cavalry" in UnitDict["Restricted"]: Worth["Attack"] /= 5.0; armorTotal = float(UnitDict['Armour']["Hack"]) + float(UnitDict['Armour']["Pierce"]) + float(UnitDict['Armour']["Crush"])/2.0 Worth["Defence"] = int(UnitDict['HP']) / (0.9**armorTotal)/paramHPImp; TotalCost = 0 for ress in paramRessImp: TotalCost += int(UnitDict['Cost'][ress]) * paramRessImp[ress] if (paramIncludePopCost): TotalCost *= (int(UnitDict['Cost']["population"])+1)/2.0 Worth["Cost"] = TotalCost/400.0; Worth["Cost"] = Worth["Cost"] * int(UnitDict['BuildTime'])/12.0 * paramBuildTimeImp + (1.0-paramBuildTimeImp) * Worth["Cost"]; # Speed makes you way less evasive, and you can chase less. # It's not really a cost so just decrease Attack and Defence Worth["Attack"] = Worth["Attack"] * float(UnitDict["WalkSpeed"])/10.0 * paramSpeedImp + (1.0-paramSpeedImp) * Worth["Attack"]; Worth["Defence"] = Worth["Defence"] * float(UnitDict["WalkSpeed"])/10.0 * paramSpeedImp + (1.0-paramSpeedImp) * Worth["Defence"]; Worth["Total"] = (Worth["Attack"] + Worth["Defence"])/Worth["Cost"] return Worth def canAttack(Attacker, Defender): ARestrictions = Attacker["Restricted"] BClasses = Defender["Classes"] for Classes in ARestrictions: if Classes in BClasses: return False return True def GetAttackBonus(Attacker, Defender): if not canAttack(Attacker, Defender): return 0.0 Abonuses = Attacker["AttackBonuses"] BClasses = Defender["Classes"] Bonus = 1.0; for BonusTypes in Abonuses: found = True for classe in Abonuses[BonusTypes]["Classes"]: if classe not in BClasses: found = False; for civi in Abonuses[BonusTypes]["Civs"]: if Defender["Civ"] not in civi: found = False; if found: Bonus *= Abonuses[BonusTypes]["Multiplier"] return Bonus; # Returns how much stronger unit A is compared to unit B in a direct fight. Uses counters, speed. Not perfect, but not widely inaccurate either. def Compare2(UnitDictA,UnitDictB): AWorth = 1.0; BWorth = 1.0; output = 1.0; # Get some DPS up in here. Use only melee or Ranged if available. AAttack = "Melee" if (UnitDictA["Ranged"] == "true"): AAttack = "Ranged" BAttack = "Melee" if (UnitDictB["Ranged"] == "true"): BAttack = "Ranged" ADPS = 0.0 BDPS = 0.0 # Exponential armor for att in AttackTypes: ADPS += float(UnitDictA['Attack'][AAttack][att]) * 0.9**float(UnitDictB['Armour'][att]) BDPS += float(UnitDictB['Attack'][BAttack][att]) * 0.9**float(UnitDictA['Armour'][att]) #Check if A is bonused against B and vice versa if (paramFactorCounters == True): ADPS *= GetAttackBonus(UnitDictA,UnitDictB) BDPS *= GetAttackBonus(UnitDictB,UnitDictA) if (AAttack == "Ranged"): ADPS += float(UnitDictA["Range"]) / float(UnitDictB["WalkSpeed"]) if (BAttack == "Ranged"): BDPS += float(UnitDictB["Range"]) / float(UnitDictA["WalkSpeed"]) ADPS /= (float(UnitDictA['RepeatRate'][AAttack])+float(UnitDictA['PrepRate'][AAttack])+1)/1000 BDPS /= (float(UnitDictB['RepeatRate'][BAttack])+float(UnitDictB['PrepRate'][BAttack])+1)/1000 #Ranged units can get a real advantage against slower enemy units. if AAttack == "Ranged" and BAttack != "Ranged" and float(UnitDictA["WalkSpeed"]) - paramMicroAutoSpeed > float(UnitDictB["WalkSpeed"]): ADPS += 20 elif BAttack == "Ranged" and AAttack != "Ranged" and float(UnitDictB["WalkSpeed"]) - paramMicroAutoSpeed > float(UnitDictA["WalkSpeed"]): BDPS += 20 if AAttack == "Ranged" and BAttack != "Ranged" and float(UnitDictA["WalkSpeed"]) - paramMicroPerfectSpeed > float(UnitDictB["WalkSpeed"]): ADPS += 5000 elif BAttack == "Ranged" and AAttack != "Ranged" and float(UnitDictB["WalkSpeed"]) - paramMicroPerfectSpeed > float(UnitDictA["WalkSpeed"]): BDPS += 5000 AWorth += int(UnitDictA['HP']) / (BDPS+1); BWorth += int(UnitDictB['HP']) / (ADPS+1); SpeedRatio = 1.0-paramSpeedImp + paramSpeedImp * float(UnitDictA["WalkSpeed"]) / float(UnitDictB["WalkSpeed"]) return AWorth / BWorth * SpeedRatio # Sort the templates dictionary. def SortFn(A): sortVal = 0 for classe in SortTypes: sortVal += 1 if classe in A[1]["Classes"]: break if (ComparativeSortByChamp == True and A[0].find("champion") == -1): sortVal -= 20 if (ComparativeSortByCav == True and A[0].find("cavalry") == -1): sortVal -= 10 if (A[1]["Civ"] != None): sortVal += 100 * Civs.index(A[1]["Civ"]) return sortVal # Output a matrix of each units against one another, where the square is greener if the unit is stronger, redder if weaker. def WriteComparisonTable(Units, OtherUnits = None): f.write("") if OtherUnits == None: OtherUnits = Units sortedDict = sorted(Units.items(), key=SortFn) sortedOtherDict = sorted(OtherUnits.items(), key=SortFn) # First column: write them all f.write ("") f.write("") for unitName in sortedOtherDict: f.write("") f.write("") for unitName in sortedOtherDict: f.write("") f.write("") # Write the comparisons for unitName in sortedDict: f.write("\n") for otherUnitName in sortedOtherDict: outcome = Compare2(unitName[1],otherUnitName[1]) green = max(0.0, min(4.0,outcome)) if (green > 1.0): f.write("") else: f.write("") f.write("") for otherUnitName in sortedOtherDict: outcome = GetAttackBonus(unitName[1],otherUnitName[1]) green = max(0.0, min(3.0,outcome)) if (green > 1.0): f.write("") else: f.write("") f.write("") f.write("
" + unitName[0] + "" + unitName[0] + "
" + unitName[0] + "
") def WriteWorthList(Units): def SortFn(A): return CalcValue(A[1])["Total"] sortedDict = sorted(Units.items(), key=SortFn, reverse=True) f.write("") for unitName in sortedDict: value = CalcValue(unitName[1]) valueAtt = int(2*value["Attack"]/value["Cost"]) valueDef = int(2*value["Defence"]/value["Cost"]) f.write("") f.write("") f.write("") f.write("
" + unitName[0] + ": " + str(valueAtt) + "" + str(valueDef) + "
") def CivUnitComparisons(CivA, CivB): averageEffNoCost = 0 MaxMinEffNoCost = 0 MinMaxEffNoCost = 999999 averageEff = 0 MaxMinEff = 0 MinMaxEff = 999999 MaxMinEffUnit = "" MinMaxEffUnit = "" for theirUnit in CivB["Units"]: unitAverageNoCost = 0 unitMaxNoCost = 0 unitAverage = 0 unitMax = 0 for myUnit in CivA["Units"]: eff = Compare2(CivA["Units"][myUnit],CivB["Units"][theirUnit]) eff = (eff * CivA["RangedUnitsWorth"] / CivB["RangedUnitsWorth"] * paramRangedCoverage) + eff * (1.0-paramRangedCoverage) unitAverageNoCost += eff if (eff > unitMaxNoCost): unitMaxNoCost = eff eff /= CalcValue(CivA["Units"][myUnit])["Cost"] / CalcValue(CivB["Units"][theirUnit])["Cost"] unitAverage += eff if (eff > unitMax): unitMax = eff unitAverageNoCost /= len(CivB["Units"]) unitAverage /= len(CivB["Units"]) averageEffNoCost += unitAverageNoCost averageEff += unitAverage if (unitMax < MinMaxEff): MinMaxEff = unitMax MinMaxEffUnit = theirUnit if (unitMaxNoCost < MinMaxEffNoCost): MinMaxEffNoCost = unitMaxNoCost for myUnit in CivA["Units"]: unitMinNoCost = 9999999 unitMin = 9999999 for theirUnit in CivB["Units"]: eff = Compare2(CivA["Units"][myUnit],CivB["Units"][theirUnit]) eff = (eff * CivA["RangedUnitsWorth"] / CivB["RangedUnitsWorth"] * paramRangedCoverage) + eff * (1.0-paramRangedCoverage) if (eff < unitMinNoCost): unitMinNoCost = eff eff /= CalcValue(CivA["Units"][myUnit])["Cost"] / CalcValue(CivB["Units"][theirUnit])["Cost"] if (eff < unitMin): unitMin = eff if (unitMin > MaxMinEff): MaxMinEff = unitMin MaxMinEffUnit = myUnit if (unitMinNoCost > MaxMinEffNoCost): MaxMinEffNoCost = unitMinNoCost f.write("" + oCiv + "") averageEffNoCost /= len(CivA["Units"]) averageEff /= len(CivA["Units"]) f.write("" + str("%.2f" % averageEffNoCost) + "") f.write("" + str("%.2f" % MaxMinEffNoCost) + "") f.write("" + str("%.2f" % MinMaxEffNoCost) + "") f.write("" + str("%.2f" % averageEff) + "") f.write("" + str("%.2f" % MaxMinEff) + "") f.write("" + str("%.2f" % MinMaxEff) + "") if MaxMinEff <= MinMaxEff and abs(MaxMinEff * MinMaxEff - 1.0) < 0.1: f.write("Surely") elif MaxMinEff <= MinMaxEff and abs(MaxMinEff * MinMaxEff - 1.0) < 0.2: f.write("Probably") elif MaxMinEff > MinMaxEff or MaxMinEff * MinMaxEff > 1.0: f.write("No, too strong") else: f.write("No, too weak") f.write("" + str(int(CivA["RangedUnitsWorth"] / CivB["RangedUnitsWorth"]*100)) + "%" + MaxMinEffUnit + "" + MinMaxEffUnit + "") f = open(os.path.realpath(__file__).replace("unitTables.py","") + 'unit_summary_table.html', 'w') f.write("\n\n\n Unit Tables\n \n\n") htbout(f,"h1","Unit Summary Table") f.write("\n") os.chdir(basePath) #Load values up. templates = {}; # Values ############################################################ #Whilst loading I also write them. htbout(f,"h2", "Units") f.write("") f.write("") for clss in Class: for tp in Types: for sbt in Subtypes: for x in Fix: template = "template_unit" + clss + "_" + tp + "_" + x + sbt + ".xml" if (os.path.isfile(template)): templates[template] = CalcUnit(template) f.write(WriteUnit(template, templates[template])) for Tmp in AddedTemplates: if (os.path.isfile(Tmp)): templates[Tmp] = CalcUnit(Tmp) f.write(WriteUnit(Tmp, templates[Tmp])) f.write("
HPBuildTimeSpeedCostsAttack (H/P/C)Armour(H/P/C)ClassesEfficient againstCannot Attack
") f.write("

Unit Comparison Table

\n") f.write("

The first table shows how strong a unit is against another one. Full green means 4 times as strong, Yellow means equal, Full red means infinitely worse.
The second table shows hardcoded attack counters. Full Green is 3.0. White is 1.0. Red is 0.0.

") if (not paramFactorCounters): f.write("

The left graph does not account for hardcoded counters here.

") WriteComparisonTable(templates) f.write("

Worthiness

\n") f.write("

This gives a representation of how efficient a unit is cost-wise.
Red bars are offensive capabilities, blue bars defensive capabilites.

") f.write("

Costs are estimated as the sum of Food*" + str(paramRessImp["food"]) + " / Wood*" + str(paramRessImp["wood"]) + " / Stone*" + str(paramRessImp["stone"]) + " / Metal*" + str(paramRessImp["metal"]) + "
") f.write("Population cost is " + ("counted" if paramIncludePopCost else "not counted") + " in the costs.
") WriteWorthList(templates) ############################################################ # Load Civ specific templates # Will be loaded by loading buildings, and templates will be set the Civ building, the Civ and in general. # Allows to avoid special units. CivData = {}; CivTemplates = {}; for Civ in Civs: if ShowCivLists: htbout(f,"h2", Civ) f.write("") CivData[Civ] = { "Units" : {}, "Buildings" : {}, "RangedUnits" : {} } for building in CivBuildings: CivData[Civ]["Buildings"][building] = {} bdTemplate = "./structures/" + Civ + "_" + building + ".xml" TrainableUnits = [] if (os.path.isfile(bdTemplate)): Template = ET.parse(bdTemplate) if (Template.find("./ProductionQueue/Entities") != None): TrainableUnits = Template.find("./ProductionQueue/Entities").text.replace(" ","").replace("\t","").split("\n") # We have the templates this building can train. for UnitFile in TrainableUnits: breakIt = False for filter in FilterOut: if UnitFile.find(filter) != -1: breakIt = True if breakIt: continue if (os.path.isfile(UnitFile + ".xml")): templates[UnitFile + ".xml"] = CalcUnit(UnitFile + ".xml") # Parse CivData[Civ]["Buildings"][building][UnitFile + ".xml"] = templates[UnitFile + ".xml"] if UnitFile + ".xml" not in CivData[Civ]["Units"]: CivTemplates[UnitFile + ".xml"] = templates[UnitFile + ".xml"] CivData[Civ]["Units"][UnitFile + ".xml"] = templates[UnitFile + ".xml"] if ShowCivLists: f.write(WriteUnit(UnitFile + ".xml", templates[UnitFile + ".xml"])) if "Ranged" in templates[UnitFile + ".xml"]["Classes"]: CivData[Civ]["RangedUnits"][UnitFile + ".xml"] = templates[UnitFile + ".xml"] f.write("
") ############################################################ f.write("\n\n

Units Specializations

\n") f.write("

This table compares each template to its parent, showing the differences between the two.
Note that like any table, you can copy/paste this in Excel (or Numbers or ...) and sort it.

") TemplatesByParent = {} #Get them in the array for CivUnitTemplate in CivTemplates: parent = CivTemplates[CivUnitTemplate]["Parent"] if parent in templates and templates[parent]["Civ"] == None: if parent not in TemplatesByParent: TemplatesByParent[parent] = [] TemplatesByParent[parent].append( (CivUnitTemplate,CivTemplates[CivUnitTemplate])) #Sort them by civ and write them in a table. f.write("\n") f.write("\n") f.write("\n") for parent in TemplatesByParent: TemplatesByParent[parent].sort(key=lambda x : Civs.index(x[1]["Civ"])) f.write("") for tp in TemplatesByParent[parent]: f.write("") diff = int(tp[1]["HP"]) - int(templates[parent]["HP"]) f.write("") diff = int(tp[1]["BuildTime"]) - int(templates[parent]["BuildTime"]) f.write("") diff = float(tp[1]["WalkSpeed"]) - float(templates[parent]["WalkSpeed"]) f.write("") for atype in AttackTypes: diff = float(tp[1]["Armour"][atype]) - float(templates[parent]["Armour"][atype]) f.write("") attType = ("Ranged" if tp[1]["Ranged"] else "Melee") for atype in AttackTypes: diff = float(tp[1]["Attack"][attType][atype]) - float(templates[parent]["Attack"][attType][atype]) f.write("") for rtype in Resources: diff = float(tp[1]["Cost"][rtype]) - float(templates[parent]["Cost"][rtype]) f.write("") diff = float(tp[1]["Cost"]["population"]) - float(templates[parent]["Cost"]["population"]) f.write("") f.write("") diff = float(CalcValue(tp[1])["Total"]) / float(CalcValue(templates[parent])["Total"])-1.0 f.write("") f.write("\n") f.write("
HPBuildTimeSpeed(walk)ArmourAttackCostsCivWorth
HPCHPCFWSMP
" + parent + "" + tp[0] + "" + str(int(diff)) + " 0 else "0,150,0")) + ");\">" + str(int(diff)) + "" + str("%.1f" % diff) + "" + str(int(diff)) + "" + str(int(diff)) + " 0 else "0,150,0")) + ");\">" + str(int(diff)) + " 0 else "0,150,0")) + ");\">" + str(int(diff)) + "" + tp[1]["Civ"] + "" + str(int(100*diff)) + "%
") # Table of unit having or not having some units. f.write("\n\n

Roster Variety

\n") f.write("

This table show which civilizations have units who derive from each loaded generic template.
Green means 1 deriving unit, blue means 2, black means 3 or more.
The total is the total number of loaded units for this civ, which may be more than the total of units inheriting from loaded templates.

") f.write("
\n") f.write("\n") for civ in Civs: f.write("\n") f.write("\n") sortedDict = sorted(templates.items(), key=SortFn) for tp in sortedDict: if tp[0] not in TemplatesByParent: continue f.write("\n") for civ in Civs: found = 0 for temp in TemplatesByParent[tp[0]]: if temp[1]["Civ"] == civ: found += 1 if found == 1: f.write("") elif found == 2: f.write("") elif found >= 3: f.write("") else: f.write("") f.write("\n") f.write("\n") for civ in Civs: f.write("\n") f.write("\n") f.write("
Template" + civ + "
" + tp[0] +"
Total:" + str(len(CivData[civ]["Units"])) + "
") ############################################################ # Writing Civ Specific Comparisons. f.write("

Civilisation Comparisons

\n") f.write("

The following graphs attempt to do some analysis of civilizations against each other. ") f.write("Different kinds of data: A matrix of units efficiency in a 1v1 fight, a graph of unit worthiness (power/cost), and a statistical analysis of civilizations against each other:
") f.write("In this last one in particular, \"Minimal Maximal Efficiency\" is low if there is a unit this civ cannot counter, and \"Maximal Minimal Efficiency\" is high if there is a unit that cannot be countered. If the (minimal) maximal efficiency and the average maximal efficiency are close, this means there are several units for that civ that counter the enemy civ's unit.
The Balance column analyses those two to give some verdict (consider the average effectiveness too though)

Note that \"Counter\" in this data means \"Any unit that is strong against another unit\", so a unit that's simply way stronger than another counts as \"countering it\".
If a civ's average efficiency and average efficiency disregarding costs are very different, this can mean that its costly units are particularly strong (Mauryans fit the bill with elephants).

") for Civ in Civs: CivData[Civ]["RangedUnitsWorth"] = 0 for unit in CivData[Civ]["RangedUnits"]: value = CalcValue(CivData[Civ]["RangedUnits"][unit])["Total"] if paramRangedMode == "Average": CivData[Civ]["RangedUnitsWorth"] += value elif value > CivData[Civ]["RangedUnitsWorth"]: CivData[Civ]["RangedUnitsWorth"] = value if (paramRangedMode == "Average" and len(CivData[Civ]["RangedUnits"]) > 0): CivData[Civ]["RangedUnitsWorth"] /= len(CivData[Civ]["RangedUnits"]) for Civ in Civs: f.write("

" + Civ + "

") z = {} for oCiv in Civs: if (oCiv != Civ): z.update(CivData[oCiv]["Units"]) WriteComparisonTable(CivData[Civ]["Units"],z) WriteWorthList(CivData[Civ]["Units"]) f.write("

This Civilization has " + str(len(CivData[Civ]["RangedUnits"])) + " ranged units for a worth of " + str(CivData[Civ]["RangedUnitsWorth"]) + " (method " + ("Average" if paramRangedMode == "Average" else "Max") + "), individually:
") for unit in CivData[Civ]["RangedUnits"]: value = CalcValue(CivData[Civ]["RangedUnits"][unit])["Total"] f.write(unit + " : " + str(value) + "
") f.write("


The following table uses the value in the comparison matrix above, factoring in costs (on the 3 columns on the right), and the average efficiency of its ranged units (per ParamRangedCoverage - " + str(paramRangedCoverage) + " right now).

") f.write("
") f.write("") for oCiv in Civs: if (oCiv == Civ): continue CivUnitComparisons(CivData[Civ],CivData[oCiv]) f.write("
Average Efficiency
(disregards costs)
Maximal Minimal Efficiency
(disregards costs)
Minimal Maximal Efficiency
(disregards costs)
Average EfficiencyMaximal Minimal EfficiencyMinimal Maximal EfficiencyBalanceRanged EfficiencyCountered the leastCounters the least
") f.write("\n")