// System.out.println(String.format("Master at %s/%s/%s:%s", xCoord, yCoord, zCoord, groundY));
ArrayList<FarmTarget> potential = new ArrayList<FarmTarget>();
int xDistance = 0;
int zDistance = 0;
Vect candidate = new Vect(xCoord, groundY, zCoord);
// Determine distance from master TE
while (true) {
xDistance += direction.offsetX;
zDistance += direction.offsetZ;
// System.out.println(String.format("New offset for %s: x:%s z:%s", direction, xDistance, zDistance));
// System.out.println(String.format("Validating for %s: x:%s y:%s z:%s", direction, xCoord + xDistance, groundY, zCoord + zDistance));
TileEntity tile = worldObj.getTileEntity(xCoord + xDistance, groundY, zCoord + zDistance);
if (tile == null)
// System.out.println("NUll TE");
break;
if (!(tile instanceof IFarmComponent))
// System.out.println("Not instaceof of farm component");
break;
candidate = new Vect(xCoord + xDistance, groundY, zCoord + zDistance);
}
// System.out.println(String.format("Determined distance for %s at %s.", direction, candidate));
// Determine block to start search from
ForgeDirection search;
if (direction.offsetX != 0)
search = ForgeDirection.SOUTH;
else
search = ForgeDirection.EAST;
int xOffset = 0;
int zOffset = 0;
Vect start = candidate;
while (true) {
xOffset += search.offsetX;
zOffset += search.offsetZ;
TileEntity tile = worldObj.getTileEntity(candidate.x + xOffset, candidate.y, candidate.z + zOffset);
if (tile == null)
break;
if (!(tile instanceof IFarmComponent))
break;
start = new Vect(candidate.x + xOffset, candidate.y, candidate.z + zOffset);
}
// System.out.println(String.format("Determined start block for %s at %s.", direction, candidate));
ForgeDirection reverse = search.getOpposite();
ForgeDirection tocenter = direction.getOpposite();
Vect last = new Vect(start.x + direction.offsetX, start.y, start.z + direction.offsetZ);
potential.add(new FarmTarget(last));
while (true) {
// Switch to next potential block in the farm.
last = new Vect(last.x + reverse.offsetX, last.y, last.z + reverse.offsetZ);
// Check validity.
TileEntity tile = worldObj.getTileEntity(last.x + tocenter.offsetX, last.y, last.z + tocenter.offsetZ);
// break if we have reached the end of the farm's length.
if (tile == null)
break;
if (!(tile instanceof IFarmComponent))
break;
potential.add(new FarmTarget(last));
}
// System.out.println(String.format("Adding %s to %s", potential.size(), direction));
// Set the maximum allowed extent.
int size = potential.size() * 3;
if (size > allowedExtent)
allowedExtent = size;
targets.put(direction, potential.toArray(new FarmTarget[0]));
}
// Fill out the corners
// System.out.println("Trying to round corners");
TreeMap<ForgeDirection, FarmTarget[]> cache = new TreeMap<ForgeDirection, FarmTarget[]>();
for (Map.Entry<ForgeDirection, FarmTarget[]> entry : targets.entrySet()) {
ForgeDirection direction = entry.getKey();
// If the count of possible targets does matches the allowedExtent, we are on the long side and will not process
if (direction == ForgeDirection.SOUTH || direction == ForgeDirection.NORTH) {
cache.put(entry.getKey(), entry.getValue());
continue;
}
// Set start and direction to search
ArrayList<FarmTarget> targ = new ArrayList<FarmTarget>(Arrays.asList(entry.getValue()));
int sidecount = targ.size();
FarmTarget start = entry.getValue()[0];
ForgeDirection search = ForgeDirection.SOUTH;
int cornerShift = 0;
if (!Config.squareFarms)
cornerShift = 1;
// System.out.println(String.format("Processing start at %s for direction %s.", start.getStart(), direction));
for (int i = cornerShift; i < allowedExtent + 1; i++) {
FarmTarget corner = new FarmTarget(new Vect(start.getStart().x + search.offsetX * i, start.getStart().y, start.getStart().z + search.offsetZ
* i));
if (!Config.squareFarms) {
corner.setLimit(allowedExtent - i);
// System.out.println(String.format("Setting %s at extent %s", corner.getStart().toString(), corner.getExtent()));
if (corner.getLimit() > 0)
targ.add(0, corner);
} else
targ.add(0, corner);
}
search = search.getOpposite();
for (int i = sidecount; i < sidecount + allowedExtent - cornerShift; i++) {
FarmTarget corner = new FarmTarget(new Vect(start.getStart().x + search.offsetX * i, start.getStart().y, start.getStart().z + search.offsetZ
* i));
if (!Config.squareFarms)
corner.setLimit(sidecount + allowedExtent - 1 - i);
// System.out.println(String.format("Setting %s at extent %s", corner.getStart().toString(), corner.getExtent()));
targ.add(corner);