Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.25 KB | None | 0 0
  1. local runService = game:GetService("RunService")
  2. local rng = Random.new()
  3.  
  4. ----
  5.  
  6. local SIZE = 100
  7. local WHITE = BrickColor.White()
  8. local BLACK = BrickColor.Black()
  9.  
  10. local OFFSETS = {
  11.     {-1, -1},
  12.     {0, -1},
  13.     {1, -1},   
  14.     {-1, 1},
  15.     {0, 1},
  16.     {1, 1},
  17.     {-1, 0},
  18.     {1, 0},
  19. }
  20.  
  21. ----
  22.  
  23. local function getPart(x, y)
  24.     local part = Instance.new("Part")
  25.     part.Anchored = true
  26.     part.CanCollide = false
  27.     part.Size = Vector3.new(1, 1, 1)
  28.     part.CFrame = CFrame.new(x, 0.5, y)
  29.     part.BrickColor = BLACK
  30.     part.Parent = workspace
  31.    
  32.     return part
  33. end
  34.  
  35. local tileList = {}
  36. local boardGrid = {}
  37.  
  38. for x = 1, SIZE, 1 do
  39.     boardGrid[x] = {}
  40.    
  41.     for y = 1, SIZE, 1 do
  42.         boardGrid[x][y] = {
  43.             State = false,
  44.             Next = false,
  45.            
  46.             Position = {x, y},     
  47.             Part = getPart(x, y),
  48.             Neighbors = {}
  49.         }
  50.     end
  51. end
  52.  
  53. for originX = 1, SIZE, 1 do
  54.     for originY = 1, SIZE, 1 do
  55.         local tile = boardGrid[originX][originY]
  56.  
  57.         for offsetIndex = 1, 8, 1 do
  58.             local offset = OFFSETS[offsetIndex]            
  59.             local x = originX + offset[1]
  60.             local y = originY + offset[2]
  61.            
  62.             if x == 0 then
  63.                 x = SIZE
  64.             elseif x == SIZE + 1 then
  65.                 x = 1
  66.             end
  67.            
  68.             if y == 0 then
  69.                 y = SIZE
  70.             elseif y == SIZE + 1 then
  71.                 y = 1
  72.             end
  73.            
  74.             table.insert(tile.Neighbors, boardGrid[x][y])
  75.         end
  76.        
  77.         table.insert(tileList, tile)
  78.     end
  79. end
  80.  
  81. ---
  82.  
  83. --[[ GLIDER GUN
  84. local gliderGun = {
  85.     -- left box
  86.     {2, 6}, {2, 7},
  87.     {3, 6}, {3, 7},
  88.    
  89.     -- left ship
  90.     {12, 6}, {12, 7}, {12, 8},
  91.     {13, 5}, {13, 9},
  92.     {14, 4}, {14, 10},
  93.     {15, 4}, {15, 10},
  94.     {16, 7},
  95.     {17, 5}, {17, 9},
  96.     {18, 6}, {18, 7}, {18, 8},
  97.     {19, 7},
  98.    
  99.     -- right ship
  100.     {22, 4}, {22, 5}, {22, 6},
  101.     {23, 4}, {23, 5}, {23, 6},
  102.     {24, 3}, {24, 7},
  103.    
  104.     {26, 2}, {26, 3}, {26, 7}, {26, 8},
  105.    
  106.     -- left box
  107.     {36, 4}, {36, 5},
  108.     {37, 4}, {37, 5}
  109. }
  110.  
  111. for _, coord in next, gliderGun do
  112.     local x, y = unpack(coord)
  113.     local tile = boardGrid[x][y]
  114.    
  115.     --tile.State = true
  116.     tile.Next = true
  117. end
  118. --]]
  119.  
  120. -- RNG EVERYTHING
  121. for index, tile in next, tileList do
  122.     tile.Next = rng:NextNumber() > 0.5
  123. end
  124. --]]
  125.  
  126. --[[ BIG CIRCLE
  127. for index, tile in next, tileList do
  128.     local x, y = unpack(tile.Position)
  129.    
  130.     x = x - (SIZE * 0.5)
  131.     y = y - (SIZE * 0.5)
  132.    
  133.     local mag = math.sqrt((x * x) + (y * y))
  134.    
  135.     if mag < SIZE * 0.25 then
  136.         tile.Next = rng:NextNumber() > 0.5
  137.     end
  138. end
  139. --]]
  140.  
  141. ----
  142.  
  143. local function setNextTileState(tile)
  144.     local nearLife = 0
  145.    
  146.     for index = 1, 8, 1 do
  147.         if tile.Neighbors[index].State then
  148.             nearLife = nearLife + 1
  149.         end
  150.        
  151.         -- no case deals with more life than this
  152.         if nearLife > 3 then
  153.             break
  154.         end
  155.     end
  156.    
  157.     -- the rules of life
  158.     if tile.State then
  159.         if nearLife < 2 or nearLife > 3 then
  160.             tile.Next = false
  161.         end
  162.     else
  163.         if nearLife == 3 then
  164.             tile.Next = true
  165.         end
  166.     end
  167. end
  168.  
  169. local function stepBoard() 
  170.     -- set next states
  171.     for index = 1, SIZE * SIZE do
  172.         setNextTileState(tileList[index])  
  173.     end
  174.    
  175.     -- comit next states and draw changes
  176.     for index = 1, SIZE * SIZE do
  177.         local tile = tileList[index]       
  178.        
  179.         if tile.Next ~= tile.State then
  180.             if tile.Next then
  181.                 tile.Part.BrickColor = WHITE
  182.             else
  183.                 tile.Part.BrickColor = BLACK
  184.             end
  185.         end
  186.                
  187.         tile.State = tile.Next
  188.     end
  189. end
  190.  
  191. ----
  192.  
  193. while runService.Heartbeat:Wait() do
  194.     stepBoard()    
  195. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement