Created
February 25, 2010 18:49
-
-
Save dgladkov/314892 to your computer and use it in GitHub Desktop.
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
import glob | |
import os | |
from PIL import Image | |
class MapRow(dict): | |
def __init__(self, map_obj, x_coord, *args, **kwargs): | |
super(MapRow, self).__init__(self, *args, **kwargs) | |
self.map = map_obj | |
self.x_coord = x_coord | |
def __setitem__(self, key, value): | |
if isinstance(value, (list, tuple, dict)): | |
raise Exception("You can't have more than 2 dimensions.") | |
if key not in self.map.y_coords: | |
self.map.y_coords.append(key) | |
if value not in self.map.grids: | |
self.map.grids[value] = [self.x_coord, key] | |
super(MapRow, self).__setitem__(key, value) | |
class Map(dict): | |
def __init__(self, *args, **kwargs): | |
super(Map, self).__init__(self, *args, **kwargs) | |
self.x_coords, self.y_coords = [], [] | |
self.grids= {} | |
def __setitem__(self, key, value): | |
if not isinstance(value, MapRow): | |
raise Exception("Map rows must be MapRow instances.") | |
if key not in self: | |
self.x_coords.append(key) | |
super(Map, self).__setitem__(key, value) | |
def __getitem__(self, key): | |
if key not in self: | |
self[key] = MapRow(self, key) | |
return super(Map, self).__getitem__(key) | |
def sum(self, map_obj): | |
"""Find intersection between maps, recalculate coords and sum them""" | |
if map_obj.grids: | |
current_grids = set(self.grids.keys()) | |
new_grids = set(map_obj.grids.keys()) | |
grid_intersection = list(current_grids & new_grids) | |
if grid_intersection: | |
common_grid = grid_intersection[0] | |
x_difference = self.grids[common_grid][0] - map_obj.grids[common_grid][0] | |
y_difference = self.grids[common_grid][1] - map_obj.grids[common_grid][1] | |
different_grids = list(new_grids - current_grids) | |
for grid_name in different_grids: | |
grid_obj = map_obj.grids[grid_name] | |
new_x, new_y = grid_obj[0] + x_difference, grid_obj[1] + y_difference | |
self[new_x][new_y] = grid_name | |
def render(self): | |
"""Render map into image""" | |
x_coords = sorted(self.x_coords) | |
y_coords = sorted(self.y_coords) | |
final_map = Image.new("RGB", (CHUNK_SIZE[0]*len(x_coords), CHUNK_SIZE[1]*len(y_coords))) | |
for x_num, x in enumerate(x_coords): | |
for y_num, y in enumerate(y_coords): | |
if y in self[x]: | |
filename = self[x][y] | |
x_pos = x_num * CHUNK_SIZE[0] | |
y_pos = y_num * CHUNK_SIZE[1] | |
map_chunk = Image.open("%s%s.png" % (MAP_DIR, filename)) | |
final_map.paste(map_chunk, (x_pos, y_pos)) | |
final_map.save(MAP_DIR+RESULT_FILENAME) | |
MAP_DIR = "/Users/dgl/havenandhearth/maps/" | |
CHUNK_SIZE = (100, 100) | |
RESULT_FILENAME = "map.png" | |
global_map = Map() | |
data_files = sorted(glob.glob(MAP_DIR + "fragdata*.txt")) | |
for data_file in data_files: | |
with open(data_file, "r") as f: | |
local_map = Map() | |
for line in f.xreadlines(): | |
filename, x, y = line.split(";") | |
local_map[int(x)][int(y)] = filename | |
if not global_map: | |
global_map = local_map | |
else: | |
global_map.sum(local_map) | |
global_map.render() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment