private void GenerateTunnels(Vector2 start, float maxLength = 2000.0f, float turnStrength = 0.2f) { //1234 //12345 //498764867 //582764 var random = new FastRandom(498764867); const float step = 2.0f; var terrainHeight = _terrain.TerrainSize.Y / 2.0f; var maxSize = new Vector2(30.0f); var currentPosition = start; var currentDirection = Vector2.UnitX; var length = 0.0f; while (length < maxLength) { var currentSize = maxSize + new Vector2(Math.Max(0.0f, length + 200.0f - maxLength)) * 1.0f; var brushSize = new Vector2(random.NextFloat() * currentSize.X, random.NextFloat() * currentSize.Y); var brush = new CircleBrush( (brushSize.X + brushSize.Y) / 4.0f, currentPosition + new Vector2(random.NextRangedFloat(), random.NextRangedFloat())); _terrain.SetQuads(brush, false, true); // Add rotation. var rotationDir = random.NextRangedFloat(); // Prevent going outside terrain or going back. if (currentPosition.Y > terrainHeight - 200.0f || (currentDirection.X < 0.0f && currentDirection.Y > 0.0f)) { rotationDir = (rotationDir - 1.0f) * 0.1f; } else if (currentPosition.Y < -(terrainHeight + 200.0f) || (currentDirection.X < 0.0f && currentDirection.Y < 0.0f)) { rotationDir = (rotationDir + 1.0f) * 0.1f; } // Apply rotation. var rotation = rotationDir * turnStrength; currentDirection = new Vector2( currentDirection.X * MathCore.Cos(rotation) + -currentDirection.Y * MathCore.Sin(rotation), currentDirection.Y * MathCore.Cos(rotation) + currentDirection.X * MathCore.Sin(rotation)); currentPosition += currentDirection * step; length += step; } _terrain.Refresh(); }
private MineralNode CreateMineralNode(MineralLayer layer, Dictionary <Point, double> tiles) { int minx = int.MaxValue, miny = int.MaxValue, maxx = 0, maxy = 0; var sum = 0.0; foreach (var t in tiles) { var p = t.Key; minx = Math.Min(p.X, minx); miny = Math.Min(p.Y, miny); maxx = Math.Max(p.X, maxx); maxy = Math.Max(p.Y, maxy); sum += t.Value; } var area = new Area(minx, miny, maxx, maxy); var node = layer.CreateNode(area); foreach (var t in tiles) { var u = (t.Value / sum) * TotalAmount; u *= FastRandom.NextFloat(0.9f, 1.1f); node.SetValue(t.Key, (uint)u); } return(node); }
public void InitNoise() { var RNG = new FastRandom(); NoiseTable = new float[NoiseDim + 1, NoiseDim + 1, NoiseDim + 1]; int i, j, k; for (i = 0; i < NoiseDim; i++) for (j = 0; j < NoiseDim; j++) for (k = 0; k < NoiseDim; k++) NoiseTable[i, j, k] = RNG.NextFloat(); }
public LootItem Build() { if (!_item.IsRepackaged && _damaged) { _item.Health *= FastRandom.NextFloat(HEALTH_MODIFIER_LOW, HEALTH_MODIFIER_HIGH); } var id = Guid.NewGuid(); return(new LootItem(id, _item)); }
public void EmitParticule(StaticEntityParticule particuleMetaData, Vector3D EmittedPosition) { int nbr = particuleMetaData.EmittedParticulesAmount; Vector3D startUpPosition = EmittedPosition + particuleMetaData.PositionOffset; while (nbr > 0) { //Randomize the Velocity Vector3 velocity = particuleMetaData.EmitVelocity; velocity.X = particuleMetaData.EmitVelocity.X + _rnd.NextFloat(particuleMetaData.EmitVelocityRandomness.X); velocity.Y = particuleMetaData.EmitVelocity.Y + _rnd.NextFloat(particuleMetaData.EmitVelocityRandomness.Y); velocity.Z = particuleMetaData.EmitVelocity.Z + _rnd.NextFloat(particuleMetaData.EmitVelocityRandomness.Z); float lifetime = particuleMetaData.ParticuleLifeTime + _rnd.NextFloat(particuleMetaData.ParticuleLifeTimeRandomness); Vector3D finalPosition; finalPosition.X = startUpPosition.X + _rnd.NextFloat(particuleMetaData.PositionRandomness.X); finalPosition.Y = startUpPosition.Y + _rnd.NextFloat(particuleMetaData.PositionRandomness.X); finalPosition.Z = startUpPosition.Z + _rnd.NextFloat(particuleMetaData.PositionRandomness.X); Vector3 accelerationForce = particuleMetaData.AccelerationForces; if (particuleMetaData.ApplyWindForce) { accelerationForce += _weather.Wind.WindFlowFlat * 0.05f; } _particules.Add(new SpriteParticule() { AccelerationForce = new Vector3D(accelerationForce), ColorModifier = new ByteColor(particuleMetaData.ParticuleColor.R, particuleMetaData.ParticuleColor.G, particuleMetaData.ParticuleColor.B, (byte)0), InitialPosition = finalPosition, maxAge = lifetime, ParticuleId = particuleMetaData.ParticuleId, Position = new FTSValue <Vector3D>(finalPosition), Size = particuleMetaData.Size, SizeGrowSpeed = particuleMetaData.SizeGrowSpeed, Velocity = velocity, alphaFadingPowBase = particuleMetaData.AlphaFadingPowBase }); nbr--; } }
private static void PlaceClustersArea(DesertDescription description, ClusterGroup clusters, Rectangle area, PostPlacementEffect[,] postEffectMap, Point postEffectMapOffset) { FastRandom fastRandom = new FastRandom(Main.ActiveWorldFileData.Seed).WithModifier(57005uL); Vector2 value = new Vector2(description.Hive.Width, description.Hive.Height); Vector2 value2 = new Vector2(clusters.Width, clusters.Height); Vector2 value3 = description.BlockScale / 2f; for (int i = area.Left; i < area.Right; i++) { for (int j = area.Top; j < area.Bottom; j++) { if (!WorldGen.InWorld(i, j, 1)) { continue; } float num = 0f; int num2 = -1; float num3 = 0f; ushort type = 53; if (fastRandom.Next(3) == 0) { type = 397; } int num4 = i - description.Hive.X; int num5 = j - description.Hive.Y; Vector2 value4 = (new Vector2(num4, num5) - value3) / value * value2; for (int k = 0; k < clusters.Count; k++) { Cluster cluster = clusters[k]; if (Math.Abs(cluster[0].Position.X - value4.X) > 10f || Math.Abs(cluster[0].Position.Y - value4.Y) > 10f) { continue; } float num6 = 0f; foreach (Block item in cluster) { num6 += 1f / Vector2.DistanceSquared(item.Position, value4); } if (num6 > num) { if (num > num3) { num3 = num; } num = num6; num2 = k; } else if (num6 > num3) { num3 = num6; } } float num7 = num + num3; Tile tile = Main.tile[i, j]; bool flag = ((new Vector2(num4, num5) - value3) / value * 2f - Vector2.One).Length() >= 0.8f; PostPlacementEffect postPlacementEffect = PostPlacementEffect.None; if (num7 > 3.5f) { postPlacementEffect = PostPlacementEffect.Smooth; tile.ClearEverything(); tile.wall = 187; if (num2 % 15 == 2) { tile.ResetToType(404); } } else if (num7 > 1.8f) { tile.wall = 187; if ((double)j < Main.worldSurface) { tile.liquid = 0; } else { tile.lava(lava: true); } if (!flag || tile.active()) { tile.ResetToType(396); postPlacementEffect = PostPlacementEffect.Smooth; } } else if (num7 > 0.7f || !flag) { tile.wall = 216; tile.liquid = 0; if (!flag || tile.active()) { tile.ResetToType(type); postPlacementEffect = PostPlacementEffect.Smooth; } } else if (num7 > 0.25f) { FastRandom fastRandom2 = fastRandom.WithModifier(num4, num5); float num8 = (num7 - 0.25f) / 0.45f; if (fastRandom2.NextFloat() < num8) { tile.wall = 187; if ((double)j < Main.worldSurface) { tile.liquid = 0; } else { tile.lava(lava: true); } if (tile.active()) { tile.ResetToType(type); postPlacementEffect = PostPlacementEffect.Smooth; } } } postEffectMap[i - area.X + postEffectMapOffset.X, j - area.Y + postEffectMapOffset.Y] = postPlacementEffect; } } }
/// <summary> /// Will insert associated static entities from a landscape entity. /// Ex : /// For the landscape entity "Tree", multiple static entities can be found "around" it : apple, stick, banana, ... /// </summary> /// <param name="dataCursor"></param> /// <param name="chunk"></param> /// <param name="chunkRnd"></param> /// <param name="entityFactory"></param> /// <param name="landscapeEntities"></param> private void InsertMicrolandscapeStaticEntities(ByteChunkCursor dataCursor, GeneratedChunk chunk, FastRandom chunkRnd, EntityFactory entityFactory, List <LandscapeEntity> landscapeEntities) { if (landscapeEntities == null) { return; } Vector2I chunkWorldPosition = new Vector2I(chunk.Position.X * AbstractChunk.ChunkSize.X, chunk.Position.Z * AbstractChunk.ChunkSize.Z); //The entities are sorted by their origine chunk hashcode value foreach (LandscapeEntity entity in landscapeEntities) { bool isLandscapeEntityRootInsideChunk = (entity.RootLocation.X >= 0 && entity.RootLocation.X < AbstractChunk.ChunkSize.X && entity.RootLocation.Z >= 0 && entity.RootLocation.Z < AbstractChunk.ChunkSize.Z); //Get LandscapeEntity var landscapeEntity = _worldParameters.Configuration.LandscapeEntitiesDico[entity.LandscapeEntityId]; foreach (var staticEntity in landscapeEntity.StaticItems) { //Get number of object var nbr = chunkRnd.Next(staticEntity.Quantity.Min, staticEntity.Quantity.Max); if (isLandscapeEntityRootInsideChunk) { //This entity location is inside the correct chunk. //Its a tree ! if (landscapeEntity is TreeBluePrint) { //Create tree soul and attach this entity to the chunk var soul = entityFactory.CreateEntity <TreeSoul>(); soul.Position = new Vector3D(chunkWorldPosition.X + entity.RootLocation.X + 0.5, entity.RootLocation.Y, chunkWorldPosition.Y + entity.RootLocation.Z + 0.5); soul.TreeRndSeed = entity.GenerationSeed; soul.TreeTypeId = entity.LandscapeEntityId; chunk.Entities.Add(soul); } } else { //If Root out of chunk, devide the qt of object to spawn by 2 nbr = (int)(nbr / 2.0); } //Foreach object to create while (nbr > 0) { //find location of the static entity double x = chunkRnd.NextDouble(-staticEntity.SpawningRange, staticEntity.SpawningRange) + entity.RootLocation.X; double z = chunkRnd.NextDouble(-staticEntity.SpawningRange, staticEntity.SpawningRange) + entity.RootLocation.Z; //If out of current chunk, don't create it if (x < 0 || x >= AbstractChunk.ChunkSize.X || z < 0 || z >= AbstractChunk.ChunkSize.Z) { break; } bool groundSpawing; if (staticEntity.SpawningType == SpawningType.Both) { groundSpawing = chunkRnd.NextFloat() > 0.5; } else { groundSpawing = staticEntity.SpawningType == SpawningType.Ground; } StaticEntity landscapeStaticEntity = null; //Find Y spawning position if (groundSpawing) { dataCursor.SetInternalPosition(MathHelper.Floor(x), entity.RootLocation.Y, MathHelper.Floor(z)); //Loop until I hit the ground, with a maximum of Y - 15 blocks for (int y = entity.RootLocation.Y; y > entity.RootLocation.Y - 15 && y > 0; y--) { if (dataCursor.Read() != WorldConfiguration.CubeId.Air) { //Add Static item here on the ground ! landscapeStaticEntity = (StaticEntity)entityFactory.CreateFromBluePrint(staticEntity.ItemblueprintId); landscapeStaticEntity.Position = new Vector3D(x + chunkWorldPosition.X, dataCursor.InternalPosition.Y + 1, z + chunkWorldPosition.Y); break; } dataCursor.Move(CursorRelativeMovement.Down); } } else { dataCursor.SetInternalPosition(MathHelper.Floor(x), entity.RootLocation.Y + 1, MathHelper.Floor(z)); //Loop until I hit the ground, with a maximum of Y + 15 blocks for (int y = entity.RootLocation.Y + 1; y <= entity.RootLocation.Y + 15 && y < AbstractChunk.ChunkSize.Y; y++) { if (dataCursor.Read() != WorldConfiguration.CubeId.Air) { //Add Static item here on the ground ! landscapeStaticEntity = (StaticEntity)entityFactory.CreateFromBluePrint(staticEntity.ItemblueprintId); landscapeStaticEntity.Position = new Vector3D(x + chunkWorldPosition.X, dataCursor.InternalPosition.Y - 0.25, z + chunkWorldPosition.Y); break; } dataCursor.Move(CursorRelativeMovement.Up); } } if (landscapeStaticEntity != null) { chunk.Entities.Add(landscapeStaticEntity); } nbr--; } } } }
public static float NextFloatRange(this FastRandom rnd) { return (rnd.NextFloat() - 0.5f) * 2f; }
public static float NextFloatRange(this FastRandom rnd, float max) { return (rnd.NextFloat() - 0.5f) * 2f * max; }
private void Initialize() { FastRandom rnd = new FastRandom(Seed); _offset = 9999 * new Vector2(rnd.NextFloat(), rnd.NextFloat()); }
public void EmitParticuleForCubeDestruction(int nbr, TerraCube cube, Vector3I CubeLocation, ref Vector3D cameraLocation) { //Check distance to emit if (MaxRenderingDistance > 0 && Vector3D.DistanceSquared(cameraLocation, new Vector3D(CubeLocation)) > _maxRenderingDistanceSquared) { return; } //GetCube Profile VisualChunk chunk = null; var profile = _visualWorldParameters.WorldParameters.Configuration.BlockProfiles[cube.Id]; //Get Chunk in case if the block is subject to BiomeColoring chunk = _worldChunk.GetChunk(CubeLocation.X, CubeLocation.Z); //Foreach Surrending Cube ByteColor blockAvgColorReceived = new ByteColor(); foreach (var surrendingCube in chunk.BlockData.ChunkCubes.GetSurroundingBlocksIndex(ref CubeLocation)) { var cubeColor = chunk.BlockData.ChunkCubes.Cubes[surrendingCube.Index].EmissiveColor; blockAvgColorReceived.A = Math.Max(blockAvgColorReceived.A, cubeColor.A); blockAvgColorReceived.R = Math.Max(blockAvgColorReceived.R, cubeColor.R); blockAvgColorReceived.G = Math.Max(blockAvgColorReceived.G, cubeColor.G); blockAvgColorReceived.B = Math.Max(blockAvgColorReceived.B, cubeColor.B); } //Get Cube color palette Color[] palette = _cubeColorSampled[cube.Id]; while (nbr > 0) { //Randomize the Velocity Vector3 finalVelocity = new Vector3(0, 1, 0); finalVelocity.X += (float)_rnd.NextDouble(-1.0, 1.0) * 1.5f; finalVelocity.Y += (float)_rnd.NextDouble() * 3f; finalVelocity.Z += (float)_rnd.NextDouble(-1.0, 1.0) * 1.5f; Vector3D CubeCenteredPosition = new Vector3D(CubeLocation.X + _rnd.NextDouble(0.2, 0.8), CubeLocation.Y + _rnd.NextDouble(0.2, 0.8), CubeLocation.Z + _rnd.NextDouble(0.2, 0.8)); //Get Color var color = palette[_rnd.Next(24)]; if (color.A < 255) { ApplyBiomeColor(ref color, profile.BiomeColorArrayTexture, chunk.BlockData.GetColumnInfo(CubeLocation.X - chunk.ChunkPositionBlockUnit.X, CubeLocation.Z - chunk.ChunkPositionBlockUnit.Y)); } _particules.Add(new ColoredParticule() { Age = 0 + (float)_rnd.NextDouble(0, 2.0), computationAge = 0, InitialPosition = CubeCenteredPosition, ParticuleColor = color, Position = new FTSValue <Vector3D>(CubeCenteredPosition), Size = new Vector2(0.1f, 0.1f), Velocity = finalVelocity, ColorReceived = blockAvgColorReceived, SpinningRotation = Quaternion.RotationAxis(Vector3.UnitY, _rnd.NextFloat(-MathHelper.Pi / 8, MathHelper.Pi / 8)) * Quaternion.RotationAxis(Vector3.UnitX, _rnd.NextFloat(-MathHelper.Pi / 16, MathHelper.Pi / 16)) * Quaternion.RotationAxis(Vector3.UnitZ, _rnd.NextFloat(-MathHelper.Pi / 16, MathHelper.Pi / 16)), RotationAngles = new FTSValue <Quaternion>(Quaternion.Identity) }); nbr--; } }
private void RefreshBuffers(TreeBluePrint treeTemplate) { FastRandom rnd = new FastRandom(); FastRandom rndColor = new FastRandom(); //Generate the list of Tree points. List <BlockWithPosition> result = _treeSystem.Generate(rnd.Next(), new S33M3Resources.Structs.Vector3I(), treeTemplate); _letreeVertexCollection = new List <VertexHLSLLTree>(); _letreeIndexCollection = new List <ushort>(); MaxtreeSize.X = int.MinValue; MaxtreeSize.Y = int.MinValue; MaxtreeSize.Z = int.MinValue; MintreeSize.X = int.MaxValue; MintreeSize.Y = int.MaxValue; MintreeSize.Z = int.MaxValue; //For each block foreach (BlockWithPosition block in result) { float blockShade = rnd.NextFloat(0.8f, 1.0f); int vertexOffset = _letreeVertexCollection.Count; //Create the 24 vertex + 36 Index data per cube ! for (int i = 0; i < vertexCube.Length; i++) { ByteColor c; if (block.BlockId == treeTemplate.TrunkBlock) { c = Color.Brown; } else { c = Color.Green; } //int blue = c.B + rndColor.Next(-10, 10); if (blue < 0 || blue > 255) blue = c.B; c.B += (byte)blue; //int red = c.R + rndColor.Next(-10, 10); if (red < 0 || red > 255) red = c.R; c.R += (byte)red; //int green = c.G + rndColor.Next(-10, 10); if (green < 0 || green > 255) blue = c.G; c.G += (byte)green; if (block.WorldPosition.X > MaxtreeSize.X) { MaxtreeSize.X = block.WorldPosition.X; } if (block.WorldPosition.Y > MaxtreeSize.Y) { MaxtreeSize.Y = block.WorldPosition.Y; } if (block.WorldPosition.Z > MaxtreeSize.Z) { MaxtreeSize.Z = block.WorldPosition.Z; } if (block.WorldPosition.X < MintreeSize.X) { MintreeSize.X = block.WorldPosition.X; } if (block.WorldPosition.Y < MintreeSize.Y) { MintreeSize.Y = block.WorldPosition.Y; } if (block.WorldPosition.Z < MintreeSize.Z) { MintreeSize.Z = block.WorldPosition.Z; } _letreeVertexCollection.Add(new VertexHLSLLTree(vertexCube[i] + block.WorldPosition, blockShade, c)); } foreach (var index in indicesCube) { _letreeIndexCollection.Add((ushort)(index + vertexOffset)); } } _bufferDirty = true; }
private void Initialize(ICamera camera) { // Retrieve the necessary data. var vector3 = camera.SceneNode.Position; var cameraPosition = new Vector2(vector3.X, vector3.Y); var halfScreenSize = camera.ScreenSize / 2.0f; _lastCameraPosition = cameraPosition; // Use the viewport's and screen's height to determine our particle size since it's contstant. // Particle size = 2 pixels. _particleSize = camera.ScreenSize.Y / (float)camera.Viewport.Height * 2.0f; _halfParticleSize = _particleSize / 2.0f; // Initialize the particles. for (int i = 0; i < _particles.Length; ++i) { var particle = new Particle { Position = new Vector2( cameraPosition.X + halfScreenSize.X * _random.NextRangedFloat(), cameraPosition.Y + halfScreenSize.Y * _random.NextRangedFloat()), Color = new Color( 1.0f - _random.NextFloat() * 0.2f, 1.0f - _random.NextFloat() * 0.2f, 1.0f - _random.NextFloat() * 0.2f, 1.0f - _random.NextFloat() * 0.2f) }; _particles[i] = particle; RebuildPartice(i, ref particle); } _hasInitialized = true; }