#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <random>

class AdvancedRangeSystem {
private:
    // 基础范围参数
    float baseRangeMultiplier;
    float optimalRange;
    float maxEffectiveRange;
    float minEffectiveRange;
    
    // 伤害衰减系统
    float damageFalloffStart;
    float damageFalloffEnd;
    float criticalRangeMultiplier;
    float minDamageMultiplier;
    
    // 弹道与精度系统
    float projectileVelocity;
    float gravityInfluence;
    float accuracyAtOptimal;
    float accuracyDecayRate;
    float spreadAngle;
    
    // 高级功能参数
    float penetrationDecay;
    float ricochetChance;
    float rangePrediction;
    float environmentalFactor;
    float targetMovementCompensation;
    
    // 动态调整参数
    float currentStability;
    float temperatureEffect;
    float windInfluence;
    
    // 随机数生成器
    std::mt19937 rng;
    std::uniform_real_distribution<float> randomVariation;

public:
    AdvancedRangeSystem() :
        baseRangeMultiplier(1.8f),
        optimalRange(300.0f),
        maxEffectiveRange(1200.0f),
        minEffectiveRange(25.0f),
        damageFalloffStart(150.0f),
        damageFalloffEnd(800.0f),
        criticalRangeMultiplier(2.2f),
        minDamageMultiplier(0.25f),
        projectileVelocity(950.0f),
        gravityInfluence(0.8f),
        accuracyAtOptimal(0.92f),
        accuracyDecayRate(0.15f),
        spreadAngle(2.5f),
        penetrationDecay(0.3f),
        ricochetChance(0.05f),
        rangePrediction(1.1f),
        environmentalFactor(1.0f),
        targetMovementCompensation(0.7f),
        currentStability(0.95f),
        temperatureEffect(1.0f),
        windInfluence(1.0f)
    {
        std::random_device rd;
        rng = std::mt19937(rd());
        randomVariation = std::uniform_real_distribution<float>(-0.1f, 0.1f);
    }

    // 核心伤害计算系统
    float calculatePreciseDamage(float distance, int penetrationCount = 0) {
        if (distance < minEffectiveRange) {
            return baseRangeMultiplier * 0.6f; // 过近惩罚
        }
        
        float damageMultiplier;
        
        // 临界范围奖励
        if (distance <= optimalRange * 1.1f && distance >= optimalRange * 0.9f) {
            damageMultiplier = criticalRangeMultiplier;
        }
        // 伤害衰减计算
        else if (distance <= damageFalloffStart) {
            damageMultiplier = baseRangeMultiplier;
        } else if (distance >= damageFalloffEnd) {
            damageMultiplier = baseRangeMultiplier * minDamageMultiplier;
        } else {
            float falloffRatio = (distance - damageFalloffStart) / (damageFalloffEnd - damageFalloffStart);
            // 使用曲线衰减而非线性
            float curveFactor = 1.0f - std::pow(falloffRatio, 1.5f);
            damageMultiplier = baseRangeMultiplier * (minDamageMultiplier + curveFactor * (1.0f - minDamageMultiplier));
        }
        
        // 穿透衰减
        if (penetrationCount > 0) {
            damageMultiplier *= std::pow(1.0f - penetrationDecay, penetrationCount);
        }
        
        // 环境因素影响
        damageMultiplier *= environmentalFactor;
        
        return std::max(damageMultiplier * currentStability, baseRangeMultiplier * minDamageMultiplier);
    }

    // 高级精度计算系统
    float calculateDynamicAccuracy(float distance, float targetSpeed = 0.0f) {
        float baseAccuracy = accuracyAtOptimal;
        
        // 距离精度衰减（非线性）
        float distanceFactor = distance / optimalRange;
        if (distanceFactor > 1.0f) {
            baseAccuracy *= std::exp(-accuracyDecayRate * (distanceFactor - 1.0f));
        }
        
        // 目标移动补偿
        float movementPenalty = targetSpeed * targetMovementCompensation * 0.01f;
        baseAccuracy = std::max(0.1f, baseAccuracy - movementPenalty);
        
        // 稳定性影响
        baseAccuracy *= currentStability;
        
        // 环境因素
        baseAccuracy *= (1.0f / windInfluence);
        
        // 随机波动
        baseAccuracy += randomVariation(rng) * 0.05f;
        
        return std::clamp(baseAccuracy, 0.1f, 1.0f);
    }

    // 弹道预测系统
    float calculateLeadDistance(float targetDistance, float targetSpeed, float targetAngle) {
        if (targetSpeed <= 0.1f) return 0.0f;
        
        float timeToTarget = targetDistance / projectileVelocity;
        
        // 重力补偿
        timeToTarget *= (1.0f + gravityInfluence * 0.001f * targetDistance);
        
        // 预测倍率调整
        float leadDistance = targetSpeed * timeToTarget * rangePrediction;
        
        // 角度补偿（侧面移动需要更多预判）
        float angleFactor = 1.0f + std::abs(std::sin(targetAngle)) * 0.3f;
        
        return leadDistance * angleFactor;
    }

