Advertisement
osmarks

GoLFloor

Aug 23rd, 2018
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.64 KB | None | 0 0
  1. local y = 135
  2. local minx, maxx, minz, maxz = 2514, 2544, -1488, -1518
  3. local w, h = maxx - minx, maxz - minz
  4. local dead, alive = {"minecraft:concrete", 3}, {"minecraft:concrete", 0}
  5.  
  6. local function make_board()
  7. local board = {}
  8.     for x = 0, w do
  9.         board[x] = {}
  10.         for z = 0, h do
  11.             local pick = false
  12.             if math.random() < 0.5 then pick = true end
  13.             board[x][z] = pick
  14.         end
  15.     end
  16.     return board
  17. end
  18.  
  19. local function wrap(n, max)
  20.     return n % max
  21. end
  22.  
  23. local function get_neighbours(board, x, y, w, h)
  24.     local total = 0
  25.     for dx = -1, 1 do
  26.         for dy = -1, 1 do
  27.             if not (dx == 0 and dy == 0) then
  28.                 local thing = 0
  29.                 if board[wrap(x + dx, w)][wrap(y + dy, h)] then thing = 1 end
  30.                 total = total + thing
  31.             end
  32.         end
  33.     end
  34.     return total
  35. end
  36.  
  37. local function setblock(x, y, z, state)
  38.     local b
  39.     if state then b = alive else b = dead end
  40.     commands.execAsync(string.format("setblock %d %d %d %s %d", x, y, z, b[1], b[2]))
  41. end
  42.  
  43. local function update(board, new_board)
  44.     for x = 0, w do
  45.         for y = 0, h do
  46.             local alive_now = board[x][y]
  47.             local alive_next
  48.  
  49.             local neighbours = get_neighbours(board, x, y, w, h)
  50.  
  51.             if alive_now then
  52.                 alive_next = neighbours == 2 or neighbours == 3
  53.             else
  54.                 alive_next = neighbours == 3
  55.             end
  56.  
  57.             new_board[x][y] = alive_next
  58.         end
  59.     end
  60.     return new_board
  61. end
  62.  
  63. local function draw(board)
  64.     for x = 0, w do
  65.         for z = 0, h do
  66.             setblock(x + minx, y, z + minz, board[x][z])
  67.         end
  68.     end
  69. end
  70.  
  71. local b1, b2 = make_board(), make_board()
  72. local gens = 0
  73. while true do
  74.     draw(b1)
  75.     update(b1, b2)
  76.     b1, b2 = b2, b1
  77.     gens = gens + 1
  78.     if gens % 100 == 0 then b1 = make_board() end
  79.     sleep(1)
  80. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement