• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[TFS 1.0] Position.iterateArea

Crypton3

Retired
Joined
Mar 13, 2010
Messages
549
Reaction score
139
Simple and useful function for executing other function on all tiles of specified area.
Code:
function Position.iterateArea(func, from, to)
   for z = to.z, from.z do
     for y = from.y, to.y do
       for x = from.x, to.x do
         func(Position(x, y, z))
       end
     end
   end
end

Usage:
Code:
Position.iterateArea(function(position)
  position:sendMagicEffect(CONST_ME_POFF)
end, {x = 1000, y = 980, z = 8}, {x = 1100, y = 990, z = 8})
Sends magic effect on all tiles of the area.

@ down
Yeah, I forgot about it. It'll be a bit faster now :)
 
Last edited:
z should be at the 1st for loops, unless your intention is to repeat z, also this could hang the server

Code:
local a  = {x = 1, y = 1, z = 10}
local b = {x = 2, y = 2, z = 1}


function iterateArea(from, to)
   for x = from.x, to.x do
     for y = from.y, to.y do
       for z = to.z, from.z do
         print(" x "..x, " y "..y, " z "..z)
       end
     end
   end
end


iterateArea(a, b)

Code:
x 1    y 1    z 1
 x 1    y 1    z 2
 x 1    y 1    z 3
 x 1    y 1    z 4
 x 1    y 1    z 5
 x 1    y 1    z 6
 x 1    y 1    z 7
 x 1    y 1    z 8
 x 1    y 1    z 9
 x 1    y 1    z 10
 x 1    y 2    z 1
 x 1    y 2    z 2
 x 1    y 2    z 3
 x 1    y 2    z 4
 x 1    y 2    z 5
 x 1    y 2    z 6
 x 1    y 2    z 7
 x 1    y 2    z 8
 x 1    y 2    z 9
 x 1    y 2    z 10
 x 2    y 1    z 1
 x 2    y 1    z 2
 x 2    y 1    z 3
 x 2    y 1    z 4
 x 2    y 1    z 5
 x 2    y 1    z 6
 x 2    y 1    z 7
 x 2    y 1    z 8
 x 2    y 1    z 9
 x 2    y 1    z 10
 x 2    y 2    z 1
 x 2    y 2    z 2
 x 2    y 2    z 3
 x 2    y 2    z 4
 x 2    y 2    z 5
 x 2    y 2    z 6
 x 2    y 2    z 7
 x 2    y 2    z 8
 x 2    y 2    z 9
 x 2    y 2    z 10
 
@artofwork
Calling the function with wrong parameters will cause unintended behavior, but it will not hang the server, the loop will just not execute at all.
 
@Summ
Your always trying to correct me on things as if you know more then me, my knowledge far exceeds this framework and the it's supporting languages.
This isn't exactly a community of learning, I've used frameworks where we were forced to build everything from the ground up, where we had to know the actual languages and concepts to produce anything meaningful.
Here people just request things and someone writes it for them, instead of making them think about how they could build it themselves.
Lua is not just limited to what we see here on these forums everyday it is a very powerful language capable of so many great things.

@Topic
My original post was directed to @Crypton3 where he had x instead of z in the 1st for loop and he had corrected that, but then @Summ you jumped in and gave your 2 cents not knowing he switch the parameters.. anyway whatever keep on trucking.
 
There is no scenario in which it would hang the server, but you are right anyway.
In the future I will just ignore your posts (and unrelated stories) and we are fine.
 
Back
Top