Skip to content

Instantly share code, notes, and snippets.

@mullikine
Created November 22, 2014 11:22
Show Gist options
  • Save mullikine/fd9a3d2b37bd1043235a to your computer and use it in GitHub Desktop.
Save mullikine/fd9a3d2b37bd1043235a to your computer and use it in GitHub Desktop.
For use in conjunction with AOK Trigger Studio. This program reorders the execution of a scenarios triggers using XML produced by aokts.
import xml.etree.ElementTree as ET
import sys, getopt
# Author: Shane Mulligan
# 13 April 2014
# This program reorders the execution of a scenarios triggers
# to match their display orders.
def main(argv):
inputfile = 'trigs.xml'
outputfile = 'output.xml'
outputTSE = 'output.txt' # optionally, output for trigger script editor
try:
opts, args = getopt.getopt(argv,"hi:o:t:",["ifile=","ofile=","tse="])
except getopt.GetoptError:
print 'This program reorders the execution of a scenarios triggers to match their display orders'
print 'test.py -i <inputfile=trigs.xml> -o <outputfile=output.xml> -m <trigger_to_move> -d <destination_trigger>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'test.py -i <inputfile> -o <outputfile>'
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
elif opt in ("-t", "--tse"):
tseoutputfile = arg
tree = ET.parse(inputfile)
root = tree.getroot()
for i in range(0,len(root)):
trigmove = [p for p in root.iter() for mt in p if mt.tag == "display_order" and mt.text == str(i)]
trigdest = [p for p in root if 'id' in p.attrib and p.attrib['id'] == str(i)]
trigmove = trigmove[0]
trigdest = trigdest[0]
# if m before d
if int(trigmove.attrib['id']) < int(trigdest.attrib['id']):
for trig in root:
for c in trig:
if c.tag == 'effect':
for e in c:
if e.tag == 'trigger':
if e.text == trigmove.attrib['id']:
e.text = "-5" # marks foreign trigmove id
elif e.tag == 'trigger' and int(trigmove.attrib["id"]) < int(e.text) < int(trigdest.attrib["id"]):
e.text = str(int(e.text) - 1)
for trig in root:
for c in trig:
if c.tag == 'effect':
for e in c:
if e.tag == 'trigger':
if e.text == "-5":
e.text = str(int(trigdest.attrib['id']) - 1)
# for trig in root.iter if movetrig < trig < destrig
for trig in root:
if int(trigmove.attrib['id']) < int(trig.attrib['id']) < int(trigdest.attrib['id']):
trig.attrib['id'] = str(int(trig.attrib['id']) - 1)
trigmove.attrib['id'] = str(int(trigdest.attrib['id']) - 1)
# if m after d
elif int(trigmove.attrib["id"]) > int(trigdest.attrib["id"]):
for trig in root:
for c in trig:
if c.tag == 'effect':
for e in c:
if e.tag == 'trigger':
if e.text == trigmove.attrib['id']:
e.text = "-5" # marks foreign trigmove id
elif int(trigmove.attrib['id']) > int(e.text) >= int(trigdest.attrib['id']):
e.text = str(int(e.text) + 1)
for trig in root:
for c in trig:
if c.tag == 'effect':
for e in c:
if e.tag == 'trigger':
if e.text == "-5":
#print "fixed foreign trigmove ids"
e.text = str(int(trigdest.attrib['id']))
# for trig in root.iter if movetrig < trig < destrig
for trig in root:
if int(trigmove.attrib['id']) > int(trig.attrib['id']) >= int(trigdest.attrib['id']):
trig.attrib['id'] = str(int(trig.attrib['id']) + 1)
trigmove.attrib['id'] = str(int(trigdest.attrib['id']) - 1)
# sort the xml model by trigger id
data = []
for trig in root:
key = int(trig.attrib['id'])
data.append((key, trig))
data.sort()
root[:] = [trig[-1] for trig in data]
tree.write(outputfile)
# write Tigger Script Editor 2.0 output to use for updating scenario because Trigger Studio doesn't can't import XML.
f = open('output.txt','w')
f.write('Scenario:\n')
for trig in root:
#print str(int(trig.attrib['id']) + 1)
f.write('# -- Trigger' + str(int(trig.attrib['id']) + 1) + '-- \n')
f.write('Trigger:start\n')
f.write('Enabled:' + ("true" if int(trig.find('enabled').text) else "false") + '\n')
f.write('Looping:' + ("true" if int(trig.find('looping').text) else "false") + '\n')
f.write('Objective:' + ("true" if int(trig.find('objective').text) else "false") + '\n')
f.write('Desc_order:' + trig.find('desc_order').text + '\n')
if trig.find('description') is not None:
f.write('Description:' + trig.find('description').text + '\n')
f.write('Name:' + trig.find('name').text + '\n')
for c in trig:
if c.tag == 'condition':
f.write('\tCondition:start\n')
condition_type = c.find('type').text
if condition_type == "Research Technology":
condition_type = "Research Tehcnology"
f.write('\t\tCondition_Type:' + condition_type + '\n')
if c.find('unit_object') is not None:
f.write('\t\tCondition_Unit_object:' + c.find('unit_object').text + '\n')
if c.find('timer') is not None:
f.write('\t\tCondition_Timer:' + c.find('timer').text + '\n')
if c.find('amount') is not None:
f.write('\t\tCondition_Amount:' + c.find('amount').text + '\n')
if c.find('resource') is not None:
f.write('\t\tCondition_Resource:' + c.find('resource').text + '\n')
if c.find('unit_type') is not None:
f.write('\t\tCondition_Unit_type:' + c.find('unit_type').text + '\n')
if c.find('player') is not None:
f.write('\t\tCondition_Player:' + c.find('player').text + '\n')
if c.find('technology') is not None:
f.write('\t\tCondition_Technology:' + c.find('technology').text + '\n')
if c.find('area_ll') is not None:
f.write('\t\tCondition_Area_ll:' + ','.join(c.find('area_ur').text.split(',')[::-1]) + '\n')
if c.find('area_ur') is not None:
f.write('\t\tCondition_Area_ur:' + ','.join(c.find('area_ll').text.split(',')[::-1]) + '\n')
f.write('\tEndCondition:end\n')
for c in trig:
if c.tag == 'effect':
f.write('\tEffect:start\n')
effect_type = c.find('type').text
f.write('\t\tEffect_Type:' + effect_type + '\n')
if c.find('display_time') is not None:
f.write('\t\tEffect_Display_time:' + c.find('display_time').text + '\n')
if c.find('amount') is not None:
f.write('\t\tEffect_Amount:' + c.find('amount').text + '\n')
if c.find('resource') is not None:
f.write('\t\tEffect_Resource:' + c.find('resource').text + '\n')
if c.find('instruction_panel') is not None:
f.write('\t\tEffect_Instruction_panel:' + c.find('instruction_panel').text + '\n')
if c.find('num_selected') is not None:
f.write('\t\tEffect_Num_selected:' + c.find('num_selected').text + '\n')
if c.find('unit_type') is not None:
f.write('\t\tEffect_Unit_type:' + c.find('unit_type').text + '\n')
if c.find('location_unit') is not None:
f.write('\t\tEffect_Location_unit:' + c.find('location_unit').text + '\n')
if c.find('player_source') is not None:
f.write('\t\tEffect_Player_source:' + c.find('player_source').text + '\n')
if c.find('player_target') is not None:
f.write('\t\tEffect_Player_target:' + c.find('player_target').text + '\n')
if c.find('location') is not None:
f.write('\t\tEffect_Location:' + c.find('location').text + '\n')
if c.find('area_ll') is not None:
f.write('\t\tEffect_Area_ll:' + ','.join(c.find('area_ur').text.split(',')[::-1]) + '\n')
if c.find('area_ur') is not None:
f.write('\t\tEffect_Area_ur:' + ','.join(c.find('area_ll').text.split(',')[::-1]) + '\n')
if c.find('unit_group') is not None:
f.write('\t\tEffect_Unit_group:' + c.find('unit_group').text + '\n')
if c.find('text') is not None:
f.write('\t\tEffect_Text:' + c.find('text').text + '\n')
if c.find('unit_ids') is not None:
f.write('\t\tEffect_Unit_ids:start' + '\n')
f.write('\t\t\tId:' + c.find('unit_ids').text + '\n')
f.write('\t\tEffect_EndUnit_ids:end' + '\n')
# Only use this when using activate or deactivate trigger. Doing so will clean the scenario.
if effect_type == 'Activate Trigger' or effect_type == 'Deactivate Trigger':
if c.find('trigger') is not None:
f.write('\t\tEffect_Efftrigger:' + c.find('trigger').text + '\n')
if effect_type == 'Research Technology':
if c.find('technology') is not None:
f.write('\t\tEffect_Technology:' + c.find('technology').text + '\n')
#if effect_type != 'Task Object' # enable this later
if c.find('building_type') is not None:
f.write('\t\tEffect_Building_type:' + c.find('building_type').text + '\n')
f.write('\tEndEffect:end\n')
f.write('Endtrigger:end\n')
f.write('End Scenario:\n')
#f.write('hi there\n') # python will convert \n to os.linesep
f.close() # you can omit in most cases as the destructor will call if
# point of entry
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment