diff --git a/src/main/java/crazypants/enderio/machine/light/BlockLightNode.java b/src/main/java/crazypants/enderio/machine/light/BlockLightNode.java index b90035e6..97372066 100644 --- a/src/main/java/crazypants/enderio/machine/light/BlockLightNode.java +++ b/src/main/java/crazypants/enderio/machine/light/BlockLightNode.java @@ -25,6 +25,7 @@ public class BlockLightNode extends BlockEio { super(ModObject.blockLightNode.unlocalisedName, TileLightNode.class, Material.air); setCreativeTab(null); setBlockBounds(0, 0, 0, 0, 0, 0); + setTickRandomly(true); } @Override @@ -39,13 +40,7 @@ public class BlockLightNode extends BlockEio { @Override public boolean isBlockSolid(IBlockAccess iblockaccess, int x, int y, int z, int l) { - Block blockID = iblockaccess.getBlock(x, y, z); - if(blockID == this) { - return false; - } else { - - return super.isBlockSolid(iblockaccess, x, y, z, l); - } + return false; } @Override @@ -68,17 +63,7 @@ public class BlockLightNode extends BlockEio { @Override public int getLightValue(IBlockAccess world, int x, int y, int z) { - Block block = world.getBlock(x, y, z); - if(block != null && block != this) { - return block.getLightValue(world, x, y, z); - } - int onVal = 15; - // TileEntity te = world.getTileEntity(x, y, z); - // if(te instanceof TileLightNode && ((TileLightNode)te).isDiagnal) { - // System.out.println("BlockLightNode.getLightValue: "); - // onVal = 5; - // } - return world.getBlockMetadata(x, y, z) > 0 ? onVal : 0; + return world.getBlockMetadata(x, y, z) > 0 ? 15 : 0; } @Override @@ -89,6 +74,14 @@ public class BlockLightNode extends BlockEio { } } + @Override + public void updateTick(World world, int x, int y, int z, Random r) { + TileLightNode te = (TileLightNode) world.getTileEntity(x, y, z); + if(te != null) { + te.checkParent(); + } + } + @Override @SideOnly(Side.CLIENT) public void registerBlockIcons(IIconRegister iIconRegister) { diff --git a/src/main/java/crazypants/enderio/machine/light/TileElectricLight.java b/src/main/java/crazypants/enderio/machine/light/TileElectricLight.java index 6ba55d29..6f6f118f 100644 --- a/src/main/java/crazypants/enderio/machine/light/TileElectricLight.java +++ b/src/main/java/crazypants/enderio/machine/light/TileElectricLight.java @@ -1,8 +1,10 @@ package crazypants.enderio.machine.light; import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; import java.util.List; -import java.util.Map; +import java.util.Set; import net.minecraft.block.Block; import net.minecraft.nbt.NBTTagCompound; @@ -11,12 +13,8 @@ import net.minecraft.world.EnumSkyBlock; import net.minecraftforge.common.util.ForgeDirection; import crazypants.enderio.EnderIO; import crazypants.enderio.TileEntityEio; -import crazypants.enderio.config.Config; -import crazypants.enderio.machine.wireless.IWirelessCharger; -import crazypants.enderio.machine.wireless.WirelessChargerController; -import crazypants.enderio.power.BasicCapacitor; +import crazypants.enderio.machine.wireless.WirelessChargedLocation; import crazypants.enderio.power.Capacitors; -import crazypants.enderio.power.ICapacitor; import crazypants.enderio.power.IInternalPowerReceiver; import crazypants.enderio.power.PowerHandlerUtil; import crazypants.util.BlockCoord; @@ -43,17 +41,11 @@ public class TileElectricLight extends TileEntityEio implements IInternalPowerRe private boolean requiresPower = true; - private boolean isWireless; - private IWirelessCharger charger; - private int ticksSinceLastSearch = 50; - - private ICapacitor capacitor; + private WirelessChargedLocation chargedLocation; private int energyStoredRF; public TileElectricLight() { - capacitor = new BasicCapacitor(Capacitors.BASIC_CAPACITOR.capacitor.getMaxEnergyReceived(), 100); - energyStoredRF = 0; } public void onNeighborBlockChange(Block blockID) { @@ -99,11 +91,15 @@ public class TileElectricLight extends TileEntityEio implements IInternalPowerRe } public void setWireless(boolean wireless) { - isWireless = wireless; + if(!wireless) { + chargedLocation = null; + } else if(chargedLocation == null) { + chargedLocation = new WirelessChargedLocation(this); + } } public boolean isWireless() { - return isWireless; + return chargedLocation != null; } @Override @@ -112,20 +108,20 @@ public class TileElectricLight extends TileEntityEio implements IInternalPowerRe return; } - boolean hasRedstone = hasRedstoneSignal(); + boolean isActivated = hasRedstoneSignal() ^ isInvereted; - boolean isActivated = (requiresPower ? hasPower() : true) && (hasRedstone && !isInvereted || !hasRedstone && isInvereted); - - if(isActivated && requiresPower) { - setEnergyStored(getEnergyStored() - RF_USE_PER_TICK); - } - - if(!hasPower() && requiresPower) { - isActivated = false; - } + if(requiresPower) { + if(isActivated) { + if(!hasPower()) { + isActivated = false; + } else { + setEnergyStored(getEnergyStored() - RF_USE_PER_TICK); + } + } - if(init && requiresPower) { - updateLightNodes(); + if(init) { + updateLightNodes(); + } } if(isActivated != lastActive || init) { @@ -146,46 +142,12 @@ public class TileElectricLight extends TileEntityEio implements IInternalPowerRe lastActive = isActivated; } - if (isWireless) { - if (ticksSinceLastSearch > 50) { - charger = findNearestCharger(); - ticksSinceLastSearch = 0; - } else { - ticksSinceLastSearch++; - } - - if (charger != null && energyStoredRF < getMaxEnergyStored()) { - energyStoredRF += charger.takeEnergy(Math.min(getMaxEnergyStored() - energyStoredRF, 10)); + if (chargedLocation != null) { + if (energyStoredRF < getMaxEnergyStored()) { + energyStoredRF += chargedLocation.takeEnergy(Math.min(getMaxEnergyStored() - energyStoredRF, 10)); } } } - - private IWirelessCharger findNearestCharger() { - int minDist = Integer.MAX_VALUE; - BlockCoord charger = null; - Map map = WirelessChargerController.instance.getChargerMap(worldObj); - - if (map == null) { - return null; - } - - for (BlockCoord b : map.keySet()) { - int dist = b.distance(new BlockCoord(this)); - if (dist < minDist) { - minDist = dist; - charger = b; - } - } - - if (charger != null && minDist < Config.wirelessChargerRange) { - TileEntity te = charger.getTileEntity(worldObj); - if (te instanceof IWirelessCharger) { - return (IWirelessCharger) te; - } - } - - return null; - } public void onBlockRemoved() { if(!requiresPower) { @@ -200,17 +162,17 @@ public class TileElectricLight extends TileEntityEio implements IInternalPowerRe } private void updateLightNodes() { - if(!requiresPower) { - return; - } - updatingLightNodes = true; - List before = new ArrayList(17); - if(lightNodes != null) { + Set before; + if(lightNodes != null && !lightNodes.isEmpty()) { + before = new HashSet(lightNodes.size()); for (TileLightNode node : lightNodes) { - before.add(new NodeEntry(node)); + before.add(node.getLocation()); } + } else { + before = Collections.emptySet(); } - List after = new ArrayList(17); + Set after = new HashSet(17); + updatingLightNodes = true; try { if(lightNodeCoords != null) { @@ -255,19 +217,17 @@ public class TileElectricLight extends TileEntityEio implements IInternalPowerRe addDiaganals(diags, new Vector3d(), after); addDiaganals(diags, ForgeDirectionOffsets.forDirCopy(face.getOpposite()), after); - if(!areEqual(before, after)) { - + if(!before.equals(after)) { clearLightNodes(); - for (NodeEntry entry : after) { - worldObj.setBlock(entry.coord.x, entry.coord.y, entry.coord.z, EnderIO.blockLightNode); - TileEntity te = worldObj.getTileEntity(entry.coord.x, entry.coord.y, entry.coord.z); + for (BlockCoord entry : after) { + worldObj.setBlock(entry.x, entry.y, entry.z, EnderIO.blockLightNode); + TileEntity te = worldObj.getTileEntity(entry.x, entry.y, entry.z); if(te instanceof TileLightNode) { TileLightNode ln = (TileLightNode) te; ln.parentX = xCoord; ln.parentY = yCoord; ln.parentZ = zCoord; - ln.isDiagnal = entry.isDiagnal; lightNodes.add(ln); } } @@ -281,52 +241,37 @@ public class TileElectricLight extends TileEntityEio implements IInternalPowerRe } } - private boolean areEqual(List before, List after) { - if(before.size() != after.size()) { - return false; - } - for (NodeEntry entry : before) { - if(!after.contains(entry)) { - return false; - } - } - return true; - } - - private void addDiaganals(Vector3d[] diags, Vector3d trans, List result) { + private void addDiaganals(Vector3d[] diags, Vector3d trans, Set result) { Vector3d offset = new Vector3d(); offset.set(diags[0]); offset.add(diags[1]); - addNodeInDirection(offset.add(trans), true, result); + addNodeInDirection(offset.add(trans), result); offset.set(diags[0]); offset.sub(diags[1]); - addNodeInDirection(offset.add(trans), true, result); + addNodeInDirection(offset.add(trans), result); - offset.set(new Vector3d(diags[0]).negate()); + offset.set(diags[0]); + offset.negate(); offset.add(diags[1]); - addNodeInDirection(offset.add(trans), true, result); + addNodeInDirection(offset.add(trans), result); - offset.set(new Vector3d(diags[0]).negate()); + offset.set(diags[0]); + offset.negate(); offset.sub(diags[1]); - addNodeInDirection(offset.add(trans), true, result); + addNodeInDirection(offset.add(trans), result); } - private void addNodeInDirection(Vector3d offset, List after) { - addNodeInDirection(offset, false, after); - } - - private void addNodeInDirection(Vector3d offset, boolean diagnal, List result) { - + private void addNodeInDirection(Vector3d offset, Set result) { boolean isAir = isAir(offset); boolean isTransp = isTranparent(offset); if(isAir || isTransp) { offset.scale(2); if(isAir(offset)) { - addLightNode(offset, diagnal, result); + addLightNode(offset, result); } else if(isAir) { offset.scale(0.5); - addLightNode(offset, diagnal, result); + addLightNode(offset, result); } } } @@ -346,8 +291,7 @@ public class TileElectricLight extends TileEntityEio implements IInternalPowerRe } } - private void addLightNode(Vector3d offset, boolean isDiag, List result) { - + private void addLightNode(Vector3d offset, Set result) { int x = xCoord + (int) offset.x; int y = yCoord + (int) offset.y; int z = zCoord + (int) offset.z; @@ -359,17 +303,12 @@ public class TileElectricLight extends TileEntityEio implements IInternalPowerRe return; } } - result.add(new NodeEntry(new BlockCoord(x, y, z), isDiag)); + result.add(new BlockCoord(x, y, z)); } private boolean isRailcraftException(Block id) { - String className = id.getClass().getName(); - if(className.equals("mods.railcraft.common.blocks.machine.BlockMachine")) { - return true; - } - - return false; + return className.equals("mods.railcraft.common.blocks.machine.BlockMachine"); } private boolean isTranparent(Vector3d offset) { @@ -390,7 +329,7 @@ public class TileElectricLight extends TileEntityEio implements IInternalPowerRe face = ForgeDirection.values()[root.getShort("face")]; isInvereted = root.getBoolean("isInverted"); requiresPower = root.getBoolean("requiresPower"); - isWireless = root.getBoolean("isWireless"); + setWireless(root.getBoolean("isWireless")); if(root.hasKey("storedEnergy")) { float se = root.getFloat("storedEnergy"); @@ -409,7 +348,7 @@ public class TileElectricLight extends TileEntityEio implements IInternalPowerRe root.setInteger("storedEnergyRF", energyStoredRF); root.setBoolean("isInverted", isInvereted); root.setBoolean("requiresPower", requiresPower); - root.setBoolean("isWireless", isWireless); + root.setBoolean("isWireless", isWireless()); if(lightNodes != null) { int[] lnLoc = new int[lightNodes.size() * 3]; @@ -424,7 +363,7 @@ public class TileElectricLight extends TileEntityEio implements IInternalPowerRe } public boolean hasPower() { - return energyStoredRF > RF_USE_PER_TICK; + return energyStoredRF >= RF_USE_PER_TICK; } private boolean hasRedstoneSignal() { @@ -443,10 +382,7 @@ public class TileElectricLight extends TileEntityEio implements IInternalPowerRe @Override public boolean canConnectEnergy(ForgeDirection from) { - if(!requiresPower) { - return false; - } - return true; + return requiresPower; } @Override @@ -464,7 +400,7 @@ public class TileElectricLight extends TileEntityEio implements IInternalPowerRe if(!requiresPower) { return 0; } - return capacitor.getMaxEnergyReceived(); + return Capacitors.BASIC_CAPACITOR.capacitor.getMaxEnergyReceived(); } @Override @@ -480,7 +416,7 @@ public class TileElectricLight extends TileEntityEio implements IInternalPowerRe if(!requiresPower) { return 0; } - return capacitor.getMaxEnergyStored(); + return 100; } @Override @@ -488,56 +424,6 @@ public class TileElectricLight extends TileEntityEio implements IInternalPowerRe energyStoredRF = stored; } - static class NodeEntry { - final BlockCoord coord; - final boolean isDiagnal; - - NodeEntry(BlockCoord coord, boolean isDiagnal) { - this.coord = coord; - this.isDiagnal = isDiagnal; - } - - NodeEntry(TileLightNode node) { - coord = new BlockCoord(node); - isDiagnal = node.isDiagnal; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((coord == null) ? 0 : coord.hashCode()); - result = prime * result + (isDiagnal ? 1231 : 1237); - return result; - } - - @Override - public boolean equals(Object obj) { - if(this == obj) { - return true; - } - if(obj == null) { - return false; - } - if(getClass() != obj.getClass()) { - return false; - } - NodeEntry other = (NodeEntry) obj; - if(coord == null) { - if(other.coord != null) { - return false; - } - } else if(!coord.equals(other.coord)) { - return false; - } - if(isDiagnal != other.isDiagnal) { - return false; - } - return true; - } - - } - @Override public boolean displayPower() { return isRequiresPower(); diff --git a/src/main/java/crazypants/enderio/machine/light/TileLightNode.java b/src/main/java/crazypants/enderio/machine/light/TileLightNode.java index e55d4bbb..2a2e1fea 100644 --- a/src/main/java/crazypants/enderio/machine/light/TileLightNode.java +++ b/src/main/java/crazypants/enderio/machine/light/TileLightNode.java @@ -11,8 +11,6 @@ public class TileLightNode extends TileEntityEio { int parentY; int parentZ; - boolean isDiagnal = false; - public TileElectricLight getParent() { TileEntity te = worldObj.getTileEntity(parentX, parentY, parentZ); if(te instanceof TileElectricLight) { @@ -22,11 +20,12 @@ public class TileLightNode extends TileEntityEio { } @Override - public void doUpdate() { - if(worldObj.isRemote) { - return; - } - if(shouldDoWorkThisTick(42)) { + protected boolean shouldUpdate() { + return false; + } + + public void checkParent() { + if(worldObj.blockExists(parentX, parentY, parentZ)) { if(worldObj.getBlock(parentX, parentY, parentZ) != EnderIO.blockElectricLight) { worldObj.setBlockToAir(xCoord, yCoord, zCoord); } @@ -52,7 +51,6 @@ public class TileLightNode extends TileEntityEio { parentX = root.getInteger("parentX"); parentY = root.getInteger("parentY"); parentZ = root.getInteger("parentZ"); - isDiagnal = root.getBoolean("isDiagnal"); } @Override @@ -60,12 +58,11 @@ public class TileLightNode extends TileEntityEio { root.setInteger("parentX", parentX); root.setInteger("parentY", parentY); root.setInteger("parentZ", parentZ); - root.setBoolean("isDiagnal", isDiagnal); } @Override public String toString() { - return "TileLightNode [parentX=" + parentX + ", parentY=" + parentY + ", parentZ=" + parentZ + ", isDiagnal=" + isDiagnal + ", xCoord=" + xCoord + return "TileLightNode [parentX=" + parentX + ", parentY=" + parentY + ", parentZ=" + parentZ + ", xCoord=" + xCoord + ", yCoord=" + yCoord + ", zCoord=" + zCoord + ", tileEntityInvalid=" + tileEntityInvalid + "]"; } diff --git a/src/main/java/crazypants/enderio/machine/wireless/WirelessChargedLocation.java b/src/main/java/crazypants/enderio/machine/wireless/WirelessChargedLocation.java index ef371b4e..0f0ff33b 100644 --- a/src/main/java/crazypants/enderio/machine/wireless/WirelessChargedLocation.java +++ b/src/main/java/crazypants/enderio/machine/wireless/WirelessChargedLocation.java @@ -7,6 +7,8 @@ import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import crazypants.util.BlockCoord; +import java.util.Collections; +import java.util.Comparator; public class WirelessChargedLocation { @@ -24,13 +26,26 @@ public class WirelessChargedLocation { WirelessChargerController wcc = WirelessChargerController.instance; chargers.clear(); lastChangeCount = wcc.getChangeCount(); - wcc.getChargers(te.getWorldObj(), new BlockCoord(te), chargers); + final BlockCoord bc = new BlockCoord(te); + wcc.getChargers(te.getWorldObj(), bc, chargers); + Collections.sort(chargers, new Comparator() { + @Override + public int compare(IWirelessCharger o1, IWirelessCharger o2) { + int dist1 = o1.getLocation().distanceSquared(bc); + int dist2 = o2.getLocation().distanceSquared(bc); + return dist1 - dist2; + } + }); } - public boolean chargeItems(ItemStack[] items) { + private void checkChangeCount() { if(lastChangeCount != WirelessChargerController.instance.getChangeCount()) { updateChargers(); } + } + + public boolean chargeItems(ItemStack[] items) { + checkChangeCount(); for(IWirelessCharger wc : chargers) { if(wc.isActive()) { if(wc.chargeItems(items)) { @@ -40,4 +55,18 @@ public class WirelessChargedLocation { } return false; } + + public int takeEnergy(int max) { + checkChangeCount(); + int charged = 0; + for(IWirelessCharger wc : chargers) { + if(wc.isActive()) { + charged += wc.takeEnergy(max - charged); + if(charged >= max) { + break; + } + } + } + return charged; + } }