leo3065

Factorio GoL Script

Jun 5th, 2016
938
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.42 KB | None | 0 0
  1. px=-143
  2. py=-3
  3. sx=40
  4. sy=40
  5.  
  6. surface=game.get_surface(1)
  7.  
  8. function clear(px,py,sx,sy)
  9.  for k,v in pairs(surface.find_entities_filtered({area = {{px-1, py-1}, {px+sx+1, py+sy+1}}, name= "coal"})) do
  10.   v.destroy()
  11.  end
  12.  for k,v in pairs(surface.find_entities_filtered({area = {{px-1, py-1}, {px+sx+1, py+sy+1}}, name= "quartz"})) do
  13.   v.destroy()
  14.  end
  15. end
  16.  
  17. function put(i,n,x,y)
  18.  surface.create_entity({name=i, amount=n, position={x, y}})
  19. end
  20.  
  21. function check(i,x,y)
  22.  el=#surface.find_entities_filtered({area = {{x, y}, {x+1, y+1}}, name=i})
  23.  return el
  24. end
  25.  
  26. function near(patt,x,y)
  27.  c=0
  28.  for n=-1,1 do
  29.   for m=-1,1 do
  30.    c=c+patt[(y+m+sy-1)%sy+1][(x+n+sx-1)%sx+1]
  31.   end
  32.  end
  33.  c=c-patt[y][x]
  34.  return c
  35. end
  36.  
  37. function cycle(patt,x,y)
  38.  s=patt[y][x]
  39.  n=near(patt,x,y)
  40.  if (s==1 and n==2) or (n==3) then
  41.   return 1
  42.  else
  43.   return 0
  44.  end
  45. end
  46.  
  47. patt={}
  48. for y=1,sy do
  49.  row={}
  50.  for x=1,sx do
  51.   row[x]=check("quartz",x+px,y+py)
  52.   if check("wooden-chest",x+px,y+py)==1 then
  53.    surface.find_entities_filtered({area = {{x+px, y+py}, {x+px+1, y+py+1}}, name="wooden-chest"})[1].destroy()
  54.    row[x]=1-row[x]
  55.   end
  56.  patt[y]=row
  57.  end
  58. end
  59.    
  60. pattT={}
  61. for y=1,sy do
  62.  row={}
  63.  for x=1,sx do
  64.   row[x]=cycle(patt,x,y)
  65.  end
  66.  pattT[y]=row
  67. end
  68.  
  69. clear(px,py,sx,sy)
  70.  
  71. for y=1,sy do
  72.  for x=1,sx do
  73.   if pattT[y][x]==1 then
  74.    put("quartz",1000,x+px,y+py)
  75.   else
  76.    put("coal",1000,x+px,y+py)
  77.   end
  78.  end
  79. end
Advertisement
Add Comment
Please, Sign In to add comment