updating cooking task intiator to be easier to read, fewer nested function

This commit is contained in:
Isadora White 2025-05-01 15:08:07 -07:00
parent f8c61c64ec
commit abf3532433
3 changed files with 385 additions and 357 deletions

View file

@ -1,19 +1,18 @@
import { getPosition } from "../library/world.js"; import { getPosition } from "../library/world.js";
export class CookingTaskInitiator { export class CookingTaskInitiator {
constructor(data, agent) { constructor(data, bot) {
this.agent = agent; this.bot = bot;
this.data = data; this.data = data;
} }
async init() { async init() {
let bot = this.agent.bot; let bot = this.bot;
//// Setting up the cooking world using minecraft cheats //// //// Setting up the cooking world using minecraft cheats ////
// Only run the setup if the agent is the first one // Only run the setup if the agent is the first one
if (this.agent.count_id === 0) {
// Clear and prepare the base area // Clear and prepare the base area
await bot.chat(`/fill ~ ~-1 ~ ~50 ~-3 ~50 grass_block`); await bot.chat(`/fill ~ ~-1 ~ ~50 ~-3 ~50 grass_block`);
await bot.chat(`/fill ~ ~-1 ~ ~-50 ~-3 ~50 grass_block`); await bot.chat(`/fill ~ ~-1 ~ ~-50 ~-3 ~50 grass_block`);
@ -46,7 +45,7 @@ export class CookingTaskInitiator {
const minZStart = position.z - 25; const minZStart = position.z - 25;
let attempts = 0; let attempts = 0;
while (attempts < 1000) { while (attempts < 10000) {
const xStart = Math.floor(minXStart + Math.random() * (maxXStart - minXStart + 1)); const xStart = Math.floor(minXStart + Math.random() * (maxXStart - minXStart + 1));
const zStart = Math.floor(minZStart + Math.random() * (maxZStart - minZStart + 1)); const zStart = Math.floor(minZStart + Math.random() * (maxZStart - minZStart + 1));
const xMin = xStart; const xMin = xStart;
@ -84,8 +83,6 @@ export class CookingTaskInitiator {
regionsToPlace[i].depth = depth + 4; regionsToPlace[i].depth = depth + 4;
} }
const occupiedRegions = [{ const occupiedRegions = [{
xMin : botX - 1, xMin : botX - 1,
xMax : botX + 1, xMax : botX + 1,
@ -113,65 +110,124 @@ export class CookingTaskInitiator {
} }
} }
// Planting functions with dynamic positions // Execute all planting
const plantWheat = async (xStart, zStart) => { // await plantWheat(regionPositions.wheat.xStart, regionPositions.wheat.zStart);
await this.plantCrops(regionPositions.wheat.xStart, regionPositions.wheat.zStart, 'wheat[age=7]', true);
await this.plantCrops(regionPositions.beetroots.xStart, regionPositions.beetroots.zStart, 'beetroots[age=3]', true);
await this.plantMushrooms(regionPositions.mushrooms.xStart, regionPositions.mushrooms.zStart);
await new Promise(resolve => setTimeout(resolve, 300));
await this.plantCrops(regionPositions.potatoes.xStart, regionPositions.potatoes.zStart, 'potatoes[age=7]', true);
await this.plantCrops(regionPositions.carrots.xStart, regionPositions.carrots.zStart, 'carrots[age=7]', true);
await this.plantCrops(regionPositions.pumpkins.xStart, regionPositions.pumpkins.zStart, 'pumpkin', false);
await this.plantSugarCane(regionPositions.sugar_cane);
await new Promise(resolve => setTimeout(resolve, 300));
// await plantPumpkins(regionPositions.pumpkins.xStart, regionPositions.pumpkins.zStart);
// await new Promise(resolve => setTimeout(resolve, 300));
await this.buildHouse(regionPositions.house.xStart, regionPositions.house.zStart);
// Add a chest with cooking items near the bot
const addChestWithItems = async () => {
// Find a valid position near the bot (within 10 blocks)
const findChestPosition = () => {
const maxAttempts = 100;
for (let attempt = 0; attempt < maxAttempts; attempt++) {
const x = botX + Math.floor(Math.random() * 10 - 5); // Within ±5 blocks X
const z = botZ + Math.floor(Math.random() * 10 - 5); // Within ±5 blocks Z
const y = position.y;
// Check if the position is not overlapping with existing structures
if (!isOverlapping(x, x, z, z, occupiedRegions)) {
return { x, y, z };
}
}
throw new Error('Failed to find valid chest position');
};
const { x, y, z } = findChestPosition();
// Place the chest
await bot.chat(`/setblock ${x} ${y} ${z} chest`);
const cookingItems = [
['minecraft:milk_bucket', 1], // Non-stackable
['minecraft:egg', 16], // Stacks to 16
['minecraft:dandelion', 64], // Stacks to 64
['minecraft:sugar', 64],
['minecraft:cocoa_beans', 64],
['minecraft:apple', 64],
['minecraft:milk_bucket', 1],
['minecraft:milk_bucket', 1],
['minecraft:salmon', 64],
['minecraft:cod', 64],
['minecraft:kelp', 64],
['minecraft:dried_kelp', 64],
['minecraft:sweet_berries', 64],
['minecraft:honey_bottle', 1], // Non-stackable
['minecraft:glow_berries', 64],
['minecraft:bowl', 64],
['minecraft:milk_bucket', 1],
['minecraft:milk_bucket', 1],
['minecraft:milk_bucket', 1],
['minecraft:milk_bucket', 1],
['minecraft:cooked_salmon', 64],
['minecraft:cooked_cod', 64],
['minecraft:gold_ingot', 64],
['minecraft:oak_planks', 64],
['minecraft:iron_ingot', 64],
['minecraft:milk_bucket', 1],
['minecraft:milk_bucket', 1],
];
// Fill the chest with random cooking items
for (let slot = 0; slot < cookingItems.length; slot++) { // Chest has 27 slots
const randomItem = cookingItems[slot];
await bot.chat(`/item replace block ${x} ${y} ${z} container.${slot} with ${randomItem[0]} ${randomItem[1]}`);
}
// Mark the chest area as occupied
occupiedRegions.push({
xMin: x,
xMax: x,
zMin: z,
zMax: z
});
};
await addChestWithItems();
await new Promise(resolve => setTimeout(resolve, 300));
const animals = ['chicken', 'cow', 'llama', 'mooshroom', 'pig', 'rabbit', 'sheep'];
// Animal management
await this.killEntities(["item"]);
await this.killEntities(animals);
await this.killEntities(["item"]);
await new Promise(resolve => setTimeout(resolve, 300));
// Summon new animals
await this.summonAnimals(animals, 8);
}
async plantCrops (xStart, zStart, crop_and_age, till=true) {
for (let i = 0; i < 6; i++) { for (let i = 0; i < 6; i++) {
for (let j = 0; j < 6; j++) { for (let j = 0; j < 6; j++) {
const x = xStart + i; const x = xStart + i;
const z = zStart + j; const z = zStart + j;
if (till) {
await bot.chat(`/setblock ${x} ${position.y - 1} ${z} farmland`); await bot.chat(`/setblock ${x} ${position.y - 1} ${z} farmland`);
await bot.chat(`/setblock ${x} ${position.y} ${z} wheat[age=7]`);
} }
await bot.chat(`/setblock ${x} ${position.y} ${z} ${crop_and_age}`);
}
}
await new Promise(resolve => setTimeout(resolve, 300));
} }
}; async plantSugarCane (patches) {
const plantBeetroots = async (xStart, zStart) => {
for (let i = 0; i < 4; i++) {
for (let j = 0; j < 5; j++) {
const x = xStart + i;
const z = zStart + j;
await bot.chat(`/setblock ${x} ${position.y - 1} ${z} farmland`);
await bot.chat(`/setblock ${x} ${position.y} ${z} beetroots[age=3]`);
}
}
};
const plantMushrooms = async (xStart, zStart) => {
for (let i = 0; i < 4; i++) {
for (let j = 0; j < 5; j++) {
const x = xStart + i;
const z = zStart + j;
await bot.chat(`/setblock ${x} ${position.y - 1} ${z} mycelium`);
const mushroomType = (i + j) % 2 === 0 ? 'red_mushroom' : 'brown_mushroom';
await bot.chat(`/setblock ${x} ${position.y} ${z} ${mushroomType}`);
}
}
};
const plantPotatoes = async (xStart, zStart) => {
for (let i = 0; i < 4; i++) {
for (let j = 0; j < 5; j++) {
const x = xStart + i;
const z = zStart + j;
await bot.chat(`/setblock ${x} ${position.y - 1} ${z} farmland`);
await bot.chat(`/setblock ${x} ${position.y} ${z} potatoes[age=7]`);
}
}
};
const plantCarrots = async (xStart, zStart) => {
for (let i = 0; i < 4; i++) {
for (let j = 0; j < 5; j++) {
const x = xStart + i;
const z = zStart + j;
await bot.chat(`/setblock ${x} ${position.y - 1} ${z} farmland`);
await bot.chat(`/setblock ${x} ${position.y} ${z} carrots[age=7]`);
}
}
};
const plantSugarCane = async (patches) => {
for (const patch of patches) { for (const patch of patches) {
const xCenter = patch.xStart + 1; const xCenter = patch.xStart + 1;
const zCenter = patch.zStart + 1; const zCenter = patch.zStart + 1;
@ -183,32 +239,102 @@ export class CookingTaskInitiator {
} }
}; };
const plantPumpkins = async (xStart, zStart) => { async plantMushrooms(xStart, zStart) {
for (let i = 0; i < 10; i++) { for (let i = 0; i < 4; i++) {
for (let j = 0; j < 5; j++) {
const x = xStart + i; const x = xStart + i;
const z = zStart; const z = zStart + j;
await bot.chat(`/setblock ${x} ${position.y} ${z} pumpkin`); await bot.chat(`/setblock ${x} ${position.y - 1} ${z} mycelium`);
const mushroomType = (i + j) % 2 === 0 ? 'red_mushroom' : 'brown_mushroom';
await bot.chat(`/setblock ${x} ${position.y} ${z} ${mushroomType}`);
} }
}
}
async summonAnimals (animals, amount) {
for (const animal of animals) {
for (let i = 0; i < amount; i++) {
const x = position.x - 25 + Math.random() * 50;
const z = position.z - 25 + Math.random() * 50;
await bot.chat(`/summon ${animal} ${Math.floor(x)} ${position.y} ${Math.floor(z)}`);
}
}
}
async killEntities(entities) {
for (const entity of entities) {
await bot.chat(`/kill @e[type=${animal},distance=..200]`);
}
}
async addChestWithItems () {
// Find a valid position near the bot (within 10 blocks)
const findChestPosition = () => {
const maxAttempts = 100;
for (let attempt = 0; attempt < maxAttempts; attempt++) {
const x = botX + Math.floor(Math.random() * 10 - 5); // Within ±5 blocks X
const z = botZ + Math.floor(Math.random() * 10 - 5); // Within ±5 blocks Z
const y = position.y;
// Check if the position is not overlapping with existing structures
if (!isOverlapping(x, x, z, z, occupiedRegions)) {
return { x, y, z };
}
}
throw new Error('Failed to find valid chest position');
}; };
// Execute all planting const { x, y, z } = findChestPosition();
await plantWheat(regionPositions.wheat.xStart, regionPositions.wheat.zStart);
await new Promise(resolve => setTimeout(resolve, 300));
await plantBeetroots(regionPositions.beetroots.xStart, regionPositions.beetroots.zStart);
await new Promise(resolve => setTimeout(resolve, 300));
await plantMushrooms(regionPositions.mushrooms.xStart, regionPositions.mushrooms.zStart);
await new Promise(resolve => setTimeout(resolve, 300));
await plantPotatoes(regionPositions.potatoes.xStart, regionPositions.potatoes.zStart);
await new Promise(resolve => setTimeout(resolve, 300));
await plantCarrots(regionPositions.carrots.xStart, regionPositions.carrots.zStart);
await new Promise(resolve => setTimeout(resolve, 300));
await plantSugarCane(regionPositions.sugar_cane);
await new Promise(resolve => setTimeout(resolve, 300));
await plantPumpkins(regionPositions.pumpkins.xStart, regionPositions.pumpkins.zStart);
await new Promise(resolve => setTimeout(resolve, 300));
// House construction // Place the chest
const buildHouse = async (xStart, zStart) => { await bot.chat(`/setblock ${x} ${y} ${z} chest`);
const cookingItems = [
['minecraft:milk_bucket', 1], // Non-stackable
['minecraft:egg', 16], // Stacks to 16
['minecraft:dandelion', 64], // Stacks to 64
['minecraft:sugar', 64],
['minecraft:cocoa_beans', 64],
['minecraft:apple', 64],
['minecraft:milk_bucket', 1],
['minecraft:milk_bucket', 1],
['minecraft:salmon', 64],
['minecraft:cod', 64],
['minecraft:kelp', 64],
['minecraft:dried_kelp', 64],
['minecraft:sweet_berries', 64],
['minecraft:honey_bottle', 1], // Non-stackable
['minecraft:glow_berries', 64],
['minecraft:bowl', 64],
['minecraft:milk_bucket', 1],
['minecraft:milk_bucket', 1],
['minecraft:milk_bucket', 1],
['minecraft:milk_bucket', 1],
['minecraft:cooked_salmon', 64],
['minecraft:cooked_cod', 64],
['minecraft:gold_ingot', 64],
['minecraft:oak_planks', 64],
['minecraft:iron_ingot', 64],
['minecraft:milk_bucket', 1],
['minecraft:milk_bucket', 1],
];
// Fill the chest with random cooking items
for (let slot = 0; slot < cookingItems.length; slot++) { // Chest has 27 slots
const randomItem = cookingItems[slot];
await bot.chat(`/item replace block ${x} ${y} ${z} container.${slot} with ${randomItem[0]} ${randomItem[1]}`);
}
// Mark the chest area as occupied
occupiedRegions.push({
xMin: x,
xMax: x,
zMin: z,
zMax: z
});
}
async buildHouse (xStart, zStart) {
const startX = xStart; const startX = xStart;
const startY = position.y; const startY = position.y;
const startZ = zStart; const startZ = zStart;
@ -283,108 +409,6 @@ export class CookingTaskInitiator {
// Add fuel to the smoker // Add fuel to the smoker
await bot.chat(`/data merge block ${startX + 4} ${startY + 1} ${startZ + 7} {Items:[{Slot:1b,id:"minecraft:coal",Count:64b}]}`) await bot.chat(`/data merge block ${startX + 4} ${startY + 1} ${startZ + 7} {Items:[{Slot:1b,id:"minecraft:coal",Count:64b}]}`)
await bot.chat(`/setblock ${startX + depth - 3} ${startY + 1} ${startZ + 2} bed`); await bot.chat(`/setblock ${startX + depth - 3} ${startY + 1} ${startZ + 2} bed`);
};
await buildHouse(regionPositions.house.xStart, regionPositions.house.zStart);
await new Promise(resolve => setTimeout(resolve, 300)); await new Promise(resolve => setTimeout(resolve, 300));
// Add a chest with cooking items near the bot
const addChestWithItems = async () => {
// Find a valid position near the bot (within 10 blocks)
const findChestPosition = () => {
const maxAttempts = 100;
for (let attempt = 0; attempt < maxAttempts; attempt++) {
const x = botX + Math.floor(Math.random() * 10 - 5); // Within ±5 blocks X
const z = botZ + Math.floor(Math.random() * 10 - 5); // Within ±5 blocks Z
const y = position.y;
// Check if the position is not overlapping with existing structures
if (!isOverlapping(x, x, z, z, occupiedRegions)) {
return { x, y, z };
}
}
throw new Error('Failed to find valid chest position');
};
const { x, y, z } = findChestPosition();
// Place the chest
await bot.chat(`/setblock ${x} ${y} ${z} chest`);
const cookingItems = [
['minecraft:milk_bucket', 1], // Non-stackable
['minecraft:egg', 16], // Stacks to 16
['minecraft:dandelion', 64], // Stacks to 64
['minecraft:sugar', 64],
['minecraft:cocoa_beans', 64],
['minecraft:apple', 64],
['minecraft:milk_bucket', 1],
['minecraft:milk_bucket', 1],
['minecraft:salmon', 64],
['minecraft:cod', 64],
['minecraft:kelp', 64],
['minecraft:dried_kelp', 64],
['minecraft:sweet_berries', 64],
['minecraft:honey_bottle', 1], // Non-stackable
['minecraft:glow_berries', 64],
['minecraft:bowl', 64],
['minecraft:milk_bucket', 1],
['minecraft:milk_bucket', 1],
['minecraft:milk_bucket', 1],
['minecraft:milk_bucket', 1],
['minecraft:cooked_salmon', 64],
['minecraft:cooked_cod', 64],
['minecraft:gold_ingot', 64],
['minecraft:oak_planks', 64],
['minecraft:iron_ingot', 64],
['minecraft:milk_bucket', 1],
['minecraft:milk_bucket', 1],
];
// Fill the chest with random cooking items
for (let slot = 0; slot < cookingItems.length; slot++) { // Chest has 27 slots
const randomItem = cookingItems[slot];
await bot.chat(`/item replace block ${x} ${y} ${z} container.${slot} with ${randomItem[0]} ${randomItem[1]}`);
}
// Mark the chest area as occupied
occupiedRegions.push({
xMin: x,
xMax: x,
zMin: z,
zMax: z
});
};
await addChestWithItems();
await new Promise(resolve => setTimeout(resolve, 300));
// Animal management
await bot.chat('/kill @e[type=item,distance=..200]');
await bot.chat('/kill @e[type=chicken,distance=..200]');
await bot.chat('/kill @e[type=cow,distance=..200]');
await bot.chat('/kill @e[type=llama,distance=..200]');
await bot.chat('/kill @e[type=mooshroom,distance=..200]');
await bot.chat('/kill @e[type=pig,distance=..200]');
await bot.chat('/kill @e[type=rabbit,distance=..200]');
await bot.chat('/kill @e[type=sheep,distance=..200]');
await bot.chat(`/kill @e[type=item,distance=..200]`);
await new Promise(resolve => setTimeout(resolve, 300));
// Summon new animals
const summonAnimals = async () => {
const animals = ['chicken', 'cow', 'llama', 'mooshroom', 'pig', 'rabbit', 'sheep'];
for (const animal of animals) {
for (let i = 0; i < 8; i++) {
const x = position.x - 25 + Math.random() * 50;
const z = position.z - 25 + Math.random() * 50;
await bot.chat(`/summon ${animal} ${Math.floor(x)} ${position.y} ${Math.floor(z)}`);
}
}
};
await summonAnimals();
}
} }
} }

View file

@ -417,7 +417,7 @@ export class Task {
return; return;
if (this.task_type === 'cooking') { if (this.task_type === 'cooking') {
this.initiator = new CookingTaskInitiator(this.data, this.agent); this.initiator = new CookingTaskInitiator(this.data, this.agent.bot);
} else { } else {
this.initiator = null; this.initiator = null;
} }
@ -482,7 +482,7 @@ export class Task {
await new Promise((resolve) => setTimeout(resolve, 500)); await new Promise((resolve) => setTimeout(resolve, 500));
} }
if (this.initiator) { if (this.initiator && this.agent.count_id === 0) {
await this.initiator.init(); await this.initiator.init();
} }

View file

@ -1,5 +1,7 @@
import mineflayer from 'mineflayer'; import mineflayer from 'mineflayer';
import yargs from 'yargs'; import yargs from 'yargs';
import { resetConstructionWorld } from '../src/agent/tasks/construction_tasks.js';
import { cookingTaskInitalization } from '../src/agent/tasks/cooking_tasks.js';
import { worldToBlueprint, blueprintToTask } from '../../src/agent/tasks/construction_tasks.js'; import { worldToBlueprint, blueprintToTask } from '../../src/agent/tasks/construction_tasks.js';
import fs from 'fs'; import fs from 'fs';
import { start } from 'repl'; import { start } from 'repl';
@ -43,6 +45,8 @@ const selectedTaskId = taskData.task_id;
// give the required inventory items to the usernames specified in the usernames list // give the required inventory items to the usernames specified in the usernames list
bot.on('spawn', async () => { bot.on('spawn', async () => {
console.log("Bot spawned. Starting task..."); console.log("Bot spawned. Starting task...");
// initiate the world according to the construction or cooking world
const usernames = args.usernames; const usernames = args.usernames;
const task = taskData[selectedTaskId]; const task = taskData[selectedTaskId];
const inventory = task.initial_inventory; const inventory = task.initial_inventory;