Author Topic: [Lua] RayCaster  (Read 44044 times)

0 Members and 1 Guest are viewing this topic.

Offline critor

  • Editor
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2079
  • Rating: +439/-13
    • View Profile
    • TI-Planet
Re: [Lua] RayCaster
« Reply #105 on: August 07, 2011, 05:16:56 pm »
So we need a real TI-Nspire CX emulator...
Yep. I had the student software, but my license expired and I didn't feel like buying it. I wonder if it would be easy to add color support in the emu?

It's more complicated than that, as the current Nspire emulator just doesn't run the CX 3.0 Boot2, which is encrypted unlike non-CX Boot2 versions.
« Last Edit: August 07, 2011, 05:17:24 pm by critor »
TI-Planet co-admin.

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Re: [Lua] RayCaster
« Reply #106 on: August 07, 2011, 05:24:42 pm »
I thought we had to decrypt the boot2 anyways for the emulator. Or is this a new kind of encryption?
Also, kinda off topic, but if the calc has 16 bit color, that means updating the screen from a screen buffer will take 4x as long.
« Last Edit: August 07, 2011, 05:26:37 pm by t0xic_kitt3n »

Offline critor

  • Editor
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2079
  • Rating: +439/-13
    • View Profile
    • TI-Planet
Re: [Lua] RayCaster
« Reply #107 on: August 07, 2011, 05:47:52 pm »
I thought we had to decrypt the boot2 anyways for the emulator. Or is this a new kind of encryption?
Also, kinda off topic, but if the calc has 16 bit color, that means updating the screen from a screen buffer will take 4x as long.

Non-CX Boot2 were encoded and had to be decoded, not decrypted.

The CX Boot2 is encoded AND encrypted. This is much worse. It has to be decoded and decrypted (which requires some key).
It is probably decrypted by the CX Boot1 we'll never be able to link legally online (unlike the Boot2 which just needs to be extracted from any OS installation file).
« Last Edit: August 07, 2011, 06:09:08 pm by critor »
TI-Planet co-admin.

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: [Lua] RayCaster
« Reply #108 on: August 07, 2011, 05:51:27 pm »
Hmm I see... I hope this doesn't compromise the arrival of a CX emu in the future...

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Re: [Lua] RayCaster
« Reply #109 on: August 07, 2011, 06:38:58 pm »
It's illegal to decrypt stuff now?

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: [Lua] RayCaster
« Reply #110 on: August 07, 2011, 09:13:09 pm »
Yeah, it may be possible to reverse engineer the boot1 to make a decryption routine built into the emulator. Would that be sufficiently legal?
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Re: [Lua] RayCaster
« Reply #111 on: August 07, 2011, 09:17:03 pm »
I wouldn't be surprised if TI does the same thing as the signing key controversy if that happens.

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: [Lua] RayCaster
« Reply #112 on: August 07, 2011, 09:18:16 pm »
But they had no real ground to stand on in that case, and they might not in this case either.
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Re: [Lua] RayCaster
« Reply #113 on: August 07, 2011, 09:22:34 pm »
Well, I could see them trying to pose the encrypted code as DRM, and then saying we are circumventing it which is apparently illegal in the DMCA.

Offline NecroBumpist

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 130
  • Rating: +14/-5
  • Master of Lua
    • View Profile
Re: [Lua] RayCaster
« Reply #114 on: August 19, 2011, 01:08:30 am »
While I know this thread is somewhat dated, and the topic really has shifted more towards the CX's DRM, I thought I would highlight the importance of basic Lua optimizations.

I ran the code from the first page (slightly modified, included a timer call so I could get a consistent frame rate), and it ran at 4 to 5 frames per second.
I modify the code some (only localizes variables and common functions), and then I can get 6 - 7 frames per second.
This doesn't change the algorithm at all, though that could probably be improved.

Code: (minor optimizations) [Select]

local timer_stop = timer.stop;
local timer_start = timer.start;
local window = platform.window;
local invalidate = window.invalidate;
local w, h, planeX, planeY, map;


function on.timer()
timer_stop();
invalidate(window);
end

local math_abs = math.abs;
local math_floor = math.floor;
local math_cos, math_sin = math.cos, math.sin;

local function racine(a)
local x=1
local y=0.5*(1+a)
while math_abs(y-x)>0.001 do
x=y
y=0.5*(x+a/x)
end
return y
end

local function renderMap(gc,map,w,h,camera)
local math_cos, math_sin = math_cos, math_sin;
local dirX, dirY, rayPosX, rayPosY, rayDirX, rayDirY, mapX, mapY, deltaDistX,
deltaDisY, hit, stepX, sideDistX, stepY, sideDistY;
for x=0,w,2 do
dirX=math_cos(camera[3])
dirY=math_sin(camera[3])
planeX=math_cos(camera[3]-1.5707963267949)
planeY=math_sin(camera[3]-1.5707963267949)
cameraX=(x*2/w)-1
rayPosX=camera[1]
rayPosY=camera[2]
rayDirX=dirX+planeX*cameraX
rayDirY=dirY+planeY*cameraX
mapX=math_floor(rayPosX)
mapY=math_floor(rayPosY)
deltaDistX=racine(1+(rayDirY*rayDirY)/(rayDirX*rayDirX))
deltaDistY=racine(1+(rayDirX*rayDirX)/(rayDirY*rayDirY))
hit=0

