home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The CDPD Public Domain Collection for CDTV 4
/
CDPD_IV.bin
/
e
/
mailinglists
/
binaries
/
map.lha
/
map.e
Wrap
Text File
|
1993-08-10
|
4KB
|
175 lines
/*
MapGenerator 1.0 was written by Kai.Nikulainen@utu.fi. Any comments
are welcome. (Except the negative ones concerning my programming-style:)
*/
MODULE 'intuition/intuition'
CONST HEI=128,WID=128,BUFSIZE=3*GADGETSIZE
/* These are minimum and maximum values that can be in the map-array.
If you change MIN<0 or MAX>15, you must change the draw-procedure,
because it uses the map-array's values as color numbers
*/
CONST MINI=0,MAXI=15
ENUM QUIT,SMOOTH,START
DEF map,s,w,steps,buf[BUFSIZE]:ARRAY
PROC openAll()
DEF next
map:=New((HEI+1)*(WID+1)) /* This is a bit too big, just to be sure */
next:=Gadget(buf,NIL,START,0,20,186,80,'New map')
next:=Gadget(next,buf,SMOOTH,0,120,186,80,'Smooth')
next:=Gadget(next,buf,QUIT,0,220,186,80,'Quit')
s:=OpenS(320,200,4,0,' MapGenerator 1.0')
w:=OpenW(0,0,320,200,IDCMP_GADGETUP,WFLG_BACKDROP+WFLG_BORDERLESS,'',s,15,buf)
LoadRGB4(ViewPortAddress(w),[$03b,$cb9,$b97,$8a3,$792,$681,$571,$460,$450,
$540,$430,$410,$400,$666,$999,$ccc]:INT,16)
ENDPROC
PROC init()
DEF i
msg('Initializing')
/* You might want to change VbeamPos to some other function that has more
possible values */
Rnd(-VbeamPos())
FOR i:=0 TO (1+HEI)*(WID+1)-1 DO map[i]:=8
ENDPROC
PROC end()
CloseW(w)
CloseS(s)
CleanUp(0)
ENDPROC
PROC msg(s)
Box(0,153,310,165,0)
Colour(1,0)
TextF(120,160,'\s',s)
ENDPROC
/* Smooth calculates the average of 5 pixels and uses that as a new value.
You might want smooth a map several times to get more lakes and rivers or
to lower the mountains. */
PROC smooth()
DEF i,j,s
msg('Smoothing..')
FOR i:=1 TO WID-2
FOR j:=1 TO HEI-2
s:=i*WID+j
map[s]:=(map[s-1]+map[s+1]+map[s-HEI]+map[s+HEI]+map[s]+2)/5
IF map[s]>MAXI THEN map[s]:=MAXI
ENDFOR
ENDFOR
FOR i:=1 TO WID-2
map[i*WID]:=map[i*WID+1]
map[i*WID+HEI-1]:=map[i*WID+HEI-2]
ENDFOR
FOR i:=0 TO HEI-1
map[i]:=map[i+WID]
map[WID*(WID-1)+i]:=map[WID*(WID-2)+i]
ENDFOR
ENDPROC
/* change divides the map in small equally sized squares. Inside a square
to each value d is added. d is -1,0 or 1. */
PROC change()
DEF dx,dy,i,j,x,y=0,a,b,d,k
msg('Creating...')
dx:=WID/steps
dy:=HEI/steps
IF dx<1 THEN dx:=1
IF dy<1 THEN dy:=1
FOR i:=1 TO steps
x:=0
FOR j:=1 TO steps
d:=Rnd(3)-1
FOR a:=x TO x+dx-1
FOR b:=y TO y+dy-1
k:=map[a*WID+b]+d
IF And(k>=MINI,k<=MAXI) THEN map[a*WID+b]:=k
ENDFOR
ENDFOR
x:=x+dx
ENDFOR
y:=y+dy
ENDFOR
steps:=steps*2
ENDPROC
/* change2 uses larger squares than change and the squares are placed randomly.
This means they can be partially overlapping. The value of d is between
-steps/2 and steps/2. */
PROC change2()
DEF x,y,i,j,d,c,a
msg('Creating...')
/* changing the following multiplyer also changes the maps quite a lot */
FOR c:=1 TO steps*3
x:=Rnd(WID)
y:=Rnd(HEI)
d:=Rnd(steps+1)-(steps/2)
FOR i:=x-(WID/steps) TO x+(WID/steps)
FOR j:=y-(HEI/steps) TO y+(HEI/steps)
IF And(And(i>=MINI,j>=MINI),And(i<WID,j<HEI))
a:=map[i*WID+j]+d
IF And(a>=MINI,a<=MAXI) THEN map[i*WID+j]:=a
ENDIF
ENDFOR
ENDFOR
ENDFOR
steps:=steps*2
ENDPROC
PROC draw()
DEF i,j
msg('Plotting...')
FOR i:=0 TO WID-1
FOR j:=0 TO HEI-1
Plot(i+96,j+15,map[i*WID+j])
ENDFOR
ENDFOR
ENDPROC
PROC main()
DEF i,class=NIL,adr:PTR TO gadget,usrdat
openAll()
REPEAT
init()
steps:=2
FOR i:=1 TO 7
/* Change the bordervalue and the maps may change a lot */
IF i<3 THEN change2() ELSE change()
draw()
ENDFOR
REPEAT
msg('What next?')
WHILE class=NIL
class:=WaitIMessage(w)
adr:=MsgIaddr()
SELECT class
CASE IDCMP_GADGETUP
usrdat:=adr.userdata
DEFAULT
class:=NIL
ENDSELECT
ENDWHILE
SELECT usrdat
CASE SMOOTH
smooth()
draw()
CASE START
CASE QUIT
DEFAULT
usrdat:=666
ENDSELECT
class:=NIL
UNTIL Or(usrdat=QUIT,usrdat=START)
UNTIL usrdat=QUIT
end()
ENDPROC