merge context and query files

This commit is contained in:
Kolby Nottingham 2023-12-28 09:20:26 -08:00
parent a056575073
commit 8abd8fee94
2 changed files with 61 additions and 117 deletions

View file

@ -1,111 +0,0 @@
import { readFileSync } from 'fs';
import { getNearestBlock, getNearbyMobTypes, getNearbyPlayerNames, getNearbyBlockTypes, getInventoryCounts } from './world.js';
import { getAllItems } from '../utils/mcdata.js';
export function getStats(bot) {
let res = 'STATS';
res += `\n- position: x:${bot.entity.position.x}, y:${bot.entity.position.y}, z:${bot.entity.position.z}`;
res += `\n- health: ${bot.health} / 20`;
if (bot.time.timeOfDay < 6000) {
res += '\n- time: Morning';
} else if (bot.time.timeOfDay < 12000) {
res += '\n- time: Afternoon';
} else {
res += '\n- time: Night';
}
return res;
}
export function getInventory(bot) {
let inventory = getInventoryCounts(bot);
let res = 'INVENTORY';
for (const item in inventory) {
if (inventory[item] && inventory[item] > 0)
res += `\n- ${item}: ${inventory[item]}`;
}
if (res == 'INVENTORY') {
res += ': none';
}
return res;
}
export function getBlocks(bot) {
let res = 'NEARBY_BLOCKS';
let blocks = getNearbyBlockTypes(bot);
for (let i = 0; i < blocks.length; i++) {
res += `\n- ${blocks[i]}`;
}
if (blocks.length == 0) {
res += ': none';
}
return res;
}
export function getNearbyEntities(bot) {
let res = 'NEARBY_ENTITIES';
for (const entity of getNearbyPlayerNames(bot)) {
res += `\n- player: ${entity}`;
}
for (const entity of getNearbyMobTypes(bot)) {
res += `\n- mob: ${entity}`;
}
if (res == 'NEARBY_ENTITIES') {
res += ': none';
}
return res;
}
export function getCraftable(bot) {
const table = getNearestBlock(bot, 'crafting_table');
let res = 'CRAFTABLE_ITEMS';
for (const item of getAllItems()) {
let recipes = bot.recipesFor(item.id, null, 1, table);
if (recipes.length > 0) {
res += `\n- ${item.name}`;
}
}
if (res == 'CRAFTABLE_ITEMS') {
res += ': none';
}
return res;
}
export function getDetailedSkills() {
let res = 'namespace skills {';
let contents = readFileSync("./utils/skills.js", "utf-8").split('\n');
for (let i = 0; i < contents.length; i++) {
if (contents[i].slice(0, 3) == '/**') {
res += '\t' + contents[i];
} else if (contents[i].slice(0, 2) == ' *') {
res += '\t' + contents[i];
} else if (contents[i].slice(0, 4) == ' **/') {
res += '\t' + contents[i] + '\n\n';
}
}
res = res.trim() + '\n}'
return res;
}
export function getWorldFunctions() {
let res = 'namespace world {';
let contents = readFileSync("./utils/world.js", "utf-8").split('\n');
for (let i = 0; i < contents.length; i++) {
if (contents[i].slice(0, 3) == '/**') {
res += '\t' + contents[i];
} else if (contents[i].slice(0, 2) == ' *') {
res += '\t' + contents[i];
} else if (contents[i].slice(0, 4) == ' **/') {
res += '\t' + contents[i] + '\n\n';
}
}
res = res.trim() + '\n}'
return res;
}

View file

@ -1,4 +1,6 @@
import { getStats, getInventory, getBlocks, getNearbyEntities, getCraftable } from './context.js';
import { getNearestBlock, getNearbyMobTypes, getNearbyPlayerNames, getNearbyBlockTypes, getInventoryCounts } from './world.js';
import { getAllItems } from '../utils/mcdata.js';
const pad = (str) => {
return '\n' + str + '\n';
@ -9,35 +11,88 @@ const queryList = [
name: "!stats",
description: "Get your bot's stats",
perform: function (agent) {
return pad(getStats(agent.bot));
let bot = agent.bot;
let res = 'STATS';
res += `\n- position: x:${bot.entity.position.x}, y:${bot.entity.position.y}, z:${bot.entity.position.z}`;
res += `\n- health: ${bot.health} / 20`;
if (bot.time.timeOfDay < 6000) {
res += '\n- time: Morning';
} else if (bot.time.timeOfDay < 12000) {
res += '\n- time: Afternoon';
} else {
res += '\n- time: Night';
}
return pad(res);
}
},
{
name: "!inventory",
description: "Get your bot's inventory.",
perform: function (agent) {
return pad(getInventory(agent.bot));
let bot = agent.bot;
let inventory = getInventoryCounts(bot);
let res = 'INVENTORY';
for (const item in inventory) {
if (inventory[item] && inventory[item] > 0)
res += `\n- ${item}: ${inventory[item]}`;
}
if (res == 'INVENTORY') {
res += ': none';
}
return pad(res);
}
},
{
name: "!blocks",
description: "Get the blocks near the bot.",
perform: function (agent) {
return pad(getBlocks(agent.bot));
let bot = agent.bot;
let res = 'NEARBY_BLOCKS';
let blocks = getNearbyBlockTypes(bot);
for (let i = 0; i < blocks.length; i++) {
res += `\n- ${blocks[i]}`;
}
if (blocks.length == 0) {
res += ': none';
}
return pad(res);
}
},
{
name: "!craftable",
description: "Get the craftable items with the bot's inventory.",
perform: function (agent) {
return pad(getCraftable(agent.bot));
const bot = agent.bot;
const table = getNearestBlock(bot, 'crafting_table');
let res = 'CRAFTABLE_ITEMS';
for (const item of getAllItems()) {
let recipes = bot.recipesFor(item.id, null, 1, table);
if (recipes.length > 0) {
res += `\n- ${item.name}`;
}
}
if (res == 'CRAFTABLE_ITEMS') {
res += ': none';
}
return pad(res);
}
},
{
name: "!entities",
description: "Get the nearby players and entities.",
perform: function (agent) {
return pad(getNearbyEntities(agent.bot));
let bot = agent.bot;
let res = 'NEARBY_ENTITIES';
for (const entity of getNearbyPlayerNames(bot)) {
res += `\n- player: ${entity}`;
}
for (const entity of getNearbyMobTypes(bot)) {
res += `\n- mob: ${entity}`;
}
if (res == 'NEARBY_ENTITIES') {
res += ': none';
}
return pad(res);
}
},
{