Package:"com.tencent.tmgp.pubgmhd"

#include <vector>
#include <cmath>

struct Vector3 {
    float x, y, z;

    float DistanceTo(const Vector3& other) const {
        float dx = x - other.x;
        float dy = y - other.y;
        float dz = z - other.z;
        return std::sqrt(dx * dx + dy * dy + dz * dz);
    }

    Vector3 operator-(const Vector3& other) const {
        return {x - other.x, y - other.y, z - other.z};
    }

    Vector3 Normalize() const {
        float len = std::sqrt(x * x + y * y + z * z);
        if (len == 0) return {0, 0, 0};
        return {x / len, y / len, z / len};
    }
};

class Character {
public:
    Vector3 position;
    bool isEnemy;
    bool isAlive;

    Vector3 GetHeadPosition() const {
        Vector3 head = position;
        head.y += 1.6f;
        return head;
    }

    Character(Vector3 pos, bool enemy)
        : position(pos), isEnemy(enemy), isAlive(true) {}
};

class Bullet {
public:
    Vector3 position;
    Vector3 direction;
    float speed = 100000.0f; // 每秒移动速度为 100,000 单位（约等于瞬间命中）
    Character* target = nullptr;

    Bullet(Vector3 startPos, Character* enemy) {
        position = startPos;
        target = enemy;
        if (target) {
            Vector3 headPos = target->GetHeadPosition();
            direction = (headPos - position).Normalize();
        }
    }

    void Update(float deltaTime) {
        if (target && target->isAlive) {
            Vector3 headPos = target->GetHeadPosition();
            Vector3 desiredDir = (headPos - position).Normalize();
            direction = desiredDir;

            position.x += direction.x * speed * deltaTime;
            position.y += direction.y * speed * deltaTime;
            position.z += direction.z * speed * deltaTime;

            if ((position - headPos).DistanceTo({0, 0, 0}) < 0.1f) {
                target->isAlive = false;
            }
        }
    }
};

class GameWorld {
public:
    std::vector<Character*> characters;
    std::vector<Bullet> bullets;

    void Fire(Character* shooter) {
        Character* nearestEnemy = nullptr;
        float minDistance = 30000.0f; // 支持最大 3 万米范围内锁定敌人

        for (auto ch : characters) {
            if (ch->isEnemy != shooter->isEnemy && ch->isAlive) {
                float dist = shooter->position.DistanceTo(ch->position);
                if (dist < minDistance) {
                    minDistance = dist;
                    nearestEnemy = ch;
                }
            }
        }

        if (nearestEnemy) {
            bullets.emplace_back(shooter->position, nearestEnemy);
        }
    }

    void Update(float deltaTime) {
        for (auto& bullet : bullets) {
            bullet.Update(deltaTime);
        }

        bullets.erase(
            std::remove_if(bullets.begin(), bullets.end(),
                           [](const Bullet& b) { return b.target == nullptr || !b.target->isAlive; }),
            bullets.end());
    }
};