Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make the planter robot handle any IPlantable item/block and sugarcane, f... #2402

Merged
merged 2 commits into from
Jan 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 47 additions & 17 deletions common/buildcraft/core/robots/boards/BoardRobotPlanter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,25 @@
import java.util.Collection;

import net.minecraft.block.Block;
import net.minecraft.block.BlockDirt;
import net.minecraft.block.BlockGrass;
import net.minecraft.item.ItemSeeds;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemReed;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;

import net.minecraftforge.common.IPlantable;
import net.minecraftforge.common.util.ForgeDirection;

import buildcraft.api.boards.RedstoneBoardRobot;
import buildcraft.api.boards.RedstoneBoardRobotNBT;
import buildcraft.api.core.BlockIndex;
import buildcraft.api.core.BuildCraftAPI;
import buildcraft.api.robots.AIRobot;
import buildcraft.api.robots.EntityRobotBase;
import buildcraft.core.inventory.filters.ArrayStackFilter;
import buildcraft.core.inventory.filters.ArrayStackOrListFilter;
import buildcraft.core.inventory.filters.CompositeFilter;
import buildcraft.core.inventory.filters.IStackFilter;
import buildcraft.core.inventory.filters.OreStackFilter;
import buildcraft.core.robots.AIRobotFetchAndEquipItemStack;
import buildcraft.core.robots.AIRobotGotoBlock;
import buildcraft.core.robots.AIRobotGotoRandomGroundBlock;
Expand All @@ -42,7 +43,7 @@

public class BoardRobotPlanter extends RedstoneBoardRobot {

private IStackFilter stackFilter = new CompositeFilter(new OreStackFilter("treeSapling"), new SeedFilter());
private IStackFilter stackFilter = new CompositeFilter(new PlantableFilter(), new ReedFilter());
private BlockIndex blockFound;

public BoardRobotPlanter(EntityRobotBase iRobot) {
Expand Down Expand Up @@ -81,25 +82,40 @@ public void update() {
startDelegateAI(new AIRobotFetchAndEquipItemStack(robot, stackFilter));
}
} else {
if (robot.getHeldItem().getItem() instanceof ItemSeeds) {
startDelegateAI(new AIRobotSearchBlock(robot, new IBlockFilter() {
final ItemStack itemStack = robot.getHeldItem();
IBlockFilter blockFilter;
if (itemStack.getItem() instanceof ItemReed) {
blockFilter = new IBlockFilter() {
@Override
public boolean matches(World world, int x, int y, int z) {
return world.getBlock(x, y, z).canSustainPlant(world, x, y, z, ForgeDirection.UP, (IPlantable) Blocks.reeds)
&& world.getBlock(x, y, z) != Blocks.reeds
&& !robot.getRegistry().isTaken(new ResourceIdBlock(x, y, z))
&& isAirAbove(world, x, y, z);
}
};
} else if (itemStack.getItem() instanceof ItemBlock) {
final Block plantBlock = ((ItemBlock) itemStack.getItem()).field_150939_a;
blockFilter = new IBlockFilter() {
@Override
public boolean matches(World world, int x, int y, int z) {
return BuildCraftAPI.isFarmlandProperty.get(world, x, y, z)
return world.getBlock(x, y, z).canSustainPlant(world, x, y, z, ForgeDirection.UP, (IPlantable) plantBlock)
&& world.getBlock(x, y, z) != plantBlock
&& !robot.getRegistry().isTaken(new ResourceIdBlock(x, y, z))
&& isAirAbove(world, x, y, z);
}
}));
};
} else {
startDelegateAI(new AIRobotGotoRandomGroundBlock(robot, 100, new IBlockFilter() {
blockFilter = new IBlockFilter() {
@Override
public boolean matches(World world, int x, int y, int z) {
Block b = robot.worldObj.getBlock(x, y, z);

return b instanceof BlockDirt || b instanceof BlockGrass;
return world.getBlock(x, y, z).canSustainPlant(world, x, y, z, ForgeDirection.UP, (IPlantable) itemStack.getItem())
&& !robot.getRegistry().isTaken(new ResourceIdBlock(x, y, z))
&& isAirAbove(world, x, y, z);
}
}, robot.getZoneToWork()));
};
}
startDelegateAI(new AIRobotSearchBlock(robot, blockFilter));
}
}

Expand All @@ -124,6 +140,7 @@ public void delegateAIEnded(AIRobot ai) {
}

blockFound = gotoBlock.blockFound;
gotoBlock.path.removeLast();
startDelegateAI(new AIRobotGotoBlock(robot, gotoBlock.path));
} else {
startDelegateAI(new AIRobotGotoSleep(robot));
Expand All @@ -137,10 +154,23 @@ public void delegateAIEnded(AIRobot ai) {
}
}

private static class SeedFilter implements IStackFilter {
private static class PlantableFilter implements IStackFilter {
@Override
public boolean matches(ItemStack stack) {
if (stack.getItem() instanceof IPlantable) {
return true;
}
if (stack.getItem() instanceof ItemBlock && ((ItemBlock) stack.getItem()).field_150939_a instanceof IPlantable) {
return true;
}
return false;
}
}

private static class ReedFilter implements IStackFilter {
@Override
public boolean matches(ItemStack stack) {
return stack.getItem() instanceof ItemSeeds;
return stack.getItem() instanceof ItemReed;
}
}

Expand Down
23 changes: 13 additions & 10 deletions common/buildcraft/core/utils/PathFinding.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,19 @@ public void preRun() {

private boolean searchForTarget(int range) {
for (int dx = -range; dx <= range; dx++) {
for (int dy = -range; dy <= range; dy++) {
for (int dz = -range; dz <= range; dz++) {
int x = start.x + dx;
int y = Math.max(0, start.y + dy);
int z = start.z + dz;
if (zone != null && !zone.contains(x, y, z)) {
continue;
}
if (pathFound.matches(world, x, y, z)) {
return false;
for (int dz = -range; dz <= range; dz++) {
int x = start.x + dx;
int z = start.z + dz;
if (world.getChunkProvider().chunkExists (x >> 4, z >> 4)) {
int height = world.getChunkFromChunkCoords(x >> 4, z >> 4).getHeightValue(x & 0xF, z & 0xF);
for (int dy = -range; dy <= height; dy++) {
int y = Math.max(0, start.y + dy);
if (zone != null && !zone.contains(x, y, z)) {
continue;
}
if (pathFound.matches(world, x, y, z)) {
return false;
}
}
}
}
Expand Down