Search in sources :

Example 1 with BoundingBox

use of net.glowstone.entity.physics.BoundingBox in project Glowstone by GlowstoneMC.

the class GlowLightningStrike method getNearbyEntities.

@Override
public List<Entity> getNearbyEntities(double x, double y, double z) {
    // This behavior is similar to CraftBukkit, where a call with args
    // (0, 0, 0) finds any entities whose bounding boxes intersect that of
    // this entity.
    BoundingBox searchBox = BoundingBox.fromPositionAndSize(location.toVector(), new Vector(0, 0, 0));
    Vector vec = new Vector(x, y, z);
    Vector vec2 = new Vector(0, 0.5 * y, 0);
    searchBox.minCorner.subtract(vec).add(vec2);
    searchBox.maxCorner.add(vec).add(vec2);
    return world.getEntityManager().getEntitiesInside(searchBox, this);
}
Also used : BoundingBox(net.glowstone.entity.physics.BoundingBox) Vector(org.bukkit.util.Vector)

Example 2 with BoundingBox

use of net.glowstone.entity.physics.BoundingBox in project Glowstone by GlowstoneMC.

the class GlowWorld method getNearbyEntities.

/**
     * Returns a list of entities within a bounding box centered around a Location.
     * <p>
     * Some implementations may impose artificial restrictions on the size of the search bounding box.
     *
     * @param location The center of the bounding box
     * @param x        1/2 the size of the box along x axis
     * @param y        1/2 the size of the box along y axis
     * @param z        1/2 the size of the box along z axis
     * @return the collection of entities near location. This will always be a non-null collection.
     */
@Override
public Collection<Entity> getNearbyEntities(Location location, double x, double y, double z) {
    Vector minCorner = new Vector(location.getX() - x, location.getY() - y, location.getZ() - z);
    Vector maxCorner = new Vector(location.getX() + x, location.getY() + y, location.getZ() + z);
    // TODO: test
    BoundingBox searchBox = BoundingBox.fromCorners(minCorner, maxCorner);
    GlowEntity except = null;
    return entities.getEntitiesInside(searchBox, except);
}
Also used : BoundingBox(net.glowstone.entity.physics.BoundingBox) Vector(org.bukkit.util.Vector)

Example 3 with BoundingBox

use of net.glowstone.entity.physics.BoundingBox in project Glowstone by GlowstoneMC.

the class GlowWorld method strikeLightningInChunk.

