Skip to content

Instantly share code, notes, and snippets.

Created July 28, 2017 05:06
Show Gist options
  • Save anonymous/ead0ef9bfc819f93b3a214550af2e957 to your computer and use it in GitHub Desktop.
Save anonymous/ead0ef9bfc819f93b3a214550af2e957 to your computer and use it in GitHub Desktop.
import zlib
import base64
import json
import sys
def flip_entity(entity):
# translation tables
newX = [-3,-2, 0, 2,3,2,0,-2]
newY = [ 0,-2,-3,-2,0,2,3, 2]
flipDirC = [1,0,7,6,5,4,3,2]
flipDirR = [0,7,2,5,4,3,6,1]
flipDirS = [0,7,6,5,4,3,2,1]
entity['position']['x'] = -entity['position']['x']
if "-rail" in entity['name']:
# Entity is either a straight or a curved rail
table = flipDirR if 'straight' in entity['name'] else flipDirC
entity['direction'] = table[entity.get('direction', 0)]
else:
# Entity is a signal
table = flipDirS
entity['direction'] = table[entity.get('direction', 0)]
entity['position']['x'], entity['position']['y'] = \
entity['position']['x'] + newX[entity['direction']], \
entity['position']['y'] + newY[entity['direction']]
with open(sys.argv[1]) as f:
s = f.read()
# decode
data = json.loads(str(zlib.decompress(base64.b64decode(s[1:]), wbits=0), 'utf-8'))
# process
entities = data['blueprint']['entities']
for entity in entities:
flip_entity(entity)
# encode
outdata = "0" + str(base64.b64encode(zlib.compress(json.dumps(bp_json_obj,separators=(',', ':')).encode('utf-8'),9)),'utf-8')
with open(sys.argv[2],'w') as file:
file.write(bpsO)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment