Search in sources :

Example 1 with Random

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

the class TextureAssetResolverTest method testColorTextures.

@Test
public void testColorTextures() {
    Random r = new FastRandom(123456);
    for (int i = 0; i < 10; i++) {
        int rgba = r.nextInt();
        Color red = new Color(rgba);
        ResourceUrn textureUriForColor = TextureUtil.getTextureUriForColor(red);
        String simpleString = textureUriForColor.toString();
        Optional<Texture> tex = Assets.getTexture(simpleString);
        assertTrue(tex.isPresent());
        ByteBuffer dataBuffer = tex.get().getData().getBuffers()[0];
        int firstPixel = dataBuffer.asIntBuffer().get(0);
        Assert.assertEquals(rgba, firstPixel);
    }
}
Also used : Random(org.terasology.utilities.random.Random) FastRandom(org.terasology.utilities.random.FastRandom) Color(org.terasology.rendering.nui.Color) FastRandom(org.terasology.utilities.random.FastRandom) ResourceUrn(org.terasology.assets.ResourceUrn) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 2 with Random

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

the class TextureDataFactory method createWhiteNoiseTexture.

public static TextureData createWhiteNoiseTexture(int size, long seed, int min, int max) {
    int width = size;
    int height = size;
    ByteBuffer data = ByteBuffer.allocateDirect(4 * width * height);
    Random rng = new FastRandom(seed);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            data.put((byte) TeraMath.clamp(rng.nextInt(min, max), 0, 255));
            data.put((byte) TeraMath.clamp(rng.nextInt(min, max), 0, 255));
            data.put((byte) TeraMath.clamp(rng.nextInt(min, max), 0, 255));
            data.put((byte) 255);
        }
    }
    // The buffer must be reset back to the initial position before passing it onward.
    data.rewind();
    return new TextureData(width, height, new ByteBuffer[] { data }, WrapMode.REPEAT, FilterMode.NEAREST);
}
Also used : Random(org.terasology.utilities.random.Random) FastRandom(org.terasology.utilities.random.FastRandom) FastRandom(org.terasology.utilities.random.FastRandom) ByteBuffer(java.nio.ByteBuffer)

Example 3 with Random

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

the class PlayerConfig method defaultPlayerColor.

/**
 * Randomly generates a default color for the player via a random int generator using FastRandom object.
 *
 * @return a Color object with the player's default color.
 */
private Color defaultPlayerColor() {
    Random rng = new FastRandom();
    List<Color> colors = CieCamColors.L65C65;
    return colors.get(rng.nextInt(colors.size()));
}
Also used : Random(org.terasology.utilities.random.Random) FastRandom(org.terasology.utilities.random.FastRandom) Color(org.terasology.rendering.nui.Color) FastRandom(org.terasology.utilities.random.FastRandom)

Example 4 with Random

use of org.terasology.utilities.random.Random 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 5 with Random

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

the class TreeTests method computeAABB.

private Vector3i computeAABB(TreeGenerator treeGen, long seed) {
    Vector3i pos = new Vector3i(ChunkConstants.SIZE_X / 2, 0, ChunkConstants.SIZE_Z / 2);
    final Vector3i min = new Vector3i(pos);
    final Vector3i max = new Vector3i(pos);
    Rect2i chunks = Rect2i.createFromMinAndMax(-1, -1, 1, 1);
    for (BaseVector2i chunkPos : chunks.contents()) {
        Chunk chunk = new ChunkImpl(chunkPos.getX(), 0, chunkPos.getY(), blockManager, biomeManager) {

            @Override
            public Block setBlock(int x, int y, int z, Block block) {
                Vector3i world = chunkToWorldPosition(x, y, z);
                minimize(min, world);
                maximize(max, world);
                return null;
            }
        };
        Random random = new MersenneRandom(seed);
        BlockManager blockManagerLocal = CoreRegistry.get(BlockManager.class);
        Vector3i relPos = chunk.chunkToWorldPosition(0, 0, 0).sub(pos).invert();
        treeGen.generate(blockManagerLocal, chunk, random, relPos.x, relPos.y, relPos.z);
    }
    Vector3i ext = new Vector3i(max).sub(min);
    return ext;
}
Also used : Rect2i(org.terasology.math.geom.Rect2i) MersenneRandom(org.terasology.utilities.random.MersenneRandom) Random(org.terasology.utilities.random.Random) MersenneRandom(org.terasology.utilities.random.MersenneRandom) ChunkImpl(org.terasology.world.chunks.internal.ChunkImpl) BlockManager(org.terasology.world.block.BlockManager) Vector3i(org.terasology.math.geom.Vector3i) BaseVector2i(org.terasology.math.geom.BaseVector2i) Block(org.terasology.world.block.Block) Chunk(org.terasology.world.chunks.Chunk)

Aggregations

Random (org.terasology.utilities.random.Random)5 FastRandom (org.terasology.utilities.random.FastRandom)4 ByteBuffer (java.nio.ByteBuffer)2 Color (org.terasology.rendering.nui.Color)2 Map (java.util.Map)1 Test (org.junit.Test)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 BaseVector2i (org.terasology.math.geom.BaseVector2i)1 BaseVector3i (org.terasology.math.geom.BaseVector3i)1 Rect2i (org.terasology.math.geom.Rect2i)1 Vector3i (org.terasology.math.geom.Vector3i)1 MersenneRandom (org.terasology.utilities.random.MersenneRandom)1 Block (org.terasology.world.block.Block)1 BlockManager (org.terasology.world.block.BlockManager)1 Chunk (org.terasology.world.chunks.Chunk)1 ChunkImpl (org.terasology.world.chunks.internal.ChunkImpl)1