Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nextgen physics #257

Open
wants to merge 11 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
"peerJsServerFallback": "https://p2p.mcraft.fun",
"promoteServers": [
{
"ip": "kaboom.pw",
"version": "1.20.3",
"description": "Very nice a polite server. Must try for everyone!"
"ip": "grim.mcraft.fun",
"version": "1.19.4"
}
]
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
"https-browserify": "^1.0.0",
"mc-assets": "^0.2.28",
"minecraft-inventory-gui": "github:zardoy/minecraft-inventory-gui#next",
"mineflayer": "github:zardoy/mineflayer",
"mineflayer": "github:GenerelSchwerz/mineflayer",
"mineflayer-pathfinder": "^2.4.4",
"npm-run-all": "^4.1.5",
"os-browserify": "^0.3.0",
Expand All @@ -169,8 +169,8 @@
},
"pnpm": {
"overrides": {
"@nxg-org/mineflayer-physics-util": "latest",
"buffer": "^6.0.3",
"@nxg-org/mineflayer-physics-util": "1.5.8",
"three": "0.154.0",
"diamond-square": "github:zardoy/diamond-square",
"prismarine-block": "github:zardoy/prismarine-block#next-era",
Expand Down
33 changes: 22 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

242 changes: 121 additions & 121 deletions src/controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ let lastCommandTrigger = null as { command: string, time: number } | null

const secondActionActivationTimeout = 300
const secondActionCommands = {
'general.jump' () {
// if (bot.game.gameMode === 'spectator') return
toggleFly()
},
// 'general.jump' () {
// // if (bot.game.gameMode === 'spectator') return
// toggleFly()
// },
'general.forward' () {
setSprinting(true)
}
Expand Down Expand Up @@ -680,135 +680,135 @@ const makeInterval = (fn, interval) => {
return cleanup
}

const isFlying = () => bot.physics.gravity === 0
let endFlyLoop: ReturnType<typeof makeInterval> | undefined
// const isFlying = () => bot.physics.gravity === 0
// let endFlyLoop: ReturnType<typeof makeInterval> | undefined

const currentFlyVector = new Vec3(0, 0, 0)
window.currentFlyVector = currentFlyVector
// const currentFlyVector = new Vec3(0, 0, 0)
// window.currentFlyVector = currentFlyVector

// todo cleanup
const flyingPressedKeys = {
down: false,
up: false
}
// const flyingPressedKeys = {
// down: false,
// up: false
// }

const startFlyLoop = () => {
if (!isFlying()) return
endFlyLoop?.()
// const startFlyLoop = () => {
// if (!isFlying()) return
// endFlyLoop?.()

endFlyLoop = makeInterval(() => {
if (!bot) {
endFlyLoop?.()
return
}
// endFlyLoop = makeInterval(() => {
// if (!bot) {
// endFlyLoop?.()
// return
// }

bot.entity.position.add(currentFlyVector.clone().multiply(new Vec3(0, 0.5, 0)))
}, 50)
}
// bot.entity.position.add(currentFlyVector.clone().multiply(new Vec3(0, 0.5, 0)))
// }, 50)
// }

// todo we will get rid of patching it when refactor controls
let originalSetControlState
const patchedSetControlState = (action, state) => {
if (!isFlying()) {
return originalSetControlState(action, state)
}

const actionPerFlyVector = {
jump: new Vec3(0, 1, 0),
sneak: new Vec3(0, -1, 0),
}

const changeVec = actionPerFlyVector[action]
if (!changeVec) {
return originalSetControlState(action, state)
}
if (flyingPressedKeys[state === 'jump' ? 'up' : 'down'] === state) return
const toAddVec = changeVec.scaled(state ? 1 : -1)
for (const coord of ['x', 'y', 'z']) {
if (toAddVec[coord] === 0) continue
if (currentFlyVector[coord] === toAddVec[coord]) return
}
currentFlyVector.add(toAddVec)
flyingPressedKeys[state === 'jump' ? 'up' : 'down'] = state
}

const startFlying = (sendAbilities = true) => {
bot.entity['creativeFly'] = true
if (sendAbilities) {
bot._client.write('abilities', {
flags: 2,
})
}
// window.flyingSpeed will be removed
bot.physics['airborneAcceleration'] = window.flyingSpeed ?? 0.1 // todo use abilities
bot.entity.velocity = new Vec3(0, 0, 0)
bot.creative.startFlying()
startFlyLoop()
}

const endFlying = (sendAbilities = true) => {
bot.entity['creativeFly'] = false
if (bot.physics.gravity !== 0) return
if (sendAbilities) {
bot._client.write('abilities', {
flags: 0,
})
}
Object.assign(flyingPressedKeys, {
up: false,
down: false
})
currentFlyVector.set(0, 0, 0)
bot.physics['airborneAcceleration'] = standardAirborneAcceleration
bot.creative.stopFlying()
endFlyLoop?.()
}

let allowFlying = false
// let originalSetControlState
// const patchedSetControlState = (action, state) => {
// if (!isFlying()) {
// return originalSetControlState(action, state)
// }

// const actionPerFlyVector = {
// jump: new Vec3(0, 1, 0),
// sneak: new Vec3(0, -1, 0),
// }

// const changeVec = actionPerFlyVector[action]
// if (!changeVec) {
// return originalSetControlState(action, state)
// }
// if (flyingPressedKeys[state === 'jump' ? 'up' : 'down'] === state) return
// const toAddVec = changeVec.scaled(state ? 1 : -1)
// for (const coord of ['x', 'y', 'z']) {
// if (toAddVec[coord] === 0) continue
// if (currentFlyVector[coord] === toAddVec[coord]) return
// }
// currentFlyVector.add(toAddVec)
// flyingPressedKeys[state === 'jump' ? 'up' : 'down'] = state
// }

// const startFlying = (sendAbilities = true) => {
// bot.entity['creativeFly'] = true
// if (sendAbilities) {
// bot._client.write('abilities', {
// flags: 2,
// })
// }
// // window.flyingSpeed will be removed
// bot.physics['airborneAcceleration'] = window.flyingSpeed ?? 0.1 // todo use abilities
// bot.entity.velocity = new Vec3(0, 0, 0)
// bot.creative.startFlying()
// startFlyLoop()
// }

// const endFlying = (sendAbilities = true) => {
// bot.entity['creativeFly'] = false
// if (bot.physics.gravity !== 0) return
// if (sendAbilities) {
// bot._client.write('abilities', {
// flags: 0,
// })
// }
// Object.assign(flyingPressedKeys, {
// up: false,
// down: false
// })
// currentFlyVector.set(0, 0, 0)
// bot.physics['airborneAcceleration'] = standardAirborneAcceleration
// bot.creative.stopFlying()
// endFlyLoop?.()
// }

const allowFlying = false

export const onBotCreate = () => {
let wasSpectatorFlying = false
bot._client.on('abilities', ({ flags }) => {
if (flags & 2) { // flying
toggleFly(true, false)
} else {
toggleFly(false, false)
}
allowFlying = !!(flags & 4)
})
const gamemodeCheck = () => {
if (bot.game.gameMode === 'spectator') {
toggleFly(true, false)
wasSpectatorFlying = true
} else if (wasSpectatorFlying) {
toggleFly(false, false)
wasSpectatorFlying = false
}
}
bot.on('game', () => {
gamemodeCheck()
})
bot.on('login', () => {
gamemodeCheck()
})
// let wasSpectatorFlying = false
// bot._client.on('abilities', ({ flags }) => {
// if (flags & 2) { // flying
// toggleFly(true, false)
// } else {
// toggleFly(false, false)
// }
// allowFlying = !!(flags & 4)
// })
// const gamemodeCheck = () => {
// if (bot.game.gameMode === 'spectator') {
// toggleFly(true, false)
// wasSpectatorFlying = true
// } else if (wasSpectatorFlying) {
// toggleFly(false, false)
// wasSpectatorFlying = false
// }
// }
// bot.on('game', () => {
// gamemodeCheck()
// })
// bot.on('login', () => {
// gamemodeCheck()
// })
}

const standardAirborneAcceleration = 0.02
const toggleFly = (newState = !isFlying(), sendAbilities?: boolean) => {
// if (bot.game.gameMode !== 'creative' && bot.game.gameMode !== 'spectator') return
if (!allowFlying) return
if (bot.setControlState !== patchedSetControlState) {
originalSetControlState = bot.setControlState
bot.setControlState = patchedSetControlState
}

if (newState) {
startFlying(sendAbilities)
} else {
endFlying(sendAbilities)
}
gameAdditionalState.isFlying = isFlying()
}
// const standardAirborneAcceleration = 0.02
// const toggleFly = (newState = !isFlying(), sendAbilities?: boolean) => {
// // if (bot.game.gameMode !== 'creative' && bot.game.gameMode !== 'spectator') return
// if (!allowFlying) return
// if (bot.setControlState !== patchedSetControlState) {
// originalSetControlState = bot.setControlState
// bot.setControlState = patchedSetControlState
// }

// if (newState) {
// startFlying(sendAbilities)
// } else {
// endFlying(sendAbilities)
// }
// gameAdditionalState.isFlying = isFlying()
// }
// #endregion

const selectItem = async () => {
Expand Down
Loading
Loading