Compare commits

...

45 Commits

Author SHA1 Message Date
jay
feb0b0927a feat(informer): add villager profession details
Provide names in place of numbers for villager profession.
Implementation uses an enum
2021-05-15 18:37:24 +05:00
jay
8276e68489 feat(informer): add block info based on relative position for players 2021-05-11 17:59:26 +05:00
jay
2b7163bef3 feat(informer): add block info based on other players
Now should work for position of players other than the callee.

Alternative implementation could use `entity` function.
This allows to also work for named entities as a bonus.
2021-05-11 17:54:19 +05:00
jay
a0b4641f37 feat(informer): add entity detail by type and kind for some entities
So far:
- mobs
 - villager
- object
 - drops
2021-05-11 17:42:31 +05:00
jay
186d6ac3d2 feat(informer): 🚸 add more detail to entity info 2021-05-11 17:36:41 +05:00
jay
984c9490c3 fix(informer): 🥅 make player undefined if nul
This allows to fallback to defaults in functions
2021-05-11 17:34:25 +05:00
jay
3379f75ab9 feat(informer): 🚸 make entity search case insensitive 2021-05-11 17:32:38 +05:00
jay
69b1ab0b0b feat(informer): add reverse mcdata lookup by block or item id 2021-05-11 14:44:22 +05:00
jay
3c9d62441f feat(informer): add block info based on player's relative position
Relative position includes simple words like "feet", "standing", "head", etc
2021-05-11 10:39:53 +05:00
jay
665102e19c fix(informer): 🚑 remove extra let cfg declaration left over from refactor 2021-05-11 09:46:07 +05:00
jay
db459f52e6 fix(informer): ✏️ fix typo in isVec3 check function 2021-05-11 09:32:54 +05:00
jay
360eeff02f feat(informer): add item info based on player
Change the parameter order of `item` function to accomodate this
2021-05-11 07:32:05 +05:00
jay
25faac2f4c feat(informer): add command me for block info based on player position 2021-05-10 18:37:48 +05:00
jay
ccf0c598e8 feat(informer): add block info based on player position or manually provided coords
This paves the way to get block info based on positions relative from the player
2021-05-07 16:27:44 +05:00
jay
050e2b3bd9 feat(informer): 🚸 add sub-command for module level quiet 2021-05-07 12:53:24 +05:00
jay
e879d1f4ad refactor(informer): ♻️ reorder config loading 2021-05-07 12:40:17 +05:00
jay
df193da779 feat(command): 🚧 add stub for bot._client events
Not sure how these are different:
- `bot._client.on("chat")`
- `bot.on("message(str)")`
2021-05-06 13:15:38 +05:00
jay
269d258763 feat(command): use a simpler text based system for receiving server commands 2021-05-06 13:11:31 +05:00
jay
cbb105fe49 fix(command): 🚸 make bot address regex (prefix) more lenient
Should also accept server commands now on address
2021-05-03 07:20:33 +05:00
jay
356f83e39b fix(command): 👽 fix whisper event api change with an extra parameter
Whisper also now includes who it was whispered to
2021-05-03 07:14:45 +05:00
jay
5b3804893b feat(command): fix to show web (including extra [MODE]) messages
There's no way to remove `bot.addChatPattern`, so only does once on load
2021-04-29 08:37:46 +05:00
jay
9f6fea2423 feat(command): 🔊 add system (non-chat) messages to console log
This includes anything that doesn't return a `.text`.
Such as an `extra`, `translate` or other `json` formatted message.
Ex:
- afk
- errors
- player leave and joins
2021-04-29 08:24:51 +05:00
jay
3686bab167 fix: 🐛 fix plugin reload for multi-file/dir based plugins
Now, it also (un-)loads the whole plugin (sub and master).

This appears to be the most stable option.
Loading behaviour is unchanged from before (load only top level plugin).
However as a precaution, loading tries to unload bad plugins on error.
This helps to not end up in a bad state during reload.
2021-04-29 08:14:40 +05:00
jay
e0c477a46f feat: 🔨 make fs.watch recursive, so it reloadplugin works also on directories 2021-04-27 11:47:25 +05:00
jay
6046123074 build: ⬆️ update deps 2021-04-25 07:13:55 +05:00
jay
ae1f7cf269 feat: 🔧 extract the hardcoded admin and mod list out into environment variables 2021-04-23 06:07:29 +05:00
jay
afd2e002df feat(mover): add new pathfinder Movements
- safeMove without parkour
- aggresiveMove that digs
2021-04-19 13:34:22 +05:00
jay
3c5ec6b360 fix(mover): ✏️ fix !go up moving left instead 2021-04-19 13:31:36 +05:00
jay
b71728e503 feat(mover): add Mule to ridable mobs 2021-04-19 13:25:04 +05:00
jay
d001280383 fix: 🥅 fix a plugin loading error
While reloading, plugin cache may not be deleted.
This happens when a loaded plugin encounters an error during reload.
This causes a deadlock in the unloading code.

So make sure cache is always deleted:
- ignore when an `unload()` function doesn't exist
- delete cache even if there are no `exports`
2021-04-19 13:15:25 +05:00
jay
f50e388c39 feat: 🔊 fix path in plugin reload log so that it actually works 2021-04-14 09:59:48 +05:00
jay
135ce6567b fix(mover): 🐛 attempt to fix move while riding vehicle
Try to give a larger range and more freedom to see if other values work.
Specifically, attempt turning.
No test on vehicles or mobs appear to behave consistently so far.
Tried: pigs, minecarts, horse.
2021-04-14 09:57:36 +05:00
jay
e5803eee59 fix(mover): ✏️ fix missed variable during rename 2021-04-11 23:30:34 +05:00
jay
1b21fcb096 refactor(mover): ♻️ use a better name for default move 2021-04-07 07:02:40 +05:00
jay
4b3e58ceb3 fix(mover): 🐛 attempt to fix move while riding vehicle
This never worked previously.
Using one of `-1 0 1` instead of a larger number appears to work.
2021-04-07 06:42:11 +05:00
jay
8e854a0a2f refactor(mover): 🔊 improve debug logging code 2021-04-07 06:37:26 +05:00
jay
c42a3a2304 feat(mover): add an action to move / run away from an entity or goal
based on `pathfinder` goal `GoalInvert`; behaves slightly strange
2021-04-07 06:26:16 +05:00
jay
b453b7d6bd feat(mover): add shortcuts and aliases for the jump command 2021-04-05 18:16:13 +05:00
jay
6b79f1fc60 refactor(plugins): ♻️ change plugin load order 2021-04-05 17:47:06 +05:00
jay
6e1ef5aada refactor(statemachine): 🔇 remove debug
code
2021-04-05 17:43:48 +05:00
jay
e2ae7e5ad2 fix(statemachine): ✏️ fix SM action function sending Lower instead of Upper case commands 2021-04-03 15:16:46 +05:00
jay
42138a421b fix(statemachine): 🥅 fix for lookatPlayer when no or invalid player
Fixes the case when player isn't nearby or entity is invalid due to teleport
2021-04-03 15:15:08 +05:00
jay
aded1e4193 style: 🎨 fix crlf -> lf for real this time, using .gitattributes
Add a .gitattributes file to permanentyl fix crlf issues.

For explanantion:
https://adaptivepatchwork.com/2012/03/01/mind-the-end-of-your-line/#the-new-system
2021-03-24 15:07:45 +05:00
jay
60394e38eb refactor: ♻️ replace add with vec3.offset
This avoids having to import and creat a `new vec3.v()`
2021-03-24 00:40:54 +05:00
jay
22490f7ec1 fix(command): 🐛 fix bot address regex returning undefined
Waits till spawn to make sure `bot.username` is definitely initialized
2021-03-23 23:10:58 +05:00
9 changed files with 922 additions and 586 deletions

16
.gitattributes vendored Normal file
View File

@@ -0,0 +1,16 @@
# 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

60
.gitignore vendored
View File

@@ -1,31 +1,31 @@
# 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 /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

View File

