mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Dec 2002
Posts: 252
T
Talon Offline OP
Fjord artisan
OP Offline
Fjord artisan
T
Joined: Dec 2002
Posts: 252
Ok, raycasting is casting out rays in a range which is your field of view, and converting those points back to 2d coordinates on a map. depending on what you hit, you would draw some vertical lines, this was the gaming idea for old dos games such as wolfenstein 3d and doom. This was great because you only had to draw 320 vertical lines (resolution was 320x200) and was a fast method to 3d games. I've tried to reproduce this but mIRC draw commands are just too darn slow. Great example tho.

;=====================================================
; mIRC Raycaster v1.0 By: Talon
; This is a VERY slow raycasting engine!
; To try it out, just type "/raycaster"
;=====================================================
menu @RayCast {
show rendering:hdel -w RayCast RenderMode
Only show complete render:hadd -m RayCast RenderMode -n
}
on *:Close:@RayCast: { hfree RayCast }
alias raycaster {
hfree -w RayCast
window -p @RayCast 0 0 300 200
if ($1 = 2) {
var %xs = 24 , %ys = 24
hadd -m RayCast Table.1 4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,7,6,7,6,7,6,7,6
hadd -m RayCast Table.2 3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,0,0,0,0,0,0,7
hadd -m RayCast Table.3 4,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6
hadd -m RayCast Table.4 3,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7
hadd -m RayCast Table.5 4,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,7,0,0,0,0,0,0,6
hadd -m RayCast Table.6 3,0,4,0,0,0,0,5,4,5,4,5,4,5,4,5,6,7,0,8,6,7,6,7
hadd -m RayCast Table.7 4,0,5,0,0,0,0,5,0,5,0,5,0,5,0,5,7,0,0,0,7,7,7,1
hadd -m RayCast Table.8 3,0,6,0,0,0,0,5,0,0,0,0,0,0,0,5,6,0,0,0,0,0,0,8
hadd -m RayCast Table.9 4,0,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,7,7,1
hadd -m RayCast Table.10 3,0,8,0,0,0,0,5,0,0,0,0,0,0,0,5,7,0,0,0,0,0,0,8
hadd -m RayCast Table.11 4,0,0,0,0,0,0,5,0,0,0,0,0,0,0,5,6,0,0,0,7,7,7,1
hadd -m RayCast Table.12 3,0,0,0,0,0,0,5,5,5,5,0,5,5,5,5,7,7,7,7,7,7,7,2
hadd -m RayCast Table.13 6,6,6,6,6,6,6,6,6,6,6,0,6,6,6,6,6,6,6,6,6,6,6,6
hadd -m RayCast Table.14 8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4
hadd -m RayCast Table.15 6,6,6,6,6,6,0,6,6,6,6,0,6,6,6,6,6,6,6,6,6,6,6,6
hadd -m RayCast Table.16 4,4,4,4,4,4,0,4,4,4,6,0,6,2,2,2,2,2,2,2,3,3,3,3
hadd -m RayCast Table.17 3,0,0,0,0,0,0,0,0,4,6,0,6,2,0,0,0,0,0,2,0,0,0,2
hadd -m RayCast Table.18 4,0,0,0,0,0,0,0,0,0,0,0,6,2,0,0,5,0,0,2,0,0,0,3
hadd -m RayCast Table.19 3,0,0,0,0,0,0,0,0,4,6,0,6,2,0,0,0,0,0,2,2,0,2,2
hadd -m RayCast Table.20 4,0,6,0,6,0,0,0,0,4,6,0,0,0,0,0,5,0,0,0,0,0,0,3
hadd -m RayCast Table.21 3,0,0,5,0,0,0,0,0,4,6,0,6,2,0,0,0,0,0,2,2,0,2,2
hadd -m RayCast Table.22 4,0,6,0,6,0,0,0,0,4,6,-1,6,2,0,0,5,0,0,2,0,0,0,3
hadd -m RayCast Table.23 3,0,0,0,0,0,0,0,0,4,6,0,6,2,0,0,0,0,0,2,0,0,0,2
hadd -m RayCast Table.24 4,4,4,4,4,4,4,4,4,4,1,1,1,2,2,2,2,2,2,3,3,3,3,3
hadd -m RayCast Angle 90
}
else {
var %xs = 10 , %ys = 10
hadd -m RayCast Table.1 2, 5, 3, 5, 2, 5, 3, 5, 2, 5
hadd -m RayCast Table.2 5, 0, 0, 0, 0, 0, 0, 0, 0, 2
hadd -m RayCast Table.3 3, 0, 3, 0, 0, 0, 3, 4, 0, 5
hadd -m RayCast Table.4 5, 0, 2, 0, 0, 0, 4, 2, 0, 3
hadd -m RayCast Table.5 2, 0, 3, 0, 0, 0, 0, 0, 0, 5
hadd -m RayCast Table.6 5, 0, 2, 0,-1, 0, 0, 0, 0, 2
hadd -m RayCast Table.7 3, 0, 0, 0, 0, 0, 7, 0, 6, 5
hadd -m RayCast Table.8 5, 0,10,12,10,12, 0, 0, 0, 3
hadd -m RayCast Table.9 2, 0, 0, 0, 0, 0, 0, 0, 0, 5
hadd -m RayCast Table.10 5, 3, 5, 2, 5, 3, 5, 2, 5, 3
hadd -m RayCast Angle 180
}
var %x = 0
while (%x < %xs) {
inc %x
var %y = 0
while (%y < %ys) {
inc %y
if ($RayMap(%x,%y) = -1) {
RayMap %x %y 0
hadd -m RayCast PlayerX $calc(%x * 1024)
hadd -m RayCast PlayerY $calc(%y * 1024)
}
}
}
raycast
}
alias RayMap {
if ($3 = $null) { return $gettok($hget(RayCast,$+(Table.,$1)),$2,44) }
else { hadd -m RayCast $+(Table.,$1) $puttok($hget(RayCast,$+(Table.,$1)),$3,$2,44) }
}
alias SinTable { return $int($calc($Sin($calc($1 * $PI / 180)) * 1024 / 10)) }
alias CosTable { return $int($calc($Cos($calc($1 * $PI / 180)) * 1024 / 10)) }
alias RayCast {
var %ticks = $ticks
var %s = -5 , %a = 0 , %Angle = $hget(RayCast,Angle)
var %z = -30
var %SkyColor = 14
var %FloorColor = 15
while (%z < 31) {
inc %z
titlebar @RayCast - Rendering... $round($calc(((%z + 31) / 62) * 100),2) $+ %
%s = %s + 5
%a = %Angle + %z
if (%a < 0) { %a = %a + 360 }
if (%a > 359) { %a = %a - 360 }
var %xx = $hget(RayCast,PlayerX)
var %yy = $hget(RayCast,PlayerY)
var %StepX = $SinTable(%a)
var %StepY = $CosTable(%a)
var %dd = 0 , %ds = 0 , %m
while (!%m) {
%xx = %xx - %StepX
%yy = %yy - %StepY
%m = $RayMap($int($calc(%xx / 1024)),$int($calc(%yy / 1024)))
inc %dd
}
%ds = $int($calc(1000 / %dd))
var %show = $hget(RayCast,RenderMode)
drawline %show @RayCast %SkyColor 5 %s 0 %s $calc(99 - %ds)
drawline %show @RayCast %m 5 %s $calc(100 - %ds) %s $calc(100 + %ds)
drawline %show @RayCast %FloorColor 5 %s $calc(101 + %ds) %s 200
}
if (%show) { drawdot @RayCast }
titlebar @RayCast - Done. $calc(($ticks - %ticks) / 1000) secs $round($calc(1000 / ($ticks - %ticks)),2) FPS
}
on *:KeyDown:@RayCast:*: {
var %Angle = $hget(RayCast,Angle) , %m
var %PlayerX = $hget(RayCast,PlayerX)
var %PlayerY = $hget(RayCast,PlayerY)

if ($keyval = 37) {
;========== left ==========
%Angle = %Angle - 5
hadd -m RayCast Angle %Angle
raycast
}
if ($keyval = 38) {
;========== up ==========
%PlayerX = %PlayerX - $calc($SinTable(%Angle) * 5)
%PlayerY = %PlayerY - $calc($CosTable(%Angle) * 5)
%m = $RayMap($int($calc(%PlayerX / 1024)),$int($calc(%PlayerY / 1024)))
if (!%m) {
hadd -m RayCast PlayerX %PlayerX
hadd -m RayCast PlayerY %PlayerY
raycast
}
}
if ($keyval = 39) {
;========== right ==========
%Angle = %Angle + 5
hadd -m RayCast Angle %Angle
raycast
}
if ($keyval = 40) {
;========== down ==========
%PlayerX = %PlayerX + $calc($SinTable(%Angle) * 5)
%PlayerY = %PlayerY + $calc($CosTable(%Angle) * 5)
%m = $RayMap($int($calc(%PlayerX / 1024)),$int($calc(%PlayerY / 1024)))
if (!%m) {
hadd -m RayCast PlayerX %PlayerX
hadd -m RayCast PlayerY %PlayerY
raycast
}
}
}