private void strikeLightningInChunk(int cx, int cz) {
    int n = random.nextInt();
    // get lightning target block
    int x = (cx << 4) + (n & 0xF);
    int z = (cz << 4) + (n >> 8 & 0xF);
    int y = getHighestBlockYAt(x, z);
    // search for living entities in a 6×6×h (there's an error in the wiki!) region from 3 below the
    // target block up to the world height
    BoundingBox searchBox = BoundingBox.fromPositionAndSize(new Vector(x, y, z), new Vector(0, 0, 0));
    Vector vec = new Vector(3, 3, 3);
    Vector vec2 = new Vector(0, getMaxHeight(), 0);
    searchBox.minCorner.subtract(vec);
    searchBox.maxCorner.add(vec).add(vec2);
    List<LivingEntity> livingEntities = new LinkedList<>();
    // make sure entity can see sky
    getEntityManager().getEntitiesInside(searchBox, null).stream().filter(entity -> entity instanceof LivingEntity && !entity.isDead()).forEach(entity -> {
        Vector pos = entity.getLocation().toVector();
        int minY = getHighestBlockYAt(pos.getBlockX(), pos.getBlockZ());
        if (pos.getBlockY() >= minY) {
            livingEntities.add((LivingEntity) entity);
        }
    });
    // re-target lightning if required
    if (!livingEntities.isEmpty()) {
        // randomly choose an entity
        LivingEntity entity = livingEntities.get(random.nextInt(livingEntities.size()));
        // re-target lightning on this living entity
        Vector newTarget = entity.getLocation().toVector();
        x = newTarget.getBlockX();
        z = newTarget.getBlockZ();
        y = newTarget.getBlockY();
    }
    // lightning strike if the target block is under rain
    if (GlowBiomeClimate.isRainy(getBiome(x, z), x, y, z)) {
        strikeLightning(new Location(this, x, y, z));
    }
}
Also used : Plugin(org.bukkit.plugin.Plugin) GlowBlock(net.glowstone.block.GlowBlock) BlockType(net.glowstone.block.blocktype.BlockType) ItemTable(net.glowstone.block.ItemTable) GameRuleManager(net.glowstone.util.GameRuleManager) EntitySpawnEvent(org.bukkit.event.entity.EntitySpawnEvent) Biome(org.bukkit.block.Biome) ChunkGenerator(org.bukkit.generator.ChunkGenerator) MaterialData(org.bukkit.material.MaterialData) org.bukkit(org.bukkit) AnvilWorldStorageProvider(net.glowstone.io.anvil.AnvilWorldStorageProvider) Block(org.bukkit.block.Block) EntityStatusMessage(net.glowstone.net.message.play.entity.EntityStatusMessage) ToString(lombok.ToString) WorldConfig(net.glowstone.util.config.WorldConfig) BoundingBox(net.glowstone.entity.physics.BoundingBox) org.bukkit.entity(org.bukkit.entity) LightningStrikeEvent(org.bukkit.event.weather.LightningStrikeEvent) BlockPopulator(org.bukkit.generator.BlockPopulator) WorldStorageProvider(net.glowstone.io.WorldStorageProvider) ConcurrentSet(net.glowstone.util.collection.ConcurrentSet) CreatureSpawnEvent(org.bukkit.event.entity.CreatureSpawnEvent) GlowChunk(net.glowstone.chunk.GlowChunk) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) ItemStack(org.bukkit.inventory.ItemStack) InvocationTargetException(java.lang.reflect.InvocationTargetException) WorldFinalValues(net.glowstone.io.WorldMetadataService.WorldFinalValues) MetadataValue(org.bukkit.metadata.MetadataValue) ChunkLock(net.glowstone.chunk.ChunkManager.ChunkLock) Key(net.glowstone.chunk.GlowChunk.Key) GlowFallingBlock(net.glowstone.entity.objects.GlowFallingBlock) GlowStructure(net.glowstone.generator.structures.GlowStructure) EmptySnapshot(net.glowstone.chunk.GlowChunkSnapshot.EmptySnapshot) Setter(lombok.Setter) ThunderChangeEvent(org.bukkit.event.weather.ThunderChangeEvent) java.util(java.util) Getter(lombok.Getter) Message(com.flowpowered.network.Message) WeatherChangeEvent(org.bukkit.event.weather.WeatherChangeEvent) Constructor(java.lang.reflect.Constructor) Level(java.util.logging.Level) Consumer(org.bukkit.util.Consumer) ServerDifficultyMessage(net.glowstone.net.message.play.player.ServerDifficultyMessage) RayUtil(net.glowstone.util.RayUtil) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) GlowItem(net.glowstone.entity.objects.GlowItem) SpawnReason(org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason) org.bukkit.event.world(org.bukkit.event.world) MetadataStore(org.bukkit.metadata.MetadataStore) ChunkSection(net.glowstone.chunk.ChunkSection) StandardMessenger(org.bukkit.plugin.messaging.StandardMessenger) BlockState(org.bukkit.block.BlockState) IOException(java.io.IOException) File(java.io.File) Vector(org.bukkit.util.Vector) net.glowstone.constants(net.glowstone.constants) BlockStateDelegate(net.glowstone.util.BlockStateDelegate) ChunkManager(net.glowstone.chunk.ChunkManager) MetadataStoreBase(org.bukkit.metadata.MetadataStoreBase) net.glowstone.entity(net.glowstone.entity) BoundingBox(net.glowstone.entity.physics.BoundingBox) Vector(org.bukkit.util.Vector)

Example 4 with BoundingBox

use of net.glowstone.entity.physics.BoundingBox in project Glowstone by GlowstoneMC.

the class GlowEntity method pulsePhysics.

