-2

I am working on a Lua script(cell automation) and now I need to divide the cellularspace into 4 parts, each part has its own function, I use the following code (might looks stupid to some of you) :

if  cells:getCell(Coord{x < 25 ,y < 25}) then
cell.P = (cell.past.P + e*i1 + u1*i2)
elseif cells:getCell(Coord{x < 25 ,y > 25})then
cell.P = (cell.past.P + e*i1 + u2*i2)
elseif cells:getCell(Coord{x > 25 ,y < 25})then
cell.P = (cell.past.P + e*i1 + u3*i2)
else
cell.P = (cell.past.P + e*i1 + u4*i2)
end

Now I would like ask what is the right way to rewrite the above code? Any functions? Thank you!

1 Answers1

0

How about this:

do
  local t = {
    { {x < 25, y < 25}, u1 },
    { {x < 25, y > 25}, u2 },
    { {x > 25, y < 25}, u3 },
    { {x > 25, y > 25}, u4 },
  }

  for i = 1, 4 do
    if cells:getCell(Coord(t[i][1])) then
      cell.P = (cell.past.P + e*i1 + t[i][2]*i2)
      break
    end
  end
end

It's not much shorter, but at least there is a lot less code repetition.

Brian Bowman
  • 892
  • 6
  • 9