-
Notifications
You must be signed in to change notification settings - Fork 497
/
PipeTransportPower.java
293 lines (235 loc) · 8.44 KB
/
PipeTransportPower.java
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
/**
* Copyright (c) SpaceToad, 2011 http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public License
* 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.transport;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import buildcraft.BuildCraftCore;
import buildcraft.api.core.SafeTimeTracker;
import buildcraft.api.gates.ITrigger;
import buildcraft.api.power.IPowerProvider;
import buildcraft.api.power.IPowerReceptor;
import buildcraft.core.DefaultProps;
import buildcraft.core.IMachine;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.utils.Utils;
import buildcraft.transport.network.PacketPowerUpdate;
public class PipeTransportPower extends PipeTransport {
private static final short MAX_DISPLAY = 100;
private static final double DISPLAY_SMOOTHING = 5.0;
private boolean needsInit = true;
private TileEntity[] tiles = new TileEntity[6];
public double[] displayPower = new double[6];
public short[] clientDisplayPower = new short[6];
public boolean overload;
public int[] powerQuery = new int[6];
public int[] nextPowerQuery = new int[6];
public long currentDate;
public double[] internalPower = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
public double[] internalNextPower = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
public int maxPower = 8;
public PipeTransportPower() {
for (int i = 0; i < 6; ++i) {
powerQuery[i] = 0;
}
}
SafeTimeTracker tracker = new SafeTimeTracker();
@Override
public boolean canPipeConnect(TileEntity tile, ForgeDirection side) {
if (tile instanceof TileGenericPipe) {
Pipe pipe2 = ((TileGenericPipe) tile).pipe;
if (BlockGenericPipe.isValid(pipe2) && !(pipe2.transport instanceof PipeTransportPower))
return false;
}
return tile instanceof TileGenericPipe || tile instanceof IMachine || tile instanceof IPowerReceptor;
}
@Override
public void onNeighborBlockChange(int blockId) {
super.onNeighborBlockChange(blockId);
updateTiles();
}
private void updateTiles() {
for (int i = 0; i < 6; ++i) {
TileEntity tile = container.getTile(ForgeDirection.VALID_DIRECTIONS[i]);
if (Utils.checkPipesConnections(tile, container)) {
tiles[i] = tile;
} else {
tiles[i] = null;
}
}
}
private void init() {
if (needsInit) {
needsInit = false;
updateTiles();
}
}
@Override
public void updateEntity() {
if (CoreProxy.proxy.isRenderWorld(worldObj))
return;
step();
init();
// Send the power to nearby pipes who requested it
double highestPower = 0;
for (int i = 0; i < 6; ++i) {
if (internalPower[i] > 0) {
double div = 0;
for (int j = 0; j < 6; ++j) {
if (j != i && powerQuery[j] > 0)
if (tiles[j] instanceof TileGenericPipe || tiles[j] instanceof IPowerReceptor) {
div += powerQuery[j];
}
}
double totalWatt = internalPower[i];
if (totalWatt > highestPower) {
highestPower = totalWatt;
}
for (int j = 0; j < 6; ++j) {
if (j != i && powerQuery[j] > 0) {
double watts = (totalWatt / div * powerQuery[j]);
if (tiles[j] instanceof TileGenericPipe) {
TileGenericPipe nearbyTile = (TileGenericPipe) tiles[j];
PipeTransportPower nearbyTransport = (PipeTransportPower) nearbyTile.pipe.transport;
nearbyTransport.receiveEnergy(ForgeDirection.VALID_DIRECTIONS[j].getOpposite(), watts);
displayPower[j] = (displayPower[j] * (DISPLAY_SMOOTHING - 1.0) + watts) / DISPLAY_SMOOTHING;
displayPower[i] = (displayPower[i] * (DISPLAY_SMOOTHING - 1.0) + watts) / DISPLAY_SMOOTHING;
internalPower[i] -= watts;
} else if (tiles[j] instanceof IPowerReceptor) {
IPowerReceptor pow = (IPowerReceptor) tiles[j];
IPowerProvider prov = pow.getPowerProvider();
if (prov != null) {
prov.receiveEnergy((float) watts, ForgeDirection.VALID_DIRECTIONS[j].getOpposite());
displayPower[j] = (displayPower[j] * (DISPLAY_SMOOTHING - 1.0) + watts) / DISPLAY_SMOOTHING;
displayPower[i] = (displayPower[i] * (DISPLAY_SMOOTHING - 1.0) + watts) / DISPLAY_SMOOTHING;
internalPower[i] -= watts;
}
}
}
}
}
}
overload = highestPower > maxPower * 0.9;
// Compute the tiles requesting energy that are not pipes
for (int i = 0; i < 6; ++i) {
if (tiles[i] instanceof IPowerReceptor && !(tiles[i] instanceof TileGenericPipe)) {
IPowerReceptor receptor = (IPowerReceptor) tiles[i];
int request = receptor.powerRequest(ForgeDirection.VALID_DIRECTIONS[i].getOpposite());
if (request > 0) {
requestEnergy(ForgeDirection.VALID_DIRECTIONS[i], request);
}
}
}
// Sum the amount of energy requested on each side
int transferQuery[] = {0, 0, 0, 0, 0, 0};
for (int i = 0; i < 6; ++i) {
transferQuery[i] = 0;
for (int j = 0; j < 6; ++j) {
if (j != i) {
transferQuery[i] += powerQuery[j];
}
}
}
// Transfer the requested energy to nearby pipes
for (int i = 0; i < 6; ++i) {
if (transferQuery[i] != 0) {
if (tiles[i] != null) {
TileEntity entity = tiles[i];
if (entity instanceof TileGenericPipe) {
TileGenericPipe nearbyTile = (TileGenericPipe) entity;
if (nearbyTile.pipe == null) {
continue;
}
PipeTransportPower nearbyTransport = (PipeTransportPower) nearbyTile.pipe.transport;
nearbyTransport.requestEnergy(ForgeDirection.VALID_DIRECTIONS[i].getOpposite(), transferQuery[i]);
}
}
}
}
if (tracker.markTimeIfDelay(worldObj, 2 * BuildCraftCore.updateFactor)) {
PacketPowerUpdate packet = new PacketPowerUpdate(xCoord, yCoord, zCoord);
double displayFactor = MAX_DISPLAY / 256.0;
for (int i = 0; i < clientDisplayPower.length; i++) {
clientDisplayPower[i] = (short) (displayPower[i] * displayFactor + .9999);
}
packet.displayPower = clientDisplayPower;
packet.overload = overload;
CoreProxy.proxy.sendToPlayers(packet.getPacket(), worldObj, xCoord, yCoord, zCoord, DefaultProps.PIPE_CONTENTS_RENDER_DIST);
}
}
public void step() {
if (currentDate != worldObj.getWorldTime()) {
currentDate = worldObj.getWorldTime();
powerQuery = nextPowerQuery;
nextPowerQuery = new int[]{0, 0, 0, 0, 0, 0};
internalPower = internalNextPower;
internalNextPower = new double[6];
for (int i = 0; i < nextPowerQuery.length; i++) {
if (powerQuery[i] == 0.0d && internalNextPower[i] > 0) {
internalNextPower[i] -= 1;
}
}
}
}
public void receiveEnergy(ForgeDirection from, double val) {
step();
if (this.container.pipe instanceof IPipeTransportPowerHook) {
((IPipeTransportPowerHook) this.container.pipe).receiveEnergy(from, val);
} else {
internalNextPower[from.ordinal()] += val;
if (internalNextPower[from.ordinal()] >= maxPower) {
internalNextPower[from.ordinal()] = maxPower;
}
}
}
public void requestEnergy(ForgeDirection from, int i) {
step();
if (this.container.pipe instanceof IPipeTransportPowerHook) {
((IPipeTransportPowerHook) this.container.pipe).requestEnergy(from, i);
} else {
step();
nextPowerQuery[from.ordinal()] += i;
}
}
@Override
public void initialize() {
currentDate = worldObj.getWorldTime();
}
@Override
public void readFromNBT(NBTTagCompound nbttagcompound) {
super.readFromNBT(nbttagcompound);
for (int i = 0; i < 6; ++i) {
powerQuery[i] = nbttagcompound.getInteger("powerQuery[" + i + "]");
nextPowerQuery[i] = nbttagcompound.getInteger("nextPowerQuery[" + i + "]");
internalPower[i] = nbttagcompound.getDouble("internalPower[" + i + "]");
internalNextPower[i] = nbttagcompound.getDouble("internalNextPower[" + i + "]");
}
}
@Override
public void writeToNBT(NBTTagCompound nbttagcompound) {
super.writeToNBT(nbttagcompound);
for (int i = 0; i < 6; ++i) {
nbttagcompound.setInteger("powerQuery[" + i + "]", powerQuery[i]);
nbttagcompound.setInteger("nextPowerQuery[" + i + "]", nextPowerQuery[i]);
nbttagcompound.setDouble("internalPower[" + i + "]", internalPower[i]);
nbttagcompound.setDouble("internalNextPower[" + i + "]", internalNextPower[i]);
}
}
public boolean isTriggerActive(ITrigger trigger) {
return false;
}
/**
* Client-side handler for receiving power updates from the server;
*
* @param packetPower
*/
public void handlePowerPacket(PacketPowerUpdate packetPower) {
clientDisplayPower = packetPower.displayPower;
overload = packetPower.overload;
}
}