Created
September 9, 2017 15:04
-
-
Save kbauer/954190e60ea4b84a0b529124b46b4430 to your computer and use it in GitHub Desktop.
Rescale Mount & Blade maps - A python 3.6+ script for rescaling the world map. Performed by multiplying the positions of all "parties" (cities, spawn points, bridges, ..., and actual parties) and map vertices (the geometry of the world map) by a factor. Factors below 0.7 will look cramped. Used by running the script in the module directory. The …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import shutil | |
import re | |
import sys | |
import contextlib as cl | |
import time | |
@cl.contextmanager | |
def openw(filename): | |
tmpname = filename + '~' | |
with open(tmpname,'w') as f: | |
exc = yield f | |
os.replace(tmpname, filename) | |
def make_backup_file(filename : str) -> str: | |
backup = os.path.join('_rescale',filename) | |
if not os.path.exists(backup): | |
os.makedirs('_rescale', exist_ok=True) | |
shutil.copy(filename,backup) | |
return backup | |
def rescale_map_txt(factor): | |
with open(make_backup_file("map.txt")) as mapf, \ | |
openw('map.txt') as out: | |
cb = out.write | |
i = iter(mapf) | |
cb(next(i)) | |
for line in i: | |
try: | |
x,y,z = tuple(map(float, line.split())) | |
except: | |
cb(line) | |
break | |
x,y,z = factor*x, factor*y, factor*z | |
cb(f'{x:.6f} {y:.6f} {z:.6f}\n') | |
for line in i: | |
cb(line) | |
def rescale_parties_txt(factor): | |
with open(make_backup_file('parties.txt')) as infile,\ | |
openw('parties.txt') as outfile: | |
cb = outfile.write | |
lines = list(infile) | |
cb(lines.pop(0)) | |
cb(lines.pop(0)) | |
linenum = 2 | |
while lines: | |
linenum += 1 | |
line = lines.pop(0) | |
data = line.split() | |
try: | |
if len(data) < 20: | |
cb(line) | |
else: | |
for idx in range(14,20): | |
data[idx] = '{:.6f}'.format(factor * float(data[idx])) | |
cb(' ' + ' '.join(data) + '\n') | |
except Exception as e: | |
print(f'At line {linenum} in parties.txt:', file=sys.stderr) | |
print(f' {line!r}', file=sys.stderr) | |
print(e) | |
break | |
FACTOR = 0.7 | |
rescale_map_txt(FACTOR) | |
rescale_parties_txt(FACTOR) | |
print(f'Done: Rescaled by factor {FACTOR}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment