forked from RosaryMala/DwarvenRealms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapCrafter.cs
584 lines (506 loc) · 25.3 KB
/
MapCrafter.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
using Substrate;
using Substrate.Core;
using System;
using System.Diagnostics;
using DwarvenRealms.Properties;
namespace DwarvenRealms
{
class MapCrafter
{
int shift = -35; // ??? base world greyscale?
NbtWorld currentWorld;
DwarfWorldMap currentDwarfMap;
CaveThing currentCaveMap;
int maxHeight = -9999;
int minHeight = 9999;
mapData internalMap;
public static int getChunkStartX() { return ((Settings.Default.borderWest - Settings.Default.mapCenterX) * Settings.Default.blocksPerEmbarkTile) / 16; }
public static int getChunkStartY() { return ((Settings.Default.borderNorth - Settings.Default.mapCenterY) * Settings.Default.blocksPerEmbarkTile) / 16; }
public static int getChunkFinishX() { return ((Settings.Default.borderEast - Settings.Default.mapCenterX) * Settings.Default.blocksPerEmbarkTile) / 16; }
public static int getChunkFinishY() { return ((Settings.Default.borderSouth - Settings.Default.mapCenterY) * Settings.Default.blocksPerEmbarkTile) / 16; }
public void loadDwarfMaps()
{
currentDwarfMap = new DwarfWorldMap();
currentDwarfMap.loadElevationMap(Settings.Default.elevationMapPath);
currentDwarfMap.loadWaterMap(Settings.Default.elevationWaterMapPath);
currentDwarfMap.loadBiomeMap(Settings.Default.biomeMapPath);
currentDwarfMap.loadStructureMap(Settings.Default.structureMapPath);
currentCaveMap = new CaveThing(currentDwarfMap);
/* internalMap = new mapData(((Settings.Default.borderEast - Settings.Default.borderWest + 2) * Settings.Default.blocksPerEmbarkTile) * 16,
((Settings.Default.borderSouth - Settings.Default.borderNorth + 2) * Settings.Default.blocksPerEmbarkTile) * 16, Settings.Default.mapCenterX * 16, Settings.Default.mapCenterY * 16);
*/
}
public void initializeMinecraftWorld()
{
currentWorld = AnvilWorld.Create(Settings.Default.outputPath);
// We can set different world parameters
currentWorld.Level.LevelName = Settings.Default.levelName;
currentWorld.Level.Spawn = new SpawnPoint(0, 255, 0);
if (Settings.Default.gameMode == 0)
{
currentWorld.Level.GameType = GameType.SURVIVAL;
}
else
{
currentWorld.Level.GameType = GameType.CREATIVE;
currentWorld.Level.AllowCommands = true;
}
}
public void saveMinecraftWorld()
{
// Save all remaining data (including a default level.dat)
// If we didn't save chunks earlier, they would be saved here
currentWorld.Save();
}
/* public void simpleWriteTest()
{
initializeMinecraftWorld();
IChunkManager cm = currentWorld.GetChunkManager();
loadDwarfMaps();
int cropWidth = 40;
int cropHeight = 40;
Settings.Default.borderWest = 40;
Settings.Default.borderEast = Settings.Default.borderWest + cropWidth;
Settings.Default.borderNorth = 40;
Settings.Default.borderSouth = Settings.Default.borderNorth + cropHeight;
//FIXME get rid of this junk
Settings.Default.mapCenterX = (Settings.Default.borderWest + Settings.Default.borderEast) / 2;
Settings.Default.mapCenterY = (Settings.Default.borderNorth + Settings.Default.borderSouth) / 2;
Console.WriteLine("Starting conversion now.");
Stopwatch watch = Stopwatch.StartNew();
for (int xi = getChunkStartX(); xi < getChunkFinishX(); xi++)
{
for (int zi = getChunkStartY(); zi < getChunkFinishY(); zi++)
{
// This line will create a default empty chunk, and create a
// backing region file if necessary (which will immediately be
// written to disk)
ChunkRef chunk = cm.CreateChunk(xi, zi);
// This will make sure all the necessary things like trees and
// ores are generated for us.
chunk.IsTerrainPopulated = false;
// Auto light recalculation is horrifically bad for creating
// chunks from scratch, because we're placing thousands
// of blocks. Turn it off.
chunk.Blocks.AutoLight = false;
double xMin = ((xi * 16.0 / (double)Settings.Default.blocksPerEmbarkTile) + Settings.Default.mapCenterX);
double xMax = (((xi + 1) * 16.0 / (double)Settings.Default.blocksPerEmbarkTile) + Settings.Default.mapCenterX);
double yMin = ((zi * 16.0 / (double)Settings.Default.blocksPerEmbarkTile) + Settings.Default.mapCenterY);
double yMax = (((zi + 1) * 16.0 / (double)Settings.Default.blocksPerEmbarkTile) + Settings.Default.mapCenterY);
// Make the terrain
HeightMapChunk(chunk, xMin, xMax, yMin, yMax);
// Reset and rebuild the lighting for the entire chunk at once
chunk.Blocks.RebuildHeightMap();
if (Settings.Default.lightGenerationEnable)
{
chunk.Blocks.RebuildBlockLight();
chunk.Blocks.RebuildSkyLight();
}
// Save the chunk to disk so it doesn't hang around in RAM
cm.Save();
}
//TimeSpan elapsedTime = watch.Elapsed;
//int finished = xi - chunkStartX + 1;
//int left = chunkFinishX - xi - 1;
//TimeSpan remainingTime = TimeSpan.FromTicks(elapsedTime.Ticks / finished * left);
//Console.WriteLine("Built Chunk Row {0} of {1}. {2}:{3}:{4} elapsed, {5}:{6}:{7} remaining.",
// finished, chunkFinishX - chunkStartX,
// elapsedTime.Hours, elapsedTime.Minutes, elapsedTime.Seconds,
// remainingTime.Hours, remainingTime.Minutes, remainingTime.Seconds);
maxHeight = int.MinValue;
minHeight = int.MaxValue;
}
saveMinecraftWorld();
}
*/
public internalData generateInternalMapChunk(bounds bound)
{
return new internalData(
(int)currentDwarfMap.getElevation(bound.mux, bound.muy) + shift,
currentDwarfMap.getBiome(bound.MuxInt(), bound.MuyInt()),
currentDwarfMap.getRiverType(bound.MuxInt(), bound.MuyInt()),
currentDwarfMap.getRiverLevel(bound.MuxInt(), bound.MuyInt()) + shift,
currentDwarfMap.getWaterbodyLevel(bound.MuxInt(), bound.MuyInt()) + shift,
currentDwarfMap.getStructureMap(bound.MuxInt(), bound.MuyInt())
);
}
public class bounds
{
public double xMin;
public double xMid;
public double xMax;
public double yMin;
public double yMid;
public double yMax;
public int x, xt;
public int z, zt;
public double mux;
public double muy;
// This converts from MC chunk location back to DF Map reference
//double mux = (mapXMax - mapXMin) * x / 16.0 + mapXMin; // mux = width / 16 + xOffset
//double muy = (mapYMax - mapYMin) * z / 16.0 + mapYMin; // muy = height / 16 + yOffset
/*
int height = (int)currentDwarfMap.getElevation(mux, muy) + shift;
int waterLevel = currentDwarfMap.getWaterbodyLevel((int)Math.Floor(mux + 0.5), (int)Math.Floor(muy + 0.5)) + shift;
int riverLevel = currentDwarfMap.getRiverLevel((int)Math.Floor(mux + 0.5), (int)Math.Floor(muy + 0.5)) + shift;
int depth = currentDwarfMap.getRiverType((int)Math.Floor(mux + 0.5), (int)Math.Floor(muy + 0.5));
int structure = currentDwarfMap.getStructureMap((int)Math.Floor(mux + 0.5), (int)Math.Floor(muy + 0.5));
*/
//int xt = ((int)Math.Floor(mux + 0.5)*16) + x;
//int zt = ((int)Math.Floor(muy + 0.5)*16) + z;
/* int biomeIndex = currentDwarfMap.getBiome((int)Math.Floor(mux + 0.5), (int)Math.Floor(muy + 0.5));
*/
public bounds(int xi, int zi)
{
x = xi;
z = zi;
xMin = ((xi * 16.0 / (double)Settings.Default.blocksPerEmbarkTile) + Settings.Default.mapCenterX);
xMax = (((xi + 1) * 16.0 / (double)Settings.Default.blocksPerEmbarkTile) + Settings.Default.mapCenterX);
xMid = (xMax - xMin) / 2;
yMin = ((zi * 16.0 / (double)Settings.Default.blocksPerEmbarkTile) + Settings.Default.mapCenterY);
yMax = (((zi + 1) * 16.0 / (double)Settings.Default.blocksPerEmbarkTile) + Settings.Default.mapCenterY);
yMid = (yMax - yMin) / 2;
updateBound(0, 0);
}
// This converts from MC chunk location back to DF Map reference
public void updateBound(int x, int z)
{
mux = (xMax - xMin) * x / 16.0 + xMin;
muy = (yMax - yMin) * z / 16.0 + yMin;
xt = ((int)Math.Floor(mux + 0.5) * 16) + x;
zt = ((int)Math.Floor(muy + 0.5) * 16) + z;
}
public int MuxInt()
{
return (int)Math.Floor(mux + 0.5);
}
public int MuyInt()
{
return (int)Math.Floor(muy + 0.5);
}
}
/// <summary>
/// Generates and saves a single minecraft chunk using current settings.
/// </summary>
/// <param name="xi">X coordinate of the chunk.</param>
/// <param name="zi">Y coordinate of the chunk.</param>
public void generateSingleChunk(int xi, int zi)
{
// This line will create a default empty chunk, and create a
// backing region file if necessary (which will immediately be
// written to disk)
ChunkRef chunk = currentWorld.GetChunkManager().CreateChunk(xi, zi);
// This will make sure all the necessary things like trees and
// ores are generated for us.
chunk.IsTerrainPopulated = false;
// Auto light recalculation is horrifically bad for creating
// chunks from scratch, because we're placing thousands
// of blocks. Turn it off.
chunk.Blocks.AutoLight = false;
// This scales the dimensions based off the blocksPerEmbarkTile [1..8]
bounds bound = new bounds(xi, zi);
// Make the terrain
HeightMapChunk(chunk, bound);
// Reset and rebuild the lighting for the entire chunk at once
chunk.Blocks.RebuildHeightMap();
chunk.Blocks.RebuildBlockLight();
chunk.Blocks.RebuildSkyLight();
// Save the chunk to disk so it doesn't hang around in RAM
currentWorld.GetChunkManager().Save();
}
void HeightMapChunk(ChunkRef chunk, bounds bound)
{
double mapXMin = bound.xMin;
double mapXMax = bound.xMax;
double mapYMin = bound.yMin;
double mapYMax = bound.yMax;
int size = 16;
internalData[,] mapData = new internalData[3*size+2, 3*size+2];
for (int x = -size-1; x <= 2*size; x++)
{
for (int z = -size-1; z <= 2 * size; z++)
{
bound.updateBound(x, z);
// build 48x48 chunk of source maps for use below
mapData[x+size+1, z+size+1] = generateInternalMapChunk(bound);
}
}
for (int x = 0; x < 16; x++)
{
for (int z = 0; z < 16; z++)
{
bound.updateBound(x, z);
internalData id = mapData[x+1, z+1]; // .getInternalData(xt, zt);
int height = id.height;
int waterLevel = id.waterLevel;
int riverLevel = id.riverLevel;
int depth = id.riverType;
Structures.Type structure = id.structureType;
int biomeIndex = id.biomeIndex < 0 ? 0 : id.biomeIndex;
if (height > maxHeight) maxHeight = height;
if (height < minHeight) minHeight = height;
chunk.Biomes.SetBiome(x, z, BiomeList.biomes[biomeIndex].mineCraftBiome);
//create bedrock
for (int y = 0; y < 2; y++)
{
chunk.Blocks.SetID(x, y, z, BlockType.BEDROCK);
}
////deal with rivers
// BezierRivers.MakeRivers mr = new BezierRivers.MakeRivers();
// mr.makeRivers();
if (riverLevel >= 0)
{
chunk.Biomes.SetBiome(x, z, BiomeType.River);
height = riverLevel;
// All DF rivers are 1 px wide.
// That scales into embarkWidth regardless of type of river
// This metric allows us to fine-tune river width beyond that of embarkWidth
double oX = (mapXMax - mapXMin) * x % 16 - 8;
double oY = (mapYMax - mapYMin) * z % 16 - 8;
// Console.WriteLine("[{3},{4},({2}): {0},{1} (({5},{6})) [{7},{8}]", oX, oY, depth,x,z, mux, muy, mux-Math.Floor(mux + 0.5), muy-Math.Floor(muy+0.5));
int scale = Settings.Default.blocksPerEmbarkTile;
// can account for depth of river here
for (int y = 0; y < height - depth - 2; y++)
{
chunk.Blocks.SetID(x, y, z, BlockType.STONE);
}
for (int y = height - depth - 2; y < height - depth - 1; y++)
{
chunk.Blocks.SetID(x, y, z, BlockType.GRAVEL);
}
for (int y = height - depth - 1; y < height; y++)
{
chunk.Blocks.SetID(x, y, z, BlockType.WATER);
}
for (int y = height; y < height + 1; y++)//Just to blockupdate for flowing water(falls).
{
chunk.Blocks.SetID(x, y, z, BlockType.AIR);
}
}
else if (BiomeList.biomes[biomeIndex].mineCraftBiome == BiomeID.DeepOcean && waterLevel <= height)
{
//make beaches
chunk.Biomes.SetBiome(x, z, BiomeType.Beach);
height = 98 + shift;
for (int y = 0; y < height - 4; y++)
{
chunk.Blocks.SetID(x, y, z, BlockType.STONE);
}
for (int y = height - 4; y < height - 3; y++)
{
chunk.Blocks.SetID(x, y, z, BlockType.SANDSTONE);
}
for (int y = height - 3; y < height; y++)
{
chunk.Blocks.SetID(x, y, z, BlockType.SAND);
}
}
else
{
// Create the rest, according to biome
for (int y = 2; y < height; y++)
{
if (y >= chunk.Blocks.YDim) break;
chunk.Blocks.SetID(x, y, z, BiomeList.biomes[biomeIndex].getBlockID(height - y, x + (chunk.X * 16), z + (chunk.Z * 16)));
}
}
//// Create Oceans and lakes
for (int y = height; y < waterLevel; y++)
{
if (y < 2) continue;
if (y >= chunk.Blocks.YDim) break;
chunk.Blocks.SetID(x, y, z, BlockType.STATIONARY_WATER);
}
// Fill caves
for (int y = 2; y < height; y++)
{
if (y >= chunk.Blocks.YDim) break;
int caveID = currentCaveMap.getCaveBlock(bound.mux, y - shift, bound.muy);
if (caveID == -2)
break;
if (caveID >= 0)
chunk.Blocks.SetID(x, y, z, caveID);
}
// Populate Structures
int tunnelHeight = 5;
int tunnelDepth = 10;
// structureNeighbor:
// 1 2 3
// 4 X 5
// 6 7 8
//
switch (structure)
{
case Structures.Type.Underground_Road:
chunk.Blocks.SetID(x, height - tunnelDepth - tunnelHeight, z, BlockType.COBBLESTONE);
for (int y = height - tunnelHeight - tunnelDepth; y < height - tunnelDepth; y++)
{
chunk.Blocks.SetID(x, y, z, BlockType.AIR);
}
chunk.Blocks.SetID(x, height - tunnelHeight, z, BlockType.COBBLESTONE);
break;
case Structures.Type.Road: // road
for (int y = height - 2; y < height; y++)
{
chunk.Blocks.SetID(x, y, z, BlockType.STONE_BRICK);
if (mapData[x + 1 - 1, z +1 ].structureType == Structures.Type.Bridge)
{
chunk.Blocks.SetID(x, height + 1, z, BlockType.COBBLESTONE_STAIRS);
chunk.Blocks.GetBlock(x, height + 1, z).Data = (int)StairOrientation.ASCEND_WEST;
}
if (mapData[x + 1 + 1, z + 1 ].structureType == Structures.Type.Bridge)
{
chunk.Blocks.SetID(x, height + 1, z, BlockType.COBBLESTONE_STAIRS);
chunk.Blocks.GetBlock(x, height + 1, z).Data = (int)StairOrientation.ASCEND_EAST;
}
}
break;
case Structures.Type.Bridge: // bridge
chunk.Blocks.SetID(x, height + 1, z, BlockType.HARDENED_CLAY);
break;
case Structures.Type.HumanCity1: // wheat?
chunk.Blocks.SetID(x, height - 1, z, BlockType.GOLD_BLOCK);
break;
case Structures.Type.HumanCity2: // potato
chunk.Blocks.SetID(x, height - 1, z, BlockType.HAY_BLOCK);
break;
case Structures.Type.HumanCity3: // carrot
chunk.Blocks.SetID(x, height - 1, z, BlockType.OBSIDIAN);
break;
case Structures.Type.HumanCity4: // carrot
chunk.Blocks.SetID(x, height - 1, z, BlockType.BIRCH_WOOD_STAIRS);
break;
case Structures.Type.Fortress: // Fort?
chunk.Blocks.SetID(x, height - 1, z, BlockType.IRON_BLOCK);
break;
case Structures.Type.ElvenCity1: // Farm?
chunk.Blocks.SetID(x, height - 1, z, BlockType.FARMLAND);
break;
case Structures.Type.ElvenCity2: // Farm?
chunk.Blocks.SetID(x, height - 1, z, BlockType.MELON);
break;
case Structures.Type.ElvenCity3: // Farm?
chunk.Blocks.SetID(x, height - 1, z, BlockType.PUMPKIN);
break;
default:
directions neighbor = getRoadEdge(mapData, x + 1, z + 1); // +1 for edge shift
switch (neighbor)
{
case directions.north:
case directions.south:
if (x % 4 < 3)
{
if (x % 8 == 1)
{
chunk.Blocks.SetID(x, height + 1, z, BlockType.TORCH);
currentWorld.
chunk.Blocks.GetBlock(x, height + 1, z).SetTileEntity( }
chunk.Blocks.SetID(x, height, z, BlockType.FENCE);
}
break;
case directions.west:
case directions.east:
if (z % 4 < 3)
{
chunk.Blocks.SetID(x, height, z, BlockType.FENCE);
if (z % 8 == 1)
{
chunk.Blocks.SetID(x, height + 1, z, BlockType.TORCH);
}
}
break;
}
break;
}
}
}
}
// North: 0;
// East: 1;
// South: 2;
// West: 3;
enum directions
{
none = -1,
north,
south,
west,
east,
northwest,
northeast,
southwest,
southeast
}
private directions getRoadEdge(internalData[,] map, int x, int y)
{
Structures.Type nw = map[x - 1, y + 1].structureType;
Structures.Type no = map[x - 0, y + 1].structureType;
Structures.Type ne = map[x + 1, y + 1].structureType;
Structures.Type ea = map[x - 1, y - 0].structureType;
Structures.Type we = map[x + 1, y - 0].structureType;
Structures.Type sw = map[x - 1, y - 1].structureType;
Structures.Type so = map[x - 0, y - 1].structureType;
Structures.Type se = map[x + 1, y - 1].structureType;
if (nw == Structures.Type.Road &&
no == Structures.Type.Road &&
ne == Structures.Type.Road) return directions.north;
if (sw == Structures.Type.Road &&
so == Structures.Type.Road &&
se == Structures.Type.Road) return directions.south;
if (nw == Structures.Type.Road &&
we == Structures.Type.Road &&
sw == Structures.Type.Road) return directions.west;
if (ne == Structures.Type.Road &&
ea == Structures.Type.Road &&
se == Structures.Type.Road) return directions.east;
return directions.none;
}
static void FlatChunk(ChunkRef chunk, int height)
{
// Create bedrock
for (int y = 0; y < 2; y++)
{
for (int x = 0; x < 16; x++)
{
for (int z = 0; z < 16; z++)
{
chunk.Blocks.SetID(x, y, z, (int)BlockType.BEDROCK);
}
}
}
// Create stone
for (int y = 2; y < height - 5; y++)
{
for (int x = 0; x < 16; x++)
{
for (int z = 0; z < 16; z++)
{
chunk.Blocks.SetID(x, y, z, (int)BlockType.STONE);
}
}
}
// Create dirt
for (int y = height - 5; y < height - 1; y++)
{
for (int x = 0; x < 16; x++)
{
for (int z = 0; z < 16; z++)
{
chunk.Blocks.SetID(x, y, z, (int)BlockType.DIRT);
}
}
}
// Create grass
for (int y = height - 1; y < height; y++)
{
for (int x = 0; x < 16; x++)
{
for (int z = 0; z < 16; z++)
{
chunk.Blocks.SetID(x, y, z, (int)BlockType.GRASS);
}
}
}
}
}
}