fCraft.Forester.PlantRainForestTrees C# (CSharp) Method

PlantRainForestTrees() private static method

private static PlantRainForestTrees ( ForesterArgs args, ICollection treelist ) : void
args ForesterArgs
treelist ICollection
return void
        private static void PlantRainForestTrees( ForesterArgs args, ICollection<Tree> treelist )
        {
            int treeHeight = args.Height;

            int existingTreeNum = treelist.Count;
            int remainingTrees = args.TreeCount - existingTreeNum;

            const int shortTreeFraction = 6;
            int attempts = 0;
            for ( int i = 0; i < remainingTrees && attempts < MaxTries; attempts++ ) {
                float randomfac =
                    ( float )( ( Math.Sqrt( args.Rand.NextDouble() ) * 1.618 - .618 ) * args.HeightVariation + .5 );

                int height;
                if ( i % shortTreeFraction == 0 ) {
                    height = ( int )( treeHeight + randomfac );
                } else {
                    height = ( int )( treeHeight - randomfac );
                }
                Vector3I xyz = FindRandomTreeLocation( args, height );
                if ( xyz.Y < 0 )
                    continue;

                xyz.Y++;

                bool displaced = false;
                // ReSharper disable LoopCanBeConvertedToQuery
                foreach ( Tree otherTree in treelist ) {
                    Vector3I otherLoc = otherTree.Pos;
                    float otherheight = otherTree.Height;
                    int tallx = otherLoc[0];
                    int tallz = otherLoc[2];
                    float dist = ( float )Math.Sqrt( Sqr( tallx - xyz.X + .5 ) + Sqr( tallz - xyz.Z + .5 ) );
                    float threshold = ( otherheight + height ) * .193f;
                    if ( dist < threshold ) {
                        displaced = true;
                        break;
                    }
                }
                // ReSharper restore LoopCanBeConvertedToQuery
                if ( displaced )
                    continue;
                treelist.Add( new RainforestTree {
                    Args = args,
                    Pos = xyz,
                    Height = height
                } );
                i++;
            }
        }