• 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.X+ Easiest check of items in a 3x3

Mjmackan

Mapper ~ Writer
Premium User
Joined
Jul 18, 2009
Messages
1,424
Solutions
15
Reaction score
177
Location
Sweden
Alo! I'm looking for the best/shortest way to check for a certain item in a 3x3 shown as below:
check.png

My original idea was to write out every position and then do a for-loop with an if statement inside, something like:
Lua:
local purplePos = {
{x,y,z}, {x,y,z}, {x,y,z},
{x,y,z}, {x,y,z}, {x,y,z},
{x,y,z}, {x,y,z}, {x,y,z},
}
for i = 1,9 do
 if getTileThingByPos(purplePos[i]).itemid ~= 9564 then
     return false
 end
end

(My first thought however is that the positions could be fetched in a better way, but not sure how.)
 
explain what need and not what you're trying to do, it will make it easier for someone to give you the optimal solution.

If you want to check if there's 3 similar colors in a line/column the solution is one, if you just want to iterate the 3x3 square I'd say you can just pass the corner position and do 2 loops:


Lua:
local initialPos = {x = 100, y = 50, z = 7}
for i = 0, 2 do
    for j = 0, 2 do
        local tempPos = {x = initialPos.x + i, y = initialPos.y + j, z = initialPos.z}  
       --- Pick the item from temp position and make your check here
   end
end
 
so u only want to check if from pos to pos has the current item inside?
or what exactly u mean?
Thats exactly what I want, excuse my poor explaining.
explain what need and not what you're trying to do, it will make it easier for someone to give you the optimal solution.

If you want to check if there's 3 similar colors in a line/column the solution is one, if you just want to iterate the 3x3 square I'd say you can just pass the corner position and do 2 loops:


Lua:
local initialPos = {x = 100, y = 50, z = 7}
for i = 0, 2 do
    for j = 0, 2 do
        local tempPos = {x = initialPos.x + i, y = initialPos.y + j, z = initialPos.z}
       --- Pick the item from temp position and make your check here
   end
end
I want to check if there's a certain item on all tiles inside the 3x3 with as little code as possible, so your double loop is an awesome optimization.
 
Back
Top