Search in sources :

Example 1 with BaseVector2i

use of org.terasology.math.geom.BaseVector2i in project Terasology by MovingBlocks.

the class AbstractSpawner method findSpawnPosition.

/**
 * Tries to find a suitable spawning point based on {@link SurfaceHeightFacet} and {@link SeaLevelFacet}.
 * @param searchRadius the radius within a suitable spawning point will be searched
 * @param world the facet-based world
 * @param pos the desired 2D position in that world
 * @return a 3D position above the surface and sea level or <code>null</code> if none was found
 * @throws IllegalStateException if no SurfaceHeightFacet can be created.
 */
protected Vector3f findSpawnPosition(World world, Vector2i pos, int searchRadius) {
    Vector3i ext = new Vector3i(searchRadius, 1, searchRadius);
    Vector3i desiredPos = new Vector3i(pos.getX(), 1, pos.getY());
    // try and find somewhere in this region a spot to land
    Region3i spawnArea = Region3i.createFromCenterExtents(desiredPos, ext);
    Region worldRegion = world.getWorldData(spawnArea);
    // check if generation uses sea level and surface height facets
    SurfaceHeightFacet surfaceHeightFacet = worldRegion.getFacet(SurfaceHeightFacet.class);
    if (surfaceHeightFacet == null) {
        throw new IllegalStateException("surface height facet not found");
    }
    SeaLevelFacet seaLevelFacet = worldRegion.getFacet(SeaLevelFacet.class);
    int seaLevel = (seaLevelFacet != null) ? seaLevelFacet.getSeaLevel() : 0;
    int spiralRad = searchRadius / 2 - 1;
    SpiralIterable spiral = SpiralIterable.clockwise(pos).maxRadius(spiralRad).scale(2).build();
    for (BaseVector2i test : spiral) {
        float val = surfaceHeightFacet.getWorld(test.getX(), test.getY());
        int height = TeraMath.floorToInt(val);
        if (height >= seaLevel) {
            return new Vector3f(test.getX(), height, test.getY());
        }
    }
    // nothing above sea level found
    float y = surfaceHeightFacet.getWorld(pos.getX(), pos.getY());
    return new Vector3f(pos.getX(), y, pos.getY());
}
Also used : SurfaceHeightFacet(org.terasology.world.generation.facets.SurfaceHeightFacet) Vector3f(org.terasology.math.geom.Vector3f) Vector3i(org.terasology.math.geom.Vector3i) BaseVector2i(org.terasology.math.geom.BaseVector2i) Region(org.terasology.world.generation.Region) SeaLevelFacet(org.terasology.world.generation.facets.SeaLevelFacet) SpiralIterable(org.terasology.math.geom.SpiralIterable) Region3i(org.terasology.math.Region3i)

Example 2 with BaseVector2i

use of org.terasology.math.geom.BaseVector2i in project Terasology by MovingBlocks.

the class SurfaceToDensityProvider method process.

@Override
public void process(GeneratingRegion region) {
    SurfaceHeightFacet surfaceHeight = region.getRegionFacet(SurfaceHeightFacet.class);
    DensityFacet facet = new DensityFacet(region.getRegion(), region.getBorderForFacet(DensityFacet.class));
    Region3i area = region.getRegion();
    Rect2i rect = Rect2i.createFromMinAndMax(facet.getRelativeRegion().minX(), facet.getRelativeRegion().minZ(), facet.getRelativeRegion().maxX(), facet.getRelativeRegion().maxZ());
    for (BaseVector2i pos : rect.contents()) {
        float height = surfaceHeight.get(pos);
        for (int y = facet.getRelativeRegion().minY(); y <= facet.getRelativeRegion().maxY(); ++y) {
            facet.set(pos.x(), y, pos.y(), height - area.minY() - y);
        }
    }
    region.setRegionFacet(DensityFacet.class, facet);
}
Also used : SurfaceHeightFacet(org.terasology.world.generation.facets.SurfaceHeightFacet) Rect2i(org.terasology.math.geom.Rect2i) DensityFacet(org.terasology.world.generation.facets.DensityFacet) BaseVector2i(org.terasology.math.geom.BaseVector2i) Region3i(org.terasology.math.Region3i)

Example 3 with BaseVector2i

use of org.terasology.math.geom.BaseVector2i in project Terasology by MovingBlocks.

the class LayeredZoneRegionFunctionTest method setup.

