Greasy Fork

Advanced Paper.io Bot with Food Collection

Smart bot for Paper.io that collects food, avoids threats, and grows bigger by targeting high-value items when nearby.

// ==UserScript==
// @name         Advanced Paper.io Bot with Food Collection
// @namespace    http://tampermonkey.net/
// @version      3.0
// @description  Smart bot for Paper.io that collects food, avoids threats, and grows bigger by targeting high-value items when nearby.
// @author       hunter
// @match        *://paper-io.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Key bindings for controls
    const KEYS = {
        freeze: 'F', // Press 'F' to toggle freeze mode
        autoPlay: 'A' // Press 'A' to toggle auto-play mode
    };

    let freezeMode = false;
    let autoPlayMode = false;
    let intervalId;

    // Log status updates to the console
    function logStatus(message) {
        console.log(`[Paper.io Bot]: ${message}`);
    }

    // Simulate key presses to control the player
    function movePlayer(dir) {
        const event = new KeyboardEvent('keydown', { key: dir });
        document.dispatchEvent(event);
    }

    // Get all food items on the map
    function getFoodItems() {
        return Array.from(document.querySelectorAll('.food')); // Assumes food items have the class "food"
    }

    // Detect the closest or most valuable food
    function getClosestFood(playerBounds) {
        const foodItems = getFoodItems();
        let closestFood = null;
        let closestDistance = Infinity;

        for (let food of foodItems) {
            const foodBounds = food.getBoundingClientRect();

            // Calculate the distance between the player and the food
            const distance = Math.hypot(
                playerBounds.left - foodBounds.left,
                playerBounds.top - foodBounds.top
            );

            // Prioritize closer and more valuable food
            const value = food.dataset.value || 1; // Assume higher `data-value` means more valuable
            const weightedDistance = distance / value;

            if (weightedDistance < closestDistance) {
                closestDistance = weightedDistance;
                closestFood = food;
            }
        }

        return closestFood;
    }

    // Check for nearby opponents
    function detectThreat() {
        const opponents = Array.from(document.querySelectorAll('.enemy')); // Find all enemy players
        const player = document.querySelector('.player');
        if (!player || opponents.length === 0) return false;

        const playerBounds = player.getBoundingClientRect();

        for (let opponent of opponents) {
            const opponentBounds = opponent.getBoundingClientRect();

            // Check if opponent is near the player in any direction
            const isThreatNearby =
                Math.abs(opponentBounds.top - playerBounds.top) < 50 &&
                Math.abs(opponentBounds.left - playerBounds.left) < 50;

            if (isThreatNearby) {
                logStatus('Threat detected! Initiating evasive action...');
                return true;
            }
        }

        return false;
    }

    // Determine the safest direction to move
    function getSafeDirection() {
        const directions = ['up', 'down', 'left', 'right'];
        let safeDirection = directions[Math.floor(Math.random() * directions.length)];

        // Simple logic: Avoid edges as much as possible
        const player = document.querySelector('.player');
        if (player) {
            const bounds = player.getBoundingClientRect();

            if (bounds.top <= 50) safeDirection = 'down';
            else if (bounds.bottom >= window.innerHeight - 50) safeDirection = 'up';
            else if (bounds.left <= 50) safeDirection = 'right';
            else if (bounds.right >= window.innerWidth - 50) safeDirection = 'left';
        }

        return safeDirection;
    }

    // Main bot logic
    function startBot() {
        intervalId = setInterval(() => {
            if (!freezeMode) {
                const player = document.querySelector('.player');
                if (!player) return;

                const playerBounds = player.getBoundingClientRect();

                if (detectThreat()) {
                    // If a threat is detected, move in the safest direction
                    const escapeDirection = getSafeDirection();
                    movePlayer(escapeDirection);
                    logStatus(`Escaping to ${escapeDirection}`);
                } else {
                    // Target the closest and most valuable food
                    const closestFood = getClosestFood(playerBounds);
                    if (closestFood) {
                        const foodBounds = closestFood.getBoundingClientRect();
                        const dx = foodBounds.left - playerBounds.left;
                        const dy = foodBounds.top - playerBounds.top;

                        if (Math.abs(dx) > Math.abs(dy)) {
                            movePlayer(dx > 0 ? 'right' : 'left');
                        } else {
                            movePlayer(dy > 0 ? 'down' : 'up');
                        }

                        logStatus(`Moving towards food at (${foodBounds.left}, ${foodBounds.top})`);
                    } else {
                        // No food detected; continue expanding territory
                        const nextDirection = getSafeDirection();
                        movePlayer(nextDirection);
                        logStatus(`Expanding territory by moving ${nextDirection}`);
                    }
                }
            } else {
                logStatus('Freeze mode activated. No movement.');
            }
        }, 100); // Adjust speed as needed
    }

    // Stop the bot
    function stopBot() {
        clearInterval(intervalId);
        logStatus('Bot stopped.');
    }

    // Toggle freeze mode
    function toggleFreeze() {
        freezeMode = !freezeMode;
        logStatus(freezeMode ? 'Freeze mode ON' : 'Freeze mode OFF');
    }

    // Toggle auto-play mode
    function toggleAutoPlay() {
        autoPlayMode = !autoPlayMode;
        if (autoPlayMode) {
            logStatus('Auto-play mode ON');
            startBot();
        } else {
            logStatus('Auto-play mode OFF');
            stopBot();
        }
    }

    // Listen for user input (toggle features)
    document.addEventListener('keydown', (e) => {
        if (e.key.toUpperCase() === KEYS.freeze) toggleFreeze();
        if (e.key.toUpperCase() === KEYS.autoPlay) toggleAutoPlay();
    });

    // Notify user of controls
    logStatus(`Script loaded. Press "${KEYS.autoPlay}" to toggle Auto-Play, "${KEYS.freeze}" to toggle Freeze.`);
})();