-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathWiringWrapper.cs
2819 lines (2508 loc) · 113 KB
/
WiringWrapper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using Terraria.Audio;
using Terraria.DataStructures;
using Terraria.GameContent.Events;
using Terraria.GameContent.UI;
using Terraria.ID;
using Terraria.Localization;
using WireHead;
/*
* This is a wrapper for decompiled wiring code with some small changes to make it compatible with tModLoader
* I really, really would like to do it a different way but I simply can't get the performance I want without
* Overriding the default wiring implementation inside the mod.
*
* I tried for a long time with reflection to access internals (e.g. _wireSkip), but given this happens at
* time sensitive points I can't take the performance hit doing that.
*
* ReLogic if you ever read this although I doubt it: If you really don't like me posting this snippet of
* decompiled code then I guess I can remove it, although it's required for this mod to work. Plus what's
* shown in the tModLoader repo in the diffs is more than this anyways
*
*/
namespace Terraria
{
public static class WiringWrapper
{
// Use Wiring.blockPlayerTeleportationForOneIteration since other files check it
/* public static bool blockPlayerTeleportationForOneIteration; */
public static bool running;
public static Dictionary<Point16, bool> _wireSkip;
public static DoubleStack<Point16> _wireList;
public static DoubleStack<byte> _wireDirectionList;
public static Dictionary<Point16, byte> _toProcess;
public static Queue<Point16> _GatesCurrent;
public static Queue<Point16> _LampsToCheck;
public static Queue<Point16> _GatesNext;
public static Dictionary<Point16, bool> _GatesDone;
public static Dictionary<Point16, byte> _PixelBoxTriggers;
public static Vector2[] _teleport;
public const int MaxPump = 20;
public static int[] _inPumpX;
public static int[] _inPumpY;
public static int _numInPump;
public static int[] _outPumpX;
public static int[] _outPumpY;
public static int _numOutPump;
public const int MaxMech = 1000;
public static int[] _mechX;
public static int[] _mechY;
public static int _numMechs;
public static int[] _mechTime;
public static int _currentWireColor;
public static int CurrentUser = 255;
public static void SetCurrentUser(int plr = -1)
{
if (plr < 0 || plr > 255)
plr = 255;
if (Main.netMode == 0)
plr = Main.myPlayer;
CurrentUser = plr;
}
public static void Initialize()
{
_wireSkip = new Dictionary<Point16, bool>();
_wireList = new DoubleStack<Point16>();
_wireDirectionList = new DoubleStack<byte>();
_toProcess = new Dictionary<Point16, byte>();
_GatesCurrent = new Queue<Point16>();
_GatesNext = new Queue<Point16>();
_GatesDone = new Dictionary<Point16, bool>();
_LampsToCheck = new Queue<Point16>();
_PixelBoxTriggers = new Dictionary<Point16, byte>();
_inPumpX = new int[20];
_inPumpY = new int[20];
_outPumpX = new int[20];
_outPumpY = new int[20];
_teleport = new Vector2[2];
_mechX = new int[1000];
_mechY = new int[1000];
_mechTime = new int[1000];
}
public static void SkipWire(int x, int y)
{
//_wireSkip[new Point16(x, y)] = true;
}
public static void SkipWire(Point16 point)
{
//_wireSkip[point] = true;
}
public static void UpdateMech()
{
SetCurrentUser();
for (int num = _numMechs - 1; num >= 0; num--)
{
_mechTime[num]--;
if (!Main.tile[_mechX[num], _mechY[num]].IsActuated && Main.tile[_mechX[num], _mechY[num]].TileType == 144)
{
if (Main.tile[_mechX[num], _mechY[num]].TileFrameY == 0)
{
_mechTime[num] = 0;
}
else
{
int num2 = Main.tile[_mechX[num], _mechY[num]].TileFrameX / 18;
switch (num2)
{
case 0:
num2 = 60;
break;
case 1:
num2 = 180;
break;
case 2:
num2 = 300;
break;
case 3:
num2 = 30;
break;
case 4:
num2 = 15;
break;
}
if (Math.IEEERemainder(_mechTime[num], num2) == 0.0)
{
_mechTime[num] = 18000;
TripWire(_mechX[num], _mechY[num], 1, 1);
}
}
}
if (_mechTime[num] <= 0)
{
if (!Main.tile[_mechX[num], _mechY[num]].IsActuated && Main.tile[_mechX[num], _mechY[num]].TileType == 144)
{
Main.tile[_mechX[num], _mechY[num]].TileFrameY = 0;
NetMessage.SendTileSquare(-1, _mechX[num], _mechY[num]);
}
if (!Main.tile[_mechX[num], _mechY[num]].IsActuated && Main.tile[_mechX[num], _mechY[num]].TileType == 411)
{
Tile tile = Main.tile[_mechX[num], _mechY[num]];
int num3 = tile.TileFrameX % 36 / 18;
int num4 = tile.TileFrameY % 36 / 18;
int num5 = _mechX[num] - num3;
int num6 = _mechY[num] - num4;
int num7 = 36;
if (Main.tile[num5, num6].TileFrameX >= 36)
num7 = -36;
for (int i = num5; i < num5 + 2; i++)
{
for (int j = num6; j < num6 + 2; j++)
{
Main.tile[i, j].TileFrameX = (short)(Main.tile[i, j].TileFrameX + num7);
}
}
NetMessage.SendTileSquare(-1, num5, num6, 2, 2);
}
for (int k = num; k < _numMechs; k++)
{
_mechX[k] = _mechX[k + 1];
_mechY[k] = _mechY[k + 1];
_mechTime[k] = _mechTime[k + 1];
}
_numMechs--;
}
}
}
public static void HitSwitch(int i, int j)
{
if (!WorldGen.InWorld(i, j) || Main.tile[i, j] == null)
return;
if (Main.tile[i, j].TileType == 135 || Main.tile[i, j].TileType == 314 || Main.tile[i, j].TileType == 423 || Main.tile[i, j].TileType == 428 || Main.tile[i, j].TileType == 442 || Main.tile[i, j].TileType == 476)
{
//SoundEngine.PlaySound(28, i * 16, j * 16, 0);
TripWire(i, j, 1, 1);
}
else if (Main.tile[i, j].TileType == 440)
{
//SoundEngine.PlaySound(28, i * 16 + 16, j * 16 + 16, 0);
TripWire(i, j, 3, 3);
}
else if (Main.tile[i, j].TileType == 136)
{
if (Main.tile[i, j].TileFrameY == 0)
Main.tile[i, j].TileFrameY = 18;
else
Main.tile[i, j].TileFrameY = 0;
//SoundEngine.PlaySound(28, i * 16, j * 16, 0);
TripWire(i, j, 1, 1);
}
else if (Main.tile[i, j].TileType == 443)
{
GeyserTrap(i, j);
}
else if (Main.tile[i, j].TileType == 144)
{
if (Main.tile[i, j].TileFrameY == 0)
{
Main.tile[i, j].TileFrameY = 18;
if (Main.netMode != 1)
CheckMech(i, j, 18000);
}
else
{
Main.tile[i, j].TileFrameY = 0;
}
//SoundEngine.PlaySound(28, i * 16, j * 16, 0);
}
else if (Main.tile[i, j].TileType == 441 || Main.tile[i, j].TileType == 468)
{
int num = Main.tile[i, j].TileFrameX / 18 * -1;
int num2 = Main.tile[i, j].TileFrameY / 18 * -1;
num %= 4;
if (num < -1)
num += 2;
num += i;
num2 += j;
//SoundEngine.PlaySound(28, i * 16, j * 16, 0);
TripWire(num, num2, 2, 2);
}
else if (Main.tile[i, j].TileType == 467)
{
if (Main.tile[i, j].TileFrameX / 36 == 4)
{
int num3 = Main.tile[i, j].TileFrameX / 18 * -1;
int num4 = Main.tile[i, j].TileFrameY / 18 * -1;
num3 %= 4;
if (num3 < -1)
num3 += 2;
num3 += i;
num4 += j;
//SoundEngine.PlaySound(28, i * 16, j * 16, 0);
TripWire(num3, num4, 2, 2);
}
}
else
{
if (Main.tile[i, j].TileType != 132 && Main.tile[i, j].TileType != 411)
return;
short num5 = 36;
int num6 = Main.tile[i, j].TileFrameX / 18 * -1;
int num7 = Main.tile[i, j].TileFrameY / 18 * -1;
num6 %= 4;
if (num6 < -1)
{
num6 += 2;
num5 = -36;
}
num6 += i;
num7 += j;
if (Main.netMode != 1 && Main.tile[num6, num7].TileType == 411)
CheckMech(num6, num7, 60);
for (int k = num6; k < num6 + 2; k++)
{
for (int l = num7; l < num7 + 2; l++)
{
if (Main.tile[k, l].TileType == 132 || Main.tile[k, l].TileType == 411)
Main.tile[k, l].TileFrameX += num5;
}
}
WorldGen.TileFrame(num6, num7);
//SoundEngine.PlaySound(28, i * 16, j * 16, 0);
TripWire(num6, num7, 2, 2);
}
}
public static void PokeLogicGate(int lampX, int lampY)
{
if (Main.netMode != 1)
{
_LampsToCheck.Enqueue(new Point16(lampX, lampY));
LogicGatePass();
}
}
public static bool Actuate(int i, int j)
{
Tile tile = Main.tile[i, j];
if (!tile.HasActuator)
return false;
if (tile.IsActuated)
ReActive(i, j);
else
DeActive(i, j);
return true;
}
public static void ActuateForced(int i, int j)
{
if (Main.tile[i, j].IsActuated)
ReActive(i, j);
else
DeActive(i, j);
}
public static void MassWireOperation(Point ps, Point pe, Player master)
{
int wireCount = 0;
int actuatorCount = 0;
for (int i = 0; i < 58; i++)
{
if (master.inventory[i].type == 530)
wireCount += master.inventory[i].stack;
if (master.inventory[i].type == 849)
actuatorCount += master.inventory[i].stack;
}
int num = wireCount;
int num2 = actuatorCount;
MassWireOperationInner(master, ps, pe, master.Center, master.direction == 1, ref wireCount, ref actuatorCount);
int num3 = num - wireCount;
int num4 = num2 - actuatorCount;
if (Main.netMode == 2)
{
NetMessage.SendData(110, master.whoAmI, -1, null, 530, num3, master.whoAmI);
NetMessage.SendData(110, master.whoAmI, -1, null, 849, num4, master.whoAmI);
return;
}
for (int j = 0; j < num3; j++)
{
master.ConsumeItem(530);
}
for (int k = 0; k < num4; k++)
{
master.ConsumeItem(849);
}
}
private static bool CheckMech(int i, int j, int time)
{
for (int k = 0; k < _numMechs; k++)
{
if (_mechX[k] == i && _mechY[k] == j)
return false;
}
if (_numMechs < 999)
{
_mechX[_numMechs] = i;
_mechY[_numMechs] = j;
_mechTime[_numMechs] = time;
_numMechs++;
return true;
}
return false;
}
private static void XferWater()
{
for (int i = 0; i < _numInPump; i++)
{
int num = _inPumpX[i];
int num2 = _inPumpY[i];
int liquid = Main.tile[num, num2].LiquidType;
if (liquid <= 0)
continue;
bool flag = Main.tile[num, num2].LiquidType == LiquidID.Lava;
bool flag2 = Main.tile[num, num2].LiquidType == LiquidID.Honey;
for (int j = 0; j < _numOutPump; j++)
{
int num3 = _outPumpX[j];
int num4 = _outPumpY[j];
int liquid2 = Main.tile[num3, num4].LiquidType;
if (liquid2 >= 255)
continue;
bool flag3 = Main.tile[num3, num4].LiquidType == LiquidID.Lava;
bool flag4 = Main.tile[num3, num4].LiquidType == LiquidID.Honey;
if (liquid2 == 0)
{
flag3 = flag;
flag4 = flag2;
}
if (flag == flag3 && flag2 == flag4)
{
int num5 = liquid;
if (num5 + liquid2 > 255)
num5 = 255 - liquid2;
// Pumps are weird with new api, don't feel like figuring them out
//Main.tile[num3, num4].LiquidType += (byte)num5;
//Main.tile[num, num2].LiquidType -= (byte)num5;
liquid = Main.tile[num, num2].LiquidType;
//Main.tile[num3, num4].lava(flag);
//Main.tile[num3, num4].honey(flag2);
WorldGen.SquareTileFrame(num3, num4);
if (Main.tile[num, num2].LiquidType == 0)
{
//Main.tile[num, num2].lava(lava: false);
WorldGen.SquareTileFrame(num, num2);
break;
}
}
}
WorldGen.SquareTileFrame(num, num2);
}
}
public static void TripWire(int left, int top, int width, int height)
{
if (Main.netMode == 1)
return;
// Modified to accomodate terracc
if(WireHead.WireHead.useTerracc){
for(int c = 0; c < Accelerator.colors; ++c){
Accelerator.toHit[Accelerator.numToHit,c] = -1;
for(int x = left; x < left+width; ++x){
for(int y = top; y < top+height; ++y){
int g = Accelerator.wireGroup[x, y, c];
if(g == -1) continue;
/* if(Accelerator.triggerable[g].Length == Accelerator.groupStandardLamps[g].Count){ */
Accelerator.toHit[Accelerator.numToHit, c] = g;
/* Console.WriteLine($"Trigger c:{c}, group: {g}"); */
/* } */
}
}
}
++Accelerator.numToHit;
}
running = true;
if (_wireList.Count != 0)
_wireList.Clear(quickClear: true);
if (_wireDirectionList.Count != 0)
_wireDirectionList.Clear(quickClear: true);
Vector2[] array = new Vector2[8];
int num = 0;
for (int i = left; i < left + width; i++)
{
for (int j = top; j < top + height; j++)
{
Point16 back = new Point16(i, j);
Tile tile = Main.tile[i, j];
if (tile != null && tile.RedWire)
_wireList.PushBack(back);
}
}
_teleport[0].X = -1f;
_teleport[0].Y = -1f;
_teleport[1].X = -1f;
_teleport[1].Y = -1f;
if (_wireList.Count > 0)
{
_numInPump = 0;
_numOutPump = 0;
HitWire(_wireList, 1);
if (_numInPump > 0 && _numOutPump > 0)
XferWater();
}
array[num++] = _teleport[0];
array[num++] = _teleport[1];
for (int k = left; k < left + width; k++)
{
for (int l = top; l < top + height; l++)
{
Point16 back = new Point16(k, l);
Tile tile2 = Main.tile[k, l];
if (tile2 != null && tile2.BlueWire)
_wireList.PushBack(back);
}
}
_teleport[0].X = -1f;
_teleport[0].Y = -1f;
_teleport[1].X = -1f;
_teleport[1].Y = -1f;
if (_wireList.Count > 0)
{
_numInPump = 0;
_numOutPump = 0;
HitWire(_wireList, 2);
if (_numInPump > 0 && _numOutPump > 0)
XferWater();
}
array[num++] = _teleport[0];
array[num++] = _teleport[1];
_teleport[0].X = -1f;
_teleport[0].Y = -1f;
_teleport[1].X = -1f;
_teleport[1].Y = -1f;
for (int m = left; m < left + width; m++)
{
for (int n = top; n < top + height; n++)
{
Point16 back = new Point16(m, n);
Tile tile3 = Main.tile[m, n];
if (tile3 != null && tile3.GreenWire)
_wireList.PushBack(back);
}
}
if (_wireList.Count > 0)
{
_numInPump = 0;
_numOutPump = 0;
HitWire(_wireList, 3);
if (_numInPump > 0 && _numOutPump > 0)
XferWater();
}
array[num++] = _teleport[0];
array[num++] = _teleport[1];
_teleport[0].X = -1f;
_teleport[0].Y = -1f;
_teleport[1].X = -1f;
_teleport[1].Y = -1f;
for (int num2 = left; num2 < left + width; num2++)
{
for (int num3 = top; num3 < top + height; num3++)
{
Point16 back = new Point16(num2, num3);
Tile tile4 = Main.tile[num2, num3];
if (tile4 != null && tile4.YellowWire)
_wireList.PushBack(back);
}
}
if (_wireList.Count > 0)
{
_numInPump = 0;
_numOutPump = 0;
HitWire(_wireList, 4);
if (_numInPump > 0 && _numOutPump > 0)
XferWater();
}
array[num++] = _teleport[0];
array[num++] = _teleport[1];
running = false;
for (int num4 = 0; num4 < 8; num4 += 2)
{
_teleport[0] = array[num4];
_teleport[1] = array[num4 + 1];
if (_teleport[0].X >= 0f && _teleport[1].X >= 0f)
Teleport();
}
PixelBoxPass();
LogicGatePass();
}
private static void PixelBoxPass()
{
// Pixel boxes are handled by accelerator
// foreach (KeyValuePair<Point16, byte> pixelBoxTrigger in _PixelBoxTriggers)
// {
// if (pixelBoxTrigger.Value == 2)
// continue;
//
// if (pixelBoxTrigger.Value == 1)
// {
// if (Main.tile[pixelBoxTrigger.Key.X, pixelBoxTrigger.Key.Y].TileFrameX != 0)
// {
// Main.tile[pixelBoxTrigger.Key.X, pixelBoxTrigger.Key.Y].TileFrameX = 0;
// NetMessage.SendTileSquare(-1, pixelBoxTrigger.Key.X, pixelBoxTrigger.Key.Y);
// }
// }
// else if (pixelBoxTrigger.Value == 3 && Main.tile[pixelBoxTrigger.Key.X, pixelBoxTrigger.Key.Y].TileFrameX != 18)
// {
// Main.tile[pixelBoxTrigger.Key.X, pixelBoxTrigger.Key.Y].TileFrameX = 18;
// NetMessage.SendTileSquare(-1, pixelBoxTrigger.Key.X, pixelBoxTrigger.Key.Y);
// }
// }
//
// _PixelBoxTriggers.Clear();
}
private static void LogicGatePass()
{
if (_GatesCurrent.Count != 0)
return;
// Reset groups triggered
Accelerator.numGroupsTriggered = 0;
_GatesDone.Clear();
while (_LampsToCheck.Count > 0)
{
while (_LampsToCheck.Count > 0)
{
Point16 point = _LampsToCheck.Dequeue();
CheckLogicGate(point.X, point.Y);
}
while (_GatesNext.Count > 0)
{
Utils.Swap(ref _GatesCurrent, ref _GatesNext);
while (_GatesCurrent.Count > 0)
{
Point16 key = _GatesCurrent.Peek();
if (_GatesDone.TryGetValue(key, out bool value) && value)
{
_GatesCurrent.Dequeue();
continue;
}
_GatesDone.Add(key, value: true);
TripWire(key.X, key.Y, 1, 1);
_GatesCurrent.Dequeue();
}
// Reset groups triggered
Accelerator.numGroupsTriggered = 0;
}
}
_GatesDone.Clear();
if (Wiring.blockPlayerTeleportationForOneIteration)
{
// Other files check this so we need to make sure it stays in sync
Wiring.blockPlayerTeleportationForOneIteration = false;
}
}
private static void CheckLogicGate(int lampX, int lampY)
{
/*
* Custom addition with optimized faulty logic gate handling
*/
if (Accelerator.standardLamps[lampX, lampY])
{
Accelerator.CheckFaultyGate(lampX, lampY);
return;
}
if (!WorldGen.InWorld(lampX, lampY, 1))
return;
int gateY = lampY;
Tile gateTile;
while (true)
{
if (gateY < Main.maxTilesY)
{
gateTile = Main.tile[lampX, gateY];
if (gateTile.IsActuated)
return;
if (gateTile.TileType == 420)
break;
if (gateTile.TileType != 419)
return;
gateY++;
continue;
}
return;
}
_GatesDone.TryGetValue(new Point16(lampX, gateY), out bool value);
int gateType = gateTile.TileFrameY / 18;
bool gateOn = gateTile.TileFrameX == 18;
bool gateFaulty = gateTile.TileFrameX == 36;
if (gateType < 0)
return;
int numLamps = 0;
int numOn = 0;
bool lampFaulty = false;
for (int lampIterBack = gateY - 1; lampIterBack > 0; lampIterBack--)
{
Tile curLamp = Main.tile[lampX, lampIterBack];
if (curLamp.IsActuated || curLamp.TileType != 419)
break;
if (curLamp.TileFrameX == 36)
{
lampFaulty = true;
break;
}
numLamps++;
numOn += (curLamp.TileFrameX == 18).ToInt();
}
bool conditionFulfilled = false;
switch (gateType)
{
default:
return;
case 0:
conditionFulfilled = (numLamps == numOn);
break;
case 2:
conditionFulfilled = (numLamps != numOn);
break;
case 1:
conditionFulfilled = (numOn > 0);
break;
case 3:
conditionFulfilled = (numOn == 0);
break;
case 4:
conditionFulfilled = (numOn == 1);
break;
case 5:
conditionFulfilled = (numOn != 1);
break;
}
bool faultyStateWrong = !lampFaulty && gateFaulty;
bool faultyLampTriggered = false;
if (lampFaulty && Framing.GetTileSafely(lampX, lampY).TileFrameX == 36)
faultyLampTriggered = true;
if (!(conditionFulfilled != gateOn || faultyStateWrong || faultyLampTriggered))
return;
_ = gateTile.TileFrameX % 18 / 18;
gateTile.TileFrameX = (short)(18 * conditionFulfilled.ToInt());
if (lampFaulty)
gateTile.TileFrameX = 36;
SkipWire(lampX, gateY);
WorldGen.SquareTileFrame(lampX, gateY); //3182, 216
NetMessage.SendTileSquare(-1, lampX, gateY);
bool flag7 = !lampFaulty || faultyLampTriggered;
if (faultyLampTriggered)
{
if (numOn == 0 || numLamps == 0)
flag7 = false;
flag7 = (Main.rand.NextFloat() < (float)numOn / (float)numLamps);
}
if (faultyStateWrong)
flag7 = false;
if (flag7)
{
if (!value)
{
_GatesNext.Enqueue(new Point16(lampX, gateY));
return;
}
Vector2 position = new Vector2(lampX, gateY) * 16f - new Vector2(10f);
Utils.PoofOfSmoke(position);
NetMessage.SendData(106, -1, -1, null, (int)position.X, position.Y);
}
}
private static void HitWire(DoubleStack<Point16> next, int wireType)
{
// Changed to wrapper
Accelerator.HitWire(next, wireType);
}
public static IEntitySource GetProjectileSource(int sourceTileX, int sourceTileY) => new EntitySource_Wiring(sourceTileX, sourceTileY);
public static IEntitySource GetNPCSource(int sourceTileX, int sourceTileY) => new EntitySource_Wiring(sourceTileX, sourceTileY);
public static IEntitySource GetItemSource(int sourceTileX, int sourceTileY) => new EntitySource_Wiring(sourceTileX, sourceTileY);
public static void HitWireSingle(int i, int j)
{
Tile tile = Main.tile[i, j];
bool? forcedStateWhereTrueIsOn = null;
bool doSkipWires = true;
int type = tile.TileType;
if (tile.HasActuator)
ActuateForced(i, j);
if (tile.IsActuated)
return;
switch (type)
{
case 144:
HitSwitch(i, j);
WorldGen.SquareTileFrame(i, j);
NetMessage.SendTileSquare(-1, i, j);
break;
case 421:
if (!tile.HasActuator)
{
tile.TileType = 422;
WorldGen.SquareTileFrame(i, j);
NetMessage.SendTileSquare(-1, i, j);
}
break;
case 422:
if (!tile.HasActuator)
{
tile.TileType = 421;
WorldGen.SquareTileFrame(i, j);
NetMessage.SendTileSquare(-1, i, j);
}
break;
}
if (type >= 255 && type <= 268)
{
if (!tile.HasActuator)
{
if (type >= 262)
tile.TileType -= 7;
else
tile.TileType += 7;
WorldGen.SquareTileFrame(i, j);
NetMessage.SendTileSquare(-1, i, j);
}
return;
}
if (type == 419)
{
int num = 18;
if (tile.TileFrameX >= num)
num = -num;
if (tile.TileFrameX == 36)
num = 0;
SkipWire(i, j);
tile.TileFrameX = (short)(tile.TileFrameX + num);
WorldGen.SquareTileFrame(i, j);
NetMessage.SendTileSquare(-1, i, j);
_LampsToCheck.Enqueue(new Point16(i, j));
return;
}
if (type == 406)
{
int num2 = tile.TileFrameX % 54 / 18;
int num3 = tile.TileFrameY % 54 / 18;
int num4 = i - num2;
int num5 = j - num3;
int num6 = 54;
if (Main.tile[num4, num5].TileFrameY >= 108)
num6 = -108;
for (int k = num4; k < num4 + 3; k++)
{
for (int l = num5; l < num5 + 3; l++)
{
SkipWire(k, l);
Main.tile[k, l].TileFrameY = (short)(Main.tile[k, l].TileFrameY + num6);
}
}
NetMessage.SendTileSquare(-1, num4 + 1, num5 + 1, 3);
return;
}
if (type == 452)
{
int num7 = tile.TileFrameX % 54 / 18;
int num8 = tile.TileFrameY % 54 / 18;
int num9 = i - num7;
int num10 = j - num8;
int num11 = 54;
if (Main.tile[num9, num10].TileFrameX >= 54)
num11 = -54;
for (int m = num9; m < num9 + 3; m++)
{
for (int n = num10; n < num10 + 3; n++)
{
SkipWire(m, n);
Main.tile[m, n].TileFrameX = (short)(Main.tile[m, n].TileFrameX + num11);
}
}
NetMessage.SendTileSquare(-1, num9 + 1, num10 + 1, 3);
return;
}
if (type == 411)
{
int num12 = tile.TileFrameX % 36 / 18;
int num13 = tile.TileFrameY % 36 / 18;
int num14 = i - num12;
int num15 = j - num13;
int num16 = 36;
if (Main.tile[num14, num15].TileFrameX >= 36)
num16 = -36;
for (int num17 = num14; num17 < num14 + 2; num17++)
{
for (int num18 = num15; num18 < num15 + 2; num18++)
{
SkipWire(num17, num18);
Main.tile[num17, num18].TileFrameX = (short)(Main.tile[num17, num18].TileFrameX + num16);
}
}
NetMessage.SendTileSquare(-1, num14, num15, 2, 2);
return;
}
if (type == 425)
{
int num19 = tile.TileFrameX % 36 / 18;
int num20 = tile.TileFrameY % 36 / 18;
int num21 = i - num19;
int num22 = j - num20;
for (int num23 = num21; num23 < num21 + 2; num23++)
{
for (int num24 = num22; num24 < num22 + 2; num24++)
{
SkipWire(num23, num24);
}
}
if (Main.AnnouncementBoxDisabled)
return;
Color pink = Color.Pink;
int num25 = Sign.ReadSign(num21, num22, CreateIfMissing: false);
if (num25 == -1 || Main.sign[num25] == null || string.IsNullOrWhiteSpace(Main.sign[num25].text))
return;
if (Main.AnnouncementBoxRange == -1)
{
if (Main.netMode == 0)
Main.NewTextMultiline(Main.sign[num25].text, force: false, pink, 460);
else if (Main.netMode == 2)
NetMessage.SendData(107, -1, -1, NetworkText.FromLiteral(Main.sign[num25].text), 255, (int)pink.R, (int)pink.G, (int)pink.B, 460);
}
else if (Main.netMode == 0)
{
if (Main.player[Main.myPlayer].Distance(new Vector2(num21 * 16 + 16, num22 * 16 + 16)) <= (float)Main.AnnouncementBoxRange)
Main.NewTextMultiline(Main.sign[num25].text, force: false, pink, 460);
}
else
{
if (Main.netMode != 2)
return;
for (int num26 = 0; num26 < 255; num26++)
{
if (Main.player[num26].active && Main.player[num26].Distance(new Vector2(num21 * 16 + 16, num22 * 16 + 16)) <= (float)Main.AnnouncementBoxRange)
NetMessage.SendData(107, num26, -1, NetworkText.FromLiteral(Main.sign[num25].text), 255, (int)pink.R, (int)pink.G, (int)pink.B, 460);
}
}
return;
}
if (type == 405)
{
ToggleFirePlace(i, j, tile, forcedStateWhereTrueIsOn, doSkipWires);
return;
}
if (type == 209)
{
int num27 = tile.TileFrameX % 72 / 18;
int num28 = tile.TileFrameY % 54 / 18;
int num29 = i - num27;
int num30 = j - num28;
int num31 = tile.TileFrameY / 54;
int num32 = tile.TileFrameX / 72;
int num33 = -1;
if (num27 == 1 || num27 == 2)