Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
TerrainPass.cs
Go to the documentation of this file.
1using System;
2using Terraria.IO;
4
6
7public class TerrainPass : GenPass
8{
9 private enum TerrainFeatureType
10 {
11 Plateau,
12 Hill,
13 Dale,
15 Valley
16 }
17
18 private class SurfaceHistory
19 {
20 private readonly double[] _heights;
21
22 private int _index;
23
24 public double this[int index]
25 {
26 get
27 {
28 return _heights[(index + _index) % _heights.Length];
29 }
30 set
31 {
32 _heights[(index + _index) % _heights.Length] = value;
33 }
34 }
35
36 public int Length => _heights.Length;
37
38 public SurfaceHistory(int size)
39 {
40 _heights = new double[size];
41 }
42
43 public void Record(double height)
44 {
45 _heights[_index] = height;
46 _index = (_index + 1) % _heights.Length;
47 }
48 }
49
50 public TerrainPass()
51 : base("Terrain", 449.3721923828125)
52 {
53 }
54
55 protected override void ApplyPass(GenerationProgress progress, GameConfiguration configuration)
56 {
57 int num = configuration.Get<int>("FlatBeachPadding");
58 progress.Message = Lang.gen[0].Value;
59 TerrainFeatureType terrainFeatureType = TerrainFeatureType.Plateau;
60 int num2 = 0;
61 double num3 = (double)Main.maxTilesY * 0.3;
62 num3 *= (double)GenBase._random.Next(90, 110) * 0.005;
63 double num4 = num3 + (double)Main.maxTilesY * 0.2;
64 num4 *= (double)GenBase._random.Next(90, 110) * 0.01;
66 {
67 num4 = (double)Main.maxTilesY * 0.5;
68 if (Main.maxTilesX > 2500)
69 {
70 num4 = (double)Main.maxTilesY * 0.6;
71 }
72 num4 *= (double)GenBase._random.Next(95, 106) * 0.01;
73 }
74 double num5 = num3;
75 double num6 = num3;
76 double num7 = num4;
77 double num8 = num4;
78 double num9 = (double)Main.maxTilesY * 0.23;
79 SurfaceHistory surfaceHistory = new SurfaceHistory(500);
80 num2 = GenVars.leftBeachEnd + num;
81 for (int i = 0; i < Main.maxTilesX; i++)
82 {
83 progress.Set((double)i / (double)Main.maxTilesX);
84 num5 = Math.Min(num3, num5);
85 num6 = Math.Max(num3, num6);
86 num7 = Math.Min(num4, num7);
87 num8 = Math.Max(num4, num8);
88 if (num2 <= 0)
89 {
90 terrainFeatureType = (TerrainFeatureType)GenBase._random.Next(0, 5);
91 num2 = GenBase._random.Next(5, 40);
92 if (terrainFeatureType == TerrainFeatureType.Plateau)
93 {
94 num2 *= (int)((double)GenBase._random.Next(5, 30) * 0.2);
95 }
96 }
97 num2--;
98 if ((double)i > (double)Main.maxTilesX * 0.45 && (double)i < (double)Main.maxTilesX * 0.55 && (terrainFeatureType == TerrainFeatureType.Mountain || terrainFeatureType == TerrainFeatureType.Valley))
99 {
100 terrainFeatureType = (TerrainFeatureType)GenBase._random.Next(3);
101 }
102 if ((double)i > (double)Main.maxTilesX * 0.48 && (double)i < (double)Main.maxTilesX * 0.52)
103 {
104 terrainFeatureType = TerrainFeatureType.Plateau;
105 }
106 num3 += GenerateWorldSurfaceOffset(terrainFeatureType);
107 double num10 = 0.17;
108 double num11 = 0.26;
110 {
111 num10 = 0.15;
112 num11 = 0.28;
113 }
114 if (i < GenVars.leftBeachEnd + num || i > GenVars.rightBeachStart - num)
115 {
116 num3 = Utils.Clamp(num3, (double)Main.maxTilesY * 0.17, num9);
117 }
118 else if (num3 < (double)Main.maxTilesY * num10)
119 {
120 num3 = (double)Main.maxTilesY * num10;
121 num2 = 0;
122 }
123 else if (num3 > (double)Main.maxTilesY * num11)
124 {
125 num3 = (double)Main.maxTilesY * num11;
126 num2 = 0;
127 }
128 while (GenBase._random.Next(0, 3) == 0)
129 {
130 num4 += (double)GenBase._random.Next(-2, 3);
131 }
133 {
134 if (Main.maxTilesX > 2500)
135 {
136 if (num4 > (double)Main.maxTilesY * 0.7)
137 {
138 num4 -= 1.0;
139 }
140 }
141 else if (num4 > (double)Main.maxTilesY * 0.6)
142 {
143 num4 -= 1.0;
144 }
145 }
146 else
147 {
148 if (num4 < num3 + (double)Main.maxTilesY * 0.06)
149 {
150 num4 += 1.0;
151 }
152 if (num4 > num3 + (double)Main.maxTilesY * 0.35)
153 {
154 num4 -= 1.0;
155 }
156 }
157 surfaceHistory.Record(num3);
158 FillColumn(i, num3, num4);
159 if (i == GenVars.rightBeachStart - num)
160 {
161 if (num3 > num9)
162 {
163 RetargetSurfaceHistory(surfaceHistory, i, num9);
164 }
165 terrainFeatureType = TerrainFeatureType.Plateau;
166 num2 = Main.maxTilesX - i;
167 }
168 }
169 Main.worldSurface = (int)(num6 + 25.0);
170 Main.rockLayer = num8;
171 double num12 = (int)((Main.rockLayer - Main.worldSurface) / 6.0) * 6;
172 Main.rockLayer = (int)(Main.worldSurface + num12);
173 int num13 = (int)(Main.rockLayer + (double)Main.maxTilesY) / 2 + GenBase._random.Next(-100, 20);
174 int lavaLine = num13 + GenBase._random.Next(50, 80);
176 {
177 lavaLine = (int)(Main.worldSurface * 4.0 + num4) / 5;
178 }
179 int num14 = 20;
180 if (num7 < num6 + (double)num14)
181 {
182 double num15 = (num7 + num6) / 2.0;
183 double num16 = Math.Abs(num7 - num6);
184 if (num16 < (double)num14)
185 {
186 num16 = num14;
187 }
188 num7 = num15 + num16 / 2.0;
189 num6 = num15 - num16 / 2.0;
190 }
191 GenVars.rockLayer = num4;
192 GenVars.rockLayerHigh = num8;
193 GenVars.rockLayerLow = num7;
194 GenVars.worldSurface = num3;
195 GenVars.worldSurfaceHigh = num6;
196 GenVars.worldSurfaceLow = num5;
197 GenVars.waterLine = num13;
198 GenVars.lavaLine = lavaLine;
199 }
200
201 private static void FillColumn(int x, double worldSurface, double rockLayer)
202 {
203 for (int i = 0; (double)i < worldSurface; i++)
204 {
205 Main.tile[x, i].active(active: false);
206 Main.tile[x, i].frameX = -1;
207 Main.tile[x, i].frameY = -1;
208 }
209 for (int j = (int)worldSurface; j < Main.maxTilesY; j++)
210 {
211 if ((double)j < rockLayer)
212 {
213 Main.tile[x, j].active(active: true);
214 Main.tile[x, j].type = 0;
215 Main.tile[x, j].frameX = -1;
216 Main.tile[x, j].frameY = -1;
217 }
218 else
219 {
220 Main.tile[x, j].active(active: true);
221 Main.tile[x, j].type = 1;
222 Main.tile[x, j].frameX = -1;
223 Main.tile[x, j].frameY = -1;
224 }
225 }
226 }
227
228 private static void RetargetColumn(int x, double worldSurface)
229 {
230 for (int i = 0; (double)i < worldSurface; i++)
231 {
232 Main.tile[x, i].active(active: false);
233 Main.tile[x, i].frameX = -1;
234 Main.tile[x, i].frameY = -1;
235 }
236 for (int j = (int)worldSurface; j < Main.maxTilesY; j++)
237 {
238 if (Main.tile[x, j].type != 1 || !Main.tile[x, j].active())
239 {
240 Main.tile[x, j].active(active: true);
241 Main.tile[x, j].type = 0;
242 Main.tile[x, j].frameX = -1;
243 Main.tile[x, j].frameY = -1;
244 }
245 }
246 }
247
248 private static double GenerateWorldSurfaceOffset(TerrainFeatureType featureType)
249 {
250 double num = 0.0;
252 {
253 switch (featureType)
254 {
255 case TerrainFeatureType.Plateau:
256 while (GenBase._random.Next(0, 6) == 0)
257 {
258 num += (double)GenBase._random.Next(-1, 2);
259 }
260 break;
261 case TerrainFeatureType.Hill:
262 while (GenBase._random.Next(0, 3) == 0)
263 {
264 num -= 1.0;
265 }
266 while (GenBase._random.Next(0, 10) == 0)
267 {
268 num += 1.0;
269 }
270 break;
271 case TerrainFeatureType.Dale:
272 while (GenBase._random.Next(0, 3) == 0)
273 {
274 num += 1.0;
275 }
276 while (GenBase._random.Next(0, 10) == 0)
277 {
278 num -= 1.0;
279 }
280 break;
281 case TerrainFeatureType.Mountain:
282 while (GenBase._random.Next(0, 3) != 0)
283 {
284 num -= 1.0;
285 }
286 while (GenBase._random.Next(0, 6) == 0)
287 {
288 num += 1.0;
289 }
290 break;
291 case TerrainFeatureType.Valley:
292 while (GenBase._random.Next(0, 3) != 0)
293 {
294 num += 1.0;
295 }
296 while (GenBase._random.Next(0, 5) == 0)
297 {
298 num -= 1.0;
299 }
300 break;
301 }
302 }
303 else
304 {
305 switch (featureType)
306 {
307 case TerrainFeatureType.Plateau:
308 while (GenBase._random.Next(0, 7) == 0)
309 {
310 num += (double)GenBase._random.Next(-1, 2);
311 }
312 break;
313 case TerrainFeatureType.Hill:
314 while (GenBase._random.Next(0, 4) == 0)
315 {
316 num -= 1.0;
317 }
318 while (GenBase._random.Next(0, 10) == 0)
319 {
320 num += 1.0;
321 }
322 break;
323 case TerrainFeatureType.Dale:
324 while (GenBase._random.Next(0, 4) == 0)
325 {
326 num += 1.0;
327 }
328 while (GenBase._random.Next(0, 10) == 0)
329 {
330 num -= 1.0;
331 }
332 break;
333 case TerrainFeatureType.Mountain:
334 while (GenBase._random.Next(0, 2) == 0)
335 {
336 num -= 1.0;
337 }
338 while (GenBase._random.Next(0, 6) == 0)
339 {
340 num += 1.0;
341 }
342 break;
343 case TerrainFeatureType.Valley:
344 while (GenBase._random.Next(0, 2) == 0)
345 {
346 num += 1.0;
347 }
348 while (GenBase._random.Next(0, 5) == 0)
349 {
350 num -= 1.0;
351 }
352 break;
353 }
354 }
355 return num;
356 }
357
358 private static void RetargetSurfaceHistory(SurfaceHistory history, int targetX, double targetHeight)
359 {
360 for (int i = 0; i < history.Length / 2; i++)
361 {
362 if (history[history.Length - 1] <= targetHeight)
363 {
364 break;
365 }
366 for (int j = 0; j < history.Length - i * 2; j++)
367 {
368 double num = history[history.Length - j - 1];
369 num -= 1.0;
370 history[history.Length - j - 1] = num;
371 if (num <= targetHeight)
372 {
373 break;
374 }
375 }
376 }
377 for (int k = 0; k < history.Length; k++)
378 {
379 double worldSurface = history[history.Length - k - 1];
380 RetargetColumn(targetX - k, worldSurface);
381 }
382 }
383}
static byte Min(byte val1, byte val2)
Definition Math.cs:912
static double Abs(double value)
static byte Max(byte val1, byte val2)
Definition Math.cs:738
override void ApplyPass(GenerationProgress progress, GameConfiguration configuration)
static double GenerateWorldSurfaceOffset(TerrainFeatureType featureType)
static void RetargetSurfaceHistory(SurfaceHistory history, int targetX, double targetHeight)
static void RetargetColumn(int x, double worldSurface)
static void FillColumn(int x, double worldSurface, double rockLayer)
static LocalizedText[] gen
Definition Lang.cs:22
static int maxTilesY
Definition Main.cs:1116
static double worldSurface
Definition Main.cs:1272
static double rockLayer
Definition Main.cs:1274
static int maxTilesX
Definition Main.cs:1114
static Tile[,] tile
Definition Main.cs:1675
static UnifiedRandom _random
Definition GenBase.cs:9
static bool remixWorldGen
Definition WorldGen.cs:1148
static UnifiedRandom genRand
Definition WorldGen.cs:1215
static bool drunkWorldGen
Definition WorldGen.cs:1154
static bool getGoodWorldGen
Definition WorldGen.cs:1156