Skip to content

Commit 92184c3

Browse files
author
Vyacheslav
committed
Added flat world generator sample
1 parent eeb19e7 commit 92184c3

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
using System.Collections.Generic;
2+
using UnityEngine;
3+
4+
namespace VoxelGame.Voxel
5+
{
6+
public class FlatGenerator : IGenerator
7+
{
8+
private readonly VoxelSettingsSO voxelSettings;
9+
10+
public FlatGenerator(VoxelSettingsSO voxelSettings)
11+
{
12+
this.voxelSettings = voxelSettings;
13+
}
14+
15+
const uint p = 2147483647;
16+
const uint a = 16807;
17+
18+
private uint Rand(uint current)
19+
{
20+
current = a * current % p;
21+
return current;
22+
}
23+
24+
public void GenerateMap(Vector3Int position, ref byte[,,] map)
25+
{
26+
if (map == null)
27+
return;
28+
29+
int lengthX = map.GetLength(0);
30+
int lengthY = map.GetLength(1);
31+
int lengthZ = map.GetLength(2);
32+
33+
if (lengthX == 0 || lengthY == 0 || lengthZ == 0)
34+
return;
35+
36+
byte[] tmpMapColumn = new byte[lengthY];
37+
38+
for (int x = 0; x < lengthX; x++)
39+
for (int z = 0; z < lengthZ; z++)
40+
{
41+
GenerateMapColumn(position + new Vector3Int(x, 0, z), tmpMapColumn);
42+
43+
for (int y = 0; y < lengthY; y++)
44+
map[x, y, z] = tmpMapColumn[y];
45+
}
46+
}
47+
48+
public void GenerateChunkStructures(Vector2Int coords, ref byte[,,] map)
49+
{
50+
List<ChunkStructure> structures = new List<ChunkStructure>();
51+
52+
for (int x = -1; x <= 1; x++)
53+
for (int z = -1; z <= 1; z++)
54+
structures.AddRange(GetChunkStructures(coords + new Vector2Int(x, z)));
55+
56+
ChunkStructure.CreateStructures(coords, map, structures);
57+
}
58+
59+
private void GenerateMapColumn(Vector3Int position, byte[] mapY)
60+
{
61+
uint rndCurrent = (uint)(voxelSettings.Seed + position.x + position.z * 7 + voxelSettings.Seed * position.x);
62+
63+
byte lastVoxel = 0;
64+
int deep = 0;
65+
66+
for (int y = mapY.Length - 1; y >= 0; y--)
67+
{
68+
mapY[y] = GetVoxel(position + new Vector3Int(0, y, 0));
69+
deep = mapY[y] > 0 ? deep + 1 : 0;
70+
mapY[y] = FixVoxel(mapY[y], deep);
71+
72+
if (y < mapY.Length - 1)
73+
{
74+
rndCurrent = Rand(rndCurrent);
75+
mapY[y + 1] = FixPrevVoxel(mapY[y], lastVoxel, y, (int)(rndCurrent % 50));
76+
}
77+
78+
lastVoxel = mapY[y];
79+
}
80+
}
81+
82+
private List<ChunkStructure> GetChunkStructures(Vector2Int coords)
83+
{
84+
Vector3Int position = VoxelUtils.GetChunkPosition(coords);
85+
86+
uint rndCurrent = (uint)(position.x + position.z * 7 + voxelSettings.Seed);
87+
88+
List<ChunkStructure> structures = new List<ChunkStructure>();
89+
90+
for (int x = 0; x < VoxelData.chunkWidth; x++)
91+
for (int z = 0; z < VoxelData.chunkWidth; z++)
92+
{
93+
rndCurrent = Rand(rndCurrent);
94+
95+
if ((int)(rndCurrent % 400) != 1)
96+
continue;
97+
98+
byte[] mapColumn = new byte[VoxelData.chunkHeight];
99+
GenerateMapColumn(position + new Vector3Int(x, 0, z), mapColumn);
100+
101+
for (int y = VoxelData.chunkHeight - 1; y >= 0; y--)
102+
{
103+
Vector3Int v = new Vector3Int(x, y, z);
104+
105+
if (!VoxelUtils.IsVoxelInChunk(v))
106+
continue;
107+
108+
byte voxel = mapColumn[y];
109+
110+
if (voxel == Voxels.GRASS)
111+
{
112+
ChunkStructure s = new ChunkStructure();
113+
s.position = new Vector3Int(x, y + 1, z) + position;
114+
s.structure = voxelSettings.Structures.GetStructure("tree_1");
115+
structures.Add(s);
116+
117+
break;
118+
}
119+
}
120+
}
121+
122+
return structures;
123+
}
124+
125+
private byte GetVoxel(Vector3Int pos)
126+
{
127+
return pos.y > VoxelData.chunkHeight / 2 ? Voxels.AIR : Voxels.GRASS;
128+
}
129+
130+
private byte FixVoxel(byte voxel, int deep)
131+
{
132+
if (voxel == Voxels.GRASS)
133+
{
134+
if (deep == 1)
135+
{
136+
return Voxels.GRASS;
137+
}
138+
else if (deep < 4)
139+
{
140+
return Voxels.DIRT;
141+
}
142+
else
143+
{
144+
return Voxels.STONE;
145+
}
146+
}
147+
148+
return voxel;
149+
}
150+
151+
private byte FixPrevVoxel(byte voxel, byte lastVoxel, int y, int rnd100)
152+
{
153+
if (lastVoxel == 0 && y < VoxelData.chunkHeight - 1)
154+
if (voxel == Voxels.GRASS)
155+
if (rnd100 < 5)
156+
return Voxels.DECOR_GRASS;
157+
158+
return lastVoxel;
159+
}
160+
}
161+
}

VoxelGame_UnityProject/Assets/VoxelGame/Voxel/Scripts/Generators/FlatGenerator.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)