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 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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);
})();