Search in sources :

Example 1 with FarmTarget

use of forestry.farming.FarmTarget in project ForestryMC by ForestryMC.

the class FarmController method cultivateTargets.

private FarmWorkStatus cultivateTargets(FarmWorkStatus farmWorkStatus, List<FarmTarget> farmTargets, IFarmLogic logic, FarmDirection farmSide) {
    boolean hasFarmland = false;
    for (FarmTarget target : farmTargets) {
        if (target.getExtent() > 0) {
            hasFarmland = true;
            farmWorkStatus.hasFarmland = true;
            break;
        }
    }
    if (hasFarmland && !FarmHelper.isCycleCanceledByListeners(logic, farmSide, farmListeners)) {
        final float hydrationModifier = hydrationManager.getHydrationModifier();
        final int fertilizerConsumption = Math.round(logic.getFertilizerConsumption() * Config.fertilizerModifier);
        final int liquidConsumption = logic.getWaterConsumption(hydrationModifier);
        final FluidStack liquid = new FluidStack(FluidRegistry.WATER, liquidConsumption);
        for (FarmTarget target : farmTargets) {
            // Check fertilizer and water
            if (!fertilizerManager.hasFertilizer(inventory, fertilizerConsumption)) {
                farmWorkStatus.hasFertilizer = false;
                continue;
            }
            if (liquid.amount > 0 && !hasLiquid(liquid)) {
                farmWorkStatus.hasLiquid = false;
                continue;
            }
            if (FarmHelper.cultivateTarget(world, this, target, logic, farmListeners)) {
                // Remove fertilizer and water
                fertilizerManager.removeFertilizer(inventory, fertilizerConsumption);
                removeLiquid(liquid);
                farmWorkStatus.didWork = true;
            }
        }
    }
    return farmWorkStatus;
}
Also used : FarmTarget(forestry.farming.FarmTarget) FluidStack(net.minecraftforge.fluids.FluidStack)

Example 2 with FarmTarget

use of forestry.farming.FarmTarget in project ForestryMC by ForestryMC.

the class FarmController method doWork.

@Override
public boolean doWork() {
    farmWorkTicks++;
    if (targets.isEmpty() || farmWorkTicks % 20 == 0) {
        setUpFarmlandTargets();
    }
    IErrorLogic errorLogic = getErrorLogic();
    if (!pendingProduce.isEmpty()) {
        boolean added = inventory.tryAddPendingProduce(pendingProduce);
        errorLogic.setCondition(!added, EnumErrorCode.NO_SPACE_INVENTORY);
        return added;
    }
    boolean hasFertilizer = fertilizerManager.maintainFertilizer(inventory);
    if (errorLogic.setCondition(!hasFertilizer, EnumErrorCode.NO_FERTILIZER)) {
        return false;
    }
    // Cull queued crops.
    if (!pendingCrops.isEmpty() && harvestProvider != null) {
        ICrop first = pendingCrops.get(0);
        if (cullCrop(first, harvestProvider)) {
            pendingCrops.remove(0);
            return true;
        } else {
            return false;
        }
    }
    // Cultivation and collection
    FarmWorkStatus farmWorkStatus = new FarmWorkStatus();
    List<FarmDirection> farmDirections = Arrays.asList(FarmDirection.values());
    Collections.shuffle(farmDirections, world.rand);
    for (FarmDirection farmSide : farmDirections) {
        IFarmLogic logic = getFarmLogic(farmSide);
        // Always try to collect windfall.
        if (collectWindfall(logic)) {
            farmWorkStatus.didWork = true;
        }
        List<FarmTarget> farmTargets = targets.get(farmSide);
        if (stage == Stage.HARVEST) {
            Collection<ICrop> harvested = FarmHelper.harvestTargets(world, farmTargets, logic, farmListeners);
            farmWorkStatus.didWork = !harvested.isEmpty();
            if (!harvested.isEmpty()) {
                pendingCrops.addAll(harvested);
                pendingCrops.sort(FarmHelper.TopDownICropComparator.INSTANCE);
                harvestProvider = logic;
            }
        } else if (stage == Stage.CULTIVATE) {
            farmWorkStatus = cultivateTargets(farmWorkStatus, farmTargets, logic, farmSide);
        }
        if (farmWorkStatus.didWork) {
            break;
        }
    }
    if (stage == Stage.CULTIVATE) {
        errorLogic.setCondition(!farmWorkStatus.hasFarmland, EnumErrorCode.NO_FARMLAND);
        errorLogic.setCondition(!farmWorkStatus.hasFertilizer, EnumErrorCode.NO_FERTILIZER);
        errorLogic.setCondition(!farmWorkStatus.hasLiquid, EnumErrorCode.NO_LIQUID_FARM);
    }
    // alternate between cultivation and harvest.
    stage = stage.next();
    return farmWorkStatus.didWork;
}
Also used : FarmWorkStatus(forestry.farming.FarmHelper.FarmWorkStatus) FarmTarget(forestry.farming.FarmTarget) FarmDirection(forestry.api.farming.FarmDirection) IFarmLogic(forestry.api.farming.IFarmLogic) ICrop(forestry.api.farming.ICrop) IErrorLogic(forestry.api.core.IErrorLogic)

Example 3 with FarmTarget

use of forestry.farming.FarmTarget in project ForestryMC by ForestryMC.

the class TilePlanter method cultivateTargets.

