-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileEnergyEmitter.java
executable file
·245 lines (194 loc) · 5.65 KB
/
TileEnergyEmitter.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
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* 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.energy;
import java.util.Map;
import java.util.TreeMap;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.World;
import buildcraft.api.core.BlockIndex;
import buildcraft.api.core.SafeTimeTracker;
import buildcraft.api.mj.MjBattery;
import buildcraft.core.LaserData;
import buildcraft.core.TileBuildCraft;
import buildcraft.core.network.RPC;
import buildcraft.core.network.RPCHandler;
import buildcraft.core.network.RPCMessageInfo;
import buildcraft.core.network.RPCSide;
public class TileEnergyEmitter extends TileBuildCraft {
@MjBattery (maxCapacity = 1024, maxReceivedPerCycle = 1204, minimumConsumption = 0)
public double mjStored;
public Map<BlockIndex, Target> targets = new TreeMap<BlockIndex, Target>();
public float mjAcc = 0;
public int accumulated = 0;
private SafeTimeTracker syncMJ = new SafeTimeTracker(20, 5);
private SafeTimeTracker scanTracker = new SafeTimeTracker(100, 10);
public static class Target {
public LaserData data = new LaserData();
TileEnergyReceiver receiver;
}
public TileEnergyEmitter () {
}
@Override
public void initialize () {
super.initialize();
if (worldObj.isRemote) {
RPCHandler.rpcServer(this, "requestLasers");
}
}
@Override
public void updateEntity() {
super.updateEntity();
if (worldObj.isRemote) {
for (Target t : targets.values()) {
if (t.data.isVisible) {
t.data.update();
t.data.wavePosition += 0.2F;
if (t.data.wavePosition > t.data.renderSize) {
t.data.wavePosition = 0;
t.data.waveSize = (float) (mjStored / targets.size() / 10F);
if (t.data.waveSize > 1) {
t.data.waveSize = 1F;
}
}
t.data.iterateTexture();
}
}
return;
}
if (scanTracker.markTimeIfDelay(worldObj)) {
for (TileEnergyReceiver receiver : TileEnergyReceiver.knownReceivers) {
float dx = xCoord - receiver.xCoord;
float dy = yCoord - receiver.yCoord;
float dz = zCoord - receiver.zCoord;
if (dx * dx + dy * dy + dz * dz < 100 * 100) {
BlockIndex index = new BlockIndex(receiver.xCoord, receiver.yCoord, receiver.zCoord);
if (!targets.containsKey(index)) {
addLaser(receiver.xCoord, receiver.yCoord,
receiver.zCoord);
RPCHandler.rpcBroadcastWorldPlayers(worldObj, this, "addLaser",
receiver.xCoord, receiver.yCoord,
receiver.zCoord);
targets.get(index).receiver = receiver;
}
}
}
}
// synchronize regularly with the client an average of the energy in
// the emitter
mjAcc += mjStored;
accumulated++;
if (syncMJ.markTimeIfDelay(worldObj)) {
RPCHandler.rpcBroadcastWorldPlayers(worldObj, this, "synchronizeMJ", mjAcc
/ accumulated);
mjAcc = 0;
accumulated = 0;
}
if (mjStored == 0) {
for (Target t : targets.values()) {
if (t.data.isVisible) {
t.data.isVisible = false;
RPCHandler.rpcBroadcastWorldPlayers(worldObj, this, "disableLaser",
t.receiver.xCoord, t.receiver.yCoord,
t.receiver.zCoord);
}
}
} else {
double perTargetEnergy = 10;
if (mjStored > targets.size() * 10) {
mjStored -= targets.size() * 10;
} else {
perTargetEnergy = mjStored / targets.size();
mjStored = 0;
}
for (Target t : targets.values()) {
if (!t.data.isVisible) {
t.data.isVisible = true;
RPCHandler.rpcBroadcastWorldPlayers(worldObj, this, "enableLaser",
t.receiver.xCoord, t.receiver.yCoord,
t.receiver.zCoord);
}
}
for (Target t : targets.values()) {
t.receiver.energyStored += perTargetEnergy;
}
}
}
@RPC (RPCSide.CLIENT)
public void synchronizeMJ (float val) {
mjStored = val;
}
@RPC (RPCSide.CLIENT)
public void addLaser (int x, int y, int z) {
BlockIndex index = new BlockIndex(x, y, z);
if (!targets.containsKey(index)) {
Target t = new Target();
t.data.head.x = xCoord + 0.5F;
t.data.head.y = yCoord + 0.5F;
t.data.head.z = zCoord + 0.5F;
t.data.tail.x = x + 0.5F;
t.data.tail.y = y + 0.5F;
t.data.tail.z = z + 0.5F;
targets.put(index, t);
}
}
@RPC (RPCSide.CLIENT)
public void enableLaser (int x, int y, int z) {
BlockIndex index = new BlockIndex(x, y, z);
if (targets.containsKey(index)) {
targets.get(index).data.isVisible = true;
}
}
@RPC (RPCSide.CLIENT)
public void disableLaser (int x, int y, int z) {
BlockIndex index = new BlockIndex(x, y, z);
if (targets.containsKey(index)) {
targets.get(index).data.isVisible = false;
}
}
@RPC (RPCSide.SERVER)
public void requestLasers (RPCMessageInfo info) {
for (BlockIndex b : targets.keySet()) {
RPCHandler.rpcPlayer(info.sender, this, "addLaser", b.x, b.y, b.z);
}
}
@Override
public World getWorld() {
return worldObj;
}
@Override
public AxisAlignedBB getRenderBoundingBox() {
double xMin = xCoord;
double yMin = yCoord;
double zMin = zCoord;
double xMax = xCoord + 1.0;
double yMax = yCoord + 1.0;
double zMax = zCoord + 1.0;
for (Target t : targets.values()) {
if (t.data.tail.x < xMin) {
xMin = t.data.tail.x;
}
if (t.data.tail.y < yMin) {
yMin = t.data.tail.y;
}
if (t.data.tail.z < zMin) {
zMin = t.data.tail.z;
}
if (t.data.tail.x > xMax) {
xMax = t.data.tail.x;
}
if (t.data.tail.y > yMax) {
yMax = t.data.tail.y;
}
if (t.data.tail.z > zMax) {
zMax = t.data.tail.z;
}
}
return AxisAlignedBB.getBoundingBox(xMin, yMin, zMin, xMax, yMax, zMax);
}
}