Haxball Ball Trajectory Predictor (Beta)

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

2025-04-14 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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);
})();