Joined: Dec 2002
Posts: 252
T
Talon Offline OP
Fjord artisan
OP Offline
Fjord artisan
T
Joined: Dec 2002
Posts: 252
what good is the code without a screenshot? whoops! smile

Joined: May 2006
Posts: 2
J
Bowl of petunias
Offline
Bowl of petunias
J
Joined: May 2006
Posts: 2
You could try replacing divisions by constants by multiplications by stored reciprocals and generally tidying up the code if you want it to run faster. e.g.

* 1024 / 10 -> * 102.4

You use division by 1024 a lot so storing 1/1024 once and multiplying by that instead might improve fps

Instead of using functions called SinTable and CosTable you could use actual lookup tables... sin and cos are both slow. Or rename your functions because its confusing :P

Comments don't hurt too...

Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
There are a few things that can be optimized, apart from what Jheriko said:

1. "$Sin($calc($1 * $PI / 180))" can be replaced by "$sin($1).deg"

2. "%s = %s + 5" can be replaced by "inc %s 5"

But realistically, these are minor improvements; while this is a nifty snippet, calculations like these cannot be performed in mirc scripting fast enough to be of any use.


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
Joined: Sep 2003
Posts: 4,230
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
The problem isnt in the mirc draw commands at all, there quite fast enough for this. The problem exists in the while (!%m) { } inside the RayCast alias, this is where the bulk of the time is consummed.

