//You must include 'parpaing.inc' before including this file.

#include "woods.inc"

#declare WareH_Width_ParpNum = 12;		//Build parpaings on x axis
#declare WareH_Length_ParpNum = 30;		//Build parpaings on z axis
#declare WareH_Height_ParpNum = 20;
#declare ParpaingBox = max_extent( ParpaingWMortar ) - min_extent( ParpaingWMortar );

#declare WarehouseMinCorner = < 4, 0, 4 > ;
#declare WarehouseMaxCorner = WarehouseMinCorner + < 	ParpaingBox.z * WareH_Width_ParpNum, 
														ParpaingBox.y * WareH_Height_ParpNum, 
														ParpaingBox.z * WareH_Length_ParpNum > ;
#declare WarehouseWidth = ParpaingBox.z * WareH_Width_ParpNum;
#declare WarehouseLength = ParpaingBox.z * WareH_Length_ParpNum + ParpaingBox.x;
#declare WarehouseHighness = WarehouseMaxCorner.y + ParpaingBox.y;

#declare SouthWestCam = camera{ location < -30, 22, 0 > look_at < 4, 21, 4 > }
#declare SouthEastCam = camera{ location < 4 + WarehouseWidth + 30, 8, -30 > look_at < 4 + WarehouseWidth, 7, 0> }
#declare NorthWestCam = camera{ location < -30, 22, 4 + WarehouseLength + 30 > look_at  < 4, 21, 4 + WarehouseLength > }
#declare NorthEastCam = camera{ location < 4 + WarehouseWidth + 30, 8, 4 + WarehouseLength + 30 > 
								look_at < 4 + WarehouseWidth, 7, 4 + WarehouseLength > }

#declare WoodBoardWidth = 0.5;
#declare WoodBoardDepth = 0.1;
#declare WoodBoardColor = color srgb < 99 / 255, 59 / 255, 37 / 255 >;

#declare RealRoofColor = color srgb < 221 / 255, 211 / 255, 167 / 255 >;

#declare TileNormal = normal{ gradient x 2 
										slope_map { 
											[0 <0, 0>] 
											[.01 <0.5, 1>] 
											[.05 <1, 0>] 
											[.95 <1, 0>] 
											[.99 <0.5, -1>]
											[1 <0, 0>] 
										}
						}
                             
#declare FinalTileNormal = normal{ average normal_map{ 
													[1 TileNormal]
													[1 TileNormal rotate 90*z]
								}
							}   
#declare RealRoofTexture = texture{ uv_mapping
								T_Wood7 rotate 90 * x
								normal{ FinalTileNormal }
							}

#declare WoodBoardTextureMoveSeed = seed( 89123 );
#declare WoodBoardTexture = texture { T_Wood7 rotate 90 * x scale 1 }

#macro MakeBoardTexture( TextureMoveSeed, DecalNum )
	texture{ WoodBoardTexture translate < 	rand( TextureMoveSeed ) * DecalNum - DecalNum / 2, 
											rand( TextureMoveSeed ) * DecalNum - DecalNum / 2, 
											rand( TextureMoveSeed ) * DecalNum - DecalNum / 2 > }
#end

#declare WarehouseWindowWidth = 5;
#declare WarehouseWindowHeight = 2.5;
#declare WarehouseWindowCylinderColor = color srgb 1;
#declare WarehouseWindowGlassColor = color srgb < 236 / 255, 231 / 255, 143 / 255 > transmit 0.35 filter 0.35;

#macro MakeParpWidthLine(StartPos, ParpNum)
	#for( It, 0, ParpNum - 1 )
		object{ ParpaingWMortar 
				translate -min_extent( ParpaingWMortar ) 
				rotate 90 * y 
				translate ParpaingBox.x * z 
				translate StartPos + It * ParpaingBox.z * x 
		}
	#end
#end

#macro MakeParpWidthWall(StartPos, Interval)
	#local BaseInterval = Interval;
	#for( ItY, 0, WareH_Height_ParpNum )
		MakeParpWidthLine(StartPos + < BaseInterval, ItY * ParpaingBox.y + MortarHeight, 0 >, WareH_Width_ParpNum )
		#if( BaseInterval = 0 )
			#local BaseInterval = ParpaingBox.x;
		#else
			#local BaseInterval = 0;
		#end
	#end
