Search in sources :

Example 1 with SemiBlockLogistics

use of pneumaticCraft.common.semiblock.SemiBlockLogistics in project PneumaticCraft by MineMaarten.

the class LogisticsManager method getTasks.

public PriorityQueue<LogisticsTask> getTasks(Object holdingStack) {
    ItemStack item = holdingStack instanceof ItemStack ? (ItemStack) holdingStack : null;
    FluidStack fluid = holdingStack instanceof FluidStack ? (FluidStack) holdingStack : null;
    PriorityQueue<LogisticsTask> tasks = new PriorityQueue<LogisticsTask>();
    for (int priority = logistics.length - 1; priority >= 0; priority--) {
        for (SemiBlockLogistics requester : logistics[priority]) {
            for (int i = 0; i < priority; i++) {
                for (SemiBlockLogistics provider : logistics[i]) {
                    if (provider.shouldProvideTo(priority)) {
                        if (item != null) {
                            int requestedAmount = getRequestedAmount(requester, item);
                            if (requestedAmount > 0) {
                                ItemStack stack = item.copy();
                                stack.stackSize = requestedAmount;
                                tasks.add(new LogisticsTask(provider, requester, stack));
                                return tasks;
                            }
                        } else if (fluid != null) {
                            int requestedAmount = getRequestedAmount(requester, fluid);
                            if (requestedAmount > 0) {
                                fluid = fluid.copy();
                                fluid.amount = requestedAmount;
                                tasks.add(new LogisticsTask(provider, requester, new FluidStackWrapper(fluid)));
                                return tasks;
                            }
                        } else {
                            tryProvide(provider, requester, tasks);
                        }
                    }
                }
            }
        }
    }
    return tasks;
}
Also used : SemiBlockLogistics(pneumaticCraft.common.semiblock.SemiBlockLogistics) FluidStack(net.minecraftforge.fluids.FluidStack) FluidStackWrapper(pneumaticCraft.common.semiblock.SemiBlockLogistics.FluidStackWrapper) ItemStack(net.minecraft.item.ItemStack) PriorityQueue(java.util.PriorityQueue)

Example 2 with SemiBlockLogistics

use of pneumaticCraft.common.semiblock.SemiBlockLogistics in project PneumaticCraft by MineMaarten.

the class DroneAILogistics method shouldExecute.

@Override
public boolean shouldExecute() {
    manager.clearLogistics();
    Set<ChunkPosition> area = widget.getCachedAreaSet();
    if (area.size() == 0)
        return false;
    int minX = Integer.MAX_VALUE, maxX = Integer.MIN_VALUE, minZ = Integer.MAX_VALUE, maxZ = Integer.MIN_VALUE;
    for (ChunkPosition pos : area) {
        minX = Math.min(minX, pos.chunkPosX);
        maxX = Math.max(maxX, pos.chunkPosX);
        minZ = Math.min(minZ, pos.chunkPosZ);
        maxZ = Math.max(maxZ, pos.chunkPosZ);
    }
    for (int x = minX; x < maxX + 16; x += 16) {
        for (int z = minZ; z < maxZ + 16; z += 16) {
            Chunk chunk = drone.getWorld().getChunkFromBlockCoords(x, z);
            Map<ChunkPosition, ISemiBlock> map = SemiBlockManager.getInstance(drone.getWorld()).getSemiBlocks().get(chunk);
            if (map != null) {
                for (Map.Entry<ChunkPosition, ISemiBlock> entry : map.entrySet()) {
                    if (entry.getValue() instanceof SemiBlockLogistics && area.contains(entry.getKey())) {
                        SemiBlockLogistics logisticsBlock = (SemiBlockLogistics) entry.getValue();
                        manager.addLogisticFrame(logisticsBlock);
                    }
                }
            }
        }
    }
    curTask = null;
    return doLogistics();
}
Also used : SemiBlockLogistics(pneumaticCraft.common.semiblock.SemiBlockLogistics) ISemiBlock(pneumaticCraft.common.semiblock.ISemiBlock) ChunkPosition(net.minecraft.world.ChunkPosition) Chunk(net.minecraft.world.chunk.Chunk) Map(java.util.Map)

Example 3 with SemiBlockLogistics

use of pneumaticCraft.common.semiblock.SemiBlockLogistics in project PneumaticCraft by MineMaarten.

the class ContainerLogistics method getLogistics.

public static SemiBlockLogistics getLogistics(EntityPlayer player, ItemStack itemRequester) {
    if (itemRequester != null && itemRequester.getItem() instanceof ItemLogisticsFrame) {
        SemiBlockLogistics logistics = (SemiBlockLogistics) SemiBlockManager.getSemiBlockForKey(((ItemLogisticsFrame) itemRequester.getItem()).semiBlockId);
        logistics.initialize(player.worldObj, new ChunkPosition(0, 0, 0));
        logistics.onPlaced(player, itemRequester);
        return logistics;
    } else {
        return null;
    }
}
Also used : SemiBlockLogistics(pneumaticCraft.common.semiblock.SemiBlockLogistics) ItemLogisticsFrame(pneumaticCraft.common.item.ItemLogisticsFrame) ChunkPosition(net.minecraft.world.ChunkPosition)

Example 4 with SemiBlockLogistics

use of pneumaticCraft.common.semiblock.SemiBlockLogistics 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)

Aggregations

SemiBlockLogistics (pneumaticCraft.common.semiblock.SemiBlockLogistics)4 ItemStack (net.minecraft.item.ItemStack)2 ChunkPosition (net.minecraft.world.ChunkPosition)2 FluidStack (net.minecraftforge.fluids.FluidStack)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 PriorityQueue (java.util.PriorityQueue)1 TileEntity (net.minecraft.tileentity.TileEntity)1 Chunk (net.minecraft.world.chunk.Chunk)1 ForgeDirection (net.minecraftforge.common.util.ForgeDirection)1 IFluidHandler (net.minecraftforge.fluids.IFluidHandler)1 LogisticsManager (pneumaticCraft.common.ai.LogisticsManager)1 LogisticsTask (pneumaticCraft.common.ai.LogisticsManager.LogisticsTask)1 ItemLogisticsFrame (pneumaticCraft.common.item.ItemLogisticsFrame)1 PacketUpdateLogisticModule (pneumaticCraft.common.network.PacketUpdateLogisticModule)1 ISemiBlock (pneumaticCraft.common.semiblock.ISemiBlock)1 FluidStackWrapper (pneumaticCraft.common.semiblock.SemiBlockLogistics.FluidStackWrapper)1