|
|
Hi. Im trying to code a macro that creates a heightfield uning some sort of
recursive algorithm. I've searched the internet for a suitable algorithm
that I could convert to SDL (suitable=simple enough for me to understand).
A came across something named the Diamond-Square Algorithm (aka Midpoint
Displacement Algorithm). I got the general idea of the algorithm and
started to convert it to the SDL. I now have a file containing the code,
its still not finished, it is very inefficient, but I think the main things
are done.
The problem is...it doesnt work.
Here's the code:
//BEGIN CODE
#macro tilenum(num)
#while (num<0 & num>Size)
#if(num>Size)
(num-Size)
#end
#if(num<Size)
(num+Size)
#end
#end
#end
#macro Diamond(Sub_Size,Point_X,Point_Z)
#declare Array[Point_X][Point_Z]=(
(Array[tilenum(Point_X-(Sub_Size/2))][tilenum(Point_Z-(Sub_Size/2))])+
(Array[tilenum(Point_X+(Sub_Size/2))][tilenum(Point_Z-(Sub_Size/2))])+
(Array[tilenum(Point_X+(Sub_Size/2))][tilenum(Point_Z+(Sub_Size/2))])+
(Array[tilenum(Point_X-(Sub_Size/2))][tilenum(Point_Z+(Sub_Size/2))])
)
/4+
(rand(Rand)*(RandVal*2)-RandVal);
#end
#macro Square(Sub_Size,Point_X,Point_Z)
#declare Array[Point_X][Point_Z]=(
(Array[Point_X][tilenum(Point_Z-(Sub_Size/2))])+
(Array[tilenum(Point_X+(Sub_Size/2))][Point_Z])+
(Array[Point_X][tilenum(Point_Z-(Sub_Size/2))])+
(Array[tilenum(Point_X-(Sub_Size/2))][Point_Z])
)
/4+
(rand(Rand)*(RandVal*2)-RandVal);
#end
#macro MainLoop(Sub_Size)
#declare N1=0;
#declare N2=0;
#while (N2<Size)
#declare D_X=N1+(Sub_Size/2);
#declare D_Z=N2+(Sub_Size/2);
Diamond(Sub_Size,D_X,D_Z)
Square(Sub_Size,D_X,D_Z-(Sub_Size/2))
Square(Sub_Size,D_X+(Sub_Size/2),D_Z)
Square(Sub_Size,D_X,D_Z+(Sub_Size/2))
Square(Sub_Size,D_X-(Sub_Size/2),D_Z)
#declare N1=N1+Sub_Size;
#if (N1>=Size)
#declare N1=0;
#declare N2=N2+Sub_Size;
#end
#end
#declare RandVal=RandVal/2;
#declare Int_Val=Int_Val+1;
#if(Int_Val<=Int)
MainLoop(Sub_Size/2)
#end
#end
#macro FractalTerrain(Size,BaseHeight,Seed,Int,RandVal)
#declare Array=array[Size+1][Size+1]
#declare Array[0][0]=BaseHeight;
#declare Array[0][Size]=BaseHeight;
#declare Array[Size][0]=BaseHeight;
#declare Array[Size][Size]=BaseHeight;
#declare Rand=seed(Seed);
#declare Size=Size;
#declare RandVal=RandVal;
#declare Int=0;
MainLoop(Size)
#end
FractalTerrain(4,1,1,2,1)
//END CODE
I get the error "Parse Error: Expected 'numeric expression', ] found
instead"
Cant you use a macro when reffering to arrays?
Any comment on this would be appreciated.
Oh, I almost forgot to thank Thorsten, Shay and Mike for the help with the
"function macro" (long time ago).
It turned out quite good, thanks.
/The Ugly
Post a reply to this message
|
|
|
|
> #macro tilenum(num)
>
> #while (num<0 & num>Size)
> #if(num>Size)
> (num-Size)
> #end
> #if(num<Size)
> (num+Size)
> #end
> #end
>
> #end
I guess the problem is in the above quoted macro. I've often found that it
is better to do it like this:
#macro tilenum(num)
#local Temp=num;
#while (Temp<0 & Temp>Size)
#if (Temp>Size) #local Temp=Temp-Size; #end
#if (Temp<0) #local Temp=Temp+Size; #end
#end
Temp
#end
This way, a local value get's returned. I'm not sure if your code is
altogether correct, as your running a whilte-loop and evaluating it by hand
would come up with something like
"(num-Size)(num-Size)..." or "(num+Size)(num+Size)..." depending if num is
smaller than 0 or larger than Size.
I haven't tested it, but the idea is generally to use a local variable and
return that instead of returning a value inside any kind of loop or if.
Regards,
Tim
--
"Tim Nikias v2.0"
Homepage: <http://www.nolights.de>
Post a reply to this message
|
|