Log In  
Log In  

Just a small snippet from a token-saving discussion on the Discord last night.

If you need to iterate over neighboring tiles (for example when writing a path finding algorithm for 7DRL), this natural approach is pretty token heavy:

-- four directions, 29 tokens
for direction in all{{-1,0},{0,-1},{1,0},{0,1}} do
 local x,y=direction[1],direction[2]
end

-- eight directions, 45 tokens
for direction in all{{-1,0},{0,-1},{1,0},{0,1},{1,1},{-1,-1},{1,-1},{-1,1}} do
 local x,y=direction[1],direction[2]
end

-- eight directions, 43 tokens
directions={0,-1,-1,0,1,0,0,1,1,-1,-1,1,1,1,-1,-1}
for i=1,16,2 do
 local x,y=directions[i],directions[i+1]
end

-- eight directions, 30 tokens
directions={-1,0,1}
for x in all(directions) do
 for y in all(directions) do
  if x!=0 or y!=0 then
   --
  end
 end
end

Why not use trigonometry?

-- four directions, 16 tokens
for i=0,1,0.25 do
 local x,y=cos(i),sin(i)
end

-- eight directions, 24 tokens
for i=0.125,1,0.125 do
 local x,y=flr(cos(i)+.5),flr(sin(i)+.5)
end
P#62065 2019-02-19 10:00 ( Edited 2019-02-19 10:14)

lol

P#62781 2019-03-12 22:37
:: Xii
1

Clever!

Got you beat tho ^_^

-- eight directions, 23 tokens
directions=explodeval("0,-1,-1,0,1,0,0,1,1,-1,-1,1,1,1,-1,-1")
for i=1,16,2 do
 local x,y=directions[i],directions[i+1]
end

Provided, of course, you have this defined (which you absolutely should, because it's universally useful):

function explode(s)
 local retval,lastpos={},1
 for i=1,#s do
  if sub(s,i,i)=="," then
   add(retval,sub(s, lastpos, i-1))
   i+=1
   lastpos=i
  end
 end
 add(retval,sub(s,lastpos,#s))
 return retval
end

function explodeval(_arr)
 return toval(explode(_arr))
end
P#62792 2019-03-13 08:03

[Please log in to post a comment]

About | Contact | Updates | Terms of Use
Follow Lexaloffle:        
Generated 2019-05-27 14:45 | 0.024s | 2097k | Q:13