Package:"com.tencent.tmgp.pubgmhd"

#include <vector>
#include <cmath>
#include <limits>

#define INF std::numeric_limits<float>::infinity()

struct Vector3 {
    float x, y, z;

    inline Vector3 operator-(const Vector3& o) const { return {x - o.x, y - o.y, z - o.z}; }
    inline float Dot(const Vector3& o) const { return x * o.x + y * o.y + z * o.z; }
    inline float Length() const { return std::sqrt(Dot(*this)); }
    inline Vector3 Normalize() const {
        float len = Length();
        if (len == 0.0f) return {0.0f, 0.0f, 0.0f};
        return {x / len, y / len, z / len};
    }
};

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

    inline 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:
    explicit Bullet(Character* enemy) {
        if (enemy && enemy->isAlive) {
            InstantHit(enemy);
        }
    }

    static inline void InstantHit(Character* enemy) {
        enemy->isAlive = false;
    }
};

class CameraSystem {
public:
    static inline void AimAt(const Vector3& targetHead, const Vector3& cameraPos) {
        // 强制准星在 0.01 秒内对准敌人头部
        Vector3 direction = (targetHead - cameraPos).Normalize();
    }
};

class Player {
public:
    Vector3 position;
    bool isFiring;

    inline void Fire(std::vector<Character*>& enemies, std::vector<Bullet>& bullets) {
        if (!isFiring) return;

        Character* nearest = FindNearestEnemy(enemies);
        if (nearest) {
            bullets.emplace_back(nearest);
            AimToHead(nearest);
        }
    }

    inline void AimToHead(Character* enemy) {
        Vector3 head = enemy->GetHeadPosition();
        CameraSystem::AimAt(head, position);
    }

    inline Character* FindNearestEnemy(std::vector<Character*>& enemies) {
        Character* result = nullptr;
        float minDist = INF;

        for (auto ch : enemies) {
            if (ch->isEnemy && ch->isAlive) {
                float dist = (ch->position - position).Length();
                if (dist < minDist) {
                    minDist = dist;
                    result = ch;
                }
            }
        }

        return result;
    }
};

class GameWorld {
public:
    Player player;
    std::vector<Character*> enemies;
    std::vector<Bullet> bullets;

    inline void SimulateFire() {
        player.Fire(enemies, bullets);
    }

    inline void Update(float deltaTime) {
        bullets.erase(
            std::remove_if(bullets.begin(), bullets.end(),
                           [](const Bullet& b) { return !b.target || !b.target->isAlive; }),
            bullets.end());
    }
};