#end

#macro MakeParpLengthLine(StartPos, ParpNum)
	#for( It, 0, ParpNum - 1 )
		object{ ParpaingWMortar
			translate -min_extent( ParpaingWMortar )
			translate StartPos + It * ParpaingBox.z * z
		}
	#end
#end

#macro MakeParpLengthWall(StartPos, Interval, StartHeightNum, EndHeightNum)
	#local BaseInterval = Interval;
	#for( ItY, StartHeightNum, EndHeightNum )
		MakeParpLengthLine(StartPos + < 0, ItY * ParpaingBox.y + MortarHeight, BaseInterval >, WareH_Length_ParpNum )
		#if( BaseInterval = 0 )
			#local BaseInterval = ParpaingBox.x;
		#else
			#local BaseInterval = 0;
		#end
	#end
#end

#declare RoofPtA = < 0, WarehouseLength - ParpaingBox.x, 0 >;
#declare RoofPtB = < WarehouseWidth + ParpaingBox.x, WarehouseLength - ParpaingBox.x, 0 >;
#declare RoofPtC = < WarehouseWidth + ParpaingBox.x, ParpaingBox.x, 0 >;
#declare RoofPtD = < 0, ParpaingBox.x, 0 >;
#declare RoofPtE = < ( WarehouseWidth + ParpaingBox.x ) / 2, WarehouseLength - ParpaingBox.x, -WarehouseWidth / 4 >;
#declare RoofPtF = < ( WarehouseWidth + ParpaingBox.x ) / 2, ParpaingBox.x, -WarehouseWidth / 4 >;

#declare X_Uv_Size = 40;
#declare Y_Uv_Size = 8;

#declare RealWoodMeshRoof =
mesh{
	triangle{ RoofPtA, RoofPtB, RoofPtE uv_vectors < 1, 0 >, < -1, 0 >, < 0, 1 > } //Side triangle
	triangle{ RoofPtD, RoofPtC, RoofPtF uv_vectors < 1, 0 >, < -1, 0 >, < 0, 1 > } //Side triangle
	triangle{ RoofPtE, RoofPtB, RoofPtC uv_vectors < -X_Uv_Size / 2, 0 >, < -X_Uv_Size / 2, Y_Uv_Size >, < X_Uv_Size / 2, Y_Uv_Size > } //East triangle
	triangle{ RoofPtE, RoofPtC, RoofPtF uv_vectors < -X_Uv_Size / 2, 0 >, < X_Uv_Size / 2, Y_Uv_Size >, < X_Uv_Size / 2, 0 > } //East triangle
	triangle{ RoofPtA, RoofPtE, RoofPtF uv_vectors < X_Uv_Size / 2, Y_Uv_Size >, < X_Uv_Size / 2, 0 >, < -X_Uv_Size / 2, 0 > } //West triangle
	triangle{ RoofPtA, RoofPtF, RoofPtD uv_vectors < X_Uv_Size / 2, Y_Uv_Size >, < -X_Uv_Size / 2, 0 >, < -X_Uv_Size / 2, Y_Uv_Size > } //West triangle
	triangle{ RoofPtA, RoofPtB, RoofPtC }
	triangle{ RoofPtA, RoofPtC, RoofPtD }
	rotate 90 * x
	translate ( WarehouseHighness + MortarHeight ) * y translate WarehouseMinCorner
}

#declare RealWoodRoof =
prism{
	linear_sweep linear_spline 
	ParpaingBox.x, WarehouseLength - ParpaingBox.x, 4
	< 0, 0 >,
	< ( WarehouseWidth + ParpaingBox.x ) / 2, -WarehouseWidth / 4 >, 
	< WarehouseWidth + ParpaingBox.x , 0 >, 
	< 0, 0 >
	rotate 90 * x
	translate ( WarehouseHighness + MortarHeight ) * y translate WarehouseMinCorner
	texture{ RealRoofTexture }
}

