Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- john conway's game of life
- -- SETTINGS:
- local cellsize = 1 -- per stud
- local halttime = 0.25 -- miliseconds (0 for as soon as possible)
- local yoffset = 0 -- how high the cells will be placed off of the ground (0 is default for MSB base)
- --
- local cells = {}
- local folder = Instance.new("Folder",script)
- local icount = 0
- -- drawing state:
- local begin = false
- owner.Chatted:Connect(function(msg)
- print(msg)
- if string.lower(msg) == ";begin" then
- print("begin said")
- begin = true
- print("begin made true")
- elseif string.lower(msg) == ";clr" then
- cells = {}
- drawScene()
- end
- end)
- local function drawScene()
- folder:ClearAllChildren()
- for i,v in pairs(cells) do
- local x = v[1]*cellsize
- local y = v[2]*cellsize
- if v[3] then
- local part = Instance.new("MeshPart",folder)
- part.Size = Vector3.new(cellsize,1.5,cellsize)
- part.Position = Vector3.new(x,0,y)
- part.Anchored = true
- else
- local part = Instance.new("MeshPart",folder)
- part.Size = Vector3.new(cellsize,1.5,cellsize)
- part.Position = Vector3.new(x,0,y)
- part.Anchored = true
- part.Color = Color3.fromRGB(0,0,0)
- part.Transparency = 0.7
- end
- end
- end
- local function cellValue(x,y)
- for i,v in pairs(cells) do
- if v[1] == x and v[2] == y then
- v[4] = icount
- return v[3]
- end
- end
- table.insert(cells,{x,y,false,icount})
- return false
- end
- local function cellValue2(x,y)
- for i,v in pairs(cells) do
- if v[1] == x and v[2] == y then
- return v[3]
- end
- end
- return false
- end
- local function getCell(x,y)
- for i,v in pairs(cells) do
- if v[1] == x and v[2] == y then
- v[4] = icount
- return i
- end
- end
- table.insert(cells,{x,y,false,icount})
- return #cells
- end
- local function getCell2(x,y)
- for i,v in pairs(cells) do
- if v[1] == x and v[2] == y then
- return i
- end
- end
- return nil
- end
- local function removeCell(x,y)
- for i,v in pairs(cells) do
- if v[1] == x and v[2] == y then
- table.remove(cells,i)
- end
- end
- end
- local function testCell(x,y)
- local state = cellValue2(x,y)
- local cellnumber = getCell2(x,y)
- local proxcells = {}
- if state then
- proxcells = {
- cellValue(x,y-1),
- cellValue(x,y+1),
- cellValue(x-1,y-1),
- cellValue(x-1,y),
- cellValue(x-1,y+1),
- cellValue(x+1,y-1),
- cellValue(x+1,y+1),
- cellValue(x+1,y)
- }
- else
- proxcells = {
- cellValue2(x,y-1),
- cellValue2(x,y+1),
- cellValue2(x-1,y-1),
- cellValue2(x-1,y),
- cellValue2(x-1,y+1),
- cellValue2(x+1,y-1),
- cellValue2(x+1,y+1),
- cellValue2(x+1,y)
- }
- end
- local amounttru = 0
- local amountfal = 0
- for i,v in pairs(proxcells) do
- if v then amounttru = amounttru + 1 else amountfal = amountfal + 1 end
- end
- if not state then
- if amounttru == 3 then
- return true
- elseif amounttru == 0 then
- return false
- else
- return false
- end
- else
- if amounttru < 2 then
- return false
- elseif amounttru > 3 then
- return false
- else
- return true
- end
- end
- end
- local RunService = game:GetService("RunService")
- local function AccurateWait(seconds)
- if not seconds then
- seconds = 0
- end
- local last = tick()
- local secondsPassed = 0
- local connection = RunService.Heartbeat:Connect(function(dt)
- secondsPassed = secondsPassed + dt
- end)
- repeat
- RunService.Heartbeat:Wait()
- until secondsPassed >= seconds
- connection:Disconnect()
- end
- local function deepCopy(original)
- local copy = {}
- for k, v in pairs(original) do
- if type(v) == "table" then
- v = deepCopy(v)
- end
- copy[k] = v
- end
- return copy
- end
- local stop = false
- local remote = owner:FindFirstChild("gameoflife")
- if remote == nil then remote = Instance.new("RemoteEvent",owner) end
- remote.Name = "gameoflife"
- local lastx, lastz = nil,nil
- remote.OnServerEvent:Connect(function(plr,pos,sto,mousedown)
- stop = sto
- if mousedown then
- local x = pos.X
- local z = pos.Z
- x = x-(x%cellsize)
- z = z-(z%cellsize)
- local dx,dz = x/cellsize,z/cellsize
- if (dx ~= lastx or dz ~= lastz) then
- lastx = dx
- lastz = dz
- local cellval = cellValue(dx,dz)
- local cellget = getCell(dx,dz)
- cells[cellget][3] = not cells[cellget][3]
- drawScene()
- end
- end
- end)
- NLS([[
- local plr = owner
- local mouse = plr:GetMouse()
- local mousedown = false
- local begin = false
- local stop = false
- local remote = owner:WaitForChild("gameoflife")
- mouse.Button1Down:Connect(function()
- mousedown = true
- end)
- mouse.Button1Up:Connect(function()
- mousedown = false
- end)
- mouse.KeyUp:Connect(function(key)
- if string.lower(key) == "f" then
- stop = not stop
- end
- end)
- owner.Chatted:Connect(function(msg)
- if string.lower(msg) == ";begin" then
- --begin = true
- end
- end)
- while not begin do
- wait()
- if 3 == 3 then
- remote:FireServer(mouse.Hit.p,stop,mousedown)
- end
- end]],owner.Backpack)
- repeat wait() until begin == true
- if halttime == 0 then halttime = 0.01 end
- script.Name = "game of life"
- while true do
- local killlist = {}
- local cellcache = {}
- if stop then
- repeat wait() until not stop
- end
- icount = icount + 1
- for i,v in pairs(cells) do
- local test = testCell(v[1],v[2])
- if test == true then
- table.insert(cellcache,{v[1],v[2],true,v[4]})
- elseif test == false then
- table.insert(cellcache,{v[1],v[2],false,v[4]})
- elseif test == nil then
- table.insert(killlist,{v[1],v[2]-1})
- table.insert(killlist,{v[1],v[2]+1})
- table.insert(killlist,{v[1]-1,v[2]-1})
- table.insert(killlist,{v[1]-1,v[2]})
- table.insert(killlist,{v[1]-1,v[2]+1})
- table.insert(killlist,{v[1]+1,v[2]-1})
- table.insert(killlist,{v[1]+1,v[2]+1})
- table.insert(killlist,{v[1]+1,v[2]})
- --
- table.insert(cellcache,{v[1],v[2],false,v[4]})
- end
- end
- local function getKill(x,y)
- for i,v in pairs(killlist) do
- if v[1] == x and v[2] == y then
- return true
- end
- end
- return false
- end
- cells = {}
- for i,v in pairs(cellcache) do
- if not getKill(v[1],v[2]) then
- if v[3] == true then
- table.insert(cells,{v[1],v[2],v[3],v[4]})
- elseif v[4] + 3 > icount then
- table.insert(cells,{v[1],v[2],v[3],v[4]})
- end
- end
- end
- drawScene()
- wait(halttime)
- print('scene drawn')
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement