home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / Amos / AMOSList-1097 / AMOSLIST / Cafeeng4.lha / RAYCAST.ASC next >
Encoding:
Text File  |  1997-10-14  |  7.5 KB  |  254 lines

  1. ' Cafe engine v0.9 b5  
  2. ' Raycasting routine 
  3. '
  4. ' Written by Adam Parrott © 1997 
  5. ' Copyright © 1997 Parrottsoft, Ltd. 
  6. '
  7. ' Raycasting routine to: 
  8. ' 1. Cast rays in x-y planes 
  9. ' 2. Draw walls (orthogonal-90°) 
  10. ' 3. Return needed info to Cafe engine 
  11. '
  12. ' This program is protected under copyright law
  13. ' from provision of the United States government 
  14. ' and national and international treaties. 
  15. '
  16. ' P.S. Inserted are some comments and Rem(s)
  17. ' everywhere I could so as to help you in
  18. ' figuring out my variables and the engine setup.
  19.  
  20. Set Buffer 128 : Degree 
  21. Dim MAP(4096),XNEXT(360),YNEXT(360),DISTTBL(4096)
  22.  
  23. ' Load Iff "<Your texture here>",2 
  24.  
  25. ' You can change to anything you want, 
  26. ' although at this point, since I don't
  27. ' have the distance, there is no texture 
  28. ' mapping, although there will be later. 
  29.  
  30. ' Open the screen for drawing. 
  31. Screen Open 1,320,200,64,Lowres
  32. Flash Off : Curs Off : Cls 0
  33.  
  34. ' Open the main-display screen.
  35. Screen Open 0,320,200,64,Lowres
  36. Flash Off : Curs Off : Cls 0
  37.  
  38. ' Get Palette 2  
  39. 'Screen Hide 2 
  40. Screen Hide 1
  41. Screen Show 0
  42.  
  43. Global MAP(),X,Y,A,ANG,ANGLE,WLDIST
  44. Global MAPPOS,MAP_XPOS,MAP_YPOS,DISTTBL()
  45. Global CASTING,SLICE,GRID_SIZE,DETAIL
  46. Global DEPTH,XDIST,YDIST,XNEXT,YNEXT
  47. Global XDEPTH,YDEPTH,XDELTA,YDELTA,IK
  48. Global XBOUND,YBOUND,RAYDEP,NHS,CSA
  49. Global XNEXT(),YNEXT(),XC,XCI,ANGLEST
  50.  
  51. Open In 1,"Ram:x_Tantbl.asc"
  52. For A=1 To 360 : Input #1,XNEXT(A) : Next A : Close 1
  53. ' This opens in and reads the x-Tan table
  54.  
  55. Open In 1,"Ram:y_Tantbl.asc"
  56. For B=0 To 360 : Input #1,YNEXT(B) : Next B : Close 1
  57. ' This opens in and reads the y-Tan table
  58.  
  59. ' For C=1 To 4096 : DISTTBL(C)=(2047/C) : Next C 
  60. ' Rem Setup distance table for walls in DistTbl()  
  61. ' This was a distance table I had set up.
  62.  
  63. Paper 0
  64. For M=0 To 20
  65. MAP(M)=3 : Next 
  66.  
  67. XCI=0 : XC=0 : IK=22
  68. X=200 : Y=200 : A=270
  69. DETAIL=5 : RAYCAST[X,Y,A]
  70.  
  71. ' XCI = Increments ANGLE(s) i.e. 
  72. ' XCI = 5, therefore if XCI=5, then Inc Angle
  73. ' and cast new ray. With 320 columns, cast an
  74. ' ray, then if XCI=5, then draw the same wall
  75. ' slice 5 times (same thing for 5 columns), then 
  76. ' exit loop, and cast new ray, looping the process.
  77.  
  78. ' XC = Screen column. (XColumn)
  79. ' IK = InK number for drawing. 
  80. ' A  = Current facing angle
  81.  
  82. ' DETAIL = Pretty self-explanatory. Lower the number,
  83. ' the great the detail, and the longer it takes (to draw/
  84. ' update the screen, as shown in the loop at bottom.)  
  85.  
  86. Do 
  87.    If Inkey$="4" Then X=X-10 : RAYCAST[X,Y,A]
  88.    If Inkey$="6" Then X=X+10 : RAYCAST[X,Y,A]
  89.    If Jup(1) Then Y=Y-10 : RAYCAST[X,Y,A]
  90.    If Jdown(1) Then Y=Y+10 : RAYCAST[X,Y,A]
  91.    If Jleft(1) Then A=A-5 : RAYCAST[X,Y,A]
  92.    If Jright(1) Then A=A+5 : RAYCAST[X,Y,A]
  93. Loop 
  94.  
  95. ' As shown above, I don't have a fully functional
  96. ' "moving" algorithm implemented, but I will in
  97. ' the future, as with the tmapping one as well. The
  98. ' about routine is just a crude way of getting basic 
  99. ' movement, since I'm usually testing in y-plane anyhow! 
  100.  
  101. ' Much of the following procedure has been modelled
  102. ' after John Bintz's original raycasting routine, as
  103. ' I examined it first, then used some of it's basic
  104. ' concepts that are universal in raycasting and my book.
  105.  
  106. Procedure RAYCAST[X,Y,ANG]
  107.    ANGLE=((ANG-32+360) mod 360)
  108.    ANGST=((ANG+32+360) mod 360)
  109.    CORNER=0 : GRID_SIZE=64
  110.    MAP_XPOS=X : MAP_YPOS=Y
  111.    
  112.    CSA=-32 : Screen 1 : Cls 0
  113.    Ink 15 : Bar 0,0 To 320,100
  114.    Ink 2 : Bar 0,100 To 320,200
  115.    
  116.    Repeat 
  117.       
  118.       ' Get immediate map pos. 
  119.       If ANGLE>90 and ANGLE<270
  120.       MAP_XPOS=(MAP_XPOS/64)*64 : Else 
  121.       MAP_XPOS=((MAP_XPOS/64)*64)+64 : End If 
  122.       
  123.       YNEXT=YNEXT(ANGLE)
  124.       MAP_YPOS=MAP_YPOS+YNEXT
  125.       
  126.       ' Setup for Delta vars 
  127.       If ANGLE>90 and ANGLE<270
  128.       XDELTA=-GRID_SIZE : Else 
  129.       XDELTA=GRID_SIZE : End If 
  130.       
  131.       CASTING=-1
  132.       
  133.       Repeat 
  134.          ' Uncomment this to see the actual 
  135.          ' rays being cast on the screen. 
  136.          
  137.          ' Draw X/5,Y/5 To MAP_XPOS/5,MAP_YPOS/5  
  138.          MAPPOS=(MAP_YPOS/64)*64+(MAP_XPOS/64)
  139.          
  140.          If MAPPOS<0 Then MAPPOS=0 : CASTING=0
  141.          If MAPPOS>4096 Then MAPPOS=4096 : CASTING=0
  142.          
  143.          If MAP(MAPPOS)>1 : CASTING=0
  144.          XDIST=MAP_XPOS-X : Else 
  145.          XDIST=0 : CASTING=-1 : End If 
  146.          
  147.          If CASTING : MAP_XPOS=MAP_XPOS+XDELTA
  148.          MAP_YPOS=MAP_YPOS+YNEXT : End If 
  149.          
  150.          If MAP_XPOS<0 Then MAP_XPOS=0 : CASTING=0
  151.          If MAP_XPOS>4096 Then MAP_XPOS=4096 : CASTING=0
  152.          
  153.          If MAP_YPOS<0 Then MAP_YPOS=0 : CASTING=0
  154.          If MAP_YPOS>4096 Then MAP_YPOS=4096 : CASTING=0
  155.          
  156.       Until Not CASTING
  157.       
  158.       CASTING=-1
  159.       MAP_XPOS=X
  160.       MAP_YPOS=Y
  161.       
  162.       ' Get Immediate map pos. 
  163.       If ANGLE>0 and ANGLE<180
  164.       MAP_YPOS=((MAP_YPOS/64)*64)+64 : Else 
  165.       MAP_YPOS=(MAP_YPOS/64)*64 : End If 
  166.       
  167.       XNEXT=XNEXT(ANGLE)
  168.       MAP_XPOS=MAP_XPOS+XNEXT
  169.       
  170.       If ANGLE>0 and ANGLE<180
  171.       YDELTA=GRID_SIZE : Else 
  172.       YDELTA=-GRID_SIZE : End If 
  173.       
  174.       Repeat 
  175.          'Draw X/5,Y/5 To MAP_XPOS/5,MAP_YPOS/5 
  176.          MAPPOS=(MAP_YPOS/64)*64+(MAP_XPOS/64)
  177.          
  178.          If MAPPOS<0 Then MAPPOS=0 : CASTING=0
  179.          If MAPPOS>4096 Then MAPPOS=4096 : CASTING=0
  180.          
  181.          If MAP(MAPPOS)>1 : CASTING=0
  182.          YDIST=MAP_YPOS-Y : Else 
  183.          YDIST=0 : CASTING=-1 : End If 
  184.          
  185.          If CASTING : MAP_XPOS=MAP_XPOS+XNEXT
  186.          MAP_YPOS=MAP_YPOS+YDELTA : End If 
  187.          
  188.          If MAP_XPOS<0 Then MAP_XPOS=0 : CASTING=0
  189.          If MAP_XPOS>4096 Then MAP_XPOS=4096 : CASTING=0
  190.          
  191.          If MAP_YPOS<0 Then MAP_YPOS=0 : CASTING=0
  192.          If MAP_YPOS>4096 Then MAP_YPOS=4096 : CASTING=0
  193.          
  194.       Until Not CASTING
  195.       
  196.       ' According to my book, the routine calls for
  197.       ' the *inverse* Sine and Cosine of the angles. 
  198.       
  199.       XDIST=XDIST*-Cos(ANGLE)
  200.       YDIST=YDIST*-Sin(ANGLE)
  201.       
  202.       ' Determine which wall is closer, the x
  203.       ' or y wall, then pass that onto WLDIST. 
  204.       
  205.       If YDIST<XDIST
  206.       WLDIST=YDIST : SLICE=YDIST/64 : IK=4 : Else 
  207.       WLDIST=XDIST : SLICE=XDIST/64 : IK=5 : End If 
  208.       
  209.       ' WLDIST is "Wall DISTance", 
  210.       ' as you can probably tell.. 
  211.       
  212.       ' NHS was derived from the old term
  213.       ' "New Half Size". It caught on, and
  214.       ' it's short, so I used it.
  215.       
  216.       NHS=Abs(WLDIST)*Cos(CSA)
  217.       
  218.       ' As in most casters, here I'm multiplying 
  219.       ' by the Cosine of the current casing angle, 
  220.       ' starting at -32 and going upto 32 (the entire
  221.       ' number of angles in your Field of View), 
  222.       ' as explained ealier in the Readme file.  
  223.       
  224.       For S=1 To DETAIL
  225.          ' <Insert your distance routine here>
  226.          ' <Insert your distance routine here>
  227.          ' <Insert your distance routine here>
  228.          
  229.          ' This is pretty much where I'm stuck. 
  230.          ' Tried using distance tables, which 
  231.          ' worked ok on y-walls, but not on 
  232.          ' x-walls. This is what not's working.
  233.          
  234.          Screen 1 : Cls IK,XC,100-NHS To XC+1,100+NHS
  235.          Inc XCI : If XCI=5 : XCI=0
  236.          Inc ANGLE : Inc CSA : End If 
  237.          
  238.          Inc XC : MAP_XPOS=X : MAP_YPOS=Y
  239.          ANGLE=((ANGLE+360) mod 360)
  240.          
  241.       Next S : Rem Loops according to DETAIL variable 
  242.       
  243.       'Screen 0 : Ink 1 : Paper 0
  244.       'Locate 0,22 : Print "NHS:";NHS;Space$(4)
  245.       'Locate 15,22 : Print "A:";A;Space$(6) 
  246.       'Locate 0,23 : Print "X:";X;Space$(1)
  247.       'Locate 6,23 : Print "Y:";Y;Space$(4)
  248.       'Locate 15,23 : Print "MAPPOS:";MAPPOS;Space$(3) 
  249.       
  250.    Until ANGLE=ANGST : XC=0
  251.    Screen Copy 1 To 0
  252.    
  253. End Proc : Rem End raycasting procedure 
  254.