#declare IntersectionRoofBase =
prism{
	linear_sweep linear_spline
	0, WarehouseLength, 4, 
	< 0, 0 > , 
	< ( WarehouseWidth + ParpaingBox.x ) / 2, -WarehouseWidth / 4 >, 
	< ( WarehouseWidth + ParpaingBox.x ), 0 >, 
	< 0, 0 >
	rotate 90 * x 
	translate ( WarehouseHighness + MortarHeight ) * y translate WarehouseMinCorner
	MakeBoardTexture( WoodBoardTextureMoveSeed, 20 )
}

#declare WoodenRoofBoard = 
prism{
	linear_sweep linear_spline
	0, WarehouseWidth / 2, 7, 
	< 0, 0>, 
	< 0, ParpaingBox.x >,
	< WoodBoardWidth, ParpaingBox.x >,
	< WoodBoardWidth, 0 >, 
	< WoodBoardWidth - WoodBoardDepth, -WoodBoardDepth >, 
	< WoodBoardDepth, -WoodBoardDepth >, 
	< 0, 0 > 
	
}

#declare SouthWoodBoards =
intersection{
	object{ IntersectionRoofBase }
	union{
		#for( It, 0, WarehouseWidth / WoodBoardDepth )
			object{ WoodenRoofBoard 
					translate WarehouseMinCorner 
							+ WarehouseHighness * y 
							+ It * WoodBoardWidth * x 
							+ < 0, 0, WoodBoardDepth * 1.01>
							+ MortarHeight * y
				MakeBoardTexture( WoodBoardTextureMoveSeed, 20 )
			}
		#end
	}
}

#declare NorthWoodBoards =
intersection{
	object{ IntersectionRoofBase }
	union{
		#for( It, 0, WarehouseWidth / WoodBoardDepth )
			object{ WoodenRoofBoard rotate 180 * y 
					translate < WoodBoardWidth, 0, 0 > 
								+ WarehouseMinCorner 
								+ < 0, 0, WarehouseLength > 
								+ WarehouseHighness * y 
								+ It * WoodBoardWidth * x 
								+ < 0, 0, -WoodBoardDepth * 1.01 > 
								+ MortarHeight * y
				MakeBoardTexture( WoodBoardTextureMoveSeed, 20 )
			}
		#end
	}
}



#declare WindowRemoveBox = box{ < -ParpaingBox.x, 0, -WarehouseWindowWidth / 2 >,< ParpaingBox.x, WarehouseWindowHeight, WarehouseWindowWidth / 2 > texture{ MortarTexture } }

#declare WarehouseGlassWindow =
union{
	#for( It, 0, WarehouseWindowWidth - 1 )
		cylinder{ < 0, 0, 0 >, < 0, WarehouseWindowHeight, 0 >, 0.1 translate It * z pigment{ WarehouseWindowCylinderColor } }
		polygon{ 5 < 0, 0 >, < 0, WarehouseWindowHeight >, < 1, WarehouseWindowHeight >, < 1, 0 >, < 0, 0 > 
			rotate 90 * y 
			translate < 0, 0, It + 1 >
			texture{ pigment{ WarehouseWindowGlassColor } } 
		}
	#end
	cylinder{ 0, < 0, WarehouseWindowHeight, 0 >, 0.1 translate 5 * z pigment{ WarehouseWindowCylinderColor } }
}

#declare WarehouseWindowPos = array[8]{
									< WarehouseMinCorner.x, WarehouseHighness - WarehouseWindowHeight * 1.25, WarehouseMinCorner.z + WarehouseWindowWidth * 1.25 >,
									< WarehouseMinCorner.x, WarehouseHighness - WarehouseWindowHeight * 1.25, WarehouseMinCorner.z + WarehouseWindowWidth * 5.25 >,
									< WarehouseMinCorner.x, WarehouseHighness - WarehouseWindowHeight * 1.25, WarehouseMaxCorner.z - WarehouseWindowWidth * 1.25 >,
									< WarehouseMinCorner.x, WarehouseHighness - WarehouseWindowHeight * 1.25, WarehouseMaxCorner.z - WarehouseWindowWidth * 5.25 >,
									< WarehouseMaxCorner.x, WarehouseHighness - WarehouseWindowHeight * 1.25, WarehouseMinCorner.z + WarehouseWindowWidth * 1.25 >,
									< WarehouseMaxCorner.x, WarehouseHighness - WarehouseWindowHeight * 1.25, WarehouseMinCorner.z + WarehouseWindowWidth * 5.25 >,
									< WarehouseMaxCorner.x, WarehouseHighness - WarehouseWindowHeight * 1.25, WarehouseMaxCorner.z - WarehouseWindowWidth * 1.25 >
									< WarehouseMaxCorner.x, WarehouseHighness - WarehouseWindowHeight * 1.25, WarehouseMaxCorner.z - WarehouseWindowWidth * 5.25 >
								}

