File size: 7,375 Bytes
a08f3c3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | 'use strict';
/**
* Mining System — نظام التعدين
* يروح يحفر، يجمع معادن، يكتشف كهوف
*/
const { EventEmitter } = require('events');
const sleep = ms => new Promise(r => setTimeout(r, Math.max(0, ms)));
const rand = (a, b) => Math.floor(Math.random() * (b - a + 1)) + a;
const chance = pct => Math.random() * 100 < pct;
const pick = arr => arr[Math.floor(Math.random() * arr.length)];
const ORE_PRIORITY = [
'diamond_ore','deepslate_diamond_ore',
'emerald_ore','deepslate_emerald_ore',
'gold_ore','deepslate_gold_ore',
'iron_ore','deepslate_iron_ore',
'redstone_ore','deepslate_redstone_ore',
'lapis_ore','deepslate_lapis_ore',
'coal_ore','deepslate_coal_ore',
'copper_ore','deepslate_copper_ore',
];
const VALUABLE_ITEMS = new Set([
'diamond','emerald','gold_ingot','iron_ingot','netherite_scrap',
'netherite_ingot','redstone','lapis_lazuli','coal','copper_ingot',
'raw_iron','raw_gold','raw_copper','flint',
]);
class MiningSystem extends EventEmitter {
constructor(bot, config = {}) {
super();
this.bot = bot;
this.config = {
scanRadius: 24,
mineDepth: 40,
torchInterval: 12,
...config,
};
this._active = false;
this._timers = [];
this._mining = false;
this._blocksMined = 0;
this._oresFound = {};
this._totalValue = 0;
this._log('INIT', 'نظام التعدين جاهز');
}
_log(tag, msg) {
const ts = new Date().toISOString().replace('T', ' ').slice(0, 19);
console.log(`[${ts}] [MINING] ${tag} ${msg}`);
}
start() {
if (this._active) return;
this._active = true;
this._log('START', 'بدء نظام التعدين');
// يدور على معادن كل 5 ثواني
this._addTimer(() => this._scanForOres(), 5000);
// يحط مشاعل تلقائياً
this._addTimer(() => this._placeTorches(), 8000);
}
stop() {
this._active = false;
this._mining = false;
this._timers.forEach(t => { try { clearInterval(t); clearTimeout(t); } catch(_) {} });
this._timers = [];
this._log('STOP', 'إيقاف نظام التعدين');
}
_addTimer(fn, interval) {
const t = setInterval(() => { if (this._active) fn(); }, interval);
this._timers.push(t);
}
// ═══ كشف المعادن القريبة ═══
_scanForOres() {
if (!this.bot?.entity || this._mining) return;
const pos = this.bot.entity.position;
const radius = this.config.scanRadius;
for (let x = -radius; x <= radius; x += 2) {
for (let y = -radius; y <= radius; y++) {
for (let z = -radius; z <= radius; z += 2) {
const block = this.bot.blockAt(pos.offset(x, y, z));
if (!block) continue;
if (ORE_PRIORITY.includes(block.name)) {
this._mineBlock(block);
return;
}
}
}
}
}
// ═══ حفر كتلة ═══
async _mineBlock(block) {
if (this._mining || !this.bot?.entity) return;
this._mining = true;
try {
// اختار أفضل مجرفة/فأس
await this._equipBestTool(block.name);
// امشي للكتلة
await this._walkTo(block.position);
// حفر
try {
await this.bot.dig(block);
this._blocksMined++;
// سجّل المعدن
if (ORE_PRIORITY.includes(block.name)) {
const ore = block.name.replace('deepslate_', '').replace('_ore', '');
this._oresFound[ore] = (this._oresFound[ore] || 0) + 1;
this._totalValue += this._getOreValue(ore);
this._log('⛏️', `حفر ${block.name} (إجمالي: ${this._blocksMined})`);
this.emit('ore', ore, block.name);
}
} catch(_) {}
await sleep(rand(100, 300));
} catch(e) {
this._log('❌', `خطأ: ${e.message}`);
} finally {
this._mining = false;
}
}
// ═══ اختيار أفضل أداة ═══
async _equipBestTool(blockName) {
if (!this.bot) return;
const isOre = blockName.includes('ore');
const isStone = ['stone','deepslate','granite','diorite','andesite'].some(s => blockName.includes(s));
let toolType = 'pickaxe';
if (blockName.includes('log') || blockName.includes('wood')) toolType = 'axe';
else if (blockName.includes('dirt') || blockName.includes('sand') || blockName.includes('gravel')) toolType = 'shovel';
const items = this.bot.inventory.items();
const tools = items.filter(i => i.name.includes(toolType));
if (tools.length > 0) {
// الأعلى في الترتيب
const best = tools.sort((a, b) => this._toolRank(b.name) - this._toolRank(a.name))[0];
try { await this.bot.equip(best, 'hand'); } catch(_) {}
}
}
_toolRank(name) {
if (name.includes('netherite')) return 6;
if (name.includes('diamond')) return 5;
if (name.includes('iron')) return 4;
if (name.includes('stone')) return 3;
if (name.includes('golden')) return 2;
if (name.includes('wooden')) return 1;
return 0;
}
// ═══ المشي ═══
async _walkTo(position) {
if (!this.bot?.entity) return;
const yaw = Math.atan2(
position.z - this.bot.entity.position.z,
position.x - this.bot.entity.position.x
);
this.bot.look(yaw, 0, false);
this.bot.setControlState('forward', true);
await sleep(rand(200, 600));
this.bot.setControlState('forward', false);
}
// ═══ وضع مشاعل ═══
async _placeTorches() {
if (!this.bot?.entity) return;
const torch = this.bot.inventory.items().find(i => i.name === 'torch' || i.name === 'soul_torch');
if (!torch) return;
// لو المشاعل قليلة، حط واحدة كل فتره
const torchCount = this.bot.inventory.items().filter(i => i.name === 'torch').length;
if (torchCount > 3 && chance(50)) return;
try {
await this.bot.equip(torch, 'hand');
const block = this.bot.blockAt(this.bot.entity.position.offset(0, -1, 0));
if (block && block.name !== 'air') {
await this.bot.placeBlock(block, { x: 0, y: 1, z: 0 });
}
} catch(_) {}
}
// ═══ قيمة المعدن ═══
_getOreValue(ore) {
const values = { diamond: 10, emerald: 8, gold: 5, iron: 3, redstone: 2, lapis: 2, coal: 1, copper: 1 };
return values[ore] || 1;
}
// ═══ النزول للأعماق ═══
async digDown() {
if (!this.bot?.entity) return;
this._log('🔽', 'النزول للأعماق');
for (let i = 0; i < this.config.mineDepth; i++) {
if (!this.bot?.entity || !this._active) break;
const block = this.bot.blockAt(this.bot.entity.position.offset(0, -1, 0));
if (block && block.name !== 'air' && block.name !== 'water' && block.name !== 'lava') {
try {
await this._equipBestTool(block.name);
await this.bot.dig(block);
this._blocksMined++;
} catch(_) {}
}
await sleep(rand(150, 400));
}
}
getStats() {
return {
blocksMined: this._blocksMined,
oresFound: { ...this._oresFound },
totalValue: this._totalValue,
mining: this._mining,
active: this._active,
};
}
destroy() {
this.stop();
this.removeAllListeners();
}
}
module.exports = { MiningSystem, ORE_PRIORITY, VALUABLE_ITEMS };
|