// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.server.npc.systems;

import com.hypixel.hytale.server.core.modules.debug.DebugUtils;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.server.npc.role.support.DebugSupport;
import com.hypixel.hytale.server.npc.role.Role;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.npc.role.RoleDebugFlags;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.ArchetypeChunk;
import com.hypixel.hytale.component.system.tick.EntityTickingSystem;
import com.hypixel.hytale.component.dependency.SystemDependency;
import com.hypixel.hytale.component.dependency.Order;
import com.hypixel.hytale.component.dependency.Dependency;
import java.util.Set;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.npc.entities.NPCEntity;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.math.vector.Vector3f;

public class AvoidanceSystem extends SteppableTickingSystem
{
    public static final Vector3f DEBUG_COLOR_STEERING_POST;
    public static final Vector3f DEBUG_COLOR_STEERING_PRE;
    public static final Vector3f DEBUG_COLOR_AVOIDANCE;
    public static final Vector3f DEBUG_COLOR_SEPARATION;
    public static final double DEBUG_MIN_VECTOR_DRAW_LENGTH_SQUARED = 0.01;
    public static final double DEBUG_VECTORS_SCALE = 4.0;
    public static final float DEBUG_VECTORS_TIME = 0.05f;
    @Nonnull
    private final ComponentType<EntityStore, NPCEntity> componentType;
    @Nonnull
    private final ComponentType<EntityStore, TransformComponent> transformComponentType;
    @Nonnull
    private final Query<EntityStore> query;
    @Nonnull
    private final Set<Dependency<EntityStore>> dependencies;
    
    public AvoidanceSystem(@Nonnull final ComponentType<EntityStore, NPCEntity> componentType) {
        this.dependencies = (Set<Dependency<EntityStore>>)Set.of(new SystemDependency(Order.AFTER, RoleSystems.BehaviourTickSystem.class));
        this.componentType = componentType;
        this.transformComponentType = TransformComponent.getComponentType();
        this.query = (Query<EntityStore>)Query.and(componentType, this.transformComponentType);
    }
    
    @Nonnull
    @Override
    public Set<Dependency<EntityStore>> getDependencies() {
        return this.dependencies;
    }
    
    @Override
    public boolean isParallel(final int archetypeChunkSize, final int taskCount) {
        return EntityTickingSystem.maybeUseParallel(archetypeChunkSize, taskCount);
    }
    
    @Nonnull
    @Override
    public Query<EntityStore> getQuery() {
        return this.query;
    }
    
    @Override
    public void steppedTick(final float dt, final int index, @Nonnull final ArchetypeChunk<EntityStore> archetypeChunk, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
        final Ref<EntityStore> npcRef = archetypeChunk.getReferenceTo(index);
        final NPCEntity npcComponent = archetypeChunk.getComponent(index, this.componentType);
        assert npcComponent != null;
        final Role role = npcComponent.getRole();
        if (role.isAvoidingEntities() || role.isApplySeparation()) {
            final Ref<EntityStore> target = role.getMarkedEntitySupport().getTargetReferenceToIgnoreForAvoidance();
            if (target != null && target.isValid()) {
                role.getIgnoredEntitiesForAvoidance().add(target);
            }
        }
        if (!role.getActiveMotionController().isObstructed()) {
            final DebugSupport debugSupport = role.getDebugSupport();
            final boolean debugVisAvoidance = debugSupport.isDebugFlagSet(RoleDebugFlags.VisAvoidance);
            final boolean debugVisSeparation = debugSupport.isDebugFlagSet(RoleDebugFlags.VisSeparation);
            final Vector3d preBlendSteering = (debugVisSeparation || debugVisAvoidance) ? role.getBodySteering().getTranslation().clone() : null;
            boolean renderSteering = false;
            final TransformComponent transformComponent = archetypeChunk.getComponent(index, this.transformComponentType);
            assert transformComponent != null;
            final Vector3d position = transformComponent.getPosition();
            final World world = commandBuffer.getExternalData().getWorld();
            if (role.isAvoidingEntities()) {
                role.blendAvoidance(npcRef, position, role.getBodySteering(), commandBuffer);
                if (debugVisAvoidance) {
                    renderSteering = true;
                    renderDebugSteeringVector(position, role.getLastAvoidanceSteering(), AvoidanceSystem.DEBUG_COLOR_AVOIDANCE, world);
                }
            }
            if (role.isApplySeparation()) {
                role.blendSeparation(archetypeChunk.getReferenceTo(index), position, role.getBodySteering(), this.transformComponentType, commandBuffer);
                if (debugVisSeparation) {
                    renderSteering = true;
                    renderDebugSteeringVector(position, role.getLastSeparationSteering(), AvoidanceSystem.DEBUG_COLOR_SEPARATION, world);
                }
            }
            if (renderSteering) {
                renderDebugSteeringVectorInverse(position, preBlendSteering, AvoidanceSystem.DEBUG_COLOR_STEERING_PRE, world);
                renderDebugSteeringVector(position, role.getBodySteering().getTranslation(), AvoidanceSystem.DEBUG_COLOR_STEERING_POST, world);
            }
        }
    }
    
    private static void renderDebugSteeringVector(@Nonnull final Vector3d position, @Nonnull final Vector3d direction, @Nonnull final Vector3f color, @Nonnull final World world) {
        if (direction.squaredLength() < 0.01) {
            return;
        }
        final Vector3d scaledDir = direction.clone().scale(4.0);
        DebugUtils.addArrow(world, position, scaledDir, color, 0.05f, false);
    }
    
    private static void renderDebugSteeringVectorInverse(@Nonnull final Vector3d position, @Nonnull final Vector3d direction, @Nonnull final Vector3f color, @Nonnull final World world) {
        if (direction.squaredLength() < 0.01) {
            return;
        }
        final Vector3d scaledDir = direction.clone().scale(4.0);
        final Vector3d start = position.clone().subtract(scaledDir);
        DebugUtils.addArrow(world, start, scaledDir, color, 0.05f, false);
    }
    
    static {
        DEBUG_COLOR_STEERING_POST = new Vector3f(0.0f, 1.0f, 0.0f);
        DEBUG_COLOR_STEERING_PRE = new Vector3f(1.0f, 0.0f, 0.0f);
        DEBUG_COLOR_AVOIDANCE = new Vector3f(1.0f, 1.0f, 1.0f);
        DEBUG_COLOR_SEPARATION = new Vector3f(0.0f, 0.0f, 1.0f);
    }
}
