Advertisement
BobMe

gameoflife

May 24th, 2021 (edited)
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.09 KB | None | 0 0
  1. -- john conway's game of life
  2.  
  3. -- SETTINGS:
  4. local cellsize = 1 -- per stud
  5. local halttime = 0.25 -- miliseconds (0 for as soon as possible)
  6. local yoffset = 0 -- how high the cells will be placed off of the ground (0 is default for MSB base)
  7. --
  8. local cells = {}
  9. local folder = Instance.new("Folder",script)
  10. local icount = 0
  11. -- drawing state:
  12.  
  13. local begin = false
  14. owner.Chatted:Connect(function(msg)
  15. print(msg)
  16. if string.lower(msg) == ";begin" then
  17. print("begin said")
  18. begin = true
  19. print("begin made true")
  20. elseif string.lower(msg) == ";clr" then
  21. cells = {}
  22. drawScene()
  23. end
  24. end)
  25.  
  26. local function drawScene()
  27. folder:ClearAllChildren()
  28. for i,v in pairs(cells) do
  29. local x = v[1]*cellsize
  30. local y = v[2]*cellsize
  31. if v[3] then
  32. local part = Instance.new("MeshPart",folder)
  33. part.Size = Vector3.new(cellsize,1.5,cellsize)
  34. part.Position = Vector3.new(x,0,y)
  35. part.Anchored = true
  36. else
  37. local part = Instance.new("MeshPart",folder)
  38. part.Size = Vector3.new(cellsize,1.5,cellsize)
  39. part.Position = Vector3.new(x,0,y)
  40. part.Anchored = true
  41. part.Color = Color3.fromRGB(0,0,0)
  42. part.Transparency = 0.7
  43. end
  44. end
  45. end
  46.  
  47. local function cellValue(x,y)
  48. for i,v in pairs(cells) do
  49. if v[1] == x and v[2] == y then
  50. v[4] = icount
  51. return v[3]
  52. end
  53. end
  54. table.insert(cells,{x,y,false,icount})
  55. return false
  56. end
  57.  
  58. local function cellValue2(x,y)
  59. for i,v in pairs(cells) do
  60. if v[1] == x and v[2] == y then
  61. return v[3]
  62. end
  63. end
  64. return false
  65. end
  66.  
  67. local function getCell(x,y)
  68. for i,v in pairs(cells) do
  69. if v[1] == x and v[2] == y then
  70. v[4] = icount
  71. return i
  72. end
  73. end
  74. table.insert(cells,{x,y,false,icount})
  75. return #cells
  76. end
  77.  
  78. local function getCell2(x,y)
  79. for i,v in pairs(cells) do
  80. if v[1] == x and v[2] == y then
  81. return i
  82. end
  83. end
  84. return nil
  85. end
  86.  
  87. local function removeCell(x,y)
  88. for i,v in pairs(cells) do
  89. if v[1] == x and v[2] == y then
  90. table.remove(cells,i)
  91. end
  92. end
  93. end
  94.  
  95. local function testCell(x,y)
  96. local state = cellValue2(x,y)
  97. local cellnumber = getCell2(x,y)
  98. local proxcells = {}
  99. if state then
  100. proxcells = {
  101. cellValue(x,y-1),
  102. cellValue(x,y+1),
  103. cellValue(x-1,y-1),
  104. cellValue(x-1,y),
  105. cellValue(x-1,y+1),
  106. cellValue(x+1,y-1),
  107. cellValue(x+1,y+1),
  108. cellValue(x+1,y)
  109. }
  110. else
  111. proxcells = {
  112. cellValue2(x,y-1),
  113. cellValue2(x,y+1),
  114. cellValue2(x-1,y-1),
  115. cellValue2(x-1,y),
  116. cellValue2(x-1,y+1),
  117. cellValue2(x+1,y-1),
  118. cellValue2(x+1,y+1),
  119. cellValue2(x+1,y)
  120. }
  121. end
  122. local amounttru = 0
  123. local amountfal = 0
  124. for i,v in pairs(proxcells) do
  125. if v then amounttru = amounttru + 1 else amountfal = amountfal + 1 end
  126. end
  127. if not state then
  128. if amounttru == 3 then
  129. return true
  130. elseif amounttru == 0 then
  131. return false
  132. else
  133. return false
  134. end
  135. else
  136. if amounttru < 2 then
  137. return false
  138. elseif amounttru > 3 then
  139. return false
  140. else
  141. return true
  142. end
  143. end
  144. end
  145.  
  146. local RunService = game:GetService("RunService")
  147. local function AccurateWait(seconds)
  148. if not seconds then
  149. seconds = 0
  150. end
  151. local last = tick()
  152. local secondsPassed = 0
  153. local connection = RunService.Heartbeat:Connect(function(dt)
  154. secondsPassed = secondsPassed + dt
  155. end)
  156. repeat
  157. RunService.Heartbeat:Wait()
  158. until secondsPassed >= seconds
  159. connection:Disconnect()
  160. end
  161.  
  162. local function deepCopy(original)
  163. local copy = {}
  164. for k, v in pairs(original) do
  165. if type(v) == "table" then
  166. v = deepCopy(v)
  167. end
  168. copy[k] = v
  169. end
  170. return copy
  171. end
  172. local stop = false
  173. local remote = owner:FindFirstChild("gameoflife")
  174. if remote == nil then remote = Instance.new("RemoteEvent",owner) end
  175. remote.Name = "gameoflife"
  176. local lastx, lastz = nil,nil
  177. remote.OnServerEvent:Connect(function(plr,pos,sto,mousedown)
  178. stop = sto
  179. if mousedown then
  180. local x = pos.X
  181. local z = pos.Z
  182. x = x-(x%cellsize)
  183. z = z-(z%cellsize)
  184. local dx,dz = x/cellsize,z/cellsize
  185. if (dx ~= lastx or dz ~= lastz) then
  186. lastx = dx
  187. lastz = dz
  188. local cellval = cellValue(dx,dz)
  189. local cellget = getCell(dx,dz)
  190. cells[cellget][3] = not cells[cellget][3]
  191. drawScene()
  192. end
  193. end
  194. end)
  195.  
  196. NLS([[
  197. local plr = owner
  198. local mouse = plr:GetMouse()
  199. local mousedown = false
  200. local begin = false
  201. local stop = false
  202. local remote = owner:WaitForChild("gameoflife")
  203. mouse.Button1Down:Connect(function()
  204. mousedown = true
  205. end)
  206. mouse.Button1Up:Connect(function()
  207. mousedown = false
  208. end)
  209. mouse.KeyUp:Connect(function(key)
  210. if string.lower(key) == "f" then
  211. stop = not stop
  212. end
  213. end)
  214. owner.Chatted:Connect(function(msg)
  215. if string.lower(msg) == ";begin" then
  216. --begin = true
  217. end
  218. end)
  219.  
  220. while not begin do
  221. wait()
  222. if 3 == 3 then
  223. remote:FireServer(mouse.Hit.p,stop,mousedown)
  224. end
  225. end]],owner.Backpack)
  226.  
  227. repeat wait() until begin == true
  228. if halttime == 0 then halttime = 0.01 end
  229. script.Name = "game of life"
  230.  
  231. while true do
  232. local killlist = {}
  233. local cellcache = {}
  234. if stop then
  235. repeat wait() until not stop
  236. end
  237. icount = icount + 1
  238. for i,v in pairs(cells) do
  239. local test = testCell(v[1],v[2])
  240. if test == true then
  241. table.insert(cellcache,{v[1],v[2],true,v[4]})
  242. elseif test == false then
  243. table.insert(cellcache,{v[1],v[2],false,v[4]})
  244. elseif test == nil then
  245. table.insert(killlist,{v[1],v[2]-1})
  246. table.insert(killlist,{v[1],v[2]+1})
  247. table.insert(killlist,{v[1]-1,v[2]-1})
  248. table.insert(killlist,{v[1]-1,v[2]})
  249. table.insert(killlist,{v[1]-1,v[2]+1})
  250. table.insert(killlist,{v[1]+1,v[2]-1})
  251. table.insert(killlist,{v[1]+1,v[2]+1})
  252. table.insert(killlist,{v[1]+1,v[2]})
  253. --
  254. table.insert(cellcache,{v[1],v[2],false,v[4]})
  255. end
  256. end
  257. local function getKill(x,y)
  258. for i,v in pairs(killlist) do
  259. if v[1] == x and v[2] == y then
  260. return true
  261. end
  262. end
  263. return false
  264. end
  265. cells = {}
  266. for i,v in pairs(cellcache) do
  267. if not getKill(v[1],v[2]) then
  268. if v[3] == true then
  269. table.insert(cells,{v[1],v[2],v[3],v[4]})
  270. elseif v[4] + 3 > icount then
  271. table.insert(cells,{v[1],v[2],v[3],v[4]})
  272. end
  273. end
  274. end
  275. drawScene()
  276. wait(halttime)
  277. print('scene drawn')
  278. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement