(async (bot) => {

    
    // Define house parameters
    const houseWidth = 5; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    const houseDepth = 5; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    const houseHeight = 3; // Wall height (number of blocks high for the walls)
    const material = "oak_log"; // Material to build the house with - using logs directly as planks cannot be crafted with current skills
    
    // Calculate total blocks needed for the house
    const totalBlocksNeeded = (houseWidth * houseDepth) + // Floor
                              (2 * houseWidth * houseHeight) + // Front/Back walls
                              (2 * (houseDepth - 2) * houseHeight) + // Side walls (excluding corners)
                              (houseWidth * houseDepth); // Roof
    
    log(bot,`Need to collect approximately ${totalBlocksNeeded} ${material}s.`); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
    let collectedCount = 0; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    const searchRange = 64; // Max distance to search for logs
    
    // 1. Collect sufficient wood logs
    // Loop until enough logs are collected or no more logs can be found
    while (collectedCount < totalBlocksNeeded) {
        // Find the nearest oak log within the specified range
        const logBlock = world.getNearestBlock(bot, "oak_log", searchRange); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
        if (!logBlock) {
            log(bot,"Could not find any more oak logs within range. Stopping collection."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            break; // Exit loop if no more logs are found
        }
    
        log(bot,`Going to collect oak log at X:${logBlock.position.x}, Y:${logBlock.position.y}, Z:${logBlock.position.z}`); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        // Navigate to within 1 block of the log for breaking
        const arrived = await skills.goToNearestBlock(bot, "oak_log", 1, searchRange); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
        if (!arrived) {
            log(bot,`Failed to reach oak log at ${logBlock.position.x}, ${logBlock.position.y}, ${logBlock.position.z}. Trying next.`); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            continue; // Skip to the next iteration to find another log
        }
    
        // Break the log block
        log(bot,`Breaking oak log at X:${logBlock.position.x}, Y:${logBlock.position.y}, Z:${logBlock.position.z}`); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        const broken = await skills.breakBlockAt(bot, logBlock.position.x, logBlock.position.y, logBlock.position.z); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
        if (broken) {
            collectedCount++; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            log(bot,`Collected 1 oak log. Total collected: ${collectedCount}/${totalBlocksNeeded}`); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        } else {
            log(bot,`Failed to break oak log at X:${logBlock.position.x}, Y:${logBlock.position.y}, Z:${logBlock.position.z}.`); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            // If unable to break, continue to try and find another log
        }
    }
    
    // Check if enough materials were collected to proceed with building
    if (collectedCount < totalBlocksNeeded) {
        log(bot,`Only collected ${collectedCount} ${material}s, which is less than the required ${totalBlocksNeeded}. Cannot build a complete house.`); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        return; // Exit if not enough materials collected
    }
    
    log(bot,`Successfully collected ${collectedCount} ${material}s.`); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
    // 2. Find a suitable building location for the house
    // Search for a 5x5 area (matching house width/depth) within 15 blocks of the bot's current position.
    const buildPos = await world.getNearestFreeSpace(bot, houseWidth, 15); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
    if (!buildPos) {
        log(bot,"Could not find a suitable free space to build the house."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        return; // Exit if no suitable space is found
    }
    
    const baseStartX = buildPos.x; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    const baseStartY = buildPos.y; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    const baseStartZ = buildPos.z; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
    log(bot,`Building a ${houseWidth}x${houseDepth}x${houseHeight} ${material} house at X:${baseStartX}, Y:${baseStartY}, Z:${baseStartZ}`); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
    // 3. Build the base (floor) of the house
    for (let x = 0; x < houseWidth; x++) {
        for (let z = 0; z < houseDepth; z++) {
            await skills.placeBlock(bot, material, baseStartX + x, baseStartY, baseStartZ + z); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        }
    }
    
    // 4. Build the walls of the house
    for (let y = 1; y <= houseHeight; y++) {
        // Build the front and back walls
        for (let x = 0; x < houseWidth; x++) {
            await skills.placeBlock(bot, material, baseStartX + x, baseStartY + y, baseStartZ); // Front wall
            await skills.placeBlock(bot, material, baseStartX + x, baseStartY + y, baseStartZ + houseDepth - 1); // Back wall
        }
        // Build the side walls (excluding the corners, which are covered by front/back walls)
        for (let z = 1; z < houseDepth - 1; z++) {
            await skills.placeBlock(bot, material, baseStartX, baseStartY + y, baseStartZ + z); // Left wall
            await skills.placeBlock(bot, material, baseStartX + houseWidth - 1, baseStartY + y, baseStartZ + z); // Right wall
        }
    }
    
    // 5. Build the roof of the house
    const roofY = baseStartY + houseHeight + 1; // Y-coordinate for the roof layer (1 block above the highest wall)
    for (let x = 0; x < houseWidth; x++) {
        for (let z = 0; z < houseDepth; z++) {
            await skills.placeBlock(bot, material, baseStartX + x, roofY, baseStartZ + z); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        }
    }
    
    log(bot,"House building complete!");

log(bot, 'Code finished.');

})