Haxball Ball Trajectory Predictor (Beta)

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

Stan na 14-04-2025. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

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