    // 范围效果计算（用于爆炸物或范围攻击）
    float calculateAreaEffectiveness(float distanceFromCenter, float effectRadius) {
        if (distanceFromCenter > effectRadius) return 0.0f;
        
        // 中心效果最强，边缘衰减
        float distanceRatio = distanceFromCenter / effectRadius;
        float effectiveness = 1.0f - std::pow(distanceRatio, 1.8f);
        
        // 环境因素影响
        effectiveness *= environmentalFactor;
        
        return std::max(0.0f, effectiveness);
    }

    // 环境因素更新
    void updateEnvironmentalConditions(float temperature, float windSpeed, float stability) {
        temperatureEffect = 1.0f + (temperature - 20.0f) * 0.002f; // 每度影响0.2%
        windInfluence = 1.0f + windSpeed * 0.15f; // 风速影响
        currentStability = std::clamp(stability, 0.3f, 1.0f);
        
        // 综合环境因子
        environmentalFactor = (1.0f / temperatureEffect) * (1.0f / windInfluence) * currentStability;
    }

    // 弹道散布计算
    std::pair<float, float> calculateShotSpread(float distance, float accuracy) {
        float baseSpread = spreadAngle * (1.0f - accuracy);
        float distanceSpread = baseSpread * (distance / optimalRange) * 0.01f;
        
        // 随机散布
        float horizontalSpread = distanceSpread * (0.8f + randomVariation(rng) * 0.4f);
        float verticalSpread = distanceSpread * (0.8f + randomVariation(rng) * 0.4f);
        
        return {horizontalSpread, verticalSpread};
    }

    // 设置器方法
    void setRangeParameters(float optimal, float maxRange, float minRange) {
        optimalRange = optimal;
        maxEffectiveRange = maxRange;
        minEffectiveRange = minRange;
        
        // 自动调整衰减区间
        damageFalloffStart = optimalRange * 1.5f;
        damageFalloffEnd = maxEffectiveRange * 0.9f;
    }

    void setProjectileCharacteristics(float velocity, float gravity, float spread) {
        projectileVelocity = velocity;
        gravityInfluence = gravity;
        spreadAngle = spread;
    }

    // 获取器方法
    float getOptimalRange() const { return optimalRange; }
    float getMaxEffectiveRange() const { return maxEffectiveRange; }
    float getCurrentStability() const { return currentStability; }
    float getEnvironmentalFactor() const { return environmentalFactor; }

    // 调试信息输出
    void printRangeInfo(float testDistance) {
        std::cout << "=== 范围系统状态 ===" << std::endl;
        std::cout << "测试距离: " << testDistance << " 单位" << std::endl;
        std::cout << "伤害倍率: " << calculatePreciseDamage(testDistance) << "x" << std::endl;
        std::cout << "精度评估: " << calculateDynamicAccuracy(testDistance) * 100 << "%" << std::endl;
        std::cout << "环境因子: " << environmentalFactor << std::endl;
        std::cout << "稳定性: " << currentStability * 100 << "%" << std::endl;
        
        auto spread = calculateShotSpread(testDistance, calculateDynamicAccuracy(testDistance));
        std::cout << "弹道散布: " << spread.first << "° x " << spread.second << "°" << std::endl;
        std::cout << "====================" << std::endl;
    }
};

// 测试函数
int main() {
    AdvancedRangeSystem rangeSystem;
    
    // 设置测试参数
    rangeSystem.setRangeParameters(350.0f, 1500.0f, 30.0f);
    rangeSystem.setProjectileCharacteristics(1200.0f, 0.6f, 1.8f);
    
    // 模拟环境条件
    rangeSystem.updateEnvironmentalConditions(25.0f, 5.0f, 0.9f);
    
    // 测试不同距离的效果
    std::vector<float> testDistances = {50.0f, 200.0f, 350.0f, 600.0f, 1000.0f, 1400.0f};
    
    std::cout << "高级范围系统测试报告" << std::endl;
    std::cout << "====================" << std::endl;
    
    for (float distance : testDistances) {
        rangeSystem.printRangeInfo(distance);
        std::cout << std::endl;
    }
    
    // 测试移动目标预判
    std::cout << "移动目标预判测试:" << std::endl;
    std::cout << "距离800单位，目标速度15单位/秒，90度角: " 
              << rangeSystem.calculateLeadDistance(800.0f, 15.0f, 90.0f) << " 单位提前量" << std::endl;
    
    return 0;
}
