Advertisement
Guest User

Untitled

a guest
Jun 24th, 2022
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.53 KB | None | 0 0
  1. import copy
  2. import json
  3. import re
  4. import sys
  5.  
  6.  
  7. def mirror(wall, axis):
  8.   # Wall points appear in the form 'PoolVector2Array( x1, y1, x2, y2, ..., x99, y99)'
  9.   # Capture each float, noting that whole numbers won't have a decimal.
  10.   matches =  re.findall(r" (\d+(?:\.\d+)?)", wall["points"])
  11.   assert (len(matches) % 2) == 0
  12.  
  13.   # We have a flattened list of points. Even indices are x values. Mirror them
  14.   # across the y-axis, but leave the y values alone.
  15.   mirrored_points = (f"{2*axis*256-float(n)}" if i % 2 == 0 else n for i, n in enumerate(matches))
  16.  
  17.   new_wall = copy.deepcopy(wall)
  18.   new_wall["points"] = f"PoolVector2Array( {', '.join(mirrored_points)} )"
  19.  
  20.   return new_wall
  21.  
  22.  
  23. def main():
  24.   if len(sys.argv) != 2:
  25.     print(f"Usage: {sys.argv[0]} map_file", file=sys.stderr)
  26.     sys.exit(1)
  27.  
  28.   map_file = sys.argv[1]
  29.   with open(map_file, "r") as f:
  30.     d = json.load(f)
  31.  
  32.   world = d["world"]
  33.  
  34.   # This assumes the first wall on the first level is the target.
  35.   walls = world["levels"]["0"]["walls"]
  36.   # Mirroring is along the y-axis, even width assumed.
  37.   axis = round(world["width"] / 2)
  38.   new_wall = mirror(walls[0], axis)
  39.  
  40.   # The next node id is stored as a hex string. Use and update manually for the new wall.
  41.   next_node_id = world["next_node_id"]
  42.   new_wall["node_id"] = next_node_id
  43.   world["next_node_id"] = format(int(next_node_id, 16)+1, "x")
  44.   world["levels"]["0"]["walls"].append(new_wall)
  45.  
  46.   with open(f"mirrored_{map_file}", "w") as f:
  47.     json.dump(d, f)
  48.  
  49.  
  50. if __name__ == "__main__":
  51.   main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement