-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Foreman.cs
171 lines (143 loc) · 4.94 KB
/
Foreman.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
using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
public class Foreman {
//This is recommend max static octree size because it takes 134 MB
private volatile ITerraMesher mesher;
private volatile Octree octree;
//private int grassMeshID;
private volatile Weltschmerz weltschmerz;
private volatile Terra terra;
private int maxViewDistance;
private float fov;
private ChunkFiller chunkFiller;
private ConcurrentQueue<Tuple<int, int, int>> queue;
private Position[] localCenters;
private volatile bool runThread = true;
private volatile List<long> chunkSpeed;
private volatile LoadMarker loadMarker = null;
public volatile static int chunksLoaded = 0;
public volatile static int chunksPlaced = 0;
public volatile static int positionsScreened = 0;
public volatile static int positionsInRange = 0;
public volatile static int positionsNeeded = 0;
private ConcurrentDictionary<Tuple<int, int, int>, OctreeNode> leafOctants;
private Stopwatch stopwatch;
private ITerraSemaphore preparation;
private ITerraSemaphore generation;
private int Length = 0;
private int maxSize;
private int preparationThreads;
public Foreman (Weltschmerz weltschmerz, Terra terra, Registry registry, ITerraMesher mesher,
int viewDistance, float fov, int preparationThreads, ITerraSemaphore preparation, ITerraSemaphore generation) {
this.preparationThreads = preparationThreads;
this.preparation = preparation;
this.generation = generation;
this.weltschmerz = weltschmerz;
this.terra = terra;
this.octree = terra.GetOctree ();
queue = new ConcurrentQueue<Tuple<int, int, int>> ();
maxViewDistance = viewDistance;
this.fov = fov;
this.mesher = mesher;
chunkSpeed = new List<long> ();
stopwatch = new Stopwatch ();
this.leafOctants = new ConcurrentDictionary<Tuple<int, int, int>, OctreeNode> ();
List<Position> localCenters = new List<Position> ();
for (int l = -maxViewDistance; l < maxViewDistance; l += 8) {
for (int y = -Utils.GetPosFromFOV (fov, l); y < Utils.GetPosFromFOV (fov, l); y += 8) {
for (int x = -Utils.GetPosFromFOV (fov, l); x < Utils.GetPosFromFOV (fov, l); x += 8) {
localCenters.Add (new Position (x, y, -l));
}
}
}
this.localCenters = localCenters.ToArray ();
localCenters.Clear ();
maxSize = this.localCenters.Length;
}
public void Release () {
queue = new ConcurrentQueue<Tuple<int, int, int>> ();
Length = 0;
for (int i = 0; i < preparationThreads; i++) {
preparation.Post ();
}
}
public void AddLoadMarker (LoadMarker loadMarker) {
if (this.loadMarker == null) {
this.loadMarker = loadMarker;
Release ();
}
}
//Initial generation
public void Process () {
while (runThread) {
preparation.Wait ();
if (Length < maxSize) {
Position pos = localCenters[Length];
pos = loadMarker.ToGlobal(pos) / 8;
Tuple<int, int, int> key = new Tuple<int, int, int> (pos.x, pos.y, pos.z);
OctreeNode node = null;
if (!leafOctants.ContainsKey (key) || leafOctants.TryGetValue (key, out node)) {
if (node == null) {
node = terra.TraverseOctree (pos.x, pos.y, pos.z, 0);
}
if (node != null && node.chunk == null) {
leafOctants.TryAdd (key, node);
queue.Enqueue (key);
generation.Post ();
}
}
preparation.Post ();
Length++;
}
}
}
public void Generate () {
ArrayPool<Position> pool = ArrayPool<Position>.Create (Constants.CHUNK_SIZE3D * 4 * 6, 1);
while (runThread) {
generation.Wait ();
Tuple<int, int, int> pos;
OctreeNode node;
if (queue.TryDequeue (out pos)) {
if (terra.CheckBoundries (pos.Item1, pos.Item2, pos.Item3) && leafOctants.TryGetValue (pos, out node)) {
LoadArea (pos, node, pool);
}
}
}
}
//Loads chunks
private void LoadArea (Tuple<int, int, int> pos, OctreeNode node, ArrayPool<Position> pool) {
Chunk chunk;
if (pos.Item2 << Constants.CHUNK_EXPONENT > weltschmerz.GetConfig ().elevation.max_elevation) {
chunk = new Chunk ();
chunk.IsEmpty = true;
chunk.x = (uint) pos.Item1 << Constants.CHUNK_EXPONENT;
chunk.y = (uint) pos.Item2 << Constants.CHUNK_EXPONENT;
chunk.z = (uint) pos.Item3 << Constants.CHUNK_EXPONENT;
} else {
chunk = chunkFiller.GenerateChunk (pos.Item1 << Constants.CHUNK_EXPONENT, pos.Item2 << Constants.CHUNK_EXPONENT,
pos.Item3 << Constants.CHUNK_EXPONENT, weltschmerz);
if (!chunk.IsSurface) {
chunk.x = (uint) pos.Item1 << Constants.CHUNK_EXPONENT;
chunk.y = (uint) pos.Item2 << Constants.CHUNK_EXPONENT;
chunk.z = (uint) pos.Item3 << Constants.CHUNK_EXPONENT;
}
}
if (!chunk.IsEmpty) {
mesher.MeshChunk (chunk, pool);
chunksPlaced++;
}
node.chunk = chunk;
}
public void SetMaterials (Registry registry) {
chunkFiller = new ChunkFiller (registry.SelectByName ("dirt").worldID, registry.SelectByName ("grass").worldID);
}
public void Stop () {
runThread = false;
}
public List<long> GetMeasures () {
return chunkSpeed;
}
}