跳到正文

剪贴板

剪贴板 21isi0h0

原文
剪贴板信息

剪贴板信息

收录
更新

几何决斗---眩晕

代码
// ==UserScript==
// @name         几何决斗
// @namespace    http://tampermonkey.net/
// @version      35.7
// @author       sunhy
// @description  完整内容
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const CONFIG = {
        WIDTH: 1200,
        HEIGHT: 800,
        SIZE: 26,

        SPEED: {
            NORMAL: 14,
            ATTACK: 5,
            CHARGE: 2,
            ARROW: 10
        },

        CHARGE_TIME: 400,
        ARROW_COOLDOWN: 120,
        STUN_TIME: 500,

        AIM_LINE_TIME: 300,

        MAX_PARTICLES: 150,
        SHAKE_DURATION: 60
    };

    let fishMode = true;
    let gameMode = 'single';
    let gameState = 'playing';
    let screenShake = 0;
    let shakeTimer = 0;
    let winner = '';
    let winningPlayer = '';

    // AI模式
    let aiMode = 'enhanced';

    const game = {
        keys: {},
        p1: null, p2: null,
        arrows: [],
        aimLines: [],
        particles: [],
        bgOffset: 0,
        frame: 0
    };

    // ==================== DOM ====================
    const container = document.createElement('div');
    container.style.cssText = 'position:fixed;top:0;left:0;width:100vw;height:100vh;z-index:2147483647;background:#fff;opacity:0;pointer-events:none;';

    const canvas = document.createElement('canvas');
    canvas.width = CONFIG.WIDTH;
    canvas.height = CONFIG.HEIGHT;
    canvas.style.cssText = 'position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);';
    container.appendChild(canvas);

    const modeBtn = document.createElement('div');
    modeBtn.textContent = '单人';
    modeBtn.style.cssText = 'position:absolute;top:20px;right:20px;padding:8px 16px;background:#000;color:#fff;font-family:monospace;font-size:14px;cursor:pointer;z-index:2147483648;opacity:0;pointer-events:none;';
    modeBtn.onclick = (e) => {
        e.stopPropagation();
        if(gameMode === 'single') gameMode = 'dual';
        else if(gameMode === 'dual') gameMode = 'single';
        modeBtn.textContent = gameMode === 'single' ? '单人' : '双人';
        resetGame();

        if(aiModeBtn) {
            aiModeBtn.style.opacity = fishMode ? '0' : (gameMode === 'single' ? '1' : '0');
            aiModeBtn.style.pointerEvents = fishMode ? 'none' : (gameMode === 'single' ? 'auto' : 'none');
        }
    };
    container.appendChild(modeBtn);

    // AI模式切换按钮
    let aiModeBtn = null;
    function createAIModeButton() {
        aiModeBtn = document.createElement('div');
        aiModeBtn.textContent = 'AI: 增强';
        aiModeBtn.style.cssText = `
            position: absolute;
            top: 20px;
            left: 20px;
            padding: 8px 16px;
            background: #000;
            color: #fff;
            font-family: monospace;
            font-size: 14px;
            cursor: pointer;
            z-index: 2147483648;
            opacity: 0;
            pointer-events: none;
            border-radius: 4px;
            border: 2px solid #0ff;
            transition: all 0.3s;
        `;

        aiModeBtn.onclick = (e) => {
            e.stopPropagation();
            aiMode = aiMode === 'enhanced' ? 'simple' : 'enhanced';
            aiModeBtn.textContent = `AI: ${aiMode === 'enhanced' ? '增强' : '简单'}`;
            aiModeBtn.style.borderColor = aiMode === 'enhanced' ? '#0ff' : '#ff0';
            aiModeBtn.style.background = aiMode === 'enhanced' ? '#000' : '#222';
        };

        container.appendChild(aiModeBtn);
    }

    createAIModeButton();

    if(document.body) document.body.appendChild(container);
    else window.addEventListener('load', () => document.body.appendChild(container));

    const ctx = canvas.getContext('2d');

    function resize() {
        const s = Math.min(window.innerWidth / CONFIG.WIDTH, window.innerHeight / CONFIG.HEIGHT) * 0.98;
        canvas.style.width = (CONFIG.WIDTH * s) + 'px';
        canvas.style.height = (CONFIG.HEIGHT * s) + 'px';
    }
    window.addEventListener('resize', resize);
    resize();

    // ==================== 向量类 ====================
    class Vec {
        constructor(x, y) { this.x = x; this.y = y; }
        add(v) { return new Vec(this.x + v.x, this.y + v.y); }
        sub(v) { return new Vec(this.x - v.x, this.y - v.y); }
        mult(n) { return new Vec(this.x * n, this.y * n); }
        mag() { return Math.sqrt(this.x*this.x + this.y*this.y); }
        norm() { const m = this.mag(); return m ? new Vec(this.x/m, this.y/m) : new Vec(0,0); }
        angle() { return Math.atan2(this.y, this.x); }
        dist(v) { return Math.sqrt((this.x-v.x)**2 + (this.y-v.y)**2); }
    }

    // ==================== 粒子系统 ====================
    function addSquareParticle(x, y, vx, vy, size, life) {
        if(game.particles.length >= CONFIG.MAX_PARTICLES) return;
        game.particles.push({
            x, y, vx, vy, size, life, maxLife: life,
            rot: Math.random() * Math.PI,
            rotSpeed: (Math.random()-0.5) * 0.2
        });
    }

    function updateParticles() {
        const pauseMovement = (gameState === 'killcam');

        for(let i = game.particles.length - 1; i >= 0; i--) {
            const p = game.particles[i];

            if(!pauseMovement) {
                p.x += p.vx;
                p.y += p.vy;
                p.vx *= 0.98;
                p.vy *= 0.98;
                p.rot += p.rotSpeed;
            }

            p.life -= 0.03;
            if(p.life <= 0) game.particles.splice(i, 1);
        }
    }

    function drawParticles(ctx) {
        for(const p of game.particles) {
            const a = p.life / p.maxLife;
            ctx.globalAlpha = a * 0.9;

            ctx.save();
            ctx.translate(p.x, p.y);
            ctx.rotate(p.rot);
            const s = p.size * a;

            ctx.fillStyle = '#fff';
            ctx.fillRect(-s/2, -s/2, s, s);
            ctx.strokeStyle = '#000';
            ctx.lineWidth = 1.5;
            ctx.strokeRect(-s/2, -s/2, s, s);

            ctx.restore();
        }
        ctx.globalAlpha = 1;
    }

    // ==================== 箭矢 ====================
    class Arrow {
        constructor(owner, pos, target) {
            this.owner = owner;
            this.x = pos.x;
            this.y = pos.y;
            const dir = target.sub(pos).norm();
            this.vx = dir.x * CONFIG.SPEED.ARROW;
            this.vy = dir.y * CONFIG.SPEED.ARROW;
            this.angle = dir.angle();
            this.active = true;
            this.life = 100;
            this.width = 3;
            this.length = 16;
            this.spawnTime = Date.now();
        }

        getTip() {
            return {x: this.x + Math.cos(this.angle) * 8, y: this.y + Math.sin(this.angle) * 8};
        }

        checkLaserCollision(laserX, laserY, laserAngle) {
            if(!this.active) return false;

            const dir = new Vec(Math.cos(laserAngle), Math.sin(laserAngle));
            const dx = this.x - laserX;
            const dy = this.y - laserY;
            const proj = dx * dir.x + dy * dir.y;

            if(proj < 0) return false;

            const closestX = laserX + dir.x * proj;
            const closestY = laserY + dir.y * proj;
            const dist = Math.sqrt((this.x - closestX)**2 + (this.y - closestY)**2);

            return dist < 20;
        }

        update() {
            if(!this.active) return false;

            this.x += this.vx;
            this.y += this.vy;
            this.life--;

            if(game.frame % 4 === 0) {
                addSquareParticle(this.x, this.y, 0, 0, 2, 0.5);
            }

            if(this.life <= 0 || this.x < -50 || this.x > CONFIG.WIDTH + 50 || this.y < -50 || this.y > CONFIG.HEIGHT + 50) {
                return false;
            }

            // ✅ 修复:任何碰撞的箭矢都抵消,移除时间限制
            const myTip = this.getTip();
            for(const other of game.arrows) {
                if(other === this || !other.active || other.owner === this.owner) continue;

                const otherTip = other.getTip();
                const dist = Math.sqrt((myTip.x-otherTip.x)**2 + (myTip.y-otherTip.y)**2);

                if(dist < 12) {
                    for(let i=0; i<8; i++) {
                        const a = Math.random() * Math.PI * 2;
                        addSquareParticle((myTip.x+otherTip.x)/2, (myTip.y+otherTip.y)/2, Math.cos(a)*2, Math.sin(a)*2, 4, 0.7);
                    }
                    other.active = false;
                    return false;
                }
            }

            const target = this.owner === 'p1' ? game.p2 : game.p1;
            const tip = this.getTip();
            const dist = Math.sqrt((tip.x-target.pos.x)**2 + (tip.y-target.pos.y)**2);
            if(dist < target.size) {
                target.stun = CONFIG.STUN_TIME;

                for(let i=0; i<20; i++) {
                    const a = Math.random() * Math.PI * 2;
                    addSquareParticle(tip.x, tip.y, Math.cos(a)*3, Math.sin(a)*3, 5, 0.8);
                }
                return false;
            }

            return true;
        }

        draw(ctx) {
            ctx.save();
            ctx.translate(this.x, this.y);
            ctx.rotate(this.angle);

            ctx.strokeStyle = '#000';
            ctx.lineWidth = 1.5;
            ctx.beginPath();
            ctx.moveTo(-14, 0);
            ctx.lineTo(4, 0);
            ctx.stroke();

            ctx.fillStyle = '#fff';
            ctx.strokeStyle = '#000';
            ctx.lineWidth = 1;
            ctx.beginPath();
            ctx.moveTo(10, 0);
            ctx.lineTo(-3, 2.5);
            ctx.lineTo(-3, -2.5);
            ctx.closePath();
            ctx.fill();
            ctx.stroke();

            ctx.strokeStyle = '#000';
            ctx.lineWidth = 1;
            ctx.beginPath();
            ctx.moveTo(-12, -3); ctx.lineTo(-16, 3);
            ctx.moveTo(-12, 3); ctx.lineTo(-16, -3);
            ctx.stroke();

            ctx.restore();
        }
    }

    // ==================== 红线预瞄 ====================
    class AimLine {
        constructor(owner, pos, angle) {
            this.owner = owner;
            this.x = pos.x;
            this.y = pos.y;
            this.angle = angle;
            this.life = Math.ceil(CONFIG.AIM_LINE_TIME / 16.67);
            this.active = true;
            this.maxLife = this.life;
        }

        update() {
            const player = this.owner === 'p1' ? game.p1 : game.p2;
            this.x = player.pos.x;
            this.y = player.pos.y;

            this.life--;
            if(this.life <= 0) {
                this.fireLaser();
                return false;
            }
            return true;
        }

        fireLaser() {
            const player = this.owner === 'p1' ? game.p1 : game.p2;
            const target = this.owner === 'p1' ? game.p2 : game.p1;

            const dir = new Vec(Math.cos(this.angle), Math.sin(this.angle));

            let hit = false;
            let hitX = 0, hitY = 0;
            let laserLength = 2000;

            for(let i=0; i<200; i++) {
                const checkX = this.x + dir.x * i * 10;
                const checkY = this.y + dir.y * i * 10;

                if(checkX < 0 || checkX > CONFIG.WIDTH || checkY < 0 || checkY > CONFIG.HEIGHT) {
                    laserLength = i * 10;
                    break;
                }

                const dist = Math.sqrt((checkX-target.pos.x)**2 + (checkY-target.pos.y)**2);
                if(dist < target.size) {
                    hit = true;
                    hitX = checkX;
                    hitY = checkY;
                    laserLength = Math.sqrt((hitX-this.x)**2 + (hitY-this.y)**2);

                    if(this.owner === 'p1') {
                        winner = 'p1';
                        winningPlayer = gameMode === 'single' ? '您' : 'P1';
                    } else {
                        winner = 'p2';
                        winningPlayer = gameMode === 'single' ? 'AI' : 'P2';
                    }

                    break;
                }
            }

            for(let i = game.arrows.length - 1; i >= 0; i--) {
                const arrow = game.arrows[i];
                if(arrow.owner !== this.owner && arrow.checkLaserCollision(this.x, this.y, this.angle)) {
                    for(let j=0; j<10; j++) {
                        const a = Math.random() * Math.PI * 2;
                        addSquareParticle(arrow.x, arrow.y, Math.cos(a)*3, Math.sin(a)*3, 4, 0.6);
                    }
                    arrow.active = false;
                }
            }

            screenShake = hit ? 30 : 15;
            shakeTimer = CONFIG.SHAKE_DURATION;

            for(let i=0; i<50; i++) {
                const t = Math.random();
                const px = this.x + dir.x * t * laserLength;
                const py = this.y + dir.y * t * laserLength;
                addSquareParticle(px, py, (Math.random()-0.5)*2, (Math.random()-0.5)*2, 4, 0.6);
            }

            const endX = this.x + dir.x * laserLength;
            const endY = this.y + dir.y * laserLength;
            for(let i=0; i<15; i++) {
                const a = Math.random() * Math.PI * 2;
                const speed = Math.random() * 6 + 2;
                addSquareParticle(endX, endY, Math.cos(a)*speed, Math.sin(a)*speed, 5, 0.5);
            }

            if(hit) {
                gameState = 'killcam';
                for(let i=0; i<80; i++) {
                    const a = Math.random() * Math.PI * 2;
                    const speed = Math.random() * 15 + 5;
                    const size = Math.random()*12 + 4;
                    addSquareParticle(hitX, hitY, Math.cos(a)*speed, Math.sin(a)*speed, size, 1.8);
                }
            }
        }

        draw(ctx) {
            const dir = new Vec(Math.cos(this.angle), Math.sin(this.angle));
            const endX = this.x + dir.x * 1200;
            const endY = this.y + dir.y * 1200;

            const progress = 1 - (this.life / this.maxLife);
            const flash = 0.5 + Math.sin(Date.now() / 15) * 0.3 + progress * 0.4;
            const alpha = Math.min(1, flash);

            ctx.strokeStyle = `rgba(255,30,30,${alpha})`;
            ctx.lineWidth = 2 + progress * 2;
            ctx.setLineDash([10 - progress * 5, 5]);
            ctx.beginPath();
            ctx.moveTo(this.x, this.y);
            ctx.lineTo(endX, endY);
            ctx.stroke();
            ctx.setLineDash([]);

            if(this.life < 6) {
                ctx.fillStyle = `rgba(255,0,0,${this.life/6})`;
                ctx.beginPath();
                ctx.arc(this.x + dir.x * 25, this.y + dir.y * 25, 4 + (6-this.life), 0, Math.PI*2);
                ctx.fill();
            }
        }
    }

    // ==================== 玩家 ====================
    class Player {
        constructor(id, isAI) {
            this.id = id;
            this.isAI = isAI;
            this.reset();
        }

        reset() {
            this.pos = new Vec(
                this.id === 'p1' ? 150 : CONFIG.WIDTH - 150,
                this.id === 'p1' ? CONFIG.HEIGHT - 150 : 150
            );
            this.vel = new Vec(0, 0);
            this.size = CONFIG.SIZE;
            this.rot = 0;

            this.charging = false;
            this.chargeStart = 0;
            this.chargeLevel = 0;
            this.chargeAng = 0;
            this.aimAngle = 0;

            this.stun = 0;
            this.lastAttack = 0;
            this.aiTimer = 0;
            this.canUlt = true;
        }

        get speed() {
            if(this.stun > 0) return 0;
            if(this.charging) return CONFIG.SPEED.CHARGE;
            return CONFIG.SPEED.NORMAL;
        }

        update() {
            if(this.stun > 0) this.stun -= 16;

            this.pos.x += this.vel.x;
            this.pos.y += this.vel.y;

            this.vel.x *= 0.94;
            this.vel.y *= 0.94;

            this.pos.x = Math.max(this.size, Math.min(CONFIG.WIDTH - this.size, this.pos.x));
            this.pos.y = Math.max(this.size, Math.min(CONFIG.HEIGHT - this.size, this.pos.y));

            if(Math.abs(this.vel.x) + Math.abs(this.vel.y) > 0.5) {
                let targetRot = Math.atan2(this.vel.y, this.vel.x);
                let diff = targetRot - this.rot;
                while(diff > Math.PI) diff -= Math.PI*2;
                while(diff < -Math.PI) diff += Math.PI*2;
                this.rot += diff * 0.12;
            }

            if(this.charging) {
                this.chargeLevel = Math.min((Date.now() - this.chargeStart) / CONFIG.CHARGE_TIME, 1);
                this.chargeAng += 0.15;

                const target = this.id === 'p1' ? game.p2.pos : game.p1.pos;
                this.aimAngle = target.sub(this.pos).angle();
            }
        }

        move(dir) {
            if(this.stun > 0) return;
            if(dir.mag() > 0) {
                const want = dir.norm().mult(this.speed);
                this.vel.x += (want.x - this.vel.x) * 0.15;
                this.vel.y += (want.y - this.vel.y) * 0.15;
            }
        }

        shoot(target) {
            if(this.stun > 0) return;
            if(Date.now() - this.lastAttack < CONFIG.ARROW_COOLDOWN) return;
            this.lastAttack = Date.now();
            game.arrows.push(new Arrow(this.id, this.pos, target));
        }

        startCharge() {
            if(this.charging) return;
            if(this.stun > 0) return;

            this.charging = true;
            this.chargeStart = Date.now();

            const target = this.id === 'p1' ? game.p2.pos : game.p1.pos;
            this.aimAngle = target.sub(this.pos).angle();
        }

        releaseCharge() {
            if(!this.charging) return;
            const full = this.chargeLevel >= 1;

            this.charging = false;
            this.chargeLevel = 0;

            if(full && this.canUlt) {
                game.aimLines.push(new AimLine(this.id, this.pos, this.aimAngle));
            }
        }

        draw(ctx) {
            ctx.save();
            ctx.translate(this.pos.x, this.pos.y);
            ctx.rotate(this.rot);

            if(this.id === 'p1') {
                ctx.fillStyle = '#fff';
                ctx.fillRect(-this.size/2, -this.size/2, this.size, this.size);
                ctx.strokeStyle = '#000';
                ctx.lineWidth = 2.5;
                ctx.strokeRect(-this.size/2, -this.size/2, this.size, this.size);
            } else {
                ctx.fillStyle = '#000';
                ctx.fillRect(-this.size/2, -this.size/2, this.size, this.size);
                ctx.strokeStyle = '#fff';
                ctx.lineWidth = 2.5;
                ctx.strokeRect(-this.size/2, -this.size/2, this.size, this.size);
            }

            ctx.restore();

            if(this.charging) {
                const r = this.size + 16;
                const alpha = 0.4 + this.chargeLevel * 0.6;

                ctx.save();
                ctx.translate(this.pos.x, this.pos.y);

                ctx.rotate(this.chargeAng);
                ctx.strokeStyle = `rgba(0,0,0,${alpha})`;
                ctx.lineWidth = 3;
                ctx.beginPath();
                ctx.arc(0, 0, r, 0.2, Math.PI * 0.8);
                ctx.stroke();

                ctx.rotate(-this.chargeAng * 1.5);
                ctx.strokeStyle = `rgba(0,0,0,${alpha * 0.8})`;
                ctx.lineWidth = 2;
                ctx.beginPath();
                ctx.arc(0, 0, r - 8, Math.PI * 1.2, -0.2);
                ctx.stroke();

                ctx.restore();

                if(this.chargeLevel >= 1) {
                    ctx.fillStyle = `rgba(200,0,0,${0.5 + Math.sin(Date.now()/20)*0.5})`;
                    ctx.font = 'bold 18px monospace';
                    ctx.textAlign = 'center';
                    ctx.fillText('MAX', this.pos.x, this.pos.y - 50);
                }
            }
        }
    }

    // ==================== 增强版AI(修复:躲避大招+主动攻击) ====================
    function updateEnhancedAI() {
        const ai = game.p2;
        if(!ai) return;

        const target = game.p1.pos;
        const dx = target.x - ai.pos.x;
        const dy = target.y - ai.pos.y;
        const dist = Math.sqrt(dx*dx + dy*dy);
        const dir = new Vec(dx, dy).norm();

        // ========== 智能移动 ==========
        let moveIntent = new Vec(0, 0);

        // 基础移动策略
        if(dist > 350) {
            moveIntent = dir.mult(0.9);
        } else if(dist < 120) {
            moveIntent = dir.mult(-0.7);
        } else if(dist < 220) {
            const sideDir = new Vec(-dir.y, dir.x);
            if(Math.random() > 0.5) sideDir.mult(-1);
            moveIntent = sideDir.mult(0.6);
        } else {
            moveIntent = dir.mult(0.4);
        }

        // ========== 躲避子弹 ==========
        let dangerLevel = 0;
        let closestArrowDist = 9999;
        let dodgeVector = new Vec(0, 0);

        for(const arrow of game.arrows) {
            if(arrow.owner === 'p1' && arrow.active) {
                const toArrow = new Vec(arrow.x - ai.pos.x, arrow.y - ai.pos.y);
                const arrowDist = toArrow.mag();

                if(arrowDist < 250) {
                    if(arrowDist < closestArrowDist) {
                        closestArrowDist = arrowDist;
                    }

                    const arrowDir = new Vec(Math.cos(arrow.angle), Math.sin(arrow.angle));
                    const cross = Math.abs(toArrow.x * arrowDir.y - toArrow.y * arrowDir.x);

                    if(cross < 50) {
                        dangerLevel++;

                        const dodgeDir = new Vec(-arrowDir.y, arrowDir.x);
                        const dot = dodgeDir.x * dir.x + dodgeDir.y * dir.y;
                        if(dot > 0) dodgeDir.mult(-1);

                        const dodgeStrength = Math.max(0.3, 1.2 - (arrowDist / 250));
                        dodgeVector = dodgeVector.add(dodgeDir.mult(dodgeStrength));
                    }
                }
            }
        }

        // ========== ✅ 修复:躲避大招 ==========
        for(const aimLine of game.aimLines) {
            if(aimLine.owner === 'p1' && aimLine.active) {
                const toLine = new Vec(aimLine.x - ai.pos.x, aimLine.y - ai.pos.y);
                const lineDist = toLine.mag();

                if(lineDist < 300) {  // 大招300像素内危险
                    const lineDir = new Vec(Math.cos(aimLine.angle), Math.sin(aimLine.angle));

                    // 计算到瞄准线的垂直距离
                    const cross = Math.abs(toLine.x * lineDir.y - toLine.y * lineDir.x);

                    if(cross < 60) {  // 瞄准线60像素内
                        dangerLevel += 2;  // 大招危险程度更高

                        // ✅ 紧急躲避大招:垂直于瞄准线方向
                        const dodgeDir = new Vec(-lineDir.y, lineDir.x);
                        if(Math.random() > 0.5) dodgeDir.mult(-1);

                        const dodgeStrength = Math.max(1.0, 2.0 - (lineDist / 300));
                        dodgeVector = dodgeVector.add(dodgeDir.mult(dodgeStrength * 1.5));

                        // ✅ 如果距离瞄准线太近,也向后移动
                        if(lineDist < 150) {
                            const backDir = new Vec(-lineDir.x, -lineDir.y);
                            dodgeVector = dodgeVector.add(backDir.mult(0.8));
                        }
                    }
                }
            }
        }

        if(dangerLevel > 0 && dodgeVector.mag() > 0) {
            dodgeVector = dodgeVector.norm().mult(1.5);
            moveIntent = moveIntent.add(dodgeVector);
        }

        // 避免撞墙
        const buffer = 60;
        if(ai.pos.x < buffer) moveIntent.x += 0.7;
        if(ai.pos.x > CONFIG.WIDTH - buffer) moveIntent.x -= 0.7;
        if(ai.pos.y < buffer) moveIntent.y += 0.7;
        if(ai.pos.y > CONFIG.HEIGHT - buffer) moveIntent.y -= 0.7;

        // 应用移动
        if(moveIntent.mag() > 0) {
            moveIntent = moveIntent.norm();
            const desiredVx = moveIntent.x * ai.speed;
            const desiredVy = moveIntent.y * ai.speed;
            ai.vel.x += (desiredVx - ai.vel.x) * 0.12;
            ai.vel.y += (desiredVy - ai.vel.y) * 0.12;
        }

        ai.aiTimer++;

        // ========== ✅ 修复:主动攻击决策 ==========
        if(ai.aiTimer > 8) {  // 更频繁思考(8帧一次)

            const angleToTarget = Math.atan2(dy, dx);
            const facingDiff = Math.abs(angleToTarget - ai.rot);
            const isFacing = facingDiff < 1.0;

            if(!ai.stun && dist > 80 && dist < 450) {
                // ✅ 提高攻击概率
                let shootChance = 0.06;  // 6%每8帧,相当于每秒7-8次攻击

                // 调整概率
                if(isFacing) shootChance *= 1.8;
                if(dist < 200) shootChance *= 1.5;
                if(dist > 300) shootChance *= 0.7;
                if(dangerLevel > 0) shootChance *= 0.8;

                // 预判射击
                if(Math.random() < shootChance * 0.7) {
                    const predictTime = dist / CONFIG.SPEED.ARROW;
                    const predictX = target.x + game.p1.vel.x * predictTime * 0.9;
                    const predictY = target.y + game.p1.vel.y * predictTime * 0.9;

                    const offsetX = (Math.random() - 0.5) * 20;
                    const offsetY = (Math.random() - 0.5) * 20;
                    ai.shoot(new Vec(predictX + offsetX, predictY + offsetY));
                    ai.aiTimer = 0;
                }

                // 连射
                if(Math.random() < 0.012) {  // 1.2%概率触发连射
                    setTimeout(() => {
                        if(game.p2 && !game.p2.stun) {
                            const offsetX = (Math.random() - 0.5) * 15;
                            const offsetY = (Math.random() - 0.5) * 15;
                            ai.shoot(new Vec(target.x + offsetX, target.y + offsetY));
                        }
                    }, 100);

                    setTimeout(() => {
                        if(game.p2 && !game.p2.stun) {
                            const offsetX = (Math.random() - 0.5) * 15;
                            const offsetY = (Math.random() - 0.5) * 15;
                            ai.shoot(new Vec(target.x + offsetX, target.y + offsetY));
                        }
                    }, 220);
                }
            }

            // ✅ 蓄力决策
            if(!ai.charging && ai.aiTimer > 40) {
                if(dist > 180 && dist < 320 && !ai.stun) {
                    // 如果玩家在蓄力,AI也蓄力反击
                    if(game.p1.charging) {
                        if(Math.random() < 0.3) {  // 30%概率反击
                            ai.startCharge();
                            ai.aiTimer = 0;
                        }
                    }
                    // 随机蓄力攻击
                    else if(Math.random() < 0.02) {  // 2%概率开始蓄力
                        ai.startCharge();
                        ai.aiTimer = 0;
                    }
                }
            }
        }

        if(ai.charging) {
            ai.aimAngle = target.sub(ai.pos).angle();

            // 危险时取消蓄力
            if(dist < 100 || dangerLevel > 1) {
                ai.charging = false;
                ai.chargeLevel = 0;
            }

            if(ai.chargeLevel >= 1) {
                setTimeout(() => {
                    if(ai.charging) {
                        ai.releaseCharge();
                    }
                }, 50);
            }
        }
    }

    // ==================== 简化版AI(修复:躲避大招+主动攻击) ====================
    function updateSimpleAI() {
        const ai = game.p2;
        if(!ai) return;

        const target = game.p1.pos;
        const dx = target.x - ai.pos.x;
        const dy = target.y - ai.pos.y;
        const dist = Math.sqrt(dx*dx + dy*dy);
        const dir = new Vec(dx, dy).norm();

        // ========== 简单移动 ==========
        let moveIntent = new Vec(0, 0);

        if(dist > 250) {
            moveIntent = dir.mult(0.6);
        } else if(dist < 100) {
            moveIntent = dir.mult(-0.4);
        } else {
            const sideDir = new Vec(-dir.y, dir.x);
            if(Date.now() % 4000 < 2000) sideDir.mult(-1);
            moveIntent = sideDir.mult(0.3);
        }

        // 躲避子弹
        for(const arrow of game.arrows) {
            if(arrow.owner === 'p1' && arrow.active) {
                const toArrow = new Vec(arrow.x - ai.pos.x, arrow.y - ai.pos.y);
                const arrowDist = toArrow.mag();

                if(arrowDist < 150) {
                    const arrowDir = new Vec(Math.cos(arrow.angle), Math.sin(arrow.angle));
                    const cross = Math.abs(toArrow.x * arrowDir.y - toArrow.y * arrowDir.x);

                    if(cross < 30) {
                        const dodgeDir = new Vec(-arrowDir.y, arrowDir.x);
                        moveIntent = moveIntent.add(dodgeDir.mult(0.8));
                    }
                }
            }
        }

        // ✅ 修复:躲避大招
        for(const aimLine of game.aimLines) {
            if(aimLine.owner === 'p1' && aimLine.active) {
                const toLine = new Vec(aimLine.x - ai.pos.x, aimLine.y - ai.pos.y);
                const lineDist = toLine.mag();

                if(lineDist < 250) {
                    const lineDir = new Vec(Math.cos(aimLine.angle), Math.sin(aimLine.angle));
                    const cross = Math.abs(toLine.x * lineDir.y - toLine.y * lineDir.x);

                    if(cross < 50) {
                        const dodgeDir = new Vec(-lineDir.y, lineDir.x);
                        moveIntent = moveIntent.add(dodgeDir.mult(1.2));
                    }
                }
            }
        }

        if(moveIntent.mag() > 0) {
            moveIntent = moveIntent.norm();
            const desiredVx = moveIntent.x * ai.speed;
            const desiredVy = moveIntent.y * ai.speed;
            ai.vel.x += (desiredVx - ai.vel.x) * 0.1;
            ai.vel.y += (desiredVy - ai.vel.y) * 0.1;
        }

        ai.aiTimer++;

        // ✅ 修复:主动攻击
        if(ai.aiTimer > 12) {
            const angleToTarget = Math.atan2(dy, dx);
            const facingDiff = Math.abs(angleToTarget - ai.rot);

            if(!ai.stun && dist > 80 && dist < 400) {
                let baseChance = 0.05;  // 提高攻击概率

                if(facingDiff < 1.0) baseChance = 0.08;

                if(Math.random() < baseChance) {
                    const predictTime = dist / CONFIG.SPEED.ARROW;
                    const predictX = target.x + game.p1.vel.x * predictTime * 0.6;
                    const predictY = target.y + game.p1.vel.y * predictTime * 0.6;
                    ai.shoot(new Vec(predictX, predictY));
                    ai.aiTimer = 0;
                }

                // 蓄力攻击
                if(!ai.charging && ai.aiTimer > 40 && dist > 150 && dist < 300) {
                    if(Math.random() < 0.02) {  // 提高蓄力概率
                        ai.startCharge();
                        ai.aiTimer = 0;
                    }
                }
            }
        }

        if(ai.charging) {
            ai.aimAngle = target.sub(ai.pos).angle();

            if(ai.chargeLevel >= 1) {
                ai.releaseCharge();
            }
        }
    }

    // ==================== 眩晕图标 ====================
    function drawStunIcon(ctx, x, y) {
        ctx.save();
        ctx.translate(x, y);
        ctx.rotate(Date.now() * 0.005);

        ctx.fillStyle = 'rgba(255,50,50,0.8)';
        ctx.beginPath();
        for(let i = 0; i < 5; i++) {
            const angle = i * Math.PI * 0.4;
            const radius = 8;
            ctx.lineTo(
                Math.cos(angle) * radius,
                Math.sin(angle) * radius
            );
        }
        ctx.closePath();
        ctx.fill();

        ctx.restore();
    }

    // ==================== 游戏控制 ====================
    function resetGame() {
        game.p1 = new Player('p1', false);
        game.p2 = new Player('p2', gameMode === 'single');
        game.arrows = [];
        game.aimLines = [];
        game.particles = [];
        gameState = 'playing';
        screenShake = 0;
        shakeTimer = 0;
        winner = '';
        winningPlayer = '';

        if(aiModeBtn) {
            aiModeBtn.style.opacity = fishMode ? '0' : (gameMode === 'single' ? '1' : '0');
            aiModeBtn.style.pointerEvents = fishMode ? 'none' : (gameMode === 'single' ? 'auto' : 'none');
        }
    }

    function toggleFish() {
        fishMode = !fishMode;
        container.style.opacity = fishMode ? '0' : '1';
        container.style.pointerEvents = fishMode ? 'none' : 'auto';
        modeBtn.style.opacity = fishMode ? '0' : '1';
        modeBtn.style.pointerEvents = fishMode ? 'none' : 'auto';

        if(aiModeBtn) {
            aiModeBtn.style.opacity = fishMode ? '0' : (gameMode === 'single' ? '1' : '0');
            aiModeBtn.style.pointerEvents = fishMode ? 'none' : (gameMode === 'single' ? 'auto' : 'none');
        }
    }

    // 按键处理
    document.addEventListener('keydown', (e) => {
        if(e.key === 'Escape') {
            e.preventDefault();
            toggleFish();
            return;
        }

        if(fishMode) return;

        if(gameState === 'killcam') {
            if(e.code === 'Space') {
                resetGame();
            }
            return;
        }

        const k = e.key.toLowerCase();
        game.keys[k] = true;

        if(gameState !== 'playing') return;

        // 单人模式
        if(gameMode === 'single') {
            if(game.p1) {
                if(k === 'j') {
                    e.preventDefault();
                    game.p1.shoot(game.p2.pos);
                }
                if(k === 'k' && !game.p1.charging) {
                    e.preventDefault();
                    game.p1.startCharge();
                }
            }
        }

        // 双人模式
        if(gameMode === 'dual') {
            if(game.p1) {
                if(k === 'q') {
                    e.preventDefault();
                    game.p1.shoot(game.p2.pos);
                }
                if(k === 'e' && !game.p1.charging) {
                    e.preventDefault();
                    game.p1.startCharge();
                }
            }

            if(gameMode === 'dual' && game.p2 && !game.p2.isAI) {
                if(k === 'u') {
                    e.preventDefault();
                    game.p2.shoot(game.p1.pos);
                }
                if(k === 'o' && !game.p2.charging) {
                    e.preventDefault();
                    game.p2.startCharge();
                }
            }
        }
    });

    document.addEventListener('keyup', (e) => {
        const k = e.key.toLowerCase();
        game.keys[k] = false;

        if(fishMode) return;
        if(gameState === 'killcam') return;

        if(gameMode === 'single' && k === 'k' && game.p1 && game.p1.charging) {
            game.p1.releaseCharge();
        }

        if(gameMode === 'dual') {
            if(k === 'e' && game.p1 && game.p1.charging) {
                game.p1.releaseCharge();
            }

            if(gameMode === 'dual' && k === 'o' && game.p2 && game.p2.charging && !game.p2.isAI) {
                game.p2.releaseCharge();
            }
        }
    });

    // 游戏循环
    function update() {
        if(gameState === 'killcam') {
            if(shakeTimer > 0) {
                shakeTimer--;
                if(shakeTimer <= 0) screenShake = 0;
            } else {
                screenShake = 0;
            }
            updateParticles();
            return;
        }

        if(gameState !== 'playing') return;

        game.frame++;

        if(shakeTimer > 0) {
            shakeTimer--;
            if(shakeTimer <= 0) screenShake = 0;
        } else if(screenShake > 0) {
            screenShake *= 0.9;
            if(screenShake < 0.5) screenShake = 0;
        }

        // P1移动
        if(game.p1) {
            let moveDir = new Vec(0, 0);
            if(game.keys['w']) moveDir.y -= 1;
            if(game.keys['s']) moveDir.y += 1;
            if(game.keys['a']) moveDir.x -= 1;
            if(game.keys['d']) moveDir.x += 1;
            game.p1.move(moveDir);
            game.p1.update();
        }

        // P2移动
        if(game.p2) {
            if(game.p2.isAI) {
                if(aiMode === 'enhanced') {
                    updateEnhancedAI();
                } else {
                    updateSimpleAI();
                }
            } else {
                let moveDir = new Vec(0, 0);
                if(game.keys['i']) moveDir.y -= 1;
                if(game.keys['k']) moveDir.y += 1;
                if(game.keys['j']) moveDir.x -= 1;
                if(game.keys['l']) moveDir.x += 1;
                game.p2.move(moveDir);
            }
            game.p2.update();
        }

        game.arrows = game.arrows.filter(a => a.update());
        game.aimLines = game.aimLines.filter(l => l.update());
        updateParticles();
    }

    function draw() {
        let sx = 0, sy = 0;
        if(screenShake > 0) {
            sx = (Math.random() - 0.5) * screenShake;
            sy = (Math.random() - 0.5) * screenShake;
        }

        ctx.save();
        ctx.translate(sx, sy);

        // 背景
        ctx.fillStyle = '#fff';
        ctx.fillRect(0, 0, CONFIG.WIDTH, CONFIG.HEIGHT);

        // 网格
        ctx.strokeStyle = '#ddd';
        ctx.lineWidth = 1;
        game.bgOffset = (game.bgOffset + 0.5) % 40;
        for(let i = -40; i < CONFIG.WIDTH + 40; i += 40) {
            ctx.beginPath();
            ctx.moveTo(i - game.bgOffset, 0);
            ctx.lineTo(i - game.bgOffset, CONFIG.HEIGHT);
            ctx.stroke();
        }
        for(let i = -40; i < CONFIG.HEIGHT + 40; i += 40) {
            ctx.beginPath();
            ctx.moveTo(0, i - game.bgOffset);
            ctx.lineTo(CONFIG.WIDTH, i - game.bgOffset);
            ctx.stroke();
        }

        if(game.p1) game.p1.draw(ctx);
        if(game.p2) game.p2.draw(ctx);

        for(const arrow of game.arrows) arrow.draw(ctx);
        for(const line of game.aimLines) line.draw(ctx);
        drawParticles(ctx);

        // 控制说明
        ctx.fillStyle = '#666';
        ctx.font = '14px monospace';
        ctx.textAlign = 'left';
        if(gameMode === 'single') {
            ctx.fillText('P1: WASD移动 | J射击 | K蓄力(满0.4秒后自动发射)', 20, 30);
            ctx.fillText(`AI模式: ${aiMode === 'enhanced' ? '增强' : '简单'}`, 20, 50);
        } else {
            ctx.fillText('P1: WASD移动 | Q射击 | E蓄力  |  P2: IJKL移动 | U射击 | O蓄力', 20, 30);
        }
        ctx.fillText('ESC隐藏/显示 | 点击右上角切换单/双人', 20, gameMode === 'single' ? 70 : 50);

        // 眩晕图标
        if(game.p1 && game.p1.stun > 0) {
            drawStunIcon(ctx, game.p1.pos.x, game.p1.pos.y - 50);
        }
        if(game.p2 && game.p2.stun > 0) {
            drawStunIcon(ctx, game.p2.pos.x, game.p2.pos.y - 50);
        }

        if(gameState === 'killcam') {
            ctx.fillStyle = 'rgba(0,0,0,0.7)';
            ctx.fillRect(0, 0, CONFIG.WIDTH, CONFIG.HEIGHT);
            ctx.fillStyle = '#fff';
            ctx.font = 'bold 60px monospace';
            ctx.textAlign = 'center';
            ctx.fillText('KO', CONFIG.WIDTH/2, CONFIG.HEIGHT/2 - 20);

            ctx.font = 'bold 40px monospace';
            if(winner === 'p1') {
                if(gameMode === 'single') {
                    ctx.fillText('🎉 您赢了!', CONFIG.WIDTH/2, CONFIG.HEIGHT/2 + 40);
                } else {
                    ctx.fillText('🎉 P1 赢了!', CONFIG.WIDTH/2, CONFIG.HEIGHT/2 + 40);
                }
            } else if(winner === 'p2') {
                if(gameMode === 'single') {
                    ctx.fillText('💀 您输了!', CONFIG.WIDTH/2, CONFIG.HEIGHT/2 + 40);
                } else {
                    ctx.fillText('🎉 P2 赢了!', CONFIG.WIDTH/2, CONFIG.HEIGHT/2 + 40);
                }
            }

            ctx.font = '20px monospace';
            ctx.fillText('按空格键重新开始', CONFIG.WIDTH/2, CONFIG.HEIGHT/2 + 100);
        }

        ctx.restore();
    }

    function loop() {
        update();
        draw();
        requestAnimationFrame(loop);
    }

    resetGame();
    loop();

    window.geometryDuel = {toggle: toggleFish, reset: resetGame};
})();

几何决斗---击退

C++
// P1281 [CERC1998] 书的复制
#include<bits/stdc++.h>
using namespace std;
long long n,m;
long long a[505];
int x[505],y[505];
bool check(int s){
	int num=1,t=0;
	for(int i=n;i>=1;i--){
		if(t+a[i]>s) t=0,num++;
		t+=a[i];
	}
	return num<=m;
}
int find(int l,int r){
	int mid;
	while(l+1<r){
		mid=l+(r-l)/2;
		if(check(mid)) r=mid;
		else l=mid;
	}
	return r;
}
int main() {
	long long l=0,r=0;
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		r+=a[i];
		l=max(l,a[i]);
	}
	int s=find(l,r);
	int t=0,num=1;
	for(int i=1;i<=m;i++) x[i]=y[i]=0;
	y[1]=n;
	for(int i=n;i>=1;i--){
		if(t+a[i]>s) t=0,x[num]=i+1,y[++num]=i;
		t+=a[i];
	}
	x[num]=1;
	for(int i=m;i>=1;i--) cout<<x[i]<<" "<<y[i]<<endl;
	return 0;
}