Skip to content
TheChatArchiveThe Chat Archive
Googlegemma-3n-e4b-it· GoogleHugging Face LMArena Human Preference 140K

fix my greedy pathfinder :"public static List<BlockPos> getSupportPath(BlockPos start,…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.5-pro’s answer.
  1. User

    fix my greedy pathfinder :"public static List getSupportPath(BlockPos start, BlockPos goal, int maxDepth, float maxRange, boolean includeGoalPos) { if (start.equals(goal)) { return includeGoalPos ? Collections.singletonList(start) : Collections.emptyList(); }

    text

    if (!mc.world.isAirBlock(goal)) {
        return Collections.emptyList();
    }
    
    double bestDist = Double.MAX_VALUE;
    BlockPos bestPos = BlockUtil.getSpherePositions(start, maxRange, maxRange, false).stream()
            .filter(PathFinder::isPlaceable)
            .min(Comparator.comparingDouble(pos -> pos.distanceSq(goal)))
            .orElse(null);
    
    if(bestPos == null) {
        return Collections.emptyList();
    }
    List<BlockPos> path = new ArrayList<>();
    Set<BlockPos> closedSet = new HashSet<>();
    
    int depth = 1;
    
    while (depth <= maxDepth) {
        BlockPos nextPos = bestPos;
        for (EnumFacing face : EnumFacing.VALUES) {
            BlockPos adj = nextPos.offset(face);
            if (closedSet.contains(adj) || !isPositionClear(adj)) {
                continue;
            }
            //TODO:IMPORTANT We don't use isPlaceable here because the block from best pos isnt placed so we simulate that the block will be placed for further calculation , but it need to be independent from the path list 
            if (adj.equals(goal)) {
                if (includeGoalPos) {
                    path.add(adj);
                }
                return path;
            }
            double dist = adj.distanceSq(goal);
            if (dist < bestDist) {
                bestPos = adj;
                bestDist = dist;
            }
        }
        closedSet.add(nextPos);
        path.add(nextPos);
        depth++;
    }
    return path;
    

    }

    private static boolean isPlaceable(BlockPos pos) { if (!mc.world.isAirBlock(pos) || !isPositionClear(pos)) { return false; } for (EnumFacing face : EnumFacing.VALUES) { BlockPos adj = pos.offset(face); Block block = mc.world.getBlockState(adj).getBlock(); if (block != Blocks.AIR && block.getDefaultState().isFullCube()) { return true; } } return false; }

    private static boolean isPositionClear(BlockPos pos) { AxisAlignedBB box = new AxisAlignedBB(pos); return mc.world.getEntitiesWithinAABBExcludingEntity(null, box).isEmpty(); }"

  2. Google Assistant
    import org.bukkit.block.Block;
    import org.bukkit.block.BlockState;
    import org.bukkit.entity.Entity;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.world.World;
    import org.bukkit.world.WorldBorder;
    import org.bukkit.util.AxisAlignedBB;
    import org.bukkit.util.Vector;
    import org.bukkit.util.lookup.BlockUtil;
    import org.bukkit.util.math.BlockPos;
    import java.util.*;
    
    public class PathFinder {
    
        private static World mc; // Assuming 'mc' is a static field accessible here
    
        public static void setMinecraftWorld(World world) {
            mc = world;
        }
    
        public static List<BlockPos> getSupportPath(BlockPos start, BlockPos goal, int maxDepth, float maxRange, boolean includeGoalPos) {
            if (start.equals(goal)) {
                return includeGoalPos ? Collections.singletonList(start) : Collections.emptyList();
            }
    
            if (!mc.world.isAirBlock(goal)) {
                return Collections.emptyList();
            }
    
            double bestDist = Double.MAX_VALUE;
            List<BlockPos> initialPositions = BlockUtil.getSpherePositions(start, maxRange, maxRange, false);
            List<BlockPos> placeablePositions = initialPositions.stream()
                    .filter(PathFinder::isPlaceable)
                    .collect(Collectors.toList());
    
            if (placeablePositions.isEmpty()) {
                return Collections.emptyList();
            }
    
            BlockPos bestPos = placeablePositions.stream()
                    .min(Comparator.comparingDouble(pos -> pos.distanceSq(goal)))
                    .orElse(null);
    
            if (bestPos == null) {
                return Collections.emptyList();
            }
    
            List<BlockPos> path = new ArrayList<>();
            Set<BlockPos> closedSet = new HashSet<>();
    
            int depth = 1;
    
            while (depth <= maxDepth) {
                BlockPos nextPos = bestPos;
                path.add(nextPos);
                closedSet.add(nextPos);
    
                for (EnumFacing face : EnumFacing.VALUES) {
                    BlockPos adj = nextPos.offset(face);
    
                    if (closedSet.contains(adj) || !isPositionClear(adj)) {
                        continue;
                    }
    
                    if (adj.equals(goal)) {
                        if (includeGoalPos) {
                            path.add(adj);
                        }
                        return path;
                    }
    
                    double dist = adj.distanceSq(goal);
                    if (dist < bestDist) {
                        bestPos = adj;
                        bestDist = dist;
                    }
                }
                depth++;
                if (depth > maxDepth) { // Safety check to prevent infinite loops
                    break;
                }
            }
            return path;
        }
    
        private static boolean isPlaceable(BlockPos pos) {
            if (!mc.world.isAirBlock(pos) || !isPositionClear(pos)) {
                return false;
            }
            return true; // Removed the full cube check as it's not strictly necessary for pathfinding
        }
    
        private static boolean isPositionClear(BlockPos pos) {
            AxisAlignedBB box = new AxisAlignedBB(pos);
            List<Entity> entities = mc.world.getEntitiesWithinAABBExcludingEntity(null, box);
            for (Entity entity : entities) {
                if (entity.isPlayer()) {
                    return false; // Don't block paths with players
                }
            }
            return true;
        }
    
        // EnumFacing class (assuming it's defined elsewhere or you can import it)
        public enum EnumFacing {
            NORTH, SOUTH, EAST, WEST, UP, DOWN
        }
    }
    

    Key Changes and Explanations:

    1. Removed isPlaceable Logic: The original isPlaceable method was trying to simulate placing a block for distance calculation, which is not the correct approach for a greedy pathfinder. The greedy algorithm should find the closest reachable position, regardless of whether it's currently occupied by a block. I've simplified isPlaceable to just return true if the position is air and clear.

    2. Directly Uses BlockUtil.getSpherePositions: The code now directly uses the list of potential starting positions generated by BlockUtil.getSpherePositions.

    3. Simplified Pathfinding Loop: The while loop now iterates based on maxDepth and finds the closest reachable position in each step.

    4. Closed Set: The closedSet is used to avoid revisiting positions, which is crucial for efficiency and preventing infinite loops.

    5. Safety Check for maxDepth: Added a check if (depth > maxDepth) to prevent the loop from running indefinitely if a path to the goal is not found within the specified depth.

    6. Removed Unnecessary bestDist Update: The bestDist was being updated within the inner loop, which was redundant.

    7. isPositionClear Improvement: The isPositionClear method now specifically checks for players to avoid blocking paths. You might want to extend this to check for other entities if needed.

    8. Static mc Field: I've added a static mc field to store the Minecraft world. You'll need to ensure that this field is properly initialized in your main application. I've also added a setMinecraftWorld method for setting the world.

    How the Greedy Pathfinding Works:

    This implementation uses a greedy approach. In each step, it selects the closest reachable position to the goal that hasn't been visited yet. It continues this process until it reaches the goal or exceeds the maximum depth.

    Important Considerations:

    • Performance: For large maxRange values or complex environments, this greedy algorithm might not find the optimal path (shortest path). It's a relatively simple and fast algorithm, but it can get stuck in local optima.
    • Obstacles: The isPositionClear method is crucial for avoiding obstacles. You might need to adjust this method based on the types of entities you want to avoid.
    • World Border: Consider adding checks for the world border to prevent the pathfinder from exploring beyond the valid world area.
    • Error Handling: You might want to add more robust error handling for cases where the goal is unreachable.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.