-
Notifications
You must be signed in to change notification settings - Fork 7
/
PatchContainers.py
345 lines (298 loc) · 14.3 KB
/
PatchContainers.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import os
import re
from KSPUtils.config_node_utils import ConfigNode, NamedObject, Part, Module, Resource
from KSPUtils.config_node_utils.named_object import ChildrenDict, ValueProperty
from KSPUtils.config_node_utils.search import SearchQuery, SearchTerm
class TankType(NamedObject):
type = 'TANKTYPE'
UsefulVolumeRatio = ValueProperty(float)
TankCostPerSurface = ValueProperty(float)
def __init__(self):
NamedObject.__init__(self)
self.UnitsPerLiter = dict()
def load(self, node):
NamedObject.load(self, node)
val = self.values.get('PossibleResources')
if val:
self.UnitsPerLiter = {r[0]: float(r[1]) for r in
(res.strip().split() for res in val.value.split(';'))
if len(r) > 1}
@property
def PossibleResources(self):
return list(self.UnitsPerLiter.keys())
class TanksLib(NamedObject):
type = 'TANKSLIB'
types = ChildrenDict(TankType)
FLOAT_DECIMALS = 6
def round_float(val):
return round(float(val), FLOAT_DECIMALS)
class ModuleTankManager(Module):
Volume = ValueProperty(round_float)
DoCostPatch = ValueProperty(bool)
DoMassPatch = ValueProperty(bool)
IncludeTankTypes = ValueProperty(str)
ExcludeTankTypes = ValueProperty(str)
def __init__(self):
Module.__init__(self)
self.name = 'ModuleTankManager'
class ModuleTank(Module):
Volume = ValueProperty(round_float)
InitialAmount = ValueProperty(round_float)
TankType = ValueProperty(str)
CurrentResource = ValueProperty(str)
ChooseTankType = ValueProperty(bool)
DoCostPatch = ValueProperty(bool)
DoMassPatch = ValueProperty(bool)
IncludeTankTypes = ValueProperty(str)
ExcludeTankTypes = ValueProperty(str)
def __init__(self):
Module.__init__(self)
self.name = 'ModuleSwitchableTank'
class Tank(NamedObject):
type = 'TANK'
Volume = ValueProperty(round_float)
InitialAmount = ValueProperty(round_float)
TankType = ValueProperty(str)
CurrentResource = ValueProperty(str)
def __init__(self, name=None):
NamedObject.__init__(self)
if name: self.name = name
common_patch_spec = ':HAS[' \
'!MODULE[InterstellarFuelSwitch],' \
'!MODULE[FSfuelSwitch],' \
'!MODULE[ModuleB9PartSwitch]:HAS[@SUBTYPE:HAS[#tankType]]]' \
':NEEDS[!modularFuelTanks&!RealFuels]'
class Patcher(object):
def __init__(self, typelib, gamedata):
self.game_data = gamedata
self.tank_types_file = typelib
self.tank_types = TanksLib.from_node(ConfigNode.Load(self.tank_types_file))
self.types = self.tank_types.types
self.part_filter = None
def volume(self, tanktype, name, units):
t = self.types[tanktype]
upl = t.UnitsPerLiter.get(name)
if not upl:
print('WARNING: no UnitsPerLiter value for %s in %s:\n%s' % (name, tanktype, str(t)))
return units / upl / t.UsefulVolumeRatio / 1e3 if upl else 0
@staticmethod
def get_parts(path):
parts = []
for dirpath, _dirnames, filenames in os.walk(path):
for filename in filenames:
if filename.endswith('.cfg'):
node = ConfigNode.Load(os.path.join(dirpath, filename))
if node.name == 'PART':
part = Part.from_node(node)
resources = part.resources
parts.append((part, resources))
return parts
@staticmethod
def print_patches(stream, patches, header):
stream.write(f'\n//{header}\n//Automatically generated using PyKSPutils library\n')
stream.write('\n\n'.join(str(p) for p in patches))
@staticmethod
def add_patches(part, patch, addons):
for term, addon in addons:
if term.match(part):
if isinstance(addon, NamedObject.Value):
patch.AddValueItem(addon)
elif isinstance(addon, NamedObject):
patch.AddChild(addon)
def patch_1RES(self, stream, parts, tank_type, res_name,
monotype=None, addons=None, add_spec=''):
patches = []
rate = self.volume(tank_type, res_name, 1)
polytype = lambda part: True
if monotype:
if isinstance(monotype, (list, tuple)):
polytype = lambda part: part.name not in monotype
elif monotype in ('all', 'strict'):
polytype = lambda part: False
elif isinstance(monotype, str):
mono_re = re.compile(monotype)
polytype = lambda part: mono_re.match(part.name) is None
for part, resources in parts:
if len(resources) == 1 and res_name in resources:
if self.part_filter is not None and self.part_filter.match(part):
continue
print('Patching %s' % part.name)
res = resources[res_name]
patch = Part.Patch('@', part.name, common_patch_spec + add_spec)
V = res.maxAmount * rate
ini = res.amount / res.maxAmount
comment = f'{res.maxAmount} units of {res_name}: conversion rate is {rate:.6f} m3/u'
patch.AddChild(Resource.Patch('!', res_name))
can_change_type = polytype(part)
if can_change_type or monotype != 'strict':
mgr = ModuleTankManager()
mgr.Volume = V
mgr.SetComment('Volume', comment)
mgr.DoCostPatch = True
mgr.DoMassPatch = True
if not can_change_type:
mgr.IncludeTankTypes = tank_type
tank = Tank()
tank.TankType = tank_type
tank.CurrentResource = res_name
tank.InitialAmount = ini
tank.Volume = 100
mgr.AddChild(tank)
patch.AddChild(mgr)
else:
tank = ModuleTank()
tank.Volume = V
tank.SetComment('Volume', comment)
tank.InitialAmount = ini
tank.DoCostPatch = True
tank.DoMassPatch = True
tank.ChooseTankType = False
tank.TankType = tank_type
tank.CurrentResource = res_name
patch.AddChild(tank)
if addons:
self.add_patches(part, patch, addons)
patches.append(patch)
if patches: self.print_patches(stream, patches, '%s Tanks' % res_name)
def patch_LFO(self, stream, parts, addons=None, add_spec=''):
patches = []
rate = self.volume('LiquidChemicals', 'LiquidFuel', 1) / 0.45
for part, resources in parts:
if len(resources) == 2 and 'LiquidFuel' in resources and 'Oxidizer' in resources:
if self.part_filter is not None and self.part_filter.match(part):
continue
print('Patching %s' % part.name)
lf = resources['LiquidFuel']
patch = Part.Patch('@', part.name, common_patch_spec + add_spec)
patch.AddChild(Resource.Patch('!', 'LiquidFuel'))
patch.AddChild(Resource.Patch('!', 'Oxidizer'))
mgr = ModuleTankManager()
mgr.Volume = lf.maxAmount * rate
mgr.SetComment('Volume',
f'{lf.maxAmount} units of LF: conversion rate is {rate:.6f} m3/u')
mgr.DoCostPatch = True
mgr.DoMassPatch = True
tank = Tank('LFO')
tank.Volume = 100
mgr.AddChild(tank)
patch.AddChild(mgr)
if addons:
self.add_patches(part, patch, addons)
patches.append(patch)
if patches: self.print_patches(stream, patches, 'Rocket Fuel Tanks')
def patch_parts(self, output, paths, addons=None, add_spec=''):
for path in paths:
parts = self.get_parts(os.path.join(self.game_data, *path))
if not parts:
print('No parts in: {}'.format('/'.join(path)))
continue
with open(os.path.join(self.game_data, *output), 'w') as out:
out.write('//Configurable Containers patch for %s\n' % os.path.join(*path))
self.patch_LFO(out, parts, addons=addons, add_spec=add_spec)
self.patch_1RES(out, parts,
'LiquidChemicals', 'LiquidFuel', '.*[Ww]ing.*',
addons=addons, add_spec=add_spec)
self.patch_1RES(out, parts,
'LiquidChemicals', 'MonoPropellant',
addons=addons, add_spec=add_spec)
self.patch_1RES(out, parts,
'Gases', 'XenonGas', 'strict',
addons=addons, add_spec=add_spec)
self.patch_1RES(out, parts,
'Gases', 'ArgonGas', 'strict',
addons=addons, add_spec=add_spec)
self.patch_1RES(out, parts,
'Soil', 'Ore',
addons=addons, add_spec=add_spec)
out.write('\n//:mode=c#:\n')
print('%s done.\n' % os.path.join(*path))
def patch_mod(self, mod, addons=None, add_spec=''):
output = ('ConfigurableContainers', 'Parts', f'{mod}_Patch.cfg')
path = [[mod]]
add_spec += ':AFTER[%(mod)s]' % {'mod': mod}
self.patch_parts(output, path, addons, add_spec)
def patch_mods(self, *mods):
for m in mods: self.patch_mod(m)
if __name__ == '__main__':
patcher = Patcher('../GameData/000_AT_Utils/TankTypes.cfg',
'/home/storage/Games/KSP_linux/PluginsArchives/'
'Development/AT_KSP_Plugins/KSP-test/'
'KSP_test_1.9.1/GameData')
patcher.part_filter = SearchQuery('PART/MODULE:.*Engines.*/')
patcher.part_filter.Or('PART/MODULE:.*Converter.*/')
patcher.part_filter.Or('PART/MODULE:.*Harvester.*/')
patcher.part_filter.Or('PART/MODULE:.*Drill.*/')
patcher.part_filter.Or('PART/MODULE:.*[Ff]uelSwitch/')
patcher.part_filter.Or('PART/MODULE:.*[Rr]esourceSwitch/')
# filter out only resource switching B9PS
patcher.part_filter.Or('PART/MODULE:ModuleB9PartSwitch/SUBTYPE/tankType:.*')
xenon_titles = [
(SearchTerm('name:xenonTank$'),
Part.PatchValue('@', 'title', 'PB-X150 Pressurized Gas Container')),
(SearchTerm('name:xenonTankLarge$'),
Part.PatchValue('@', 'title', 'PB-X750 Pressurized Gas Container')),
(SearchTerm('name:xenonTankRadial$'),
Part.PatchValue('@', 'title', 'PB-X50R Pressurized Gas Container')),
]
patcher.patch_parts(('ConfigurableContainers', 'Parts', 'Squad_Patch.cfg'),
[('Squad', 'Parts')], xenon_titles)
patcher.patch_parts(('ConfigurableContainers', 'Parts', 'MakingHistory_Patch.cfg'),
[('SquadExpansion', 'MakingHistory', 'Parts')])
# nothing to patch there
# patcher.patch_parts(('ConfigurableContainers', 'Parts', 'Squad_Serenity_Patch.cfg'),
# [('SquadExpansion', 'Serenity', 'Parts')])
patcher.patch_mods('KWRocketry',
'Mk2Expansion',
'Mk3Expansion',
'SpaceY-Lifters',
'SpaceY-Expanded',
'FuelTanksPlus',
'ModRocketSys',
'NearFuturePropulsion',
'SPS', # Standard Propulsion Systems
'RaginCaucasian', # Mk2.5 spaceplane parts
'MunarIndustries', # Fuel Tank Expansion
'Bluedog_DB', # Bluedog Design Bureau
'StreamlineEnginesTanks',
# 'UniversalStorage2', # uses USFuelSwitch
'MiningExpansion',
# 'StationPartsExpansionRedux', # uses B9PartSwitch
# 'ReStock', # no parts
'ReStockPlus',
'PlanetaryBaseInc', # Kerbal Planetary Base Systems
# 'FelineUtilityRover', # uses ModuleKerbetrotterResourceSwitch
'Benjee10_X-37B', # Mk-X Spaceplane Parts
'Mk3HypersonicSystems',
'DodoLabs', # Stockalike Electron
# 'Mkerb', # no parts
# 'JSI' # no parts
)
patcher.patch_parts(('ConfigurableContainers', 'Parts', 'Tal-Tanks_Patch.cfg'),
[('ModsByTal', 'Parts')],
[(SearchTerm(''), Module.Patch('!', 'ModuleFuelTanks'))],
add_spec=':AFTER[ModsByTal]')
# USI uses FSfuelSwitch, so no patching for it
# patcher.patch_parts(('ConfigurableContainers', 'Parts', 'USI-MKS_Patch.cfg'),
# [('UmbraSpaceIndustries', 'Akita'),
# ('UmbraSpaceIndustries', 'Konstruction'),
# ('UmbraSpaceIndustries', 'Kontainers'),
# ('UmbraSpaceIndustries', 'MKS'),
# ('UmbraSpaceIndustries', 'ReactorPack'),
# ],
# add_spec=':NEEDS[MKS]:AFTER[MKS]')
#
# patcher.patch_parts(('ConfigurableContainers', 'Parts', 'USI-LS_Patch.cfg'),
# [('UmbraSpaceIndustries', 'LifeSupport'),
# ],
# add_spec=':NEEDS[USILifeSupport]:AFTER[USILifeSupport]')
#
# patcher.patch_parts(('ConfigurableContainers', 'Parts', 'USI-FTT_Patch.cfg'),
# [('UmbraSpaceIndustries', 'FTT'),
# ],
# add_spec=':NEEDS[FTT]:AFTER[FTT]')
#
# patcher.patch_parts(('ConfigurableContainers', 'Parts', 'USI-ExpPack_Patch.cfg'),
# [('UmbraSpaceIndustries', 'ExpPack'),
# ],
# add_spec=':NEEDS[ExpPack]:AFTER[ExpPack]')
print('Done')