if rayDirX<0 then
stepX=-1
sideDistX=(rayPosX-mapX)*deltaDistX
else
stepX = 1;
sideDistX = (mapX+1-rayPosX)*deltaDistX
end
if rayDirY<0 then
stepY=-1;
sideDistY=(rayPosY-mapY)*deltaDistY
else
stepY=1;
sideDistY=(mapY+1-rayPosY)*deltaDistY
end


while hit==0 do
if sideDistX<sideDistY then
sideDistX=sideDistX+deltaDistX
mapX=mapX+stepX
side=0
else
sideDistY=sideDistY+deltaDistY
mapY=mapY+stepY
side=1
end

if map[mapX][mapY]>0 then
hit=1
end
end

local perpWallDist;

if side==0 then
perpWallDist=math_abs((mapX-rayPosX+(1-stepX)/2)/rayDirX)
else
perpWallDist=math.abs((mapY-rayPosY+(1-stepY)/2)/rayDirY)
end
local hauteurLigne=math.abs(math_floor(h/perpWallDist))
local drawStart=math_floor(-hauteurLigne/2+h/2)
local drawEnd=math_floor(hauteurLigne/2+h/2)
if drawStart<0 then
drawStart=0
end
if drawEnd>=h then
drawEnd=h-1
end

gc:setColorRGB(10,10,10)
if side==1 then
gc:setColorRGB(100,100,100)
end
gc:fillRect(x,drawStart,2,drawEnd-drawStart)
end
end


local menu;
function on.escapeKey()
menu=1
invalidate(window);
end
on.escapeKey()

function on.enterKey()
menu=nil
--fovy=60
camera={10,10,0.05}
w=320 h=240
planeX=0 planeY=1
map={{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},{1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,1,0,0,1,1,1,1,1,1,1},{1,0,0,1,1,1,1,0,0,1,0,0,0,0,0,1},{1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1},{1,0,0,0,0,0,1,0,0,1,0,0,1,0,0,1},{1,1,1,1,1,1,1,0,0,1,0,0,1,0,0,1},{1,0,0,1,0,0,0,0,0,0,0,0,1,1,1,1},{1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1},{1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1},{1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1},{1,0,0,1,1,1,0,0,1,1,1,1,1,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}}
invalidate(window);
end

function on.arrowKey(ar)
turn=nil
if ar=="right" then
camera[3]=camera[3]-0.1
end
if ar=="left" then
camera[3]=camera[3]+0.1
end
if ar=="up" then
if map[math_floor(camera[1]+math_cos(camera[3])/3)][math_floor(camera[2]+math_sin(camera[3])/3)]==0 then
camera[1]=camera[1]+math_cos(camera[3])/3
camera[2]=camera[2]+math_sin(camera[3])/3
end
end
if ar=="down" then
if map[math_floor(camera[1]-math_cos(camera[3])/4)][math_floor(camera[2]-math_sin(camera[3])/4)]==0 then
camera[1]=camera[1]-math_cos(camera[3])/4
camera[2]=camera[2]-math_sin(camera[3])/4
end

end
invalidate(window);
end

local milli = timer.getMilliSecCounter;
local last = 0;

function on.paint(gc)
if menu then
gc:setColorRGB(0,0,255)
gc:setFont("sansserif","r",30)
gc:drawString("RayCaster Demo",30,0,"top")
gc:setFont("serif","b",10)
gc:drawString("Press Enter to start",100,100,"top")
else
gc:setColorRGB(200,200,200)
gc:fillRect(0,0,platform.window:width(),platform.window:height())
renderMap(gc,map,platform.window:width(),platform.window:height(),camera)
end
gc:setColorRGB(0,0,0)
gc:setFont("sansserif","r",8)
gc:drawString("FPS: " .. tostring(1000/(milli()-last)),10,200,"top")
last = milli();
timer_start(0.01);
end
Developing Lua scripts for the NSpire ?
Check out the Necrotorium
Need a few routines to run faster ? Checkout the MODS Lua Assembly Toolkit.
Need to save space for your scripts ? Checkout LuaSrcDiet

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: [Lua] RayCaster
« Reply #115 on: August 19, 2011, 07:00:35 am »
Very good example NecroBumpist :)
I recommend people this guide for optimizing their code: http://trac.caspring.org/wiki/LuaPerformance .

Offline Chockosta

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 447
  • Rating: +169/-6
    • View Profile
Re: [Lua] RayCaster
« Reply #116 on: August 27, 2011, 06:33:59 am »
Thank you for your help, NecroBumpist !
Unfortunately, you optimized the old version, which was a bit slower. I will try to improve the new one.

I didn't know localization was that faster.