@nxg-org/mineflayer-util-plugin 1.8.4 → 1.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,188 +1,188 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PredictiveFunctions = void 0;
4
- const vec3_1 = require("vec3");
5
- const predictiveWorld_1 = require("./worldRelated/predictiveWorld");
6
- const static_1 = require("./static");
7
- const armorPieces = ["head", "torso", "legs", "feet"];
8
- // https://minecraft.fandom.com/wiki/Explosion
9
- // Use bot.world, there's no typing yet.
10
- function calcExposure(playerPos, explosionPos, world) {
11
- const dx = 1 / (0.6 * 2 + 1);
12
- const dy = 1 / (1.8 * 2 + 1);
13
- const dz = 1 / (0.6 * 2 + 1);
14
- const d3 = (1 - Math.floor(1 / dx) * dx) / 2;
15
- const d4 = (1 - Math.floor(1 / dz) * dz) / 2;
16
- let sampled = 0;
17
- let exposed = 0;
18
- const pos = new vec3_1.Vec3(0, 0, 0);
19
- for (pos.y = playerPos.y; pos.y <= playerPos.y + 1.8; pos.y += 1.8 * dy) {
20
- for (pos.x = playerPos.x - 0.3 + d3; pos.x <= playerPos.x + 0.3; pos.x += 0.6 * dx) {
21
- for (pos.z = playerPos.z - 0.3 + d4; pos.z <= playerPos.z + 0.3; pos.z += 0.6 * dz) {
22
- const dir = pos.minus(explosionPos);
23
- const range = dir.norm();
24
- if (world.raycast(explosionPos, dir.normalize(), range) === null) {
25
- exposed++;
26
- }
27
- sampled++;
28
- }
29
- }
30
- }
31
- return exposed / sampled;
32
- }
33
- function calcExposureAABB(entityBB, explosionPos, world /* prismarine-world*/) {
34
- const xWidth = entityBB.maxX - entityBB.minX;
35
- const yWidth = entityBB.maxY - entityBB.minY;
36
- const zWidth = entityBB.maxZ - entityBB.minZ;
37
- const dx = 1 / (xWidth * 2 + 1);
38
- const dy = 1 / (yWidth * 2 + 1);
39
- const dz = 1 / (zWidth * 2 + 1);
40
- const d3 = (1 - Math.floor(1 / dx) * dx) / 2;
41
- const d4 = (1 - Math.floor(1 / dz) * dz) / 2;
42
- let sampled = 0;
43
- let exposed = 0;
44
- const pos = new vec3_1.Vec3(0, 0, 0);
45
- for (pos.y = entityBB.minY; pos.y <= entityBB.maxY; pos.y += yWidth * dy) {
46
- for (pos.x = entityBB.minX + d3; pos.x <= entityBB.maxX; pos.x += xWidth * dx) {
47
- for (pos.z = entityBB.minZ + d4; pos.z <= entityBB.maxZ; pos.z += zWidth * dz) {
48
- const dir = pos.minus(explosionPos);
49
- const range = dir.norm();
50
- if (world.raycast(explosionPos, dir.normalize(), range) === null) {
51
- exposed++;
52
- }
53
- sampled++;
54
- }
55
- }
56
- }
57
- return exposed / sampled;
58
- }
59
- // https://minecraft.fandom.com/wiki/Armor#Damage_protection
60
- function getDamageAfterAbsorb(damages, armorValue, toughness) {
61
- const var3 = 2 + toughness / 4;
62
- const var4 = Math.min(Math.max(armorValue - damages / var3, armorValue * 0.2), 20);
63
- return damages * (1 - var4 / 25);
64
- }
65
- // https://minecraft.fandom.com/wiki/Attribute#Operations
66
- function getAttributeValue(prop) {
67
- let X = prop.value;
68
- for (const mod of prop.modifiers) {
69
- if (mod.operation !== 0)
70
- continue;
71
- X += mod.amount;
72
- }
73
- let Y = X;
74
- for (const mod of prop.modifiers) {
75
- if (mod.operation !== 1)
76
- continue;
77
- Y += X * mod.amount;
78
- }
79
- for (const mod of prop.modifiers) {
80
- if (mod.operation !== 2)
81
- continue;
82
- Y += Y * mod.amount;
83
- }
84
- return Y;
85
- }
86
- function getDamageWithEnchantments(damage, equipment) {
87
- const enchantments = equipment.some((e) => !!e)
88
- ? equipment
89
- .map((armor) => {
90
- var _a;
91
- return (_a = armor === null || armor === void 0 ? void 0 : armor.enchants.map((enchant) => {
92
- switch (enchant === null || enchant === void 0 ? void 0 : enchant.name) {
93
- case "protection":
94
- return enchant.lvl;
95
- case "blast_protection":
96
- return enchant.lvl * 2;
97
- default:
98
- return 0;
99
- }
100
- }).reduce((b, a) => b + a, 0)) !== null && _a !== void 0 ? _a : [0];
101
- })
102
- .reduce((b, a) => b + a, 0)
103
- : 0;
104
- return damage * (1 - Math.min(enchantments, 20) / 25);
105
- }
106
- const DIFFICULTY_VALUES = {
107
- peaceful: 0,
108
- easy: 1,
109
- normal: 2,
110
- hard: 3,
111
- };
112
- class PredictiveFunctions {
113
- constructor(bot) {
114
- this.bot = bot;
115
- this.resistanceIndex = 11;
116
- const effects = bot.registry.effects;
117
- for (const effectId in effects) {
118
- const effect = effects[effectId];
119
- if (effect.name.includes("esistance")) {
120
- this.resistanceIndex = Number(effectId);
121
- break;
122
- }
123
- }
124
- this.damageMultiplier = bot.registry.version[">="]("1.9") ? 8 : 7; // for 1.12+ 8 for 1.8 TODO check when the change occur (likely 1.9)
125
- this.armorToughnessKey = bot.registry.version[">="]("1.16")
126
- ? "minecraft:generic.armor_toughness"
127
- : "generic.armorToughness"; // was renamed in 1.16
128
- this.armorProtectionKey = bot.registry.version[">="]("1.16") ? "minecraft:generic.armor" : "generic.armor"; // was renamed in 1.16
129
- this.world = new predictiveWorld_1.PredictiveWorld(bot);
130
- }
131
- //There's a mistyping in mineflayer. Effect[] is not accurate. You cannot map over it.
132
- getDamageWithEffects(damage, effects) {
133
- var _a, _b;
134
- const resistanceLevel = (_b = (_a = effects === null || effects === void 0 ? void 0 : effects[this.resistanceIndex]) === null || _a === void 0 ? void 0 : _a.amplifier) !== null && _b !== void 0 ? _b : 0;
135
- return damage * (1 - resistanceLevel / 5);
136
- }
137
- placeBlocks(blocks) {
138
- this.world.setBlocks(blocks);
139
- }
140
- removePredictedBlocks(positions, force = false) {
141
- this.world.removeBlocks(positions, force);
142
- }
143
- selfExplosionDamage(sourcePos, power, rawDamages = false) {
144
- const distance = this.bot.entity.position.distanceTo(sourcePos);
145
- const radius = 2 * power;
146
- if (distance >= radius)
147
- return 0;
148
- const exposure = calcExposure(this.bot.entity.position, sourcePos, this.world);
149
- const impact = (1 - distance / radius) * exposure;
150
- let damages = Math.floor((impact * impact + impact) * this.damageMultiplier * power + 1);
151
- // The following modifiers are constant for the input bot.entity and doesnt depend
152
- // on the source position, so if the goal is to compare between positions they can be
153
- // ignored to save computations
154
- if (!rawDamages && this.bot.entity.attributes[this.armorProtectionKey]) {
155
- const armor = getAttributeValue(this.bot.entity.attributes[this.armorProtectionKey]);
156
- const armorToughness = getAttributeValue(this.bot.entity.attributes[this.armorToughnessKey]);
157
- damages = getDamageAfterAbsorb(damages, armor, armorToughness);
158
- damages = getDamageWithEnchantments(damages, this.bot.entity.equipment);
159
- damages = this.getDamageWithEffects(damages, this.bot.entity.effects);
160
- damages *= DIFFICULTY_VALUES[this.bot.game.difficulty] * 0.5;
161
- }
162
- return Math.floor(damages);
163
- }
164
- getExplosionDamage(targetEntity, sourcePos, power, rawDamages = false) {
165
- const distance = targetEntity.position.distanceTo(sourcePos);
166
- const radius = 2 * power;
167
- if (distance >= radius)
168
- return 0;
169
- const exposure = calcExposureAABB(static_1.AABBUtils.getEntityAABB(targetEntity), sourcePos, this.world);
170
- const impact = (1 - distance / radius) * exposure;
171
- let damages = Math.floor((impact * impact + impact) * this.damageMultiplier * power + 1);
172
- // The following modifiers are constant for the input targetEntity and doesnt depend
173
- // on the source position, so if the goal is to compare between positions they can be
174
- // ignored to save computations
175
- if (!rawDamages && targetEntity.attributes[this.armorProtectionKey]) {
176
- const armor = getAttributeValue(targetEntity.attributes[this.armorProtectionKey]);
177
- const armorToughness = getAttributeValue(targetEntity.attributes[this.armorToughnessKey]);
178
- damages = getDamageAfterAbsorb(damages, armor, armorToughness);
179
- damages = getDamageWithEnchantments(damages, targetEntity.equipment);
180
- damages = this.getDamageWithEffects(damages, targetEntity.effects);
181
- if (targetEntity.type === "player") {
182
- damages *= DIFFICULTY_VALUES[this.bot.game.difficulty] * 0.5;
183
- }
184
- }
185
- return Math.floor(damages);
186
- }
187
- }
188
- exports.PredictiveFunctions = PredictiveFunctions;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PredictiveFunctions = void 0;
4
+ const vec3_1 = require("vec3");
5
+ const predictiveWorld_1 = require("./worldRelated/predictiveWorld");
6
+ const static_1 = require("./static");
7
+ const armorPieces = ["head", "torso", "legs", "feet"];
8
+ // https://minecraft.fandom.com/wiki/Explosion
9
+ // Use bot.world, there's no typing yet.
10
+ function calcExposure(playerPos, explosionPos, world) {
11
+ const dx = 1 / (0.6 * 2 + 1);
12
+ const dy = 1 / (1.8 * 2 + 1);
13
+ const dz = 1 / (0.6 * 2 + 1);
14
+ const d3 = (1 - Math.floor(1 / dx) * dx) / 2;
15
+ const d4 = (1 - Math.floor(1 / dz) * dz) / 2;
16
+ let sampled = 0;
17
+ let exposed = 0;
18
+ const pos = new vec3_1.Vec3(0, 0, 0);
19
+ for (pos.y = playerPos.y; pos.y <= playerPos.y + 1.8; pos.y += 1.8 * dy) {
20
+ for (pos.x = playerPos.x - 0.3 + d3; pos.x <= playerPos.x + 0.3; pos.x += 0.6 * dx) {
21
+ for (pos.z = playerPos.z - 0.3 + d4; pos.z <= playerPos.z + 0.3; pos.z += 0.6 * dz) {
22
+ const dir = pos.minus(explosionPos);
23
+ const range = dir.norm();
24
+ if (world.raycast(explosionPos, dir.normalize(), range) === null) {
25
+ exposed++;
26
+ }
27
+ sampled++;
28
+ }
29
+ }
30
+ }
31
+ return exposed / sampled;
32
+ }
33
+ function calcExposureAABB(entityBB, explosionPos, world /* prismarine-world*/) {
34
+ const xWidth = entityBB.maxX - entityBB.minX;
35
+ const yWidth = entityBB.maxY - entityBB.minY;
36
+ const zWidth = entityBB.maxZ - entityBB.minZ;
37
+ const dx = 1 / (xWidth * 2 + 1);
38
+ const dy = 1 / (yWidth * 2 + 1);
39
+ const dz = 1 / (zWidth * 2 + 1);
40
+ const d3 = (1 - Math.floor(1 / dx) * dx) / 2;
41
+ const d4 = (1 - Math.floor(1 / dz) * dz) / 2;
42
+ let sampled = 0;
43
+ let exposed = 0;
44
+ const pos = new vec3_1.Vec3(0, 0, 0);
45
+ for (pos.y = entityBB.minY; pos.y <= entityBB.maxY; pos.y += yWidth * dy) {
46
+ for (pos.x = entityBB.minX + d3; pos.x <= entityBB.maxX; pos.x += xWidth * dx) {
47
+ for (pos.z = entityBB.minZ + d4; pos.z <= entityBB.maxZ; pos.z += zWidth * dz) {
48
+ const dir = pos.minus(explosionPos);
49
+ const range = dir.norm();
50
+ if (world.raycast(explosionPos, dir.normalize(), range) === null) {
51
+ exposed++;
52
+ }
53
+ sampled++;
54
+ }
55
+ }
56
+ }
57
+ return exposed / sampled;
58
+ }
59
+ // https://minecraft.fandom.com/wiki/Armor#Damage_protection
60
+ function getDamageAfterAbsorb(damages, armorValue, toughness) {
61
+ const var3 = 2 + toughness / 4;
62
+ const var4 = Math.min(Math.max(armorValue - damages / var3, armorValue * 0.2), 20);
63
+ return damages * (1 - var4 / 25);
64
+ }
65
+ // https://minecraft.fandom.com/wiki/Attribute#Operations
66
+ function getAttributeValue(prop) {
67
+ let X = prop.value;
68
+ for (const mod of prop.modifiers) {
69
+ if (mod.operation !== 0)
70
+ continue;
71
+ X += mod.amount;
72
+ }
73
+ let Y = X;
74
+ for (const mod of prop.modifiers) {
75
+ if (mod.operation !== 1)
76
+ continue;
77
+ Y += X * mod.amount;
78
+ }
79
+ for (const mod of prop.modifiers) {
80
+ if (mod.operation !== 2)
81
+ continue;
82
+ Y += Y * mod.amount;
83
+ }
84
+ return Y;
85
+ }
86
+ function getDamageWithEnchantments(damage, equipment) {
87
+ const enchantments = equipment.some((e) => !!e)
88
+ ? equipment
89
+ .map((armor) => {
90
+ var _a;
91
+ return (_a = armor === null || armor === void 0 ? void 0 : armor.enchants.map((enchant) => {
92
+ switch (enchant === null || enchant === void 0 ? void 0 : enchant.name) {
93
+ case "protection":
94
+ return enchant.lvl;
95
+ case "blast_protection":
96
+ return enchant.lvl * 2;
97
+ default:
98
+ return 0;
99
+ }
100
+ }).reduce((b, a) => b + a, 0)) !== null && _a !== void 0 ? _a : [0];
101
+ })
102
+ .reduce((b, a) => b + a, 0)
103
+ : 0;
104
+ return damage * (1 - Math.min(enchantments, 20) / 25);
105
+ }
106
+ const DIFFICULTY_VALUES = {
107
+ peaceful: 0,
108
+ easy: 1,
109
+ normal: 2,
110
+ hard: 3,
111
+ };
112
+ class PredictiveFunctions {
113
+ constructor(bot) {
114
+ this.bot = bot;
115
+ this.resistanceIndex = 11;
116
+ const effects = bot.registry.effects;
117
+ for (const effectId in effects) {
118
+ const effect = effects[effectId];
119
+ if (effect.name.includes("esistance")) {
120
+ this.resistanceIndex = Number(effectId);
121
+ break;
122
+ }
123
+ }
124
+ this.damageMultiplier = bot.registry.version[">="]("1.9") ? 8 : 7; // for 1.12+ 8 for 1.8 TODO check when the change occur (likely 1.9)
125
+ this.armorToughnessKey = bot.registry.version[">="]("1.16")
126
+ ? "minecraft:generic.armor_toughness"
127
+ : "generic.armorToughness"; // was renamed in 1.16
128
+ this.armorProtectionKey = bot.registry.version[">="]("1.16") ? "minecraft:generic.armor" : "generic.armor"; // was renamed in 1.16
129
+ this.world = new predictiveWorld_1.PredictiveWorld(bot);
130
+ }
131
+ //There's a mistyping in mineflayer. Effect[] is not accurate. You cannot map over it.
132
+ getDamageWithEffects(damage, effects) {
133
+ var _a, _b;
134
+ const resistanceLevel = (_b = (_a = effects === null || effects === void 0 ? void 0 : effects[this.resistanceIndex]) === null || _a === void 0 ? void 0 : _a.amplifier) !== null && _b !== void 0 ? _b : 0;
135
+ return damage * (1 - resistanceLevel / 5);
136
+ }
137
+ placeBlocks(blocks) {
138
+ this.world.setBlocks(blocks);
139
+ }
140
+ removePredictedBlocks(positions, force = false) {
141
+ this.world.removeBlocks(positions, force);
142
+ }
143
+ selfExplosionDamage(sourcePos, power, rawDamages = false) {
144
+ const distance = this.bot.entity.position.distanceTo(sourcePos);
145
+ const radius = 2 * power;
146
+ if (distance >= radius)
147
+ return 0;
148
+ const exposure = calcExposure(this.bot.entity.position, sourcePos, this.world);
149
+ const impact = (1 - distance / radius) * exposure;
150
+ let damages = Math.floor((impact * impact + impact) * this.damageMultiplier * power + 1);
151
+ // The following modifiers are constant for the input bot.entity and doesnt depend
152
+ // on the source position, so if the goal is to compare between positions they can be
153
+ // ignored to save computations
154
+ if (!rawDamages && this.bot.entity.attributes[this.armorProtectionKey]) {
155
+ const armor = getAttributeValue(this.bot.entity.attributes[this.armorProtectionKey]);
156
+ const armorToughness = getAttributeValue(this.bot.entity.attributes[this.armorToughnessKey]);
157
+ damages = getDamageAfterAbsorb(damages, armor, armorToughness);
158
+ damages = getDamageWithEnchantments(damages, this.bot.entity.equipment);
159
+ damages = this.getDamageWithEffects(damages, this.bot.entity.effects);
160
+ damages *= DIFFICULTY_VALUES[this.bot.game.difficulty] * 0.5;
161
+ }
162
+ return Math.floor(damages);
163
+ }
164
+ getExplosionDamage(targetEntity, sourcePos, power, rawDamages = false) {
165
+ const distance = targetEntity.position.distanceTo(sourcePos);
166
+ const radius = 2 * power;
167
+ if (distance >= radius)
168
+ return 0;
169
+ const exposure = calcExposureAABB(static_1.AABBUtils.getEntityAABB(targetEntity), sourcePos, this.world);
170
+ const impact = (1 - distance / radius) * exposure;
171
+ let damages = Math.floor((impact * impact + impact) * this.damageMultiplier * power + 1);
172
+ // The following modifiers are constant for the input targetEntity and doesnt depend
173
+ // on the source position, so if the goal is to compare between positions they can be
174
+ // ignored to save computations
175
+ if (!rawDamages && targetEntity.attributes[this.armorProtectionKey]) {
176
+ const armor = getAttributeValue(targetEntity.attributes[this.armorProtectionKey]);
177
+ const armorToughness = getAttributeValue(targetEntity.attributes[this.armorToughnessKey]);
178
+ damages = getDamageAfterAbsorb(damages, armor, armorToughness);
179
+ damages = getDamageWithEnchantments(damages, targetEntity.equipment);
180
+ damages = this.getDamageWithEffects(damages, targetEntity.effects);
181
+ if (targetEntity.type === "player") {
182
+ damages *= DIFFICULTY_VALUES[this.bot.game.difficulty] * 0.5;
183
+ }
184
+ }
185
+ return Math.floor(damages);
186
+ }
187
+ }
188
+ exports.PredictiveFunctions = PredictiveFunctions;
@@ -1,21 +1,21 @@
1
- import { Bot } from "mineflayer";
2
- import type { Block } from "prismarine-block";
3
- import type { Entity } from "prismarine-entity";
4
- import { Vec3 } from "vec3";
5
- import AABB from "./calcs/aabb";
6
- import { BlockFace } from "./calcs/iterators";
7
- type RayAddtion = {
8
- intersect: Vec3;
9
- face: BlockFace;
10
- };
11
- export type EntityRaycastReturn = (Block | Entity) & RayAddtion;
12
- export declare class RayTraceFunctions {
13
- private bot;
14
- constructor(bot: Bot);
15
- entityRaytrace(startPos: Vec3, dir: Vec3, maxDistance?: number, matcher?: (e: Entity) => boolean): EntityRaycastReturn | null;
16
- entityRaytraceRaw(startPos: Vec3, dir: Vec3, aabbMap: {
17
- [id: string]: AABB;
18
- }, maxDistance?: number, matcher?: (e: Entity) => boolean): EntityRaycastReturn | null;
19
- entityAtEntityCursor(entity: Entity, maxDistance?: number): Entity | null;
20
- }
21
- export {};
1
+ import { Bot } from "mineflayer";
2
+ import type { Block } from "prismarine-block";
3
+ import type { Entity } from "prismarine-entity";
4
+ import { Vec3 } from "vec3";
5
+ import AABB from "./calcs/aabb";
6
+ import { BlockFace } from "./calcs/iterators";
7
+ type RayAddtion = {
8
+ intersect: Vec3;
9
+ face: BlockFace;
10
+ };
11
+ export type EntityRaycastReturn = (Block | Entity) & RayAddtion;
12
+ export declare class RayTraceFunctions {
13
+ private bot;
14
+ constructor(bot: Bot);
15
+ entityRaytrace(startPos: Vec3, dir: Vec3, maxDistance?: number, matcher?: (e: Entity) => boolean): EntityRaycastReturn | null;
16
+ entityRaytraceRaw(startPos: Vec3, dir: Vec3, aabbMap: {
17
+ [id: string]: AABB;
18
+ }, maxDistance?: number, matcher?: (e: Entity) => boolean): EntityRaycastReturn | null;
19
+ entityAtEntityCursor(entity: Entity, maxDistance?: number): Entity | null;
20
+ }
21
+ export {};
@@ -1,85 +1,85 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RayTraceFunctions = void 0;
4
- const vec3_1 = require("vec3");
5
- const iterators_1 = require("./calcs/iterators");
6
- const static_1 = require("./static");
7
- class RayTraceFunctions {
8
- constructor(bot) {
9
- this.bot = bot;
10
- }
11
- entityRaytrace(startPos, dir, maxDistance = 3.5, matcher) {
12
- matcher || (matcher = (e) => true);
13
- const aabbMap = {};
14
- Object.values(this.bot.entities)
15
- .filter((e) => e.username !== this.bot.entity.username && matcher(e))
16
- .forEach((e) => (aabbMap[e.id] = static_1.AABBUtils.getEntityAABB(e)));
17
- return this.entityRaytraceRaw(startPos, dir, aabbMap, maxDistance, matcher);
18
- }
19
- entityRaytraceRaw(startPos, dir, aabbMap, maxDistance = 3.5, matcher) {
20
- var _a;
21
- matcher || (matcher = (e) => true);
22
- const eyePos = this.bot.entity.position.offset(0, this.bot.entity.height, 0);
23
- Object.entries(aabbMap).forEach(([id, bb]) => {
24
- if (bb.distanceToVec(eyePos) > maxDistance)
25
- delete aabbMap[id];
26
- });
27
- dir = dir.normalize();
28
- let returnVal = this.bot.world.raycast(startPos, dir, maxDistance);
29
- if (returnVal && Object.keys(aabbMap).length === 0)
30
- return returnVal;
31
- maxDistance = (_a = returnVal === null || returnVal === void 0 ? void 0 : returnVal.intersect.distanceTo(startPos)) !== null && _a !== void 0 ? _a : maxDistance;
32
- const iterator = new iterators_1.RaycastIterator(startPos, dir, maxDistance);
33
- for (const id in aabbMap) {
34
- const aabb = aabbMap[id];
35
- const pt = aabb.minPoint();
36
- const entity = this.bot.entities[id];
37
- const intersect = iterator.intersect(aabb.toShapeFromMin(), pt);
38
- if (intersect) {
39
- const entityDir = pt.minus(eyePos);
40
- const sign = Math.sign(entityDir.dot(dir));
41
- if (sign !== -1) {
42
- const dist = eyePos.distanceTo(intersect.pos);
43
- if (dist <= maxDistance) {
44
- maxDistance = dist;
45
- if (matcher(entity)) {
46
- returnVal = entity;
47
- returnVal.intersect = intersect.pos;
48
- returnVal.face = intersect.face;
49
- }
50
- }
51
- }
52
- }
53
- }
54
- return returnVal;
55
- }
56
- entityAtEntityCursor(entity, maxDistance = 3.5) {
57
- var _a;
58
- const block = this.bot.blockAtCursor(maxDistance);
59
- maxDistance = (_a = block === null || block === void 0 ? void 0 : block.intersect.distanceTo(this.bot.entity.position)) !== null && _a !== void 0 ? _a : maxDistance;
60
- const entities = Object.values(this.bot.entities).filter((e) => e.type !== "object" && e.username !== entity.username && e.position.distanceTo(entity.position) <= maxDistance);
61
- const dir = new vec3_1.Vec3(-Math.sin(entity.yaw) * Math.cos(entity.pitch), Math.sin(entity.pitch), -Math.cos(entity.yaw) * Math.cos(entity.pitch));
62
- const iterator = new iterators_1.RaycastIterator(entity.position.offset(0, entity.height, 0), dir.normalize(), maxDistance);
63
- let targetEntity = null;
64
- let targetDist = maxDistance;
65
- for (let i = 0; i < entities.length; i++) {
66
- const e = entities[i];
67
- const w = (e.height >= 1.62 && e.height <= 1.99) || e.height === 2.9 ? 0.3 : e.height / 2;
68
- const shapes = [[-w, 0, -w, w, e.height + (e.height === 1.62 ? 0.18 : 0), w]];
69
- const intersect = iterator.intersect(shapes, e.position);
70
- if (intersect) {
71
- const entityDir = e.position.minus(entity.position); // Can be combined into 1 line
72
- const sign = Math.sign(entityDir.dot(dir));
73
- if (sign !== -1) {
74
- const dist = entity.position.distanceTo(intersect.pos);
75
- if (dist < targetDist) {
76
- targetEntity = e;
77
- targetDist = dist;
78
- }
79
- }
80
- }
81
- }
82
- return targetEntity;
83
- }
84
- }
85
- exports.RayTraceFunctions = RayTraceFunctions;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RayTraceFunctions = void 0;
4
+ const vec3_1 = require("vec3");
5
+ const iterators_1 = require("./calcs/iterators");
6
+ const static_1 = require("./static");
7
+ class RayTraceFunctions {
8
+ constructor(bot) {
9
+ this.bot = bot;
10
+ }
11
+ entityRaytrace(startPos, dir, maxDistance = 3.5, matcher) {
12
+ matcher || (matcher = (e) => true);
13
+ const aabbMap = {};
14
+ Object.values(this.bot.entities)
15
+ .filter((e) => e.username !== this.bot.entity.username && matcher(e))
16
+ .forEach((e) => (aabbMap[e.id] = static_1.AABBUtils.getEntityAABB(e)));
17
+ return this.entityRaytraceRaw(startPos, dir, aabbMap, maxDistance, matcher);
18
+ }
19
+ entityRaytraceRaw(startPos, dir, aabbMap, maxDistance = 3.5, matcher) {
20
+ var _a;
21
+ matcher || (matcher = (e) => true);
22
+ const eyePos = this.bot.entity.position.offset(0, this.bot.entity.height, 0);
23
+ Object.entries(aabbMap).forEach(([id, bb]) => {
24
+ if (bb.distanceToVec(eyePos) > maxDistance)
25
+ delete aabbMap[id];
26
+ });
27
+ dir = dir.normalize();
28
+ let returnVal = this.bot.world.raycast(startPos, dir, maxDistance);
29
+ if (returnVal && Object.keys(aabbMap).length === 0)
30
+ return returnVal;
31
+ maxDistance = (_a = returnVal === null || returnVal === void 0 ? void 0 : returnVal.intersect.distanceTo(startPos)) !== null && _a !== void 0 ? _a : maxDistance;
32
+ const iterator = new iterators_1.RaycastIterator(startPos, dir, maxDistance);
33
+ for (const id in aabbMap) {
34
+ const aabb = aabbMap[id];
35
+ const pt = aabb.minPoint();
36
+ const entity = this.bot.entities[id];
37
+ const intersect = iterator.intersect(aabb.toShapeFromMin(), pt);
38
+ if (intersect) {
39
+ const entityDir = pt.minus(eyePos);
40
+ const sign = Math.sign(entityDir.dot(dir));
41
+ if (sign !== -1) {
42
+ const dist = eyePos.distanceTo(intersect.pos);
43
+ if (dist <= maxDistance) {
44
+ maxDistance = dist;
45
+ if (matcher(entity)) {
46
+ returnVal = entity;
47
+ returnVal.intersect = intersect.pos;
48
+ returnVal.face = intersect.face;
49
+ }
50
+ }
51
+ }
52
+ }
53
+ }
54
+ return returnVal;
55
+ }
56
+ entityAtEntityCursor(entity, maxDistance = 3.5) {
57
+ var _a;
58
+ const block = this.bot.blockAtCursor(maxDistance);
59
+ maxDistance = (_a = block === null || block === void 0 ? void 0 : block.intersect.distanceTo(this.bot.entity.position)) !== null && _a !== void 0 ? _a : maxDistance;
60
+ const entities = Object.values(this.bot.entities).filter((e) => e.type !== "object" && e.username !== entity.username && e.position.distanceTo(entity.position) <= maxDistance);
61
+ const dir = new vec3_1.Vec3(-Math.sin(entity.yaw) * Math.cos(entity.pitch), Math.sin(entity.pitch), -Math.cos(entity.yaw) * Math.cos(entity.pitch));
62
+ const iterator = new iterators_1.RaycastIterator(entity.position.offset(0, entity.height, 0), dir.normalize(), maxDistance);
63
+ let targetEntity = null;
64
+ let targetDist = maxDistance;
65
+ for (let i = 0; i < entities.length; i++) {
66
+ const e = entities[i];
67
+ const w = (e.height >= 1.62 && e.height <= 1.99) || e.height === 2.9 ? 0.3 : e.height / 2;
68
+ const shapes = [[-w, 0, -w, w, e.height + (e.height === 1.62 ? 0.18 : 0), w]];
69
+ const intersect = iterator.intersect(shapes, e.position);
70
+ if (intersect) {
71
+ const entityDir = e.position.minus(entity.position); // Can be combined into 1 line
72
+ const sign = Math.sign(entityDir.dot(dir));
73
+ if (sign !== -1) {
74
+ const dist = entity.position.distanceTo(intersect.pos);
75
+ if (dist < targetDist) {
76
+ targetEntity = e;
77
+ targetDist = dist;
78
+ }
79
+ }
80
+ }
81
+ }
82
+ return targetEntity;
83
+ }
84
+ }
85
+ exports.RayTraceFunctions = RayTraceFunctions;
@@ -1,24 +1,24 @@
1
- import type { Block } from "prismarine-block";
2
- import type { Vec3 } from "vec3";
3
- import { AABB } from "../calcs/aabb";
4
- export declare namespace AABBUtils {
5
- function getBlockAABB(block: Block, height?: number): AABB;
6
- function getBlockPosAABB(block: Vec3, height?: number): AABB;
7
- function getEntityAABB(entity: {
8
- type: string;
9
- position: Vec3;
10
- height: number;
11
- width?: number;
12
- }): AABB;
13
- function getPlayerAABB(entity: {
14
- position: Vec3;
15
- height?: number;
16
- width?: number;
17
- }): AABB;
18
- function getPlayerAABBRaw(position: Vec3, height?: number): AABB;
19
- function getEntityAABBRaw(entity: {
20
- position: Vec3;
21
- height: number;
22
- width?: number;
23
- }): AABB;
24
- }
1
+ import type { Block } from "prismarine-block";
2
+ import type { Vec3 } from "vec3";
3
+ import { AABB } from "../calcs/aabb";
4
+ export declare namespace AABBUtils {
5
+ function getBlockAABB(block: Block, height?: number): AABB;
6
+ function getBlockPosAABB(block: Vec3, height?: number): AABB;
7
+ function getEntityAABB(entity: {
8
+ type: string;
9
+ position: Vec3;
10
+ height: number;
11
+ width?: number;
12
+ }): AABB;
13
+ function getPlayerAABB(entity: {
14
+ position: Vec3;
15
+ height?: number;
16
+ width?: number;
17
+ }): AABB;
18
+ function getPlayerAABBRaw(position: Vec3, height?: number): AABB;
19
+ function getEntityAABBRaw(entity: {
20
+ position: Vec3;
21
+ height: number;
22
+ width?: number;
23
+ }): AABB;
24
+ }