-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileEngineBase.java
630 lines (520 loc) · 15.6 KB
/
TileEngineBase.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
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
/**
* 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.core.lib.engines;
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ICrafting;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.util.ForgeDirection;
import cofh.api.energy.IEnergyConnection;
import cofh.api.energy.IEnergyHandler;
import cofh.api.energy.IEnergyReceiver;
import buildcraft.BuildCraftCore;
import buildcraft.api.power.IEngine;
import buildcraft.api.tiles.IHeatable;
import buildcraft.api.tools.IToolWrench;
import buildcraft.api.transport.IPipeConnection;
import buildcraft.api.transport.IPipeTile;
import buildcraft.core.CompatHooks;
import buildcraft.core.lib.block.TileBuildCraft;
import buildcraft.core.lib.utils.MathUtils;
import buildcraft.core.lib.utils.ResourceUtils;
import buildcraft.core.lib.utils.Utils;
public abstract class TileEngineBase extends TileBuildCraft implements IPipeConnection, IEnergyHandler, IEngine, IHeatable {
// TEMP
public static final ResourceLocation TRUNK_BLUE_TEXTURE = new ResourceLocation("buildcraftcore:textures/blocks/engine/trunk_blue.png");
public static final ResourceLocation TRUNK_GREEN_TEXTURE = new ResourceLocation("buildcraftcore:textures/blocks/engine/trunk_green.png");
public static final ResourceLocation TRUNK_YELLOW_TEXTURE = new ResourceLocation("buildcraftcore:textures/blocks/engine/trunk_yellow.png");
public static final ResourceLocation TRUNK_RED_TEXTURE = new ResourceLocation("buildcraftcore:textures/blocks/engine/trunk_red.png");
public static final ResourceLocation TRUNK_OVERHEAT_TEXTURE = new ResourceLocation("buildcraftcore:textures/blocks/engine/trunk_overheat.png");
public enum EnergyStage {
BLUE, GREEN, YELLOW, RED, OVERHEAT;
public static final EnergyStage[] VALUES = values();
}
public static final float MIN_HEAT = 20;
public static final float IDEAL_HEAT = 100;
public static final float MAX_HEAT = 250;
public int currentOutput = 0;
public boolean isRedstonePowered = false;
public float progress;
public int energy;
public float heat = MIN_HEAT;
public EnergyStage energyStage = EnergyStage.BLUE;
public ForgeDirection orientation = ForgeDirection.UP;
protected int progressPart = 0;
private boolean checkOrientation = false;
private boolean isPumping = false; // Used for SMP synch
public TileEngineBase() {
}
@Override
public void initialize() {
if (!worldObj.isRemote) {
checkRedstonePower();
}
}
public abstract String getResourcePrefix();
public ResourceLocation getBaseTexture() {
return new ResourceLocation(getResourcePrefix() + "/base.png");
}
public ResourceLocation getChamberTexture() {
return new ResourceLocation(getResourcePrefix() + "/chamber.png");
}
public ResourceLocation getTrunkTexture(EnergyStage stage) {
if (ResourceUtils.resourceExists(getResourcePrefix() + "/trunk.png")) {
return new ResourceLocation(getResourcePrefix() + "/trunk.png");
}
switch (stage) {
case BLUE:
return TRUNK_BLUE_TEXTURE;
case GREEN:
return TRUNK_GREEN_TEXTURE;
case YELLOW:
return TRUNK_YELLOW_TEXTURE;
case RED:
return TRUNK_RED_TEXTURE;
case OVERHEAT:
return TRUNK_OVERHEAT_TEXTURE;
default:
return TRUNK_RED_TEXTURE;
}
}
public boolean onBlockActivated(EntityPlayer player, ForgeDirection side) {
if (!player.worldObj.isRemote && player.getCurrentEquippedItem() != null &&
player.getCurrentEquippedItem().getItem() instanceof IToolWrench) {
IToolWrench wrench = (IToolWrench) player.getCurrentEquippedItem().getItem();
if (wrench.canWrench(player, xCoord, yCoord, zCoord)) {
if (getEnergyStage() == EnergyStage.OVERHEAT && !Utils.isFakePlayer(player)) {
energyStage = computeEnergyStage();
sendNetworkUpdate();
}
checkOrientation = true;
wrench.wrenchUsed(player, xCoord, yCoord, zCoord);
return true;
}
}
return false;
}
public double getEnergyLevel() {
return ((double) energy) / getMaxEnergy();
}
protected EnergyStage computeEnergyStage() {
float energyLevel = getHeatLevel();
if (energyLevel < 0.25f) {
return EnergyStage.BLUE;
} else if (energyLevel < 0.5f) {
return EnergyStage.GREEN;
} else if (energyLevel < 0.75f) {
return EnergyStage.YELLOW;
} else if (energyLevel < 1f) {
return EnergyStage.RED;
} else {
return EnergyStage.OVERHEAT;
}
}
public final EnergyStage getEnergyStage() {
if (!worldObj.isRemote) {
if (energyStage == EnergyStage.OVERHEAT) {
return energyStage;
}
EnergyStage newStage = computeEnergyStage();
if (energyStage != newStage) {
energyStage = newStage;
if (energyStage == EnergyStage.OVERHEAT) {
overheat();
}
sendNetworkUpdate();
}
}
return energyStage;
}
public void overheat() {
this.isPumping = false;
if (BuildCraftCore.canEnginesExplode) {
worldObj.createExplosion(null, xCoord, yCoord, zCoord, 3, true);
worldObj.setBlockToAir(xCoord, yCoord, zCoord);
}
}
public void updateHeat() {
heat = (float) ((MAX_HEAT - MIN_HEAT) * getEnergyLevel()) + MIN_HEAT;
}
public float getHeatLevel() {
return (heat - MIN_HEAT) / (MAX_HEAT - MIN_HEAT);
}
public float getPistonSpeed() {
if (!worldObj.isRemote) {
return Math.max(0.16f * getHeatLevel(), 0.01f);
}
switch (getEnergyStage()) {
case BLUE:
return 0.02F;
case GREEN:
return 0.04F;
case YELLOW:
return 0.08F;
case RED:
return 0.16F;
default:
return 0;
}
}
@Override
public void updateEntity() {
super.updateEntity();
if (worldObj.isRemote) {
if (progressPart != 0) {
progress += getPistonSpeed();
if (progress > 1) {
progressPart = 0;
progress = 0;
}
} else if (this.isPumping) {
progressPart = 1;
}
return;
}
if (checkOrientation) {
checkOrientation = false;
if (!isOrientationValid()) {
switchOrientation(true);
}
}
updateHeat();
getEnergyStage();
if (getEnergyStage() == EnergyStage.OVERHEAT) {
this.energy = Math.max(this.energy - 50, 0);
return;
}
engineUpdate();
Object tile = getEnergyProvider(orientation);
if (progressPart != 0) {
progress += getPistonSpeed();
if (progress > 0.5 && progressPart == 1) {
progressPart = 2;
} else if (progress >= 1) {
progress = 0;
progressPart = 0;
}
} else if (isRedstonePowered && isActive()) {
if (isPoweredTile(tile, orientation)) {
progressPart = 1;
setPumping(true);
if (getPowerToExtract() > 0) {
progressPart = 1;
setPumping(true);
} else {
setPumping(false);
}
} else {
setPumping(false);
}
} else {
setPumping(false);
}
burn();
if (!isRedstonePowered) {
currentOutput = 0;
} else if (isRedstonePowered && isActive()) {
sendPower();
}
}
public Object getEnergyProvider(ForgeDirection orientation) {
return CompatHooks.INSTANCE.getEnergyProvider(getTile(orientation));
}
private int getPowerToExtract() {
Object tile = getEnergyProvider(orientation);
if (tile instanceof IEngine) {
IEngine engine = (IEngine) tile;
int maxEnergy = engine.receiveEnergyFromEngine(
orientation.getOpposite(),
this.energy, true);
return extractEnergy(maxEnergy, false);
} else if (tile instanceof IEnergyHandler) {
IEnergyHandler handler = (IEnergyHandler) tile;
int maxEnergy = handler.receiveEnergy(
orientation.getOpposite(),
this.energy, true);
return extractEnergy(maxEnergy, false);
} else if (tile instanceof IEnergyReceiver) {
IEnergyReceiver handler = (IEnergyReceiver) tile;
int maxEnergy = handler.receiveEnergy(
orientation.getOpposite(),
this.energy, true);
return extractEnergy(maxEnergy, false);
} else {
return 0;
}
}
protected void sendPower() {
Object tile = getEnergyProvider(orientation);
if (isPoweredTile(tile, orientation)) {
int extracted = getPowerToExtract();
if (extracted <= 0) {
setPumping(false);
return;
}
setPumping(true);
if (tile instanceof IEngine) {
IEngine engine = (IEngine) tile;
int neededRF = engine.receiveEnergyFromEngine(
orientation.getOpposite(),
extracted, false);
extractEnergy(neededRF, true);
} else if (tile instanceof IEnergyHandler) {
IEnergyHandler handler = (IEnergyHandler) tile;
int neededRF = handler.receiveEnergy(
orientation.getOpposite(),
extracted, false);
extractEnergy(neededRF, true);
} else if (tile instanceof IEnergyReceiver) {
IEnergyReceiver handler = (IEnergyReceiver) tile;
int neededRF = handler.receiveEnergy(
orientation.getOpposite(),
extracted, false);
extractEnergy(neededRF, true);
}
}
}
protected void burn() {
}
protected void engineUpdate() {
if (!isRedstonePowered) {
if (energy >= 10) {
energy -= 10;
} else if (energy < 10) {
energy = 0;
}
}
}
public boolean isActive() {
return true;
}
protected final void setPumping(boolean isActive) {
if (this.isPumping == isActive) {
return;
}
this.isPumping = isActive;
sendNetworkUpdate();
}
public boolean isOrientationValid() {
Object tile = getEnergyProvider(orientation);
return isPoweredTile(tile, orientation);
}
public boolean switchOrientation(boolean preferPipe) {
if (preferPipe && switchOrientationDo(true)) {
return true;
} else {
return switchOrientationDo(false);
}
}
private boolean switchOrientationDo(boolean pipesOnly) {
for (int i = orientation.ordinal() + 1; i <= orientation.ordinal() + 6; ++i) {
ForgeDirection o = ForgeDirection.VALID_DIRECTIONS[i % 6];
TileEntity tile = getTile(o);
if ((!pipesOnly || tile instanceof IPipeTile) && isPoweredTile(tile, o)) {
orientation = o;
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord, worldObj.getBlock(xCoord, yCoord, zCoord));
return true;
}
}
return false;
}
@Override
public void invalidate() {
super.invalidate();
checkOrientation = true;
}
@Override
public void validate() {
super.validate();
checkOrientation = true;
}
@Override
public void readFromNBT(NBTTagCompound data) {
super.readFromNBT(data);
orientation = ForgeDirection.getOrientation(data.getByte("orientation"));
progress = data.getFloat("progress");
energy = data.getInteger("energy");
heat = data.getFloat("heat");
}
@Override
public void writeToNBT(NBTTagCompound data) {
super.writeToNBT(data);
data.setByte("orientation", (byte) orientation.ordinal());
data.setFloat("progress", progress);
data.setInteger("energy", energy);
data.setFloat("heat", heat);
}
@Override
public void readData(ByteBuf stream) {
int flags = stream.readUnsignedByte();
energyStage = EnergyStage.values()[flags & 0x07];
isPumping = (flags & 0x08) != 0;
orientation = ForgeDirection.getOrientation(stream.readByte());
}
@Override
public void writeData(ByteBuf stream) {
stream.writeByte(energyStage.ordinal() | (isPumping ? 8 : 0));
stream.writeByte(orientation.ordinal());
}
public void getGUINetworkData(int id, int value) {
switch (id) {
case 0:
int iEnergy = Math.round(energy);
iEnergy = (iEnergy & 0xffff0000) | (value & 0xffff);
energy = iEnergy;
break;
case 1:
iEnergy = Math.round(energy);
iEnergy = (iEnergy & 0xffff) | ((value & 0xffff) << 16);
energy = iEnergy;
break;
case 2:
currentOutput = value;
break;
case 3:
heat = value / 100F;
break;
}
}
public void sendGUINetworkData(Container container, ICrafting iCrafting) {
iCrafting.sendProgressBarUpdate(container, 0, Math.round(energy) & 0xffff);
iCrafting.sendProgressBarUpdate(container, 1, (Math.round(energy) & 0xffff0000) >> 16);
iCrafting.sendProgressBarUpdate(container, 2, Math.round(currentOutput));
iCrafting.sendProgressBarUpdate(container, 3, Math.round(heat * 100));
}
/* STATE INFORMATION */
public abstract boolean isBurning();
public void addEnergy(int addition) {
if (getEnergyStage() == EnergyStage.OVERHEAT) {
return;
}
energy += addition;
if (energy > getMaxEnergy()) {
energy = getMaxEnergy();
}
}
public int extractEnergy(int energyMax, boolean doExtract) {
int max = Math.min(energyMax, maxEnergyExtracted());
int extracted;
if (energy >= max) {
extracted = max;
if (doExtract) {
energy -= max;
}
} else {
extracted = energy;
if (doExtract) {
energy = 0;
}
}
return extracted;
}
public boolean isPoweredTile(Object tile, ForgeDirection side) {
if (tile == null) {
return false;
} else if (tile instanceof IEngine) {
return ((IEngine) tile).canReceiveFromEngine(side.getOpposite());
} else if (tile instanceof IEnergyHandler || tile instanceof IEnergyReceiver) {
return ((IEnergyConnection) tile).canConnectEnergy(side.getOpposite());
} else {
return false;
}
}
public abstract int getMaxEnergy();
public int minEnergyReceived() {
return 20;
}
public abstract int maxEnergyReceived();
public abstract int maxEnergyExtracted();
public int getEnergyStored() {
return energy;
}
public abstract int calculateCurrentOutput();
@Override
public ConnectOverride overridePipeConnection(IPipeTile.PipeType type, ForgeDirection with) {
if (type == IPipeTile.PipeType.POWER) {
return ConnectOverride.DEFAULT;
} else if (with == orientation) {
return ConnectOverride.DISCONNECT;
} else {
return ConnectOverride.DEFAULT;
}
}
public void checkRedstonePower() {
isRedstonePowered = worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord);
}
public void onNeighborUpdate() {
checkRedstonePower();
checkOrientation = true;
}
// RF support
@Override
public int extractEnergy(ForgeDirection from, int maxExtract,
boolean simulate) {
return 0;
}
@Override
public int getEnergyStored(ForgeDirection from) {
if (!(from == orientation)) {
return 0;
}
return energy;
}
@Override
public int getMaxEnergyStored(ForgeDirection from) {
return this.getMaxEnergy();
}
@Override
public boolean canConnectEnergy(ForgeDirection from) {
return from == orientation;
}
// IEngine
@Override
public boolean canReceiveFromEngine(ForgeDirection side) {
return side == orientation.getOpposite();
}
@Override
public int receiveEnergyFromEngine(ForgeDirection side, int amount, boolean simulate) {
if (canReceiveFromEngine(side)) {
int targetEnergy = Math.min(this.getMaxEnergy() - this.energy, amount);
if (!simulate) {
energy += targetEnergy;
}
return targetEnergy;
} else {
return 0;
}
}
// IHeatable
@Override
public double getMinHeatValue() {
return MIN_HEAT;
}
@Override
public double getIdealHeatValue() {
return IDEAL_HEAT;
}
@Override
public double getMaxHeatValue() {
return MAX_HEAT;
}
@Override
public double getCurrentHeatValue() {
return heat;
}
@Override
public double setHeatValue(double value) {
heat = (float) MathUtils.clamp(value, MIN_HEAT, MAX_HEAT);
return heat;
}
}