def remove_spawns_outside_radius(directory): with open( pathlib.Path(resourcesdir.get(), 'modifications/server/types_universal.json')) as f: type_universal_config = json.load(f) radius = type_universal_config.get('limit_spawn_locations_radius', 0) if radius <= 0: return p = pathlib.Path(deploydir.get(), 'mpmissions', mission.get(), 'mapgrouppos.xml') count = 0 areas = list(mark[1] for mark in locations.marks[:4]) mapgroups = ElementTree.parse(p).getroot() for group in mapgroups.findall('.//group'): raw = group.get('pos') # log.info('{} {}'.format(group.get('name'), raw)) x, y, z = (float(i) for i in raw.split(' ')) is_good = False for position in areas: if locations.overlaps(position, radius, x, z, 1): is_good = True if not is_good: mapgroups.remove(group) count += 1 log.debug('removed group {}, {}, {}'.format( group.get('name'), x, z)) if count > 0: log.info('removed {} groups from {}'.format(count, p.name)) with file_writing.f_open(pathlib.Path(directory, p.name), mode='w') as f: f.write(file_writing.convert_to_string(mapgroups))
def random_presets_config(directory): p = pathlib.Path(deploydir.get(), 'mpmissions', mission.get(), 'cfgrandompresets.xml') with open(p) as f: # The parser in ElementTree was having trouble with the comments in the xml file # So, we're removing them manually beforehand raw = f.read() raw = re.sub(r'<!--.*-->', '', raw) presets_xml = ElementTree.fromstring(raw) with open( pathlib.Path(resourcesdir.get(), 'modifications/server/random_presets.json')) as f: presets_modifications = json.load(f) for entry in presets_modifications: if 'cargo' in entry: cargo = entry.get('cargo') if 'chance' in entry: for preset in presets_xml.findall( f'.//cargo[@name="{cargo}"]'): preset.set('chance', entry.get('chance')) for item in entry.get('items'): for preset in presets_xml.findall( './/cargo[@name="{}"]//item[@name="{}"]'.format( cargo, item.get('name'))): preset.set('chance', item.get('chance')) if 'attachments' in entry: attachments = entry.get('attachments') for item in entry.get('items'): for preset in presets_xml.findall( './/attachments[@name="{}"]//item[@name="{}"]'.format( attachments, item.get('name'))): preset.set('chance', item.get('chance')) with file_writing.f_open(pathlib.Path(directory, 'cfgrandompresets.xml'), mode='w') as f: f.write(file_writing.convert_to_string(presets_xml))
def spawnable_types_config(directory): """ This configuration operates in an overriding fashion. That is, if a preset or list of items is specified for a type in the config, any existing entries for that type with the same tag and attribute will be erased. """ p = pathlib.Path(deploydir.get(), 'mpmissions', mission.get(), 'cfgspawnabletypes.xml') with open(p) as f: # The parser in ElementTree was having trouble with the comments in the xml file # So, we're removing them manually beforehand raw = f.read() raw = re.sub(r'<!--.*-->', '', raw) spawnable_xml = ElementTree.fromstring(raw) with open(pathlib.Path(resourcesdir.get(), 'modifications/server/spawnable_types.json')) as f: spawnable_modifications = json.load(f) for type_config in spawnable_modifications: if len(db_types.get().getroot().findall('.//type[@name="{}"]'.format(type_config.get('type')))) == 0: continue types = spawnable_xml.findall('.//type[@name="{}"]'.format(type_config.get('type'))) if len(types) == 0: new_type = ElementTree.SubElement(spawnable_xml, 'type', dict(name=type_config.get('type'))) types = [new_type] for t in types: if 'cargo_presets' in type_config: handle_presets(t, 'cargo', type_config.get('cargo_presets')) if 'attachments_presets' in type_config: handle_presets(t, 'attachments', type_config.get('attachments_presets')) if 'cargo_items' in type_config: handle_items(t, 'cargo', type_config.get('cargo_items')) if 'attachments' in type_config: handle_items(t, 'attachments', type_config.get('attachments')) with file_writing.f_open(pathlib.Path(directory, 'cfgspawnabletypes.xml'), mode='w') as f: f.write(file_writing.convert_to_string(spawnable_xml))
def globals_config(directory): globals_xml = ElementTree.parse( pathlib.Path(deploydir.get(), 'mpmissions', mission.get(), 'db/globals.xml')) with open(pathlib.Path(resourcesdir.get(), 'modifications/server/globals.json')) as f: globals_modifications = json.load(f) for k, v in globals_modifications.items(): item = globals_xml.getroot().find('.//var[@name="{}"]'.format(k)) item.set('value', str(v)) with file_writing.f_open(pathlib.Path(directory, 'globals.xml'), mode='w') as f: f.write(file_writing.convert_to_string(globals_xml.getroot()))
def territory_config(directory): with open(pathlib.Path(resourcesdir.get(), 'modifications/server/territories.json')) as f: territories_modifications = json.load(f) for p in pathlib.Path(deploydir.get(), 'mpmissions', mission.get(), 'env').glob('*.xml'): filename = p.name territory = ElementTree.parse(p).getroot() if filename in territories_modifications.get('ratio', dict()): ratio = territories_modifications.get('ratio').get(filename) log.info('applying ratio of {} to {}'.format(ratio, filename)) for zone in territory.findall('.//zone'): dmin = zone.get('dmin') dmax = zone.get('dmax') zone.set('dmin', str(math.floor(int(dmin) * ratio))) zone.set('dmax', str(math.floor(int(dmax) * ratio))) if filename in territories_modifications.get('radius_ratio', dict()): ratio = territories_modifications.get('radius_ratio').get(filename) log.info('applying radius ratio of {} to {}'.format(ratio, filename)) for zone in territory.findall('.//zone'): r = zone.get('r') zone.set('r', str(math.floor(float(r) * ratio))) if filename in territories_modifications.get('blanket', dict()): params = territories_modifications.get('blanket').get(filename) new_territory = ElementTree.Element('territory', attrib=dict(color='1910952871')) count = 0 for x in range(500, 12600, 1000): for z in range(3750, 14850, 1000): count += 1 ElementTree.SubElement(new_territory, 'zone', attrib={ 'name': params.get('name'), 'smin': '0', 'smax': '0', 'dmin': params.get('dmin'), 'dmax': params.get('dmax'), 'x': str(x), 'z': str(z), 'r': '500' }) territory.append(new_territory) log.info('added {} zones in a blanket to {}'.format(count, filename)) if '@Trader' in mods.get(): remove_zones_if_near_traders(territory, filename) with file_writing.f_open(pathlib.Path(directory, filename), mode='w') as f: f.write(file_writing.convert_to_string(territory))
def events_config(directory): events_xml = ElementTree.parse( pathlib.Path(deploydir.get(), 'mpmissions', mission.get(), 'db/events.xml')) with open(pathlib.Path(resourcesdir.get(), 'modifications/server/events.json')) as f: events_modifications = json.load(f) for mod in events_modifications: name_re = re.compile(mod.get('name')) for event in events_xml.getroot(): if name_re.match(event.get('name')): ratio = mod.get('ratio') if ratio > 0: event.find('active').text = '1' for item in ('nominal',): i = event.find(item) i.text = str(math.floor(max(1, int(i.text)) * ratio)) else: event.find('active').text = '0' with file_writing.f_open(pathlib.Path(directory, 'events.xml'), mode='w') as f: f.write(file_writing.convert_to_string(events_xml.getroot()))
def remove_building_spawns_near_traders(directory): p = pathlib.Path(deploydir.get(), 'mpmissions', mission.get(), 'mapgrouppos.xml') count = 0 mapgroups = ElementTree.parse(p).getroot() for group in mapgroups.findall('.//group'): raw = group.get('pos') # log.info('{} {}'.format(group.get('name'), raw)) x, y, z = (float(i) for i in raw.split(' ')) clean_traders = (mark[1] for mark in locations.marks[:4]) for position in clean_traders: if locations.overlaps(position, 200, x, z, 1): find_string = './/group[@pos="{}"]...'.format(raw) parents = mapgroups.findall(find_string) for parent in parents: if group in parent: count += 1 parent.remove(group) log.debug('removed group {}, {}, {}'.format(group.get('name'), x, z)) break if count > 0: log.info('removed {} groups from {}'.format(count, p.name)) with file_writing.f_open(pathlib.Path(directory, p.name), mode='w') as f: f.write(file_writing.convert_to_string(mapgroups))
def sort_and_write_types_config(directory): types.get().getroot()[:] = sorted(types.get().getroot(), key=lambda child: child.get('name').lower()) with file_writing.f_open(pathlib.Path(directory, 'types.xml'), mode='w') as f: f.write(file_writing.convert_to_string(types.get().getroot()))