Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --TangentPaint V.0.5
- --Part one of the final product!
- --VARIABLE SETUP--
- width,height=term.getSize()
- print(height) print(width)
- colorNames=
- {
- "white",
- "orange",
- "magenta",
- "lightblue",
- "yellow",
- "lime",
- "pink",
- "gray",
- "lightgray",
- "cyan",
- "purple",
- "blue",
- "brown",
- "green",
- "red",
- "black"
- }
- decHexTable={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"}
- canvas={}
- for i=1,height-1 do
- canvas[i]={}
- for j=1,width do
- canvas[i][j]=0
- end
- end
- overlay={}
- for i=1,height do
- overlay[i]={}
- for j=1,width do
- overlay[i][j]=0
- end
- end
- editorMatrix={}
- for i=1,height-4 do
- editorMatrix[i]={}
- for j=1,width-2 do
- editorMatrix[i][j]=" "
- end
- end
- lastPosition={0,0}
- colorSelected=1
- alive=true
- needsave=false
- --FUNCTIONS--
- function drawTools()
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(1,height)
- for i=1,width do io.write(" ") end
- term.setCursorPos(1,height)
- overlay={1,1,1,0,2,2,2,0,3,3,3,0,4,4,4}
- io.write("[L] [S] [M] [X]")
- term.setBackgroundColor(2^colorSelected-1)
- if colorSelected==1 then term.setTextColor(colors.black) end
- io.write(colorNames[colorSelected])
- term.setTextColor(colors.white)
- end
- function drawCanvas()
- for i=1,height-1 do
- for j=1,width do
- term.setBackgroundColor(2^canvas[i][j])
- io.write(" ")
- term.setBackgroundColor(2^0)
- end
- io.write("\n")
- end
- end
- function paintLoad()
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(1,height)
- for i=1,width do io.write(" ") end
- term.setCursorPos(1,height)
- loadName=io.read()
- loadData={}
- newCanvas={}
- ruleData={}
- if fs.exists(loadName) then
- file = io.open(loadName, "r")
- line = file:read()
- while line do
- table.insert(loadData, line)
- line=file:read()
- end
- newCanvas[1]=loadData[1]
- height=loadData[2]+0
- width=loadData[3]+0
- for i=4,#loadData do
- ruleData[i-3]=loadData[i]
- print(ruleData[i-3])
- end
- file:close()
- for i=1,#ruleData do
- for j=1,string.len(ruleData[i]) do
- if ruleData[i]==nil then
- ruleData[i]=" "
- end
- editorMatrix[i][j]=string.sub(ruleData[i],j,j)
- end
- end
- print(height)
- print(width)
- print(string.len(newCanvas[1]))
- print(height)
- print(width)
- for i=1,string.len(newCanvas[1]) do
- newCanvas[i]=string.sub(loadData[1],i,i)
- end
- k=1
- for i=1,height-1 do
- for j=1,width do
- for decode=1,16 do if newCanvas[k]==decHexTable[decode] then canvas[i][j]=decode-1 end end
- k=k+1
- end
- end
- end
- drawCanvas()
- drawTools()
- end
- function paintSave()
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(1,height)
- for i=1,width do io.write(" ") end
- term.setCursorPos(1,height)
- saveName=io.read()
- local saveDir = saveName:sub(1, saveName:len() - fs.getName(saveName):len() )
- if not fs.exists(saveDir) then
- fs.makeDir( saveName )
- end
- file = fs.open(saveName, "w")
- for i=1,height-1 do
- for j=1,width do
- index=canvas[i][j]+1
- file.write(decHexTable[index])
- end
- end
- file.write("\n"..height.."\n"..width)
- for i=1,#editorMatrix do
- file.write("\n")
- for j=1,#editorMatrix[i] do
- file.write(editorMatrix[i][j])
- end
- end
- file.close()
- needsave=false
- end
- function paintMenu()
- term.setBackgroundColor(colors.lightGray)
- term.setCursorPos(1,height)
- for i=1,width do io.write(" ") end
- term.setCursorPos(1,height)
- overlay={1,1,1,1,1,0,2,2,2,2,2,2,0,3,3,3,3,3,3,3}
- io.write("[TXT] [BACK] [START]")
- clickedButton=menubarInteraction()
- if clickedButton == 1 then
- textMenu()
- end
- if clickedButton == 2 then
- drawTools()
- end
- if clickedButton == 3 then
- CA_Menu()
- end
- end
- function textMenu()
- term.setBackgroundColor(colors.gray)
- term.clear()
- term.setBackgroundColor(colors.orange)
- term.setCursorPos(1,1)
- for i=1,height do
- if (i==1) or (i==height) then
- for j=1,width do
- term.setCursorPos(j,i)
- io.write(" ")
- end
- else
- for k=1,width do
- if k==1 or k==width then
- term.setCursorPos(k,i)
- io.write(" ")
- end
- end
- end
- end
- inEditor=true
- editorCursor={1,1}
- prevEditorCursor={1,1}
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(2,2) io.write("Extra save data editor. Use Ctrl to exit.")
- term.setCursorPos(1,3) term.setBackgroundColor(colors.orange) for i=1,width do io.write(" ") end term.setBackgroundColor(colors.gray)
- function updateCursor()
- term.setCursorPos(prevEditorCursor[1]+1,prevEditorCursor[2]+3)
- term.setBackgroundColor(colors.gray)
- io.write(editorMatrix[prevEditorCursor[2]][prevEditorCursor[1]])
- term.setBackgroundColor(colors.lightGray)
- term.setCursorPos(editorCursor[1]+1,editorCursor[2]+3)
- io.write(editorMatrix[editorCursor[2]][editorCursor[1]])
- term.setBackgroundColor(colors.gray)
- prevEditorCursor=editorCursor
- end
- function updateEditor()
- for i=1,height-4 do
- for j=1,width-2 do
- term.setCursorPos(j+1,i+3)
- io.write(editorMatrix[i][j])
- end
- end
- updateCursor()
- end
- updateEditor()
- while inEditor do
- event,keyPressed=os.pullEvent()
- if event=="key" then
- if keyPressed==29 then inEditor=false end
- if keyPressed==keys.up then editorCursor[2]=editorCursor[2]-1 end
- if keyPressed==keys.down then editorCursor[2]=editorCursor[2]+1 end
- if keyPressed==keys.left then editorCursor[1]=editorCursor[1]-1 end
- if keyPressed==keys.right then editorCursor[1]=editorCursor[1]+1 end
- if editorCursor[1]<1 then editorCursor[1]=1 end
- if editorCursor[1]>(width-2) then editorCursor[1]=(width-2) end
- if editorCursor[2]<1 then editorCursor[2]=1 end
- if editorCursor[2]>(height-4) then editorCursor[2]=(height-4) end
- end
- if event=="char" then
- editorMatrix[editorCursor[2]][editorCursor[1]]=keyPressed
- if editorCursor[1]==(width-2) then
- prevEditorCursor=editorCursor
- editorCursor[1]=1
- else
- editorCursor[1]=editorCursor[1]+1
- end
- end
- updateEditor()
- end
- end
- function paintExit()
- alive=false
- term.setBackgroundColor(colors.black)
- shell.run("clear")
- error()
- end
- function screenInteract()
- if mouseY==height and mouseButton==1 then checkToolBar() return end
- if mouseButton==2 then
- canvas[mouseY][mouseX]=0
- needSave=true
- end
- if mouseButton==1 then
- canvas[mouseY][mouseX]=colorSelected-1
- needSave=true
- end
- if mouseButton==3 then
- colorSelected=(canvas[mouseY][mouseX])+1
- end
- if mouseButton==5 then
- colorSelected=colorSelected+1
- if colorSelected==17 then colorSelected=1 end
- end
- if mouseButton==4 then
- colorSelected=colorSelected-1
- if colorSelected==0 then colorSelected=16 end
- end
- end
- function checkToolBar()
- toolbarSelection=overlay[mouseX]
- if toolbarSelection==1 then paintLoad() end
- if toolbarSelection==2 then paintSave() end
- if toolbarSelection==3 then paintMenu() end
- if toolbarSelection==4 then paintExit() end
- end
- function menubarInteraction()
- event,mouseX,mouseY,mouseButton=os.pullEvent("click")
- if mouseY==height and mouseButton==1 then
- return overlay[mouseX]
- else
- return 0
- end
- end
- function CA_Menu()
- local blab="To return to edit mode, press Ctrl."
- local canvasHeight=#canvas
- local canvasWidth=#canvas[1]
- local moore={}
- newSandbox={}
- for i=1,canvasHeight+2 do
- newSandbox[i]={}
- for j=1,canvasWidth+2 do
- newSandbox[i][j]=0
- end
- end
- sandbox={}
- for i=1,canvasHeight+2 do
- sandbox[i]={}
- for j=1,canvasWidth+2 do
- sandbox[i][j]=0
- end
- end
- local x=2
- local y=2
- term.setCursorPos(1,1)
- term.clear()
- for i=1,canvasHeight do
- for j=1,canvasWidth do
- sandbox[y][x]=canvas[i][j]
- term.setTextColor(colors.black)
- term.setBackgroundColor(2^(sandbox[y][x]))
- io.write(" ")
- x=x+1
- end
- io.write("\n")
- x=2
- y=y+1
- end
- function generate(index)
- term.setCursorPos(1,1)
- for i=2,(#sandbox)-1 do
- for j=2,(#sandbox[i])-1 do
- term.setTextColor(colors.black)
- term.setBackgroundColor(2^(sandbox[i][j]))
- io.write(" ")
- term.setBackgroundColor(2^(0))
- newSandbox[i][j]=checkRules(i,j)
- end
- io.write("\n")
- end
- sandbox=newSandbox
- --drawSandbox()
- end
- function checkRules(indexa,indexb)
- local returnVar=0
- local neighbors=0
- local lookingFor=1
- moore={sandbox[indexa-1][indexb-1],sandbox[indexa][indexb-1],sandbox[indexa+1][indexb-1],sandbox[indexa+1][indexb],sandbox[indexa+1][indexb+1],sandbox[indexa][indexb+1],sandbox[indexa-1][indexb+1],sandbox[indexa-1][indexb],sandbox[indexa][indexb]}
- for k=1,8 do
- if moore[k]==lookingFor then
- neighbors=neighbors+1
- end
- end
- if moore[9]==1 then returnVar=2 end
- if moore[9]==2 then returnVar=3 end
- if moore[9]==3 then returnVar=3 end
- if (neighbors == 2) and (moore[9] == 3) then returnVar=1 end
- if (neighbors == 1) and (moore[9] == 3) then returnVar=1 end
- --if (neighbors<=1) and (moore[9] == 1 or moore[9] == 0) then returnVar=0 end
- --if (neighbors >= 2 and (moore[9]) == 1) then returnVar=1 end
- --if (neighbors == 3) and (moore[9] == 0) then returnVar=1 end
- --if (neighbors >= 4) and (moore[9] == 1) then returnVar=0 end
- moore={0,0,0,0,0,0,0,0,0}
- return returnVar
- end
- function drawSandbox()
- local x=2
- local y=2
- term.setCursorPos(1,1)
- term.clear()
- for i=1,height-1 do
- for j=1,width do
- term.setTextColor(colors.black)
- term.setBackgroundColor(2^(sandbox[y][x]))
- io.write(" ")
- x=x+1
- end
- io.write("\n")
- x=2
- y=y+1
- end
- end
- term.setBackgroundColor(colors.white)
- term.setCursorPos(1,1)
- term.clear()
- term.setCursorPos(math.floor((width-string.len(blab))/2),math.floor(height/2))
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.gray)
- io.write(blab)
- event,keyPressed=os.pullEvent("key")
- term.clear()
- term.setBackgroundColor(colors.white)
- drawSandbox()
- inCA=true
- while inCA do
- event,keyPressed=os.pullEvent("key")
- if keyPressed==57 then generate(1) end
- if keyPressed==29 then inCA=false end
- if keyPressed==28 then
- term.setCursorPos(1,height-1)
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.gray)
- io.write("How many steps would you like to run for?")
- term.setCursorPos(1,height)
- local blab=io.read()
- term.setBackgroundColor(colors.white)
- for i=1,blab do
- generate(1)
- sleep(0.2)
- end
- end
- end
- end
- --MAIN PROGRAM--
- while alive do
- drawCanvas()
- drawTools()
- event,mouseX,mouseY,mouseButton=os.pullEvent("click")
- screenInteract()
- drawCanvas()
- end
Add Comment
Please, Sign In to add comment