/* Random Terrain Generator By The Ugly Using the Diamond-Square Algorithm (aka Random Midpoint Displacement Algorithm) refferences used: http://www.gameprogrammer.com/fractal.html */ //THE TILENUM MACRO #macro tilenum(num,tile) #while (num<0 | num>tile) #if(num>tile) #declare num=(num-tile); #end #if(num<0) #declare num=(num+tile); #end #end num #end //THE DIAMOND STEP MACRO #macro Diamond(Sub_Size,Point_X,Point_Z) #declare Array[Point_X][Point_Z]=( (Array[tilenum(Point_X-(Sub_Size/2), Size)][tilenum(Point_Z-(Sub_Size/2), Size)])+ (Array[tilenum(Point_X+(Sub_Size/2), Size)][tilenum(Point_Z-(Sub_Size/2), Size)])+ (Array[tilenum(Point_X+(Sub_Size/2), Size)][tilenum(Point_Z+(Sub_Size/2), Size)])+ (Array[tilenum(Point_X-(Sub_Size/2), Size)][tilenum(Point_Z+(Sub_Size/2), Size)]) ) /4+ (rand(Rand)*(RandVal*2)-RandVal); #end //THE SQUARE SET MACRO #macro Square(Sub_Size,Point_X,Point_Z) #declare Array[Point_X][Point_Z]=( (Array[Point_X][tilenum(Point_Z-(Sub_Size/2), Size)])+ (Array[tilenum(Point_X+(Sub_Size/2), Size)][Point_Z])+ (Array[Point_X][tilenum(Point_Z+(Sub_Size/2), Size)])+ (Array[tilenum(Point_X-(Sub_Size/2), Size)][Point_Z]) ) /4+ (rand(Rand)*(RandVal*2)-RandVal); #end //THE MAIN LOOP #macro MainLoop(Sub_Size) //Do the Diamond Step for the center point in the Sub_Size area #declare N1=0; #declare N2=0; #while (N2=Size) #declare N1=0; #declare N2=N2+Sub_Size; #end #end //Do the Squere Step for the left and upper points in the Sub_Size area #declare N1=0; #declare N2=0; #while (N2=Size) #declare N1=0; #declare N2=N2+Sub_Size; #end #end //Copy all the top values to the bottom and all left values to the right #declare N=0; #while(N<=Size) #declare Array[Size][N]=Array[0][N]; #declare Array[N][Size]=Array[N][0]; #declare N=N+Sub_Size/2; #end //Set the correct values before relooping #declare RandVal=RandVal*pow(2,RandReduction*-1); #declare Iteration_val=Iteration_val+1; #if(Iteration_val<=Iterations) MainLoop(Sub_Size/2) #end //end of MainLoop #end //INIT MACRO #macro FractalTerrain(Seed,Iterations,RandReduction) #declare Size=pow(2,Iterations); #declare Array=array[Size+1][Size+1] #declare Array[0][0]=1; #declare Array[0][Size]=0; #declare Array[Size][0]=0; #declare Array[Size][Size]=1; #declare Rand=seed(Seed); #declare RandVal=1; #declare RandReduction=RandReduction; #declare Iterations=Iterations; #declare Iteration_val=0; #if(Iteration_val<=Iterations) MainLoop(Size) #end #end //HEIGHTFIELD CREATION MACRO #macro CalcHF(Array, Size) #declare HF=mesh { //Calculate the triangles #declare N1=0; #declare N2=0; #while(N2, , } triangle { , , } #declare N1=N1+1; #if (N1>=Size) #declare N1=0; #declare N2=N2+1; #end #end //Calculate Min and Max points in the array #declare N1=0; #declare N2=0; #declare Array_Max=-1; #declare Array_Min=1; #while(N2<=Size) #if(Array[N1][N2]Array_Max) #declare Array_Max=Array[N1][N2]; #end #declare N1=N1+1; #if (N1>=Size) #declare N1=0; #declare N2=N2+1; #end #end //scale so that: //max point is at pos y=1 //min point is at pos y=0 //right point is at pos x=0.5 //left point is at pos x=-0.5 //the finnished HF has a "boundingbox" of 1*1*1 units scale <1/Size, 1, 1/Size> translate <-0.5,Array_Min*-1,-0.5> scale <1,1/(Array_Max-Array_Min),1> } #end /*///////////////////////////////// TESTING CODE BEGINS HERE *////////////////////////////////// // +o.\Anim\part_ +KI0.0 +KF1.0 +KFI0 +KFF101 +SF1 +EF101 -w320 -h320 // +o.\Anim\part_ +KI0.0 +KF1.0 +KFI0 +KFF11 +SF1 +EF11 -w320 -h320 //-w320 -h320 #declare Image_Size=120; #declare Image_Width=Image_Size; #declare Image_Height=Image_Size; #declare Tile=2; #declare Color_Scheme=0; //0=GrayScale 1=Rainbow #declare Camera_Type=0; //0=Orthographic_Overview 1=Circling_Perspective #declare Show_Triangles=0; #declare Smooth_Colors=1; #declare Iterations=6; #declare RandSeed=1; #declare Smoothness=1; #declare Size=pow(2,Iterations); FractalTerrain(RandSeed,Iterations,Smoothness) CalcHF(Array,Size) //GOOD ITERATIONS //This thing calculates how many Iterations you should have to get each square smaller than a pixel (with Camera_Type 0) #declare N=0; #while (pow(2,N)/Tile] [1/6*1 rgb <1,1,0>] [1/6*2 rgb <0,1,0>] [1/6*3 rgb <0,1,1>] [1/6*4 rgb <0,0,1>] [1/6*5 rgb <1,0,1>] [1/6*6 rgb <1,0,0>] } } #break #end #if(Smooth_Colors) finish { ambient 1 } #end } #switch (Camera_Type) #case(0) camera { orthographic location <0, 4, 0> look_at <0,2,0> up y*1 right x*1 } #break #case(1) camera { location look_at 0 } #break #end #if(Show_Triangles) light_source { <0, 4, 0> rgb 1 } #end