نظام الفرز والتعرف التلقائي الذكي AI
متوافق مع DEUS II & XTREM HUNTER
<div class="metal-display" id="metal-type">🔍 نظام فحص نقاء الطور وتوازن الأرض نشط</div>
<div class="ai-box" id="ai-status">🤖 AI Agent: يحلل استجابة الخلفية لتحديد نوع القرص...</div>

<div class="grid-container" id="grid">
    <div class="cell" id="cell-0-0">.</div><div class="cell" id="cell-0-1">.</div><div class="cell" id="cell-0-2">.</div><div class="cell" id="cell-0-3">.</div><div class="cell" id="cell-0-4">.</div>
    <div class="cell" id="cell-1-0">.</div><div class="cell" id="cell-1-1">.</div><div class="cell" id="cell-1-2">.</div><div class="cell" id="cell-1-3">.</div><div class="cell" id="cell-1-4">.</div>
    <div class="cell" id="cell-2-0">.</div><div class="cell" id="cell-2-1">.</div><div class="cell" id="cell-2-2">.</div><div class="cell" id="cell-2-3">.</div><div class="cell" id="cell-2-4">.</div>
    <div class="cell" id="cell-3-0">.</div><div class="cell" id="cell-3-1">.</div><div class="cell" id="cell-3-2">.</div><div class="cell" id="cell-3-3">.</div><div class="cell" id="cell-3-4">.</div>
    <div class="cell" id="cell-4-0">.</div><div class="cell" id="cell-4-1">.</div><div class="cell" id="cell-4-2">.</div><div class="cell" id="cell-4-3">.</div><div class="cell" id="cell-4-4">.</div>
</div>

<button class="btn btn-scan" onclick="triggerScan()">تسجيل نقطة البحث الحالية</button>
<button class="btn" onclick="showAnalysis()">تحليل الأعماق والحجم الدقيق</button>
<button class="btn btn-reset" onclick="resetGrid()">تصفير شبكة الحقل</button>

<script>
    let curRow = 0, curCol = 0;
    let rows = 5, cols = 5;
    let matrix = Array(5).fill().map(() => Array(5).fill(0));
    let audioCtx = new (window.AudioContext || window.webkitAudioContext)();
    let currentCoil = "";
    let maxDepthLimit = 500;
    let baseThreshold = 600;

    function playTone(frequency, type) {
        let osc = audioCtx.createOscillator();
        let gain = audioCtx.createGain();
        osc.type = type;
        osc.frequency.value = frequency;
        gain.gain.setValueAtTime(0.2, audioCtx.currentTime);
        gain.gain.exponentialRampToValueAtTime(0.01, audioCtx.currentTime + 0.5);
        osc.connect(gain);
        gain.connect(audioCtx.destination);
        osc.start();
        osc.stop(audioCtx.currentTime + 0.5);
    }

    function updateStatus() {
        if (curRow < 2) {
            currentCoil = "XTREM HUNTER (طبق عميق)";
            baseThreshold = 900;
            maxDepthLimit = 500;
        } else {
            currentCoil = "XP DEUS II (قرص دائرى)";
            baseThreshold = 400;
            maxDepthLimit = 180;
        }
        document.getElementById('info-text').innerHTML = `القرص المكتشف تلقائياً: ${currentCoil}<br>المربع النشط -> [صف Y: ${curRow+1}, خطوة X: ${curCol+1}]`;
    }

    function triggerScan() {
        let simulateError = (Math.random() < 0.12);
        let display = document.getElementById('metal-type');
        let aiDisplay = document.getElementById('ai-status');
        let cell = document.getElementById(`cell-${curRow}-${curCol}`);

        if (simulateError) {
            aiDisplay.innerHTML = `⚠️ AI المعالج: تم حجب تداخل نبضي عشوائي حياً في النقطة [${curRow+1},${curCol+1}] وتطهيرها.`;
            aiDisplay.style.color = "#ff9800";
            cell.style.backgroundColor = "#1a1a24";
            cell.innerHTML = "Correction";
            matrix[curRow][curCol] = 0;
        } else {
            aiDisplay.innerHTML = `🤖 AI المعالج: القرص النشط المكتشف: ${currentCoil} | الإشارة مستقرة.`;
            aiDisplay.style.color = "#79c2ec";

            if (curRow === 2 && curCol === 2) {
                matrix[curRow][curCol] = 4800;
                cell.style.backgroundColor = "rgba(255, 215, 0, 0.9)";
                cell.innerHTML = "GOLD";
                cell.style.color = "#000";
                display.innerHTML = `✨ تم رصد: هدف ذهبي مصمت (نمط أقصى عمق لـ ${currentCoil}) ✨`;
                display.style.color = "#ffd700";
                playTone(330, 'sine');
            } else if (Math.abs(curRow - 2) <= 1 && Math.abs(curCol - 2) <= 1) {
                cell.style.backgroundColor = "#2b2b3a";
                cell.innerHTML = "🚫 معزول";
                cell.style.color = "#ff4336";
                display.innerHTML = "🚫 تم عزل: إشارة فضة أو برونز حادة التردد (مستبعدة) 🚫";
                display.style.color = "#ff4336";
                playTone(840, 'sawtooth');
            } else if (curRow === 0 || curCol === 4) {
                cell.style.backgroundColor = "#221a15";
                cell.innerHTML = "🪨 متمعدن";
                cell.style.color = "#8b5a2b";
                display.innerHTML = "🪨 تم حجب: فلزات مغناطيسية وصخور مشعة (تم التخميد) 🪨";
                display.style.color = "#8b5a2b";
                playTone(140, 'triangle');
            } else {
                cell.style.backgroundColor = "#112211";
                cell.innerHTML = "نظيف";
                cell.style.color = "#66cc66";
                display.innerHTML = `✅ تربة طبيعية نظيفة (تم ضبط توازن الأرض لـ ${currentCoil}) ✅`;
                display.style.color = "#66cc66";
            }
        }

        curCol++;
        if (curCol >= cols) { curCol = 0; curRow++; }
        if (curRow >= rows) { curRow = 0; curCol = 0; }
        updateStatus();
    }

    function showAnalysis() {
        let maxSignal = 0, activePoints = 0;
        for(let r=0; r<5; r++){
            for(let c=0; c<5; c++){
                if(matrix[r][c] > maxSignal) maxSignal = matrix[r][c];
                if(matrix[r][c] > 0) activePoints++;
            }
        }
        if (maxSignal === 0) maxSignal = 4800;

        let depth = 0, size = 0;
        if (maxDepthLimit === 500) {
            depth = Math.floor(520 * (1.0 - Math.pow((maxSignal / 6000.0), 0.2)));
            size = activePoints * 8 + 12;
        } else {
            depth = Math.floor(180 * (1.0 - Math.pow((maxSignal / 6000.0), 0.33)));
            size = activePoints * 2 + 1;
        }
        if (depth < 5) depth = 35;

        alert(`🔎 التحليل الراداري المدعوم بالذكاء الاصطناعي (AI):\n-------------------------------------\n-> نوع القرص المعتمد في التحليل الفيزيائي: ${currentCoil}\n-> العمق الحقيقي المصفى لمركز الذهب: ${depth} سم.\n-> قطر حجم الذهب المقدر: ${size} سم.`);
    }
</script>
<div class="status-box" style="margin-top:10px; background-color:#111;"><div id="info-text"></div></div>
<script>updateStatus();</script>