Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Framing.cs
Go to the documentation of this file.
3using Terraria.ID;
4
5namespace Terraria;
6
7public class Framing
8{
9 private struct BlockStyle
10 {
11 public bool top;
12
13 public bool bottom;
14
15 public bool left;
16
17 public bool right;
18
19 public BlockStyle(bool up, bool down, bool left, bool right)
20 {
21 top = up;
22 bottom = down;
23 this.left = left;
24 this.right = right;
25 }
26
27 public void Clear()
28 {
29 top = (bottom = (left = (right = false)));
30 }
31 }
32
33 private static Point16[][] selfFrame8WayLookup;
34
35 private static Point16[][] wallFrameLookup;
36
37 private static Point16 frameSize8Way;
38
39 private static Point16 wallFrameSize;
40
41 private static BlockStyle[] blockStyleLookup;
42
43 private static int[][] phlebasTileFrameNumberLookup;
44
45 private static int[][] lazureTileFrameNumberLookup;
46
47 private static int[][] centerWallFrameLookup;
48
49 public static void Initialize()
50 {
51 selfFrame8WayLookup = new Point16[256][];
52 frameSize8Way = new Point16(18, 18);
53 Add8WayLookup(0, 9, 3, 10, 3, 11, 3);
54 Add8WayLookup(1, 6, 3, 7, 3, 8, 3);
55 Add8WayLookup(2, 12, 0, 12, 1, 12, 2);
56 Add8WayLookup(3, 15, 2);
57 Add8WayLookup(4, 9, 0, 9, 1, 9, 2);
58 Add8WayLookup(5, 13, 2);
59 Add8WayLookup(6, 6, 4, 7, 4, 8, 4);
60 Add8WayLookup(7, 14, 2);
61 Add8WayLookup(8, 6, 0, 7, 0, 8, 0);
62 Add8WayLookup(9, 5, 0, 5, 1, 5, 2);
63 Add8WayLookup(10, 15, 0);
64 Add8WayLookup(11, 15, 1);
65 Add8WayLookup(12, 13, 0);
66 Add8WayLookup(13, 13, 1);
67 Add8WayLookup(14, 14, 0);
68 Add8WayLookup(15, 14, 1);
69 Add8WayLookup(19, 1, 4, 3, 4, 5, 4);
70 Add8WayLookup(23, 16, 3);
71 Add8WayLookup(27, 17, 0);
72 Add8WayLookup(31, 13, 4);
73 Add8WayLookup(37, 0, 4, 2, 4, 4, 4);
74 Add8WayLookup(39, 17, 3);
75 Add8WayLookup(45, 16, 0);
76 Add8WayLookup(47, 12, 4);
77 Add8WayLookup(55, 1, 2, 2, 2, 3, 2);
78 Add8WayLookup(63, 6, 2, 7, 2, 8, 2);
79 Add8WayLookup(74, 1, 3, 3, 3, 5, 3);
80 Add8WayLookup(75, 17, 1);
81 Add8WayLookup(78, 16, 2);
82 Add8WayLookup(79, 13, 3);
83 Add8WayLookup(91, 4, 0, 4, 1, 4, 2);
84 Add8WayLookup(95, 11, 0, 11, 1, 11, 2);
85 Add8WayLookup(111, 17, 4);
86 Add8WayLookup(127, 14, 3);
87 Add8WayLookup(140, 0, 3, 2, 3, 4, 3);
88 Add8WayLookup(141, 16, 1);
89 Add8WayLookup(142, 17, 2);
90 Add8WayLookup(143, 12, 3);
91 Add8WayLookup(159, 16, 4);
92 Add8WayLookup(173, 0, 0, 0, 1, 0, 2);
93 Add8WayLookup(175, 10, 0, 10, 1, 10, 2);
94 Add8WayLookup(191, 15, 3);
95 Add8WayLookup(206, 1, 0, 2, 0, 3, 0);
96 Add8WayLookup(207, 6, 1, 7, 1, 8, 1);
97 Add8WayLookup(223, 14, 4);
98 Add8WayLookup(239, 15, 4);
99 Add8WayLookup(255, 1, 1, 2, 1, 3, 1);
101 blockStyleLookup[0] = new BlockStyle(up: true, down: true, left: true, right: true);
102 blockStyleLookup[1] = new BlockStyle(up: false, down: true, left: true, right: true);
103 blockStyleLookup[2] = new BlockStyle(up: false, down: true, left: true, right: false);
104 blockStyleLookup[3] = new BlockStyle(up: false, down: true, left: false, right: true);
105 blockStyleLookup[4] = new BlockStyle(up: true, down: false, left: true, right: false);
106 blockStyleLookup[5] = new BlockStyle(up: true, down: false, left: false, right: true);
107 phlebasTileFrameNumberLookup = new int[4][]
108 {
109 new int[3] { 2, 4, 2 },
110 new int[3] { 1, 3, 1 },
111 new int[3] { 2, 2, 4 },
112 new int[3] { 1, 1, 3 }
113 };
114 lazureTileFrameNumberLookup = new int[2][]
115 {
116 new int[2] { 1, 3 },
117 new int[2] { 2, 4 }
118 };
119 centerWallFrameLookup = new int[3][]
120 {
121 new int[3] { 2, 0, 0 },
122 new int[3] { 0, 1, 4 },
123 new int[3] { 0, 3, 0 }
124 };
125 wallFrameLookup = new Point16[20][];
126 wallFrameSize = new Point16(36, 36);
127 AddWallFrameLookup(0, 9, 3, 10, 3, 11, 3, 6, 6);
128 AddWallFrameLookup(1, 6, 3, 7, 3, 8, 3, 4, 6);
129 AddWallFrameLookup(2, 12, 0, 12, 1, 12, 2, 12, 5);
130 AddWallFrameLookup(3, 1, 4, 3, 4, 5, 4, 3, 6);
131 AddWallFrameLookup(4, 9, 0, 9, 1, 9, 2, 9, 5);
132 AddWallFrameLookup(5, 0, 4, 2, 4, 4, 4, 2, 6);
133 AddWallFrameLookup(6, 6, 4, 7, 4, 8, 4, 5, 6);
134 AddWallFrameLookup(7, 1, 2, 2, 2, 3, 2, 3, 5);
135 AddWallFrameLookup(8, 6, 0, 7, 0, 8, 0, 6, 5);
136 AddWallFrameLookup(9, 5, 0, 5, 1, 5, 2, 5, 5);
137 AddWallFrameLookup(10, 1, 3, 3, 3, 5, 3, 1, 6);
138 AddWallFrameLookup(11, 4, 0, 4, 1, 4, 2, 4, 5);
139 AddWallFrameLookup(12, 0, 3, 2, 3, 4, 3, 0, 6);
140 AddWallFrameLookup(13, 0, 0, 0, 1, 0, 2, 0, 5);
141 AddWallFrameLookup(14, 1, 0, 2, 0, 3, 0, 1, 5);
142 AddWallFrameLookup(15, 1, 1, 2, 1, 3, 1, 2, 5);
143 AddWallFrameLookup(16, 6, 1, 7, 1, 8, 1, 7, 5);
144 AddWallFrameLookup(17, 6, 2, 7, 2, 8, 2, 8, 5);
145 AddWallFrameLookup(18, 10, 0, 10, 1, 10, 2, 10, 5);
146 AddWallFrameLookup(19, 11, 0, 11, 1, 11, 2, 11, 5);
147 }
148
149 private static BlockStyle FindBlockStyle(Tile blockTile)
150 {
151 return blockStyleLookup[blockTile.blockType()];
152 }
153
154 public static void Add8WayLookup(int lookup, short point1X, short point1Y, short point2X, short point2Y, short point3X, short point3Y)
155 {
156 Point16[] array = new Point16[3]
157 {
158 new Point16(point1X * frameSize8Way.X, point1Y * frameSize8Way.Y),
159 new Point16(point2X * frameSize8Way.X, point2Y * frameSize8Way.Y),
160 new Point16(point3X * frameSize8Way.X, point3Y * frameSize8Way.Y)
161 };
162 selfFrame8WayLookup[lookup] = array;
163 }
164
165 public static void Add8WayLookup(int lookup, short x, short y)
166 {
167 Point16[] array = new Point16[3]
168 {
169 new Point16(x * frameSize8Way.X, y * frameSize8Way.Y),
170 new Point16(x * frameSize8Way.X, y * frameSize8Way.Y),
171 new Point16(x * frameSize8Way.X, y * frameSize8Way.Y)
172 };
173 selfFrame8WayLookup[lookup] = array;
174 }
175
176 public static void AddWallFrameLookup(int lookup, short point1X, short point1Y, short point2X, short point2Y, short point3X, short point3Y, short point4X, short point4Y)
177 {
178 Point16[] array = new Point16[4]
179 {
180 new Point16(point1X * wallFrameSize.X, point1Y * wallFrameSize.Y),
181 new Point16(point2X * wallFrameSize.X, point2Y * wallFrameSize.Y),
182 new Point16(point3X * wallFrameSize.X, point3Y * wallFrameSize.Y),
183 new Point16(point4X * wallFrameSize.X, point4Y * wallFrameSize.Y)
184 };
185 wallFrameLookup[lookup] = array;
186 }
187
188 private static bool WillItBlend(ushort myType, ushort otherType)
189 {
190 if (TileID.Sets.ForcedDirtMerging[myType] && otherType == 0)
191 {
192 return true;
193 }
194 if (Main.tileBrick[myType] && Main.tileBrick[otherType])
195 {
196 return true;
197 }
198 return TileID.Sets.GemsparkFramingTypes[otherType] == TileID.Sets.GemsparkFramingTypes[myType];
199 }
200
201 public static void SelfFrame8Way(int i, int j, Tile centerTile, bool resetFrame)
202 {
203 if (!centerTile.active())
204 {
205 return;
206 }
207 BlockStyle blockStyle = FindBlockStyle(centerTile);
208 int num = 0;
209 BlockStyle blockStyle2 = default(BlockStyle);
210 if (blockStyle.top)
211 {
212 Tile tileSafely = GetTileSafely(i, j - 1);
213 if (tileSafely.active() && WillItBlend(centerTile.type, tileSafely.type))
214 {
215 blockStyle2 = FindBlockStyle(tileSafely);
216 if (blockStyle2.bottom)
217 {
218 num |= 1;
219 }
220 else
221 {
222 blockStyle2.Clear();
223 }
224 }
225 }
226 BlockStyle blockStyle3 = default(BlockStyle);
227 if (blockStyle.left)
228 {
229 Tile tileSafely2 = GetTileSafely(i - 1, j);
230 if (tileSafely2.active() && WillItBlend(centerTile.type, tileSafely2.type))
231 {
232 blockStyle3 = FindBlockStyle(tileSafely2);
233 if (blockStyle3.right)
234 {
235 num |= 2;
236 }
237 else
238 {
239 blockStyle3.Clear();
240 }
241 }
242 }
243 BlockStyle blockStyle4 = default(BlockStyle);
244 if (blockStyle.right)
245 {
246 Tile tileSafely3 = GetTileSafely(i + 1, j);
247 if (tileSafely3.active() && WillItBlend(centerTile.type, tileSafely3.type))
248 {
249 blockStyle4 = FindBlockStyle(tileSafely3);
250 if (blockStyle4.left)
251 {
252 num |= 4;
253 }
254 else
255 {
256 blockStyle4.Clear();
257 }
258 }
259 }
260 BlockStyle blockStyle5 = default(BlockStyle);
261 if (blockStyle.bottom)
262 {
263 Tile tileSafely4 = GetTileSafely(i, j + 1);
264 if (tileSafely4.active() && WillItBlend(centerTile.type, tileSafely4.type))
265 {
266 blockStyle5 = FindBlockStyle(tileSafely4);
267 if (blockStyle5.top)
268 {
269 num |= 8;
270 }
271 else
272 {
273 blockStyle5.Clear();
274 }
275 }
276 }
277 if (blockStyle2.left && blockStyle3.top)
278 {
279 Tile tileSafely5 = GetTileSafely(i - 1, j - 1);
280 if (tileSafely5.active() && WillItBlend(centerTile.type, tileSafely5.type))
281 {
282 BlockStyle blockStyle6 = FindBlockStyle(tileSafely5);
283 if (blockStyle6.right && blockStyle6.bottom)
284 {
285 num |= 0x10;
286 }
287 }
288 }
289 if (blockStyle2.right && blockStyle4.top)
290 {
291 Tile tileSafely6 = GetTileSafely(i + 1, j - 1);
292 if (tileSafely6.active() && WillItBlend(centerTile.type, tileSafely6.type))
293 {
294 BlockStyle blockStyle7 = FindBlockStyle(tileSafely6);
295 if (blockStyle7.left && blockStyle7.bottom)
296 {
297 num |= 0x20;
298 }
299 }
300 }
301 if (blockStyle5.left && blockStyle3.bottom)
302 {
303 Tile tileSafely7 = GetTileSafely(i - 1, j + 1);
304 if (tileSafely7.active() && WillItBlend(centerTile.type, tileSafely7.type))
305 {
306 BlockStyle blockStyle8 = FindBlockStyle(tileSafely7);
307 if (blockStyle8.right && blockStyle8.top)
308 {
309 num |= 0x40;
310 }
311 }
312 }
313 if (blockStyle5.right && blockStyle4.bottom)
314 {
315 Tile tileSafely8 = GetTileSafely(i + 1, j + 1);
316 if (tileSafely8.active() && WillItBlend(centerTile.type, tileSafely8.type))
317 {
318 BlockStyle blockStyle9 = FindBlockStyle(tileSafely8);
319 if (blockStyle9.left && blockStyle9.top)
320 {
321 num |= 0x80;
322 }
323 }
324 }
325 if (resetFrame)
326 {
327 centerTile.frameNumber((byte)WorldGen.genRand.Next(0, 3));
328 }
329 Point16 point = selfFrame8WayLookup[num][centerTile.frameNumber()];
330 centerTile.frameX = point.X;
331 centerTile.frameY = point.Y;
332 }
333
334 public static void WallFrame(int i, int j, bool resetFrame = false)
335 {
336 if (WorldGen.SkipFramingBecauseOfGen || i <= 0 || j <= 0 || i >= Main.maxTilesX - 1 || j >= Main.maxTilesY - 1 || Main.tile[i, j] == null)
337 {
338 return;
339 }
340 if (Main.tile[i, j].wall >= WallID.Count)
341 {
342 Main.tile[i, j].wall = 0;
343 }
345 Tile tile = Main.tile[i, j];
346 if (tile.wall == 0)
347 {
348 tile.wallColor(0);
350 return;
351 }
352 int num = 0;
353 bool flag = Main.ShouldShowInvisibleWalls();
354 if (j - 1 >= 0)
355 {
356 Tile tile2 = Main.tile[i, j - 1];
357 if (tile2 != null && (tile2.wall > 0 || (tile2.active() && tile2.type == 54)) && (flag || !tile2.invisibleWall()))
358 {
359 num = 1;
360 }
361 }
362 if (i - 1 >= 0)
363 {
364 Tile tile2 = Main.tile[i - 1, j];
365 if (tile2 != null && (tile2.wall > 0 || (tile2.active() && tile2.type == 54)) && (flag || !tile2.invisibleWall()))
366 {
367 num |= 2;
368 }
369 }
370 if (i + 1 <= Main.maxTilesX - 1)
371 {
372 Tile tile2 = Main.tile[i + 1, j];
373 if (tile2 != null && (tile2.wall > 0 || (tile2.active() && tile2.type == 54)) && (flag || !tile2.invisibleWall()))
374 {
375 num |= 4;
376 }
377 }
378 if (j + 1 <= Main.maxTilesY - 1)
379 {
380 Tile tile2 = Main.tile[i, j + 1];
381 if (tile2 != null && (tile2.wall > 0 || (tile2.active() && tile2.type == 54)) && (flag || !tile2.invisibleWall()))
382 {
383 num |= 8;
384 }
385 }
386 int num2 = 0;
387 if (Main.wallLargeFrames[tile.wall] == 1)
388 {
389 num2 = phlebasTileFrameNumberLookup[j % 4][i % 3] - 1;
390 tile.wallFrameNumber((byte)num2);
391 }
392 else if (Main.wallLargeFrames[tile.wall] == 2)
393 {
394 num2 = lazureTileFrameNumberLookup[i % 2][j % 2] - 1;
395 tile.wallFrameNumber((byte)num2);
396 }
397 else if (resetFrame)
398 {
399 num2 = WorldGen.genRand.Next(0, 3);
400 if (tile.wall == 21 && WorldGen.genRand.Next(2) == 0)
401 {
402 num2 = 2;
403 }
404 tile.wallFrameNumber((byte)num2);
405 }
406 else
407 {
408 num2 = tile.wallFrameNumber();
409 }
410 if (num == 15)
411 {
412 num += centerWallFrameLookup[i % 3][j % 3];
413 }
414 Point16 point = wallFrameLookup[num][num2];
415 tile.wallFrameX(point.X);
416 tile.wallFrameY(point.Y);
417 }
418
419 public static Tile GetTileSafely(Vector2 position)
420 {
421 position /= 16f;
422 return GetTileSafely((int)position.X, (int)position.Y);
423 }
424
425 public static Tile GetTileSafely(Point pt)
426 {
427 return GetTileSafely(pt.X, pt.Y);
428 }
429
430 public static Tile GetTileSafely(Point16 pt)
431 {
432 return GetTileSafely(pt.X, pt.Y);
433 }
434
435 public static Tile GetTileSafely(int i, int j)
436 {
437 if (!WorldGen.InWorld(i, j))
438 {
439 return new Tile();
440 }
441 Tile tile = Main.tile[i, j];
442 if (tile == null)
443 {
444 tile = new Tile();
445 Main.tile[i, j] = tile;
446 }
447 return tile;
448 }
449}
static Tile GetTileSafely(int i, int j)
Definition Framing.cs:435
static Point16[][] selfFrame8WayLookup
Definition Framing.cs:33
static Tile GetTileSafely(Point16 pt)
Definition Framing.cs:430
static BlockStyle[] blockStyleLookup
Definition Framing.cs:41
static int[][] centerWallFrameLookup
Definition Framing.cs:47
static Point16[][] wallFrameLookup
Definition Framing.cs:35
static void SelfFrame8Way(int i, int j, Tile centerTile, bool resetFrame)
Definition Framing.cs:201
static void Add8WayLookup(int lookup, short point1X, short point1Y, short point2X, short point2Y, short point3X, short point3Y)
Definition Framing.cs:154
static int[][] phlebasTileFrameNumberLookup
Definition Framing.cs:43
static void WallFrame(int i, int j, bool resetFrame=false)
Definition Framing.cs:334
static bool WillItBlend(ushort myType, ushort otherType)
Definition Framing.cs:188
static Tile GetTileSafely(Point pt)
Definition Framing.cs:425
static int[][] lazureTileFrameNumberLookup
Definition Framing.cs:45
static Point16 frameSize8Way
Definition Framing.cs:37
static Point16 wallFrameSize
Definition Framing.cs:39
static void AddWallFrameLookup(int lookup, short point1X, short point1Y, short point2X, short point2Y, short point3X, short point3Y, short point4X, short point4Y)
Definition Framing.cs:176
static BlockStyle FindBlockStyle(Tile blockTile)
Definition Framing.cs:149
static void Initialize()
Definition Framing.cs:49
static Tile GetTileSafely(Vector2 position)
Definition Framing.cs:419
static void Add8WayLookup(int lookup, short x, short y)
Definition Framing.cs:165
static bool[] ForcedDirtMerging
Definition TileID.cs:115
static ushort[] GemsparkFramingTypes
Definition TileID.cs:173
static readonly ushort Count
Definition WallID.cs:753
static int maxTilesY
Definition Main.cs:1116
static byte[] wallLargeFrames
Definition Main.cs:1479
static bool ShouldShowInvisibleWalls()
Definition Main.cs:54895
static bool[] tileBrick
Definition Main.cs:1483
static int maxTilesX
Definition Main.cs:1114
static Tile[,] tile
Definition Main.cs:1675
bool invisibleWall()
Definition Tile.cs:521
int wallFrameY()
Definition Tile.cs:460
void ClearWallPaintAndCoating()
Definition Tile.cs:876
ushort type
Definition Tile.cs:8
byte frameNumber()
Definition Tile.cs:440
bool active()
Definition Tile.cs:565
int wallFrameX()
Definition Tile.cs:430
int blockType()
Definition Tile.cs:219
byte wallFrameNumber()
Definition Tile.cs:450
ushort wall
Definition Tile.cs:10
byte wallColor()
Definition Tile.cs:352
static UnifiedRandom genRand
Definition WorldGen.cs:1215
static bool UpdateMapTile(int i, int j, bool addToList=true)
static bool SkipFramingBecauseOfGen
Definition WorldGen.cs:1235
static bool InWorld(int x, int y, int fluff=0)
Definition WorldGen.cs:5816
BlockStyle(bool up, bool down, bool left, bool right)
Definition Framing.cs:19