@Before
public void setup() {
    parent.addZone(new Zone("Medium sky", new LayeredZoneRegionFunction(new MinMaxLayerThickness(100, 100), MEDIUM_SKY))).addZone(new Zone("Low sky", new LayeredZoneRegionFunction(new MinMaxLayerThickness(100, 100), LOW_SKY))).addZone(new Zone("Above ground", new LayeredZoneRegionFunction(new MinMaxLayerThickness(100, 100), ABOVE_GROUND))).addZone(new Zone("Ground", new LayeredZoneRegionFunction(new MinMaxLayerThickness(100, 100), GROUND))).addZone(new Zone("Shallow underground", new LayeredZoneRegionFunction(new MinMaxLayerThickness(100, 100), SHALLOW_UNDERGROUND))).addZone(new Zone("Medium underground", new LayeredZoneRegionFunction(new MinMaxLayerThickness(100, 100), MEDIUM_UNDERGROUND)));
    parent.setSeed(12345);
    parent.initialize();
    ListMultimap<Class<? extends WorldFacet>, FacetProvider> facetProviderChains = ArrayListMultimap.create();
    facetProviderChains.put(SurfaceHeightFacet.class, (generatingRegion) -> {
        SurfaceHeightFacet facet = new SurfaceHeightFacet(generatingRegion.getRegion(), generatingRegion.getBorderForFacet(SurfaceHeightFacet.class));
        for (BaseVector2i pos : facet.getRelativeRegion().contents()) {
            facet.set(pos, 100);
        }
        generatingRegion.setRegionFacet(SurfaceHeightFacet.class, facet);
    });
    Map<Class<? extends WorldFacet>, Border3D> borders = new HashMap<>();
    borders.put(SurfaceHeightFacet.class, new Border3D(0, 0, 0));
    region = new RegionImpl(Region3i.createFromCenterExtents(new Vector3i(0, 0, 0), 4), facetProviderChains, borders);
}
Also used : HashMap(java.util.HashMap) Zone(org.terasology.world.zones.Zone) FacetProvider(org.terasology.world.generation.FacetProvider) Border3D(org.terasology.world.generation.Border3D) MinMaxLayerThickness(org.terasology.world.zones.MinMaxLayerThickness) LayeredZoneRegionFunction(org.terasology.world.zones.LayeredZoneRegionFunction) WorldFacet(org.terasology.world.generation.WorldFacet) SurfaceHeightFacet(org.terasology.world.generation.facets.SurfaceHeightFacet) BaseVector2i(org.terasology.math.geom.BaseVector2i) Vector3i(org.terasology.math.geom.Vector3i) RegionImpl(org.terasology.world.generation.RegionImpl) Before(org.junit.Before)

Example 4 with BaseVector2i

use of org.terasology.math.geom.BaseVector2i in project Terasology by MovingBlocks.

the class PlayerFactory method findSpawnPos.

private Optional<Vector3f> findSpawnPos(Vector3f targetPos, float entityHeight) {
    int targetBlockX = TeraMath.floorToInt(targetPos.x);
    int targetBlockY = TeraMath.floorToInt(targetPos.y);
    int targetBlockZ = TeraMath.floorToInt(targetPos.z);
    Vector2i center = new Vector2i(targetBlockX, targetBlockZ);
    for (BaseVector2i pos : SpiralIterable.clockwise(center).maxRadius(32).scale(2).build()) {
        Vector3i testPos = new Vector3i(pos.getX(), targetBlockY, pos.getY());
        Vector3i spawnPos = findOpenVerticalPosition(testPos, entityHeight);
        if (spawnPos != null) {
            return Optional.of(new Vector3f(spawnPos.getX(), spawnPos.getY() + entityHeight, spawnPos.getZ()));
        }
    }
    return Optional.empty();
}
Also used : Vector3f(org.terasology.math.geom.Vector3f) BaseVector2i(org.terasology.math.geom.BaseVector2i) Vector3i(org.terasology.math.geom.Vector3i) Vector2i(org.terasology.math.geom.Vector2i) BaseVector2i(org.terasology.math.geom.BaseVector2i)

Example 5 with BaseVector2i

use of org.terasology.math.geom.BaseVector2i in project Terasology by MovingBlocks.

the class SurfaceProvider method process.

@Override
public void process(GeneratingRegion region) {
    // Create our surface height facet (we will get into borders later)
    Border3D border = region.getBorderForFacet(SurfaceHeightFacet.class);
    SurfaceHeightFacet facet = new SurfaceHeightFacet(region.getRegion(), border);
    // loop through every position on our 2d array
    Rect2i processRegion = facet.getWorldRegion();
    for (BaseVector2i position : processRegion.contents()) {
        facet.setWorld(position, surfaceNoise.noise(position.x(), position.y()) * 20);
    }
    // give our newly created and populated facet to the region
    region.setRegionFacet(SurfaceHeightFacet.class, facet);
}
Also used : SurfaceHeightFacet(org.terasology.world.generation.facets.SurfaceHeightFacet) Rect2i(org.terasology.math.geom.Rect2i) Border3D(org.terasology.world.generation.Border3D) BaseVector2i(org.terasology.math.geom.BaseVector2i)

Aggregations

BaseVector2i (org.terasology.math.geom.BaseVector2i)11 SurfaceHeightFacet (org.terasology.world.generation.facets.SurfaceHeightFacet)9 Rect2i (org.terasology.math.geom.Rect2i)4 Vector3i (org.terasology.math.geom.Vector3i)4 Border3D (org.terasology.world.generation.Border3D)4 Region3i (org.terasology.math.Region3i)3 Vector3f (org.terasology.math.geom.Vector3f)2 SeaLevelFacet (org.terasology.world.generation.facets.SeaLevelFacet)2 SurfaceHumidityFacet (org.terasology.world.generation.facets.SurfaceHumidityFacet)2 SurfaceTemperatureFacet (org.terasology.world.generation.facets.SurfaceTemperatureFacet)2 HashMap (java.util.HashMap)1 Before (org.junit.Before)1 BiomeFacet (org.terasology.core.world.generator.facets.BiomeFacet)1 SpiralIterable (org.terasology.math.geom.SpiralIterable)1 Vector2f (org.terasology.math.geom.Vector2f)1 Vector2i (org.terasology.math.geom.Vector2i)1 MersenneRandom (org.terasology.utilities.random.MersenneRandom)1 Random (org.terasology.utilities.random.Random)1 Block (org.terasology.world.block.Block)1 BlockManager (org.terasology.world.block.BlockManager)1