#declare WarehouseStructure = 
union{
	MakeParpWidthWall(WarehouseMinCorner, 0)
	MakeParpWidthWall( < WarehouseMinCorner.x, 0, WarehouseMaxCorner.z >, ParpaingBox.x )
	MakeParpLengthWall(WarehouseMinCorner, ParpaingBox.x, 0, WareH_Height_ParpNum - 5 )
	difference{
		union{
			MakeParpLengthWall(WarehouseMinCorner, ParpaingBox.x, WareH_Height_ParpNum - 4, WareH_Height_ParpNum )
		}
		object{ WindowRemoveBox translate WarehouseWindowPos[0] }
		object{ WindowRemoveBox translate WarehouseWindowPos[1] }
		object{ WindowRemoveBox translate WarehouseWindowPos[2] }
		object{ WindowRemoveBox translate WarehouseWindowPos[3] }
	}
	object{ WarehouseGlassWindow translate WarehouseWindowPos[0] + < ParpaingBox.x / 2, 0, -WarehouseWindowWidth / 2 > }
	object{ WarehouseGlassWindow translate WarehouseWindowPos[1] + < ParpaingBox.x / 2, 0, -WarehouseWindowWidth / 2 > }
	object{ WarehouseGlassWindow translate WarehouseWindowPos[2] + < ParpaingBox.x / 2, 0, -WarehouseWindowWidth / 2 > }
	object{ WarehouseGlassWindow translate WarehouseWindowPos[3] + < ParpaingBox.x / 2, 0, -WarehouseWindowWidth / 2 > }
	MakeParpLengthWall( < WarehouseMaxCorner.x, 0, WarehouseMinCorner.z >, 0, 0, WareH_Height_ParpNum - 5 )
	difference{
		union{
			MakeParpLengthWall( < WarehouseMaxCorner.x, 0, WarehouseMinCorner.z >, 0, WareH_Height_ParpNum - 4, WareH_Height_ParpNum )
		}
		object{ WindowRemoveBox translate WarehouseWindowPos[4] }
		object{ WindowRemoveBox translate WarehouseWindowPos[5] }
		object{ WindowRemoveBox translate WarehouseWindowPos[6] }
		object{ WindowRemoveBox translate WarehouseWindowPos[7] }
	}
	object{ WarehouseGlassWindow translate WarehouseWindowPos[4] + < ParpaingBox.x / 2, 0, -WarehouseWindowWidth / 2 > }
	object{ WarehouseGlassWindow translate WarehouseWindowPos[5] + < ParpaingBox.x / 2, 0, -WarehouseWindowWidth / 2 > }
	object{ WarehouseGlassWindow translate WarehouseWindowPos[6] + < ParpaingBox.x / 2, 0, -WarehouseWindowWidth / 2 > }
	object{ WarehouseGlassWindow translate WarehouseWindowPos[7] + < ParpaingBox.x / 2, 0, -WarehouseWindowWidth / 2 > }
	object{ SouthWoodBoards }
	object{ NorthWoodBoards }
	object{ RealWoodMeshRoof texture{ RealRoofTexture } }
}

#declare WarehouseWithWindows =	
union{
	object{ WarehouseStructure }
	
}

#declare CompleteWarehouse = object{ WarehouseWithWindows }

#declare WarehouseSize = max_extent( CompleteWarehouse ) - min_extent( CompleteWarehouse );

#debug concat( "Size of warehouse: ", vstr(3, WarehouseSize, ",", 10, 4 ), "\n" )
#debug concat( "Warehouse from min: ", vstr(3, min_extent( CompleteWarehouse ), ",", 10, 4 ), "\n" )
#debug concat( "Warehouse to max: ", vstr(3, max_extent( CompleteWarehouse ), ",", 10, 4 ), "\n" )