Search in sources :

Example 1 with EngineEntity

use of thpmc.engine.api.entity.EngineEntity in project THP-Engine by TheHollowPlanetMC.

the class TickRunner method run.

@Override
public void run() {
    if (beforeTime + TIME - 20 > System.currentTimeMillis())
        return;
    if (isStopped)
        return;
    if (i % TPS == 0) {
        long time = System.currentTimeMillis();
        tps = ((double) Math.round((20.0 / (((double) (time - beforeTime)) / 1000.0)) * 10)) / 10;
        beforeTime = time;
    }
    currentThread = Thread.currentThread();
    this.lastTickMS = System.currentTimeMillis();
    // Should remove check
    entities.removeIf(TickBase::shouldRemove);
    tickOnlyEntities.removeIf(TickBase::shouldRemove);
    // Add entities
    if (addEntities.size() != 0) {
        try {
            ADD_LOCK.lock();
            for (TickBase entity : addEntities) {
                if (entity instanceof EngineEntity) {
                    entities.add((EngineEntity) entity);
                } else {
                    tickOnlyEntities.add(entity);
                }
            }
            addEntities.clear();
        } finally {
            ADD_LOCK.unlock();
        }
    }
    // tick
    tickOnlyEntities.forEach(TickBase::tick);
    entities.forEach(TickBase::tick);
    // Tracker
    for (EnginePlayer enginePlayer : EnginePlayer.getAllPlayers()) {
        EntityTracker entityTracker = getEntityTracker(enginePlayer);
        entityTracker.tick(entities);
    }
    if (i % 40 == 0) {
        trackerMap.keySet().removeIf(enginePlayer -> !enginePlayer.getBukkitPlayer().isOnline());
    }
    entities.forEach(EngineEntity::setPreviousPosition);
    if (i % 100 == 0)
        System.out.printf("ID -> %d   TPS: %.1f%n", ID, tps);
    i++;
}
Also used : EngineEntity(thpmc.engine.api.entity.EngineEntity) TickBase(thpmc.engine.api.entity.TickBase) EnginePlayer(thpmc.engine.api.player.EnginePlayer)

Example 2 with EngineEntity

use of thpmc.engine.api.entity.EngineEntity in project THP-Engine by TheHollowPlanetMC.

the class EngineWorld method rayTraceEntitiesForShortRange.

@Nullable
default EngineRayTraceResult rayTraceEntitiesForShortRange(@NotNull Vector startPosition, @NotNull Vector direction, double distance, @NotNull CollideOption collideOption) {
    Vector movement = direction.clone().normalize().multiply(distance);
    Vector endPosition = startPosition.clone().add(movement);
    // create region for get entities
    EngineBoundingBox region = new EngineBoundingBox(startPosition.getX(), startPosition.getY(), startPosition.getZ(), endPosition.getX(), endPosition.getY(), endPosition.getZ()).expandForMovement(movement);
    region.expand(collideOption.getBoundingBoxGrow());
    region.expand(16);
    // get entities
    Collection<EngineEntity> entities = getEntitiesInRangeChunkSection(region);
    // collect collisions
    Set<EngineBoundingBox> boxList = new HashSet<>();
    for (EngineEntity entity : entities) {
        EngineEntityBoundingBox boundingBox = entity.getBoundingBox();
        if (boundingBox != null) {
            boxList.add(boundingBox);
        }
    }
    // perform raytrace
    return rayTraceForCollisionList(startPosition, direction, distance, boxList, collideOption);
}
Also used : EngineEntityBoundingBox(thpmc.engine.api.util.collision.EngineEntityBoundingBox) EngineEntity(thpmc.engine.api.entity.EngineEntity) Vector(org.bukkit.util.Vector) EngineBoundingBox(thpmc.engine.api.util.collision.EngineBoundingBox) HashSet(java.util.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with EngineEntity

use of thpmc.engine.api.entity.EngineEntity in project THP-Engine by TheHollowPlanetMC.

the class EntityTracker method tick.

public void tick(Set<EngineEntity> entities) {
    tick++;
    // Collect tracking entities
    if (tick % TRACK_INTERVAL == 0) {
        Location pl = enginePlayer.getBukkitPlayer().getLocation();
        int playerChunkX = pl.getBlockX() >> 4;
        int playerChunkZ = pl.getBlockZ() >> 4;
        int drawDistance = enginePlayer.getEntityDrawDistance();
        ThreadLocalEngineWorld world = tickRunner.getThreadLocalCache().getWorld(Objects.requireNonNull(pl.getWorld()).getName());
        for (EngineEntity entity : entities) {
            EngineLocation location = entity.getLocation();
            if (location.getWorld() != world) {
                continue;
            }
            int entityChunkX = location.getBlockX() >> 4;
            int entityChunkZ = location.getBlockZ() >> 4;
            if (Math.abs(entityChunkX - playerChunkX) + Math.abs(entityChunkZ - playerChunkZ) <= drawDistance) {
                // in range
                if (!trackedEntities.contains(entity) && trackedEntities.size() <= (enginePlayer.getEntityTrackLimit() / THPEngineAPI.getInstance().getTickRunnerPool().getPoolSize())) {
                    entity.show(enginePlayer);
                    trackedEntities.add(entity);
                }
            } else {
                // out range
                if (trackedEntities.contains(entity)) {
                    entity.hide(enginePlayer);
                    trackedEntities.remove(entity);
                }
            }
        }
    }
    // Show tick result
    boolean absolute = tick % ABSOLUTE_PACKET_INTERVAL == 0;
    trackedEntities.forEach(engineEntity -> engineEntity.playTickResult(enginePlayer, absolute));
}
Also used : EngineLocation(thpmc.engine.api.world.EngineLocation) ThreadLocalEngineWorld(thpmc.engine.api.world.cache.local.ThreadLocalEngineWorld) EngineEntity(thpmc.engine.api.entity.EngineEntity) Location(org.bukkit.Location) EngineLocation(thpmc.engine.api.world.EngineLocation)

Aggregations

EngineEntity (thpmc.engine.api.entity.EngineEntity)3 HashSet (java.util.HashSet)1 Location (org.bukkit.Location)1 Vector (org.bukkit.util.Vector)1 Nullable (org.jetbrains.annotations.Nullable)1 TickBase (thpmc.engine.api.entity.TickBase)1 EnginePlayer (thpmc.engine.api.player.EnginePlayer)1 EngineBoundingBox (thpmc.engine.api.util.collision.EngineBoundingBox)1 EngineEntityBoundingBox (thpmc.engine.api.util.collision.EngineEntityBoundingBox)1 EngineLocation (thpmc.engine.api.world.EngineLocation)1 ThreadLocalEngineWorld (thpmc.engine.api.world.cache.local.ThreadLocalEngineWorld)1