Guest User

Carl Mahnke Raycaster in QBasic

a guest
Oct 1st, 2020
797
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
QBasic 7.17 KB | None | 0 0
  1. ' Shown by Carl Mahnke, but now offline
  2. ' https://www.youtube.com/watch?v=3Qpht8dSps8
  3. ' http://rws.sebus.de/misc/raycast.rar
  4. ' Re-write by Bernhard Slawik 2020-10-01
  5.  
  6. ' But there is stuill a bug in normaly% i think...
  7. ' See "***"
  8.  
  9. DEF SEG = 0
  10. pl.x = 66: pl.xtmp = 66
  11. pl.y = 220: pl.ytmp = 220
  12. pl.ld = 5
  13. angf% = 1440
  14. angtol% = 20
  15. cols% = 320
  16. colstep% = 1    ' Set to >1 to decrease horizontal resolution
  17. notexture% = 0  ' Set to 1 for fast no-texture mode
  18. colsh% = cols% / 2
  19. DIM map(15, 15) AS INTEGER
  20. DIM costable#(angf% - 1)
  21. DIM sintable#(angf% - 1)
  22. pi# = 3.14159265#
  23. DIM buffer%(63, 448)
  24.  
  25. angh% = angf% / 2
  26. angq% = angf% / 4
  27. angtq% = 3 * angf% / 4
  28.  
  29. ' Pre-calc sine table
  30. PRINT "Calculating sine table..."
  31. FOR x = 0 TO angf% - 1
  32.     PRINT (x):
  33.     y# = x / (angf% - 1)
  34.     costable#(x) = COS(y# * pi# * 2)
  35.     sintable#(x) = SIN(y# * pi# * 2)
  36. NEXT x
  37.  
  38. ' Read map data
  39. FOR y = 0 TO 15
  40.     FOR x = 0 TO 15
  41.         READ map(x, y)
  42.     NEXT x
  43. NEXT y
  44.  
  45. ' graphic stuff
  46. SCREEN 13
  47.  
  48. PRINT "Loading textures..."
  49. 'GOTO fake       ' Do not load texture, use pattern
  50.  
  51. ' Read from BMP file
  52. OPEN "MAH_TEX2.BMP" FOR BINARY AS #1
  53. 'SEEK #1, 1079
  54. SEEK #1, 55 + (4 * 256) + (64 * 32)
  55. FOR y% = 447 TO 0 STEP -1
  56.     FOR x% = 0 TO 63
  57.         c% = ASC(INPUT$(1, #1))
  58.         buffer%(x%, y%) = c%
  59.     NEXT x%
  60. NEXT y%
  61.  
  62. SEEK #1, 55
  63. FOR x% = 0 TO 255
  64.     b% = ASC(INPUT$(1, #1)) \ 4
  65.     g% = ASC(INPUT$(1, #1)) \ 4
  66.     r% = ASC(INPUT$(1, #1)) \ 4
  67.     q% = ASC(INPUT$(1, #1))
  68.     PALETTE x%, r% + g% * 256 + b% * 256 ^ 2
  69. NEXT x%
  70. CLOSE #1
  71. GOTO main
  72.  
  73.  
  74. fake: ' Create procedural textures
  75. PRINT "Generating textures..."
  76. FOR y% = 447 TO 0 STEP -1
  77.     FOR x% = 0 TO 63
  78.         c = (x% * y%) MOD 256   ' Random
  79.         buffer%(x%, y%) = c
  80.     NEXT x%
  81. NEXT y%
  82.  
  83.  
  84. main:
  85. ' Main loop
  86. DO
  87. lookdir% = pl.ld
  88. FOR ray% = 0 TO cols% STEP colstep%
  89.     pl.ld = ((lookdir% + angf% - colsh%) + ray%) MOD angf%
  90.        
  91.     ' horizontal detection
  92.     dis1% = -1
  93.     IF pl.ld MOD angh% > angtol% AND pl.ld MOD angh% < (angh% - angtol%) THEN
  94.         dir% = (ABS(pl.ld - angh%) / (pl.ld - angh%)) * -1
  95.         pl.xb = pl.x \ 64
  96.         pl.yb = pl.y \ 64
  97.         rest% = ABS(((pl.y MOD 64) + (64 * (dir% * -1)) + ((1 + dir%) / 2)) MOD 64)
  98.         shortx% = rest% * costable#(pl.ld) / sintable#(pl.ld) * dir%
  99.         actdef% = shortx%
  100.         nblockx% = (shortx% + pl.x) \ 64
  101.         nblocky% = pl.yb + dir%
  102.         IF nblockx% > -1 AND nblockx% < 16 THEN
  103.             'dir1% = ABS(rest% / sintable#(pl.ld))
  104.             dis1% = ABS(rest% / sintable#(pl.ld))
  105.             fblock% = map(nblockx%, nblocky%)
  106.             IF fblock% = 0 THEN
  107.                 count% = 1
  108.                 dis1% = -1
  109.                 normalx% = 64 * (costable#(pl.ld) / sintable#(pl.ld)) * dir%
  110.                 'normalx% = 64 * costable#(pl.ld) / sintable#(pl.ld) * dir%
  111.                 'normalx% = 64 / costable#(pl.ld) * sintable#(pl.ld) * dir%
  112.                 DO
  113.                     actdef% = shortx% + (count% * normalx%)
  114.                     nblockx% = (actdef% + pl.x) \ 64
  115.                     nblocky% = pl.yb + (dir% * (count% + 1))
  116.                     'IF nblockx% < 16 AND nblockx% > -1 THEN fblock% = map(nblockx%, nblocky%)
  117.                     IF nblockx% > -1 AND nblockx% < 16 AND nblocky% > -1 AND nblocky% < 16 THEN fblock% = map(nblockx%, nblocky%)
  118.                     IF fblock% > 0 THEN dis1% = ABS(rest% / sintable#(pl.ld) + (64 / sintable#(pl.ld) * count%)): count% = 11
  119.                     count% = count% + 1
  120.                 LOOP UNTIL count% > 10
  121.             END IF
  122.         END IF
  123.         IF dis1% <> -1 THEN dis1% = dis1% * costable#(ABS(ray% - colsh%))
  124.         texoffh% = ABS((pl.x + actdef%) MOD 64)
  125.         tex1% = fblock%
  126.     END IF
  127.    
  128.     ' vertical detection
  129.     dis2% = -1
  130.     IF ((pl.ld + angq%) MOD angh% > angtol%) AND ((pl.ld + angq%) MOD angh% < (angh% - angtol%)) THEN
  131.         dir% = (ABS((pl.ld MOD angtq%) - angq%) / ((pl.ld MOD angtq%) - angq%)) * -1
  132.         pl.xb = pl.x \ 64
  133.         pl.yb = pl.y \ 64
  134.         rest% = ABS(((pl.x MOD 64) + (64 * (dir% * -1)) + ((1 + dir%) / 2)) MOD 64)
  135.         'shorty% = rest% * costable#(pl.ld) / sintable#(pl.ld) * dir%
  136.         shorty% = rest% / costable#(pl.ld) * sintable#(pl.ld) * dir%
  137.         actdef% = shorty%
  138.         nblocky% = (shorty% + pl.y) \ 64
  139.         nblockx% = pl.xb + dir%
  140.         IF nblocky% > -1 AND nblocky% < 16 THEN
  141.             dis2% = ABS(rest% / costable#(pl.ld))
  142.             's# = sintable#(pl.ld)
  143.             '' IF ABS(s#) < .01 THEN s# = .01 * SGN(s#)
  144.             'IF s# >= 0# AND s# < .0001 THEN s# = .0001
  145.             'ELSE IF s# < 0# AND s# > -.0001 THEN s# = -.0001
  146.             'dis2% = ABS(rest% / s#)
  147.            
  148.             fblock% = map(nblockx%, nblocky%)
  149.             IF fblock% = 0 THEN
  150.                 count% = 1
  151.                 dis2% = -1
  152.                 normaly% = 64 / costable#(pl.ld) * sintable#(pl.ld) * dir%
  153.                 DO
  154.                     actdef% = shorty% + (count% * normaly%)
  155.                     nblocky% = (actdef% + pl.y) \ 64
  156.                     nblockx% = pl.xb + (dir% * (count% + 1))
  157.                     IF nblocky% < 16 AND nblocky% > -1 THEN fblock% = map(nblockx%, nblocky%)
  158.                     IF fblock% > 0 THEN dis2% = ABS(rest% / costable#(pl.ld) + (64 / costable#(pl.ld) * count%)): count% = 11
  159.                     count% = count% + 1
  160.                 LOOP UNTIL count% > 10
  161.             END IF
  162.         END IF
  163.         IF dis2% <> -1 THEN dis2% = dis2% * costable#(ABS(ray% - colsh%))
  164.         texoffv% = ABS((pl.y + actdef%) MOD 64)
  165.         tex2% = fblock%
  166.     END IF
  167.  
  168.     vertical = 0
  169.     IF dis2% <> -1 AND dis1% = -1 THEN dis1% = dis2%: vertical = 1
  170.     IF dis2% <> -1 AND dis1% <> -1 AND dis2% < dis1% THEN dis1% = dis2%: vertical = 1
  171.     IF dis1% = -1 THEN dis1% = 0
  172.     IF vertical = 1 THEN texoffh% = texoffv%: tex1% = tex2%
  173.    
  174.     IF dis1% <> 0 THEN lineh% = 200 * (65 / dis1%)
  175.     scaler# = lineh% / 64
  176.    
  177.     IF tex1% <> 0 THEN
  178.         linehh% = lineh% / 2
  179.         ' Draw simple color
  180.         IF notexture% > 0 THEN LINE (ray%, 100 - linehh%)-(ray%, 100 + linehh%), tex1%: GOTO skiptexture
  181.  
  182.         ' Draw texture
  183.         FOR pixel% = 0 TO 63
  184.             col% = buffer%(texoffh%, pixel% + ((tex1% - 1) * 64))
  185.             IF scaler# > 1 THEN
  186.                 LINE (ray%, 100 - linehh% + pixel% * scaler#)-(ray%, 100 - linehh% + (pixel% + 1) * scaler#), col%
  187.             ELSE 'IF scaler# <= 1 THEN
  188.                 PSET (ray%, 100 - linehh% + pixel% * scaler#), col%
  189.             END IF
  190.         NEXT pixel%
  191. skiptexture:
  192.     END IF
  193.  
  194.     IF lineh% < 200 AND lineh% > 0 THEN
  195.         LINE (ray%, 100 - linehh%)-(ray%, 0), 1 ' Sky
  196.         LINE (ray%, 100 + linehh%)-(ray%, 200), 17 ' Floor
  197.     END IF
  198. NEXT ray%
  199.  
  200. pl.ld = lookdir%
  201.  
  202. ' Keyboard
  203.  
  204. 'POKE &H41A, PEEK(&H41C)
  205. 'k% = INP(&H60)
  206. k$ = INKEY$
  207. 'LOCATE 1, 1: PRINT k$
  208. 'IF k% = 30 THEN pl.ld = pl.ld - 20
  209. 'IF k% = 32 THEN pl.ld = pl.ld + 20
  210. IF k$ = CHR$(0) + "M" THEN pl.ld = pl.ld + 20
  211. IF k$ = CHR$(0) + "K" THEN pl.ld = pl.ld - 20
  212.  
  213. walkdir% = (pl.ld + angq%) MOD angf%
  214. 'speed# = speed# / 2
  215. speed# = 0
  216. 'IF k% = 17 THEN speed# = 10
  217. 'IF k% = 31 THEN speed# = -10
  218. IF k$ = CHR$(0) + "H" THEN speed# = 10
  219. IF k$ = CHR$(0) + "P" THEN speed# = -10
  220.  
  221. pl.ytmp = (pl.ytmp - speed# * costable#(walkdir%))
  222. pl.xtmp = (pl.xtmp + speed# * sintable#(walkdir%))
  223. pl.y = INT(pl.ytmp)
  224. pl.x = INT(pl.xtmp)
  225.  
  226. pl.ld = ABS((pl.ld + angf%) MOD angf%)
  227. LOOP UNTIL k$ = CHR$(27)        'k% = 16
  228.  
  229.  
  230. ' Insert Map
  231. DATA 7,2,1,2,1,1,1,2,1,1,1,1,1,1,1,1
  232. DATA 7,0,0,0,2,2,2,2,2,6,0,0,0,0,0,3
  233. DATA 7,0,0,0,0,5,0,0,3,6,0,0,0,0,0,3
  234. DATA 7,0,0,0,0,0,0,0,3,6,0,3,0,3,0,3
  235. DATA 7,0,0,0,0,5,0,0,3,6,0,0,0,0,0,3
  236. DATA 7,2,2,2,2,3,0,0,3,6,0,0,0,0,0,3
  237. DATA 1,4,1,1,1,1,7,0,7,6,0,0,0,0,7,3
  238. DATA 3,4,7,0,0,0,7,0,7,6,0,0,0,0,0,3
  239. DATA 3,0,3,5,0,5,7,0,7,7,0,0,0,0,0,3
  240. DATA 3,0,0,5,0,0,0,0,0,0,0,0,0,0,0,3
  241. DATA 3,0,0,5,5,5,5,1,5,7,0,0,0,0,0,3
  242. DATA 5,0,5,5,2,2,2,2,2,6,0,0,0,0,0,3
  243. DATA 5,0,0,0,7,0,0,0,0,0,0,3,0,3,0,3
  244. DATA 5,0,0,0,0,0,0,0,0,6,0,0,0,0,0,3
  245. DATA 5,0,0,0,7,0,0,0,0,6,0,0,0,0,0,3
  246. DATA 5,5,5,5,2,2,2,2,2,1,1,1,1,1,1,6
  247.  
  248.  
Add Comment
Please, Sign In to add comment