File size: 7,161 Bytes
5871ebc | 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 236 237 238 239 240 241 | 'use strict';
/**
* Building 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 BUILD_MATERIALS = [
'oak_planks','spruce_planks','birch_planks','jungle_planks',
'acacia_planks','dark_oak_planks','cobblestone','stone_bricks',
'smooth_stone','bricks','glass','oak_fence',
];
class BuildingSystem extends EventEmitter {
constructor(bot, config = {}) {
super();
this.bot = bot;
this.config = {
buildRadius: 4,
wallHeight: 3,
...config,
};
this._active = false;
this._timers = [];
this._building = false;
this._blocksPlaced = 0;
this._structures = 0;
this._log('INIT', 'نظام البناء جاهز');
}
_log(tag, msg) {
const ts = new Date().toISOString().replace('T', ' ').slice(0, 19);
console.log(`[${ts}] [BUILDING] ${tag} ${msg}`);
}
start() {
if (this._active) return;
this._active = true;
this._log('START', 'بدء نظام البناء');
// يرتب المخزون كل 30 ثانية
this._addTimer(() => this._organizeInventory(), 30000);
}
stop() {
this._active = false;
this._building = 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);
}
// ═══ بناء ملجأ بسيط ═══
async buildShelter() {
if (this._building || !this.bot?.entity) return;
this._building = true;
this._log('🏠', 'بناء ملجأ');
try {
const plank = this.bot.inventory.items().find(i => i.name.includes('plank'));
if (!plank) {
this._log('⚠️', 'لا خشب متاح للبناء');
return;
}
await this.bot.equip(plank, 'hand');
const pos = this.bot.entity.position;
const base = { x: Math.round(pos.x), y: Math.round(pos.y), z: Math.round(pos.z) };
// بناء أرضية 5x5
await this._buildFloor(base);
// بناء جدران
await this._buildWalls(base);
// بناء سقف
await this._buildRoof(base);
// وضع مشاعل
await this._placeTorches(base);
this._structures++;
this._log('✅', `بنى ملجأ (إجمالي: ${this._structures})`);
this.emit('built', 'shelter');
} catch(e) {
this._log('❌', `خطأ: ${e.message}`);
} finally {
this._building = false;
}
}
// ═══ بناء أرضية ═══
async _buildFloor(base) {
if (!this.bot) return;
for (let x = -2; x <= 2; x++) {
for (let z = -2; z <= 2; z++) {
const pos = { x: base.x + x, y: base.y - 1, z: base.z + z };
await this._placeBlock(pos);
}
}
}
// ═══ بناء جدران ═══
async _buildWalls(base) {
if (!this.bot) return;
const h = this.config.wallHeight;
for (let y = 0; y <= h; y++) {
// الجدران الأربعة
for (let x = -2; x <= 2; x++) {
await this._placeBlock({ x: base.x + x, y: base.y + y, z: base.z - 2 });
await this._placeBlock({ x: base.x + x, y: base.y + y, z: base.z + 2 });
}
for (let z = -1; z <= 1; z++) {
await this._placeBlock({ x: base.x - 2, y: base.y + y, z: base.z + z });
await this._placeBlock({ x: base.x + 2, y: base.y + y, z: base.z + z });
}
}
}
// ═══ بناء سقف ═══
async _buildRoof(base) {
if (!this.bot) return;
for (let x = -2; x <= 2; x++) {
for (let z = -2; z <= 2; z++) {
await this._placeBlock({ x: base.x + x, y: base.y + 4, z: base.z + z });
}
}
}
// ═══ وضع كتلة ═══
async _placeBlock(pos) {
if (!this.bot) return;
try {
const block = this.bot.blockAt(pos);
if (block?.name !== 'air') return;
// دور على كتلة تحتها نقدر نحط عليها
const below = this.bot.blockAt({ x: pos.x, y: pos.y - 1, z: pos.z });
if (!below || below.name === 'air') return;
await this.bot.placeBlock(below, { x: 0, y: 1, z: 0 });
this._blocksPlaced++;
await sleep(rand(50, 150));
} catch(_) {}
}
// ═══ وضع مشاعل ═══
async _placeTorches(base) {
if (!this.bot) return;
const torch = this.bot.inventory.items().find(i => i.name === 'torch');
if (!torch) return;
try {
await this.bot.equip(torch, 'hand');
const spots = [
{ x: base.x - 2, y: base.y + 1, z: base.z - 2 },
{ x: base.x + 2, y: base.y + 1, z: base.z + 2 },
];
for (const spot of spots) {
const block = this.bot.blockAt({ x: spot.x, y: spot.y - 1, z: spot.z });
if (block && block.name !== 'air') {
await this.bot.placeBlock(block, { x: 0, y: 1, z: 0 });
await sleep(200);
}
}
} catch(_) {}
}
// ═══ ترتيب المخزون ═══
async _organizeInventory() {
if (!this.bot) return;
const chest = this.bot.findBlock({
matching: this.bot.registry?.blocksByName?.chest?.id,
maxDistance: 16, count: 1,
});
if (!chest) return;
try {
await this.bot.openChest(chest);
await sleep(500);
// ضع الأغراض الزائدة في الصندوق
const items = this.bot.inventory.items();
const keep = ['sword','axe','pickaxe','shovel','bow','shield','torch','bread','water_bucket'];
for (const item of items) {
if (keep.some(k => item.name.includes(k))) continue;
if (chance(40)) {
try {
await this.bot.clickWindow(item.slot, 0, 0);
await sleep(100);
} catch(_) {}
}
}
this.bot.closeWindow();
this._log('📦', 'رتب المخزون');
} catch(_) {}
}
// ═══ بناء سلم ═══
async buildStairs(height = 5) {
if (!this.bot?.entity) return;
const plank = this.bot.inventory.items().find(i => i.name.includes('plank'));
if (!plank) return;
await this.bot.equip(plank, 'hand');
const pos = this.bot.entity.position;
const base = { x: Math.round(pos.x), y: Math.round(pos.y), z: Math.round(pos.z) };
for (let i = 0; i < height; i++) {
await this._placeBlock({ x: base.x + i, y: base.y + i, z: base.z });
await this._placeBlock({ x: base.x + i, y: base.y + i + 1, z: base.z });
}
this._log('🪜', `بنى سلم بارتفاع ${height}`);
}
getStats() {
return {
blocksPlaced: this._blocksPlaced,
structures: this._structures,
building: this._building,
active: this._active,
};
}
destroy() {
this.stop();
this.removeAllListeners();
}
}
module.exports = { BuildingSystem, BUILD_MATERIALS };
|