Hi all,
Some terrain generation code. Needs messing around with, but might be usefull for making some terrains for the new plugin.
sync on
sync rate 0
seed=timer()
dim hpointx(100)
dim hpointy(100)
dim hpointz(100)
dim hpoints#(100)
dim vanmap#(256,256)
dim vanmapbonus(256,256)
dim vanmaptext(256,256)
dim vannormx#(256,256)
dim vannormy#(256,256)
dim vannormz#(256,256)
dim vantrans(256)
set window on
get image 2,0,0,640,480
`gosub generate_stoked
gosub generate_random
gosub generate_paths
gosub generate_smooth
gosub generate_normals
gosub draw
if file exist("terrain.vn")=1 then delete file "terrain.vn"
open to write 1,"terrain.vn"
for y=0 to 255 step 2
for x=0 to 255 step 2
write float 1,vanmap#(x,y)
next x
next y
close file 1
do
paste image 1,0,0,1
sync
loop
generate_random:
randomize seed
for y=0 to 255
for x=0 to 255
vanmap#(x,y)=-1
next x
next y
chunk=128
vanmap#(0,0)=rnd(2000)/100.00
vanmap#(255,0)=rnd(2000)/100.00
vanmap#(0,255)=rnd(2000)/100.00
vanmap#(255,255)=rnd(2000)/100.00
vanmap#(127,0)=rnd(2000)/100.00
vanmap#(127,127)=rnd(2000)/100.00
vanmap#(127,255)=rnd(2000)/100.00
vanmap#(0,127)=rnd(2000)/100.00
vanmap#(127,127)=rnd(2000)/100.00
vanmap#(255,127)=rnd(2000)/100.00
while chunk>1
for y=0 to 255 step chunk
for x=0 to 255 step chunk
if chkvanmap#(x,y)=-1
h#=((chkvanmap#(x+chunk,y)+chkvanmap#(x,y+chunk)+chkvanmap#(x-chunk,y)+chkvanmap#(x+chunk,y))/4.00)+(rnd(chunk)*1.000)
vanmap#(x,y)=h#
endif
next x
next y
chunk=chunk/2
endwhile
gosub generate_smooth
gosub generate_smooth
return
generate_stoked:
hpointx(0)=128
hpointy(0)=500
hpointz(0)=128
randomize timer()
for n=1 to 10
hpointx(n)=rnd(255)
hpointy(n)=rnd(300)
hpointz(n)=rnd(255)
hpoints#(n)=rnd(32)+32
next n
ink 255,0
for y=0 to 255
for x=0 to 255
th#=0
for n=0 to 10
dx#=hpointx(n)-x
dz#=hpointz(n)-y
dist#=sqrt((dx#*dx#)+(dz#*dz#))
if dist#<0 then dist#=0-dist#
th#=th#+(128-dist#)+hpointy(n)
next n
h#=th#/11
vanmap#(x,y)=(h#*5)
next x
next y
for nn=0 to 10
rad#=rnd(40)
jmpx=rnd(255)
jmpy=rnd(255)
for y=0 to 255
for x=0 to 255
range=1
th#=0
dx#=jmpx-x
dz#=jmpy-y
dist#=sqrt((dx#*dx#)+(dz#*dz#))
if dist#<0 then dist#=0-dist#
if dist#<rad
h#=(dist#/rad#)*rad#
vanmap#(x,y)=vanmap#(x,y)+h#
endif
next x
next y
next nn
for y=0 to 255
for x=0 to 255
h#=rnd(200)
vanmap#(x,y)=vanmap#(x,y)+(h#/100.000)
vanmapbonus(x,y)=20
vanmaptext(x,y)=20
next x
next y
return
generate_paths:
lanes=100+rnd(100)
for n=0 to lanes
trx#=rnd(255.00)
trz#=rnd(255.00)
try#=0
trlife=rnd(128)
trdeep=20+rnd(15)
while trlife>0
dec trlife,1
if try#<trdeep then inc try#,1
if trlife<trdeep then try#=trlife
tra#=wrapvalue(tra#+rnd(40)-20)
trx#=newxvalue(trx#,tra#,2)
trz#=newzvalue(trz#,tra#,2)
maptrx=trx#
maptrz=trz#
if maptrx>0 and maptrx<255 and maptrz>0 and maptrz<255
trh#=vanmap#(maptrx,maptrz)
for ofz=0 to 1
for ofx=0 to 1
vanmap#(maptrx-ofx,maptrz-ofz)=trh#-try#
vanmapbonus(maptrx-ofx,maptrz-ofz)=5
vanmaptext(x,y)=5
next ofx
next ofy
else
trlife=-1
endif
endwhile
next n
return
generate_smooth:
for pass=1 to 3
for y=1 to 254
for x=1 to 254
vanmap#(x,y)=(vanmap#(x-1,y)+vanmap#(x+1,y)+vanmap#(x,y-1)+vanmap#(x,y+1)+vanmap#(x-1,y-1)+vanmap#(x+1,y+1)+vanmap#(x+1,y-1)+vanmap#(x-1,y+1)+vanmap#(x,y))/9
next x
next y
next pass
return
generate_normals:
for z=1 to 255
for x=1 to 255
rem Get matrix heights
h8#=vanmap#(x,z-1)
h4#=vanmap#(x-1,z)
h#=vanmap#(x,z)
h2#=vanmap#(x,z)
rem Calculate projected angle X using heights
x1#=(x-1)*25.0 : y1#=h#
x2#=(x+0)*25.0 : y2#=h4#
dx#=x2#-x1#
dy#=y2#-y1#
ax#=atanfull(dx#,dy#)
ax#=wrapvalue(90-ax#)
rem Calculate projected angle Z using heights
z1#=(z-1)*25.0 : y1#=h2#
z2#=(z+0)*25.0 : y2#=h8#
dz#=z2#-z1#
dy#=y2#-y1#
az#=atanfull(dz#,dy#)
az#=wrapvalue(90-az#)
rem Make normal from projected angle
vannormx#(x,z)=sin(ax#)
vannormy#(x,z)=cos(ax#)
vannormz#(x,z)=sin(az#)
next x
next z
return
draw:
if bitmap exist(1)=1 then delete bitmap 1
create bitmap 1,640,480
set current bitmap 1
paste image 2,0,0
tlim#=-100000
blim#=100000
for y=0 to 255
for x=0 to 255
if vanmap#(x,y)>tlim# then tlim#=vanmap#(x,y)
if vanmap#(x,y)<blim# then blim#=vanmap#(x,y)
next x
next y
for y=0 to 255
for x=0 to 255
xx=((sin(60)*y)+(sin(120)*x)*0.75)+256
yy=((cos(60)*y)+(cos(120)*x)*0.75)+256
col#=((vanmap#(x,y)-blim#)/tlim#)*255.0
ink rgb(col#,col#,col#),rgb(col#,col#,col#)
dot x,y
col2#=col#-((vannormx#(x,y)+vannormx#(x,y)+vannormx#(x,y))/3.00)
if col2#<0 then col2#=0
if col2#>255 then col2#=255
ink rgb(col2#*0.9,col2#*0.9,col2#*0.9),rgb(col2#*0.9,col2#*0.9,col2#*0.9)
box xx,yy-col#,xx,yy
ink rgb(col2#,col2#,col2#),rgb(col2#,col2#,col2#)
dot xx,yy-col#
next x
next y
get image 1,0,0,640,480
set current bitmap 0
return
function chkvanmap#(x,y)
if x<0 then inc x,255
if y<0 then inc y,255
if x>255 then dec x,255
if y>255 then dec y,255
chkvanmap#=vanmap#(x,y)
endfunction chkvanmap#
Van-B
Muhahahahaha.