@@ -1,8 +1,8 @@
const env = require("dotenv-packed").parseEnv().parsed const env = require("dotenv-packed").parseEnv().parsed
const fs = require('fs'); const fs = require('fs');
let cfg = { const cfg = {
admin: "Applezaus", admin: process.env.MINECRAFT_PLAYER_ADMIN || env.MINECRAFT_PLAYER_ADMIN || console.warn("main: bot admin user not provided"),
mods: ["Applezaus", "tanner6", "Angram42", "[WEB] Angram42", "[WEB] Applezaus"], mods: process.env.MINECRAFT_PLAYER_MODS || env.MINECRAFT_PLAYER_MODS || [], // json array,
stateMachines: {} stateMachines: {}
} }
@@ -29,25 +29,28 @@ cfg.botOptions = options
let plugins = {} let plugins = {}
function loadplugin(pluginname, pluginpath) { function loadplugin(pluginname, pluginpath = './plugins/' + pluginname) {
try { try {
plugins[pluginname] = require(pluginpath) plugins[pluginname] = require(pluginpath)
plugins[pluginname].load(cfg) plugins[pluginname].load(cfg)
} catch (error) { } catch (error) {
if (error.code == 'MODULE_NOT_FOUND') { if (error.code == 'MODULE_NOT_FOUND') {
console.warn('plugin not used:', pluginpath) console.warn('plugin not used:', pluginpath)
} else if (plugins[pluginname] && !plugins[pluginname].load) {
unloadplugin(pluginname, pluginpath)
} else { } else {
console.error(error) console.error(error)
} }
} }
} }
function unloadplugin(pluginname, pluginpath) { function unloadplugin(pluginname, pluginpath = './plugins/' + pluginname) {
pluginpath = pluginpath ? pluginpath : './plugins/' + pluginname
const plugin = require.resolve(pluginpath) const plugin = require.resolve(pluginpath)
try { try {
if (plugin && require.cache[plugin]) { if (plugin && require.cache[plugin]) {
require.cache[plugin].exports.unload() // `unload()` isn't exported sometimes,
// when plugin isn't properly loaded
require.cache[plugin].exports?.unload?.()
delete plugins[pluginname] delete plugins[pluginname]
delete require.cache[plugin] delete require.cache[plugin]
} }
@@ -56,20 +59,28 @@ function unloadplugin(pluginname, pluginpath) {
} }
} }
reloadplugin = (event, filename, pluginpath) => { function reloadplugin(event, filename, pluginpath = './plugins/') {
if (!/\.js$/.test(filename)) { return } if (!/\.js$/.test(filename)) { return }
filename = filename.replace("\\", "/") // windows
if (!cfg.fsTimeout) { if (!cfg.fsTimeout) {
console.info(event, filename) console.info(event, filename)
pluginpath = (pluginpath ? pluginpath : './plugins/') + filename const fullpluginpath = pluginpath + filename
const pluginname = (filename.split(".js")[0]).replace(/\/.+/, "")
pluginpath = pluginpath + pluginname
const hassubplugin = fullpluginpath.replace(/\.js$/, "") !== pluginpath
const check = Object.keys(cfg.plugins) const check = Object.keys(cfg.plugins)
console.info(`reload file:`, pluginpath) console.info(`reload file: ./lib/${fullpluginpath}`)
const plugin = require.resolve(pluginpath) const plugin = require.resolve(fullpluginpath)
if (plugin && require.cache[plugin]) { if (plugin && require.cache[plugin]) {
// console.debug(Object.keys(cfg.plugins)) // console.debug(Object.keys(cfg.plugins))
unloadplugin(filename.split(".js")[0], pluginpath) unloadplugin(pluginname)
console.assert(Object.keys(cfg.plugins).length == check.length - 1, "plugin not removed, potential memory leak") console.assert(Object.keys(cfg.plugins).length == check.length - 1, "plugin not removed, potential memory leak")
if (hassubplugin) {
console.info("reload: also unloading sub")
unloadplugin(pluginname, fullpluginpath)
}
} }
loadplugin(filename.split(".js")[0], pluginpath) loadplugin(pluginname)
if (Object.keys(cfg.plugins).length != check.length) { if (Object.keys(cfg.plugins).length != check.length) {
// If left < right : // If left < right :
// - new plugin that's not registered in cfg.plugins, so added // - new plugin that's not registered in cfg.plugins, so added
@@ -83,12 +94,11 @@ reloadplugin = (event, filename, pluginpath) => {
// console.log('file.js %s event', event) // console.log('file.js %s event', event)
} }
fs.watch('./lib/plugins', reloadplugin) fs.watch('./lib/plugins', { recursive: true }, reloadplugin)
cfg.bot = bot cfg.bot = bot
// TODO better name, or switch to array // TODO better name, or switch to array
cfg.botAddressPrefix = '!' cfg.botAddressPrefix = '!'
cfg.botAddressRegex = new RegExp(`^${bot.username} (${cfg.botAddressPrefix}.+)`)
cfg.quiet = true cfg.quiet = true
@@ -97,19 +107,23 @@ cfg.quiet = true
bot.once("spawn", () => { bot.once("spawn", () => {
plugins = { plugins = {
command: require('./plugins/command'), command: require('./plugins/command'),
eater: require('./plugins/eater'),
inventory: require('./plugins/inventory'),
informer: require('./plugins/informer'), informer: require('./plugins/informer'),
inventory: require('./plugins/inventory'),
finder: require('./plugins/finder'), finder: require('./plugins/finder'),
sleeper: require('./plugins/sleeper'),
armor: require('./plugins/armor'),
mover: require('./plugins/mover'), mover: require('./plugins/mover'),
sleeper: require('./plugins/sleeper'),
eater: require('./plugins/eater'),
armor: require('./plugins/armor'),
guard: require('./plugins/guard'), guard: require('./plugins/guard'),
// miner: require('./plugins/miner.js'), // miner: require('./plugins/miner.js'),
statemachine: require('./plugins/statemachine'), statemachine: require('./plugins/statemachine'),
} }
cfg.plugins = plugins cfg.plugins = plugins
// cfg.botAddressPrefix = ${bot.username.substr(-2,2)}
cfg.botAddressRegex = new RegExp(`^${bot.username}:? (/|${cfg.botAddressPrefix}.+)`)
// FIXME leaks every load, so adding here instead of command.js to load only once
bot.addChatPattern("web", /\[WEB\] (\[.+\])?\s*([\w.]+): (.+)/, { parse: true })
for (const plugin of Object.values(plugins)) { for (const plugin of Object.values(plugins)) {
try { try {

View File

@@ -27,14 +27,13 @@ function checkItemExists(name) {
} }
const events = { const events = {
whisper: function command_whisper(username, message) { whisper: function command_whisper(username, _botusername, message, ...history) {
if ([bot.username, "me"].includes(username)) return if ([bot.username, "me"].includes(username)) return // whisper back from server (afk msges, etc)
if (/^gossip/.test(message)) return if (/^gossip/.test(message)) return
// if (/^!/.test(message) && username === cfg.admin){
if (username === cfg.admin) { if (username === cfg.admin) {
console.info("whispered command", _botusername, message)
message = message.replace("\\", "@") message = message.replace("\\", "@")
console.info("whispered command", message) if (message.startsWith(cfg.botAddressPrefix)) {
if (/^!/.test(message)) {
command(username, message) command(username, message)
} else { } else {
bot.chat(message) bot.chat(message)
@@ -42,7 +41,7 @@ const events = {
} }
} else { } else {
bot.whisper(cfg.admin, `gossip ${username}: ${message}`) bot.whisper(cfg.admin, `gossip ${username}: ${message}`)
console.info(username, "whispered", message) console.info(username, "whispered", _botusername, message)
} }
} }
, chat: command , chat: command
@@ -57,6 +56,30 @@ const events = {
}, 15 * 60 * 1000, bot, cfg); }, 15 * 60 * 1000, bot, cfg);
} }
} }
// , message: systemMessage
, messagestr: function systemMessageStr(...args) { console.log("cmd msgstr:", ...args) }
, "chat:web": commandWeb
}
function systemMessage(...args) {
if (args[0]?.text) return
const metadata = (args[0]?.extra || args[0]?.with)?.map(v => v.text)
console.log(
"cmd msg:",
args[0]?.text || args[0]?.translate,
args[0]?.extra?.length || args[0]?.with?.length || Object.keys(args[0]?.json).length - 1,
metadata.length > 0 && metadata || args[0],
args.slice(1)
)
}
function commandWeb([[mode, username, message]]) {
// console.log("web msg:", mode, username, message)
message && command(username, message)
}
function _clientSystemMessage(...args) {
console.log("cmd chat:", args[0]?.message, args[0]?.extra?.length, args[0]?.extra, args[0], args.slice(1))
} }
const events_registered = [] const events_registered = []
@@ -297,7 +320,7 @@ function command(username, message) {
switch (message_parts[1]) { switch (message_parts[1]) {
case "me": case "me":
if (player) { if (player) {
bot.lookAt((new v.Vec3(0, 1, 0)).add(player.position)) bot.lookAt(player.position.offset(0, 1, 0))
} else { } else {
cfg.quiet || bot.chat("can't see you") cfg.quiet || bot.chat("can't see you")
} }
@@ -314,7 +337,7 @@ function command(username, message) {
break break
default: default:
const aPlayer = bot.players[message_parts[2]] ? bot.players[message_parts[2]].entity : null const aPlayer = bot.players[message_parts[2]] ? bot.players[message_parts[2]].entity : null
if (aPlayer) bot.lookAt((new v.Vec3(0, 1, 0)).add(aPlayer.position)) if (aPlayer) bot.lookAt(aPlayer.position.offset(0, 1, 0))
break; break;
} }
break break
@@ -475,7 +498,7 @@ function command(username, message) {
case "howdy": case "howdy":
case "heyo": case "heyo":
case "yo": case "yo":
if (player) bot.lookAt((new v.Vec3(0, 1, 0)).add(player.position)) if (player) bot.lookAt(player.position.offset(0, 1, 0))
// TODO sneak // TODO sneak
// function swingArm() { // function swingArm() {
@@ -522,6 +545,8 @@ const load = (config) => {
bot.on(key, fn) bot.on(key, fn)
) )
} }
// bot._client.on("chat", _clientSystemMessage)
} }
const unload = () => { const unload = () => {
@@ -531,6 +556,8 @@ const unload = () => {
events_registered.shift() events_registered.shift()
} }
console.log("events_registered:", bot._eventsCount) console.log("events_registered:", bot._eventsCount)
// bot._client.off("chat", _clientSystemMessage)
} }
module.exports = { load, unload, events_registered, command } module.exports = { load, unload, events_registered, command }

View File

@@ -71,9 +71,7 @@ function lookForMobs() {
bot.pvp.attack(entityEnemy) bot.pvp.attack(entityEnemy)
} else if (entityEnemy) { } else if (entityEnemy) {
bot.lookAt( bot.lookAt(
// (new v.Vec3(0, 1, 0)).add(
entityEnemy.position entityEnemy.position
// )
) )
cfg.quiet || bot.chat("AH! A creeper! They creep me out!") cfg.quiet || bot.chat("AH! A creeper! They creep me out!")
} }

View File

@@ -1,39 +1,104 @@
let cfg
let bot let bot
let mcData let mcData
let quiet
// import v from 'vec3' // import v from 'vec3'
const v = require('vec3') const v = require('vec3')
function block(pos) { let cfg = {
const block = pos ? bot.blockAt(v(pos)) : bot.blockAtCursor() info: {
console.log(block, block && block.getProperties()) quiet: quiet,
recentCommand: null,
},
// to satisfy typescript
quiet: null,
bot: bot
}
var RelativePosEnum
(function (RelativePosEnum) {
RelativePosEnum[RelativePosEnum["feet"] = 0] = "feet"
RelativePosEnum[RelativePosEnum["standing"] = 1] = "standing"
RelativePosEnum[RelativePosEnum["head"] = 2] = "head"
RelativePosEnum[RelativePosEnum["looking"] = 3] = "looking"
RelativePosEnum[RelativePosEnum["infront"] = 4] = "infront"
RelativePosEnum[RelativePosEnum["behind"] = 5] = "behind"
})(RelativePosEnum || (RelativePosEnum = {}))
function relPosToBlock(entity, relPos) {
let pos
if (!(relPos in RelativePosEnum)) return console.warn("info: not a relative position:", relPos)
relPos = typeof relPos === "number" ? RelativePosEnum[relPos] : relPos
switch (relPos) {
case "feet":
pos = entity.position;
break
case "standing":
pos = entity.position.offset(0, -1, 0)
break
// todo use CARDINAL directions api from pathfinder
// case "behind":
// entity.yaw
// pos = entity.position
// break
// case "front":
// case "infront":
// pos = entity.position
// break
case "head":
pos = entity.position.offset(0, 1.85, 0)
break
case "looking":
if (entity === bot.entity) {
return bot.blockAtCursor()
// return bot.blockInSight(128, 128)
}
default:
quiet || bot.chat(`info: pos '${relPos}' not implemented`)
break
}
if (pos) {
// nearest block
return bot.blockAt(pos)
}
}
function isVec3(vec) {
return vec?.length === 3 || vec.x && vec.y && vec.z
}
function block(entity = bot.entity, pos = entity?.position?.offset(0, -1, 0)) {
console.assert(pos || entity)
const block = isVec3(pos) ? bot.blockAt(v(pos))
: typeof pos === "string" ? relPosToBlock(entity, RelativePosEnum[pos]) : pos
console.log(block, block?.getProperties && block.getProperties())
if (!block) { if (!block) {
cfg.quiet || bot.chat("empty block") quiet || bot.chat("empty block")
return block return block
} }
bot.lookAt(block?.position)
let info = [block.type, block.name] let info = [block.type, block.name]
if (block.metadata) info.push(Object.entries(block.getProperties())) if (block.metadata) info.push(Object.entries(block.getProperties()))
cfg.quiet || bot.chat(info.join(": ")) quiet || bot.chat(info.join(": "))
} }
function item( function item(
slot, entity = bot.entity,
entity = bot.entity slot = entity.heldItem
) { ) {
const item = slot ? const item = typeof slot === "number" ?
bot.inventory.slots[parseInt(slot) + bot.QUICK_BAR_START] : bot.inventory.slots[slot + bot.QUICK_BAR_START] :
entity.heldItem slot
console.log(item) console.log("info item:", item)
if (!item) { if (!item) {
cfg.quiet || bot.chat("no item") quiet || bot.chat("no item")
return item return item
} }
let info = [item.type, item.name] let info = [item.type, item.name, item.count]
if (item.metadata) info.push("meta: " + item.metadata.length) if (item.metadata) info.push("meta: " + item.metadata.length)
if (item.nbt) { if (item.nbt) {
info.push(compound_value(item.nbt)) info.push(compound_value(item.nbt))
} }
cfg.quiet || bot.chat(info.join("; ")) quiet || bot.chat(info.join("; "))
function compound_value(obj) { function compound_value(obj) {
if (typeof obj.value == "object") { if (typeof obj.value == "object") {
return compound_value(obj.value) return compound_value(obj.value)
@@ -51,27 +116,110 @@ function item(
return item return item
} }
function entity(name) { var VillagerProfession
const entity = typeof name === "string" ? bot.nearestEntity((entity) => { (function (VillagerProfession) {
const ename = entity.name || entity.username VillagerProfession[VillagerProfession["Unemployed"] = 0] = "Unemployed"
return name && ename ? ename == name : true VillagerProfession[VillagerProfession["Armourer"] = 1] = "Armourer"
}) : entity VillagerProfession[VillagerProfession["Butcher"] = 2] = "Butcher"
console.log(entity) VillagerProfession[VillagerProfession["Cartographer"] = 3] = "Cartographer"
VillagerProfession[VillagerProfession["Cleric"] = 4] = "Cleric"
VillagerProfession[VillagerProfession["Farmer"] = 5] = "Farmer"
VillagerProfession[VillagerProfession["Fisherman"] = 6] = "Fisherman"
VillagerProfession[VillagerProfession["Fletcher"] = 7] = "Fletcher"
VillagerProfession[VillagerProfession["Leatherworker"] = 8] = "Leatherworker"
VillagerProfession[VillagerProfession["Librarian"] = 9] = "Librarian"
VillagerProfession[VillagerProfession["Mason"] = 10] = "Mason"
VillagerProfession[VillagerProfession["Nitwit"] = 11] = "Nitwit"
VillagerProfession[VillagerProfession["Shepherd"] = 12] = "Shepherd"
VillagerProfession[VillagerProfession["Toolsmith"] = 13] = "Toolsmith"
VillagerProfession[VillagerProfession["Weaponsmith"] = 14] = "Weaponsmith"
})(VillagerProfession || (VillagerProfession = {}))
function entity(name = bot.nearestEntity()) {
const entity = typeof name === "string" ? (name = name.toLowerCase()) && bot.nearestEntity((entity) => {
const enames = [entity.username?.toLowerCase(), entity.name, entity.displayName?.toLowerCase()]
return enames.includes(name)
}) : name
console.log("info entity:", entity)
if (!entity) { if (!entity) {
cfg.quiet || bot.chat("no entity") quiet || bot.chat("no entity")
return entity return entity
} }
let info = [entity.type, entity.username || entity.name] entity?.position && bot.lookAt(entity.position)
let info = [(entity.entityType && entity.entityType + ":" || "") + (entity.type === "object" ? entity.kind : entity.type)]
info.push(entity.username || entity.name)
// TODO various info depending on the type of entity; player, villager, etc // TODO various info depending on the type of entity; player, villager, etc
// TODO refactor and combine with compound_value()
switch (entity.type) {
case "object":
switch (entity.kind) {
case "Drops":
const item = entity.metadata[7];
entity.metadata;
if (item.present) {
info.push("item: " + item.itemId + ": "
+ mcData.items[item.itemId].name
+ `x${item.itemCount}`);
}
else {
console.warn("info entity: metadata expected item, got:", entity.metadata[7]);
}
break;
default:
}
break;
case "mob":
switch (name) {
case 'villager':
const { villagerProfession, ...otherProps } = entity.metadata[17]
info.push(VillagerProfession[villagerProfession])
info.push(Object.entries(otherProps).toString())
break
default:
break
}
entity.metadata[8] && info.push("health:" + entity.metadata[8])
entity.metadata[15] && info.push("baby")
if (entity.heldItem) {
info.push("holding")
item(entity)
}
case "global":
case "orb":
case "player":
break;
case "other":
info.push("kind:" + entity.kind)
default:
}
if (entity.metadata) info.push("len: " + entity.metadata.length) if (entity.metadata) info.push("len: " + entity.metadata.length)
cfg.quiet || bot.chat(info.join("; ")) quiet || bot.chat(info.join("; "))
return entity return entity
} }
function blockOrItemFromId(num, quiet = cfg.info.quiet) {
const block = mcData?.blocks[num]
const item = mcData?.items[num]
// const entity = mcData?.entities[num]
if (block || item) {
quiet || bot.chat(
(block && `block: ${block.name}, ` || "")
+ (item && `item: ${item.name}, ` || "")
// + (entity && `entity: ${entity.name}, ` || "")
)
} else {
quiet || bot.chat("info: nonexistent block or item")
}
return { block, item }
}
function command(message_parts, player) { function command(message_parts, player) {
if (message_parts.length > 0) { if (message_parts.length > 0) {
cfg.info.recentCommand = message_parts cfg.info.recentCommand = message_parts
} }
if (player === null)
player = void 0
switch (message_parts.length) { switch (message_parts.length) {
case 0: case 0:
if (cfg.info.recentCommand) { if (cfg.info.recentCommand) {
@@ -79,11 +227,15 @@ function command(message_parts, player) {
} else { } else {
// TODO dispatch on instance of entity, block, etc.. // TODO dispatch on instance of entity, block, etc..
// TODO have the logic inside the function or with a utility function // TODO have the logic inside the function or with a utility function
block(player.position || player?.entity.position || null) block()
} }
break; break;
case 1: case 1:
switch (message_parts[0]) { switch (message_parts[0]) {
case "quiet":
cfg.info.quiet = quiet = !quiet;
quiet || bot.chat(`info: ${quiet ? "" : "not "}being quiet`);
break;
case "i": case "i":
case "item": case "item":
item() item()
@@ -92,10 +244,20 @@ function command(message_parts, player) {
case "entity": case "entity":
entity() entity()
break break
case "me":
block(player)
break
case "b": case "b":
case "block": case "block":
default:
block() block()
break
default:
const num = parseInt(message_parts[0])
if (isFinite(num)) {
blockOrItemFromId(num)
} else {
quiet || bot.chat("info usage: `!info [me|i|e|b|<num>|quiet]`")
}
break; break;
} }
@@ -105,7 +267,43 @@ function command(message_parts, player) {
switch (message_parts[0]) { switch (message_parts[0]) {
case "i": case "i":
case "item": case "item":
item(message_parts[1]) switch (message_parts[1]) {
case "me":
item(player)
break
default:
const slot = parseInt(message_parts[1])
slot && item(undefined, slot)
break
}
break
case "b":
case "block":
switch (message_parts[1]) {
case "me":
block(player)
break
default:
const aPlayer = bot.players[message_parts[1]]
if (message_parts[1] in RelativePosEnum) {
block(undefined, message_parts[1])
} else if (aPlayer) {
if (aPlayer.entity) {
block(aPlayer.entity)
} else {
quiet || bot.chat(`info: player ${aPlayer.username} too far!`)
}
} else {
// or entity(message_parts[1]).position
console.log(bot.players[message_parts[1]])
quiet || bot.chat("info: not yet implemented")
}
}
break
case "me":
if (message_parts[1] in RelativePosEnum) {
block(player, message_parts[1])
}
break break
case "e": case "e":
case "entity": case "entity":
@@ -120,7 +318,7 @@ function command(message_parts, player) {
case "b": case "b":
case "block": case "block":
default: default:
block(message_parts.slice(1)) block(undefined, message_parts.slice(1))
break; break;
} }
@@ -133,12 +331,9 @@ function command(message_parts, player) {
} }
const load = (config) => { const load = (config) => {
config.info = cfg.info
cfg = config cfg = config
bot = cfg.bot bot = cfg.bot
cfg.info = {
quiet: cfg.quiet,
recentCommand: null,
}
mcData = bot.mcData || (bot.mcData = require('minecraft-data')(bot.version)) mcData = bot.mcData || (bot.mcData = require('minecraft-data')(bot.version))
} }

View File

@@ -11,20 +11,32 @@ let movements = []
function initMoves(bot = bot, mcData = bot.mcData) { function initMoves(bot = bot, mcData = bot.mcData) {
console.info(movements)
if (movements.length > 0) { if (movements.length > 0) {
bot.pathfinder.setMovements(movements.defaultMove) bot.pathfinder.setMovements(movements.defaultMove)
return console.warn("movements already initialized!") return console.warn("go init: movements already initialized!", movements)
} }
let defaultMove = new Movements(bot, mcData) const normalMove = new Movements(bot, mcData)
defaultMove.canDig = false normalMove.canDig = false
defaultMove.scafoldingBlocks.push(mcData.blocksByName.slime_block.id) normalMove.scafoldingBlocks.push(mcData.blocksByName.slime_block.id)
defaultMove.blocksCantBreak.add(mcData.blocksByName.glass.id) normalMove.blocksCantBreak.add(mcData.blocksByName.glass.id)
defaultMove.blocksToAvoid.add(mcData.blocksByName.magma_block.id) normalMove.blocksToAvoid.add(mcData.blocksByName.magma_block.id)
movements.push(defaultMove) movements.push(normalMove)
movements.defaultMove = movements[0] movements.defaultMove = movements[0]
bot.pathfinder.setMovements(defaultMove) const aggresiveMove = new Movements(bot, mcData)
//Object.create or assign?
Object.assign(aggresiveMove, normalMove)
aggresiveMove.canDig = true
movements.push(aggresiveMove)
const safeMove = new Movements(bot, mcData)
Object.assign(safeMove, normalMove)
safeMove.allowParkour = false
safeMove.canDig = false
movements.push(safeMove)
// console.info("go init: moves:", movements)
bot.pathfinder.setMovements(normalMove)
} }
@@ -82,9 +94,69 @@ function follow(entity, dynamic = true, distance = 3) {
bot.pathfinder.setGoal(new GoalFollow(entity, distance), dynamic) bot.pathfinder.setGoal(new GoalFollow(entity, distance), dynamic)
} }
function away(entity = bot.nearestEntity(), invertInvert = true, dynamic = true, distance = 10) {
const currentGoal = bot.pathfinder.goal
console.assert(currentGoal || entity)
const { GoalInvert } = require('mineflayer-pathfinder').goals
bot.pathfinder.setMovements(movements.defaultMove)
if (!currentGoal) {
const { GoalFollow } = require('mineflayer-pathfinder').goals
if (entity.entity) {
console.log("go away entity:", entity, entity.entity)
entity = entity.entity
}
cfg.quiet || bot.chat(
`going away from ${entity?.type
}: ${entity?.username || entity?.displayName
}${dynamic ? "" : " once"}`
)
// alternative implementation
// follow(entity, dynamic, distance)
// bot.pathfinder.setGoal(new GoalInvert(bot.pathfinder.goal), dynamic)
return bot.pathfinder.setGoal(new GoalInvert(
new GoalFollow(entity, distance)
), dynamic)
}
if (currentGoal instanceof GoalInvert) {
const currEntity = currentGoal.goal.entity
console.log("go away inverse goal:", currentGoal.goal)
if (invertInvert) {
cfg.quiet || bot.chat(
`switching towards ${currentGoal.goal?.constructor.name
}: ${currEntity?.type
}: ${currEntity?.username || currEntity?.displayName
}${dynamic ? "" : " once"}`
)
bot.pathfinder.setGoal(currentGoal.goal, dynamic)
} else {
cfg.quiet || bot.chat(
`already going away from ${currentGoal.goal?.constructor.name
}; not switching`
)
}
} else {
const currEntity = currentGoal.entity
console.log("go away goal:", currentGoal)
cfg.quiet || bot.chat(
`going away from ${currentGoal?.constructor.name
}: ${currEntity?.type
}: ${currEntity?.username || currEntity?.displayName
}${dynamic ? "" : " once"}`
)
bot.pathfinder.setGoal(new GoalInvert(currentGoal), dynamic)
}
}
function ride(entity) { function ride(entity) {
entity = entity?.entity || entity entity = entity?.entity || entity
const ridableMobs = ["Horse", "Donkey", "Pig", "Strider"] const ridableMobs = ["Horse", "Donkey", "Pig", "Strider", "Mule"]
const vehicle = entity && typeof entity !== "string" ? entity : bot.nearestEntity(e => { const vehicle = entity && typeof entity !== "string" ? entity : bot.nearestEntity(e => {
if (typeof entity === "string") return e.name === entity if (typeof entity === "string") return e.name === entity
const maybeRidableMob = e.mobType?.split(" ") const maybeRidableMob = e.mobType?.split(" ")
@@ -107,8 +179,9 @@ function ride(entity) {
function moveOrRide(turn = false, reverse = -1, directionLabel, message_parts2) { function moveOrRide(turn = false, reverse = -1, directionLabel, message_parts2) {
// bot.once("attach", state = "vehiccel") // bot.once("attach", state = "vehiccel")
if (bot.vehicle) { if (bot.vehicle) {
const amount = parseInt(message_parts2[0]) || 10 * -reverse // FIXME moveVehicle should be +-1 or 0?
bot.moveVehicle(turn && amount || 0, !turn && amount || 0) const amount = parseInt(message_parts2[0]) * -reverse
bot.moveVehicle(turn && Math.sign(amount) || 0, !turn && amount || 0)
} else { } else {
command([directionLabel].concat(message_parts2)) command([directionLabel].concat(message_parts2))
} }
@@ -230,6 +303,11 @@ function command(message_parts, player) {
case "mount": case "mount":
ride(message_parts2[0]) ride(message_parts2[0])
break break
case "away":
case "run":
case "runaway":
away()
break
case "w": case "w":
case "f": case "f":
moveOrRide(0, -1, "forward", message_parts2) moveOrRide(0, -1, "forward", message_parts2)
@@ -246,6 +324,11 @@ function command(message_parts, player) {
case "r": case "r":
moveOrRide(1, 1, "left", message_parts2) moveOrRide(1, 1, "left", message_parts2)
break break
case "up":
case "u":
case "j":
moveOrRide(1, 1, "jump", message_parts2)
break
case "back": case "back":
case "forward": case "forward":
case "jump": case "jump":

View File

@@ -1,476 +1,479 @@
// import { createMachine, interpret, InterpreterStatus } from "xstate"; // import { createMachine, interpret, InterpreterStatus } from "xstate";
const { createMachine, interpret, InterpreterStatus } = require('xstate'); const { createMachine, interpret, InterpreterStatus } = require('xstate');
// import { access, mkdir, writeFile, readFile } from "fs"; // import { access, mkdir, writeFile, readFile } from "fs";
const { access, mkdir, writeFile, readFile } = require('fs'); const { access, mkdir, writeFile, readFile } = require('fs');
const v = require('vec3'); // for look dummy action, maybe not needed in future // ANGRAM_PREFIX='MINECRAFT'
// ANGRAM_PREFIX='MINECRAFT' const { MINECRAFT_DATA_FOLDER } = process.env || require("dotenv-packed").parseEnv().parsed;
const { MINECRAFT_DATA_FOLDER } = process.env || require("dotenv-packed").parseEnv().parsed; const storage_dir = MINECRAFT_DATA_FOLDER || './data/' + "/sm/";
const storage_dir = MINECRAFT_DATA_FOLDER || './data/' + "/sm/"; // import { createBot } from "mineflayer"
// import { createBot } from "mineflayer" // let { pathfinder, Movements, goals } = require('mineflayer-pathfinder')
// let { pathfinder, Movements, goals } = require('mineflayer-pathfinder') // let cfg
// let cfg // let bot = createBot({ username: 'statebot' })
// let bot = createBot({ username: 'statebot' }) let bot;
let bot; let quiet;
let quiet; let updateRate = 20;
let updateRate = 20; const machines = {
const machines = { list: {},
list: {}, running: {},
running: {}, };
}; let webserver;
let webserver; let cfg = {
let cfg = { statemachine: {
statemachine: { webserver: null,
webserver: null, // quiet: true,
// quiet: true, quiet: quiet,
quiet: quiet, list: {},
list: {}, running: {},
running: {}, draft: null,
draft: null, recent: null,
recent: null, // debug: null,
// debug: null, debug: true,
debug: true, updateRate: updateRate
updateRate: updateRate }
} // FIXME temp variables to satisfy typescript autocomplete
// FIXME temp variables to satisfy typescript autocomplete // , quiet: null
// , quiet: null ,
, bot: bot,
bot: bot, plugins: { statemachine: null }
plugins: { statemachine: null } };
}; // Edit your machine(s) here
// Edit your machine(s) here function init(smName = "dummy", webserver) {
function init(smName = "dummy", webserver) { access(storage_dir, err => {
access(storage_dir, err => { if (err?.code === 'ENOENT') {
if (err?.code === 'ENOENT') { mkdir(storage_dir, e => e && console.warn("sm init: create dir", e));
mkdir(storage_dir, e => e && console.warn("sm init: create dir", e)); }
} else if (err) {
else if (err) { console.warn("sm init: create dir", err);
console.warn("sm init: create dir", err); }
} });
}); // const machine = newSM(smName)
// const machine = newSM(smName) // machine.states.idle.on.TOGGLE = "start"
// machine.states.idle.on.TOGGLE = "start" // machine.states.start.on.TOGGLE = "idle"
// machine.states.start.on.TOGGLE = "idle" const machine = createMachine({
const machine = createMachine({ id: smName,
id: smName, initial: "idle",
initial: "idle", states: {
states: { idle: {
idle: { on: { TOGGLE: "start", NEXT: "start", PREV: "look", STOP: "finish" }
on: { TOGGLE: "start", NEXT: "start", PREV: "look", STOP: "finish" } },
}, start: {
start: { on: { TOGGLE: "look", NEXT: "look", PREV: "idle" },
on: { TOGGLE: "look", NEXT: "look", PREV: "idle" }, entry: 'lookAtPlayerOnce',
entry: 'lookAtPlayerOnce', },
}, look: {
look: { on: { TOGGLE: "idle", NEXT: "idle", PREV: "start" },
on: { TOGGLE: "idle", NEXT: "idle", PREV: "start" }, // entry: ['look', 'blah']
// entry: ['look', 'blah'] // entry: 'lookAtPlayerOnce',
// entry: 'lookAtPlayerOnce', activities: 'lookAtPlayer',
activities: 'lookAtPlayer', meta: { debug: true }
meta: { debug: true } },
}, finish: {
finish: { type: 'final'
type: 'final' },
}, },
}, on: { START: '.start', STOP: '.idle' },
on: { START: '.start', STOP: '.idle' }, meta: { debug: true },
meta: { debug: true }, context: { player: null, rate: updateRate },
context: { player: null, rate: updateRate }, }, {
}, { actions: {
actions: { // action implementation
// action implementation lookAtPlayerOnce: (context, event) => {
lookAtPlayerOnce: (context, event) => { const player = context?.player && (context?.player?.isValid || context?.player?.entity?.isValid)
const player = context?.player || bot.nearestEntity(entity => entity.type === 'player'); || bot.nearestEntity(entity => entity.type === 'player');
if (player.position || player.entity) { if (player?.position || player?.entity) {
context.player = player; context.player = player;
bot.lookAt((new v.Vec3(0, 1, 0)).add((player.entity || player).position)); bot.lookAt((player.entity || player).position.offset(0, 1, 0));
} }
} }
}, },
activities: { activities: {
lookAtPlayer: (context, event) => { lookAtPlayer: (context, event) => {
const player = context?.player || bot.nearestEntity(entity => entity.type === 'player'); const player = (context?.player?.isValid || context?.player?.entity?.isValid) && context?.player
// TODO check every event? || bot.nearestEntity(entity => entity.type === 'player' && entity.isValid);
if (player.position || player.entity) { // TODO check pos every event?
context.player = player; if (player?.position || player?.entity) {
function looks() { context.player = player;
bot.lookAt((new v.Vec3(0, 1, 0)).add((player.entity || player).position)); function looks() {
} bot.lookAt((player.entity || player).position.offset(0, 1, 0));
bot.on("time", looks); }
return () => bot.off("time", looks); bot.on("time", looks);
} return () => bot.off("time", looks);
} }
}, else {
delays: { quiet || bot.chat("look: no valid players");
/* ... */ // TODO use xstate logger
}, context.debug && console.log("sm: no valid player", this, context, player);
guards: { }
/* ... */ }
}, },
services: { delays: {
/* ... */ /* ... */
} },
}); guards: {
console.log("sm init: machine", machine); /* ... */
const service = runSM(saveSM(machine)); },
if (service?.send) { services: {
setTimeout(service.send, 200, "TOGGLE"); /* ... */
// setTimeout(service.send, 400, "TOGGLE") }
} });
else { console.log("sm init: machine", machine);
console.warn("sm init: service", service); const service = runSM(saveSM(machine));
} if (service?.send) {
} setTimeout(service.send, 200, "TOGGLE");
function newSM(smName = "sm_" + Object.keys(cfg.statemachine.list).length) { // setTimeout(service.send, 400, "TOGGLE")
smName = smName.replace(/\s+/, '_'); }
if (cfg.statemachine.list[smName]) { else {
console.warn("sm exists", smName); console.warn("sm init: service", service);
quiet || bot.chat(`sm ${smName} already exists, edit or use another name instead`); }
return; }
} function newSM(smName = "sm_" + Object.keys(cfg.statemachine.list).length) {
const machine = createMachine({ smName = smName.replace(/\s+/, '_');
id: smName, if (cfg.statemachine.list[smName]) {
initial: "start", console.warn("sm exists", smName);
// TODO use history states for PAUSE and RESUME quiet || bot.chat(`sm ${smName} already exists, edit or use another name instead`);
states: { idle: { on: { START: "start" } }, start: { on: { STOP: "idle" } } } return;
}); }
cfg.statemachine.draft = machine; const machine = createMachine({
return machine; id: smName,
} initial: "start",
function saveSM(machine = cfg.statemachine.draft) { // TODO use history states for PAUSE and RESUME
if (!machine?.id) { states: { idle: { on: { START: "start" } }, start: { on: { STOP: "idle" } } }
console.warn("sm save: invalid", machine); });
quiet || bot.chat("sm: couldn't save, invalid"); cfg.statemachine.draft = machine;
return; return machine;
} }
// TODO do tests and validation function saveSM(machine = cfg.statemachine.draft) {
// 1. ensure default states [start, idle] if (!machine?.id) {
// 2. ensure closed cycle from start to idle console.warn("sm save: invalid", machine);
cfg.statemachine.list[machine.id] = machine; quiet || bot.chat("sm: couldn't save, invalid");
if (machine.id === cfg.statemachine.draft?.id) { return;
cfg.statemachine.draft = null; }
} // TODO do tests and validation
writeFile( // 1. ensure default states [start, idle]
// TODO // 2. ensure closed cycle from start to idle
// `${storage_dir}/${player}/${machine.id}.json` cfg.statemachine.list[machine.id] = machine;
`${storage_dir}/${machine.id}.json`, if (machine.id === cfg.statemachine.draft?.id) {
// TODO decide which data to store cfg.statemachine.draft = null;
// https://xstate.js.org/docs/guides/states.html#persisting-state }
// JSON.stringify(machine.toJSON), writeFile(
// JSON.stringify(machine.states.toJSON), // + activities, delays, etc // TODO
JSON.stringify({ config: machine.config, context: machine.context }), e => e && console.log("sm load sm: write file", e)); // `${storage_dir}/${player}/${machine.id}.json`
// return run ? runSM(machine) : machine `${storage_dir}/${machine.id}.json`,
return machine; // TODO decide which data to store
} // https://xstate.js.org/docs/guides/states.html#persisting-state
function loadSM(name, run = true) { // JSON.stringify(machine.toJSON),
//: StateMachine<any, any, any> { // JSON.stringify(machine.states.toJSON), // + activities, delays, etc
readFile( JSON.stringify({ config: machine.config, context: machine.context }), e => e && console.log("sm load sm: write file", e));
// readFileSync( // return run ? runSM(machine) : machine
// TODO return machine;
// `${storage_dir}/${player}/${machine.id}.json` }
`${storage_dir}/${name}.json`, afterRead function loadSM(name, run = true) {
// JSON.stringify(machine.toJSON), //: StateMachine<any, any, any> {
); readFile(
function afterRead(err, jsonString) { // readFileSync(
if (err) { // TODO
console.warn("sm load sm: read file", err); // `${storage_dir}/${player}/${machine.id}.json`
return; `${storage_dir}/${name}.json`, afterRead
} // JSON.stringify(machine.toJSON),
else { );
const machine = createMachine(JSON.parse(jsonString).config); function afterRead(err, jsonString) {
// TODO do tests and validation if (err) {
// 1. ensure default states [start, idle] console.warn("sm load sm: read file", err);
// 2. ensure closed cycle from start to idle return;
cfg.statemachine.list[machine.id] = machine; }
if (machine.id === cfg.statemachine.draft?.id) { else {
cfg.statemachine.draft = machine; const machine = createMachine(JSON.parse(jsonString).config);
} // TODO do tests and validation
if (run) { // 1. ensure default states [start, idle]
runSM(machine); // 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;
// return run ? runSM(machine) : machine }
// return machine if (run) {
} runSM(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) { // return run ? runSM(machine) : machine
const machine = typeof name === "string" ? cfg.statemachine.list[name] // return machine
: name; }
if (!machine) { // function isInterpreter(SMorService: StateMachine<any, any, any> | Interpreter<any>): SMorService is Interpreter<any> {
console.warn("sm get: doesn't exist", name); // return (SMorService as Interpreter<any>).start !== undefined;
cfg.statemachine.quiet || bot.chat(`sm ${name} doesn't exist`); // }
return; function getSM(name = cfg.statemachine.draft || cfg.statemachine.recent, asService = false, quiet = false) {
} const machine = typeof name === "string" ? cfg.statemachine.list[name]
if (asService) { : name;
const service = cfg.statemachine.running[machine?.id]; if (!machine) {
if (!service) { console.warn("sm get: doesn't exist", name);
quiet || console.warn("sm get: already stopped", machine); cfg.statemachine.quiet || bot.chat(`sm ${name} doesn't exist`);
quiet || cfg.statemachine.quiet || bot.chat(`sm ${machine?.id} isn't running`); return;
return interpret(machine); }
} if (asService) {
return service; const service = cfg.statemachine.running[machine?.id];
} if (!service) {
else { quiet || console.warn("sm get: already stopped", machine);
// return machine.machine ? machine.machine : machine quiet || cfg.statemachine.quiet || bot.chat(`sm ${machine?.id} isn't running`);
return machine; return interpret(machine);
} }
} return service;
function runSM(name = getSM(undefined, undefined, true), player // or supervisor? }
, restart = false) { else {
if (!name) // return machine.machine ? machine.machine : machine
return; return machine;
const service = getSM(name, true, true); }
if (!service) }
return; function runSM(name = getSM(undefined, undefined, true), player // or supervisor?
const machine = service.machine; , restart = false) {
if (!machine) if (!name)
return; return;
switch (service.status) { const service = getSM(name, true, true);
case InterpreterStatus.Running: if (!service)
if (!restart) { return;
console.warn("sm run: already running", service.id); const machine = service.machine;
quiet || bot.chat(`sm ${service.id} already running`); if (!machine)
return service; return;
} switch (service.status) {
stopSM(machine); case InterpreterStatus.Running:
case InterpreterStatus.NotStarted: if (!restart) {
case InterpreterStatus.Stopped: console.warn("sm run: already running", service.id);
break; quiet || bot.chat(`sm ${service.id} already running`);
default: return service;
console.warn("sm run: unknown status:", service.status, service); }
return; stopSM(machine);
break; case InterpreterStatus.NotStarted:
} case InterpreterStatus.Stopped:
if (cfg.statemachine.debug || machine.meta?.debug) { break;
service.onTransition((state) => { default:
quiet || bot.chat(`sm trans: ${machine.id}, ${state.value}`); console.warn("sm run: unknown status:", service.status, service);
quiet || state.meta?.debug && bot.chat(`sm next events: ${machine.id}, ${state.nextEvents}`); return;
console.log("sm debug: trans", state.value, state); break;
}).onDone((done) => { }
quiet || bot.chat(`sm done: ${machine.id}, ${done}`); if (cfg.statemachine.debug || machine.meta?.debug) {
console.log("sm debug: done", done.data, done); 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}`);
cfg.statemachine.running[machine.id] = service; console.log("sm debug: trans", state.value, state);
cfg.statemachine.recent = machine; }).onDone((done) => {
service.start(); quiet || bot.chat(`sm done: ${machine.id}, ${done}`);
// // TODO check if idle state is different (maybe?) console.log("sm debug: done", done.data, done);
console.log("sm run", service.state.value === machine.initialState.value, service.state); });
console.log("sm run", service.state !== machine.initialState, machine.initialState); }
// return machine cfg.statemachine.running[machine.id] = service;
return service; cfg.statemachine.recent = machine;
} service.start();
function stopSM(name = getSM(), quiet = false) { // return machine
let service = getSM(name, true, quiet); return service;
if (!service) }
return; function stopSM(name = getSM(), quiet = false) {
const machine = service.machine; let service = getSM(name, true, quiet);
switch (service.status) { if (!service)
case InterpreterStatus.NotStarted: return;
case InterpreterStatus.Stopped: const machine = service.machine;
console.log("sm stop status", service.status, service.id, cfg.statemachine.running[service.id]); switch (service.status) {
// TODO check if any bugs case InterpreterStatus.NotStarted:
case InterpreterStatus.Running: case InterpreterStatus.Stopped:
break; console.log("sm stop status", service.status, service.id, cfg.statemachine.running[service.id]);
default: // TODO check if any bugs
console.warn("sm stop: unknown status:", service.status); case InterpreterStatus.Running:
break; break;
} default:
service?.stop?.(); console.warn("sm stop: unknown status:", service.status);
cfg.statemachine.running[machine.id] = null; break;
delete cfg.statemachine.running[machine.id]; }
// return machine service?.stop?.();
return service; cfg.statemachine.running[machine.id] = null;
} delete cfg.statemachine.running[machine.id];
function actionSM(action, name = getSM()) { // return machine
if (!action) { return service;
return console.warn("sm action", action); }
} function actionSM(action, name = getSM()) {
let service = getSM(name, true, true); if (!action) {
if (service.status !== InterpreterStatus.Running) return console.warn("sm action", action);
return; }
// const machine = service.machine let service = getSM(name, true, true);
service.send(action.toLowerCase()); if (service.status !== InterpreterStatus.Running)
} return;
function stepSM(command = "", ...message_parts) { // const machine = service.machine
let service = getSM(undefined, true); service.send(action.toUpperCase());
if (!service) }
return; function stepSM(command = "", ...message_parts) {
if (!service.send) { let service = getSM(undefined, true);
console.warn("sm step: can't send", service.machine); if (!service)
// TODO start a temporary service to interpret return;
quiet || bot.chat("sm: step doesn't support machines that aren't running yet"); if (!service.send) {
return; console.warn("sm step: can't send", service.machine);
} // TODO start a temporary service to interpret
if (service?.status !== InterpreterStatus.Running) { quiet || bot.chat("sm: step doesn't support machines that aren't running yet");
console.warn("sm step: machine not running, attempting start", service); return;
runSM; }
} if (service?.status !== InterpreterStatus.Running) {
// const machine = service.machine console.warn("sm step: machine not running, attempting start", service);
switch (command) { runSM;
case "edit": }
// maybe `edit <type>`, like `add`? // const machine = service.machine
// where type: switch (command) {
// context case "edit":
// action // maybe `edit <type>`, like `add`?
// timeout | all timeout // where type:
break; // context
case "undo": // action
break; // timeout | all timeout
case "del": break;
break; case "undo":
case "p": break;
case "prev": case "del":
service?.send("PREV"); break;
break; case "p":
case "add": case "prev":
// maybe `add <type>`? service?.send("PREV");
// where type: break;
// context case "add":
// action // maybe `add <type>`?
// timeout // where type:
case "new": // context
break; // action
// case "with": // timeout
// console.log(this) case "new":
// stepSM(getSM(message_parts[0], true, true), ...message_parts.slice(1)) break;
// break; // case "with":
case "help": // console.log(this)
quiet || bot.chat("![sm ]step [ p(rev) | n(ext) ]"); // stepSM(getSM(message_parts[0], true, true), ...message_parts.slice(1))
break; // break;
case "n": case "help":
case "next": quiet || bot.chat("![sm ]step [ p(rev) | n(ext) ]");
default: break;
service?.send("NEXT"); case "n":
quiet || bot.chat("stepped"); case "next":
break; default:
} service?.send("NEXT");
} quiet || bot.chat("stepped");
function tickSM(time = bot.time.timeOfDay, rate = 20) { break;
if (time % rate !== 0) { }
return; }
} function tickSM(time = bot.time.timeOfDay, rate = 20) {
console.log("sm tick", rate, time); if (time % rate !== 0) {
} return;
function debugSM(name = getSM()) { }
if (!name) console.log("sm tick", rate, time);
return; }
let service = getSM(name, true); function debugSM(name = getSM()) {
if (!service) if (!name)
return; return;
const machine = service.machine; let service = getSM(name, true);
machine.meta.debug = !!!machine.meta.debug; if (!service)
console.info("sm debug", machine.meta, service, machine); return;
cfg.statemachine.quiet || machine.meta.debug && bot.chat("sm debug: " + machine.id); const machine = service.machine;
} machine.meta.debug = !!!machine.meta.debug;
function command(message_parts) { console.info("sm debug", machine.meta, service, machine);
const message_parts2 = message_parts.slice(1); cfg.statemachine.quiet || machine.meta.debug && bot.chat("sm debug: " + machine.id);
switch (message_parts[0]) { }
case "add": function command(message_parts) {
command(["new"].concat(message_parts2)); const message_parts2 = message_parts.slice(1);
break; switch (message_parts[0]) {
case "finish": case "add":
case "done": command(["new"].concat(message_parts2));
case "end": break;
command(["save"].concat(message_parts2)); case "finish":
break; case "done":
case "do": case "end":
case "load": command(["save"].concat(message_parts2));
case "start": break;
command(["run"].concat(message_parts2)); case "do":
break; case "load":
case "debug": case "start":
switch (message_parts[1]) { command(["run"].concat(message_parts2));
case "sm": break;
case "global": case "debug":
case "meta": switch (message_parts[1]) {
cfg.statemachine.debug = !!!cfg.statemachine.debug; case "sm":
quiet || bot.chat(`sm debug: ${cfg.statemachine.debug}`); case "global":
break; case "meta":
} cfg.statemachine.debug = !!!cfg.statemachine.debug;
case "new": quiet || bot.chat(`sm debug: ${cfg.statemachine.debug}`);
case "save": break;
case "run": }
case "step": case "new":
case "stop": case "save":
// temp case "run":
case "action": case "step":
switch (message_parts2.length) { case "stop":
case 0: // temp
console.warn(`sm ${message_parts[0]}: no name, using defaults`); case "action":
case 1: switch (message_parts2.length) {
// FIXME `this` doesn't work always case 0:
(this.runSM && this || cfg.plugins.statemachine)[message_parts[0] + "SM"](...message_parts2); console.warn(`sm ${message_parts[0]}: no name, using defaults`);
break; case 1:
default: // FIXME `this` doesn't work always
if (["action"].includes(message_parts[0])) { (this.runSM && this || cfg.plugins.statemachine)[message_parts[0] + "SM"](...message_parts2);
(this.runSM && this || cfg.plugins.statemachine)[message_parts[0] + "SM"](...message_parts2); break;
} default:
else { if (["action"].includes(message_parts[0])) {
console.warn(`sm ${message_parts[0]}: more than 1 arg passed`, message_parts2); (this.runSM && this || cfg.plugins.statemachine)[message_parts[0] + "SM"](...message_parts2);
} }
break; else {
} console.warn(`sm ${message_parts[0]}: more than 1 arg passed`, message_parts2);
break; }
case "undo": break;
break; }
case "list": break;
case "status": case "undo":
// TODO current/recent, updateRate break;
const { list, running } = cfg.statemachine; case "list":
console.log("sm list", running, list); case "status":
quiet || bot.chat(`${Object.keys(running).length} of ${Object.keys(list).length} active: ${Object.keys(running)}` // TODO current/recent, updateRate
+ (message_parts[1] === "all" ? `${Object.keys(list)}` : '')); const { list, running } = cfg.statemachine;
break; console.log("sm list", running, list);
case "quiet": quiet || bot.chat(`${Object.keys(running).length} of ${Object.keys(list).length} active: ${Object.keys(running)}`
quiet = cfg.statemachine.quiet = !!!cfg.statemachine.quiet; + (message_parts[1] === "all" ? `${Object.keys(list)}` : ''));
quiet || bot.chat(`sm: ${cfg.statemachine.quiet ? "" : "not "}being quiet`); break;
break; case "quiet":
default: quiet = cfg.statemachine.quiet = !!!cfg.statemachine.quiet;
// TODO general helper from declarative commands object quiet || bot.chat(`sm: ${cfg.statemachine.quiet ? "" : "not "}being quiet`);
quiet || bot.chat(`sm help: !sm [new | step| save | run | list | quiet | debug]`); break;
console.warn("sm unknown command", message_parts); default:
break; // TODO general helper from declarative commands object
} quiet || bot.chat(`sm help: !sm [new | step| save | run | list | quiet | debug]`);
return true; console.warn("sm unknown command", message_parts);
} break;
function load(config) { }
webserver = cfg.statemachine.webserver = config.statemachine?.webserver || webserver; return true;
config.statemachine = cfg.statemachine || { }
webserver: null, function load(config) {
// quiet: true, webserver = cfg.statemachine.webserver = config.statemachine?.webserver || webserver;
quiet: false, config.statemachine = cfg.statemachine || {
list: {}, webserver: null,
running: {}, // quiet: true,
draft: null, quiet: false,
recent: null, list: {},
// debug: null, running: {},
debug: true, draft: null,
updateRate: updateRate recent: null,
}; // debug: null,
cfg = config; debug: true,
bot = cfg.bot; updateRate: updateRate
// pathfinder = bot.pathfinder || bot.loadPlugin(require('mineflayer-pathfinder').pathfinder) };
// mcData = bot.mcData || (bot.mcData = require('minecraft-data')(bot.version)) cfg = config;
init(undefined, webserver); bot = cfg.bot;
updateRate = cfg.statemachine.updateRate || updateRate; // pathfinder = bot.pathfinder || bot.loadPlugin(require('mineflayer-pathfinder').pathfinder)
bot.on('time', tickSM); // mcData = bot.mcData || (bot.mcData = require('minecraft-data')(bot.version))
// bot.once('time', tickSM, 5) init(undefined, webserver);
console.log("sm load", cfg.statemachine); updateRate = cfg.statemachine.updateRate || updateRate;
} bot.on('time', tickSM);
function unload() { // bot.once('time', tickSM, 5)
const { list, running } = cfg.statemachine; console.log("sm load", cfg.statemachine);
bot.off('time', tickSM); }
Object.keys(running).forEach(sm => { function unload() {
stopSM(sm); const { list, running } = cfg.statemachine;
}); bot.off('time', tickSM);
// delete cfg.statemachine; Object.keys(running).forEach(sm => {
cfg.statemachine = null; stopSM(sm);
console.log("sm unload: deleted", cfg.statemachine); });
} // delete cfg.statemachine;
module.exports = { cfg.statemachine = null;
load, unload, command, init, console.log("sm unload: deleted", cfg.statemachine);
newSM, saveSM, loadSM, runSM, stopSM, actionSM, stepSM, debugSM }
module.exports = {
load, unload, command, init,
newSM, saveSM, loadSM, runSM, stopSM, actionSM, stepSM, debugSM
}; };

View File

@@ -30,16 +30,16 @@
}, },
"homepage": "https://github.com/PrismarineJS/prismarine-template#readme", "homepage": "https://github.com/PrismarineJS/prismarine-template#readme",
"devDependencies": { "devDependencies": {
"@types/node": "^14.14.35", "@types/node": "^14.14.41",
"jest": "^26.6.3", "jest": "^26.6.3",
"require-self": "^0.2.3", "require-self": "^0.2.3",
"typescript": "^4.2.3" "typescript": "^4.2.4"
}, },
"dependencies": { "dependencies": {
"dotenv-packed": "^1.2.1", "dotenv-packed": "^1.2.2",
"minecraft-data": "^2.80.0", "minecraft-data": "^2.84.0",
"mineflayer": "^3.4.0", "mineflayer": "^3.6.0",
"mineflayer-armor-manager": "^1.4.0", "mineflayer-armor-manager": "^1.4.1",
"mineflayer-pathfinder": "^1.6.1", "mineflayer-pathfinder": "^1.6.1",
"mineflayer-pvp": "^1.0.2", "mineflayer-pvp": "^1.0.2",
"prismarine-block": "^1.8.0", "prismarine-block": "^1.8.0",
@@ -49,7 +49,7 @@
"prismarine-nbt": "^1.5.0", "prismarine-nbt": "^1.5.0",
"prismarine-recipe": "^1.1.0", "prismarine-recipe": "^1.1.0",
"vec3": "^0.1.7", "vec3": "^0.1.7",
"xstate": "^4.17.0" "xstate": "^4.18.0"
}, },
"files": [ "files": [
"lib/**/*" "lib/**/*"