-
Notifications
You must be signed in to change notification settings - Fork 0
/
genreg.rb
424 lines (327 loc) · 10.2 KB
/
genreg.rb
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
require "prawn"
require "active_support/inflector"
require "./lib/plasma_fractal"
require "./lib/drainage"
require "./lib/erosion"
require "./lib/terrain"
require "./lib/fractal"
$number_of_locations = 45
sin60 = 0.866025404
$C = 10
$A = 0.5*$C
$B = $C*sin60
$hex_bases = [ [0, $B], [$A, 0], [$A+$C, 0], [2*$C, $B], [$A+$C, 2*$B], [$A, 2*$B]]
$width = 30
$height = 40
$max_height = $height*(2*$B)
$hbias = 0.0
$vbias = 0.0
$total_bias = ($hbias.abs.to_f+$vbias.abs.to_f)/2.0
def generate_ruin_or_lair(lmap, x,y)
roll = rand(30)
type = ""
case roll
when 0..3
type = "megadungeon"
when 3..13
type = "dungeon"
else
type = "lair"
end
lmap[x][y] = type
end
def generate_hex(lmap, tmap, x, y)
if tmap[x][y] == "sea"
return
end
number_of_locations = 45.0/1200.0 * ($width * $height)
if rand($width*$height) <= number_of_locations
roll = rand(100)
vbias = ((y.to_f/$height.to_f*$vbias) + (-1*($vbias/2.0)))
hbias = ((x.to_f/$width.to_f*$hbias)+ (-1*($hbias/2.0)))
if $total_bias == 0
bias = 0
else
bias = (vbias.abs.to_f/$total_bias)*vbias.to_f + (hbias.abs.to_f/$total_bias)*hbias.to_f
end
roll += bias
if (roll <= 33)
lmap[x][y] = "settlement"
else
generate_ruin_or_lair(lmap, x, y)
end
end
end
def hex_to_screen(x, y)
if x % 2 == 0
x_offset = x * ($A+$C)
y_offset = y * (2*$B)
else
x_offset = x * ($A+$C)
y_offset = (y * (2*$B)) - $B
end
y_offset = $max_height - y_offset
return x_offset, y_offset
end
def draw_rivers(pdf, location, terrain, drainage, river, x, y)
x_offset, y_offset = hex_to_screen(x, y)
coords = $hex_bases.map { | coordinate| [coordinate[0]+x_offset, coordinate[1]+y_offset] }
if river
i = river[0]
j = river[1]
if x % 2 != 0
if i == x and j < y
angle = 90
elsif i > x and j < y
angle = 30
elsif i > x and j == y
angle = 330
elsif i == x and j > y
angle = 270
elsif i < x and j == y
angle = 210
else i < x and j < y
angle = 150
end
else
if i == x and j < y
angle = 90
elsif i > x and j == y
angle = 30
elsif i > x and j > y
angle = 330
elsif i == x and j > y
angle = 270
elsif i < x and j > y
angle = 210
else i < x and j == y
angle = 150
end
end
radians = angle * Math::PI/180
to_x, to_y = hex_to_screen(river[0], river[1])
from_x = x_offset + $B
from_y = y_offset + $C
to_x += $B
to_y += $C
if i != x or j != y
y_coords = linear_midpoint_displacement(from_x, from_x+17, 17)
pdf.move_to(from_x, from_y)
last_coords = nil
pdf.rotate(angle, :origin => [from_x, from_y]) do
pdf.stroke do
y_coords.each_with_index do |y_coord, index|
pdf.join_style = :round
pdf.stroke_color "0000ff"
pdf.line_width 2
pdf.line_to(from_x+index, from_y+y_coord)
last_coords = [from_x+index, from_y+y_coord]
end
pdf.line_to(last_coords[0]+2, last_coords[1]+2)
pdf.stroke
end
end
pdf.stroke_color "000000"
end
end
end
def draw_hex(pdf, location, terrain, drainage, river, x, y)
x_offset, y_offset = hex_to_screen(x, y)
coords = $hex_bases.map { | coordinate| [coordinate[0]+x_offset, coordinate[1]+y_offset] }
pdf.stroke_polygon *coords
if terrain != nil
x_pos = x_offset+$C
y_pos = y_offset+$B
if terrain == "mountain"
pdf.image "#{settings.root}/images/mountains.png", :at => [x_pos-7, y_pos+6], :fit => [$A+$B,$A+$B]
elsif terrain == "forested_mountain"
pdf.image "#{settings.root}/images/forestedmountains.png", :at => [x_pos-7, y_pos+6], :fit => [$A+$B,$A+$B]
elsif terrain == "hill"
pdf.image "#{settings.root}/images/hills.png", :at => [x_pos-7, y_pos+6], :fit => [$A+$B,$A+$B]
elsif terrain == "forested_hill"
pdf.image "#{settings.root}/images/forestedhills.png", :at => [x_pos-7, y_pos+6], :fit => [$A+$B,$A+$B]
elsif terrain == "grassy_hill"
pdf.image "#{settings.root}/images/grassyhills.png", :at => [x_pos-7, y_pos+6], :fit => [$A+$B,$A+$B]
elsif terrain == "forest"
pdf.image "#{settings.root}/images/heavyforest.png", :at => [x_pos-7, y_pos+6], :fit => [$A+$B,$A+$B]
elsif terrain == "grassland"
#pdf.image "#{settings.root}/images/grassland.png", :at => [x_pos-7, y_pos+6], :fit => [$A+$B,$A+$B]
elsif terrain == "woods"
pdf.image "#{settings.root}/images/lightforest.png", :at => [x_pos-7, y_pos+6], :fit => [$A+$B,$A+$B]
elsif terrain == "swamp"
pdf.image "#{settings.root}/images/marsh.png", :at => [x_pos-7, y_pos+6], :fit => [$A+$B,$A+$B]
elsif terrain == "plain"
#pdf.image "#{settings.root}/images/hills.png", :at => [x_pos-7, y_pos+6], :fit => [$A+$B,$A+$B]
elsif terrain == "sea" or terrain == "lake"
pdf.fill_color "0000ff"
pdf.fill_polygon *coords
pdf.fill_color "000000"
end
if location == "dungeon"
pdf.image "#{settings.root}/images/dungeon.png", :at => [x_pos-6, y_pos+6], :fit => [$B+$A/2,$B+$A/2]
elsif location == "megadungeon"
pdf.image "#{settings.root}/images/dungeon.png", :at => [x_pos-5, y_pos+8], :fit => [$B+$A,$B+$A]
elsif location == "lair"
pdf.image "#{settings.root}/images/cave.png", :at => [x_pos-4, y_pos+5], :fit => [$B,$B]
elsif location == "settlement"
pdf.stroke_rectangle [x_pos-2.5, y_pos+2.5], 5, 5
end
end
pdf.font_size(4)
pdf.draw_text "%02d" % x + "%02d" % y,
:at => [x_offset+$C-4, y_offset+$B-7]
end
def generate_pdf(pdf, hmap, lmap, tmap, dmap, rmap)
map_each(lmap, 1, 3) do |x, y, cell|
draw_rivers(pdf, lmap[x][y], tmap[x][y], dmap[x][y], rmap[x][y], x, y)
end
map_each(lmap, 1, 3) do |x, y, cell|
draw_hex(pdf, lmap[x][y], tmap[x][y], dmap[x][y], rmap[x][y], x, y)
end
#map_each(hmap, 1, 3) do |x, y, cell|
# x_offset, y_offset = hex_to_screen(x, y)
# pdf.draw_text cell.to_i.to_s, :at => [x_offset+$C, y_offset+$B]
#end
pdf.start_new_page
pdf.move_down 10
pdf.font_size(12)
pdf.text "Region Key"
location_map = {}
pdf.column_box([0, pdf.cursor],:columns => 2, :width => 500) do
for x in 1..lmap.size-3
for y in 1..lmap[0].size-3
if lmap[x][y] != ""
pdf.text "Hex " + ("%02d" % x + "%02d" % y) +" - a " + lmap[x][y]
location_map.merge!({ lmap[x][y] => 1 }) { |key, old_count, new_count| old_count + new_count }
end
end
end
pdf.move_down 10
pdf.text "Summary"
location_map.each_pair do |k,v|
pdf.text v.to_s + " " + k.pluralize
end
end
puts "Done generating pdf."
pdf
end
def determine_heightmap(hmap)
pfsize = 1
while (2**pfsize + 1) < $height or (2**pfsize + 1) < $width
puts "plasma fractal size is " + pfsize.to_s
pfsize += 1
end
puts "plasma fractal size is " + pfsize.to_s + " => " + ((2**pfsize)+1).to_s
fractal = PlasmaFractal.new(:size =>(2**pfsize)+1, :height_seed => 100)
fractal.generate!
map_each(hmap) do |x,y,cell|
hmap[x][y] = fractal.data[x][y]
end
$mountain_threshold = 0.90 * fractal.max.to_f
$hill_threshold = 0.85 * fractal.max.to_f
$plain_threshold = 0.60 * fractal.max.to_f
$max_terrain_height = fractal.max.to_f
end
def generate_map_pdf(pdf, width, height, axis, strength, terrain)
if $width > 50
$width = 50
end
if $height > 50
$height = 50
end
$width = width + 2
$height = height + 2
puts "Request width " + width.to_s + " and height " + height.to_s
if axis == "none"
elsif axis == "north"
$vbias = strength
$hbias = 0.0
elsif axis == "northeast"
$hbias = -1 * strength/2.0
$vbias = strength/2.0
elsif axis == "east"
$hbias = -1 * strength
$vbias = 0
elsif axis == "southeast"
$hbias = -1 * strength/2.0
$vbias = -1 * strength/2.0
elsif axis == "south"
$hbias = 0.0
$vbias = -1 * strength
elsif axis == "southwest"
$hbias = strength/2.0
$vbias = -1 * strength/2.0
elsif axis == "west"
$hbias = strength
$vbias = 0.0
elsif axis == "northwest"
$hbias = strength/2.0
$vbias = strength/2.0
end
puts "hbias = " + $hbias.to_s
puts "vbias = " + $vbias.to_s
$total_bias = ($hbias.abs.to_f+$vbias.abs.to_f)/2.0
lmap = Array.new($width) { Array.new($height) { "" } }
hmap = Array.new($width) { Array.new($height) { 0.0 }}
drainage_map = Array.new($width) { Array.new($height) { [-1, -1] }}
wmap = Array.new($width) { Array.new($height) { 0.0 }}
smap = Array.new($width) { Array.new($height) { 0.0 }}
flow_map = Array.new($width) { Array.new($height) { 1.0 }}
rmap = Array.new($width) { Array.new($height) }
tmap = Array.new($width) { Array.new($height) { "plain" }}
determine_heightmap(hmap)
puts "Eroding 100 times..."
100.times { Erosion::erode(hmap, wmap, smap) }
puts "height map"
print_map(hmap)
calculate_drainage(hmap, drainage_map)
calculate_flow_accumulation(drainage_map, flow_map)
determine_rivers(flow_map, drainage_map, rmap, 6.0)
puts "drainage map"
print_map(drainage_map)
puts "\n"
puts "flow accumulation map"
print_map(flow_map)
puts "\n"
puts "river map"
print_map(rmap)
puts "\n"
puts "water map"
print_map(wmap)
puts "\n"
if terrain == 1
determine_terrain(hmap, flow_map, wmap, tmap)
end
display_map_terrain(tmap)
map_each(hmap) do |x, y, cell|
generate_hex(lmap, tmap, x, y)
end
return generate_pdf(pdf, hmap, lmap, tmap, flow_map, rmap)
end
def display_map_terrain(tmap)
for y in 0..$height-1
print "\n"
for x in 0..$width-1
v = tmap[x][y]
if v == "mountain"
print " M "
elsif v == "forested_mountain"
print " F "
elsif v == "forested_hill"
print " m "
elsif v == "grassy_hill"
print " m "
elsif v == "hill"
print " m "
elsif v == "forest"
print " f "
elsif v == "plain"
print " _ "
elsif v == "sea"
print " s "
else
print " - "
end
end
end
end