blizzard (86 tokens)
(207 chars)
p = {}
_set_fps"60"
::_::
cls"13"
for i=1,256 do
local r,_ENV=rnd,p[i] or add(p,{})
if(x) x,y = x%136-3*z-sin(y/64),y%136+2*z line(x,y,x+2*z,y-2*z,6+z) else x,y,z=r"128",r"128",0.2+r"0.9"
end
flip()
goto _
|
slightly more readable version:
particles = {}
for i=1,256 do --init
local r,_ENV=rnd,add(particles,{}) --rnd() needs to be redefined under _ENV
x, y, z=r(128), r(128), 0.2+r(0.9)
end
function _update60() end --just here for the FPS
function _draw() --process
cls(13)
foreach(particles, function(p)
local _ENV=p --but for some reason line() does not
x, y = x%136-3*z-sin(y/64), y%136+2*z
line(x,y,x+2*z,y-2*z,6+z)
end)
extcmd("set_title",stat(7).."FPS "..stat(1).." CPU ") --debug shtuff
end |
some notes and tricks:
-
utilizing _ENV is a very obtuse and complicated feature, but saving a token with every variable (x = y, 3T) vs. (scope.x = y, 4T) adds up in the long run.
-
this uses about 0.14 CPU for 256 particles. i had initially tried to use static shapes with print() but that ended up using more than twice as much - it's a very cycle heavy command. i think the more dynamic shapes of line() came out better in the long run.
-
order of operations is still a strange concept to me. apparently modulo comes first.
-
the code in this comment doesn't utilize it, but every function you can call with a single, static value (cls(13), rnd(0.9), etc) can be called with an auto-converted string - () can be substituted for "" to save another token. (rnd(128), 3T) vs. (rnd"128", 2T)
- ARRAYS START AT 1. DON't go "for i=0,255". GRIEVOUS ERROR
Looks amazing! Great job
I feel there is a story behind your last comment about arrays haha
CPU-efficient version (without _ENV):
particles = {}
for i=1,256 do --init
add(particles,{rnd"128",rnd"128",0.2+rnd"0.9"})
end
function _update60() end --just here for the FPS
function _draw() --process
cls(13)
for i=1,256 do
local p=particles[i]
local x,y,z = p[1],p[2],p[3]
p[1],p[2] = x%136-3*z-sin(y/64),y%136+2*z
line(x,y,x+2*z,y-2*z,6+z)
end
extcmd("set_title",stat(7).." FPS | "..stat(1).." CPU | "..#particles) --debug shtuff
end |
-
doing all the stuff the original version does probably saves on tokens, but it also spends 50% more CPU time - this version runs consistently at ~0.1042.
-
foreach() is not faster over
for i=1,256. -
unpack() is not faster over
x,y,z = p[1],p[2],p[3]. -
_ENV is certainly not faster. just in general.
-
you can add() a pre-compiled table
-
you can't assign p and its sub-variables on the same line
-
slightly more token expensive (86 vs. 117)
- since particles are only initialized once, there's no fear of accidentally putting
for i=0,255and creating an infinite number of zero-index particles that are never drawn
[Please log in to post a comment]




