@nxg-org/mineflayer-util-plugin
Version:
mineflayer utils for NextGEN mineflayer plugins.
112 lines (111 loc) • 4.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PredictiveWorld = void 0;
const vec3_1 = require("vec3");
const iterators_1 = require("../calcs/iterators");
const static_1 = require("../static");
/**
* A class dedicated to predictive logic.
*
* Currently, this class can predict explosion damages of crystals using a custom world.
*/
class PredictiveWorld {
constructor(bot) {
this.blocks = {};
this.originalWorld = bot.world;
}
raycast(from, direction, range, matcher = null) {
const iter = new iterators_1.RaycastIterator(from, direction, range);
let pos = iter.next();
while (pos) {
const position = new vec3_1.Vec3(pos.x, pos.y, pos.z);
const block = this.getBlock(position);
if (block && (!matcher || matcher(block))) {
const intersect = iter.intersect(block.shapes, position);
if (intersect) {
//@ts-expect-error 2
block.face = intersect.face;
//@ts-expect-error
block.intersect = intersect.pos;
return block;
}
}
pos = iter.next();
}
return null;
}
/**
* this works
* @param {Block} block
*/
setBlock(pos, block) {
var _a;
var _b, _c;
(_a = (_b = this.blocks)[_c = pos.toString()]) !== null && _a !== void 0 ? _a : (_b[_c] = block);
}
/**
* @param {Overwrites} blocks Blocks indexed by position.toString()
*/
setBlocks(blocks) {
for (const index in blocks)
this.blocks[index] = blocks[index];
}
/**
* @param {Vec3} pos
* @returns {Block | null} Block at position.
*/
getBlock(pos) {
var _a;
const pblock = this.blocks[pos.toString()];
if (pblock !== undefined && pblock !== null)
return pblock;
return (_a = this.originalWorld.getBlock(pos)) !== null && _a !== void 0 ? _a : null;
}
removeBlock(pos, force) {
if (force) {
delete this.blocks[pos.toString()];
}
else {
const realBlock = this.originalWorld.getBlock(pos);
if (realBlock)
this.blocks[pos.toString()] = realBlock;
else
delete this.blocks[pos.toString()];
}
}
removeBlocks(positions, force) {
positions.forEach((pos) => this.removeBlock(pos, force));
}
/**
* @param playerPos Position of effected entity.
* @param explosionPos Position of explosion origin.
* @param block bot.block
* @returns List of affected blocks that potentially protect the entity.
*/
getExplosionAffectedBlocks(entityBB, explosionPos) {
let blocks = {};
const { x: xWidth, y: yWidth, z: zWidth } = entityBB.heightAndWidths();
const dx = 1 / (xWidth * 2 + 1);
const dy = 1 / (yWidth * 2 + 1);
const dz = 1 / (zWidth * 2 + 1);
const d3 = (1 - Math.floor(1 / dx) * dx) / 2;
const d4 = (1 - Math.floor(1 / dz) * dz) / 2;
const pos = new vec3_1.Vec3(0, 0, 0);
for (pos.y = entityBB.minY; pos.y <= entityBB.maxY; pos.y += yWidth * dy) {
for (pos.x = entityBB.minX + d3; pos.x <= entityBB.maxX; pos.x += xWidth * dx) {
for (pos.z = entityBB.minZ + d4; pos.z <= entityBB.maxZ; pos.z += zWidth * dz) {
const dir = pos.minus(explosionPos);
const range = dir.norm();
const potentialBlock = this.raycast(explosionPos, dir.normalize(), range);
if (potentialBlock !== null)
blocks[potentialBlock.position.toString()] = potentialBlock;
}
}
}
return blocks;
}
loadProtectiveBlocks(playerPos, explosionPos) {
this.setBlocks(this.getExplosionAffectedBlocks(static_1.AABBUtils.getPlayerAABBRaw(playerPos), explosionPos));
}
}
exports.PredictiveWorld = PredictiveWorld;