Search in sources :

Example 1 with PacketUpdateLogisticModule

use of pneumaticCraft.common.network.PacketUpdateLogisticModule in project PneumaticCraft by MineMaarten.

the class ModuleLogistics method update.

@Override
public void update() {
    super.update();
    if (cachedFrame != null && cachedFrame.isInvalid())
        cachedFrame = null;
    if (!getTube().world().isRemote) {
        if (powered != getTube().getAirHandler().getPressure(null) >= MIN_PRESSURE) {
            powered = !powered;
            NetworkHandler.sendToAllAround(new PacketUpdateLogisticModule(this, 0), getTube().world());
        }
        if (--ticksUntilNextCycle <= 0) {
            LogisticsManager manager = new LogisticsManager();
            Map<SemiBlockLogistics, ModuleLogistics> frameToModuleMap = new HashMap<SemiBlockLogistics, ModuleLogistics>();
            for (TubeModule module : ModuleNetworkManager.getInstance().getConnectedModules(this)) {
                if (module instanceof ModuleLogistics) {
                    ModuleLogistics logistics = (ModuleLogistics) module;
                    if (logistics.getColorChannel() == getColorChannel()) {
                        //Make sure any connected module doesn't tick, set it to a 5 second timer. This is also a penalty value when no task is executed this tick.
                        logistics.ticksUntilNextCycle = 100;
                        if (logistics.hasPower() && logistics.getFrame() != null) {
                            frameToModuleMap.put(logistics.getFrame(), logistics);
                            manager.addLogisticFrame(logistics.getFrame());
                        }
                    }
                }
            }
            PriorityQueue<LogisticsTask> tasks = manager.getTasks(null);
            for (LogisticsTask task : tasks) {
                if (task.isStillValid(task.transportingItem != null ? task.transportingItem : task.transportingFluid.stack)) {
                    if (task.transportingItem != null) {
                        ItemStack remainder = IOHelper.insert(task.requester.getTileEntity(), task.transportingItem.copy(), true);
                        if (remainder == null || remainder.stackSize != task.transportingItem.stackSize) {
                            ItemStack toBeExtracted = task.transportingItem.copy();
                            if (remainder != null)
                                toBeExtracted.stackSize -= remainder.stackSize;
                            ItemStack extractedStack = IOHelper.extract(task.provider.getTileEntity(), toBeExtracted, true);
                            if (extractedStack != null) {
                                ModuleLogistics provider = frameToModuleMap.get(task.provider);
                                ModuleLogistics requester = frameToModuleMap.get(task.requester);
                                int airUsed = (int) (ITEM_TRANSPORT_COST * extractedStack.stackSize * Math.pow(PneumaticCraftUtils.distBetweenSq(provider.getTube().x(), provider.getTube().y(), provider.getTube().z(), requester.getTube().x(), requester.getTube().y(), requester.getTube().z()), 0.25));
                                if (requester.getTube().getAirHandler().getCurrentAir(null) > airUsed) {
                                    sendModuleUpdate(provider, true);
                                    sendModuleUpdate(requester, true);
                                    requester.getTube().getAirHandler().addAir(-airUsed, null);
                                    IOHelper.extract(task.provider.getTileEntity(), extractedStack, false);
                                    IOHelper.insert(task.requester.getTileEntity(), extractedStack, false);
                                    ticksUntilNextCycle = 20;
                                } else {
                                    sendModuleUpdate(provider, false);
                                    sendModuleUpdate(requester, false);
                                }
                            }
                        }
                    } else {
                        TileEntity providingTE = task.provider.getTileEntity();
                        TileEntity requestingTE = task.requester.getTileEntity();
                        if (providingTE instanceof IFluidHandler && requestingTE instanceof IFluidHandler) {
                            IFluidHandler provider = (IFluidHandler) task.provider.getTileEntity();
                            IFluidHandler requester = (IFluidHandler) task.requester.getTileEntity();
                            for (ForgeDirection di : ForgeDirection.VALID_DIRECTIONS) {
                                int amountFilled = requester.fill(di, task.transportingFluid.stack, false);
                                if (amountFilled > 0) {
                                    FluidStack drainingFluid = task.transportingFluid.stack.copy();
                                    drainingFluid.amount = amountFilled;
                                    FluidStack extractedFluid = null;
                                    ModuleLogistics p = frameToModuleMap.get(task.provider);
                                    ModuleLogistics r = frameToModuleMap.get(task.requester);
                                    int airUsed = 0;
                                    for (ForgeDirection d : ForgeDirection.VALID_DIRECTIONS) {
                                        extractedFluid = provider.drain(d, drainingFluid, false);
                                        if (extractedFluid != null) {
                                            airUsed = (int) (FLUID_TRANSPORT_COST * extractedFluid.amount * PneumaticCraftUtils.distBetween(p.getTube().x(), p.getTube().y(), p.getTube().z(), r.getTube().x(), r.getTube().y(), r.getTube().z()));
                                            if (r.getTube().getAirHandler().getCurrentAir(null) > airUsed) {
                                                extractedFluid = provider.drain(d, drainingFluid, true);
                                                break;
                                            } else {
                                                sendModuleUpdate(p, false);
                                                sendModuleUpdate(r, false);
                                                extractedFluid = null;
                                                break;
                                            }
                                        }
                                    }
                                    if (extractedFluid != null) {
                                        sendModuleUpdate(p, true);
                                        sendModuleUpdate(r, true);
                                        r.getTube().getAirHandler().addAir(-airUsed, null);
                                        requester.fill(di, extractedFluid, true);
                                        ticksUntilNextCycle = 20;
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    } else {
        if (ticksSinceAction >= 0) {
            ticksSinceAction++;
            if (ticksSinceAction > 3)
                ticksSinceAction = -1;
        }
        if (ticksSinceNotEnoughAir >= 0) {
            ticksSinceNotEnoughAir++;
            if (ticksSinceNotEnoughAir > 20)
                ticksSinceNotEnoughAir = -1;
        }
    }
}
Also used : LogisticsManager(pneumaticCraft.common.ai.LogisticsManager) SemiBlockLogistics(pneumaticCraft.common.semiblock.SemiBlockLogistics) HashMap(java.util.HashMap) FluidStack(net.minecraftforge.fluids.FluidStack) LogisticsTask(pneumaticCraft.common.ai.LogisticsManager.LogisticsTask) IFluidHandler(net.minecraftforge.fluids.IFluidHandler) TileEntity(net.minecraft.tileentity.TileEntity) PacketUpdateLogisticModule(pneumaticCraft.common.network.PacketUpdateLogisticModule) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) ItemStack(net.minecraft.item.ItemStack)

Example 2 with PacketUpdateLogisticModule

use of pneumaticCraft.common.network.PacketUpdateLogisticModule in project PneumaticCraft by MineMaarten.

the class ModuleLogistics method onActivated.

@Override
public boolean onActivated(EntityPlayer player) {
    if (player.getCurrentEquippedItem() != null) {
        int colorIndex = TileEntityPlasticMixer.getDyeIndex(player.getCurrentEquippedItem());
        if (colorIndex >= 0) {
            if (!player.worldObj.isRemote) {
                colorChannel = colorIndex;
                NetworkHandler.sendToAllAround(new PacketUpdateLogisticModule(this, 0), getTube().world());
            }
            return true;
        }
    }
    return super.onActivated(player);
}
Also used : PacketUpdateLogisticModule(pneumaticCraft.common.network.PacketUpdateLogisticModule)

Aggregations

PacketUpdateLogisticModule (pneumaticCraft.common.network.PacketUpdateLogisticModule)2 HashMap (java.util.HashMap)1 ItemStack (net.minecraft.item.ItemStack)1 TileEntity (net.minecraft.tileentity.TileEntity)1 ForgeDirection (net.minecraftforge.common.util.ForgeDirection)1 FluidStack (net.minecraftforge.fluids.FluidStack)1 IFluidHandler (net.minecraftforge.fluids.IFluidHandler)1 LogisticsManager (pneumaticCraft.common.ai.LogisticsManager)1 LogisticsTask (pneumaticCraft.common.ai.LogisticsManager.LogisticsTask)1 SemiBlockLogistics (pneumaticCraft.common.semiblock.SemiBlockLogistics)1