Spaces:
Configuration error

File size: 6,317 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
'use strict';
/**
 * Exploration System — نظام الاستكشاف
 * يدور على أماكن جديدة، يكتشف أ biome جديدة، يحفظ مواقع مهمة
 */
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 BIOMES_OF_INTEREST = new Set([
  'jungle','bamboo_jungle','mushroom_fields','flower_forest',
  'dark_forest','birch_forest','mesa','savanna','ice_spikes',
  'deep_dark','cherry_grove','mangrove_swamp',
]);

class ExplorationSystem extends EventEmitter {
  constructor(bot, config = {}) {
    super();
    this.bot = bot;
    this.config = {
      exploreRadius: 64,
      waypointInterval: 30000,
      ...config,
    };
    this._active = false;
    this._timers = [];
    this._exploring = false;
    this._waypoints = [];
    this._distanceTraveled = 0;
    this._placesFound = [];
    this._log('INIT', 'نظام الاستكشاف جاهز');
  }

  _log(tag, msg) {
    const ts = new Date().toISOString().replace('T', ' ').slice(0, 19);
    console.log(`[${ts}] [EXPLORE] ${tag}  ${msg}`);
  }

  start() {
    if (this._active) return;
    this._active = true;
    this._log('START', 'بدء نظام الاستكشاف');

    this._addTimer(() => this._autoExplore(), 15000);
    this._addTimer(() => this._recordWaypoint(), this.config.waypointInterval);
  }

  stop() {
    this._active = false;
    this._exploring = 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 _autoExplore() {
    if (this._exploring || !this.bot?.entity) return;
    if (chance(60)) return;

    this._exploring = true;
    try {
      const yaw = Math.random() * Math.PI * 2;
      const distance = rand(20, 50);

      this._log('🗺️', `استكشاف: ${distance} بلوك باتجاه ${Math.round(yaw * 180 / Math.PI)}°`);
      this.bot.look(yaw, 0, false);
      this.bot.setControlState('forward', true);
      if (chance(40)) this.bot.setControlState('sprint', true);

      await sleep(distance * 300);

      this.bot.setControlState('forward', false);
      this.bot.setControlState('sprint', false);
      this._distanceTraveled += distance;

      // فحص إذا لقينا حاجة مهمة
      this._checkInteresting();
    } catch(_) {}
    finally {
      this._exploring = false;
    }
  }

  // ═══ فحص أماكن مهمة ═══
  _checkInteresting() {
    if (!this.bot?.entity) return;
    const pos = this.bot.entity.position;

    // فحص كهف
    const caveBlock = this._findCave();
    if (caveBlock) {
      this._placesFound.push({ type: 'cave', pos: { ...pos }, time: Date.now() });
      this._log('🕳️', 'لقى كهف!');
      this.emit('found', 'cave', pos);
    }

    // فحص بيت لاعب
    const house = this._findPlayerHouse();
    if (house) {
      this._placesFound.push({ type: 'house', pos: { ...pos }, time: Date.now() });
      this._log('🏠', 'لقى بيت لاعب!');
      this.emit('found', 'house', pos);
    }

    // فحص شجرة كبيرة
    const bigTree = this._findBigTree();
    if (bigTree) {
      this._log('🌳', 'لقى شجرة كبيرة');
    }
  }

  _findCave() {
    if (!this.bot?.entity) return null;
    const pos = this.bot.entity.position;
    for (let x = -8; x <= 8; x++) {
      for (let z = -8; z <= 8; z++) {
        for (let y = -4; y <= 2; y++) {
          const block = this.bot.blockAt(pos.offset(x, y, z));
          if (block && block.name === 'air') {
            const below = this.bot.blockAt(pos.offset(x, y - 1, z));
            if (below && (below.name === 'stone' || below.name === 'deepslate')) {
              return block;
            }
          }
        }
      }
    }
    return null;
  }

  _findPlayerHouse() {
    if (!this.bot?.entity) return null;
    const pos = this.bot.entity.position;
    for (let x = -16; x <= 16; x += 2) {
      for (let z = -16; z <= 16; z += 2) {
        const block = this.bot.blockAt(pos.offset(x, 0, z));
        if (block && (block.name.includes('plank') || block.name.includes('brick'))) {
          return block;
        }
      }
    }
    return null;
  }

  _findBigTree() {
    if (!this.bot?.entity) return null;
    const pos = this.bot.entity.position;
    for (let x = -8; x <= 8; x++) {
      for (let z = -8; z <= 8; z++) {
        const block = this.bot.blockAt(pos.offset(x, 4, z));
        if (block && block.name.includes('leaves')) {
          return block;
        }
      }
    }
    return null;
  }

  // ═══ حفظ نقطة مرجعية ═══
  _recordWaypoint() {
    if (!this.bot?.entity) return;
    const pos = this.bot.entity.position;
    this._waypoints.push({
      x: Math.round(pos.x),
      y: Math.round(pos.y),
      z: Math.round(pos.z),
      time: Date.now(),
    });
    if (this._waypoints.length > 50) this._waypoints.shift();
  }

  // ═══ الرجوع للنقطة الأخيرة ═══
  async returnToLastWaypoint() {
    if (!this.bot?.entity || this._waypoints.length === 0) return;
    const wp = this._waypoints[this._waypoints.length - 1];
    await this._walkTo({ x: wp.x, y: wp.y, z: wp.z });
  }

  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(500, 1500));
    this.bot.setControlState('forward', false);
  }

  getStats() {
    return {
      distanceTraveled: this._distanceTraveled,
      placesFound: this._placesFound.length,
      waypoints: this._waypoints.length,
      exploring: this._exploring,
      active: this._active,
    };
  }

  destroy() {
    this.stop();
    this.removeAllListeners();
  }
}

module.exports = { ExplorationSystem, BIOMES_OF_INTEREST };