Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- save_file = "C:/Users/Geoffrey/Documents/Paradox Interactive/Hearts of Iron III/WW1Modv1.0/save games/German_Empire1914_12_28_02.hoi3"
- common = "D:/Games/SteamLibrary/steamapps/common/Hearts of Iron 3/tfh/mod/WW1Mod_v1.0/common/"
- map_path = "D:/Games/SteamLibrary/steamapps/common/Hearts of Iron 3/tfh/mod/WW1Mod_v1.0/map/"
- bmp_path = map_path + "provinces.bmp"
- map_def_path = map_path + "definition.csv"
- #remove quotation marks, and try to convert to int or float
- def tidy(s):
- if len(s) >= 2:
- if s[0] == '"' and s[-1] == '"':
- s = s[1:-1]
- if '.' in s:
- try:
- float(s)
- except ValueError:
- return s
- return float(s)
- else:
- try:
- int(s)
- except ValueError:
- return s
- return int(s)
- delim = [' ', '\n', '\t']
- def parse(f, d):
- #print "parse"
- "read first non-space char"
- b = f.read(1)
- while b in delim:
- b = f.read(1)
- "exit if close bracket"
- if b == '}':
- #print 1
- return 1
- "exit if end of file"
- if b == '':
- #print 2
- return 2
- if b == '{':
- sub_d = []
- while not parse(f, sub_d):
- pass
- d.append(sub_d)
- #print 0
- return 0
- if b == '#':
- while not b == '\n':
- b = f.read(1)
- parse(f, d)
- #print 4
- return 0
- "read first item (string)"
- name = b
- #print "name:",b
- b = f.read(1)
- while not (b == '=' or b in delim):
- name += b
- #print "n:",b
- b = f.read(1)
- if b in delim:
- while b in delim:
- b = f.read(1)
- if b == '=':
- pass
- "no '=' means this is a space-seperated list"
- else:
- name = tidy(name)
- d.append(name)
- name = ""
- while not b == '}':
- if b in delim:
- if len(name) > 0:
- name = tidy(name)
- d.append(name)
- name = ""
- else:
- name += b
- b = f.read(1)
- #print "l:", d, type(d)
- #print 3
- return 3
- "read content"
- b = f.read(1)
- while b in delim:
- b = f.read(1)
- if b == '{':
- sub_d = []
- while not parse(f, sub_d):
- pass
- name = tidy(name)
- d.append((name,sub_d))
- #print 5
- return 0
- else:
- content = b
- #print "content:",b
- if b == '"':
- b = f.read(1)
- content += b
- while not b == '"':
- b = f.read(1)
- content += b
- else:
- b = f.read(1)
- while not b in delim:
- content += b
- #print "c:",b
- b = f.read(1)
- name = tidy(name)
- content = tidy(content)
- d.append((name,content))
- #print 6
- return 0
- def show_data(d, depth = 0):
- if type(d) == tuple:
- print " " * depth, d[0], ":"
- show_data(d[1], depth+1)
- elif type(d) == int or type(d) == float or type(d) == str:
- print " " * depth, d
- elif len(d) == 0:
- print " " * depth, d
- elif type(d[0]) == tuple:
- for t in d:
- show_data(t, depth + 1)
- else:
- print " " * depth, d
- def file_data(d, f_out, depth = 0):
- indent = " " * depth
- if type(d) == tuple:
- f_out.write(indent+str(d[0])+":\n")
- file_data(d[1], f_out, depth + 1)
- elif type(d) == int or type(d) == float or type(d) == str:
- f_out.write(indent+str(d)+"\n")
- elif len(d) == 0:
- f_out.write(indent+str(d)+"\n")
- elif type(d[0]) == tuple:
- for t in d:
- file_data(t, f_out, depth + 1)
- else:
- f_out.write(indent+str(d)+"\n")
- def get_country_data(data, country_name):
- country = [x for x in data if x[0] == country_name]
- return country[0]
- army_ranks = ["theatre", "army", "corps", "division", "regiment"]
- army_filter = ["name", "id", "leader", "type", "location",
- "strength", "organisation", "experience"]
- def filter_army(army_data):
- info = [x for x in army_data[1] if x[0] in army_filter]
- info = [(x[0], x[1][0][1]) if x[0] == "id" or x[0] == "leader"
- else x for x in info]
- subordinate = [filter_army(x) for x in army_data[1]
- if x[0] in army_ranks]
- return (army_data[0], info + subordinate)
- def get_army(country_data):
- country_data = country_data[1]
- army = [x for x in country_data if x[0] in army_ranks]
- return [filter_army(x) for x in army]
- save = []
- with open(save_file) as f:
- while not parse(f, save):
- pass
- #print data
- #show_data(data, -1)
- #g = [x for x in data if x[0] == "GER"]
- #with open("dump.txt", 'w') as write_file:
- # file_data(g, write_file)
- ger = get_country_data(save, "GER")
- print len(ger)
- file_data(get_army(ger), open("army.txt", 'w'))
- from PIL import Image
- def get_pixels(bmp):
- pixels = list(bmp.getdata())
- width, height = bmp.size
- pixels = [pixels[i * width:(i + 1) * width] for i in xrange(height)]
- return pixels
- def read_map_def(file_name):
- num_to_rgb, rgb_to_num = {}, {}
- with open(file_name, 'r') as f:
- for line in f.readlines()[1:]:
- l = line.strip().split(";")
- if len(l[0]) > 0:
- [num, r, g, b] = [int(x) for x in l[:4]]
- num_to_rgb[num] = (r,g,b)
- rgb_to_num[(r,g,b)] = num
- return num_to_rgb, rgb_to_num
- map_bmp = Image.open(bmp_path)
- pixels = [map_bmp.getpixel((i,0)) for i in xrange(map_bmp.size[0])]
- print map_bmp.format, map_bmp.size, map_bmp.mode
- pixels = get_pixels(map_bmp)
- print len(pixels[0]), len(pixels), pixels[0][0], pixels[0][1]
- num_to_rgb, rgb_to_num = read_map_def(map_def_path)
- print len(num_to_rgb)
- print pixels[0].count(num_to_rgb[13442])
- def get_controllers(save):
- num = [x for x in save if type(x[0]) == int]
- land = [(x[0], x[1][2][1]) for x in num if x[1][2][0] == "controller"]
- d = {}
- for province in land:
- d[province[0]] = province[1]
- return d
- controller_dict = get_controllers(save)
- countries_def = common + "countries.txt"
- countries_files = []
- with open(countries_def, 'r') as f:
- while not parse(f, countries_files):
- pass
- file_data(countries_files, open("colours.txt", 'w'))
- countries_paths = {}
- for country in countries_files:
- countries_paths[country[0]] = common + country[1]
- countries_colours = {}
- for k, v in countries_paths.iteritems():
- with open(v, 'r') as f:
- c = [("no",0)]
- while not c[0][0] == "color":
- c = []
- parse(f, c)
- countries_colours[k] = tuple(c[0][1])
- print countries_colours["GER"]
- old_pixels = [[x for x in row] for row in pixels]
- """
- test_colour = [(84, 71, 85)]
- for x in xrange(map_bmp.size[0]):
- for y in xrange(map_bmp.size[1]):
- if old_pixels[y][x] in test_colour:
- map_bmp.putpixel((x,y),(0,0,0))
- print (x,y)
- for x in xrange(398):
- for y in xrange(267):
- map_bmp.putpixel((x,y), (0,0,0))
- """
- ignore_colour = [(255,255,255)] #lakes
- #(10,8,245), #at (398, 267)
- #(156, 62, 74), #at (1782, 423)
- #(84, 71, 85)] #at (1574, 474)
- for y in xrange(map_bmp.size[1]):
- if y % 100 == 0:
- print "y:",y
- for x in xrange(map_bmp.size[0]):
- try:
- old_colour = old_pixels[y][x]
- if not old_colour in ignore_colour:
- province = rgb_to_num[old_colour]
- try:
- country = controller_dict[province]
- new_colour = countries_colours[country]
- map_bmp.putpixel((x, y), new_colour)
- except KeyError as e:
- #print e
- map_bmp.putpixel((x, y), (255,255,255))
- except TypeError as e:
- print country, new_colour, province
- print e
- except KeyError as e:
- print e, "at", (x, y)
- map_bmp = map_bmp.transpose(Image.FLIP_TOP_BOTTOM)
- map_bmp.save("test.bmp")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement