set display mode 1280,1024,32
sync on:sync rate 30
hide mouse
 
gosub setup_crosshair
 
c_posX#=500
c_posY#=50
c_posZ#=500
 
make matrix 1,1500,1500,30,30
gosub setup_color
gosub setup_water
gosub setup_height
Normalise(1)
 
fog on
fog color rgb(181,219,221)
fog distance 1000
color backdrop rgb(181,219,221)
cfov#=3.14/2.905
`editor pointer
Make Object Sphere 100,25 : Color Object 100,RGB(255,0,0) : hide object 100
 
ink 0,0
 
`--------------------------
` MAIN LOOP
`--------------------------
do
   `------------------------------------------------
   ` Matrix Editor By Roddman (Hold CTRL & Press 1)
   ` X and Z variables are now eX and eZ, because x and z are being used in for loops
   `------------------------------------------------
   Set Cursor 1,1
   If c_rout=0 then Print "Press SPACE to Edit Map" else Print "Press RETURN to Save Map"
 
   `switching between edit and game mode is now combined with changing the camera type
   if c_rout=1
      Print "X  Pos :",eX
      Print "Z  Pos :",eZ
      Print "Height :",Height
      GoSub CT_Edit_Matrix
      If returnkey()=1 then c_rout=0 : REM CT_Save_Matrix(1) : `remove REM to save matrix data
   EndIf
 
   `----------------------------
   ` FOV effects by OBese87
   `----------------------------
   if c_posY# > -10 and switch=0 then set gamma 255,255,255:switch=1 : changefov#=3.14/2.905 : set camera FOV changefov#
   if c_posY# < -10 and switch=1 then set gamma 100,100,150:switch=0 : set camera FOV 2.4
 
   `Magnification
   if mousemovez() > mmz then changefov# = cfov#-0.1
   if mousemovez() < mmz then changefov# = cfov#+0.1
   mmz = mousemovez()
   if changefov# < 0.1 then changefov#=0.1
   if changefov# > 3.14/2.905 then changefov# = 3.14/2.905
   `apply magnification change if out of water
   if c_posY# > -10 and changefov#<>cfov#
      cfov# = changefov#
      set camera fov cfov#
   endif
   `----------------------------
 
   `Switch between edit and game mode
   if spacekey()=1 and sp=0
      c_rout = 1-c_rout
      `ENTER EDIT MODE
      if c_rout=1
         eX=0 : eZ=0
         c_posx#=-200 : c_posy#=400 : c_posz#=-200
         c_angX#=45 : c_angY#=45 : c_angZ#=0
         fog off
         show object 100
      else
         fog on
         hide object 100
      endif
      sp=1
   endif
   if spacekey()=0 then sp=0
 
   `cross-hair
   if c_rout=0 then paste image 3,screen width()/2-15,screen height()/2-15,1
 
   `call camera routines
   if c_rout = 0 then gosub c_routine0
   if c_rout = 1 then gosub c_routine1
 
   rotate camera wrapvalue(c_angX#),wrapvalue(c_angY#),wrapvalue(c_angZ#)
   position camera c_posX#,c_posY#,c_posZ#
sync
loop
 
`------------------------
` GOSUBS
`------------------------
setup_water:
   cls rgb(80,80,150)
   get image 2,0,0,41,41
 
   make matrix 2,1500,1500,1,1
   prepare matrix texture 2,2,1,1
   position matrix 2,0,-10,0
   ghost matrix on 2
return
 
`a heightmap, every number is multiplied by 25, giving a stylisch feel to the map
setup_height:
   for x = 0 to 30
      for z = 0 to 30
         read height
         set matrix height 1,x,z,height * 25
      next z
   next x
   update matrix 1
 
   data  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
   data  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
   data  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
   data  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
   data  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
 
   data  0, 0, 0, 0, 0, 0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
   data  0, 0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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
   data  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
   data  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
 
   data  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
return
 
`the block texture of the map
setup_color:
   ink rgb(142,171,135),0:box 0,0,40,40
   ink rgb(122,151,115),0:line 0,0,40,0:line 0,0,0,40:line 40,40,40,0:line 40,40,0,40
   get image 1,0,0,41,41
   prepare matrix texture 1,1,1,1
return
 
setup_crosshair:
   ink rgb(255,255,255),0
   line 14, 1,14, 9:line 15, 3,15,11
   line 28,14,20,14:line 26,15,18,15
   line 15,28,15,20:line 14,26,14,18
   line  1,15, 9,15:line  3,14,11,14
   get image 3,0,0,30,30
return
 
`camera routine 0: just like in a first person shooter; w,a,s,d = movement; shift = crouching and jumping(on release)
c_routine0:
   ` Refresh Old Values (For Slope)
   c_OldPosX#=Camera Position X()
   c_OldPosZ#=Camera Position Z()
 
   c_angYm# = c_angYm# + mousemoveX() / 5
   c_angXm# = c_angXm# + (mousemoveY() * (1-rotlimit)) / 5
   c_angY# = c_angY# + c_angYm#
   c_angX# = c_angX# + c_angXm#
   c_angYm# = c_angYm# * 0.7
   c_angXm# = c_angXm# * 0.5
   if c_angX#<-80 or c_angX#>80
      rotlimit=1
   endif
   if rotlimit=1
      if c_angX#<0 and mousemoveY()>0 then rotlimit=0
      if c_angX#>0 and mousemoveY()<0 then rotlimit=0
   endif
 
   c_posX# = newXvalue(c_posX#,c_angY#   ,(keystate(17)-keystate(31))*5/(k42+1)/((c_posY#<-10)+1))
   c_posX# = newXvalue(c_posX#,c_angY#-90,(keystate(30)-keystate(32))*5/(k42+1)/((c_posY#<-10)+1))
   c_posZ# = newZvalue(c_posZ#,c_angY#   ,(keystate(17)-keystate(31))*5/(k42+1)/((c_posY#<-10)+1))
   c_posZ# = newZvalue(c_posZ#,c_angY#-90,(keystate(30)-keystate(32))*5/(k42+1)/((c_posY#<-10)+1))
   if c_posX#<20 then c_posX#=20
   if c_posX#>1480 then c_posX#=1480
   if c_posZ#<20 then c_posZ#=20
   if c_posZ#>1480 then c_posZ#=1480
 
   c_velY# = c_velY# + 2
   c_posY# = c_posY# - c_velY#
   ggg#=get ground height(1,c_posX#,c_posZ#)
   if c_posY# <ggg#  + 10/(k42+1) then c_posY# = ggg# + 10/(k42+1) : c_velY# = 0
   if k42=1 and keystate(42)=0 and c_posY# = ggg# + 10/(k42+1) then c_velY#=-10
   k42=keystate(42)
return
 
`camera routine 1: left and right buttons control the camera
c_routine1:
   if mouseclick()=1
      c_posm# = c_posm# + 0 - mousemoveY() / 2
   endif
   if mouseclick()=2
      c_angYm# = c_angYm# + mousemoveX() / 5
      c_angXm# = c_angXm# + mousemoveY() / 5
   endif
   c_angY# = c_angY# + c_angYm#
   c_angX# = c_angX# + c_angXm#
   c_angYm# = c_angYm# * 0.5
   c_angXm# = c_angXm# * 0.5
 
   c_posX# = newXvalue(c_posX#,c_angY#,c_posm#)
   c_posY# = newYvalue(c_posY#,c_angX#,c_posm#)
   c_posZ# = newZvalue(c_posZ#,c_angY#,c_posm#)
   c_posm# = c_posm# * 0.7
return
 
CT_Edit_Matrix:
   `controls
   If UpKey()=1 then INC ez
   if DownKey()=1 then DEC eZ
   If RightKey()=1 then INC ex
   if LeftKey()=1 then DEC ex
   If ScanCode()=13 then inc Height,1 : `+
   If ScanCode()=12 then dec Height,1 : `-
 
   `Bind X and Z to matrix
   If eX < 0 then eX=0
   If eX > 30 then eX=30
   If eZ < 0 then eZ=0
   If eZ > 30 then eZ=30
rem   Height=get ground height(1,eX,eZ)
 
   `Update
   Set Matrix Height 1,eX,eZ,Height
   Update Matrix 1
   position object 100,eX*50,Height,eZ*50 : `matrix editor pointer
Return
 
`---------------------
` FUNCTIONS
`---------------------
`By Lee Bamber From DB Example - Adds shaded areas to matrix to give depth
Function Normalise(MatNum)
  For z=1 to 30
    For x=1 to 30
      h8#=get matrix height(MatNum,x,z-1)*5:`*5 added for even more depth
      h4#=get matrix height(MatNum,x-1,z)*5
      h#=get matrix height(MatNum,x,z)*5
      h2#=get matrix height(MatNum,x,z)*5
      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#)
      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#)
      nx#=sin(ax#)
      ny#=cos(ax#)
      nz#=sin(az#)
      Set matrix normal MatNum,x,z,nx#,ny#,nz#
    next x
  next z
  Update Matrix MatNum
EndFunction
 
` Save Matrix Function by Roddman
Function CT_Save_Matrix(mat)
   number=1
   Repeat
      string$="map"
      string$=string$+STR$(number)+".txt"
      INC number
   Until File Exist(string$)=0
   Open to Write 1,string$
   Print string$
   for X = 0 to 29
      line$="Data "
      for Z = 0 to 29
         line$=line$+STR$(Get Ground Height(1,X*50,Z*50)/25)+","
      next Z
      Write String 1,line$
   next X
   Close File 1
EndFunction