fCraft.FloatingIslandMapGenState.CreateIsland C# (CSharp) Method

CreateIsland() private method

private CreateIsland ( Vector3I offset ) : Island
offset Vector3I
return Island
        Island CreateIsland( Vector3I offset ) {
            List<Sphere> spheres = new List<Sphere>();

            const int hSpread = 100;
            double vSpreadMin = -hSpread*genParams.Verticality/2,
                   vSpreadMax = hSpread*genParams.Verticality/2;

            double sphereSize = Math.Max( 1,
                                          rand.Next( genParams.SphereSize - genParams.SphereSizeSpread,
                                                     genParams.SphereSize + genParams.SphereSizeSpread + 1 ) );
            Sphere firstSphere = new Sphere( 0, 0, 0, (float)sphereSize );
            spheres.Add( firstSphere );

            for( int i = 1; i < genParams.SphereCount; i++ ) {
                float newRadius = (float)(sphereSize + 1);
                double angle = rand.NextDouble()*Math.PI*2;
                Sphere newSphere = new Sphere( (float)(Math.Cos( angle )*rand.NextDouble()*hSpread),
                                               (float)(Math.Sin( angle )*rand.NextDouble()*hSpread),
                                               (float)(rand.NextDouble() * (vSpreadMax - vSpreadMin) + vSpreadMin),
                                               newRadius );

                double closestDist = newSphere.DistanceTo( spheres[0] );
                Sphere closestSphere = spheres[0];
                for( int j = 1; j < i; j++ ) {
                    double newDist = newSphere.DistanceTo( spheres[j] );
                    if( newDist < closestDist ) {
                        closestDist = newDist;
                        closestSphere = spheres[j];
                    }
                }

                Vector3F displacement = newSphere.Origin - closestSphere.Origin;
                Vector3F direction = displacement.Normalize();
                float distance = (float)Math.Pow( newSphere.Radius + closestSphere.Radius, genParams.SphereSeparation );
                newSphere.Origin = closestSphere.Origin + direction*distance;

                spheres.Add( newSphere );
                sphereSize *= genParams.SphereSizeReduction;
            }

            // step 2: voxelize our spheres
            foreach( Sphere sphere in spheres ) {
                MakeIslandHemisphere( offset, sphere );
            }

            return new Island {
                Spheres = spheres,
                Offset = offset
            };
        }