Haxball Ball Trajectory Predictor (Beta)

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

目前為 2025-04-14 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);
})();