I have made some adjustments, tried to squeeze out some more speed, didnt get that far tho. I left most other things unchanged becuase, well there just not significant enough to make a difference, the %z loop runs 61 times so any code thats no in a subloop of that istn going to make great speed differneces.

a list of the blue changes and he reason is at the bottom.

Code:
;=====================================================
; mIRC Raycaster v1.0 By: Talon
; This is a VERY slow raycasting engine!
; To try it out, just type "/raycaster"
;=====================================================
menu @RayCast {
  show rendering:hdel -w RayCast RenderMode
  Only show complete render:hadd -m RayCast RenderMode -n
}
on *:Close:@RayCast: { hfree RayCast }
alias raycaster {
  hfree -w RayCast
  window -p @RayCast 0 0 300 200
  if ($1 = 2) {
    var %xs = 24 , %ys = 24
    hadd -m RayCast Table.1 4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,7,6,7,6,7,6,7,6
    hadd -m RayCast Table.2 3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,0,0,0,0,0,0,7
    hadd -m RayCast Table.3 4,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6
    hadd -m RayCast Table.4 3,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7
    hadd -m RayCast Table.5 4,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,7,0,0,0,0,0,0,6
    hadd -m RayCast Table.6 3,0,4,0,0,0,0,5,4,5,4,5,4,5,4,5,6,7,0,8,6,7,6,7
    hadd -m RayCast Table.7 4,0,5,0,0,0,0,5,0,5,0,5,0,5,0,5,7,0,0,0,7,7,7,1
    hadd -m RayCast Table.8 3,0,6,0,0,0,0,5,0,0,0,0,0,0,0,5,6,0,0,0,0,0,0,8
    hadd -m RayCast Table.9 4,0,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,7,7,1
    hadd -m RayCast Table.10 3,0,8,0,0,0,0,5,0,0,0,0,0,0,0,5,7,0,0,0,0,0,0,8
    hadd -m RayCast Table.11 4,0,0,0,0,0,0,5,0,0,0,0,0,0,0,5,6,0,0,0,7,7,7,1
    hadd -m RayCast Table.12 3,0,0,0,0,0,0,5,5,5,5,0,5,5,5,5,7,7,7,7,7,7,7,2
    hadd -m RayCast Table.13 6,6,6,6,6,6,6,6,6,6,6,0,6,6,6,6,6,6,6,6,6,6,6,6
    hadd -m RayCast Table.14 8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4
    hadd -m RayCast Table.15 6,6,6,6,6,6,0,6,6,6,6,0,6,6,6,6,6,6,6,6,6,6,6,6
    hadd -m RayCast Table.16 4,4,4,4,4,4,0,4,4,4,6,0,6,2,2,2,2,2,2,2,3,3,3,3
    hadd -m RayCast Table.17 3,0,0,0,0,0,0,0,0,4,6,0,6,2,0,0,0,0,0,2,0,0,0,2
    hadd -m RayCast Table.18 4,0,0,0,0,0,0,0,0,0,0,0,6,2,0,0,5,0,0,2,0,0,0,3
    hadd -m RayCast Table.19 3,0,0,0,0,0,0,0,0,4,6,0,6,2,0,0,0,0,0,2,2,0,2,2
    hadd -m RayCast Table.20 4,0,6,0,6,0,0,0,0,4,6,0,0,0,0,0,5,0,0,0,0,0,0,3
    hadd -m RayCast Table.21 3,0,0,5,0,0,0,0,0,4,6,0,6,2,0,0,0,0,0,2,2,0,2,2
    hadd -m RayCast Table.22 4,0,6,0,6,0,0,0,0,4,6,-1,6,2,0,0,5,0,0,2,0,0,0,3
    hadd -m RayCast Table.23 3,0,0,0,0,0,0,0,0,4,6,0,6,2,0,0,0,0,0,2,0,0,0,2
    hadd -m RayCast Table.24 4,4,4,4,4,4,4,4,4,4,1,1,1,2,2,2,2,2,2,3,3,3,3,3
    hadd -m RayCast Angle 90
  }
  else {
    var %xs = 10 , %ys = 10
    hadd -m RayCast Table.1 2, 5, 3, 5, 2, 5, 3, 5, 2, 5
    hadd -m RayCast Table.2 5, 0, 0, 0, 0, 0, 0, 0, 0, 2
    hadd -m RayCast Table.3 3, 0, 3, 0, 0, 0, 3, 4, 0, 5
    hadd -m RayCast Table.4 5, 0, 2, 0, 0, 0, 4, 2, 0, 3
    hadd -m RayCast Table.5 2, 0, 3, 0, 0, 0, 0, 0, 0, 5
    hadd -m RayCast Table.6 5, 0, 2, 0,-1, 0, 0, 0, 0, 2
    hadd -m RayCast Table.7 3, 0, 0, 0, 0, 0, 7, 0, 6, 5
    hadd -m RayCast Table.8 5, 0,10,12,10,12, 0, 0, 0, 3
    hadd -m RayCast Table.9 2, 0, 0, 0, 0, 0, 0, 0, 0, 5
    hadd -m RayCast Table.10 5, 3, 5, 2, 5, 3, 5, 2, 5, 3
    hadd -m RayCast Angle 180
  }
  var %x = 0 
  while (%x &lt; %xs) { 
    inc %x
    var %y = 0 
    while (%y &lt; %ys) { 
      inc %y
      if ($RayMap(%x,%y) = -1) {
        RayMap %x %y 0 
        hadd -m RayCast PlayerX $calc(%x * 1024)
        hadd -m RayCast PlayerY $calc(%y * 1024)
      }
    }
  }
[color:blue]  ;
  var %i = 1 | while ($hget(RayCast,$+(Table.,%i))) { hadd RayCast $+(Table.,%i) $remove($v1,$chr(32)) | inc %i }
  ;[/color]
  raycast
}
alias RayMap { 
  if ($3 = $null) { return $gettok($hget(RayCast,$+(Table.,$1)),$2,44) }
  else { hadd -m RayCast $+(Table.,$1) $puttok($hget(RayCast,$+(Table.,$1)),$3,$2,44) }
}
[color:blue]alias SinTable { return $int($calc($Sin($1).deg * 1024 / 10)) }
alias CosTable { return $int($calc($Cos($1).deg * 1024 / 10)) }[/color]
alias RayCast {
  var %ticks = $ticks
  var %s = -5 , %a = 0 , %Angle = $hget(RayCast,Angle)
  var %z = [color:blue]-31[/color]
  var %SkyColor = 14
  var %FloorColor = 15
  while (%z &lt; 31) {
    inc %z
[color:blue]    ;titlebar @RayCast - Rendering... $round($calc(((%z + 31) / 62) * 100),2) $+ %[/color]
    %s = %s + 5
[color:blue]    %a = $calc((%Angle + %z + 360) % 360)
    var %xx = $hget(RayCast,PlayerX) / 1024
    var %yy = $hget(RayCast,PlayerY) / 1024
    var %StepX = $SinTable(%a) / 1024
    var %StepY = $CosTable(%a) / 1024
    var %dd = 0 , %ds = 0 , %m
    while (!$gettok($hget(RayCast,$+(Table.,$int(%xx))),%yy,44)) {
      dec %xx %StepX
      dec %yy %StepY
      inc %dd
    }
    %m = $gettok($hget(RayCast,$+(Table.,$int(%xx))),%yy,44)[/color]
    %ds = $int($calc(1000 / %dd))
    var %show = $hget(RayCast,RenderMode)
    drawline %show @RayCast %SkyColor 5 %s 0 %s $calc(99 - %ds)
    drawline %show @RayCast %m 5 %s $calc(100 - %ds) %s $calc(100 + %ds)
    drawline %show @RayCast %FloorColor 5 %s $calc(101 + %ds) %s 200
  }
  if (%show) { drawdot @RayCast }
  titlebar @RayCast - Done. $calc(($ticks - %ticks) / 1000) secs $round($calc(1000 / ($ticks - %ticks)),2) FPS
}
on *:KeyDown:@RayCast:*: {
  var %Angle = $hget(RayCast,Angle) , %m
  var %PlayerX = $hget(RayCast,PlayerX) 
  var %PlayerY = $hget(RayCast,PlayerY)

  if ($keyval = 37) { 
    ;========== left ==========
    %Angle = %Angle - 5
    hadd -m RayCast Angle %Angle
    raycast
  }
  if ($keyval = 38) { 
    ;========== up ==========
    %PlayerX = %PlayerX - $calc($SinTable(%Angle) * 5)
    %PlayerY = %PlayerY - $calc($CosTable(%Angle) * 5)
    %m = $RayMap($int($calc(%PlayerX / 1024)),$int($calc(%PlayerY / 1024)))
    if (!%m) {
      hadd -m RayCast PlayerX %PlayerX
      hadd -m RayCast PlayerY %PlayerY
      raycast
    }
  }
  if ($keyval = 39) { 
    ;========== right ==========
    %Angle = %Angle + 5
    hadd -m RayCast Angle %Angle
    raycast
  }
  if ($keyval = 40) { 
    ;========== down ==========
    %PlayerX = %PlayerX + $calc($SinTable(%Angle) * 5)
    %PlayerY = %PlayerY + $calc($CosTable(%Angle) * 5)
    %m = $RayMap($int($calc(%PlayerX / 1024)),$int($calc(%PlayerY / 1024)))
    if (!%m) {
      hadd -m RayCast PlayerX %PlayerX
      hadd -m RayCast PlayerY %PlayerY
      raycast
    }
  }
}


* changes in order of appearence.
(1) added a start up routine to remove spaces from the RayCast Table.N array vars (the spaces are saved into vars if accessed directly)
(2) updated the two alias to use .deg (thanks to qwerty)
(3) changed -30 to -31, seemed to make more sence, as this makes it go from -30 to +30 (might not be noticable)
(4) removed the title bar update (this wass a lag monster if ever there as one)
(5) updated the %a setting code
I divide all the needed vars by 1024 to begin with saves doing it over and over and over,
I acess the RayCast Table directly which increases performance a good deal > $gettok($hget(RayCast,$+(Table.,$int(%xx))),%yy,44)) > %xx needed to be $int() but since $gettok uses whole numbers the decimal portion of %yy could be ignored.
I used /DEC as its faster than using var = var - N
I had to set %m at the end since i wasnt wasting time setting it for each loop anymore


PS: its still to slow LOL!


Link Copied to Clipboard