protected void pulsePhysics() {
    if (velocity.lengthSquared() > 0.01) {
        double dx = 0;
        double dy = 0;
        double dz = 0;
        double ndx = 0;
        double ndy = 0;
        double ndz = 0;
        double x = location.getX();
        double y = location.getY();
        double z = location.getZ();
        Vector inc = velocity.clone().normalize();
        BoundingBox test;
        boolean bbRelevant = (boundingBox == null ? 0 : boundingBox.getSize().lengthSquared()) >= 1;
        if (velocity.getY() < 0d) {
            if (!location.clone().add(new Vector(0, -1, 0)).getBlock().getType().isSolid()) {
                block: while (dy >= velocity.getY()) {
                    ndy += inc.getY();
                    if (Material.getMaterial(((GlowWorld) location.getWorld()).getBlockTypeIdAt((int) x, (int) (y + ndy), (int) z)).isSolid()) {
                        break;
                    }
                    if (bbRelevant) {
                        test = BoundingBox.fromPositionAndSize(new Vector((int) x, (int) (y + ndy), (int) z), boundingBox.getSize());
                        Vector min = test.minCorner, max = test.maxCorner;
                        for (int bbx = min.getBlockX(); bbx <= max.getBlockX(); ++bbx) {
                            for (int bby = min.getBlockY(); bby <= max.getBlockY(); ++bby) {
                                for (int bbz = min.getBlockZ(); bbz <= max.getBlockZ(); ++bbz) {
                                    if (Material.getMaterial(world.getBlockTypeIdAt(bbx, bby, bbz)).isSolid()) {
                                        break block;
                                    }
                                }
                            }
                        }
                    }
                    dy = ndy;
                }
            }
        } else if (velocity.getY() > 0d) {
            block: while (dy <= velocity.getY()) {
                ndy += inc.getY();
                if (Material.getMaterial(((GlowWorld) location.getWorld()).getBlockTypeIdAt((int) x, (int) (y + ndy), (int) z)).isSolid()) {
                    break;
                }
                if (bbRelevant) {
                    test = BoundingBox.fromPositionAndSize(new Vector((int) x, (int) (y + ndy), (int) z), boundingBox.getSize());
                    Vector min = test.minCorner, max = test.maxCorner;
                    for (int bbx = min.getBlockX(); bbx <= max.getBlockX(); ++bbx) {
                        for (int bby = min.getBlockY(); bby <= max.getBlockY(); ++bby) {
                            for (int bbz = min.getBlockZ(); bbz <= max.getBlockZ(); ++bbz) {
                                if (Material.getMaterial(world.getBlockTypeIdAt(bbx, bby, bbz)).isSolid()) {
                                    break block;
                                }
                            }
                        }
                    }
                }
                dy = ndy;
            }
        }
        velocity.setY(dy);
        if (velocity.getX() < 0d) {
            block: while (dx >= velocity.getX()) {
                ndx += inc.getX();
                if (Material.getMaterial(((GlowWorld) location.getWorld()).getBlockTypeIdAt((int) (x + ndx), (int) (y + dy), (int) z)).isSolid()) {
                    break;
                }
                if (bbRelevant) {
                    test = BoundingBox.fromPositionAndSize(new Vector((int) (x + ndx), (int) (y + dy), (int) z), boundingBox.getSize());
                    Vector min = test.minCorner, max = test.maxCorner;
                    for (int bbx = min.getBlockX(); bbx <= max.getBlockX(); ++bbx) {
                        for (int bby = min.getBlockY(); bby <= max.getBlockY(); ++bby) {
                            for (int bbz = min.getBlockZ(); bbz <= max.getBlockZ(); ++bbz) {
                                if (Material.getMaterial(world.getBlockTypeIdAt(bbx, bby, bbz)).isSolid()) {
                                    break block;
                                }
                            }
                        }
                    }
                }
                dx = ndx;
            }
        } else if (velocity.getX() > 0d) {
            block: while (dx <= velocity.getX()) {
                ndx += inc.getX();
                if (Material.getMaterial(((GlowWorld) location.getWorld()).getBlockTypeIdAt((int) (x + ndx), (int) (y + dy), (int) z)).isSolid()) {
                    break;
                }
                if (bbRelevant) {
                    test = BoundingBox.fromPositionAndSize(new Vector((int) (x + ndx), (int) (y + dy), (int) z), boundingBox.getSize());
                    Vector min = test.minCorner, max = test.maxCorner;
                    for (int bbx = min.getBlockX(); bbx <= max.getBlockX(); ++bbx) {
                        for (int bby = min.getBlockY(); bby <= max.getBlockY(); ++bby) {
                            for (int bbz = min.getBlockZ(); bbz <= max.getBlockZ(); ++bbz) {
                                if (Material.getMaterial(world.getBlockTypeIdAt(bbx, bby, bbz)).isSolid()) {
                                    break block;
                                }
                            }
                        }
                    }
                }
                dx = ndx;
            }
        }
        velocity.setX(dx);
        if (velocity.getZ() < 0d) {
            block: while (dz >= velocity.getZ()) {
                ndz += inc.getZ();
                if (Material.getMaterial(((GlowWorld) location.getWorld()).getBlockTypeIdAt((int) (x + dx), (int) (y + dy), (int) (z + ndz))).isSolid()) {
                    break;
                }
                if (bbRelevant) {
                    test = BoundingBox.fromPositionAndSize(new Vector((int) (x + dx), (int) (y + dy), (int) (z + ndz)), boundingBox.getSize());
                    Vector min = test.minCorner, max = test.maxCorner;
                    for (int bbx = min.getBlockX(); bbx <= max.getBlockX(); ++bbx) {
                        for (int bby = min.getBlockY(); bby <= max.getBlockY(); ++bby) {
                            for (int bbz = min.getBlockZ(); bbz <= max.getBlockZ(); ++bbz) {
                                if (Material.getMaterial(world.getBlockTypeIdAt(bbx, bby, bbz)).isSolid()) {
                                    break block;
                                }
                            }
                        }
                    }
                }
                dz = ndz;
            }
        } else if (velocity.getZ() > 0d) {
            block: while (dz <= velocity.getZ()) {
                ndz += inc.getZ();
                if (Material.getMaterial(((GlowWorld) location.getWorld()).getBlockTypeIdAt((int) (x + dx), (int) (y + dy), (int) (z + ndz))).isSolid()) {
                    break;
                }
                if (bbRelevant) {
                    test = BoundingBox.fromPositionAndSize(new Vector((int) (x + dx), (int) (y + dy), (int) (z + ndz)), boundingBox.getSize());
                    Vector min = test.minCorner, max = test.maxCorner;
                    for (int bbx = min.getBlockX(); bbx <= max.getBlockX(); ++bbx) {
                        for (int bby = min.getBlockY(); bby <= max.getBlockY(); ++bby) {
                            for (int bbz = min.getBlockZ(); bbz <= max.getBlockZ(); ++bbz) {
                                if (Material.getMaterial(world.getBlockTypeIdAt(bbx, bby, bbz)).isSolid()) {
                                    break block;
                                }
                            }
                        }
                    }
                }
                dz = ndz;
            }
        }
        velocity.setZ(dz);
        setRawLocation(location.clone().add(velocity));
    }
    // apply friction and gravity
    if (location.getBlock().getType() == Material.WATER) {
        velocity.multiply(liquidDrag);
        velocity.setY(velocity.getY() + getGravityAccel().getY() / 4d);
    } else if (location.getBlock().getType() == Material.LAVA) {
        velocity.multiply(liquidDrag - 0.3);
        velocity.setY(velocity.getY() + getGravityAccel().getY() / 4d);
    } else {
        velocity.setY(airDrag * (velocity.getY() + getGravityAccel().getY()));
        if (isOnGround()) {
            velocity.setX(velocity.getX() * slipMultiplier);
            velocity.setZ(velocity.getZ() * slipMultiplier);
        } else {
            velocity.setX(velocity.getX() * 0.91);
            velocity.setZ(velocity.getZ() * 0.91);
        }
    }
}
Also used : BoundingBox(net.glowstone.entity.physics.BoundingBox) EntityBoundingBox(net.glowstone.entity.physics.EntityBoundingBox) GlowWorld(net.glowstone.GlowWorld) Vector(org.bukkit.util.Vector)

