Search in sources :

Example 11 with FastRandom

use of org.terasology.utilities.random.FastRandom in project Terasology by MovingBlocks.

the class WhiteNoiseTest method testDistribution.

@Test
public void testDistribution() {
    FastRandom rng = new FastRandom(0xBEEF);
    WhiteNoise noiseGen = new WhiteNoise(0xDEADC0DE);
    int count = 100000;
    int bucketCount = 20;
    int[] buckets = new int[bucketCount];
    for (int i = 0; i < count; i++) {
        float posX = rng.nextFloat() * 100f;
        float posY = rng.nextFloat() * 100f;
        float posZ = rng.nextFloat() * 100f;
        float noise = 0.5f + 0.5f * noiseGen.noise(posX, posY, posZ);
        int idx = (int) (noise * bucketCount);
        if (idx == bucketCount) {
            idx = bucketCount - 1;
        }
        buckets[idx]++;
    }
    float avg = count / bucketCount;
    for (int i = 0; i < bucketCount; i++) {
        float val = Math.abs((buckets[i] - avg) / avg);
        // less than 5% deviation from the expected average
        Assert.assertTrue(val < 0.05);
    }
}
Also used : WhiteNoise(org.terasology.utilities.procedural.WhiteNoise) FastRandom(org.terasology.utilities.random.FastRandom) Test(org.junit.Test)

Example 12 with FastRandom

use of org.terasology.utilities.random.FastRandom in project Terasology by MovingBlocks.

the class BlockDropGrammarSystem method initialise.

@Override
public void initialise() {
    blockItemFactory = new BlockItemFactory(entityManager);
    random = new FastRandom();
}
Also used : BlockItemFactory(org.terasology.world.block.items.BlockItemFactory) FastRandom(org.terasology.utilities.random.FastRandom)

Example 13 with FastRandom

use of org.terasology.utilities.random.FastRandom in project Terasology by MovingBlocks.

the class TreeRasterizer method generateChunk.

@Override
public void generateChunk(CoreChunk chunk, Region chunkRegion) {
    TreeFacet facet = chunkRegion.getFacet(TreeFacet.class);
    for (Map.Entry<BaseVector3i, TreeGenerator> entry : facet.getRelativeEntries().entrySet()) {
        BaseVector3i pos = entry.getKey();
        TreeGenerator treeGen = entry.getValue();
        int seed = relativeToWorld(facet, pos).hashCode();
        Random random = new FastRandom(seed);
        treeGen.generate(blockManager, chunk, random, pos.x(), pos.y(), pos.z());
    }
}
Also used : FastRandom(org.terasology.utilities.random.FastRandom) Random(org.terasology.utilities.random.Random) TreeFacet(org.terasology.core.world.generator.facets.TreeFacet) BaseVector3i(org.terasology.math.geom.BaseVector3i) FastRandom(org.terasology.utilities.random.FastRandom) Map(java.util.Map) TreeGenerator(org.terasology.core.world.generator.trees.TreeGenerator)

Example 14 with FastRandom

use of org.terasology.utilities.random.FastRandom in project Terasology by MovingBlocks.

the class BlockInventorySystem method dropContentsOfInventory.

@ReceiveEvent(components = { InventoryComponent.class, DropBlockInventoryComponent.class, LocationComponent.class })
public void dropContentsOfInventory(DoDestroyEvent event, EntityRef entity) {
    Vector3f position = entity.getComponent(LocationComponent.class).getWorldPosition();
    FastRandom random = new FastRandom();
    int slotCount = InventoryUtils.getSlotCount(entity);
    for (int i = 0; i < slotCount; i++) {
        EntityRef itemInSlot = InventoryUtils.getItemAt(entity, i);
        if (itemInSlot.exists()) {
            inventoryManager.removeItem(entity, entity, itemInSlot, false);
            itemInSlot.send(new DropItemEvent(position));
            itemInSlot.send(new ImpulseEvent(random.nextVector3f(30.0f)));
        }
    }
}
Also used : DropItemEvent(org.terasology.logic.inventory.events.DropItemEvent) Vector3f(org.terasology.math.geom.Vector3f) FastRandom(org.terasology.utilities.random.FastRandom) ImpulseEvent(org.terasology.physics.events.ImpulseEvent) LocationComponent(org.terasology.logic.location.LocationComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Aggregations

FastRandom (org.terasology.utilities.random.FastRandom)14 LocationComponent (org.terasology.logic.location.LocationComponent)4 Random (org.terasology.utilities.random.Random)4 Test (org.junit.Test)3 Component (org.terasology.entitySystem.Component)3 MeshComponent (org.terasology.rendering.logic.MeshComponent)3 ByteBuffer (java.nio.ByteBuffer)2 PojoEntityManager (org.terasology.entitySystem.entity.internal.PojoEntityManager)2 Color (org.terasology.rendering.nui.Color)2 BlockComponent (org.terasology.world.block.BlockComponent)2 WorldInfo (org.terasology.world.internal.WorldInfo)2 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 ResourceUrn (org.terasology.assets.ResourceUrn)1 TreeFacet (org.terasology.core.world.generator.facets.TreeFacet)1 TreeGenerator (org.terasology.core.world.generator.trees.TreeGenerator)1 ComponentSystemManager (org.terasology.engine.ComponentSystemManager)1