-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmonones.pl
executable file
·424 lines (384 loc) · 10.3 KB
/
monones.pl
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#! /usr/bin/env perl
# Copyright (C) 2011–2016 Alex Schroeder <alex@gnu.org>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
use Mojolicious::Lite;
use POSIX qw(INT_MAX);
use Modern::Perl;
use Class::Struct;
use List::Util qw(min max);
use Memoize;
use SVG;
use Math::Geometry::Voronoi;
use Math::Fractal::Noisemaker;
my $points = 3000;
my $width = 1000;
my $height = 550;
my $center_x = $width / 2;
my $center_y = $height / 2;
my $radius = 500;
my %color = (beach => '#a09077',
grass => '#88aa55',
forest => '#679459',
taiga => '#99aa77',
tundra => '#bbbbaa',
snow => '#ffffff',
glacier => '#eeeeee',
bare => '#888888',
ocean => '#44447a',
coast => '#55559a',
lake => '#336699',
swamp => '#337755', );
struct World => { points => '@',
centroids => '@', # matches points
voronoi => '$',
height => '@', # matches points
terrain => '@', # matches points
border => '@', # matches points
visible => '@', # matches points
water => '@', # matches points
downslope => '@', # matches points
};
sub neighbours {
my ($world, $i) = @_;
my @points;
foreach my $line (@{$world->voronoi->lines}) {
push(@points, $line->[3]) if $i == $line->[4];
push(@points, $line->[4]) if $i == $line->[3];
}
return \@points;
}
memoize('neighbours');
sub add_border {
my ($world) = @_;
foreach my $polygon ($world->voronoi->polygons) {
my ($i, @corners) = @$polygon; # see Math::Geometry::Voronoi
my $visible = 0;
my $border = 0;
foreach my $corner (@corners) {
my $x = $corner->[0];
my $y = $corner->[1];
if (!$visible and $x >= 0 and $y >= 0 and $x <= $width and $y <= $height) {
$visible = 1;
}
if ($x < 0 or $y < 0 or $x > $width or $y > $height) {
$border = 1;
last if $visible; # got all the info
}
}
$world->border($i, $border);
$world->visible($i, $visible);
}
}
sub add_random_points {
my ($world) = @_;
for my $i (1 .. $points) {
push(@{$world->points}, [rand($width), rand($height)]);
};
return $world;
}
sub add_voronoi {
my ($world) = @_;
$world->voronoi(Math::Geometry::Voronoi->new(points => $world->points));
$world->voronoi->compute;
}
sub add_centroids {
my ($world) = @_;
$world->centroids([]); # clear
foreach my $polygon ($world->voronoi->polygons) {
push(@{$world->centroids}, centroid($polygon));
}
}
sub centroid {
my ($cx, $cy) = (0, 0);
my $A = 0;
my $polygon = shift;
my ($point_index, @points) = @$polygon; # see Math::Geometry::Voronoi
my $point = $points[$#points];
my ($x0, $y0) = ($point->[0], $point->[1]);
for $point (@points) {
my ($x1, $y1) = ($point->[0], $point->[1]);
$cx += ($x0 + $x1) * ($x0 * $y1 - $x1 * $y0);
$cy += ($y0 + $y1) * ($x0 * $y1 - $x1 * $y0);
$A += ($x0 * $y1 - $x1 * $y0);
($x0, $y0) = ($x1, $y1);
}
$A /= 2;
$cx /= 6 * $A;
$cy /= 6 * $A;
return [$cx, $cy, $point_index];
}
sub add_height {
my $world = shift;
$Math::Fractal::Noisemaker::QUIET = 1;
my $grid = Math::Fractal::Noisemaker::square();
$world->height([]); # clear
my $scale = max($height, $width); # grid is a square
foreach my $point (@{$world->points}) {
my $x = int($point->[0]*255/$scale);
my $y = int($point->[1]*255/$scale);
my $h = 0; # we must not skip any points!
$h = $grid->[$x]->get($y) / 255
unless $x < 0 or $y < 0 or $x > 255 or $y > 255;
push(@{$world->height}, $h);
}
}
sub raise_point {
my ($world, $x, $y, $radius) = @_;
my $i = 0;
foreach my $point (@{$world->points}) {
my $dx = $point->[0] - $x;
my $dy = $point->[1] - $y;
my $d = sqrt($dx * $dx + $dy * $dy);
my $v = max(0, $world->height->[$i] - $d / $radius);
$world->height($i, $v);
$i++;
}
}
sub scale_height {
my ($world) = shift;
my $top = 0;
for my $i (0 .. $#{$world->points}) {
$top = $world->height->[$i] if $world->height->[$i] > $top;
}
for my $i (0 .. $#{$world->points}) {
$world->height($i, $world->height->[$i] / $top);
}
}
sub add_ocean {
my $world = shift;
my @queue;
for my $i (0 .. $#{$world->points}) {
$world->terrain($i, '');
if ($world->height->[$i] <= 0) {
$world->water($i, 1);
if ($world->border->[$i]) {
$world->terrain($i, 'ocean');
push(@queue, $i); # this is used to spread the ocean
} else {
$world->terrain($i, 'lake'); # inland sea is considered a lake
}
}
}
while (@queue) {
my $p = shift(@queue);
foreach my $i (neighbours($world, $p)) {
if ($world->water->[$i] and $world->terrain->[$i] ne 'ocean') {
$world->terrain($i, 'ocean');
push(@queue, $i);
}
}
}
}
sub add_downslopes {
my $world = shift;
for my $i (0 .. $#{$world->points}) {
next if $world->terrain->[$i] eq 'ocean';
my $lowest;
foreach my $neighbour (neighbours($world, $i)) {
if (not $lowest or $world->height->[$neighbour] < $world->height->[$i]) {
$lowest = $neighbour;
}
}
if ($world->height->[$lowest] < $world->height->[$i]) {
$world->downslope($i, $lowest);
}
}
}
sub add_lakes {
my $world = shift;
for my $i (0 .. $#{$world->points}) {
next if $world->water->[$i]; # lakes, oceans, coast, all at height 0
next if $world->downslope->[$i];
my @lake = ($i);
$world->fill_lake($world->height->[$i], @lake);
}
}
sub fill_lake {
my ($world, $level, @lake) = @_;
my %lake = map { $_ => 1 } @lake;
my $lowest;
foreach my $i (@lake) {
foreach my $neighbour (neighbours($world, $i)) {
if (not $lake{$neighbour}
and (not $lowest or $world->height->[$neighbour] < $world->height->[$lowest])) {
$lowest = $neighbour;
}
}
}
foreach my $i (@lake) {
$world->height($i, $world->height->[$lowest]);
}
if ($world->height->[$lowest] < $level) {
my @color = qw(swamp lake lake glacier);
my $color = $color[level($level)];
foreach my $i (@lake) {
$world->water($i, 1);
$world->terrain($i, $color);
$world->downslope($i, $lowest);
}
} else {
return $world->fill_lake($world->height->[$lowest], @lake, $lowest);
}
}
sub add_terrain {
my $world = shift;
my @color = qw(grass forest taiga snow);
foreach my $i (0 .. $#{$world->points}) {
if (not $world->water->[$i]) {
$world->terrain($i, $color[level($world->height->[$i])]);
}
}
}
sub level {
my $height = shift;
# The highest point is exactly 1.0; the lost point is still higher
# than 0. Return results between 0 and 3.
return int(min($height * 4, 3));
}
sub add_beach {
my $world = shift;
foreach my $i (0 .. $#{$world->points}) {
# next if $world->terrain->[$i] eq 'ocean';
next if $world->terrain->[$i] ne 'grass';
foreach my $neighbour (neighbours($world, $i)) {
if ($world->terrain->[$neighbour] eq 'coast') {
$world->terrain($i, 'beach');
last;
}
}
}
}
sub add_coast {
my $world = shift;
foreach my $i (0 .. $#{$world->points}) {
next if $world->terrain->[$i] ne 'ocean';
foreach my $neighbour (neighbours($world, $i)) {
if (not $world->water->[$neighbour]) {
$world->terrain($i, 'coast');
last;
}
}
}
}
sub svg {
my $world = shift;
my $svg = new SVG(width => $width . "px",
height => $height . "px");
foreach my $polygon ($world->voronoi->polygons) {
my ($i, @points) = @$polygon; # see Math::Geometry::Voronoi
if ($world->visible->[$i]) {
my $color = $color{$world->terrain->[$i]};
my $path = join(",", map { map { int } @$_ } @points);
$svg->polygon(points => $path,
fill => $color,
style => { 'stroke-width' => 1,
'stroke' => 'black'});
}
}
return $svg->xmlify();
}
get '/' => 'main';
get '/help';
get '/source' => sub {
my $c = shift;
seek(DATA,0,0);
local $/ = undef;
$c->render(text => <DATA>, format => 'txt');
};
get '/random' => sub {
my $c = shift;
my $seed = int(rand(INT_MAX));
return $c->redirect_to("/$seed");
};
get '/:seed' => [seed => qr/\d+/] => sub {
my $c = shift;
# initialize srand
srand $c->param('seed');
# generate voronoi
my $world = World->new;
add_random_points($world, $points);
add_voronoi($world);
for (my $i = 2; $i--; ) {
# Lloyd Relaxation
add_centroids($world);
$world->points($world->centroids);
add_voronoi($world);
}
# create island
add_border($world);
add_height($world);
raise_point($world, $center_x, $center_y, $radius);
scale_height($world);
add_ocean($world);
add_coast($world);
add_downslopes($world);
add_lakes($world);
add_terrain($world);
add_beach($world);
$c->render(text => svg($world), format => 'svg');
};
app->start;
__DATA__
@@ main.html.ep
% layout 'default';
% title 'Monones';
<h1>Monones Island Generator</h1>
<p>This web application generates an island.</p>
%= form_for random => (method => 'GET') => begin
%= submit_button 'Submit', name => 'submit'
%= end
</p>
@@ help.html.ep
% layout 'default';
% title 'Monones Help';
<h1>Monones Island Generator Help</h1>
<p>Currently, everything is in flux.</p>
@@ render.svg.ep
@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
%= stylesheet '/monones.css'
%= stylesheet begin
body {
padding: 1em;
font-family: "Palatino Linotype", "Book Antiqua", Palatino, serif;
}
textarea {
width: 100%;
}
table {
padding-bottom: 1em;
}
td, th {
padding-right: 0.5em;
}
.example {
font-size: smaller;
}
% end
<meta name="viewport" content="width=device-width">
</head>
<body>
<%= content %>
<hr>
<p>
<a href="https://campaignwiki.org/monones">Monones</a> 
<%= link_to 'Help' => 'help' %> 
<%= link_to 'Source' => 'source' %> 
<a href="https://alexschroeder.ch/cgit/hex-mapping/about/#monones">Git</a> 
<a href="https://alexschroeder.ch/wiki/Contact">Alex Schroeder</a>
</body>
</html>