Skip to content

Commit

Permalink
expand range (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
HoleFish authored Oct 9, 2024
1 parent de975bf commit 607ba24
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions src/main/java/tconstruct/items/tools/LumberAxe.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package tconstruct.items.tools;

import java.util.Comparator;
import java.util.Queue;
import java.util.Set;
import java.util.SortedSet;
import java.util.Stack;
import java.util.TreeSet;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
Expand Down Expand Up @@ -114,34 +117,43 @@ public boolean onBlockStartBreak(ItemStack stack, int x, int y, int z, EntityPla
}

public static boolean detectTree(World world, int pX, int pY, int pZ) {
ChunkPosition pos = null;
Stack<ChunkPosition> candidates = new Stack<>();
SortedSet<ChunkPosition> visited = new TreeSet<>(Comparator.comparingInt((ChunkPosition a) -> a.chunkPosY));

candidates.add(new ChunkPosition(pX, pY, pZ));

while (!candidates.isEmpty()) {
ChunkPosition candidate = candidates.pop();
int curX = candidate.chunkPosX, curY = candidate.chunkPosY, curZ = candidate.chunkPosZ;

Block block = world.getBlock(curX, curY, curZ);
if ((pos == null || candidate.chunkPosY > pos.chunkPosY) && block.isWood(world, curX, curY, curZ)) {
pos = new ChunkPosition(curX, candidate.chunkPosY + 1, curZ);
// go up
while (world.getBlock(curX, pos.chunkPosY, curZ).isWood(world, curX, pos.chunkPosY, curZ)) {
pos = new ChunkPosition(curX, pos.chunkPosY + 1, curZ);
if (!block.isWood(world, curX, curY, curZ)) {
continue;
}
if (!visited.add(candidate)) {
continue;
}

// add the current layer and above
for (int offX = 0; offX < 3; offX++) {
for (int offY = 0; offY < 2; offY++) {
for (int offZ = 0; offZ < 3; offZ++) {
ChunkPosition newCandidate = new ChunkPosition(curX - 1 + offX, curY + offY, curZ - 1 + offZ);
if (!visited.contains(newCandidate)) {
candidates.add(newCandidate);
}
}
}
// check if we still have a way diagonally up
candidates.add(new ChunkPosition(curX + 1, pos.chunkPosY + 1, curZ));
candidates.add(new ChunkPosition(curX, pos.chunkPosY + 1, curZ + 1));
candidates.add(new ChunkPosition(curX - 1, pos.chunkPosY + 1, curZ));
candidates.add(new ChunkPosition(curX, pos.chunkPosY + 1, curZ - 1));
}
}

// not even one match, so there were no logs.
if (pos == null) {
if (visited.isEmpty()) {
return false;
}

ChunkPosition topmost = visited.last();

// check if there were enough leaves around the last position
// pos now contains the block above the topmost log
// we want at least 5 leaves in the surrounding 26 blocks
Expand All @@ -150,8 +162,8 @@ public static boolean detectTree(World world, int pX, int pY, int pZ) {
for (int offX = 0; offX < d; offX++) {
for (int offY = 0; offY < d; offY++) {
for (int offZ = 0; offZ < d; offZ++) {
int xPos = pos.chunkPosX - 1 + offX, yPos = pos.chunkPosY - 1 + offY,
zPos = pos.chunkPosZ - 1 + offZ;
int xPos = topmost.chunkPosX - 1 + offX, yPos = topmost.chunkPosY - 1 + offY,
zPos = topmost.chunkPosZ - 1 + offZ;
Block leaf = world.getBlock(xPos, yPos, zPos);
if (leaf != null && leaf.isLeaves(world, xPos, yPos, zPos)) {
if (++leaves >= 5) {
Expand Down Expand Up @@ -238,16 +250,12 @@ public void onWorldTick(TickEvent.WorldTickEvent event) {
continue;
}

// save its neighbors
queueCoordinate(x + 1, y, z);
queueCoordinate(x, y, z + 1);
queueCoordinate(x - 1, y, z);
queueCoordinate(x, y, z - 1);

// also add the layer above.. stupid acacia trees
// add the current layer and above
for (int offX = 0; offX < 3; offX++) {
for (int offZ = 0; offZ < 3; offZ++) {
queueCoordinate(x - 1 + offX, y + 1, z - 1 + offZ);
for (int offY = 0; offY < 2; offY++) {
for (int offZ = 0; offZ < 3; offZ++) {
queueCoordinate(x - 1 + offX, y + offY, z - 1 + offZ);
}
}
}

Expand Down

0 comments on commit 607ba24

Please sign in to comment.