forked from notfood/RimWorld-SeedsPlease
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_plants.py
executable file
·113 lines (89 loc) · 4.21 KB
/
parse_plants.py
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
#!/bin/env python
import os
import sys
from lxml import etree
if len(sys.argv) < 2:
exit(1)
seeds = etree.Element("Defs")
recipes = etree.Element("Defs")
prefixes = ['VG_Plant', 'VGP_', 'Plant_', 'WildPlant', 'Wild', 'Plant', 'tree', 'Tree']
for file in sys.argv[1:]:
tree = etree.parse(file)
root = tree.getroot()
thingDefs = root.findall('ThingDef');
basename = ' ' + os.path.basename(file) + ' '
seeds.append(etree.Comment(basename))
recipes.append(etree.Comment(basename))
print('\n', basename, len(thingDefs))
for child in thingDefs:
defName = child.find('defName')
if defName is None:
continue
name = defName.text
for w in prefixes:
name = name.replace(w, '')
name = name.capitalize()
plant = child.find('plant')
if plant is None:
continue
parentName = child.get('ParentName')
if parentName is None:
continue
plantYield = plant.find('harvestYield');
if plantYield is None:
continue
else:
plantYield = plantYield.text
texture = 'Things/Item/Seeds/' + name
hasTexture = os.path.exists('Textures/' + texture)
yieldCount = max(int(round(int(plantYield)/3)), 4)
print('\t', name, parentName, plantYield, str(yieldCount), hasTexture)
sthingDef = etree.SubElement(seeds, 'SeedsPlease.SeedDef')
sthingDef.set('ParentName', 'SeedBase')
sdefName = etree.SubElement(sthingDef, 'defName')
sdefName.text = 'Seed_' + name;
slabel = etree.SubElement(sthingDef, 'label')
slabel.text = name.lower() + ' seeds'
splant = etree.SubElement(sthingDef, 'plant')
splant.text = defName.text
if False:
sstatBases = etree.SubElement(sthingDef, 'statBases')
sMarketValue = etree.SubElement(sstatBases, 'MarketValue')
sMarketValue.text = '0'
sseed = etree.SubElement(sthingDef, 'seed')
sharvestFactor = etree.SubElement(sseed, 'harvestFactor')
sharvestFactor.text = '1.0'
sseedFactor = etree.SubElement(sseed, 'seedFactor')
sseedFactor.text = '1.0'
sbaseChance = etree.SubElement(sseed, 'baseChance')
sbaseChance.text = '1.0'
sextraChance = etree.SubElement(sseed, 'extraChance')
sextraChance.text = '0.1'
if hasTexture:
sTexture = etree.XML('<graphicData><texPath>' + texture + '</texPath></graphicData>')
sthingDef.append(sTexture)
harvestedThingDef = plant.find('harvestedThingDef')
if harvestedThingDef is None:
continue
rthingDef = etree.SubElement(recipes, 'RecipeDef')
rthingDef.set('ParentName', 'ExtractSeed')
rdefName = etree.SubElement(rthingDef, 'defName')
rdefName.text = 'ExtractSeed_' + name;
rlabel = etree.SubElement(rthingDef, 'label')
rlabel.text = 'extract ' + name.lower() + ' seeds'
rdesc = etree.SubElement(rthingDef, 'description')
rdesc.text = 'Extract seeds from ' + harvestedThingDef.text.replace('Raw', '') + '.'
ringredients = etree.XML('<ingredients><li><filter><thingDefs><li>' + harvestedThingDef.text + '</li></thingDefs></filter><count>' + str(yieldCount) + '</count></li></ingredients>')
rthingDef.append(ringredients)
rfixedIngredientsFilter = etree.XML('<fixedIngredientFilter><thingDefs><li>' + harvestedThingDef.text + '</li></thingDefs></fixedIngredientFilter>')
rthingDef.append(rfixedIngredientsFilter);
rproducts = etree.SubElement(rthingDef, 'products')
rproduct = etree.SubElement(rproducts, sdefName.text)
rproduct.text = '3'
research = plant.find('sowResearchPrerequisites')
if research is None or not len(research):
continue
rresearch = etree.SubElement(rthingDef, 'researchPrerequisite')
rresearch.text = research[0].text
etree.ElementTree(seeds).write('Items_Seeds.xml', pretty_print=True)
etree.ElementTree(recipes).write('Recipes_Plants.xml', pretty_print=True)