Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
WorldSections.cs
Go to the documentation of this file.
1using System;
4
5namespace Terraria;
6
7public class WorldSections
8{
9 private struct IterationState
10 {
12
13 public int X;
14
15 public int Y;
16
17 public int leg;
18
19 public int xDir;
20
21 public int yDir;
22
23 public void Reset()
24 {
25 centerPos = new Vector2(-3200f, -2400f);
26 X = 0;
27 Y = 0;
28 leg = 0;
29 xDir = 0;
30 yDir = 0;
31 }
32 }
33
34 public const int BitIndex_SectionLoaded = 0;
35
36 public const int BitIndex_SectionFramed = 1;
37
38 public const int BitIndex_SectionMapDrawn = 2;
39
40 public const int BitIndex_SectionNeedsRefresh = 3;
41
42 private int width;
43
44 private int height;
45
46 private BitsByte[] data;
47
48 private int mapSectionsLeft;
49
50 private int frameSectionsLeft;
51
53
55
57
59
61
62 public WorldSections(int numSectionsX, int numSectionsY)
63 {
64 width = numSectionsX;
65 height = numSectionsY;
66 data = new BitsByte[width * height];
69 prevMap.Reset();
70 }
71
72 public void SetSectionAsRefreshed(int x, int y)
73 {
74 if (x >= 0)
75 {
76 _ = width;
77 }
78 if (y >= 0)
79 {
80 _ = height;
81 }
82 if (data[y * width + x][3])
83 {
84 data[y * width + x][3] = false;
86 }
87 }
88
89 public bool SectionNeedsRefresh(int x, int y)
90 {
91 if (x < 0 || x >= width)
92 {
93 return false;
94 }
95 if (y < 0 || y >= height)
96 {
97 return false;
98 }
99 return data[y * width + x][3];
100 }
101
103 {
104 for (int i = 0; i < data.Length; i++)
105 {
106 if (data[i][1])
107 {
108 data[i][3] = true;
110 }
111 }
112 }
113
114 public bool SectionLoaded(int x, int y)
115 {
116 if (x < 0 || x >= width)
117 {
118 return false;
119 }
120 if (y < 0 || y >= height)
121 {
122 return false;
123 }
124 return data[y * width + x][0];
125 }
126
127 public bool SectionFramed(int x, int y)
128 {
129 if (x < 0 || x >= width)
130 {
131 return false;
132 }
133 if (y < 0 || y >= height)
134 {
135 return false;
136 }
137 return data[y * width + x][1];
138 }
139
140 public bool MapSectionDrawn(int x, int y)
141 {
142 if (x < 0 || x >= width)
143 {
144 return false;
145 }
146 if (y < 0 || y >= height)
147 {
148 return false;
149 }
150 return data[y * width + x][2];
151 }
152
153 public void ClearMapDraw()
154 {
155 for (int i = 0; i < data.Length; i++)
156 {
157 data[i][2] = false;
158 }
159 prevMap.Reset();
160 mapSectionsLeft = data.Length;
161 }
162
163 public void SetSectionFramed(int x, int y)
164 {
165 if (x >= 0 && x < width && y >= 0 && y < height)
166 {
167 BitsByte bitsByte = data[y * width + x];
168 if (bitsByte[0] && !bitsByte[1])
169 {
170 bitsByte[1] = true;
171 data[y * width + x] = bitsByte;
173 }
174 }
175 }
176
177 public void SetSectionLoaded(int x, int y)
178 {
179 if (x >= 0 && x < width && y >= 0 && y < height)
180 {
181 SetSectionLoaded(ref data[y * width + x]);
182 }
183 }
184
185 private void SetSectionLoaded(ref BitsByte section)
186 {
187 if (!section[0])
188 {
189 section[0] = true;
191 }
192 else if (section[1])
193 {
194 section[1] = false;
196 }
197 }
198
200 {
201 for (int i = 0; i < data.Length; i++)
202 {
203 SetSectionLoaded(ref data[i]);
204 }
205 }
206
207 public void SetTilesLoaded(int startX, int startY, int endXInclusive, int endYInclusive)
208 {
209 int sectionX = Netplay.GetSectionX(startX);
210 int sectionY = Netplay.GetSectionY(startY);
211 int sectionX2 = Netplay.GetSectionX(endXInclusive);
212 int sectionY2 = Netplay.GetSectionY(endYInclusive);
213 for (int i = sectionX; i <= sectionX2; i++)
214 {
215 for (int j = sectionY; j <= sectionY2; j++)
216 {
217 SetSectionLoaded(i, j);
218 }
219 }
220 }
221
222 public bool GetNextMapDraw(Vector2 playerPos, out int x, out int y)
223 {
224 if (mapSectionsLeft <= 0)
225 {
226 x = -1;
227 y = -1;
228 return false;
229 }
230 Stopwatch stopwatch = new Stopwatch();
231 stopwatch.Start();
232 int num = 0;
233 int num2 = 0;
234 Vector2 vector = prevMap.centerPos;
235 playerPos *= 0.0625f;
236 int sectionX = Netplay.GetSectionX((int)playerPos.X);
237 int sectionY = Netplay.GetSectionY((int)playerPos.Y);
238 int num3 = Netplay.GetSectionX((int)vector.X);
239 int num4 = Netplay.GetSectionY((int)vector.Y);
240 int num5;
241 if (num3 != sectionX || num4 != sectionY)
242 {
243 vector = playerPos;
244 num3 = sectionX;
245 num4 = sectionY;
246 num5 = 4;
247 x = sectionX;
248 y = sectionY;
249 }
250 else
251 {
252 num5 = prevMap.leg;
253 x = prevMap.X;
254 y = prevMap.Y;
255 num = prevMap.xDir;
256 num2 = prevMap.yDir;
257 }
258 int num6 = (int)(playerPos.X - ((float)num3 + 0.5f) * 200f);
259 int num7 = (int)(playerPos.Y - ((float)num4 + 0.5f) * 150f);
260 if (num == 0)
261 {
262 num = ((num6 <= 0) ? 1 : (-1));
263 num2 = ((num7 <= 0) ? 1 : (-1));
264 }
265 int num8 = 0;
266 bool flag = false;
267 bool flag2 = false;
268 while (true)
269 {
270 if (num8 == 4)
271 {
272 if (flag2)
273 {
274 throw new Exception("Infinite loop in WorldSections.GetNextMapDraw");
275 }
276 flag2 = true;
277 x = num3;
278 y = num4;
279 num6 = (int)(vector.X - ((float)num3 + 0.5f) * 200f);
280 num7 = (int)(vector.Y - ((float)num4 + 0.5f) * 150f);
281 num = ((num6 <= 0) ? 1 : (-1));
282 num2 = ((num7 <= 0) ? 1 : (-1));
283 num5 = 4;
284 num8 = 0;
285 }
286 if (y >= 0 && y < height && x >= 0 && x < width)
287 {
288 flag = false;
289 if (!data[y * width + x][2])
290 {
291 break;
292 }
293 }
294 int num9 = x - num3;
295 int num10 = y - num4;
296 if (num9 == 0 || num10 == 0)
297 {
298 if (num5 == 4)
299 {
300 if (num9 == 0 && num10 == 0)
301 {
302 if (Math.Abs(num6) > Math.Abs(num7))
303 {
304 y -= num2;
305 }
306 else
307 {
308 x -= num;
309 }
310 }
311 else
312 {
313 if (num9 != 0)
314 {
315 x += num9 / Math.Abs(num9);
316 }
317 if (num10 != 0)
318 {
319 y += num10 / Math.Abs(num10);
320 }
321 }
322 num5 = 0;
323 num8 = -2;
324 flag = true;
325 }
326 else
327 {
328 if (num9 != 0)
329 {
330 num = ((num9 <= 0) ? 1 : (-1));
331 }
332 else
333 {
334 num2 = ((num10 <= 0) ? 1 : (-1));
335 }
336 x += num;
337 y += num2;
338 num5++;
339 }
340 if (flag)
341 {
342 num8++;
343 }
344 else
345 {
346 flag = true;
347 }
348 }
349 else
350 {
351 x += num;
352 y += num2;
353 }
354 }
355 data[y * width + x][2] = true;
357 prevMap.centerPos = playerPos;
358 prevMap.X = x;
359 prevMap.Y = y;
360 prevMap.leg = num5;
361 prevMap.xDir = num;
362 prevMap.yDir = num2;
363 stopwatch.Stop();
364 return true;
365 }
366}
static double Abs(double value)
static int GetSectionX(int x)
Definition Netplay.cs:774
static int GetSectionY(int y)
Definition Netplay.cs:779
void SetSectionFramed(int x, int y)
bool GetNextMapDraw(Vector2 playerPos, out int x, out int y)
void SetTilesLoaded(int startX, int startY, int endXInclusive, int endYInclusive)
const int BitIndex_SectionMapDrawn
WorldSections(int numSectionsX, int numSectionsY)
const int BitIndex_SectionFramed
bool SectionNeedsRefresh(int x, int y)
bool MapSectionDrawn(int x, int y)
void SetSectionLoaded(ref BitsByte section)
void SetAllFramedSectionsAsNeedingRefresh()
void SetSectionAsRefreshed(int x, int y)
IterationState prevMap
const int BitIndex_SectionLoaded
const int BitIndex_SectionNeedsRefresh
bool SectionLoaded(int x, int y)
bool SectionFramed(int x, int y)
void SetSectionLoaded(int x, int y)
IterationState prevFrame