Fluid and power working with new trans.

This commit is contained in:
CrazyPants committed 2014-10-11 14:17:55 +08:00
1 parent 4538116d75
commit ebed67e63b
4 files changed
+250 -53

No files matched your search

@@ -81,9 +81,9 @@ public final class Config {
public static double transceiverEnergyLoss = 0.1;
public static int transceiverUpkeepCostRF = 2;
public static int transceiverUpkeepCostRF = 10;
public static int transceiverBucketTransmissionCostRF = 10;
public static int transceiverBucketTransmissionCostRF = 100;
public static int transceiverMaxIoRF = 20480;
@@ -4,16 +4,22 @@ import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import crazypants.enderio.Log;
import crazypants.enderio.config.Config;
import crazypants.util.RoundRobinIterator;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;
public class ServerChannelRegister extends ChannelRegister {
@@ -82,38 +88,101 @@ public class ServerChannelRegister extends ChannelRegister {
return new File(DimensionManager.getCurrentSaveRootDirectory(), "enderio/dimensionalTransceiver.json");
}
private List<TileTransceiver> transceivers = new ArrayList<TileTransceiver>();
public void register(TileTransceiver transceiver) {
transceivers.add(transceiver);
//-----------------------------------------------------------------------------------------------
private final List<TileTransceiver> transceivers = new ArrayList<TileTransceiver>();
private Map<Channel, RoundRobinIterator<TileTransceiver>> iterators = new HashMap<Channel, RoundRobinIterator<TileTransceiver>>();
private ServerChannelRegister() {
}
public void register(TileTransceiver transceiver) {
transceivers.add(transceiver);
}
public void dergister(TileTransceiver transceiver) {
transceivers.remove(transceiver);
transceivers.remove(transceiver);
}
@Override
public void removeChannel(Channel channel) {
public void removeChannel(Channel channel) {
super.removeChannel(channel);
for(TileTransceiver trans : transceivers) {
for (TileTransceiver trans : transceivers) {
trans.removeRecieveChanel(channel);
trans.removeSendChanel(channel);
}
iterators.remove(channel);
}
//Power
public void sendPower(TileTransceiver sender, int canSend, Channel channel) {
//TODO: Round robin somehow
for(TileTransceiver trans : transceivers) {
RoundRobinIterator<TileTransceiver> iter = getIterator(channel);
for (TileTransceiver trans : iter) {
if(trans != sender && trans.getRecieveChannels(ChannelType.POWER).contains(channel)) {
double invLoss = 1 - Config.transceiverEnergyLoss;
int canSendWithLoss = (int)Math.round(canSend * invLoss);
int canSendWithLoss = (int) Math.round(canSend * invLoss);
int recieved = trans.receiveEnergy(ForgeDirection.UNKNOWN, canSendWithLoss, false);
if(recieved > 0) {
int recievedPlusLoss = (int)Math.round(recieved / invLoss);
int recievedPlusLoss = (int) Math.round(recieved / invLoss);
sender.usePower(recievedPlusLoss);
}
}
}
}
}
private RoundRobinIterator<TileTransceiver> getIterator(Channel channel) {
RoundRobinIterator<TileTransceiver> res = iterators.get(channel);
if(res == null) {
res = new RoundRobinIterator<TileTransceiver>(transceivers);
iterators.put(channel, res);
}
return res;
}
//Fluid
public FluidTankInfo[] getTankInfoForChannels(TileTransceiver tileTransceiver, List<Channel> channels) {
List<FluidTankInfo> infos = new ArrayList<FluidTankInfo>();
for (TileTransceiver tran : transceivers) {
if(tran != tileTransceiver) {
tran.getRecieveTankInfo(infos, channels);
}
}
return infos.toArray(new FluidTankInfo[infos.size()]);
}
public boolean canFill(TileTransceiver tileTransceiver, List<Channel> channels, Fluid fluid) {
for (TileTransceiver tran : transceivers) {
if(tran != tileTransceiver) {
if(tran.canReceive(channels, fluid)) {
return true;
}
}
}
return false;
}
public int fill(TileTransceiver from, List<Channel> list, FluidStack resource, boolean doFill) {
if(resource == null || !from.hasPower()) {
return 0;
}
for(Channel channel : list) {
RoundRobinIterator<TileTransceiver> iter = getIterator(channel);
for(TileTransceiver trans : iter) {
if(trans != from) {
int val = trans.recieveFluid(list, resource, doFill);
if(val > 0) {
if(doFill && Config.transceiverBucketTransmissionCostRF > 0) {
int powerUsed = (int)Math.max(1, Config.transceiverBucketTransmissionCostRF * val/1000d);
from.usePower(powerUsed);
}
return val;
}
}
}
}
return 0;
}
}
@@ -3,12 +3,21 @@ package crazypants.enderio.machine.transceiver;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import net.minecraft.block.Block;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;
import crazypants.enderio.ModObject;
import crazypants.enderio.config.Config;
import crazypants.enderio.machine.AbstractPoweredTaskEntity;
@@ -22,13 +31,14 @@ import crazypants.enderio.power.BasicCapacitor;
import crazypants.enderio.power.Capacitors;
import crazypants.enderio.power.ICapacitor;
import crazypants.enderio.power.PowerDistributor;
import crazypants.util.FluidUtil;
import crazypants.vecmath.VecmathUtil;
public class TileTransceiver extends AbstractPoweredTaskEntity {
public class TileTransceiver extends AbstractPoweredTaskEntity implements IFluidHandler {
//Power will only be sent to other transceivers is the buffer is higher than this amount
private static final float MIN_POWER_TO_SEND = 0.5f;
private final EnumMap<ChannelType, List<Channel>> sendChannels = new EnumMap<ChannelType, List<Channel>>(ChannelType.class);
private final EnumMap<ChannelType, List<Channel>> recieveChannels = new EnumMap<ChannelType, List<Channel>>(ChannelType.class);
@@ -37,25 +47,27 @@ public class TileTransceiver extends AbstractPoweredTaskEntity {
private boolean recieveChannelsDirty = false;
private boolean registered = false;
private Map<ForgeDirection, IFluidHandler> neighbourFluidHandlers = null;
private PowerDistributor powerDistributor;
public TileTransceiver() {
super(new SlotDefinition(0, 0, 0));
for (ChannelType type : ChannelType.values()) {
sendChannels.put(type, new ArrayList<Channel>());
recieveChannels.put(type, new ArrayList<Channel>());
}
currentTask = new ContinuousTask(Config.transceiverUpkeepCostRF);
currentTask = new ContinuousTask(Config.transceiverUpkeepCostRF);
}
@Override
protected boolean processTasks(boolean redstoneChecksPassed) {
boolean res = super.processTasks(redstoneChecksPassed);
if(!redstoneChecksPassed) {
if(!redstoneChecksPassed) {
return res;
}
processPower();
}
processPower();
return res;
}
@@ -80,7 +92,7 @@ public class TileTransceiver extends AbstractPoweredTaskEntity {
}
}
}
@Override
public void invalidate() {
super.invalidate();
@@ -112,15 +124,15 @@ public class TileTransceiver extends AbstractPoweredTaskEntity {
removeChannel(chan, channels);
}
}
@Override
public String getMachineName() {
return ModObject.blockTransceiver.unlocalisedName;
}
@Override
public boolean isActive() {
return hasPower();
return hasPower();
}
@Override
@@ -132,7 +144,7 @@ public class TileTransceiver extends AbstractPoweredTaskEntity {
public ICapacitor getCapacitor() {
return capacitor;
}
public int getPowerUsePerTick() {
return Config.transceiverUpkeepCostRF;
}
@@ -198,13 +210,13 @@ public class TileTransceiver extends AbstractPoweredTaskEntity {
readChannels(nbtRoot, sendChannels, "sendChannels");
readChannels(nbtRoot, recieveChannels, "recieveChannels");
currentTask = new ContinuousTask(Config.transceiverUpkeepCostRF);
}
static void readChannels(NBTTagCompound nbtRoot, EnumMap<ChannelType, List<Channel>> readInto, String key) {
for (ChannelType type : ChannelType.values()) {
readInto.get(type).clear();
}
@@ -248,38 +260,38 @@ public class TileTransceiver extends AbstractPoweredTaskEntity {
}
void setSendChannels(EnumMap<ChannelType, List<Channel>> channels) {
for(ChannelType type : ChannelType.values()) {
for (ChannelType type : ChannelType.values()) {
sendChannels.get(type).clear();
sendChannels.get(type).addAll(channels.get(type));
}
}
}
void setRecieveChannels(EnumMap<ChannelType, List<Channel>> channels) {
for(ChannelType type : ChannelType.values()) {
for (ChannelType type : ChannelType.values()) {
recieveChannels.get(type).clear();
recieveChannels.get(type).addAll(channels.get(type));
}
}
EnumMap<ChannelType, List<Channel>> getSendChannels() {
EnumMap<ChannelType, List<Channel>> getSendChannels() {
return sendChannels;
}
EnumMap<ChannelType, List<Channel>> getReceiveChannels() {
EnumMap<ChannelType, List<Channel>> getReceiveChannels() {
return recieveChannels;
}
//Power Handling
//---------------- Power Handling
private void processPower() {
List<Channel> sendTo = getSendChannels(ChannelType.POWER);
List<Channel> sendTo = getSendChannels(ChannelType.POWER);
int canSend = getMaxSendableEnergy();
if(canSend > 0 && !sendTo.isEmpty()) {
for(int i=0;i<sendTo.size() && canSend > 0;i++) {
if(canSend > 0 && !sendTo.isEmpty()) {
for (int i = 0; i < sendTo.size() && canSend > 0; i++) {
ServerChannelRegister.instance.sendPower(this, canSend, sendTo.get(i));
canSend = getMaxSendableEnergy();
}
}
}
canSend = getMaxSendableEnergy();
if(canSend > 0 && !getRecieveChannels(ChannelType.POWER).isEmpty()) {
if(powerDistributor == null) {
@@ -291,19 +303,119 @@ public class TileTransceiver extends AbstractPoweredTaskEntity {
}
private int getMaxSendableEnergy() {
return getEnergyStored() - (int)(MIN_POWER_TO_SEND * getMaxEnergyStored());
return getEnergyStored() - (int) (MIN_POWER_TO_SEND * getMaxEnergyStored());
}
private float getEnergyStoredRatio() {
return (float)getEnergyStored() / getMaxEnergyStored();
return (float) getEnergyStored() / getMaxEnergyStored();
}
@Override
public void onNeighborBlockChange(Block blockId) {
public void onNeighborBlockChange(Block blockId) {
super.onNeighborBlockChange(blockId);
if(powerDistributor != null) {
powerDistributor.neighboursChanged();
}
neighbourFluidHandlers = null;
}
private boolean hasRecieveChannel(List<Channel> channels, ChannelType type) {
boolean hasChannel = false;
for (Channel chan : channels) {
if(getRecieveChannels(type).contains(chan)) {
hasChannel = true;
break;
}
}
return hasChannel;
}
//---------------- Fluid Handling
@Override
public boolean canFill(ForgeDirection from, Fluid fluid) {
if(getSendChannels(ChannelType.FLUID).isEmpty()) {
return false;
}
return ServerChannelRegister.instance.canFill(this, getSendChannels(ChannelType.FLUID), fluid);
}
public boolean canReceive(List<Channel> channels, Fluid fluid) {
if(!hasRecieveChannel(channels, ChannelType.FLUID)) {
return false;
}
Map<ForgeDirection, IFluidHandler> handlers = getNeighbouringFluidHandlers();
for (Entry<ForgeDirection, IFluidHandler> entry : handlers.entrySet()) {
if(entry.getValue().canFill(entry.getKey().getOpposite(), fluid)) {
return true;
}
}
return false;
}
@Override
public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
if(getSendChannels(ChannelType.FLUID).isEmpty()) {
return 0;
}
return ServerChannelRegister.instance.fill(this, getSendChannels(ChannelType.FLUID), resource, doFill);
}
public int recieveFluid(List<Channel> channels, FluidStack resource, boolean doFill) {
if(!hasRecieveChannel(channels, ChannelType.FLUID)) {
return 0;
}
Map<ForgeDirection, IFluidHandler> handlers = getNeighbouringFluidHandlers();
for (Entry<ForgeDirection, IFluidHandler> entry : handlers.entrySet()) {
int res = entry.getValue().fill(entry.getKey().getOpposite(), resource, doFill);
if(res > 0) {
return res;
}
}
return 0;
}
@Override
public FluidTankInfo[] getTankInfo(ForgeDirection from) {
return ServerChannelRegister.instance.getTankInfoForChannels(this, getSendChannels(ChannelType.FLUID));
}
public void getRecieveTankInfo(List<FluidTankInfo> infos, List<Channel> channels) {
if(!hasRecieveChannel(channels, ChannelType.FLUID)) {
return;
}
Map<ForgeDirection, IFluidHandler> fluidHandlers = getNeighbouringFluidHandlers();
for (Entry<ForgeDirection, IFluidHandler> entry : fluidHandlers.entrySet()) {
FluidTankInfo[] tanks = entry.getValue().getTankInfo(entry.getKey().getOpposite());
if(tanks != null) {
for (FluidTankInfo info : tanks) {
infos.add(info);
}
}
}
}
Map<ForgeDirection, IFluidHandler> getNeighbouringFluidHandlers() {
if(neighbourFluidHandlers == null) {
neighbourFluidHandlers = FluidUtil.getNeighbouringFluidHandlers(worldObj, getLocation());
}
return neighbourFluidHandlers;
}
//Pulling liquids not supported
@Override
public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain) {
return null;
}
@Override
public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain) {
return null;
}
@Override
public boolean canDrain(ForgeDirection from, Fluid fluid) {
return false;
}
}
@@ -1,10 +1,16 @@
package crazypants.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.minecraft.init.Items;
import net.minecraft.item.ItemPotion;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.IBlockAccess;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
@@ -15,6 +21,16 @@ import crazypants.enderio.conduit.IConduitBundle;
public class FluidUtil {
public static Map<ForgeDirection, IFluidHandler> getNeighbouringFluidHandlers(IBlockAccess world, BlockCoord bc) {
Map<ForgeDirection, IFluidHandler> res = new HashMap<ForgeDirection, IFluidHandler>();
for(ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
IFluidHandler fh = getFluidHandler(world, bc.getLocation(dir));
if(fh != null) {
res.put(dir, fh);
}
}
return res;
}
public static IFluidHandler getExternalFluidHandler(IBlockAccess world, BlockCoord bc) {
IFluidHandler con = getFluidHandler(world, bc);