private FarmWorkStatus cultivateTargets(FarmWorkStatus farmWorkStatus, List<FarmTarget> farmTargets, FarmDirection farmSide) {
    boolean hasFarmland = false;
    for (FarmTarget target : farmTargets) {
        if (target.getExtent() > 0) {
            hasFarmland = true;
            farmWorkStatus.hasFarmland = true;
            break;
        }
    }
    if (hasFarmland && !FarmHelper.isCycleCanceledByListeners(logic, farmSide, Collections.emptySet())) {
        final float hydrationModifier = hydrationManager.getHydrationModifier();
        final int fertilizerConsumption = Math.round(logic.getFertilizerConsumption() * Config.fertilizerModifier * 2);
        final int liquidConsumption = logic.getWaterConsumption(hydrationModifier);
        final FluidStack liquid = new FluidStack(FluidRegistry.WATER, liquidConsumption);
        for (FarmTarget target : farmTargets) {
            // Check fertilizer and water
            if (!fertilizerManager.hasFertilizer(inventory, fertilizerConsumption)) {
                farmWorkStatus.hasFertilizer = false;
                continue;
            }
            if (liquid.amount > 0 && !hasLiquid(liquid)) {
                farmWorkStatus.hasLiquid = false;
                continue;
            }
            if (FarmHelper.cultivateTarget(world, this, target, logic, Collections.emptySet())) {
                // Remove fertilizer and water
                fertilizerManager.removeFertilizer(inventory, fertilizerConsumption);
                removeLiquid(liquid);
                farmWorkStatus.didWork = true;
            }
        }
    }
    return farmWorkStatus;
}
Also used : FarmTarget(forestry.farming.FarmTarget) FluidStack(net.minecraftforge.fluids.FluidStack)

Example 4 with FarmTarget

use of forestry.farming.FarmTarget in project ForestryMC by ForestryMC.

the class TilePlanter method workCycle.

@Override
protected boolean workCycle() {
    if (targets.isEmpty() || updateOnInterval(10)) {
        setUpFarmlandTargets();
    }
    IErrorLogic errorLogic = getErrorLogic();
    boolean hasFertilizer = fertilizerManager.maintainFertilizer(inventory);
    if (errorLogic.setCondition(!hasFertilizer, EnumErrorCode.NO_FERTILIZER)) {
        return false;
    }
    if (!pendingProduce.isEmpty()) {
        boolean added = inventory.tryAddPendingProduce(pendingProduce);
        errorLogic.setCondition(!added, EnumErrorCode.NO_SPACE_INVENTORY);
        return added;
    }
    // Cull queued crops.
    if (!pendingCrops.isEmpty()) {
        ICrop first = pendingCrops.get(0);
        if (cullCrop(first)) {
            pendingCrops.remove(0);
            return true;
        } else {
            return false;
        }
    }
    // Cultivation and collection
    FarmWorkStatus farmWorkStatus = new FarmWorkStatus();
    List<FarmDirection> farmDirections = Arrays.asList(FarmDirection.values());
    Collections.shuffle(farmDirections, world.rand);
    for (FarmDirection farmSide : farmDirections) {
        IFarmLogic logic = getFarmLogic(farmSide);
        // Always try to collect windfall.
        if (collectWindfall()) {
            farmWorkStatus.didWork = true;
        }
        List<FarmTarget> farmTargets = targets.get(farmSide);
        if (stage == Stage.HARVEST) {
            Collection<ICrop> harvested = FarmHelper.harvestTargets(world, farmTargets, logic, Collections.emptySet());
            farmWorkStatus.didWork = !harvested.isEmpty();
            if (!harvested.isEmpty()) {
                pendingCrops.addAll(harvested);
                pendingCrops.sort(FarmHelper.TopDownICropComparator.INSTANCE);
            }
        } else if (stage == Stage.CULTIVATE) {
            farmWorkStatus = cultivateTargets(farmWorkStatus, farmTargets, farmSide);
        }
        if (farmWorkStatus.didWork) {
            break;
        }
    }
    if (stage == Stage.CULTIVATE) {
        errorLogic.setCondition(!farmWorkStatus.hasFarmland, EnumErrorCode.NO_FARMLAND);
        errorLogic.setCondition(!farmWorkStatus.hasFertilizer, EnumErrorCode.NO_FERTILIZER);
        errorLogic.setCondition(!farmWorkStatus.hasLiquid, EnumErrorCode.NO_LIQUID_FARM);
    }
    // alternate between cultivation and harvest.
    stage = stage.next();
    return false;
}
Also used : FarmWorkStatus(forestry.farming.FarmHelper.FarmWorkStatus) FarmTarget(forestry.farming.FarmTarget) FarmDirection(forestry.api.farming.FarmDirection) IFarmLogic(forestry.api.farming.IFarmLogic) ICrop(forestry.api.farming.ICrop) IErrorLogic(forestry.api.core.IErrorLogic)

Aggregations

FarmTarget (forestry.farming.FarmTarget)4 IErrorLogic (forestry.api.core.IErrorLogic)2 FarmDirection (forestry.api.farming.FarmDirection)2 ICrop (forestry.api.farming.ICrop)2 IFarmLogic (forestry.api.farming.IFarmLogic)2 FarmWorkStatus (forestry.farming.FarmHelper.FarmWorkStatus)2 FluidStack (net.minecraftforge.fluids.FluidStack)2