2014-02-01 17:19:07 +01:00
|
|
|
/**
|
|
|
|
* 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;
|
2014-05-03 18:59:52 +02:00
|
|
|
|
2014-06-13 13:36:02 +02:00
|
|
|
import buildcraft.api.core.BlockIndex;
|
2014-02-01 17:19:07 +01:00
|
|
|
import buildcraft.api.core.SafeTimeTracker;
|
2014-04-06 18:05:39 +02:00
|
|
|
import buildcraft.api.mj.MjBattery;
|
2014-02-01 17:19:07 +01:00
|
|
|
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 {
|
|
|
|
|
2014-04-06 18:05:39 +02:00
|
|
|
@MjBattery (maxCapacity = 1024, maxReceivedPerCycle = 1204, minimumConsumption = 0)
|
|
|
|
public double mjStored;
|
2014-02-01 17:19:07 +01:00
|
|
|
|
2014-05-03 18:59:52 +02:00
|
|
|
public Map<BlockIndex, Target> targets = new TreeMap<BlockIndex, Target>();
|
|
|
|
|
2014-02-01 17:19:07 +01:00
|
|
|
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();
|
|
|
|
|
2014-02-16 12:10:01 +01:00
|
|
|
if (worldObj.isRemote) {
|
2014-02-01 17:19:07 +01:00
|
|
|
RPCHandler.rpcServer(this, "requestLasers");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateEntity() {
|
|
|
|
super.updateEntity();
|
|
|
|
|
2014-02-16 12:10:01 +01:00
|
|
|
if (worldObj.isRemote) {
|
2014-02-01 17:19:07 +01:00
|
|
|
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;
|
2014-04-06 18:05:39 +02:00
|
|
|
t.data.waveSize = (float) (mjStored / targets.size() / 10F);
|
2014-02-01 17:19:07 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2014-06-22 11:49:59 +02:00
|
|
|
RPCHandler.rpcBroadcastWorldPlayers(worldObj, this, "addLaser",
|
2014-02-01 17:19:07 +01:00
|
|
|
receiver.xCoord, receiver.yCoord,
|
|
|
|
receiver.zCoord);
|
|
|
|
|
|
|
|
targets.get(index).receiver = receiver;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// synchronize regularly with the client an average of the energy in
|
|
|
|
// the emitter
|
|
|
|
|
2014-04-06 18:05:39 +02:00
|
|
|
mjAcc += mjStored;
|
2014-02-01 17:19:07 +01:00
|
|
|
accumulated++;
|
|
|
|
|
|
|
|
if (syncMJ.markTimeIfDelay(worldObj)) {
|
2014-06-22 11:49:59 +02:00
|
|
|
RPCHandler.rpcBroadcastWorldPlayers(worldObj, this, "synchronizeMJ", mjAcc
|
2014-02-16 12:10:01 +01:00
|
|
|
/ accumulated);
|
2014-02-01 17:19:07 +01:00
|
|
|
mjAcc = 0;
|
|
|
|
accumulated = 0;
|
|
|
|
}
|
|
|
|
|
2014-04-06 18:05:39 +02:00
|
|
|
if (mjStored == 0) {
|
2014-02-01 17:19:07 +01:00
|
|
|
for (Target t : targets.values()) {
|
|
|
|
if (t.data.isVisible) {
|
|
|
|
t.data.isVisible = false;
|
2014-06-22 11:49:59 +02:00
|
|
|
RPCHandler.rpcBroadcastWorldPlayers(worldObj, this, "disableLaser",
|
2014-02-01 17:19:07 +01:00
|
|
|
t.receiver.xCoord, t.receiver.yCoord,
|
|
|
|
t.receiver.zCoord);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2014-04-06 18:05:39 +02:00
|
|
|
double perTargetEnergy = 10;
|
2014-02-01 17:19:07 +01:00
|
|
|
|
2014-04-06 18:05:39 +02:00
|
|
|
if (mjStored > targets.size() * 10) {
|
|
|
|
mjStored -= targets.size() * 10;
|
2014-02-01 17:19:07 +01:00
|
|
|
} else {
|
2014-04-06 18:05:39 +02:00
|
|
|
perTargetEnergy = mjStored / targets.size();
|
|
|
|
mjStored = 0;
|
2014-02-01 17:19:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (Target t : targets.values()) {
|
|
|
|
if (!t.data.isVisible) {
|
|
|
|
t.data.isVisible = true;
|
2014-06-22 11:49:59 +02:00
|
|
|
RPCHandler.rpcBroadcastWorldPlayers(worldObj, this, "enableLaser",
|
2014-02-01 17:19:07 +01:00
|
|
|
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) {
|
2014-04-06 18:05:39 +02:00
|
|
|
mjStored = val;
|
2014-02-01 17:19:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@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()) {
|
2014-06-08 11:58:55 +02:00
|
|
|
RPCHandler.rpcPlayer(info.sender, this, "addLaser", b.x, b.y, b.z);
|
2014-02-01 17:19:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
|
|
|
}
|
|
|
|
}
|