Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
MarbleBiome.cs
Go to the documentation of this file.
1using System;
3using Terraria.ID;
5
7
8public class MarbleBiome : MicroBiome
9{
10 private delegate bool SlabState(int x, int y, int scale);
11
12 private static class SlabStates
13 {
14 public static bool Empty(int x, int y, int scale)
15 {
16 return false;
17 }
18
19 public static bool Solid(int x, int y, int scale)
20 {
21 return true;
22 }
23
24 public static bool HalfBrick(int x, int y, int scale)
25 {
26 return y >= scale / 2;
27 }
28
29 public static bool BottomRightFilled(int x, int y, int scale)
30 {
31 return x >= scale - y;
32 }
33
34 public static bool BottomLeftFilled(int x, int y, int scale)
35 {
36 return x < y;
37 }
38
39 public static bool TopRightFilled(int x, int y, int scale)
40 {
41 return x > y;
42 }
43
44 public static bool TopLeftFilled(int x, int y, int scale)
45 {
46 return x < scale - y;
47 }
48 }
49
50 private struct Slab
51 {
52 public readonly SlabState State;
53
54 public readonly bool HasWall;
55
56 public bool IsSolid => State != new SlabState(SlabStates.Empty);
57
58 private Slab(SlabState state, bool hasWall)
59 {
60 State = state;
61 HasWall = hasWall;
62 }
63
65 {
66 return new Slab(state, HasWall);
67 }
68
69 public static Slab Create(SlabState state, bool hasWall)
70 {
71 return new Slab(state, hasWall);
72 }
73 }
74
75 private const int SCALE = 3;
76
77 private Slab[,] _slabs;
78
79 private void SmoothSlope(int x, int y)
80 {
81 Slab slab = _slabs[x, y];
82 if (slab.IsSolid)
83 {
84 bool isSolid = _slabs[x, y - 1].IsSolid;
85 bool isSolid2 = _slabs[x, y + 1].IsSolid;
86 bool isSolid3 = _slabs[x - 1, y].IsSolid;
87 bool isSolid4 = _slabs[x + 1, y].IsSolid;
88 switch (((isSolid ? 1 : 0) << 3) | ((isSolid2 ? 1 : 0) << 2) | ((isSolid3 ? 1 : 0) << 1) | (isSolid4 ? 1 : 0))
89 {
90 case 10:
92 break;
93 case 9:
95 break;
96 case 6:
98 break;
99 case 5:
101 break;
102 case 4:
103 _slabs[x, y] = slab.WithState(SlabStates.HalfBrick);
104 break;
105 default:
106 _slabs[x, y] = slab.WithState(SlabStates.Solid);
107 break;
108 }
109 }
110 }
111
112 private void PlaceSlab(Slab slab, int originX, int originY, int scale)
113 {
114 ushort num = 367;
115 ushort wall = 178;
117 {
118 num = 368;
119 wall = 180;
120 }
121 int num2 = -1;
122 int num3 = scale + 1;
123 int num4 = 0;
124 int num5 = scale;
125 for (int i = num2; i < num3; i++)
126 {
127 if ((i == num2 || i == num3 - 1) && WorldGen.genRand.Next(2) == 0)
128 {
129 continue;
130 }
131 if (WorldGen.genRand.Next(2) == 0)
132 {
133 num4--;
134 }
135 if (WorldGen.genRand.Next(2) == 0)
136 {
137 num5++;
138 }
139 for (int j = num4; j < num5; j++)
140 {
141 Tile tile = GenBase._tiles[originX + i, originY + j];
142 tile.ResetToType(TileID.Sets.Ore[tile.type] ? tile.type : num);
143 bool active = slab.State(i, j, scale);
144 tile.active(active);
145 if (slab.HasWall)
146 {
147 tile.wall = wall;
148 }
149 WorldUtils.TileFrame(originX + i, originY + j, frameNeighbors: true);
150 WorldGen.SquareWallFrame(originX + i, originY + j);
151 Tile.SmoothSlope(originX + i, originY + j);
152 if (WorldGen.SolidTile(originX + i, originY + j - 1) && GenBase._random.Next(4) == 0)
153 {
154 WorldGen.PlaceTight(originX + i, originY + j);
155 }
156 if (WorldGen.SolidTile(originX + i, originY + j) && GenBase._random.Next(4) == 0)
157 {
158 WorldGen.PlaceTight(originX + i, originY + j - 1);
159 }
160 }
161 }
162 }
163
164 private static bool IsGroupSolid(int x, int y, int scale)
165 {
166 int num = 0;
167 for (int i = 0; i < scale; i++)
168 {
169 for (int j = 0; j < scale; j++)
170 {
171 if (WorldGen.SolidOrSlopedTile(x + i, y + j))
172 {
173 num++;
174 }
175 }
176 }
177 return num > scale / 4 * 3;
178 }
179
180 public override bool Place(Point origin, StructureMap structures)
181 {
182 if (WorldGen.BiomeTileCheck(origin.X, origin.Y))
183 {
184 return false;
185 }
186 if (_slabs == null)
187 {
188 _slabs = new Slab[56, 26];
189 }
190 int num = GenBase._random.Next(80, 150) / 3;
191 int num2 = GenBase._random.Next(40, 60) / 3;
192 int num3 = (num2 * 3 - GenBase._random.Next(20, 30)) / 3;
193 origin.X -= num * 3 / 2;
194 origin.Y -= num2 * 3 / 2;
195 for (int i = -1; i < num + 1; i++)
196 {
197 double num4 = (double)(i - num / 2) / (double)num + 0.5;
198 int num5 = (int)((0.5 - Math.Abs(num4 - 0.5)) * 5.0) - 2;
199 for (int j = -1; j < num2 + 1; j++)
200 {
201 bool hasWall = true;
202 bool flag = false;
203 bool flag2 = IsGroupSolid(i * 3 + origin.X, j * 3 + origin.Y, 3);
204 int num6 = Math.Abs(j - num2 / 2) - num3 / 4 + num5;
205 if (num6 > 3)
206 {
207 flag = flag2;
208 hasWall = false;
209 }
210 else if (num6 > 0)
211 {
212 flag = j - num2 / 2 > 0 || flag2;
213 hasWall = j - num2 / 2 < 0 || num6 <= 2;
214 }
215 else if (num6 == 0)
216 {
217 flag = GenBase._random.Next(2) == 0 && (j - num2 / 2 > 0 || flag2);
218 }
219 if (Math.Abs(num4 - 0.5) > 0.35 + GenBase._random.NextDouble() * 0.1 && !flag2)
220 {
221 hasWall = false;
222 flag = false;
223 }
224 _slabs[i + 1, j + 1] = Slab.Create(flag ? new SlabState(SlabStates.Solid) : new SlabState(SlabStates.Empty), hasWall);
225 }
226 }
227 for (int k = 0; k < num; k++)
228 {
229 for (int l = 0; l < num2; l++)
230 {
231 SmoothSlope(k + 1, l + 1);
232 }
233 }
234 int num7 = num / 2;
235 int num8 = num2 / 2;
236 int num9 = (num8 + 1) * (num8 + 1);
237 double value = GenBase._random.NextDouble() * 2.0 - 1.0;
238 double num10 = GenBase._random.NextDouble() * 2.0 - 1.0;
239 double value2 = GenBase._random.NextDouble() * 2.0 - 1.0;
240 double num11 = 0.0;
241 for (int m = 0; m <= num; m++)
242 {
243 double num12 = (double)num8 / (double)num7 * (double)(m - num7);
244 int num13 = Math.Min(num8, (int)Math.Sqrt(Math.Max(0.0, (double)num9 - num12 * num12)));
245 num11 = ((m >= num / 2) ? (num11 + Utils.Lerp(num10, value2, (double)m / (double)(num / 2) - 1.0)) : (num11 + Utils.Lerp(value, num10, (double)m / (double)(num / 2))));
246 for (int n = num8 - num13; n <= num8 + num13; n++)
247 {
248 PlaceSlab(_slabs[m + 1, n + 1], m * 3 + origin.X, n * 3 + origin.Y + (int)num11, 3);
249 }
250 }
251 structures.AddStructure(new Rectangle(origin.X, origin.Y, num * 3, num2 * 3), 8);
252 return true;
253 }
254}
static byte Min(byte val1, byte val2)
Definition Math.cs:912
static double Sqrt(double d)
static double Abs(double value)
static byte Max(byte val1, byte val2)
Definition Math.cs:738
static bool Empty(int x, int y, int scale)
static bool Solid(int x, int y, int scale)
static bool HalfBrick(int x, int y, int scale)
static bool TopRightFilled(int x, int y, int scale)
static bool BottomLeftFilled(int x, int y, int scale)
static bool BottomRightFilled(int x, int y, int scale)
static bool TopLeftFilled(int x, int y, int scale)
static bool IsGroupSolid(int x, int y, int scale)
delegate bool SlabState(int x, int y, int scale)
override bool Place(Point origin, StructureMap structures)
void PlaceSlab(Slab slab, int originX, int originY, int scale)
static bool[] Ore
Definition TileID.cs:295
static void SmoothSlope(int x, int y, bool applyToNeighbors=true, bool sync=false)
Definition Tile.cs:759
void ResetToType(ushort type)
Definition Tile.cs:266
ushort type
Definition Tile.cs:8
bool active()
Definition Tile.cs:565
static double Lerp(double value1, double value2, double amount)
Definition Utils.cs:64
static UnifiedRandom _random
Definition GenBase.cs:9
void AddStructure(Rectangle area, int padding=0)
static void TileFrame(int x, int y, bool frameNeighbors=false)
Definition WorldUtils.cs:61
static bool BiomeTileCheck(int x, int y)
Definition WorldGen.cs:7113
static bool SolidTile(Tile testTile)
static UnifiedRandom genRand
Definition WorldGen.cs:1215
static void SquareWallFrame(int i, int j, bool resetFrame=true)
static bool drunkWorldGen
Definition WorldGen.cs:1154
static void PlaceTight(int x, int y, bool spiders=false)
static bool SolidOrSlopedTile(Tile tile)
static Slab Create(SlabState state, bool hasWall)
Slab(SlabState state, bool hasWall)