Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- reset=function()
- term.setBackgroundColor(colors.black)
- term.clear()
- Newcells={}
- Ocells={}
- counter=0
- end
- randomField=function()
- for i=1,969 do
- xc= math.fmod(i,51)
- if xc==0 then xc=51 end
- yc=math.ceil(i/51)
- if math.random(0,99)<chance then
- paintutils.drawPixel(xc,yc,colors.green)
- Ocells[xc+((yc-1)*51)]=1
- else
- paintutils.drawPixel(xc,yc,colors.black)
- Ocells[xc+((yc-1)*51)]=nil
- end
- end
- end
- loop=function()
- parallel.waitForAny(input,evolute)
- if ki==57 then drawMode()
- elseif ki==16 then error()
- elseif ki==35 then openHelp()
- elseif ki==46 then reset() drawMode()
- elseif ki==2 then sleepTime=(ki-1)*0.1 loop()
- elseif ki==3 then sleepTime=(ki-1)*0.1 loop()
- elseif ki==4 then sleepTime=(ki-1)*0.1 loop()
- elseif ki==5 then sleepTime=(ki-1)*1.5 loop()
- elseif ki==6 then sleepTime=(ki-1)*1.5 loop()
- elseif ki==7 then sleepTime=(ki-1)*1.5 loop()
- elseif ki==8 then sleepTime=(ki-1)*0.2 loop()
- elseif ki==9 then sleepTime=(ki-1)*0.2 loop()
- elseif ki==10 then sleepTime=(ki-1)*0.2 loop()
- elseif ki==11 then sleepTime=(ki-1)*0.2 loop()
- end
- end
- drawMode=function()
- var1=true
- while var1 do
- event,n,x,y=os.pullEvent()
- if event=="mouse_click" or "mouse_drag" then
- if n==1 then
- paintutils.drawPixel(x,y,colors.green)
- Ocells[x+((y-1)*51)]=1
- elseif n==2 then
- paintutils.drawPixel(x,y,colors.black)
- Ocells[x+((y-1)*51)]=nil
- end
- end
- if event=="key" then
- if n==57 then --space
- loop()
- elseif n==35 then --h
- openHelp()
- elseif n==16 then --q
- error()
- elseif n==46 then --c
- reset()
- elseif n==31 then --s
- openSettings()
- elseif n==19 then --r
- randomField()
- end
- end
- end
- end
- evolute=function()
- while true do
- sleep(sleepTime)
- counter=counter+1
- term.setCursorPos(1,19)
- term.setTextColor(colors.blue)
- write(counter)
- for i=1,969 do
- xc= math.fmod(i,51)
- if xc==0 then xc=51 end
- yc=math.ceil(i/51)
- neighbours=0
- if not(Ocells[i-52]==nil) then neighbours=neighbours+1 end
- if not(Ocells[i-51]==nil) then neighbours=neighbours+1 end
- if not(Ocells[i-50]==nil) then neighbours=neighbours+1 end
- if not(Ocells[i- 1]==nil) then neighbours=neighbours+1 end
- if not(Ocells[i+ 1]==nil) then neighbours=neighbours+1 end
- if not(Ocells[i+52]==nil) then neighbours=neighbours+1 end
- if not(Ocells[i+51]==nil) then neighbours=neighbours+1 end
- if not(Ocells[i+50]==nil) then neighbours=neighbours+1 end
- if Ocells[i]==nil then
- if neighbours==3 then
- Newcells[i]=1
- paintutils.drawPixel(xc,yc,colors.green)
- else
- if math.random(0,999)<birth then
- Newcells[i]=1
- paintutils.drawPixel(xc,yc,colors.green)
- else
- Newcells[i]=nil
- end
- end
- end
- if not(Ocells[i]==nil) then
- if neighbours>3 or neighbours<2 then
- Newcells[i]=nil
- paintutils.drawPixel(xc,yc,colors.black)
- else
- Newcells[i]=1
- end
- end
- end
- for i=1,969 do
- Ocells[i]=Newcells[i]
- end
- end
- end
- input=function()
- event,ki=os.pullEvent("key")
- return ki
- end
- openHelp=function()
- term.clear()
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- print("RULES:")
- print("cell with more than 3 neighbours dies")
- print("cell with less then 2 neighbours dies")
- print("dead cell with 3 neighbours will be reborn")
- print("")
- print("press h to open this Help-Menue")
- print("press s to open Settings-Menue")
- print("press q to leave the program")
- print("press c to kill all cells")
- print("press r to create a random pattern")
- print("press space to start/stop animation")
- print("press a number to change evolution speed")
- print("")
- print("press any key to continue")
- event,n=os.pullEvent("key")
- reset()
- drawMode()
- end
- openSettings=function()
- term.clear()
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- print("SETTINGS:")
- print("")
- print("random: chance for living cell in %:")
- print("")
- print("random birth chance in proMill:")
- print("")
- print("senility after x generations. x=")
- print("")
- print("pres any key to contniue")
- var2=true
- while var2 do
- e,n,x,y=os.pullEvent()
- if e=="key" then
- var2=false
- end
- if e=="mouse_click" then
- if y==3 then
- term.setCursorPos(37,y)
- chance=tonumber(read())
- var2=false
- elseif y==5 then
- term.setCursorPos(33,y)
- birth=tonumber(read())
- var2=false
- elseif y==7 then
- term.setCursorPos(35,y)
- senility=tonumber(read())
- var2=false
- end
- end
- end
- reset()
- drawMode()
- end
- Ocells={}
- Newcells={}
- sleepTime=0.5
- chance=33
- birth=0
- senility=0
- counter=0
- openHelp()
Add Comment
Please, Sign In to add comment