Download combat.js from Saedmh/Saed: direct link, hf CLI and curl.
- Browser
- Download file 8.3 kB
-
https://huggingface.co/spaces/Saedmh/Saed/resolve/main/combat.js
- Command line
-
hf download hf://spaces/Saedmh/Saed/combat.js
-
curl -L -o combat.js https://huggingface.co/spaces/Saedmh/Saed/resolve/main/combat.js
8.3 kB
| ; | |
| /** | |
| * Combat 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 dist = (a, b) => a.distanceTo(b); | |
| const HOSTILE_MOBS = new Set([ | |
| 'zombie','skeleton','spider','creeper','enderman','witch', | |
| 'slime','phantom','drowned','husk','stray','cave_spider', | |
| 'blaze','ghast','magma_cube','witherskeleton','piglin_brute', | |
| 'vindicator','evoker','pillager','ravager','vex','warden', | |
| 'breeze','bogged' | |
| ]); | |
| const WEAPON_RANK = { | |
| 'netherite_sword': 9, 'netherite_axe': 8, | |
| 'diamond_sword': 8, 'diamond_axe': 7, | |
| 'iron_sword': 6, 'iron_axe': 5, | |
| 'stone_sword': 4, 'stone_axe': 3, | |
| 'wooden_sword': 2, 'wooden_axe': 1, | |
| 'bow': 7, 'crossbow': 6, 'trident': 8, | |
| }; | |
| class CombatSystem extends EventEmitter { | |
| constructor(bot, config = {}) { | |
| super(); | |
| this.bot = bot; | |
| this.config = { | |
| aggroRange: 16, | |
| fleeHealthPct: 0.25, | |
| attackCooldown: 350, | |
| shieldSlot: 'off-hand', | |
| ...config, | |
| }; | |
| this._active = false; | |
| this._timers = []; | |
| this._fighting = false; | |
| this._lastAttack = 0; | |
| this._kills = 0; | |
| this._damageTaken = 0; | |
| this._log('INIT', 'Combat system جاهز'); | |
| } | |
| _log(tag, msg) { | |
| const ts = new Date().toISOString().replace('T', ' ').slice(0, 19); | |
| console.log(`[${ts}] [COMBAT] ${tag} ${msg}`); | |
| } | |
| start() { | |
| if (this._active) return; | |
| this._active = true; | |
| this._log('START', 'بدء نظام القتال'); | |
| // كشف الوحوش القريبة كل 2 ثانية | |
| this._addTimer(() => this._scanForHostiles(), 2000); | |
| // مراقبة الصحة كل ثانية | |
| this._addTimer(() => this._checkHealth(), 1000); | |
| // سلوك ليلي أكثر عدائية | |
| this._addTimer(() => this._nightBehavior(), 5000); | |
| } | |
| stop() { | |
| this._active = false; | |
| this._timers.forEach(t => { try { clearInterval(t); clearTimeout(t); } catch(_) {} }); | |
| this._timers = []; | |
| this._fighting = false; | |
| this._log('STOP', 'إيقاف نظام القتال'); | |
| } | |
| _addTimer(fn, interval) { | |
| const t = setInterval(() => { if (this._active) fn(); }, interval); | |
| this._timers.push(t); | |
| return t; | |
| } | |
| // ═══ كشف الوحوش القريبة ═══ | |
| _scanForHostiles() { | |
| if (!this.bot?.entity || this._fighting) return; | |
| const pos = this.bot.entity.position; | |
| const hostiles = this.bot.entitiesArray.filter(e => { | |
| if (!e || e.type !== 'mob' || !e.name) return false; | |
| if (!HOSTILE_MOBS.has(e.name)) return false; | |
| if (!e.position) return false; | |
| return dist(pos, e.position) < this.config.aggroRange; | |
| }); | |
| if (hostiles.length > 0) { | |
| const closest = hostiles.sort((a, b) => dist(pos, a.position) - dist(pos, b.position))[0]; | |
| this._engage(closest); | |
| } | |
| } | |
| // ═══ مهاجمة مخلوق معين ═══ | |
| async _engage(target) { | |
| if (this._fighting || !this.bot?.entity) return; | |
| this._fighting = true; | |
| this._log('⚔️', `هجوم على ${target.name}`); | |
| try { | |
| // اختر أفضل سلاح متاح | |
| await this._equipBestWeapon(); | |
| // حط الدرع لو موجود | |
| await this._equipShield(); | |
| // هاجم لحد ما يموت أو تقل صحتنا | |
| while (this._active && this.bot?.entity && target && target.health > 0) { | |
| if (!this.bot?.entity) break; | |
| // لو صحتنا قليلة، اهرب | |
| const health = this.bot.health; | |
| if (health && health < 6) { | |
| this._log('🏃', 'صحة قليلة — هروب!'); | |
| await this._flee(); | |
| break; | |
| } | |
| // ا approached للهدف | |
| const d = dist(this.bot.entity.position, target.position); | |
| if (d > 3.5) { | |
| await this._walkTo(target.position); | |
| } | |
| // اضرب | |
| if (Date.now() - this._lastAttack > this.config.attackCooldown) { | |
| try { | |
| await this.bot.attack(target); | |
| this._lastAttack = Date.now(); | |
| } catch(_) {} | |
| } | |
| // حركات جانبية عشان يتجنب الهجمات | |
| if (chance(30)) { | |
| this._strafe(); | |
| } | |
| await sleep(rand(100, 300)); | |
| } | |
| if (target.health <= 0 || !target.entity) { | |
| this._kills++; | |
| this._log('💀', `قتل ${target.name} (إجمالي: ${this._kills})`); | |
| this.emit('kill', target.name); | |
| } | |
| } catch(e) { | |
| this._log('❌', `خطأ في القتال: ${e.message}`); | |
| } finally { | |
| this._fighting = false; | |
| } | |
| } | |
| // ═══ اختيار أفضل سلاح ═══ | |
| async _equipBestWeapon() { | |
| if (!this.bot) return; | |
| const items = this.bot.inventory.items(); | |
| let bestWeapon = null; | |
| let bestRank = -1; | |
| for (const item of items) { | |
| const rank = WEAPON_RANK[item.name] || 0; | |
| if (rank > bestRank) { | |
| bestRank = rank; | |
| bestWeapon = item; | |
| } | |
| } | |
| if (bestWeapon) { | |
| try { | |
| await this.bot.equip(bestWeapon, 'hand'); | |
| } catch(_) {} | |
| } | |
| } | |
| // ═══ وضع الدرع ═══ | |
| async _equipShield() { | |
| if (!this.bot) return; | |
| const shield = this.bot.inventory.items().find(i => i.name === 'shield'); | |
| if (shield) { | |
| try { | |
| await this.bot.equip(shield, 'off-hand'); | |
| } catch(_) {} | |
| } | |
| } | |
| // ═══ حركة هروب ═══ | |
| async _flee() { | |
| if (!this.bot?.entity) return; | |
| const yaw = Math.random() * Math.PI * 2; | |
| this.bot.look(yaw, 0, false); | |
| this.bot.setControlState('forward', true); | |
| this.bot.setControlState('sprint', true); | |
| await sleep(rand(1500, 3000)); | |
| this.bot.setControlState('forward', false); | |
| this.bot.setControlState('sprint', false); | |
| } | |
| // ═══ حركة جانبية ═══ | |
| _strafe() { | |
| if (!this.bot?.entity) return; | |
| const dir = chance(50) ? 'left' : 'right'; | |
| this.bot.setControlState(dir, true); | |
| setTimeout(() => { try { this.bot.setControlState(dir, false); } catch(_) {} }, rand(200, 500)); | |
| } | |
| // ═══ المشي لهدف ═══ | |
| 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, 500)); | |
| this.bot.setControlState('forward', false); | |
| } | |
| // ═══ مراقبة الصحة ═══ | |
| _checkHealth() { | |
| if (!this.bot?.entity) return; | |
| const health = this.bot.health; | |
| if (health !== undefined && health < 4) { | |
| // أكل طعام لو متاح | |
| this._eatIfAvailable(); | |
| } | |
| } | |
| // ═══ أكل لو متاح ═══ | |
| async _eatIfAvailable() { | |
| if (!this.bot) return; | |
| const food = this.bot.inventory.items().find(i => | |
| i.name.includes('bread') || i.name.includes('cooked') || | |
| i.name.includes('apple') || i.name.includes('steak') || | |
| i.name.includes('porkchop') || i.name.includes('chicken') | |
| ); | |
| if (food) { | |
| try { | |
| await this.bot.equip(food, 'hand'); | |
| await this.bot.consume(); | |
| this._log('🍖', 'أكل عشان يسترد الصحة'); | |
| } catch(_) {} | |
| } | |
| } | |
| // ═══ سلوك ليلي ═══ | |
| _nightBehavior() { | |
| if (!this.bot?.entity) return; | |
| const time = this.bot.time?.age; | |
| if (time === undefined) return; | |
| // الليل: أكثر عدائية، يدور على الوحوش | |
| const isNight = (time % 24000) > 12500 && (time % 24000) < 23000; | |
| if (isNight) { | |
| this.config.aggroRange = 24; // يشوف أبعد بالليل | |
| } else { | |
| this.config.aggroRange = 16; | |
| } | |
| } | |
| getStats() { | |
| return { | |
| kills: this._kills, | |
| damageTaken: this._damageTaken, | |
| fighting: this._fighting, | |
| active: this._active, | |
| }; | |
| } | |
| destroy() { | |
| this.stop(); | |
| this.removeAllListeners(); | |
| } | |
| } | |
| module.exports = { CombatSystem, HOSTILE_MOBS }; | |