-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenner.cs
598 lines (548 loc) · 15.4 KB
/
Genner.cs
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
using System;
using System.Linq;
using System.Drawing;
using OpenTK;
using System.Collections.Generic;
using EmergeTk.Model;
using SuperFunCon;
using EmergeTk.Model.Providers;
namespace terrain
{
public class Genner
{
int w, h;
Bitmap b;
Graphics g;
int seed = (int)DateTime.Now.Ticks;
Random r;
Color blue = Color.FromArgb (0, 0, 255);
Color river = Color.FromArgb (25, 255, 255);
Color green = Color.FromArgb (0, 200, 0);
Color darkGreen = Color.FromArgb (0, 100, 0);
Color white = Color.FromArgb (255, 255, 255);
Color town = Color.FromArgb (255, 255, 0);
Color[] colorsToPreserve;
Vector2 min, max;
List<Triple<Vector2>> rivers = new List<Triple<Vector2>> ();
Point[] directions = new Point[] {
new Point (-1,-1), new Point (-1,0), new Point (-1,1),
new Point (0,-1), /*new Point(0,0),*/ new Point (0,1),
new Point (1,-1), new Point (1,0), new Point (1,1) };
Dictionary<int,List<Point>> riverPaths =new Dictionary<int, List<Point>>();
int[,] riverMap = null;
Hex[,] hexMap = null;
public Bitmap Bitmap {
get {
return this.b;
}
set {
b = value;
}
}
public int Seed {
get {
return this.seed;
}
set {
seed = value;
}
}
public Genner (int w,int h)
{
this.w = w;
this.h = h;
colorsToPreserve = new Color[]{white, river};
min = new Vector2 (0,0);
max = new Vector2 (w - 1,h - 1);
b = new Bitmap (w,h);
riverMap = (int[,])Array.CreateInstance(typeof(int), w, h);
hexMap = (Hex[,])Array.CreateInstance(typeof(Hex), w, h);
}
public void Setup ()
{
Console.WriteLine ("Random seed: " + seed.ToString ());
r = new Random (seed);
g = Graphics.FromImage (b);
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
b.SetPixel (x, y, blue);
}
}
}
public void DrawSpine ()
{
Console.WriteLine ("Drawing spine");
int x1 = r.Next (w/10, w - w/10);
int y1 = r.Next (h/10, h - h/10);
int x2 = r.Next (x1 - w/3, x1 + w/3);
int y2 = r.Next (y1 - h/3, y1 + h/3);
Vector2 v = new Vector2 (x2 - x1, y2 - y1);
Vector2 perpRight = Vector2.Normalize (v.PerpendicularRight);
Vector2 perpLeft = Vector2.Normalize (v.PerpendicularLeft);
while (x1 != x2 && y1 != y2) {
//75% chance for each axis moving closer to 2s
double d = r.NextDouble ();
x1 += (x2 > x1 ? 1 : -1) * (d > 0.4d ? 1 : -1);
d = r.NextDouble ();
y1 += (y2 > y1 ? 1 : -1) * (d > 0.4d ? 1 : -1);
x1 = Clamp (0, x1, w - 5);
y1 = Clamp (0, y1, h - 5);
Console.WriteLine ("{0},{1} -> {2},{3}", x1, y1, x2, y2);
b.SetPixel (x1, y1, white);
d = r.NextDouble ();
if (d < 0.2d) {
DrawLand (x1, y1, r.Next (200, 1200), green);
}
//1 / 200 chance of left river
//1 / 200 chance of right river
d = r.NextDouble ();
if (d < 0.04)
{
rivers.Add (new Triple<Vector2> () {
X = new Vector2 (x1, y1),
Y = perpLeft,
Z = perpRight
});
}
}
}
public void SmoothMountains ()
{
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
Color myColor = b.GetPixel(x,y);
if (myColor != white && NumPixelsIn4DirectionsAreColor(x,y,white) == 4)
{
b.SetPixel(x,y,white);
}
else if (r.Next(10) == 0)
b.SetPixel(x,y,green);
}
}
}
public int Clamp (int min, int a, int max)
{
if (a <= min)
a = min; else if (a >= max)
a = max;
return a;
}
public void DrawRivers ()
{
Console.WriteLine ("Drawing Rivers");
foreach (var triple in rivers)
DrawRiver (triple.X, triple.Y, triple.Z);
}
int HowFarUntil (Vector2 start, Vector2 direction, Color color)
{
for (int i = 0; i < h*2; i++)
{
start = Vector2.Add (start, direction);
if (start.X < 0 || start.X >= w || start.Y < 0 || start.Y >= h)
return -1;
if (b.GetPixel ((int)start.X, (int)start.Y) == color)
return i;
}
return -1;
}
int riverId = 0;
void DrawRiver (Vector2 p, Vector2 left, Vector2 right)
{
int rDist = HowFarUntil (p, right, blue);
int lDist = HowFarUntil (p, left, blue);
Vector2 direction = rDist > lDist && r.Next (3) != 0 ? left : right;
riverId++;
Vector2 currentDirection = direction;
List<Point> points = new List<Point> ();
riverPaths[riverId] = points;
bool foundSea = false;
while (!foundSea) {
float xFactor = ((float)r.NextDouble () - 0.5f);
float yFactor = ((float)r.NextDouble () - 0.5f);
if (r.Next (8) == 0)
currentDirection = Approximate (currentDirection, direction, 0.2f); else
currentDirection = Vector2.Add (currentDirection, new Vector2 (xFactor,yFactor));
currentDirection.Normalize ();
for (int i = 0; i < 1; i++) {
p = Vector2.Clamp (Vector2.Add (p, currentDirection), min, max);
Point ip = new Point((int)p.X, (int)p.Y);
Color colorAt = b.GetPixel (ip.X, ip.Y);
if (points.Contains (ip) || colorAt == white ) {
break; //try to prevent loopbacks.
}
bool usingFillPoint = false;
Point fillPoint = new Point(-10,-10);
if (points.Count > 0)
{
var lastP = points[points.Count-1];
if (lastP.X != ip.X && lastP.Y != ip.Y)
{
fillPoint = r.Next(2) == 0 ? new Point(lastP.X, ip.Y) : new Point(ip.X, lastP.Y);
Console.WriteLine ("filling ? - last: {0}, me: {1}, fill: {2}",
lastP, ip, fillPoint);
if (! points.Contains (fillPoint))
{
Console.WriteLine ("filled");
usingFillPoint = true;
}
}
}
if (usingFillPoint && riverMap[fillPoint.X,fillPoint.Y] > 0)
{
addRiverPoint (fillPoint, points, riverId, "fill");
addRiverPoint (ip, points, riverId, "ip");
foundSea = true;
break;
}
else if (colorAt == blue || riverMap[ip.X, ip.Y] > 0) {
foundSea = true;
if (usingFillPoint)
addRiverPoint (fillPoint, points, riverId, "fill");
addRiverPoint (ip, points, riverId, "ip");
break;
}
else
{
Point sea;
if (usingFillPoint && FirstPixelInSquareOfColor (fillPoint.X-1, fillPoint.Y-1, 3, blue, out sea))
{
addRiverPoint (fillPoint, points, riverId, "fill");
addRiverPoint (ip, points, riverId, "ip");
//get the square that is the blue
if (NeedFillPoint (ip, sea, out fillPoint))
addRiverPoint (fillPoint, points, riverId, "seaFill");
addRiverPoint (sea, points, riverId, "sea");
foundSea = true;
break;
}
if (FirstPixelInSquareOfColor(ip.X-1, ip.Y-1, 3, blue, out sea))
{
//found the sea next door. let's connect.
if (usingFillPoint)
addRiverPoint (fillPoint, points, riverId, "fill");
addRiverPoint (ip, points, riverId, "ip");
//get the square that is the blue
if (NeedFillPoint (ip, sea, out fillPoint))
addRiverPoint (fillPoint, points, riverId, "seaFill");
addRiverPoint (sea, points, riverId, "sea");
foundSea = true;
break;
}
if (usingFillPoint)
addRiverPoint (fillPoint, points, riverId, "fill");
addRiverPoint (ip, points, riverId, "ip");
}
}
}
}
bool NeedFillPoint (Point a, Point b, out Point c)
{
c = default(Point);
if (a.X != b.X & a.Y != b.Y)
{
c = r.Next(2) == 0 ? new Point(a.X, b.Y) : new Point(a.X, b.Y);
return true;
}
return false;
}
void addRiverPoint (Point p, List<Point> list, int riverId, string descr)
{
Console.WriteLine ("adding point {0}, river: {1} -- {2}", p, riverId, descr);
if (! list.Contains (p))
{
list.Add (p);
riverMap[p.X,p.Y] = riverId;
}
else
Console.WriteLine ("already added!");
}
public void DrawTowns ()
{
Console.WriteLine ("Drawing towns");
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
//if square is green, at least 1 river tile adj. and no towns within townSpacing, and 1/3 rand
Color c = b.GetPixel (x, y);
float baseProbability = 0.0f;
if (c == green)
{
if (riverMap[x,y] > 0 )
continue;
int numTownsFar = NumPixelsInSquareAreColor (x - 8, y - 8, 17, town);
int numTownsNear = NumPixelsInSquareAreColor (x - 3, y - 3, 7, town);
if (numTownsFar > 0)
baseProbability -= 0.4f;
if (numTownsNear > 0)
baseProbability -= 1.3f;
//if we are near a river, that is good
if (NumRiverWithin (x-1, y-1, 3) > 0)
baseProbability += 0.3f;
//next to ocean is nice too
if (NumPixelsInSquareAreColor (x - 1, y - 1, 3, blue) > 0 )
baseProbability += 0.3f;
//nice to have some forest neaby as well.
if (NumPixelsInSquareAreColor (x - 1, y - 1, 3, darkGreen) > 0)
baseProbability += 0.1f;
if (r.NextDouble () < baseProbability)
b.SetPixel (x, y, town);
}
}
}
}
public Vector2 Approximate (Vector2 v, Vector2 target, float dist)
{
if (v.X < target.X)
v.X += dist; else
v.X -= dist;
if (v.Y < target.Y)
v.Y += dist; else
v.Y -= dist;
return v;
}
public void DrawLand ()
{
int cx = r.Next (w);
int cy = r.Next (h);
int numTimes = r.Next (1000, 5000);
DrawLand (cx, cy, numTimes, green);
}
public void DrawLand (int cx, int cy, int numTimes, Color land)
{
//Console.WriteLine ("Drawing land x: {0}, y: {1}, numTimes: {2}, color: {3}",
// cx, cy,numTimes, land);
while (numTimes-- > 0) {
double going = r.NextDouble ();
if (going > 0.75d && cx > 0) //left
cx--; else if (going > 0.5d && cx < w - 1) //right
cx++; else if (going > 0.25d && cy > 0)//up
cy--; else if (cy < h - 1)
cy++;
//Console.WriteLine ("setting pixel at {0},{1}", cx, cy);
try {
//g.FillRectangle (Brushes.Green, cx, cy, cx+1, cy+1);
//Console.WriteLine ( b.GetPixel (cx,cy) );
if (!colorsToPreserve.Contains (b.GetPixel (cx, cy)))
b.SetPixel (cx, cy, land);
} catch (Exception e) {
Console.WriteLine ("ERROR: {0},{1},{2}", cx, cy, going);
throw e;
}
}
}
public void DrawForest ()
{
Console.WriteLine ("Drawing forest");
//first pass.
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
if (NumPixelsInSquareAreColor (x - 3, y - 3, 6, green) > 20 && r.Next (50) == 0 && ! colorsToPreserve.Contains (b.GetPixel (x, y)))
DrawLand (x, y, 20, darkGreen);
}
}
//smooth.
Console.WriteLine ("Smoothing forest");
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
Color c = b.GetPixel (x, y);
if (c != white && NumPixelsInSquareAreColor (x - 3, y - 3, 6, darkGreen) > 16 && c != blue)
b.SetPixel (x, y, darkGreen);
}
}
}
public void Cleanup ()
{
Console.WriteLine ("Smoothing");
//if every pixel around me is green, i should be green. if every pixel around me is blue,same.
//first do green.
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
Color c = b.GetPixel (x, y);
if (c == white)
EnsureAllPixelsAroundAre (x, y, green); else if (colorsToPreserve.Contains (c))
continue; else if (NumPixelsInSquareAreColor (x - 3, y - 3, 6, green) > 23)
b.SetPixel (x, y, green); else if (NumPixelsInSquareAreColor (x - 3, y - 3, 6, blue) > 23)
b.SetPixel (x, y, blue);
}
}
}
public void EnsureAllPixelsAroundAre (int x, int y, Color c)
{
foreach (Point p in directions) {
int x2 = Clamp (0, x + p.X, w - 1);
int y2 = Clamp (0, y + p.Y, h - 1);
Color cb = b.GetPixel (x2, y2);
if (cb != c && ! colorsToPreserve.Contains (cb))
b.SetPixel (x2, y2, c);
}
}
public int NumPixelsInSquareAreColor (int x1, int y1, int size, Color ca)
{
int count = 0;
if (x1 < 0)
x1 = 0;
if (y1 < 0)
y1 = 0;
for (int x = x1; x < x1+size && x < w; x++) {
for (int y = y1; y < y1+size && y < h; y++) {
Color cb = b.GetPixel (x, y);
if (ca == cb)
count++;
}
}
return count;
}
public bool FirstPixelInSquareOfColor (int x1, int y1, int size, Color ca, out Point result)
{
int count = 0;
if (x1 < 0)
x1 = 0;
if (y1 < 0)
y1 = 0;
for (int x = x1; x < x1+size && x < w; x++) {
for (int y = y1; y < y1+size && y < h; y++) {
Color cb = b.GetPixel (x, y);
if (ca == cb)
{
result = new Point (x, y);
return true;
}
}
}
result = new Point(-1,-1);
return false;
}
public int NumRiverWithin (int x1, int y1, int size)
{
int count = 0;
if (x1 < 0)
x1 = 0;
if (y1 < 0)
y1 = 0;
for (int x = x1; x < x1+size && x < w; x++) {
for (int y = y1; y < y1+size && y < h; y++) {
if (riverMap[x,y] > 0)
count++;
}
}
return count;
}
public int NumPixelsIn4DirectionsAreColor(int x, int y, Color c)
{
int count = 0;
if (y > 0 && b.GetPixel(x, y-1) == c) count++;
if (y < h-1 && b.GetPixel(x, y+1) == c) count++;
if (x > 0 && b.GetPixel(x-1, y) == c) count++;
if (x < w-1 && b.GetPixel(x+1, y) == c) count++;
return count;
}
public void SaveToDb (string name)
{
Console.WriteLine ("Saving to db");
Map map = new Map () {
Height = this.h,
Width = this.w
};
GameState game = new GameState () {
Name = name,
Map = map,
};
string[] deleteTables = new string[] {
"gamestate",
"map",
"hex",
"hexresource",
"map_cities",
"map_rivers",
"map_hexes",
"hex_edges",
"hex_effects",
"kingdom"
};
foreach (string deleteTable in deleteTables)
MySqlProvider.Provider.ExecuteNonQuery ("truncate " + deleteTable + ";");
map.Game = game;
map.Save ();
game.Save ();
RecordList<Hex> hexes = new RecordList<Hex> ();
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
Hex hex = new Hex ();
hex.EnsureId ();
hex.X = x;
hex.Y = y;
Color c = b.GetPixel (x, y);
if (c == blue)
hex.Terrain = TerrainType.Sea;
else if (c == white)
hex.Terrain = TerrainType.Mountains;
else if (c == darkGreen)
hex.Terrain = TerrainType.Forest;
else if (c == town)
{
hex.Terrain = TerrainType.Plains;
}
else
hex.Terrain = TerrainType.Plains;
hexes.Add (hex);
hexMap[x,y] = hex;
}
}
//cities have to be done in a separate pass to account for growth.
map.Hexes = hexes;
map.EnsureMapIsBuilt (true);
//check coastals
foreach (Hex h in hexes)
if (h.Terrain == TerrainType.Sea)
CheckCoastal (h);
hexes.Save ();
foreach (var kvp in riverPaths)
{
River r = new River();
int z = 0;
foreach(Point p in kvp.Value)
{
r.Path.Add (new Triple<int> () {
X = p.X,
Y = p.Y,
Z = z++
});
}
r.Save ();
r.Path.Save ();
r.SaveRelations("Path");
map.Rivers.Add (r);
}
map.SaveRelations ("Rivers");
map.SaveRelations ("Hexes");
}
public void DrawFinal (string fileName)
{
//draw rivers in.
foreach (var kvp in riverPaths)
{
foreach (var p in kvp.Value)
b.SetPixel(p.X, p.Y, river);
}
//save file.
b.Save (fileName);
}
public void CheckCoastal (Hex hex )
{
List<string> land = new List<string>();
foreach(string dir in Hex.Directions)
{
Point p = Hex.GetNeighborPoint(dir,hex.X,hex.Y);
if(p.X >= 0 && p.X < w && p.Y >= 0 && p.Y < h)
{
Color c = b.GetPixel(p.X,p.Y);
if (c != blue)
{
land.Add(dir);
}
}
}
if (land.Count > 0)
hex.ExtraInfo = Coastal.PickTiles (land);
}
}
}