Compare commits
1 Commits
Author | SHA1 | Date |
---|---|---|
jay | 6a1ab4dd91 | 3 years ago |
12 changed files with 469 additions and 985 deletions
@ -1,16 +0,0 @@ |
|||||||
# See this article for reference: https://help.github.com/articles/dealing-with-line-endings/ |
|
||||||
# Refreshing repo after line ending change: |
|
||||||
# https://help.github.com/articles/dealing-with-line-endings/#refreshing-a-repository-after-changing-line-endings |
|
||||||
|
|
||||||
# Handle line endings automatically for files detected as text |
|
||||||
# and leave all files detected as binary untouched. |
|
||||||
* text=auto |
|
||||||
|
|
||||||
# |
|
||||||
# The above will handle all files NOT found below |
|
||||||
# |
|
||||||
# These files are text and should be normalized (Convert crlf => lf) |
|
||||||
# Use lf as eol for these files |
|
||||||
*.js text eol=lf |
|
||||||
*.ts text eol=lf |
|
||||||
*.json text eol=lf |
|
@ -1,31 +1,30 @@ |
|||||||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. |
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. |
||||||
|
|
||||||
# dependencies |
# dependencies |
||||||
/node_modules |
/node_modules |
||||||
/.pnp |
/.pnp |
||||||
.pnp.js |
.pnp.js |
||||||
|
|
||||||
# testing |
# testing |
||||||
/coverage |
/coverage |
||||||
|
|
||||||
# production |
# production |
||||||
/build |
/build |
||||||
/data |
|
||||||
|
# misc |
||||||
# misc |
.DS_Store |
||||||
.DS_Store |
.env |
||||||
.env |
.env.local |
||||||
.env.local |
.env.development.local |
||||||
.env.development.local |
.env.test.local |
||||||
.env.test.local |
.env.production.local |
||||||
.env.production.local |
/lib/**/*.old |
||||||
/lib/**/*.old |
/lib/**/*.bak |
||||||
/lib/**/*.bak |
|
||||||
|
npm-debug.log* |
||||||
npm-debug.log* |
yarn-debug.log* |
||||||
yarn-debug.log* |
yarn-error.log* |
||||||
yarn-error.log* |
|
||||||
|
# Editor |
||||||
# Editor |
*.swp |
||||||
*.swp |
|
||||||
*.swo |
*.swo |
@ -0,0 +1,217 @@ |
|||||||
|
|
||||||
|
// const mineflayer = require('mineflayer')
|
||||||
|
// let pathfinder
|
||||||
|
// const { pathfinder, Movements, goals } = require('mineflayer-pathfinder')
|
||||||
|
const { Vec3 } = require('vec3') |
||||||
|
// const { GoalFollow, GoalNear } = goals
|
||||||
|
let GoalFollow, GoalNear |
||||||
|
// const mcData = require('minecraft-data')('1.16.5')
|
||||||
|
let mcData |
||||||
|
|
||||||
|
// let bot = mineflayer.createBot()
|
||||||
|
let bot |
||||||
|
let cfg = { bot: null } |
||||||
|
let timer |
||||||
|
let movements |
||||||
|
|
||||||
|
/* let mcData |
||||||
|
bot.once('spawn', () => { |
||||||
|
mcData = require('minecraft-data')(bot.version) |
||||||
|
}) */ |
||||||
|
|
||||||
|
function stopCovering(quiet = cfg.quiet, resetGoal = true) { // This is a function to stop the cover() loop when called
|
||||||
|
if (timer) { |
||||||
|
clearTimeout(timer); |
||||||
|
timer = null; |
||||||
|
} |
||||||
|
if (resetGoal) bot.pathfinder.setGoal(null) |
||||||
|
bot.stopDigging() |
||||||
|
quiet || bot.chat("stopped covering") |
||||||
|
} |
||||||
|
|
||||||
|
function init() { |
||||||
|
const { Movements, goals } = require('mineflayer-pathfinder') |
||||||
|
GoalFollow = goals.GoalFollow |
||||||
|
GoalNear = goals.GoalNear |
||||||
|
movements = new Movements(bot, mcData) |
||||||
|
movements.canDig = true // Lets the bot dig
|
||||||
|
bot.pathfinder.setMovements(movements) |
||||||
|
|
||||||
|
console.info("mycelium start") |
||||||
|
stopCovering(true) |
||||||
|
bot.waitForChunksToLoad(cover) |
||||||
|
} |
||||||
|
|
||||||
|
function cover(timeInt = 1000) { |
||||||
|
if (!Number.isSafeInteger(timeInt)) { |
||||||
|
console.log("cover int maybe goal?", timeInt) |
||||||
|
switch (timeInt.message) { |
||||||
|
case "No path to the goal!": |
||||||
|
console.info("Cover: can't reach") |
||||||
|
cfg.quiet || bot.chat("can't reach") |
||||||
|
break; |
||||||
|
|
||||||
|
default: |
||||||
|
break; |
||||||
|
} |
||||||
|
if (timeInt) { |
||||||
|
timeInt = 5000 |
||||||
|
} |
||||||
|
} else if (timeInt < 300) { |
||||||
|
timeInt = 1000 |
||||||
|
} |
||||||
|
const wool = "white_wool" |
||||||
|
const wool_item = mcData.itemsByName[wool] |
||||||
|
const inventoryWool = bot.inventory.findInventoryItem(wool_item.id) |
||||||
|
// console.info(wool_item.id, inventoryWool)
|
||||||
|
// if (!inventoryWool) return
|
||||||
|
// bot.loadPlugin(pathfinder)
|
||||||
|
|
||||||
|
const myceliumClean = bot.findBlock({ // Const that is a brown_mushroom
|
||||||
|
maxDistance: 6, |
||||||
|
matching: (block) => { |
||||||
|
// First check the type
|
||||||
|
// lol
|
||||||
|
// const { brown_mushroom, red_mushroom, white_wool } = mcData.blocksByName
|
||||||
|
// if ([brown_mushroom.id, red_mushroom.id, white_wool.id].includes(block?.type)) {
|
||||||
|
const { brown_mushroom, red_mushroom, } = mcData.blocksByName |
||||||
|
if ([brown_mushroom.id, red_mushroom.id,].includes(block?.type)) { |
||||||
|
// If position is defined, you can refine the search
|
||||||
|
if (block.position) { |
||||||
|
const blockBelow = bot.blockAt(block.position.offset(0, -1, 0)) |
||||||
|
return blockBelow?.type === mcData.blocksByName.mycelium.id || blockBelow?.type === mcData.blocksByName.spruce_fence.id // Makes sure there is mycelium below
|
||||||
|
} |
||||||
|
return true // otherwise return always true (there is water in the section so it should be checked)
|
||||||
|
} |
||||||
|
return false |
||||||
|
} |
||||||
|
}) |
||||||
|
|
||||||
|
function findMycelium(dist = 5) { |
||||||
|
return bot.findBlock({ |
||||||
|
maxDistance: dist, |
||||||
|
matching: (block) => { |
||||||
|
// First check the type
|
||||||
|
if (block?.type === mcData.blocksByName.mycelium.id) { // Const that is a mycelium block
|
||||||
|
// If position is defined, you can refine the search
|
||||||
|
if (block.position) { |
||||||
|
const blockAbove = bot.blockAt(block.position.offset(0, 1, 0)) |
||||||
|
return !blockAbove || blockAbove?.type === mcData.blocksByName.air.id // Makes sure there is nothing above
|
||||||
|
} |
||||||
|
return true // otherwise return always true (there is water in the section so it should be checked)
|
||||||
|
} |
||||||
|
return false |
||||||
|
} |
||||||
|
}) |
||||||
|
} |
||||||
|
let mycelium = findMycelium() |
||||||
|
|
||||||
|
if (myceliumClean) { |
||||||
|
// bot.dig(myceliumClean, true)
|
||||||
|
bot.dig(myceliumClean) |
||||||
|
} |
||||||
|
|
||||||
|
if (mycelium) { |
||||||
|
timeInt = 500 |
||||||
|
if (bot.heldItem?.type !== wool_item.id) { // Equips wool if not already
|
||||||
|
if (!inventoryWool || inventoryWool.count < 10) { // Checks if there is less than 10 wool in the bots inventory
|
||||||
|
timeInt = 5000 |
||||||
|
console.warn("no wool") |
||||||
|
// const chestLocation = new Vec3(10614, 70, 5350) // Sets chest location
|
||||||
|
const chestLocation = bot.findBlock({ |
||||||
|
maxDistance: 100, |
||||||
|
matching: block => block && block.type === mcData.blocksByName.chest.id |
||||||
|
})?.position // Sets chest location
|
||||||
|
const chestGoal = new GoalNear(chestLocation.x, chestLocation.y, chestLocation.z, 6) // Sets goal to chest location
|
||||||
|
return bot.pathfinder.goto(chestGoal, () => { // Run code below when it gets to the chest
|
||||||
|
bot.lookAt(chestLocation, true) // Looks at chest
|
||||||
|
const chest = bot.openChest(bot.blockAt(chestLocation)) // Sets const to for opening chest
|
||||||
|
chest.once('open', (err) => { // Opens chest
|
||||||
|
if (err) { |
||||||
|
return console.error('Chest error', err) |
||||||
|
} |
||||||
|
const chest_item = chest.items().filter(item => item.type === wool_item.id) |
||||||
|
console.info(chest, chest_item) |
||||||
|
if (chest_item.length > 0) { // Checks that there is stuff in chest
|
||||||
|
try { |
||||||
|
// Pulls out a chest (27 stack) of wool
|
||||||
|
// chest.withdraw(chest_item[0].type, null, 64 * 27)
|
||||||
|
chest.withdraw(chest_item[0].type, null, 64 * 3) |
||||||
|
} catch (error) { |
||||||
|
console.error('Chest withdraw error', error) |
||||||
|
} |
||||||
|
bot.once("close", cover) |
||||||
|
} else { |
||||||
|
console.log('Chest dont have', wool_item) |
||||||
|
cfg.quiet || bot.chat(`Not enough ${wool} in chest`) |
||||||
|
stopCovering() |
||||||
|
} |
||||||
|
setTimeout(chest.close, timeInt) |
||||||
|
}) |
||||||
|
}) |
||||||
|
} else { |
||||||
|
bot.equip(wool_item.id, "hand") |
||||||
|
} |
||||||
|
} else { |
||||||
|
const pos = mycelium.position |
||||||
|
bot.lookAt(pos, true) |
||||||
|
// let tryCount = 0
|
||||||
|
const flooredPos = bot.entity.position.floored() |
||||||
|
if (flooredPos.offset(0, 1, 0).distanceTo(pos) <= 2) { |
||||||
|
bot.setControlState('jump', true) |
||||||
|
if (bot.entity.position.y > mycelium.position.y) { |
||||||
|
bot.placeBlock(mycelium, new Vec3(0, 1, 0), (err) => { |
||||||
|
setTimeout(bot.setControlState, 2000, 'jump', false) |
||||||
|
if (err) { |
||||||
|
console.error('Place (jumped)', err) |
||||||
|
} |
||||||
|
}) |
||||||
|
} |
||||||
|
} else { |
||||||
|
bot.placeBlock(mycelium, new Vec3(0, 1, 0), (err) => { |
||||||
|
if (err) { |
||||||
|
if (err.message !== `No block has been placed : the block is still ${wool}`) { |
||||||
|
return console.error('Place (normal)', err) |
||||||
|
} else { |
||||||
|
return |
||||||
|
} |
||||||
|
} |
||||||
|
}) |
||||||
|
} |
||||||
|
} |
||||||
|
} else { |
||||||
|
mycelium = findMycelium(100) |
||||||
|
if (mycelium) { |
||||||
|
const pos = mycelium.position |
||||||
|
const goal = new GoalNear(pos.x, pos.y, pos.z, 3) |
||||||
|
stopCovering(true) |
||||||
|
timeInt = 2000 |
||||||
|
return bot.pathfinder.goto(goal, cover) |
||||||
|
} else { |
||||||
|
stopCovering(true) |
||||||
|
return cfg.quiet || bot.chat("no uncovered mycelium nearby") |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
timer = setTimeout(cover, timeInt, timeInt) |
||||||
|
} |
||||||
|
|
||||||
|
function command(params) { |
||||||
|
stopCovering(true) |
||||||
|
cover() |
||||||
|
} |
||||||
|
|
||||||
|
const load = (config) => { |
||||||
|
cfg = config |
||||||
|
bot = cfg.bot |
||||||
|
mcData = bot.mcData || (bot.mcData = require('minecraft-data')(bot.version)) |
||||||
|
|
||||||
|
pathfinder = bot.pathfinder || bot.loadPlugin(require('mineflayer-pathfinder').pathfinder) |
||||||
|
init() |
||||||
|
} |
||||||
|
|
||||||
|
const unload = () => { |
||||||
|
stopCovering(true) |
||||||
|
} |
||||||
|
|
||||||
|
module.exports = { load, unload, command, cover, stopCovering } |
@ -1,479 +1,115 @@ |
|||||||
// import { createMachine, interpret, InterpreterStatus } from "xstate";
|
// Load your dependency plugins.
|
||||||
const { createMachine, interpret, InterpreterStatus } = require('xstate'); |
|
||||||
// import { access, mkdir, writeFile, readFile } from "fs";
|
const {pathfinder} = require('mineflayer-pathfinder') |
||||||
const { access, mkdir, writeFile, readFile } = require('fs'); |
// bot.loadPlugin(require('prismarine-viewer').mineflayer)
|
||||||
// ANGRAM_PREFIX='MINECRAFT'
|
// const mineflayerViewer = require('prismarine-viewer').mineflayer
|
||||||
const { MINECRAFT_DATA_FOLDER } = process.env || require("dotenv-packed").parseEnv().parsed; |
|
||||||
const storage_dir = MINECRAFT_DATA_FOLDER || './data/' + "/sm/"; |
// Import required behaviors.
|
||||||
// import { createBot } from "mineflayer"
|
const { |
||||||
// let { pathfinder, Movements, goals } = require('mineflayer-pathfinder')
|
StateTransition, |
||||||
// let cfg
|
BotStateMachine, |
||||||
// let bot = createBot({ username: 'statebot' })
|
EntityFilters, |
||||||
let bot; |
BehaviorFollowEntity, |
||||||
let quiet; |
BehaviorLookAtEntity, |
||||||
let updateRate = 20; |
BehaviorGetClosestEntity, |
||||||
const machines = { |
NestedStateMachine, |
||||||
list: {}, |
BehaviorIdle, |
||||||
running: {}, |
StateMachineWebserver, |
||||||
}; |
} = require("mineflayer-statemachine"); |
||||||
let webserver; |
|
||||||
let cfg = { |
// TODO chat
|
||||||
statemachine: { |
|
||||||
webserver: null, |
|
||||||
// quiet: true,
|
|
||||||
quiet: quiet, |
// wait for our bot to login.
|
||||||
list: {}, |
function statemachineInit() { |
||||||
running: {}, |
cfg.botAddress = new RegExp(`^${bot.username} (!.+)`) |
||||||
draft: null, |
// This targets object is used to pass data between different states. It can be left empty.
|
||||||
recent: null, |
const targets = new Object(); |
||||||
// debug: null,
|
|
||||||
debug: true, |
// Create our states
|
||||||
updateRate: updateRate |
const getClosestPlayer = new BehaviorGetClosestEntity( |
||||||
} |
bot, targets, entity => EntityFilters().PlayersOnly(entity) && bot.entity.position.distanceTo(entity.position) <= 50 ); // && a.username !== bot.username);
|
||||||
// FIXME temp variables to satisfy typescript autocomplete
|
const followPlayer = new BehaviorFollowEntity(bot, targets); |
||||||
// , quiet: null
|
const lookAtPlayer = new BehaviorLookAtEntity(bot, targets); |
||||||
, |
const stay = new BehaviorIdle(); |
||||||
bot: bot, |
|
||||||
plugins: { statemachine: null } |
// Create our transitions
|
||||||
}; |
const transitions = [ |
||||||
// Edit your machine(s) here
|
|
||||||
function init(smName = "dummy", webserver) { |
// We want to start following the player immediately after finding them.
|
||||||
access(storage_dir, err => { |
// Since getClosestPlayer finishes instantly, shouldTransition() should always return true.
|
||||||
if (err?.code === 'ENOENT') { |
new StateTransition({ |
||||||
mkdir(storage_dir, e => e && console.warn("sm init: create dir", e)); |
parent: getClosestPlayer, |
||||||
} |
child: followPlayer, |
||||||
else if (err) { |
onTransition: (quiet) => quiet || bot.chat(`Hi ${targets.entity.username}!`), |
||||||
console.warn("sm init: create dir", err); |
shouldTransition: () => bot.entity.position.distanceTo(targets.entity.position) <= 50, |
||||||
} |
// shouldTransition: () => getClosestPlayer.distanceToTarget() < 100 || console.info("player too far!") && false,
|
||||||
}); |
}), |
||||||
// const machine = newSM(smName)
|
|
||||||
// machine.states.idle.on.TOGGLE = "start"
|
// If the distance to the player is less than two blocks, switch from the followPlayer
|
||||||
// machine.states.start.on.TOGGLE = "idle"
|
// state to the lookAtPlayer state.
|
||||||
const machine = createMachine({ |
new StateTransition({ |
||||||
id: smName, |
parent: followPlayer, |
||||||
initial: "idle", |
child: lookAtPlayer, |
||||||
states: { |
// onTransition: () => console.log(targets),
|
||||||
idle: { |
shouldTransition: () => followPlayer.distanceToTarget() < 2, |
||||||
on: { TOGGLE: "start", NEXT: "start", PREV: "look", STOP: "finish" } |
}), |
||||||
}, |
|
||||||
start: { |
// If the distance to the player is more than two blocks, switch from the lookAtPlayer
|
||||||
on: { TOGGLE: "look", NEXT: "look", PREV: "idle" }, |
// state to the followPlayer state.
|
||||||
entry: 'lookAtPlayerOnce', |
new StateTransition({ |
||||||
}, |
parent: lookAtPlayer, |
||||||
look: { |
child: followPlayer, |
||||||
on: { TOGGLE: "idle", NEXT: "idle", PREV: "start" }, |
shouldTransition: () => lookAtPlayer.distanceToTarget() >= 5, |
||||||
// entry: ['look', 'blah']
|
}), |
||||||
// entry: 'lookAtPlayerOnce',
|
new StateTransition({ |
||||||
activities: 'lookAtPlayer', |
parent: lookAtPlayer, |
||||||
meta: { debug: true } |
child: stay, |
||||||
}, |
onTransition: () => bot.chat("ok, staying"), |
||||||
finish: { |
// shouldTransition: () => true,
|
||||||
type: 'final' |
}), |
||||||
}, |
new StateTransition({ |
||||||
}, |
parent: stay, |
||||||
on: { START: '.start', STOP: '.idle' }, |
child: getClosestPlayer, |
||||||
meta: { debug: true }, |
// shouldTransition: () => Math.random() > 0.01,
|
||||||
context: { player: null, rate: updateRate }, |
// shouldTransition: () => Math.random() > 0.1 && getClosestPlayer.distanceToTarget() < 2,
|
||||||
}, { |
}), |
||||||
actions: { |
]; |
||||||
// action implementation
|
|
||||||
lookAtPlayerOnce: (context, event) => { |
// Now we just wrap our transition list in a nested state machine layer. We want the bot
|
||||||
const player = context?.player && (context?.player?.isValid || context?.player?.entity?.isValid) |
// to start on the getClosestPlayer state, so we'll specify that here.
|
||||||
|| bot.nearestEntity(entity => entity.type === 'player'); |
const rootLayer = new NestedStateMachine(transitions, getClosestPlayer, stay); |
||||||
if (player?.position || player?.entity) { |
|
||||||
context.player = player; |
// We can start our state machine simply by creating a new instance.
|
||||||
bot.lookAt((player.entity || player).position.offset(0, 1, 0)); |
cfg.stateMachines.follow = new BotStateMachine(bot, rootLayer); |
||||||
} |
const webserver = new StateMachineWebserver(bot, cfg.stateMachines.follow); |
||||||
} |
webserver.startServer(); |
||||||
}, |
|
||||||
activities: { |
// mineflayerViewer(bot, { port: 3000 })
|
||||||
lookAtPlayer: (context, event) => { |
// const path = [bot.entity.position.clone()]
|
||||||
const player = (context?.player?.isValid || context?.player?.entity?.isValid) && context?.player |
// bot.on('move', () => {
|
||||||
|| bot.nearestEntity(entity => entity.type === 'player' && entity.isValid); |
// if (path[path.length - 1].distanceTo(bot.entity.position) > 1) {
|
||||||
// TODO check pos every event?
|
// path.push(bot.entity.position.clone())
|
||||||
if (player?.position || player?.entity) { |
// bot.viewer.drawLine('path', path)
|
||||||
context.player = player; |
// }
|
||||||
function looks() { |
// })
|
||||||
bot.lookAt((player.entity || player).position.offset(0, 1, 0)); |
|
||||||
} |
|
||||||
bot.on("time", looks); |
|
||||||
return () => bot.off("time", looks); |
|
||||||
} |
|
||||||
else { |
|
||||||
quiet || bot.chat("look: no valid players"); |
|
||||||
// TODO use xstate logger
|
|
||||||
context.debug && console.log("sm: no valid player", this, context, player); |
|
||||||
} |
|
||||||
} |
|
||||||
}, |
|
||||||
delays: { |
|
||||||
/* ... */ |
|
||||||
}, |
|
||||||
guards: { |
|
||||||
/* ... */ |
|
||||||
}, |
|
||||||
services: { |
|
||||||
/* ... */ |
|
||||||
} |
|
||||||
}); |
|
||||||
console.log("sm init: machine", machine); |
|
||||||
const service = runSM(saveSM(machine)); |
|
||||||
if (service?.send) { |
|
||||||
setTimeout(service.send, 200, "TOGGLE"); |
|
||||||
// setTimeout(service.send, 400, "TOGGLE")
|
|
||||||
} |
|
||||||
else { |
|
||||||
console.warn("sm init: service", service); |
|
||||||
} |
|
||||||
} |
} |
||||||
function newSM(smName = "sm_" + Object.keys(cfg.statemachine.list).length) { |
|
||||||
smName = smName.replace(/\s+/, '_'); |
const load = (config) => { |
||||||
if (cfg.statemachine.list[smName]) { |
cfg = config |
||||||
console.warn("sm exists", smName); |
bot = cfg.bot |
||||||
quiet || bot.chat(`sm ${smName} already exists, edit or use another name instead`); |
// cfg.inventory = {
|
||||||
return; |
// auto: true,
|
||||||
} |
// quiet: false
|
||||||
const machine = createMachine({ |
// }
|
||||||
id: smName, |
// bot.on('chat', inventory)
|
||||||
initial: "start", |
bot.loadPlugin(pathfinder) |
||||||
// TODO use history states for PAUSE and RESUME
|
// statemachineInit()
|
||||||
states: { idle: { on: { START: "start" } }, start: { on: { STOP: "idle" } } } |
|
||||||
}); |
|
||||||
cfg.statemachine.draft = machine; |
|
||||||
return machine; |
|
||||||
} |
} |
||||||
function saveSM(machine = cfg.statemachine.draft) { |
|
||||||
if (!machine?.id) { |
const unload = () => { |
||||||
console.warn("sm save: invalid", machine); |
// bot.off('chat', inventory)
|
||||||
quiet || bot.chat("sm: couldn't save, invalid"); |
|
||||||
return; |
|
||||||
} |
|
||||||
// TODO do tests and validation
|
|
||||||
// 1. ensure default states [start, idle]
|
|
||||||
// 2. ensure closed cycle from start to idle
|
|
||||||
cfg.statemachine.list[machine.id] = machine; |
|
||||||
if (machine.id === cfg.statemachine.draft?.id) { |
|
||||||
cfg.statemachine.draft = null; |
|
||||||
} |
|
||||||
writeFile( |
|
||||||
// TODO
|
|
||||||
// `${storage_dir}/${player}/${machine.id}.json`
|
|
||||||
`${storage_dir}/${machine.id}.json`, |
|
||||||
// TODO decide which data to store
|
|
||||||
// https://xstate.js.org/docs/guides/states.html#persisting-state
|
|
||||||
// JSON.stringify(machine.toJSON),
|
|
||||||
// JSON.stringify(machine.states.toJSON), // + activities, delays, etc
|
|
||||||
JSON.stringify({ config: machine.config, context: machine.context }), e => e && console.log("sm load sm: write file", e)); |
|
||||||
// return run ? runSM(machine) : machine
|
|
||||||
return machine; |
|
||||||
} |
} |
||||||
function loadSM(name, run = true) { |
|
||||||
//: StateMachine<any, any, any> {
|
module.exports = { load, unload } |
||||||
readFile( |
|
||||||
// readFileSync(
|
|
||||||
// TODO
|
|
||||||
// `${storage_dir}/${player}/${machine.id}.json`
|
|
||||||
`${storage_dir}/${name}.json`, afterRead |
|
||||||
// JSON.stringify(machine.toJSON),
|
|
||||||
); |
|
||||||
function afterRead(err, jsonString) { |
|
||||||
if (err) { |
|
||||||
console.warn("sm load sm: read file", err); |
|
||||||
return; |
|
||||||
} |
|
||||||
else { |
|
||||||
const machine = createMachine(JSON.parse(jsonString).config); |
|
||||||
// TODO do tests and validation
|
|
||||||
// 1. ensure default states [start, idle]
|
|
||||||
// 2. ensure closed cycle from start to idle
|
|
||||||
cfg.statemachine.list[machine.id] = machine; |
|
||||||
if (machine.id === cfg.statemachine.draft?.id) { |
|
||||||
cfg.statemachine.draft = machine; |
|
||||||
} |
|
||||||
if (run) { |
|
||||||
runSM(machine); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
// return run ? runSM(machine) : machine
|
|
||||||
// return machine
|
|
||||||
} |
|
||||||
// function isInterpreter(SMorService: StateMachine<any, any, any> | Interpreter<any>): SMorService is Interpreter<any> {
|
|
||||||
// return (SMorService as Interpreter<any>).start !== undefined;
|
|
||||||
// }
|
|
||||||
function getSM(name = cfg.statemachine.draft || cfg.statemachine.recent, asService = false, quiet = false) { |
|
||||||
const machine = typeof name === "string" ? cfg.statemachine.list[name] |
|
||||||
: name; |
|
||||||
if (!machine) { |
|
||||||
console.warn("sm get: doesn't exist", name); |
|
||||||
cfg.statemachine.quiet || bot.chat(`sm ${name} doesn't exist`); |
|
||||||
return; |
|
||||||
} |
|
||||||
if (asService) { |
|
||||||
const service = cfg.statemachine.running[machine?.id]; |
|
||||||
if (!service) { |
|
||||||
quiet || console.warn("sm get: already stopped", machine); |
|
||||||
quiet || cfg.statemachine.quiet || bot.chat(`sm ${machine?.id} isn't running`); |
|
||||||
return interpret(machine); |
|
||||||
} |
|
||||||
return service; |
|
||||||
} |
|
||||||
else { |
|
||||||
// return machine.machine ? machine.machine : machine
|
|
||||||
return machine; |
|
||||||
} |
|
||||||
} |
|
||||||
function runSM(name = getSM(undefined, undefined, true), player // or supervisor?
|
|
||||||
, restart = false) { |
|
||||||
if (!name) |
|
||||||
return; |
|
||||||
const service = getSM(name, true, true); |
|
||||||
if (!service) |
|
||||||
return; |
|
||||||
const machine = service.machine; |
|
||||||
if (!machine) |
|
||||||
return; |
|
||||||
switch (service.status) { |
|
||||||
case InterpreterStatus.Running: |
|
||||||
if (!restart) { |
|
||||||
console.warn("sm run: already running", service.id); |
|
||||||
quiet || bot.chat(`sm ${service.id} already running`); |
|
||||||
return service; |
|
||||||
} |
|
||||||
stopSM(machine); |
|
||||||
case InterpreterStatus.NotStarted: |
|
||||||
case InterpreterStatus.Stopped: |
|
||||||
break; |
|
||||||
default: |
|
||||||
console.warn("sm run: unknown status:", service.status, service); |
|
||||||
return; |
|
||||||
break; |
|
||||||
} |
|
||||||
if (cfg.statemachine.debug || machine.meta?.debug) { |
|
||||||
service.onTransition((state) => { |
|
||||||
quiet || bot.chat(`sm trans: ${machine.id}, ${state.value}`); |
|
||||||
quiet || state.meta?.debug && bot.chat(`sm next events: ${machine.id}, ${state.nextEvents}`); |
|
||||||
console.log("sm debug: trans", state.value, state); |
|
||||||
}).onDone((done) => { |
|
||||||
quiet || bot.chat(`sm done: ${machine.id}, ${done}`); |
|
||||||
console.log("sm debug: done", done.data, done); |
|
||||||
}); |
|
||||||
} |
|
||||||
cfg.statemachine.running[machine.id] = service; |
|
||||||
cfg.statemachine.recent = machine; |
|
||||||
service.start(); |
|
||||||
// return machine
|
|
||||||
return service; |
|
||||||
} |
|
||||||
function stopSM(name = getSM(), quiet = false) { |
|
||||||
let service = getSM(name, true, quiet); |
|
||||||
if (!service) |
|
||||||
return; |
|
||||||
const machine = service.machine; |
|
||||||
switch (service.status) { |
|
||||||
case InterpreterStatus.NotStarted: |
|
||||||
case InterpreterStatus.Stopped: |
|
||||||
console.log("sm stop status", service.status, service.id, cfg.statemachine.running[service.id]); |
|
||||||
// TODO check if any bugs
|
|
||||||
case InterpreterStatus.Running: |
|
||||||
break; |
|
||||||
default: |
|
||||||
console.warn("sm stop: unknown status:", service.status); |
|
||||||
break; |
|
||||||
} |
|
||||||
service?.stop?.(); |
|
||||||
cfg.statemachine.running[machine.id] = null; |
|
||||||
delete cfg.statemachine.running[machine.id]; |
|
||||||
// return machine
|
|
||||||
return service; |
|
||||||
} |
|
||||||
function actionSM(action, name = getSM()) { |
|
||||||
if (!action) { |
|
||||||
return console.warn("sm action", action); |
|
||||||
} |
|
||||||
let service = getSM(name, true, true); |
|
||||||
if (service.status !== InterpreterStatus.Running) |
|
||||||
return; |
|
||||||
// const machine = service.machine
|
|
||||||
service.send(action.toUpperCase()); |
|
||||||
} |
|
||||||
function stepSM(command = "", ...message_parts) { |
|
||||||
let service = getSM(undefined, true); |
|
||||||
if (!service) |
|
||||||
return; |
|
||||||
if (!service.send) { |
|
||||||
console.warn("sm step: can't send", service.machine); |
|
||||||
// TODO start a temporary service to interpret
|
|
||||||
quiet || bot.chat("sm: step doesn't support machines that aren't running yet"); |
|
||||||
return; |
|
||||||
} |
|
||||||
if (service?.status !== InterpreterStatus.Running) { |
|
||||||
console.warn("sm step: machine not running, attempting start", service); |
|
||||||
runSM; |
|
||||||
} |
|
||||||
// const machine = service.machine
|
|
||||||
switch (command) { |
|
||||||
case "edit": |
|
||||||
// maybe `edit <type>`, like `add`?
|
|
||||||
// where type:
|
|
||||||
// context
|
|
||||||
// action
|
|
||||||
// timeout | all timeout
|
|
||||||
break; |
|
||||||
case "undo": |
|
||||||
break; |
|
||||||
case "del": |
|
||||||
break; |
|
||||||
case "p": |
|
||||||
case "prev": |
|
||||||
service?.send("PREV"); |
|
||||||
break; |
|
||||||
case "add": |
|
||||||
// maybe `add <type>`?
|
|
||||||
// where type:
|
|
||||||
// context
|
|
||||||
// action
|
|
||||||
// timeout
|
|
||||||
case "new": |
|
||||||
break; |
|
||||||
// case "with":
|
|
||||||
// console.log(this)
|
|
||||||
// stepSM(getSM(message_parts[0], true, true), ...message_parts.slice(1))
|
|
||||||
// break;
|
|
||||||
case "help": |
|
||||||
quiet || bot.chat("![sm ]step [ p(rev) | n(ext) ]"); |
|
||||||
break; |
|
||||||
case "n": |
|
||||||
case "next": |
|
||||||
default: |
|
||||||
service?.send("NEXT"); |
|
||||||
quiet || bot.chat("stepped"); |
|
||||||
break; |
|
||||||
} |
|
||||||
} |
|
||||||
function tickSM(time = bot.time.timeOfDay, rate = 20) { |
|
||||||
if (time % rate !== 0) { |
|
||||||
return; |
|
||||||
} |
|
||||||
console.log("sm tick", rate, time); |
|
||||||
} |
|
||||||
function debugSM(name = getSM()) { |
|
||||||
if (!name) |
|
||||||
return; |
|
||||||
let service = getSM(name, true); |
|
||||||
if (!service) |
|
||||||
return; |
|
||||||
const machine = service.machine; |
|
||||||
machine.meta.debug = !!!machine.meta.debug; |
|
||||||
console.info("sm debug", machine.meta, service, machine); |
|
||||||
cfg.statemachine.quiet || machine.meta.debug && bot.chat("sm debug: " + machine.id); |
|
||||||
} |
|
||||||
function command(message_parts) { |
|
||||||
const message_parts2 = message_parts.slice(1); |
|
||||||
switch (message_parts[0]) { |
|
||||||
case "add": |
|
||||||
command(["new"].concat(message_parts2)); |
|
||||||
break; |
|
||||||
case "finish": |
|
||||||
case "done": |
|
||||||
case "end": |
|
||||||
command(["save"].concat(message_parts2)); |
|
||||||
break; |
|
||||||
case "do": |
|
||||||
case "load": |
|
||||||
case "start": |
|
||||||
command(["run"].concat(message_parts2)); |
|
||||||
break; |
|
||||||
case "debug": |
|
||||||
switch (message_parts[1]) { |
|
||||||
case "sm": |
|
||||||
case "global": |
|
||||||
case "meta": |
|
||||||
cfg.statemachine.debug = !!!cfg.statemachine.debug; |
|
||||||
quiet || bot.chat(`sm debug: ${cfg.statemachine.debug}`); |
|
||||||
break; |
|
||||||
} |
|
||||||
case "new": |
|
||||||
case "save": |
|
||||||
case "run": |
|
||||||
case "step": |
|
||||||
case "stop": |
|
||||||
// temp
|
|
||||||
case "action": |
|
||||||
switch (message_parts2.length) { |
|
||||||
case 0: |
|
||||||
console.warn(`sm ${message_parts[0]}: no name, using defaults`); |
|
||||||
case 1: |
|
||||||
// FIXME `this` doesn't work always
|
|
||||||
(this.runSM && this || cfg.plugins.statemachine)[message_parts[0] + "SM"](...message_parts2); |
|
||||||
break; |
|
||||||
default: |
|
||||||
if (["action"].includes(message_parts[0])) { |
|
||||||
(this.runSM && this || cfg.plugins.statemachine)[message_parts[0] + "SM"](...message_parts2); |
|
||||||
} |
|
||||||
else { |
|
||||||
console.warn(`sm ${message_parts[0]}: more than 1 arg passed`, message_parts2); |
|
||||||
} |
|
||||||
break; |
|
||||||
} |
|
||||||
break; |
|
||||||
case "undo": |
|
||||||
break; |
|
||||||
case "list": |
|
||||||
case "status": |
|
||||||
// TODO current/recent, updateRate
|
|
||||||
const { list, running } = cfg.statemachine; |
|
||||||
console.log("sm list", running, list); |
|
||||||
quiet || bot.chat(`${Object.keys(running).length} of ${Object.keys(list).length} active: ${Object.keys(running)}` |
|
||||||
+ (message_parts[1] === "all" ? `${Object.keys(list)}` : '')); |
|
||||||
break; |
|
||||||
case "quiet": |
|
||||||
quiet = cfg.statemachine.quiet = !!!cfg.statemachine.quiet; |
|
||||||
quiet || bot.chat(`sm: ${cfg.statemachine.quiet ? "" : "not "}being quiet`); |
|
||||||
break; |
|
||||||
default: |
|
||||||
// TODO general helper from declarative commands object
|
|
||||||
quiet || bot.chat(`sm help: !sm [new | step| save | run | list | quiet | debug]`); |
|
||||||
console.warn("sm unknown command", message_parts); |
|
||||||
break; |
|
||||||
} |
|
||||||
return true; |
|
||||||
} |
|
||||||
function load(config) { |
|
||||||
webserver = cfg.statemachine.webserver = config.statemachine?.webserver || webserver; |
|
||||||
config.statemachine = cfg.statemachine || { |
|
||||||
webserver: null, |
|
||||||
// quiet: true,
|
|
||||||
quiet: false, |
|
||||||
list: {}, |
|
||||||
running: {}, |
|
||||||
draft: null, |
|
||||||
recent: null, |
|
||||||
// debug: null,
|
|
||||||
debug: true, |
|
||||||
updateRate: updateRate |
|
||||||
}; |
|
||||||
cfg = config; |
|
||||||
bot = cfg.bot; |
|
||||||
// pathfinder = bot.pathfinder || bot.loadPlugin(require('mineflayer-pathfinder').pathfinder)
|
|
||||||
// mcData = bot.mcData || (bot.mcData = require('minecraft-data')(bot.version))
|
|
||||||
init(undefined, webserver); |
|
||||||
updateRate = cfg.statemachine.updateRate || updateRate; |
|
||||||
bot.on('time', tickSM); |
|
||||||
// bot.once('time', tickSM, 5)
|
|
||||||
console.log("sm load", cfg.statemachine); |
|
||||||
} |
|
||||||
function unload() { |
|
||||||
const { list, running } = cfg.statemachine; |
|
||||||
bot.off('time', tickSM); |
|
||||||
Object.keys(running).forEach(sm => { |
|
||||||
stopSM(sm); |
|
||||||
}); |
|
||||||
// delete cfg.statemachine;
|
|
||||||
cfg.statemachine = null; |
|
||||||
console.log("sm unload: deleted", cfg.statemachine); |
|
||||||
} |
|
||||||
module.exports = { |
|
||||||
load, unload, command, init, |
|
||||||
newSM, saveSM, loadSM, runSM, stopSM, actionSM, stepSM, debugSM |
|
||||||
}; |
|
Loading…
Reference in new issue