-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_map_asm.py
84 lines (64 loc) · 1.79 KB
/
generate_map_asm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python3
from PIL import Image
from itertools import product
from math import sqrt
from random import shuffle, randint
FILE_HEADER = r"""
; ===== AUTOGENERATED FILE =====
%ifndef WORLDMAP_S
%define WORLDMAP_S
%assign n_worldmap_pixels 0
%macro worldmap_pixel 2
dw %1
db %2 ; y only needs 0-200
%assign n_worldmap_pixels n_worldmap_pixels + 1
%endmacro
worldmap:
"""
FILE_FOOTER = r"""
%endif
"""
def get_vga_color(r, g, b):
r = int(round(r / 255))
g = int(round(g / 255))
b = int(round(b / 255))
if (r, g, b) == (0, 0, 0): # black (ish)
return 0
if (r, g, b) == (1, 1, 1): # white (ish)
return 0x1f
raise ValueError('No implemented color for {}, {}, {}'.format(r, g, b))
def dist(a, b):
ax, ay = a
bx, by = b
return sqrt((ax - bx) ** 2 + (ay - by) ** 2)
def order_for_render(map_pixels):
"""
Basically, keep jumping to the closest thing. Looks kinda like a CRT beam!
"""
last_x, last_y = 0, 0
ordered = []
while map_pixels:
closest = min(map_pixels, key=lambda px: dist((px[0], px[1]), (last_x, last_y)))
map_pixels.remove(closest)
ordered.append(closest)
last_x, last_y = closest[0], closest[1]
return ordered
im = Image.open('./world.png')
map_pixels = []
for x, y in product(range(im.width), range(im.height)):
r, g, b = im.getpixel((x, y))
try:
color = get_vga_color(r, g, b)
except:
raise ValueError('at {} {}'.format(x, y))
if color != 0: # don't put black pixels, they're in the screen clear
map_pixels.append((
x,
y,
color
))
map_pixels = order_for_render(map_pixels)
print(FILE_HEADER)
for x, y, color in map_pixels:
print('worldmap_pixel {}, {}'.format(x, y))
print(FILE_FOOTER)