-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileLaser.java
340 lines (277 loc) · 7.54 KB
/
TileLaser.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
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
/**
* Copyright (c) 2011-2015, 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.silicon;
import java.util.LinkedList;
import java.util.List;
import io.netty.buffer.ByteBuf;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.core.Position;
import buildcraft.api.core.SafeTimeTracker;
import buildcraft.api.power.ILaserTarget;
import buildcraft.api.power.ILaserTargetBlock;
import buildcraft.api.tiles.IControllable;
import buildcraft.api.tiles.IHasWork;
import buildcraft.core.Box;
import buildcraft.core.EntityLaser;
import buildcraft.core.LaserData;
import buildcraft.core.lib.RFBattery;
import buildcraft.core.lib.block.TileBuildCraft;
import buildcraft.core.lib.utils.BlockUtils;
public class TileLaser extends TileBuildCraft implements IHasWork, IControllable {
private static final float LASER_OFFSET = 2.0F / 16.0F;
private static final short POWER_AVERAGING = 100;
public LaserData laser = new LaserData();
private final SafeTimeTracker laserTickTracker = new SafeTimeTracker(10);
private final SafeTimeTracker searchTracker = new SafeTimeTracker(100, 100);
private final SafeTimeTracker networkTracker = new SafeTimeTracker(20, 3);
private ILaserTarget laserTarget;
private int powerIndex = 0;
private short powerAverage = 0;
private final short[] power = new short[POWER_AVERAGING];
public TileLaser() {
super();
this.setBattery(new RFBattery(10000, 250, 0));
}
@Override
public void initialize() {
super.initialize();
if (laser == null) {
laser = new LaserData();
}
laser.isVisible = false;
laser.head = new Position(xCoord, yCoord, zCoord);
laser.tail = new Position(xCoord, yCoord, zCoord);
laser.isGlowing = true;
}
@Override
public void updateEntity() {
super.updateEntity();
laser.iterateTexture();
if (worldObj.isRemote) {
return;
}
// If a gate disabled us, remove laser and do nothing.
if (mode == IControllable.Mode.Off) {
removeLaser();
return;
}
// Check for any available tables at a regular basis
if (canFindTable()) {
findTable();
}
// If we still don't have a valid table or the existing has
// become invalid, we disable the laser and do nothing.
if (!isValidTable()) {
removeLaser();
return;
}
// Disable the laser and do nothing if no energy is available.
if (getBattery().getEnergyStored() == 0) {
removeLaser();
return;
}
// We have a table and can work, so we create a laser if
// necessary.
laser.isVisible = true;
// We have a laser and may update it
if (laser != null && canUpdateLaser()) {
updateLaser();
}
// Consume power and transfer it to the table.
int localPower = getBattery().useEnergy(0, getMaxPowerSent(), false);
laserTarget.receiveLaserEnergy(localPower);
if (laser != null) {
pushPower(localPower);
}
onPowerSent(localPower);
sendNetworkUpdate();
}
protected int getMaxPowerSent() {
return 40;
}
protected void onPowerSent(int power) {
}
protected boolean canFindTable() {
return searchTracker.markTimeIfDelay(worldObj);
}
protected boolean canUpdateLaser() {
return laserTickTracker.markTimeIfDelay(worldObj);
}
protected boolean isValidTable() {
if (laserTarget == null || laserTarget.isInvalidTarget() || !laserTarget.requiresLaserEnergy()) {
return false;
}
return true;
}
protected void findTable() {
int meta = getBlockMetadata();
int minX = xCoord - 5;
int minY = yCoord - 5;
int minZ = zCoord - 5;
int maxX = xCoord + 5;
int maxY = yCoord + 5;
int maxZ = zCoord + 5;
switch (ForgeDirection.getOrientation(meta)) {
case WEST:
maxX = xCoord;
break;
case EAST:
minX = xCoord;
break;
case DOWN:
maxY = yCoord;
break;
case UP:
minY = yCoord;
break;
case NORTH:
maxZ = zCoord;
break;
default:
case SOUTH:
minZ = zCoord;
break;
}
List<ILaserTarget> targets = new LinkedList<ILaserTarget>();
if (minY < 0) {
minY = 0;
}
if (maxY > 255) {
maxY = 255;
}
for (int y = minY; y <= maxY; ++y) {
for (int x = minX; x <= maxX; ++x) {
for (int z = minZ; z <= maxZ; ++z) {
if (BlockUtils.getBlock(worldObj, x, y, z) instanceof ILaserTargetBlock) {
TileEntity tile = BlockUtils.getTileEntity(worldObj, x, y, z);
if (tile instanceof ILaserTarget) {
ILaserTarget table = (ILaserTarget) tile;
if (table.requiresLaserEnergy()) {
targets.add(table);
}
}
}
}
}
}
if (targets.isEmpty()) {
return;
}
laserTarget = targets.get(worldObj.rand.nextInt(targets.size()));
}
protected void updateLaser() {
int meta = getBlockMetadata();
double px = 0, py = 0, pz = 0;
switch (ForgeDirection.getOrientation(meta)) {
case WEST:
px = -LASER_OFFSET;
break;
case EAST:
px = LASER_OFFSET;
break;
case DOWN:
py = -LASER_OFFSET;
break;
case UP:
py = LASER_OFFSET;
break;
case NORTH:
pz = -LASER_OFFSET;
break;
case SOUTH:
default:
pz = LASER_OFFSET;
break;
}
Position head = new Position(xCoord + 0.5 + px, yCoord + 0.5 + py, zCoord + 0.5 + pz);
Position tail = new Position(laserTarget.getXCoord() + 0.475 + (worldObj.rand.nextFloat() - 0.5) / 5F, laserTarget.getYCoord() + 9F / 16F,
laserTarget.getZCoord() + 0.475 + (worldObj.rand.nextFloat() - 0.5) / 5F);
laser.head = head;
laser.tail = tail;
if (!laser.isVisible) {
laser.isVisible = true;
}
}
protected void removeLaser() {
if (laser.isVisible) {
laser.isVisible = false;
// force sending the network update even if the network tracker
// refuses.
super.sendNetworkUpdate();
}
}
@Override
public void sendNetworkUpdate() {
if (networkTracker.markTimeIfDelay(worldObj)) {
super.sendNetworkUpdate();
}
}
@Override
public void readFromNBT(NBTTagCompound nbttagcompound) {
super.readFromNBT(nbttagcompound);
}
@Override
public void writeToNBT(NBTTagCompound nbttagcompound) {
super.writeToNBT(nbttagcompound);
}
@Override
public void readData(ByteBuf stream) {
laser = new LaserData();
laser.readData(stream);
powerAverage = stream.readShort();
}
@Override
public void writeData(ByteBuf stream) {
laser.writeData(stream);
stream.writeShort(powerAverage);
}
@Override
public void invalidate() {
super.invalidate();
removeLaser();
}
@Override
public boolean hasWork() {
return isValidTable();
}
private void pushPower(int received) {
powerAverage -= power[powerIndex];
powerAverage += received;
power[powerIndex] = (short) received;
powerIndex++;
if (powerIndex == power.length) {
powerIndex = 0;
}
}
public ResourceLocation getTexture() {
double avg = powerAverage / POWER_AVERAGING;
if (avg <= 10.0) {
return EntityLaser.LASER_TEXTURES[0];
} else if (avg <= 20.0) {
return EntityLaser.LASER_TEXTURES[1];
} else if (avg <= 30.0) {
return EntityLaser.LASER_TEXTURES[2];
} else {
return EntityLaser.LASER_TEXTURES[3];
}
}
@Override
public AxisAlignedBB getRenderBoundingBox() {
return new Box(this).extendToEncompass(laser.tail).getBoundingBox();
}
@Override
public boolean acceptsControlMode(Mode mode) {
return mode == IControllable.Mode.On ||
mode == IControllable.Mode.Off;
}
}