/
WorldCreator.cs
533 lines (420 loc) · 14.6 KB
/
WorldCreator.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dwarrowdelf;
using Dwarrowdelf.Server;
using Dwarrowdelf.TerrainGen;
using System.Threading.Tasks;
using System.Threading;
namespace MyArea
{
static class WorldCreator
{
const int MAP_SIZE = 7; // 2^AREA_SIZE
const int MAP_DEPTH = 20;
const int GRASS_LIMIT = 15; // No grass if z >= GRASS_LIMIT
const int NUM_SHEEP = 3;
const int NUM_ORCS = 3;
public static void InitializeWorld(World world)
{
int surfaceLevel;
var environment = CreateEnv(world, out surfaceLevel);
FinalizeEnv(environment, surfaceLevel);
}
static IntPoint3 GetRandomSurfaceLocation(EnvironmentObject env, int zLevel)
{
IntPoint3 p;
int iter = 0;
do
{
if (iter++ > 10000)
throw new Exception();
p = new IntPoint3(Helpers.GetRandomInt(env.Width), Helpers.GetRandomInt(env.Height), zLevel);
} while (!EnvironmentHelpers.CanEnter(env, p));
return p;
}
static IntPoint3 GetRandomSubterraneanLocation(EnvironmentObjectBuilder env)
{
IntPoint3 p;
int iter = 0;
do
{
if (iter++ > 10000)
throw new Exception();
p = new IntPoint3(Helpers.GetRandomInt(env.Width), Helpers.GetRandomInt(env.Height), Helpers.GetRandomInt(env.Depth));
} while (env.GetTerrainID(p) != TerrainID.NaturalWall);
return p;
}
static EnvironmentObject CreateEnv(World world, out int surfaceLevel)
{
int sizeExp = MAP_SIZE;
int size = (int)Math.Pow(2, sizeExp);
// size + 1 for the DiamondSquare algorithm
var doubleHeightMap = new ArrayGrid2D<double>(size + 1, size + 1);
var corners = new DiamondSquare.CornerData()
{
NE = 15,
NW = 10,
SW = 10,
SE = 10,
};
DiamondSquare.Render(doubleHeightMap, corners, 5, 0.75, 1);
// Normalize the heightmap to 0.0 - 1.0
Clamper.Normalize(doubleHeightMap);
// square each value, to smoothen the lower parts
doubleHeightMap.ForEach(v => Math.Pow(v, 2));
// integer heightmap. the number tells the z level where the floor is.
var intHeightMap = new ArrayGrid2D<int>(size, size);
foreach (var p in IntPoint2.Range(size, size))
{
var d = doubleHeightMap[p];
d *= MAP_DEPTH / 2;
d += (MAP_DEPTH / 2) - 1;
intHeightMap[p] = (int)Math.Round(d);
}
var envBuilder = new EnvironmentObjectBuilder(new IntSize3(size, size, MAP_DEPTH), VisibilityMode.GlobalFOV);
CreateTerrainFromHeightmap(intHeightMap, envBuilder);
CreateSlopes(envBuilder, intHeightMap);
CreateTrees(envBuilder, intHeightMap);
var oreMaterials = Materials.GetMaterials(MaterialCategory.Gem).Concat(Materials.GetMaterials(MaterialCategory.Mineral)).Select(mi => mi.ID).ToArray();
for (int i = 0; i < 30; ++i)
{
var p = GetRandomSubterraneanLocation(envBuilder);
var idx = Helpers.GetRandomInt(oreMaterials.Length);
CreateOreCluster(envBuilder, p, oreMaterials[idx]);
}
surfaceLevel = FindSurfaceLevel(intHeightMap);
return envBuilder.Create(world);
}
static void FinalizeEnv(EnvironmentObject env, int surfaceLevel)
{
var world = env.World;
// Add items
for (int i = 0; i < 6; ++i)
CreateItem(env, ItemID.Gem, GetRandomMaterial(MaterialCategory.Gem), GetRandomSurfaceLocation(env, surfaceLevel));
for (int i = 0; i < 6; ++i)
CreateItem(env, ItemID.Rock, GetRandomMaterial(MaterialCategory.Rock), GetRandomSurfaceLocation(env, surfaceLevel));
//CreateWaterTest(env, surfaceLevel);
CreateBuildings(env, surfaceLevel);
{
var p = new IntPoint3(env.Width / 2 - 1, env.Height / 2 - 2, surfaceLevel);
env.SetInterior(p, InteriorID.Empty, MaterialID.Undefined);
CreateItem(env, ItemID.Ore, MaterialID.Tin, p);
CreateItem(env, ItemID.Ore, MaterialID.Tin, p);
CreateItem(env, ItemID.Ore, MaterialID.Lead, p);
CreateItem(env, ItemID.Ore, MaterialID.Lead, p);
CreateItem(env, ItemID.Ore, MaterialID.Iron, p);
CreateItem(env, ItemID.Ore, MaterialID.Iron, p);
CreateItem(env, ItemID.Log, GetRandomMaterial(MaterialCategory.Wood), p);
CreateItem(env, ItemID.Log, GetRandomMaterial(MaterialCategory.Wood), p);
CreateItem(env, ItemID.Log, GetRandomMaterial(MaterialCategory.Wood), p);
CreateItem(env, ItemID.Door, GetRandomMaterial(MaterialCategory.Wood), p);
CreateItem(env, ItemID.Block, GetRandomMaterial(MaterialCategory.Rock), p);
CreateItem(env, ItemID.Block, GetRandomMaterial(MaterialCategory.Rock), p);
CreateItem(env, ItemID.Block, GetRandomMaterial(MaterialCategory.Rock), p);
CreateItem(env, ItemID.Block, GetRandomMaterial(MaterialCategory.Rock), p);
CreateItem(env, ItemID.Block, GetRandomMaterial(MaterialCategory.Rock), p);
}
{
var gen = FoodGenerator.Create(env.World);
gen.MoveTo(env, new IntPoint3(env.Width / 2 - 2, env.Height / 2 - 2, surfaceLevel));
}
AddMonsters(env, surfaceLevel);
/*
{
// create a wall and a hole with door
IntPoint3 p;
for (int y = 4; y < 12; ++y)
{
int x = 17;
p = new IntPoint3(x, y, surfaceLevel);
env.SetTerrain(p, TerrainID.NaturalWall, MaterialID.Granite);
env.SetInterior(p, InteriorID.Undefined, MaterialID.Undefined);
}
p = new IntPoint3(17, 7, surfaceLevel);
env.SetTerrain(p, TerrainID.NaturalFloor, MaterialID.Granite);
env.SetInterior(p, InteriorID.Empty, MaterialID.Undefined);
p = new IntPoint3(17, 7, surfaceLevel);
var item = CreateItem(env, ItemID.Door, MaterialID.Birch, p);
item.IsInstalled = true;
}*/
}
static void CreateBuildings(EnvironmentObject env, int surfaceLevel)
{
var world = env.World;
int posx = env.Width / 2 - 16;
int posy = env.Height / 2 - 10;
var floorTile = new TileData()
{
TerrainID = TerrainID.NaturalFloor,
TerrainMaterialID = MaterialID.Granite,
InteriorID = InteriorID.Empty,
InteriorMaterialID = MaterialID.Undefined,
};
{
var builder = new BuildingObjectBuilder(BuildingID.Smith, new IntRectZ(posx, posy, 3, 3, surfaceLevel));
SetArea(env, builder.Area.ToCuboid(), floorTile);
builder.Create(world, env);
}
posx += 4;
{
var builder = new BuildingObjectBuilder(BuildingID.Carpenter, new IntRectZ(posx, posy, 3, 3, surfaceLevel));
SetArea(env, builder.Area.ToCuboid(), floorTile);
builder.Create(world, env);
}
posx += 4;
{
var builder = new BuildingObjectBuilder(BuildingID.Mason, new IntRectZ(posx, posy, 3, 3, surfaceLevel));
SetArea(env, builder.Area.ToCuboid(), floorTile);
builder.Create(world, env);
}
posx = env.Width / 2 - 16;
posy += 4;
{
var builder = new BuildingObjectBuilder(BuildingID.Smelter, new IntRectZ(posx, posy, 3, 3, surfaceLevel));
SetArea(env, builder.Area.ToCuboid(), floorTile);
builder.Create(world, env);
}
posx += 4;
{
var builder = new BuildingObjectBuilder(BuildingID.Gemcutter, new IntRectZ(posx, posy, 3, 3, surfaceLevel));
SetArea(env, builder.Area.ToCuboid(), floorTile);
builder.Create(world, env);
}
}
static MaterialID GetRandomMaterial(MaterialCategory category)
{
var materials = Materials.GetMaterials(category).Select(mi => mi.ID).ToArray();
return materials[Helpers.GetRandomInt(materials.Length)];
}
static ItemObject CreateItem(EnvironmentObject env, ItemID itemID, MaterialID materialID, IntPoint3 p)
{
var builder = new ItemObjectBuilder(itemID, materialID);
var item = builder.Create(env.World);
var ok = item.MoveTo(env, p);
if (!ok)
throw new Exception();
return item;
}
static void AddMonsters(EnvironmentObject env, int surfaceLevel)
{
var world = env.World;
for (int i = 0; i < NUM_SHEEP; ++i)
{
var livingBuilder = new LivingObjectBuilder(LivingID.Sheep)
{
Color = GetRandomColor(),
};
var living = livingBuilder.Create(world);
living.SetAI(new Dwarrowdelf.AI.HerbivoreAI(living));
living.MoveTo(env, GetRandomSurfaceLocation(env, surfaceLevel));
}
for (int i = 0; i < NUM_ORCS; ++i)
{
var livingBuilder = new LivingObjectBuilder(LivingID.Orc)
{
Color = GetRandomColor(),
};
var living = livingBuilder.Create(world);
living.SetAI(new Dwarrowdelf.AI.MonsterAI(living));
Helpers.AddGem(living);
Helpers.AddBattleGear(living);
living.MoveTo(env, GetRandomSurfaceLocation(env, surfaceLevel));
}
}
/// <summary>
/// return the z of the level with most ground
/// </summary>
/// <param name="heightMap"></param>
static int FindSurfaceLevel(ArrayGrid2D<int> heightMap)
{
return heightMap
.GroupBy(i => i)
.Select(g => new { H = g.Key, Count = g.Count() })
.OrderByDescending(c => c.Count)
.First()
.H;
}
static void CreateTerrainFromHeightmap(ArrayGrid2D<int> heightMap, EnvironmentObjectBuilder env)
{
Parallel.For(0, env.Height, y =>
{
for (int x = 0; x < env.Width; ++x)
{
int surface = heightMap[x, y];
for (int z = 0; z < env.Depth; ++z)
{
var p = new IntPoint3(x, y, z);
var td = new TileData();
td.InteriorID = InteriorID.Empty;
td.InteriorMaterialID = MaterialID.Undefined;
if (z < surface)
{
td.TerrainID = TerrainID.NaturalWall;
td.TerrainMaterialID = MaterialID.Granite;
}
else if (z == surface)
{
td.TerrainID = TerrainID.NaturalFloor;
td.TerrainMaterialID = MaterialID.Granite;
if (z < GRASS_LIMIT)
td.Flags = TileFlags.Grass;
}
else
{
td.TerrainID = TerrainID.Empty;
td.TerrainMaterialID = MaterialID.Undefined;
}
env.SetTileData(p, td);
}
}
});
}
static void CreateTrees(EnvironmentObjectBuilder env, ArrayGrid2D<int> heightMap)
{
var materials = Materials.GetMaterials(MaterialCategory.Wood).ToArray();
env.Bounds.Plane.Range().AsParallel().ForAll(p2d =>
{
int z = heightMap[p2d];
var p = new IntPoint3(p2d, z);
var terrainID = env.GetTerrainID(p);
if (terrainID == TerrainID.NaturalFloor || terrainID.IsSlope())
{
var interiorID = env.GetInteriorID(p);
if (interiorID == InteriorID.Empty)
{
if (Helpers.GetRandomInt(8) == 0)
{
var material = materials[Helpers.GetRandomInt(materials.Length)].ID;
var interior = Helpers.GetRandomInt() % 2 == 0 ? InteriorID.Tree : InteriorID.Sapling;
env.SetInterior(p, interior, material);
}
}
}
});
}
static void CreateSlopes(EnvironmentObjectBuilder env, ArrayGrid2D<int> heightMap)
{
var arr = new ThreadLocal<Direction[]>(() => new Direction[8]);
var plane = env.Bounds.Plane;
plane.Range().AsParallel().ForAll(p =>
{
int z = heightMap[p];
int count = 0;
Direction dir = Direction.None;
int offset = Helpers.GetRandomInt(8);
// Count the tiles around this tile which are higher. Create slope to a random direction, but skip
// the slope if all 8 tiles are higher.
for (int i = 0; i < 8; ++i)
{
var d = DirectionExtensions.PlanarDirections[(i + offset) % 8];
var t = p + d;
if (plane.Contains(t) && heightMap[t] > z)
{
dir = d;
count++;
}
}
if (count > 0 && count < 8)
{
var p3d = new IntPoint3(p, z);
env.SetTerrain(p3d, dir.ToSlope(), env.GetTerrainMaterialID(p3d));
}
});
}
static void CreateOreCluster(EnvironmentObjectBuilder env, IntPoint3 p, MaterialID oreMaterialID)
{
CreateOreCluster(env, p, oreMaterialID, Helpers.GetRandomInt(6) + 1);
}
static void CreateOreCluster(EnvironmentObjectBuilder env, IntPoint3 p, MaterialID oreMaterialID, int count)
{
if (!env.Contains(p))
return;
if (env.GetTerrainID(p) != TerrainID.NaturalWall)
return;
if (env.GetInteriorID(p) == InteriorID.Ore)
return;
env.SetInterior(p, InteriorID.Ore, oreMaterialID);
if (count > 0)
{
foreach (var d in DirectionExtensions.CardinalUpDownDirections)
CreateOreCluster(env, p + d, oreMaterialID, count - 1);
}
}
static GameColor GetRandomColor()
{
return (GameColor)Helpers.GetRandomInt(GameColorRGB.NUMCOLORS - 1) + 1;
}
static void ClearTile(EnvironmentObject env, IntPoint3 p)
{
env.SetTerrain(p, TerrainID.Empty, MaterialID.Undefined);
env.SetInterior(p, InteriorID.Empty, MaterialID.Undefined);
}
static void ClearInside(EnvironmentObject env, IntPoint3 p)
{
env.SetTerrain(p, TerrainID.NaturalFloor, MaterialID.Granite);
env.SetInterior(p, InteriorID.Empty, MaterialID.Undefined);
}
static void SetArea(EnvironmentObject env, IntCuboid area, TileData data)
{
foreach (var p in area.Range())
env.SetTileData(p, data);
}
static void CreateWalls(EnvironmentObject env, IntRectZ area)
{
for (int x = area.X1; x < area.X2; ++x)
{
for (int y = area.Y1; y < area.Y2; ++y)
{
if ((y != area.Y1 && y != area.Y2 - 1) &&
(x != area.X1 && x != area.X2 - 1))
continue;
var p = new IntPoint3(x, y, area.Z);
env.SetTerrain(p, TerrainID.NaturalWall, MaterialID.Granite);
env.SetInterior(p, InteriorID.Empty, MaterialID.Undefined);
}
}
}
static void CreateWater(EnvironmentObject env, IntRectZ area)
{
for (int x = area.X1; x < area.X2; ++x)
{
for (int y = area.Y1; y < area.Y2; ++y)
{
var p = new IntPoint3(x, y, area.Z);
env.SetWaterLevel(p, TileData.MaxWaterLevel);
}
}
}
static void CreateWaterTest(EnvironmentObject env, int surfaceLevel)
{
var pos = new IntPoint3(10, 30, surfaceLevel);
CreateWalls(env, new IntRectZ(pos.X, pos.Y, 3, 8, surfaceLevel));
CreateWater(env, new IntRectZ(pos.X + 1, pos.Y + 1, 1, 6, surfaceLevel));
int x = 15;
int y = 30;
ClearTile(env, new IntPoint3(x, y, surfaceLevel - 0));
ClearTile(env, new IntPoint3(x, y, surfaceLevel - 1));
ClearTile(env, new IntPoint3(x, y, surfaceLevel - 2));
ClearTile(env, new IntPoint3(x, y, surfaceLevel - 3));
ClearTile(env, new IntPoint3(x, y, surfaceLevel - 4));
ClearInside(env, new IntPoint3(x + 0, y, surfaceLevel - 5));
ClearInside(env, new IntPoint3(x + 1, y, surfaceLevel - 5));
ClearInside(env, new IntPoint3(x + 2, y, surfaceLevel - 5));
ClearInside(env, new IntPoint3(x + 3, y, surfaceLevel - 5));
ClearInside(env, new IntPoint3(x + 4, y, surfaceLevel - 5));
ClearTile(env, new IntPoint3(x + 4, y, surfaceLevel - 4));
ClearTile(env, new IntPoint3(x + 4, y, surfaceLevel - 3));
ClearTile(env, new IntPoint3(x + 4, y, surfaceLevel - 2));
ClearTile(env, new IntPoint3(x + 4, y, surfaceLevel - 1));
ClearTile(env, new IntPoint3(x + 4, y, surfaceLevel - 0));
env.ScanWaterTiles();
{
// Add a water generator
var item = WaterGenerator.Create(env.World);
item.MoveTo(env, new IntPoint3(pos.X + 1, pos.Y + 2, surfaceLevel));
}
}
}
}