-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorldGen.cpp
More file actions
429 lines (361 loc) · 28.5 KB
/
WorldGen.cpp
File metadata and controls
429 lines (361 loc) · 28.5 KB
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
// WorldGen.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include "world.h"
#include "grid.h"
#include "square.h"
/*
Given the parameters generates the random world, generating things in order.
Gasses (savegery increases oxygen, inhabitable worlds have things lost, hotter worlds more greenhouse)
All parameter based.
Can change overtime w flora???
Landmasses/Elevation (Valid configuration, may all be water dependent on configuration)
Diamond-square algorithim
Height is on a scale from 0-10.
0-5 on earth like planets, water.
6 - low level/sea level land. beaches.
7-8 - normal level land
9 - highlands, valleys
10 - mountains
Everything not above SEA_LEVEL is water.
SEA_LEVEL is set to whatever fills in WATER_PERCENT parameter.
E.G. 71% for earth-like elevations means SEA_LEVEL will be ~0.
Weather (Based on elevation, gasses, and temperatures. Most advanced interactions here)
First generates temperatures based on equator, water, and gasses (savagery, parameter, etc)
Temperate zones
Temperature of individual places
Creates the "wind" thread that the world view will display. Warm air and cold air moving appropriately.
Assuming same spin and axis as Earth - Advanced Parameter later??
Generates a sort of precipitation based on gasses (acidic) and elevation
LOTS of interaction here.
Temperature and mountains can cause deserts vs temperate lands.
Tornadoes on flatter lands held in by mountains in certain zones.
High rainfall vs low rainfall
Generates bodies of water - mostly Biome tier level.
Enough water makes a "sea" world tile
Placing rivers in random configurations until it works seems like best option.??
*World tier is now a series of mountains, water, or flatlands*
Generates "biomes"
Temperature, water, and elevation selects an appropriate biome for world tier square. E.G. Desert
Biome squares filled out from there. E.G. Dunes, Oasis, Valley, Kingdom.
World square update check for defining feature.
Should still be DESERT - most of the time, unless lots of water, or something.
Entity squares randomly filled out based on likelyhood chart.
E.G. In a DUNES, ~90% chance the tile is sand. Could also be den, sandstone, etc.
Places entities.
If Planet canSustainLife (Valid parameters (All green or yellow))
//DEPENDS ON HOW ADVANCED INTERACTIONS WILL BE - Entitiese need to hunt? e
(Based on gasses)
Higher oxygen, from lush plant live, high savagery, or appropriate temperatures causes GIANTs
Also makes higher elevations livable and lower not.
(Based on biome)
Different BEASTS for biomes.
Kingdoms are randomly placed, and only heroes are independently tracked?
Time and seasons - EVENTS
Will attempt building a UI using tools - but currently, a command-line interface is fine.
*/
//Parameters: DIstance from sun, mass of sun?, mass of planet, size of planet, temperature range?, pref biomes?, gaseous makeup?, savagery?, greenhouse gas effect?
//World object w different constructors for a d v a n c e d parameters?
/*Target temperature - Celsius, determined by sun, albedo, greenhouse gasses.
*/
/*
Given parameters, generates a world
Depreceated:
int distanceFromSun, int massRelativeToEarth, int sizeRelativeToEarth, int sizeOfSun
Parameters:
int greenhouse - Target greenhouse effect
int savagery - Aggresiveness of entities, also increases oxygen content - assuming planet is habitable
int temperature - target temperature of planet ? 7 options. frozen - normal - burning. At burning no atmosphere is possible. At frozen all liquids are ice.
int seaLevel -
*/
//Info for type of square.
char WATER_CHAR = '~';
std::string WATER_DESC = "Splish Splash.";
//globals that I probably shouldn't have but my design has fallen apart anyway so deal with it maybe I'll clean it up one day?
bool HIGH_OXYGEN = false;
bool LOW_OXYGEN = false;
int main()
{
srand(time(NULL));
generateWorld(0, 0, 0, 0);
}
int gridSize = 81;
int rowSize = 9;
//advanced parameters include: bond albedo (determining makeup of planet), greenhouse gasses, size of sun, distance of sun (all to calculate temp), Atmospheric pressure at sea level
Grid * generateWorld(int temperature, int savagery, int greenhouse, int seaLevel) {
//Initialize an 'empty' world - World grid, 81 biome grids, each with 81 entitySquares.
Grid * worldGrid = &Grid();
std::cout << "Succesfully generated World Grid." << std::endl;
for (int i = 0; i < gridSize; ++i) {
Square biomeSq = worldGrid->getSquareAtIndex(i);
std::cout << "Succesfully generated Entity Grid #" << i << std::endl;
for (int j = 0; j < gridSize; ++j) {
biomeSq.setGrid(&Grid());
}
}
std::cout << "Succesfully generated Entity Squares." << std::endl;
//initialize four corners
initializeSquare(0, worldGrid);
initializeSquare(rowSize - 1, worldGrid);
initializeSquare(gridSize - rowSize, worldGrid);
initializeSquare(gridSize - 1, worldGrid);
//use diamond-square to generate a heightmap landscape
std::cout << "Succesfully Init corners." << std::endl;
std::cout << "Corners are: " << std::endl;
std::cout << worldGrid->getSquareAtIndex(0).getHeight() << std::endl;
std::cout << worldGrid->getSquareAtIndex(8).getHeight() << std::endl;
std::cout << worldGrid->getSquareAtIndex(72).getHeight() << std::endl;
std::cout << worldGrid->getSquareAtIndex(80).getHeight() << std::endl;
diamondSquare(worldGrid, rowSize);
std::cout << "Succesfully crafted Landscape." << std::endl;
//temperature is determined by distance from sun, gaseous makeup, and bond albedo. Need an atmosphere.
std::cout << "Layering the atmosphere." << std::endl;
int atmos = 1; //Atmospheric pressure at sea level. Only changed with advanced parameters.
int add = savagery * 2; //Only savagery affects the add parameter - more advanced ones have influence.
generateGasses(temperature, atmos, add);
//temp determines water level. Normal means 3 or 4. Hot means 2 or 3. Desert means 1. Ice means replace all water with ice (-1)
//cooler temperature will mean more water is frozen, just during climate instead of filling ocean phase.
int waterLevel = determineWaterLevel(temperature);
fillOceans(worldGrid, waterLevel);
createClimate();
//int temp = calcMeanTemp(distanceFromSun, sizeOfSun);
//calcAtmosPres(300);
for (int i = 0; i < gridSize; ++i) {
std::cout << "Square: #" << i << " Height: "<< worldGrid->getSquareAtIndex(i).getHeight() << std::endl;
}
return worldGrid;
}
/*
Impelments a diamond-square algorithim to 'generate' a heightmap landscape for the world.
Each square in the grid will be set to a height.
*/
void diamondSquare(Grid *worldGrid, int size)
{
int mid = size/2;
std::cout << "Mid is equal to: " << mid << std::endl;
if (mid < 1)
return;
//square steps
for (int z = mid; z < rowSize; z += size)
for (int x = mid; x < rowSize; x += size)
squareStep(worldGrid, x % rowSize, z %rowSize, mid);
// diamond steps
int col = 0;
for (int x = 0; x < rowSize; x += mid)
{
col++;
//If this is an odd column.
if (col % 2 == 1)
for (int z = mid; z < rowSize; z += size)
diamondStep(worldGrid, x % rowSize, z % rowSize, mid);
else
for (int z = 0; z < rowSize; z += size)
diamondStep(worldGrid, x % rowSize, z % rowSize, mid);
}
diamondSquare(worldGrid, size / 2);
}
void squareStep(Grid *worldGrid, int x, int z, int reach) //8 & 4 on second pass????
{
std::cout << "Performing Square Step." << std::endl;
int count = 0;
int avg = 0;
if (x - reach >= 0 && z - reach >= 0)
{
std::cout << "Reaching square #" << (rowSize * (x - reach)) + (z - reach) << std::endl;
avg += worldGrid->getSquareAtIndex((rowSize * (x - reach)) + (z - reach)).getHeight();
//avg += Array[x - reach][z - reach];
count++;
}
if (x - reach >= 0 && z + reach < rowSize)
{
std::cout << "Reaching square #" << (rowSize * (x - reach)) + (z + reach) << std::endl;
avg += worldGrid->getSquareAtIndex((rowSize * (x - reach)) + (z + reach)).getHeight();
count++;
}
if (x + reach < rowSize && z - reach >= 0)
{
std::cout << "Reaching square #" << (rowSize * (x + reach)) + (z - reach) << std::endl;
avg += worldGrid->getSquareAtIndex((rowSize * (x + reach)) + (z - reach)).getHeight();
count++;
}
if (x + reach < rowSize && z + reach < rowSize)
{
std::cout << "Reaching square #" << (rowSize * (x + reach)) + (z + reach) << std::endl;
avg += worldGrid->getSquareAtIndex((rowSize * (x + reach)) + (z + reach)).getHeight();
count++;
}
std::cout << "Sum is: " << avg << std::endl;
//avg += (rand() % (reach * 2)) - reach;
avg /= count;
std::cout << "Height set to " << avg << " at Square #" << ((rowSize * x) + z) << std::endl;
worldGrid->getSquareAtIndex((rowSize * x) + z).updateHeight(avg);
}
void diamondStep(Grid *worldGrid, int x, int z, int reach)
{
std::cout << "Performing Diamond Step." << std::endl;
int count = 0;
int avg = 0;
if (x - reach >= 0)
{
std::cout << "Reaching square #" << (rowSize * (x - reach)) + (z) << std::endl;
avg += worldGrid->getSquareAtIndex((rowSize * (x - reach)) + z).getHeight();
count++;
}
if (x + reach < rowSize)
{
std::cout << "Reaching square #" << (rowSize * (x + reach)) + (z) << std::endl;
avg += worldGrid->getSquareAtIndex((rowSize * (x + reach)) + z).getHeight();
count++;
}
if (z - reach >= 0)
{
std::cout << "Reaching square #" << (rowSize * x) + (z - reach) << std::endl;
avg += worldGrid->getSquareAtIndex((rowSize * x) + (z - reach)).getHeight();
count++;
}
if (z + reach < rowSize)
{
std::cout << "Reaching square #" << (rowSize * (x)) + (z + reach) << std::endl;
avg += worldGrid->getSquareAtIndex((rowSize * x) + (z + reach)).getHeight();
count++;
}
std::cout << "Sum is: " << avg << std::endl;
//avg += (rand() % (reach * 2)) - reach;
avg /= count;
std::cout << "Height set to " << avg << " at Square #" << ((rowSize * x) + z) << std::endl;
worldGrid->getSquareAtIndex((rowSize * x) + z).updateHeight(avg);
}
//Helper for diamond square - initializes grids corners to be
int initializeSquare(int h, Grid *g) {
int height = rand() % 10;
g->getSquareAtIndex(h).updateHeight(height);
return height;
}
//Literally just puts water where its supposed to be. Sea level. And stuff.
void fillOceans(Grid * worldGrid, int waterLevel) {
std::cout << "Filling in oceans" << std::endl;
for (int i = 0; i < gridSize; i++) {
if (worldGrid->getSquareAtIndex(i).getHeight() < waterLevel) {
worldGrid->getSquareAtIndex(i).updateGraphic(WATER_CHAR);
worldGrid->getSquareAtIndex(i).updateDesc(WATER_DESC);
}
}
}
/*
Waterlevel is dependent on randomization and temperature.
*/
int determineWaterLevel(int temperature) {
int waterLevel = -2;
if (temperature == 0) {
waterLevel = -1;
}
else if (temperature == 3) {
int coinFlip = rand() % 1;
if (coinFlip == 1) {
waterLevel = 3;
}
else {
waterLevel = 4;
}
}
else if (temperature == 4) {
int coinFlip = rand() % 1;
if (coinFlip == 1) {
waterLevel = 2;
}
else {
waterLevel = 3;
}
}
else if (temperature == 5) {
waterLevel = 1;
}
else if (temperature == 6) {
waterLevel = 0;
}
return waterLevel;
}
/*
Generates the world's general gaseous makeup.
Parameters determine ranges for possible gaseous makeup, which are selected at random
These selections result in some possible effects:
Inhabitable - Air is poisonous or too heavy/light to breathe, or not made up of breathable elements
HighOxygen - Air at low levels is poisonous, air is available at high altitudes, and creatures are more often larger. More prone to fires.
LowOxtgen - Breathable air only at low elevations, smaller creatures. Less prone to fires.
HighPressure - More intense weather
LowPressure - Less intense weather
*/
double generateGasses(int temp, double atmPress, int add) {
if (temp == 6) {
//set world to inhabitable - gasses escape at this temperature.
}
else {
double v = (double)(rand() % (10000)) / 10000.0; //randomization factor
double N = (v + 78);
double A = (.9 - v);
double O = (100.0 - N - A);
if (O > 25.0) {
//set high oxygen, and update global oxygen parameter
HIGH_OXYGEN = true;
}
else if (O < 18.0) {
//set low oxygen, and update global oxygen parameter
LOW_OXYGEN = false;
}
return O;
//otherwise keep normal
}
}
/*
Uses a function to calculate the atmospheric pressure at a given square - by using the gasses and elevation.
More oxygen, a heavier atmosphere, causing events and interactions at certain elevations.
*/
int calcAtmosPres(int altitude) {
//atmp*exp(-(altitude) / scaleHeight)
//exp = e^(); ~2.178
//scale height = 8.3144598 * temp / .028949469
//scale constant, atomic weight constant
return 1;
}
/*
The mean temperature across a planets surface is ~Luminosity^1/4 / Distance^1/2, assuming a similar gaseous makeup.
int calcMeantemp(int d, int s) {
int l = sqrt(s);
int di = sqrt(d);
return l / di;
}
*/
void createClimate(int temp) {
/*
Weather(Based on elevation, gasses, and temperatures. Most advanced interactions here)
First generates temperatures based on equator, water, and gasses(savagery, parameter, etc)
Temperate zones
Temperature of individual places
Creates the "wind" thread that the world view will display.Warm air and cold air moving appropriately.
Assuming same spin and axis as Earth - Advanced Parameter later ? ?
Generates a sort of precipitation based on gasses(acidic) and elevation
LOTS of interaction here.
Temperature and mountains can cause deserts vs temperate lands.
Tornadoes on flatter lands held in by mountains in certain zones.
High rainfall vs low rainfall
If square index past 4th row on world grid is south, before is north, 4th row is equator.
Climate is mostly based on temperature and rainfall.
*/
genTemperateZones(temp);
}
void genTemperateZones(int temp) {
//Define normal, and expand/contract zones as necessary
//temp = 3
//Breakdown: N Frigid 1, N Temp 2, Torrid N 1, Equator(Tropic 1), Torrid S 1, S Temp 2, S Frigid 1.
if (temp == 3) {
//Frigid sone = ~10%
for (int i = 0; i < rowSize; ++i) {
}
for (int j = 0; j < rowSize * 2; ++j) {
}
for (int k = 0; j < rowSize * 3; ++k) {
}
}
}