//This file is intended to place the swamp flore (reeds and high grass blades) on the river rim

#declare SwampFloreChoice = seed(13579);
#declare ReedScale = seed(24678);
#declare GrassBlScale = seed(150);
#declare SwampPosSeed = seed(1984);

#declare FloreSpacing = 0.5;
#declare FlorePerLines = 6;

//Return the z coordinate of the bank of river in order to place the swamp flore
//Tests collision with 'HF_RiverBank' height field
#macro DetectCoordSwampAlongRim(X_starting_pos)
	#local Norm = <0, 0, 0>;
	#local Start = < X_starting_pos, 80, 0>;
	#local Position = < 0, 0, 0 >;
	#while( ( (Norm.x = 0 & Norm.y = 0 & Norm.z = 0) | !(Position.y > 1.7 & Position.y < 3.2) ) & (Position.z <= 0 & Position.z > -60 ) )
		#local Position = trace( HF_RiverBank, Start, -y, Norm );
		#local Start = Start - <0, 0, 0.001>;
	#end
	Position
#end

#macro PlaceSwampFloreSample( SeedValue, Position, GrassScaleValue, ReedScaleValue )
	#local RandPosNoise = < - FloreSpacing / 2 + FloreSpacing * rand(SwampPosSeed), 0, - FloreSpacing / 2 + FloreSpacing * rand(SwampPosSeed)>;
	#local FinalPos = Position + RandPosNoise;
	#if( SeedValue < 0.65 )
		object{ CompleteReed scale ReedScaleValue translate FinalPos  }
	#else
		object{ HighGrassBlade scale GrassScaleValue translate FinalPos }
	#end
#end

#macro PlaceLineOfSwampFlore( FirstPos )
	#for( it_z , FirstPos.z , FirstPos.z - FloreSpacing * FlorePerLines, -FloreSpacing )
		#local FinalPos = < FirstPos.x, 1.78, it_z > ;
		PlaceSwampFloreSample( rand(SwampFloreChoice), FinalPos, rand(GrassBlScale) * 0.25 + 0.75, rand(ReedScale) * 0.25 + 0.75)
	#end 
#end

#macro PlaceSwampFlore()
	#debug "Placing river bank elements: grass blade and reeds.\n"
	//For loop from -39 to 39 ideally
	#for( ItX , -79, 79, FloreSpacing )
		#local FirstFlorePosition = DetectCoordSwampAlongRim( ItX );
		#if( FirstFlorePosition.z > -60 & FirstFlorePosition.y > 1.8 & FirstFlorePosition.y < 3.2 )
			PlaceLineOfSwampFlore( FirstFlorePosition )
		#end
	#end
#end