Example 5 with BoundingBox

use of net.glowstone.entity.physics.BoundingBox in project Glowstone by GlowstoneMC.

the class GlowEntity method getNearbyEntities.

////////////////////////////////////////////////////////////////////////////
// Miscellaneous actions
@Override
public List<Entity> getNearbyEntities(double x, double y, double z) {
    // This behavior is similar to CraftBukkit, where a call with args
    // (0, 0, 0) finds any entities whose bounding boxes intersect that of
    // this entity.
    BoundingBox searchBox;
    if (boundingBox == null) {
        searchBox = BoundingBox.fromPositionAndSize(location.toVector(), new Vector(0, 0, 0));
    } else {
        searchBox = BoundingBox.copyOf(boundingBox);
    }
    Vector vec = new Vector(x, y, z);
    searchBox.minCorner.subtract(vec);
    searchBox.maxCorner.add(vec);
    return world.getEntityManager().getEntitiesInside(searchBox, this);
}
Also used : BoundingBox(net.glowstone.entity.physics.BoundingBox) EntityBoundingBox(net.glowstone.entity.physics.EntityBoundingBox) Vector(org.bukkit.util.Vector)

Aggregations

BoundingBox (net.glowstone.entity.physics.BoundingBox)5 Vector (org.bukkit.util.Vector)5 EntityBoundingBox (net.glowstone.entity.physics.EntityBoundingBox)2 Message (com.flowpowered.network.Message)1 File (java.io.File)1 IOException (java.io.IOException)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 java.util (java.util)1 Executors (java.util.concurrent.Executors)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 Level (java.util.logging.Level)1 Collectors (java.util.stream.Collectors)1 Getter (lombok.Getter)1 Setter (lombok.Setter)1 ToString (lombok.ToString)1 GlowWorld (net.glowstone.GlowWorld)1 GlowBlock (net.glowstone.block.GlowBlock)1 ItemTable (net.glowstone.block.ItemTable)1 BlockType (net.glowstone.block.blocktype.BlockType)1