This repository was archived by the owner on Dec 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPower.cs
More file actions
198 lines (176 loc) · 5.5 KB
/
Copy pathPower.cs
File metadata and controls
198 lines (176 loc) · 5.5 KB
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
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using System.Linq;
using Techarria.Content.Tiles;
using Techarria.Content.Tiles.Machines;
using Terraria;
using Terraria.DataStructures;
using Terraria.ModLoader;
namespace Techarria
{
public class PowerDisplayInfo
{
public bool Right;
public bool Left;
public bool Up;
public bool Down;
public int channel;
public int age = 0;
public PowerDisplayInfo(int c, bool r = false, bool l = false, bool u = false, bool d = false)
{
channel = c;
Right = r;
Left = l;
Up = u;
Down = d;
}
public void AddDirection(Direction dir)
{
if (dir == Direction.Right)
Right = true;
if (dir == Direction.Left)
Left = true;
if (dir == Direction.Up)
Up = true;
if (dir == Direction.Down)
Down = true;
}
}
public class Wire
{
public int X;
public int Y;
public int C;
public Wire(int i, int j, int channel)
{
X = i;
Y = j;
C = channel;
}
public Wire(Point p, int channel)
{
X = p.X;
Y = p.Y;
C = channel;
}
public override bool Equals(object obj)
{
if (obj is Wire wire)
{
return (wire.X == X) && (wire.Y == Y) && (wire.C == C);
}
return false;
}
public override int GetHashCode()
{
return C.GetHashCode() << 30 ^ X.GetHashCode() << 15 ^ Y.GetHashCode();
}
}
public class Power
{
public static List<Wire> scanned = new();
public static Dictionary<Wire, PowerDisplayInfo> DisplayInfos = new() { };
public static byte GetWire(Tile tile)
{
byte val = 0;
if (tile.RedWire) val += 1;
if (tile.BlueWire) val += 2;
if (tile.GreenWire) val += 4;
if (tile.YellowWire) val += 8;
return val;
}
public static bool GetWire(Tile tile, int channel)
{
switch (channel)
{
case 0: return tile.RedWire;
case 1: return tile.BlueWire;
case 2: return tile.GreenWire;
case 3: return tile.YellowWire;
default: return false;
}
}
public static List<T> Concat<T>(List<T> a, List<T> b)
{
List<T> ret = new List<T>();
foreach (T obj in a)
{
ret.Add(obj);
}
foreach (T obj in b)
{
ret.Add(obj);
}
return ret;
}
public static bool TransferCharge(int amount, int i, int j, int width = 1, int height = 1)
{
scanned.Clear();
List<Point> consumers = new List<Point>();
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
for (int c = 0; c < 4; c++)
{
if (GetWire(Main.tile[i + x, j + y], c))
consumers = Concat(consumers, Search(i + x, j + y, c));
}
}
}
if (consumers.Count() == 0)
{
return false;
}
int splitCharge = amount / consumers.Count();
foreach (Point p in consumers)
{
Tile tile = Main.tile[p];
if (ModContent.GetModTile(tile.TileType) is IPowerConsumer consumer)
consumer.InsertPower(p.X, p.Y, splitCharge);
}
return true;
}
public static List<Point> Search(int i, int j, int channel)
{
return Search(new Wire(i, j, channel));
}
public static List<Point> Search(Wire wire) {
List<Point> list = new List<Point>();
scanned.Add(wire);
Point p = new Point(wire.X, wire.Y);
Tile tile = Main.tile[p];
if (ModContent.GetModTile(tile.TileType) is IPowerConsumer consumer && consumer.IsConsumer(p.X, p.Y)) {
list.Add(p);
}
PowerDisplayInfo displayInfo = new PowerDisplayInfo(wire.C);
foreach (Direction dir in Direction.directions()) {
Point target = p + dir.point;
Wire w = new Wire(target.X, target.Y, wire.C);
tile = Main.tile[target];
if (!scanned.Contains(w) && GetWire(tile, wire.C)) {
displayInfo.AddDirection(dir);
list = Concat(list, Search(w));
}
}
if (Main.tile[wire.X, wire.Y].TileType == ModContent.TileType<CableConnector>())
{
CableConnectorTE connectorTE = CableConnector.GetTileEntity(wire.X, wire.Y);
CableConnectorTE connectedTE = TileEntity.ByID[connectorTE.connectedID] as CableConnectorTE;
Wire w = new Wire(connectedTE.Position.X, connectedTE.Position.Y, wire.C);
if (!scanned.Contains(w) && GetWire(Main.tile[connectedTE.Position.X, connectedTE.Position.Y], wire.C))
list = Concat(list, Search(w));
}
foreach (Wire point in DisplayInfos.Keys)
{
if (point.X == p.X && point.Y == p.Y)
{
DisplayInfos[point].age = 0;
return list;
}
}
DisplayInfos.Add(wire, displayInfo);
return list;
}
}
}