-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.py
129 lines (96 loc) · 4.06 KB
/
gen.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import datetime, random, sys, numpy, pkgutil, importlib, time, os
import voronoi, graph, renderer, world
import plugins
# Configuration variables
PointCount = 8000 # default = 3500
NumWorlds = 1
def generate(world_idx):
def point_cloud(n):
return numpy.random.rand(n, 2)
def generate_world(world, vd):
available_cellprops = []
available_worldparams = []
def is_ready(plugin):
'''
Determine whether all pre-requisites are satisfied. If yes, its safe to run this plugin
given the current state of the world.
'''
genfunc = plugin.generate
if hasattr(genfunc, 'cellprops'):
for cp in genfunc.cellprops:
if cp not in available_cellprops:
return False
if hasattr(genfunc, 'worldparams'):
for wp in genfunc.worldparams:
if wp not in available_worldparams:
return False
return True
def all_plugins():
'''
Dynamically load all plugins from the `plugins/` directory.
'''
plugin_list = pkgutil.iter_modules(plugins.__path__, plugins.__name__ + '.')
modules = []
for _, name, __ in plugin_list:
pl = importlib.import_module(name)
modules.append(pl)
return modules
plugin_queue = all_plugins()
plugin_count = len(plugin_queue)
while len(plugin_queue) > 0:
available_plugins = list( filter(is_ready, plugin_queue) )
for pl in available_plugins:
print(' * [{} / {}] Running {}...\t'.format(
plugin_count - len(plugin_queue) + 1,
plugin_count,
pl.__name__
),
end='',
flush=True,
)
start_ts = time.time() * 1000
pl.generate(world, vd)
end_ts = time.time() * 1000
print( '[%.1fms]' % (round(end_ts - start_ts, 1)) )
# Once run, remove the plugin from the queue
plugin_queue.remove(pl)
available_cellprops = list( map(lambda k: k[3:], filter(lambda k: k.startswith('cp_'), world.__dict__.keys())) )
available_worldparams = world.list_params()
# Seed RNG once per iteration
seed = round( datetime.datetime.now().timestamp() * 10000 )
random.seed(seed)
print(' [------] Generating Voronoi points for world...')
points = point_cloud(PointCount)
vor = voronoi.generate(points)
cell_idxs = [idx for idx in range(PointCount)]
cell_mapping = {}
for cell_idx, v_idx in enumerate( sorted(vor.point_region) ):
cell_mapping[cell_idx] = v_idx
print(' [------] Building world graph...')
worldgraph = graph.BuildGraph(cell_idxs, vor, cell_mapping)
w = world.World(cell_idxs, vor, worldgraph)
vd = voronoi.VoronoiDiagram(vor, cell_mapping)
# Generate the world
print(' [{}] Generating world #{}...'.format(w.id, world_idx + 1))
generate_world(w, vd)
## Render one or more images
print(' [{}] Rendering world...'.format(w.id))
folder = 'gallery/%s' % (str(datetime.datetime.now()),)
os.mkdir(folder)
render_opts = renderer.RenderOptions()
render_opts.filename = 'sample.png'
# render_opts.filename = 'batch/{}.png'.format(w.id)
print(' * Rendering png ({})...'.format(render_opts.filename))
renderer.print_render(w, vd, render_opts)
# print_render_opts = renderer.RenderOptions()
# print_render_opts.filename = 'print.svg'
# print(' * Rendering svg...')
# renderer.print_render(w, vd, print_render_opts)
if __name__ == '__main__':
if len(sys.argv) > 1:
NumWorlds = int(sys.argv[1])
print('num_worlds={}, num_points={}'.format(NumWorlds, PointCount))
print('')
print('Generating world(s)...')
for idx in range(NumWorlds):
generate(idx)