Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
TELogicSensor.cs
Go to the documentation of this file.
1using System;
3using System.IO;
6
8
10{
11 public enum LogicCheckType
12 {
13 None,
14 Day,
15 Night,
17 Water,
18 Lava,
19 Honey,
20 Liquid
21 }
22
23 private static byte _myEntityID;
24
26
28
29 private static List<int> markedIDsForRemoval = new List<int>();
30
31 private static bool inUpdateLoop;
32
33 private static bool playerBoxFilled;
34
36
37 public bool On;
38
39 public int CountedData;
40
41 public override void RegisterTileEntityID(int assignedID)
42 {
43 _myEntityID = (byte)assignedID;
44 TileEntity._UpdateStart += UpdateStartInternal;
45 TileEntity._UpdateEnd += UpdateEndInternal;
46 }
47
48 public override void NetPlaceEntityAttempt(int x, int y)
49 {
50 NetPlaceEntity(x, y);
51 }
52
53 public static void NetPlaceEntity(int x, int y)
54 {
55 int num = Place(x, y);
56 ((TELogicSensor)TileEntity.ByID[num]).FigureCheckState();
57 NetMessage.SendData(86, -1, -1, null, num, x, y);
58 }
59
60 public override bool IsTileValidForEntity(int x, int y)
61 {
62 return ValidTile(x, y);
63 }
64
65 public override TileEntity GenerateInstance()
66 {
67 return new TELogicSensor();
68 }
69
70 private static void UpdateStartInternal()
71 {
72 inUpdateLoop = true;
73 markedIDsForRemoval.Clear();
74 playerBox.Clear();
75 playerBoxFilled = false;
77 }
78
79 private static void FillPlayerHitboxes()
80 {
82 {
83 return;
84 }
85 for (int i = 0; i < 255; i++)
86 {
87 Player player = Main.player[i];
88 if (player.active && !player.dead && !player.ghost)
89 {
90 playerBox[i] = player.getRect();
91 }
92 }
93 playerBoxFilled = true;
94 }
95
96 private static void UpdateEndInternal()
97 {
98 inUpdateLoop = false;
100 {
101 Wiring.blockPlayerTeleportationForOneIteration = tripPoint.Item2;
102 Wiring.HitSwitch(tripPoint.Item1.X, tripPoint.Item1.Y);
103 if (Main.netMode == 2)
104 {
105 NetMessage.SendData(59, -1, -1, null, tripPoint.Item1.X, tripPoint.Item1.Y);
106 }
107 }
108 Wiring.blockPlayerTeleportationForOneIteration = false;
109 tripPoints.Clear();
110 foreach (int item in markedIDsForRemoval)
111 {
112 if (TileEntity.ByID.TryGetValue(item, out var value) && value.type == _myEntityID)
113 {
115 {
116 TileEntity.ByID.Remove(item);
117 TileEntity.ByPosition.Remove(value.Position);
118 }
119 }
120 }
121 markedIDsForRemoval.Clear();
122 }
123
124 public override void Update()
125 {
126 bool state = GetState(Position.X, Position.Y, logicCheck, this);
127 switch (logicCheck)
128 {
129 case LogicCheckType.Day:
130 case LogicCheckType.Night:
131 if (!On && state)
132 {
133 ChangeState(onState: true, TripWire: true);
134 }
135 if (On && !state)
136 {
137 ChangeState(onState: false, TripWire: false);
138 }
139 break;
140 case LogicCheckType.PlayerAbove:
141 case LogicCheckType.Water:
142 case LogicCheckType.Lava:
143 case LogicCheckType.Honey:
144 case LogicCheckType.Liquid:
145 if (On != state)
146 {
147 ChangeState(state, TripWire: true);
148 }
149 break;
150 }
151 }
152
153 public void ChangeState(bool onState, bool TripWire)
154 {
155 if (onState == On || SanityCheck(Position.X, Position.Y))
156 {
157 Main.tile[Position.X, Position.Y].frameX = (short)(onState ? 18 : 0);
158 On = onState;
159 if (Main.netMode == 2)
160 {
162 }
163 if (TripWire && Main.netMode != 1)
164 {
165 tripPoints.Add(Tuple.Create(Position, logicCheck == LogicCheckType.PlayerAbove));
166 }
167 }
168 }
169
170 public static bool ValidTile(int x, int y)
171 {
172 if (!Main.tile[x, y].active() || Main.tile[x, y].type != 423 || Main.tile[x, y].frameY % 18 != 0 || Main.tile[x, y].frameX % 18 != 0)
173 {
174 return false;
175 }
176 return true;
177 }
178
180 {
182 On = false;
183 }
184
185 public static LogicCheckType FigureCheckType(int x, int y, out bool on)
186 {
187 on = false;
188 if (!WorldGen.InWorld(x, y))
189 {
190 return LogicCheckType.None;
191 }
192 Tile tile = Main.tile[x, y];
193 if (tile == null)
194 {
195 return LogicCheckType.None;
196 }
197 LogicCheckType result = LogicCheckType.None;
198 switch (tile.frameY / 18)
199 {
200 case 0:
201 result = LogicCheckType.Day;
202 break;
203 case 1:
204 result = LogicCheckType.Night;
205 break;
206 case 2:
207 result = LogicCheckType.PlayerAbove;
208 break;
209 case 3:
210 result = LogicCheckType.Water;
211 break;
212 case 4:
213 result = LogicCheckType.Lava;
214 break;
215 case 5:
216 result = LogicCheckType.Honey;
217 break;
218 case 6:
219 result = LogicCheckType.Liquid;
220 break;
221 }
222 on = GetState(x, y, result);
223 return result;
224 }
225
226 public static bool GetState(int x, int y, LogicCheckType type, TELogicSensor instance = null)
227 {
228 switch (type)
229 {
230 case LogicCheckType.Day:
231 return Main.dayTime;
232 case LogicCheckType.Night:
233 return !Main.dayTime;
234 case LogicCheckType.PlayerAbove:
235 {
236 bool result = false;
237 Rectangle value = new Rectangle(x * 16 - 32 - 1, y * 16 - 160 - 1, 82, 162);
239 {
240 if (item.Value.Intersects(value))
241 {
242 result = true;
243 break;
244 }
245 }
246 return result;
247 }
248 case LogicCheckType.Water:
249 case LogicCheckType.Lava:
250 case LogicCheckType.Honey:
251 case LogicCheckType.Liquid:
252 {
253 if (instance == null)
254 {
255 return false;
256 }
257 Tile tile = Main.tile[x, y];
258 bool flag = true;
259 if (tile == null || tile.liquid == 0)
260 {
261 flag = false;
262 }
263 if (!tile.lava() && type == LogicCheckType.Lava)
264 {
265 flag = false;
266 }
267 if (!tile.honey() && type == LogicCheckType.Honey)
268 {
269 flag = false;
270 }
271 if ((tile.honey() || tile.lava()) && type == LogicCheckType.Water)
272 {
273 flag = false;
274 }
275 if (!flag && instance.On)
276 {
277 if (instance.CountedData == 0)
278 {
279 instance.CountedData = 15;
280 }
281 else if (instance.CountedData > 0)
282 {
283 instance.CountedData--;
284 }
285 flag = instance.CountedData > 0;
286 }
287 return flag;
288 }
289 default:
290 return false;
291 }
292 }
293
299
300 public static void GetFrame(int x, int y, LogicCheckType type, bool on)
301 {
302 Main.tile[x, y].frameX = (short)(on ? 18 : 0);
303 switch (type)
304 {
305 case LogicCheckType.Day:
306 Main.tile[x, y].frameY = 0;
307 break;
308 case LogicCheckType.Night:
309 Main.tile[x, y].frameY = 18;
310 break;
311 case LogicCheckType.PlayerAbove:
312 Main.tile[x, y].frameY = 36;
313 break;
314 case LogicCheckType.Water:
315 Main.tile[x, y].frameY = 54;
316 break;
317 case LogicCheckType.Lava:
318 Main.tile[x, y].frameY = 72;
319 break;
320 case LogicCheckType.Honey:
321 Main.tile[x, y].frameY = 90;
322 break;
323 case LogicCheckType.Liquid:
324 Main.tile[x, y].frameY = 108;
325 break;
326 default:
327 Main.tile[x, y].frameY = 0;
328 break;
329 }
330 }
331
332 public static bool SanityCheck(int x, int y)
333 {
334 if (!Main.tile[x, y].active() || Main.tile[x, y].type != 423)
335 {
336 Kill(x, y);
337 return false;
338 }
339 return true;
340 }
341
342 public static int Place(int x, int y)
343 {
345 tELogicSensor.Position = new Point16(x, y);
346 tELogicSensor.ID = TileEntity.AssignNewID();
347 tELogicSensor.type = _myEntityID;
349 {
352 }
353 return tELogicSensor.ID;
354 }
355
356 public static int Hook_AfterPlacement(int x, int y, int type = 423, int style = 0, int direction = 1, int alternate = 0)
357 {
358 bool on;
360 GetFrame(x, y, logicCheckType, on);
361 if (Main.netMode == 1)
362 {
364 NetMessage.SendData(87, -1, -1, null, x, y, (int)_myEntityID);
365 return -1;
366 }
367 int num = Place(x, y);
368 ((TELogicSensor)TileEntity.ByID[num]).FigureCheckState();
369 return num;
370 }
371
372 public static void Kill(int x, int y)
373 {
374 if (!TileEntity.ByPosition.TryGetValue(new Point16(x, y), out var value) || value.type != _myEntityID)
375 {
376 return;
377 }
378 Wiring.blockPlayerTeleportationForOneIteration = ((TELogicSensor)value).logicCheck == LogicCheckType.PlayerAbove;
379 bool flag = false;
381 {
382 flag = true;
383 }
385 {
386 flag = true;
387 }
389 {
390 flag = true;
391 }
393 {
394 flag = true;
395 }
397 {
398 flag = true;
399 }
400 if (flag)
401 {
402 Wiring.HitSwitch(value.Position.X, value.Position.Y);
403 NetMessage.SendData(59, -1, -1, null, value.Position.X, value.Position.Y);
404 }
405 Wiring.blockPlayerTeleportationForOneIteration = false;
406 if (inUpdateLoop)
407 {
409 return;
410 }
412 {
413 TileEntity.ByPosition.Remove(new Point16(x, y));
414 TileEntity.ByID.Remove(value.ID);
415 }
416 }
417
418 public static int Find(int x, int y)
419 {
420 if (TileEntity.ByPosition.TryGetValue(new Point16(x, y), out var value) && value.type == _myEntityID)
421 {
422 return value.ID;
423 }
424 return -1;
425 }
426
427 public override void WriteExtraData(BinaryWriter writer, bool networkSend)
428 {
429 if (!networkSend)
430 {
431 writer.Write((byte)logicCheck);
432 writer.Write(On);
433 }
434 }
435
436 public override void ReadExtraData(BinaryReader reader, bool networkSend)
437 {
438 if (!networkSend)
439 {
441 On = reader.ReadBoolean();
442 }
443 }
444
445 public override string ToString()
446 {
447 return Position.X + "x " + Position.Y + "y " + logicCheck;
448 }
449}
virtual bool ReadBoolean()
virtual byte ReadByte()
static Dictionary< int, TileEntity > ByID
Definition TileEntity.cs:18
static Dictionary< Point16, TileEntity > ByPosition
Definition TileEntity.cs:20
override void WriteExtraData(BinaryWriter writer, bool networkSend)
override void ReadExtraData(BinaryReader reader, bool networkSend)
override bool IsTileValidForEntity(int x, int y)
static List< Tuple< Point16, bool > > tripPoints
static int Hook_AfterPlacement(int x, int y, int type=423, int style=0, int direction=1, int alternate=0)
override void NetPlaceEntityAttempt(int x, int y)
void ChangeState(bool onState, bool TripWire)
static LogicCheckType FigureCheckType(int x, int y, out bool on)
static void GetFrame(int x, int y, LogicCheckType type, bool on)
static bool GetState(int x, int y, LogicCheckType type, TELogicSensor instance=null)
static Dictionary< int, Rectangle > playerBox
override void RegisterTileEntityID(int assignedID)
static int myPlayer
Definition Main.cs:1801
static int netMode
Definition Main.cs:2095
static bool dayTime
Definition Main.cs:1282
static Tile[,] tile
Definition Main.cs:1675
static Player[] player
Definition Main.cs:1803
static void SendData(int msgType, int remoteClient=-1, int ignoreClient=-1, NetworkText text=null, int number=0, float number2=0f, float number3=0f, float number4=0f, int number5=0, int number6=0, int number7=0)
Definition NetMessage.cs:88
static void SendTileSquare(int whoAmi, int tileX, int tileY, int xSize, int ySize, TileChangeType changeType=TileChangeType.None)
Rectangle getRect()
Definition Player.cs:40162
byte liquid
Definition Tile.cs:12
short frameY
Definition Tile.cs:24
bool honey()
Definition Tile.cs:379
bool lava()
Definition Tile.cs:362
static void HitSwitch(int i, int j)
Definition Wiring.cs:237
static bool InWorld(int x, int y, int fluff=0)
Definition WorldGen.cs:5816