/Package: com.tencent.tmgp.pubgmhd

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

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

struct Vector3 {
    float x, y, z;

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

    inline float Dot(const Vector3& other) const {
        return x * other.x + y * other.y + z * other.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:
    Vector3 position;
    Character* target;

    explicit Bullet(Vector3 startPos, Character* enemy)
        : position(startPos), target(enemy) {
        if (target && target->isAlive) {
            InstantHit();
        }
    }

    inline void InstantHit() {
        if (target && target->isAlive) {
            target->isAlive = false;
        }
    }
};

class CameraSystem {
public:
    static inline void AimAt(const Vector3& targetHead, const Vector3& cameraPos) {
        Vector3 direction = targetHead - cameraPos;
        direction.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(position, nearest);
            AimToHead(nearest);
        }
    }

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

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

        for (auto ch : enemies) {
            if (ch->isEnemy && ch->isAlive) {
                float dist = (ch->position - position).Length();
                if (dist < minDistance) {
                    minDistance = 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());
    }
};