|
| 1 | +# [[ RAYCAST EXAMPLE BY DEVKEV AKA PHILIPP GERSCH 30.08.23 ]] # |
| 2 | +# This Example renders a pseudo 3d scene using raycasting and drawing is using ascii characters # |
| 3 | +# I do not not recommend creating stuff like this yourself using this language. # |
| 4 | +# This example should only show the capabilities of this language and the overall acceptable performance # |
| 5 | + |
| 6 | +# The view will rotate automatically, but you could enable controls if you want. You can also add more walls using the variables bofore the MAIN loop # |
| 7 | +# If manual control is enabled, you can move and look around using the W A S D keys # |
| 8 | + |
| 9 | +MANUAL_CONTROL = $false; |
| 10 | + |
| 11 | + |
| 12 | +sizeX = 100; |
| 13 | +sizeY = 30; |
| 14 | +pixels = []; |
| 15 | +tileSize = 10; |
| 16 | + |
| 17 | +for i $sizeY { |
| 18 | + row = []; |
| 19 | + for j $sizeX { |
| 20 | + row[$j] = " "; |
| 21 | + }; |
| 22 | + pixels[$i] = $row; |
| 23 | +}; |
| 24 | + |
| 25 | +#-----------------------------------------# |
| 26 | +#UTILS# |
| 27 | + |
| 28 | +#Returns a new object with two dimenstions x and y# |
| 29 | +point = { |
| 30 | + return (new-object {x = $0;y = $1}); |
| 31 | +}; |
| 32 | + |
| 33 | +#Calculates the distance between two points# |
| 34 | +dist = { |
| 35 | + dx = ($0 - $2); |
| 36 | + dy = ($1 - $3); |
| 37 | + return (sqrt (($dx * $dx) + ($dy * $dy))); |
| 38 | +}; |
| 39 | + |
| 40 | +#-----------------------------------------# |
| 41 | +#OBJECT CREATION# |
| 42 | + |
| 43 | +#Creates a new wall# |
| 44 | +new-wall = { |
| 45 | + return (new-object { |
| 46 | + #Create coordinates on the grid# |
| 47 | + x1 = ($0 * $tileSize); |
| 48 | + y1 = ($1 * $tileSize); |
| 49 | + x2 = ($2 * $tileSize); |
| 50 | + y2 = ($3 * $tileSize); |
| 51 | + }); |
| 52 | +}; |
| 53 | + |
| 54 | +#Creates a new ray object# |
| 55 | +new-ray = { |
| 56 | + return (new-object { |
| 57 | + x = $0; |
| 58 | + y = $1; |
| 59 | + dirX = $2; |
| 60 | + dirY = $3; |
| 61 | + |
| 62 | + collisionX = 0; |
| 63 | + collisionY = 0; |
| 64 | + dist = -1; |
| 65 | + angle = 0; |
| 66 | + |
| 67 | + #Argument 0 needs to be a wall object. Returns a new point# |
| 68 | + cast = { |
| 69 | + wall = $0; |
| 70 | + |
| 71 | + ifnot ($dirY typeof float) { |
| 72 | + dirY = 0; |
| 73 | + }; |
| 74 | + ifnot ($dirX typeof float) { |
| 75 | + dirX = 0; |
| 76 | + }; |
| 77 | + |
| 78 | + x1 = $x; |
| 79 | + x3 = $wall.x1; |
| 80 | + x4 = $wall.x2; |
| 81 | + y3 = $wall.y1; |
| 82 | + y4 = $wall.y2; |
| 83 | + x2 = ($x + ($dirX * "100")); |
| 84 | + y1 = $y; |
| 85 | + y2 = ($y + ($dirY * "100")); |
| 86 | + |
| 87 | + nenner = ((($x1 - $x2) * ($y3 - $y4)) - (($y1 - $y2) * ($x3 - $x4))); |
| 88 | + |
| 89 | + if ($nenner == 0) { |
| 90 | + return ""; |
| 91 | + }; |
| 92 | + |
| 93 | + tt = (((($x1 - $x3) * ($y3 - $y4)) - (($y1 - $y3) * ($x3 - $x4))) / $nenner); |
| 94 | + uu = (((($x1 - $x2) * ($y1 - $y3)) - (($y1 - $y2) * ($x1 - $x3))) / $nenner); |
| 95 | + |
| 96 | + ifnot ($uu typeof float) { |
| 97 | + return ""; |
| 98 | + }; |
| 99 | + |
| 100 | + uu = ($uu * -1); |
| 101 | + |
| 102 | + if (($tt gt 0) and ($uu gt 0) and ($uu lt 1)) { |
| 103 | + rx = ($tt * ($x2 - $x1)); |
| 104 | + ry = ($tt * ($y2 - $y1)); |
| 105 | + return (call $point ($x1 + $rx) ($y1 + $ry)); |
| 106 | + }; |
| 107 | + return ""; |
| 108 | + }; |
| 109 | + }); |
| 110 | +}; |
| 111 | + |
| 112 | +new-point = { |
| 113 | + return (new-object { |
| 114 | + x = (($0 * $tileSize) + ($tileSize / 2)); |
| 115 | + y = (($1 * $tileSize) + ($tileSize / 2)); |
| 116 | + rayCount = $2; |
| 117 | + fov = $3; |
| 118 | + rays = []; |
| 119 | + |
| 120 | + angleStep = ($fov / $rayCount); |
| 121 | + angle = (($fov / 2) + ($angleStep / 2)); |
| 122 | + direction = (call $point 0 0); |
| 123 | + println $angleStep; |
| 124 | + |
| 125 | + for i $rayCount { |
| 126 | + angle = ($angle + $angleStep); |
| 127 | + println "Setting ray at " $angle " degrees"; |
| 128 | + rays[$i] = (call $new-ray $x $y (cos $angle) (sin $angle)); |
| 129 | + }; |
| 130 | + angle = 0; |
| 131 | + |
| 132 | + setPos = { |
| 133 | + x = $0; |
| 134 | + y = $1; |
| 135 | + |
| 136 | + for i $rayCount { |
| 137 | + rays[$i].x = $0; |
| 138 | + rays[$i].y = $1; |
| 139 | + }; |
| 140 | + }; |
| 141 | + |
| 142 | + setAngle = { |
| 143 | + direction = (call $point (cos $0) (sin $0)); |
| 144 | + angle = $0; |
| 145 | + angleCount = 0; |
| 146 | + for i $rayCount { |
| 147 | + rays[$i].angle = (($angleCount - ($fov / 2)) + ($angleStep / 2)); |
| 148 | + rays[$i].dirX = (cos ($angle + $rays[$i].angle)); |
| 149 | + rays[$i].dirY = (sin ($angle + $rays[$i].angle)); |
| 150 | + angleCount = ($angleCount + $angleStep); |
| 151 | + }; |
| 152 | + }; |
| 153 | + |
| 154 | + cast = { |
| 155 | + walls = $0; |
| 156 | + for i $rayCount { |
| 157 | + recordPos = ""; |
| 158 | + for j (length $walls) { |
| 159 | + castPos = (call $rays[$i].cast $walls[$j]); |
| 160 | + if ($castPos != "") { |
| 161 | + if ($recordPos == "") { |
| 162 | + recordPos = $castPos; |
| 163 | + } else { |
| 164 | + distRecord = (call $dist $recordPos.x $recordPos.y $rays[$i].x $rays[$i].y); |
| 165 | + dist = (call $dist $castPos.x $castPos.y $rays[$i].x $rays[$i].y); |
| 166 | + if ($dist lt $distRecord) { |
| 167 | + recordPos = $castPos; |
| 168 | + }; |
| 169 | + }; |
| 170 | + }; |
| 171 | + }; |
| 172 | + |
| 173 | + if ($recordPos != "") { |
| 174 | + rays[$i].dist = (call $dist $recordPos.x $recordPos.y $rays[$i].x $rays[$i].y); |
| 175 | + rays[$i].collisionX = $recordPos.x; |
| 176 | + rays[$i].collisionY = $recordPos.y; |
| 177 | + } else { |
| 178 | + rays[$i].dist = -1; |
| 179 | + }; |
| 180 | + }; |
| 181 | + }; |
| 182 | + }); |
| 183 | +}; |
| 184 | + |
| 185 | +#-----------------------------------------# |
| 186 | +#DRAWING AND RENDERING# |
| 187 | + |
| 188 | +drawScreen = { |
| 189 | + |
| 190 | + for i $sizeY { |
| 191 | + print "|"; |
| 192 | + for j $sizeX { |
| 193 | + print $pixels[$i][$j]; |
| 194 | + }; |
| 195 | + print "|"; |
| 196 | + println; |
| 197 | + }; |
| 198 | +}; |
| 199 | + |
| 200 | +clearBuffer = { |
| 201 | + for i $sizeY { |
| 202 | + for j $sizeX { |
| 203 | + pixels[$i][$j] = " "; |
| 204 | + }; |
| 205 | + println; |
| 206 | + }; |
| 207 | +}; |
| 208 | + |
| 209 | +#Paints a pixel to the buffer# |
| 210 | +paint = { |
| 211 | + x = (string (int $0)); |
| 212 | + y = (string (int $1)); |
| 213 | + if (($x lt $sizeX) and ($y lt $sizeY) and ($x gteq 0) and ($y gteq 0)) { |
| 214 | + pixels[$y][$x] = $2; |
| 215 | + } |
| 216 | +}; |
| 217 | + |
| 218 | + |
| 219 | +#-----------------------------------------# |
| 220 | +#MAIN# |
| 221 | + |
| 222 | +# Every wall has a start x and y and end x and y coordinate # |
| 223 | +walls = [ |
| 224 | + (call $new-wall -3 0 1 0) |
| 225 | + (call $new-wall 2 0 1 1) |
| 226 | + (call $new-wall 1 1 -2 1) |
| 227 | +]; |
| 228 | + |
| 229 | +anchor = (call $new-point 0 0 $sizeX 90); |
| 230 | +lastInput = ""; |
| 231 | +pixelStep = (string (int ($sizeX / $anchor.rayCount))); |
| 232 | + |
| 233 | +loop { |
| 234 | + call $anchor.cast $walls; |
| 235 | + call $clearBuffer; |
| 236 | + |
| 237 | + for i (length $anchor.rays) { |
| 238 | + if ($anchor.rays[$i].dist != -1) { |
| 239 | + actualDistance = ($anchor.rays[$i].dist * (cos $anchor.rays[$i].angle)); |
| 240 | + height = (((4 / $actualDistance) * $sizeY) / 2); |
| 241 | + for k $pixelStep { |
| 242 | + x = (($pixelStep * $i) + $k); |
| 243 | + topY = (($sizeY / 2) - $height); |
| 244 | + botY = (($sizeY / 2) + $height)); |
| 245 | + for y (string (int ($botY - $topY))) { |
| 246 | + if ($anchor.rays[$i].dist lt 8) { |
| 247 | + call $paint $x ($y + $topY) <#>; |
| 248 | + } elseif ($anchor.rays[$i].dist lt 12) { |
| 249 | + call $paint $x ($y + $topY) <+>; |
| 250 | + } else { |
| 251 | + call $paint $x ($y + $topY) <.>; |
| 252 | + }; |
| 253 | + }; |
| 254 | + call $paint $x $topY "_"; |
| 255 | + call $paint $x $botY "@"; |
| 256 | + }; |
| 257 | + firstRay = $true; |
| 258 | + }; |
| 259 | + }; |
| 260 | + |
| 261 | + try { |
| 262 | + exec "cls"; |
| 263 | + clearConsole; |
| 264 | + }; |
| 265 | + call $drawScreen; |
| 266 | + |
| 267 | + |
| 268 | + if (not $MANUAL_CONTROL) { |
| 269 | + call $anchor.setAngle (($anchor.angle + 15) % 360); |
| 270 | + } else { |
| 271 | + |
| 272 | + println "\nangle: " $anchor.angle; |
| 273 | + println "x: " $anchor.x; |
| 274 | + println "y: " $anchor.y; |
| 275 | + |
| 276 | + print "Move: "; |
| 277 | + dir = (input); |
| 278 | + if ($dir == "") { |
| 279 | + dir = $lastInput; |
| 280 | + }; |
| 281 | + |
| 282 | + if ($dir == d) { |
| 283 | + call $anchor.setAngle (($anchor.angle + 30) % 360); |
| 284 | + lastInput = d; |
| 285 | + } elseif ($dir == a) { |
| 286 | + call $anchor.setAngle (($anchor.angle - 30) % 360); |
| 287 | + lastInput = a; |
| 288 | + } elseif ($dir == w) { |
| 289 | + call $anchor.setPos ($anchor.x + (($anchor.direction.x / 2) * $tileSize)) ($anchor.y + (($anchor.direction.y / 2) * $tileSize)); |
| 290 | + lastInput = w; |
| 291 | + } elseif ($dir == s) { |
| 292 | + call $anchor.setPos ($anchor.x - (($anchor.direction.x / 2) * $tileSize)) ($anchor.y - (($anchor.direction.y / 2) * $tileSize)); |
| 293 | + lastInput = s; |
| 294 | + }; |
| 295 | + } |
| 296 | +}; |
| 297 | + |
0 commit comments