Haxball Ball Trajectory Predictor (Beta)

Tenta prever onde a bola vai bater na parede em salas públicas de Haxball.

2025-04-14 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

You will need to install an extension such as Tampermonkey to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         Haxball Ball Trajectory Predictor (Beta)
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Tenta prever onde a bola vai bater na parede em salas públicas de Haxball.
// @author       Você
// @match        https://www.haxball.com/play*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    const canvas = document.querySelector("canvas");

    if (!canvas) return;

    let lastPos = null;
    let lastTime = performance.now();

    function drawPrediction(ctx, ballPos, ballSpeed) {
        let predictedX = ballPos.x;
        let predictedY = ballPos.y;
        let vx = ballSpeed.x;
        let vy = ballSpeed.y;
        let steps = 300;

        ctx.strokeStyle = "yellow";
        ctx.beginPath();
        ctx.moveTo(predictedX, predictedY);

        for (let i = 0; i < steps; i++) {
            predictedX += vx;
            predictedY += vy;

            if (predictedX <= 0 || predictedX >= canvas.width) vx *= -1;
            if (predictedY <= 0 || predictedY >= canvas.height) vy *= -1;

            ctx.lineTo(predictedX, predictedY);
        }
        ctx.stroke();
    }

    function trackBall() {
        const ctx = canvas.getContext("2d");
        const imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
        const data = imgData.data;

        let found = false;
        let centerX = 0, centerY = 0, count = 0;

        for (let y = 0; y < canvas.height; y += 2) {
            for (let x = 0; x < canvas.width; x += 2) {
                const idx = (y * canvas.width + x) * 4;
                const r = data[idx], g = data[idx + 1], b = data[idx + 2];

                // Detecta a bola (geralmente branca com tom amarelado)
                if (r > 200 && g > 200 && b < 150) {
                    centerX += x;
                    centerY += y;
                    count++;
                }
            }
        }

        if (count > 0) {
            centerX = centerX / count;
            centerY = centerY / count;

            const now = performance.now();
            const dt = (now - lastTime) / 1000;

            if (lastPos) {
                const vx = (centerX - lastPos.x) / dt;
                const vy = (centerY - lastPos.y) / dt;

                // Redesenha a previsão
                drawPrediction(ctx, { x: centerX, y: centerY }, { x: vx, y: vy });
            }

            lastPos = { x: centerX, y: centerY };
            lastTime = now;
        }

        requestAnimationFrame(trackBall);
    }

    setTimeout(() => {
        console.log("[Previsão de Trajetória] Iniciando...");
        trackBall();
    }, 3000);
})();