-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cs
executable file
·359 lines (307 loc) · 11.6 KB
/
main.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
using System;
using System.Collections.Generic;
using SFML.Graphics;
using SFML.Window;
using SFML.System;
using System.Diagnostics;
public class TerrainGenDemo
{
public static Tuple<T,T> ArrayBounds2D<T>(T[,] array) where T:IComparable<T>
{
int width = array.GetLength(0);
int height = array.GetLength(1);
if (width <= 0 || height <= 0)
{
throw new Exception("Can't get the bounds of an empty array");
}
T min = array[0,0];
T max = array[0,0];
for (int x=0; x<width; x++)
{
for (int y=0; y<height; y++)
{
if (array[x,y].CompareTo(max) > 0)
max = array[x,y];
if (array[x,y].CompareTo(min) < 0)
min = array[x,y];
}
}
return new Tuple<T,T>(min, max);
}
public static double Lerp(double a, double b, double t)
{
/*
double min, max;
if (a < b)
{
min = a;
max = b;
}
else
{
max = a;
min = b;
}
*/
double min = a;
double max = b;
return min + (max-min)*t;
}
public static Color ColorLerp(Color c1, Color c2, double t)
{
byte r = (byte)Lerp(c1.R, c2.R, t);
byte g = (byte)Lerp(c1.G, c2.G, t);
byte b = (byte)Lerp(c1.B, c2.B, t);
byte a = (byte)Lerp(c1.A, c2.A, t);
return new Color(r,g,b,a);
}
public static void OnClose(object sender, EventArgs e)
{
RenderWindow w = (RenderWindow)sender;
w.Close();
}
public static void OnKeyPress(object sender, KeyEventArgs e)
{
if (e.Code == Keyboard.Key.R)
{
Console.WriteLine("Generating new map");
//GenerateWorldAndSprites();
RefreshWorldNew();
}
foreach (MapVisualisation mv in MapLayers)
{
if (e.Code == mv.ToggleKey)
{
mv.Enabled = !mv.Enabled;
}
}
}
private static double GetVegetationFitness(int moisture, double temperature, int bestTemperature)
{
Tuple<int,int> moistureMinMax = ArrayBounds2D<int>(world.GetMap<int>("moisture"));
int moistureMin = moistureMinMax.Item1;
int moistureMax = moistureMinMax.Item2;
double maxFitness = 100.0;
//reduction of fitness caused by 1-kelvin difference in temperature
//let's say plants can't stand below -10C or above 40C average, where 15C is best, so 25C should be 100% reduction
double tempFactor = 4.0;
double perfectTemp = 288.0;
maxFitness -= Math.Abs(perfectTemp - temperature) * tempFactor;
maxFitness = Math.Max(maxFitness, 0.0);
//more moisture is better right
//moisture doesn't correspond to a real world quantity yet so it's arbitrary
double neededMoisture = 0.4 * (moistureMax - moistureMin);
double goodMoistureRange = moistureMax - neededMoisture;
maxFitness *= (moisture - neededMoisture) / goodMoistureRange;
maxFitness = Math.Max(maxFitness, 0.0);
return maxFitness;
}
//represent a world's height and water map with nice colours
private static Image GetWorldImage()
{
int[,] map = world.GetMap<int>("height");
int[,] waterMap = world.GetMap<int>("water");
int[,] moistureMap = world.GetMap<int>("moisture");
double[,] tempMap = world.GetMap<double>("temperature");
Image returnImage = new Image((uint)map.GetLength(0), (uint)map.GetLength(1));
int min = (int)world.GetProperty("minheight");
int max = (int)world.GetProperty("maxheight");
int seaLevel = (int)world.GetProperty("sealevel");
int maxTemp = (int)world.GetProperty("maxtemp");
int dif = max-min;
Color poorVegColor = new Color(188,167,122,255);
Color richVegColor = new Color(77,158,58);
double maxVegFitness = 500;
for (int x=0; x<world.Width; x++)
{
for (int y=0; y<world.Height; y++)
{
int effectiveHeight = Math.Max(map[x,y], seaLevel);
int heightFromMin = effectiveHeight - min;
byte brightness = (byte)((double)heightFromMin/dif * 255);
double vegFitness = GetVegetationFitness(moistureMap[x,y], tempMap[x,y], maxTemp/2);
Color pixCol = new Color(0,0,0,255);
if (waterMap[x,y] > 0)
{
byte a = (byte)Math.Max(brightness, (byte)80);
pixCol = new Color(0, a, a, 255);
}
//else if (tempMap[x,y] < 70 && moistureMap[x,y] > 100)
else if (vegFitness < 200 && tempMap[x,y] < 100 && moistureMap[x,y] > 100)
{
pixCol = ColorLerp(new Color(128,128,128,255), Color.White, (double)heightFromMin/dif);
}
else
{
//default: vegetation tile
vegFitness = Math.Min(Math.Max(vegFitness, 0), maxVegFitness);
pixCol = ColorLerp(poorVegColor, richVegColor, vegFitness/maxVegFitness);
//pixCol = ColorLerp(pixCol, Color.White, (double)heightFromSea / seaDif * 0.1);
}
returnImage.SetPixel((uint)x, (uint)y, pixCol);
}
}
return returnImage;
}
private static void CreateVisualisations()
{
MapLayers.Add(new MapVisualisation(
world,
Keyboard.Key.F,
true,
Updaters.FitnessImage
));
MapLayers.Add(new MapVisualisation(
world,
Keyboard.Key.F,
true,
Updaters.HeightShadeImage
));
MapLayers.Add(new MapVisualisation(
world,
Keyboard.Key.B,
true,
Updaters.IsolineImage
));
MapLayers.Add(new MapVisualisation(
world,
Keyboard.Key.T,
false,
Updaters.TempImage
));
MapLayers.Add(new MapVisualisation(
world,
Keyboard.Key.M,
false,
Updaters.MoistureImage
));
MapLayers.Add(new MapVisualisation(
world,
Keyboard.Key.I,
false,
Updaters.IslandImage
));
MapLayers.Add(new MapVisualisation(
world,
Keyboard.Key.P,
false,
Updaters.PressureImage
));
}
private static void RefreshWorldNew()
{
gen.Reset();
gen.Run(seedRandom.Next());
foreach (MapVisualisation mv in MapLayers)
{
mv.UpdateSprite();
}
}
private static List<MapVisualisation> MapLayers = new List<MapVisualisation>();
private static Random seedRandom = new Random();
private static RenderWindow gameWindow;
private static World world;
private static WorldGenerator gen;
public static void Main(string[] args)
{
gameWindow = new RenderWindow(new VideoMode(1024,700), "Terrain Generator");
gameWindow.Closed += new EventHandler(OnClose);
gameWindow.KeyPressed += new EventHandler<KeyEventArgs>(OnKeyPress);
world = new World((int)gameWindow.Size.X, (int)gameWindow.Size.Y);
CreateVisualisations();
gen = new WorldGenerator(world);
gen.AddStep(new GeneratorStep(
gen,
"perlin",
new string[0],
PerlinNoiseMaker.ApplyNoise
));
gen.AddStep(new GeneratorStep(
gen,
"planetprops",
new string[0],
GenStepLibrary.ComputePlanetProperties
));
gen.AddStep(new GeneratorStep(
gen,
"seawater",
new string[] {"perlin"},
GenStepLibrary.ComputeSeaWater
));
gen.AddStep(new GeneratorStep(
gen,
"rainfall",
new string[] {"perlin", "seawater", "pressure"},
GenStepLibrary.ComputeRainfall
));
gen.AddStep(new GeneratorStep(
gen,
"temperature",
new string[] {"perlin", "seawater", "planetprops"},
GenStepLibrary.ComputeTemperature
));
gen.AddStep(new GeneratorStep(
gen,
"findislands",
new string[] {"seawater"},
GenStepLibrary.FindIslands
));
gen.AddStep(new GeneratorStep(
gen,
"pressure",
new string[] {"planetprops"},
GenStepLibrary.ComputePressure
));
gen.AddStep(new GeneratorStep(
gen,
"fitness",
new string[] {"rainfall", "temperature", "seawater"},
GenStepLibrary.ComputeFitness
));
RefreshWorldNew();
Text cellValueText = new Text();
cellValueText.CharacterSize = 15;
cellValueText.Color = new Color(255,255,255,255);
cellValueText.Font = new Font("arial.ttf");
string[] mapNames = world.MapNames;
string[] cellValueLines = new string[mapNames.Length];
while(gameWindow.IsOpen)
{
gameWindow.DispatchEvents();
gameWindow.Clear();
foreach (MapVisualisation mv in MapLayers)
{
if (mv.Enabled)
{
mv.Draw(gameWindow, RenderStates.Default);
}
}
Vector2i mousePos = Mouse.GetPosition(gameWindow);
if (mousePos.X >= 0 && mousePos.X < world.Width && mousePos.Y >= 0 && mousePos.Y < world.Height)
{
for (int i=0; i<mapNames.Length; i++)
{
object thisCell = world.GetMapRawCell(mapNames[i], mousePos.X, mousePos.Y);
string valueString;
if (thisCell.GetType().IsPrimitive)
{
valueString = String.Format("{0:F2}", thisCell);
}
else
{
valueString = thisCell.ToString();
}
cellValueLines[i] = mapNames[i] + "~ " + valueString;
}
cellValueText.DisplayedString = string.Join("\n", cellValueLines);
cellValueText.Color = new Color(0,0,0,255);
cellValueText.Position = new Vector2f(mousePos.X+10, mousePos.Y+1);
cellValueText.Draw(gameWindow, RenderStates.Default);
cellValueText.Color = new Color(255,255,255,255);
cellValueText.Position -= new Vector2f(1,1);
cellValueText.Draw(gameWindow, RenderStates.Default);
}
gameWindow.Display();
}
}
}