import { world, system, EntityDamageCause } from "@minecraft/server";


const VEHICLES = {
    "halo:warthog_gauss": { multiplier: 1.5 },
    "halo:ghost": { multiplier: 1.1 },
    "halo:banshee_hereje": { multiplier: 1.3 },
    "halo:banshee": { multiplier: 1.3 },
    "halo:banshee_2": { multiplier: 1.3 },
    "halo:banshee_3": { multiplier: 1.3 },
    "halo:whartog_gauss_2": { multiplier: 1.5 },
    "halo:whartog_razorback_2": { multiplier: 2 },
    "halo:whartog_razorback": { multiplier: 2 },
    "halo:whartog_metralleta1": { multiplier: 1.5 },
    "halo:whartog_metralleta": { multiplier: 1.5 },
    "halo:ghost1": { multiplier: 1.1 }
};

const BASE_DAMAGE = 250;
const DAMAGE_RADIUS = 3;
const REQUIRED_MOVE_TICKS = 20;
const MIN_DAMAGE_SPEED = 0.4;

const KNOCKBACK_FORCE = 4.5;
const KNOCKBACK_UP = 0.7;

const DIMENSIONS = ["overworld", "nether", "the_end"];


const movingTicks = new Map();

system.runInterval(() => {

    for (const dimId of DIMENSIONS) {

        const dimension = world.getDimension(dimId);
        if (!dimension) continue;

        const entities = dimension.getEntities();

        for (const vehicle of entities) {

            if (!VEHICLES[vehicle.typeId]) continue;

            const vel = vehicle.getVelocity();
            const speed = Math.sqrt(
                vel.x * vel.x + vel.z * vel.z
            );

            if (speed > 0.05) {
                movingTicks.set(
                    vehicle.id,
                    (movingTicks.get(vehicle.id) ?? 0) + 1
                );
            } else {
                movingTicks.set(vehicle.id, 0);
            }

            if (movingTicks.get(vehicle.id) < REQUIRED_MOVE_TICKS) continue;

            if (speed < MIN_DAMAGE_SPEED) continue;

            let riders = [];
            const rideable = vehicle.getComponent("minecraft:rideable");
            if (rideable) {
                riders = rideable.getRiders();
            }

            const forward = vehicle.getViewDirection();
            const pos = vehicle.location;

            const targets = dimension.getEntities({
                location: pos,
                maxDistance: DAMAGE_RADIUS
            });

            for (const target of targets) {

                if (target.id === vehicle.id) continue;

                if (riders.includes(target)) continue;

                const dx = target.location.x - pos.x;
                const dz = target.location.z - pos.z;

                const dot = forward.x * dx + forward.z * dz;
                if (dot <= 0) continue;

                const finalDamage =
                    BASE_DAMAGE * VEHICLES[vehicle.typeId].multiplier;

                target.applyDamage(finalDamage, {
                    cause: EntityDamageCause.entityAttack,
                    damagingEntity: vehicle
                });

                target.applyKnockback(
                    forward.x,
                    forward.z,
                    KNOCKBACK_FORCE,
                    KNOCKBACK_UP
                );
            }
        }
    }
}, 3);


world.afterEvents.entityRemove.subscribe(ev => {
    movingTicks.delete(ev.removedEntityId);
});
