Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --ComputerCraft Architect by Teki 0.1
- function deepcopy(orig) --copied/pasted from http://lua-users.org/wiki/CopyTable
- local orig_type = type(orig)
- local copy
- if orig_type == 'table' then
- copy = {}
- for orig_key, orig_value in next, orig, nil do
- copy[deepcopy(orig_key)] = deepcopy(orig_value)
- end
- setmetatable(copy, deepcopy(getmetatable(orig)))
- else -- number, string, boolean, etc
- copy = orig
- end
- return copy
- end
- --Data
- local running = true
- local blueprint = {{{}}}
- local tmpBp = {{}}
- local emptyLayer = {{}}
- local copyLayer = nil
- local clicAction = false
- local toolColor = 1
- local toolColors = {
- colors.white,
- colors.orange,
- --colors.magenta,
- colors.lightBlue,
- colors.yellow,
- colors.lime,
- --colors.pink,
- colors.cyan,
- colors.purple,
- colors.blue,
- --colors.brown,
- colors.green
- }
- local offsetX = 0
- local offsetY = 0
- local currentLayer = 1
- local tool = false
- local lastXPos = 1
- local lastYPos = 1
- local filePath = ""
- -- Monitor
- local monitor
- local eventType = "mouse_click"
- if peripheral.find("monitor") == nil then
- monitor = term.current()
- else
- monitor = peripheral.find("monitor")
- monitor.setTextScale(0.5)
- eventType = "monitor_touch"
- term.redirect(monitor)
- end
- local sizeX,sizeY = monitor.getSize()
- if sizeX > 66 then sizeX = 66 end
- if sizeY > 65 then sizeY = 65 end
- -- Initiate tables !!! dirty
- blueprint[currentLayer] = {}
- for X = 1, 64 do
- for Y = 1, 64 do
- if Y == 1 then emptyLayer[X] = {} end
- emptyLayer[X][Y] = nil
- end
- end
- blueprint[currentLayer] = deepcopy(emptyLayer)
- tmpBp = deepcopy(emptyLayer)
- function Save()
- paintutils.drawBox(5, 7, sizeX-4, 10, colors.green)
- term.setCursorPos(6,7)
- term.write("Donner un nom au fichier")
- paintutils.drawBox(6, 8, sizeX-5, 9, colors.black)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.setCursorPos(6,9)
- term.write("Annuler")
- term.setCursorPos(sizeX-6,9)
- term.write("OK")
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- done = false
- doneOk = false
- while not done do
- paintutils.drawBox(6, 8, sizeX-5, 8, colors.black)
- term.setCursorPos(6,8)
- term.write("blueprints/" .. filePath)
- event, side, xPos, yPos = os.pullEvent()
- if event == eventType then
- if (xPos == sizeX-5 or xPos == sizeX-6) and yPos == 9 and filePath ~= "" then
- doneOk = true
- done = true
- elseif xPos >= 6 and xPos <= 12 and yPos == 9 then
- done = true
- end
- elseif event == "char" then
- filePath = filePath .. side
- elseif event == "key" and filePath ~= "" then
- if keys.getName(side) == "backspace" then
- filePath = string.sub(filePath, 1, string.len(filePath)-1)
- elseif keys.getName(side) == "enter" then
- doneOk = true
- done = true
- end
- end
- end
- if filePath ~= "" and doneOk == true then
- if fs.exists("blueprints/" .. filePath) then
- term.setBackgroundColor(colors.green)
- term.setCursorPos(6,7)
- term.write("Fichier existant, ecraser ?")
- done = false
- while not done do
- event, side, xPos, yPos = os.pullEvent()
- if event == eventType then
- if (xPos == sizeX-6 or xPos == sizeX-8) and yPos == 9 and filePath ~= "" then
- done = true
- elseif xPos >= 6 and xPos <= 12 and yPos == 9 then
- done = true
- doneOk = false
- end
- elseif event == "key" then
- if keys.getName(side) == "enter" then
- done = true
- end
- end
- end
- end
- if doneOk then
- savedFile = fs.open("blueprints/" .. filePath, "w")
- savedFile.write(textutils.serialize(deepcopy(blueprint)))
- savedFile.flush()
- savedFile.close()
- end
- end
- end
- function Load()
- paintutils.drawBox(5, 7, sizeX-4, 10, colors.green)
- term.setCursorPos(6,7)
- term.write("Nom du fichier à ouvrir :")
- paintutils.drawBox(6, 8, sizeX-5, 9, colors.black)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.setCursorPos(6,9)
- term.write("Annuler")
- term.setCursorPos(sizeX-6,9)
- term.write("OK")
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- done = false
- doneOk = false
- while not done do
- paintutils.drawBox(6, 8, sizeX-5, 8, colors.black)
- term.setCursorPos(6,8)
- term.write("blueprints/" .. filePath)
- event, side, xPos, yPos = os.pullEvent()
- if event == eventType then
- if (xPos == sizeX-5 or xPos == sizeX-6) and yPos == 9 and filePath ~= "" then
- doneOk = true
- done = true
- elseif xPos >= 6 and xPos <= 12 and yPos == 9 then
- done = true
- end
- elseif event == "char" then
- filePath = filePath .. side
- elseif event == "key" and filePath ~= "" then
- if keys.getName(side) == "backspace" then
- filePath = string.sub(filePath, 1, string.len(filePath)-1)
- elseif keys.getName(side) == "enter" then
- doneOk = true
- done = true
- end
- end
- end
- if filePath ~= "" and doneOk == true then
- if fs.exists("blueprints/" .. filePath) then
- savedFile = fs.open("blueprints/" .. filePath, "r")
- blueprint = deepcopy(textutils.unserialize(savedFile.readAll()))
- tmpBp = deepcopy(blueprint[currentLayer])
- savedFile.close()
- end
- end
- end
- function cleanQuit()
- paintutils.drawFilledBox(5, 7, sizeX-4, 10, colors.green)
- term.setCursorPos(6,7)
- term.write("Voulez vous quitter sans")
- term.setCursorPos(6,8)
- term.write("sauvegarder ?")
- paintutils.drawBox(6, 9, sizeX-5, 9, colors.black)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.setCursorPos(6,9)
- term.write("Annuler")
- term.setCursorPos(sizeX-6,9)
- term.write("OK")
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- done = false
- doneOk = false
- while not done do
- event, side, xPos, yPos = os.pullEvent()
- if event == eventType then
- if (xPos == sizeX-5 or xPos == sizeX-6) and yPos == 9 then
- doneOk = true
- done = true
- elseif xPos >= 6 and xPos <= 12 and yPos == 9 then
- done = true
- end
- elseif event == "key" then
- if keys.getName(side) == "enter" then
- doneOk = true
- done = true
- end
- end
- end
- if doneOk then
- term.clear()
- term.setCursorPos(1,1)
- running = false
- end
- end
- --Background
- local gridUnit = 8 -- 8 cut the grid by chunks of 8 blocks
- local bgWindow = window.create(monitor, 1, 1, sizeX, sizeY, true)
- for X = 1, sizeX do
- for Y = 1, sizeY do
- bgWindow.setCursorPos(X, Y)
- bgWindow.setBackgroundColor(colors.black)
- if X > 1 then
- bgWindow.setBackgroundColor(colors.lightGray)
- end
- if (X % 2 == 0 and Y % 2 ~= 0) or (X % 2 ~= 0 and Y % 2 == 0) and X > 1 then
- bgWindow.setBackgroundColor(colors.gray)
- end
- bgWindow.write(" ")
- end
- end
- -- Buttons
- bgWindow.setBackgroundColor(colors.black)
- bgWindow.setCursorPos(1, 2)
- bgWindow.write("+")
- bgWindow.setCursorPos(1, 3)
- bgWindow.write("-")
- bgWindow.setCursorPos(1, 4)
- bgWindow.write("C")
- bgWindow.setCursorPos(1, 5)
- bgWindow.write("P")
- bgWindow.setCursorPos(1, sizeY-2)
- bgWindow.write("O")
- bgWindow.setCursorPos(1, sizeY-1)
- bgWindow.write("S")
- bgWindow.setCursorPos(1, sizeY)
- bgWindow.write("Q")
- local function drawGridNumbers()
- monitor.setBackgroundColor(colors.black)
- monitor.setCursorPos(1, 1)
- monitor.write(string.format("%02d", currentLayer))
- bgWindow.setCursorPos(1, 7)
- if tool then
- bgWindow.write("o")
- else
- bgWindow.write("#")
- end
- bgWindow.setCursorPos(1, 8)
- bgWindow.setBackgroundColor(toolColors[toolColor])
- bgWindow.setTextColor(colors.black)
- bgWindow.write(toolColor)
- bgWindow.setTextColor(colors.white)
- bgWindow.setBackgroundColor(colors.black)
- for X = 1, sizeX do
- for Y = 1, sizeY do
- monitor.setCursorPos(X, Y)
- if ((X-2+offsetX) % gridUnit == 0 and (Y-1+offsetY) % gridUnit == 0) or (Y == 1 and X == 3) or (X-1 == 1 and Y == 2)then
- monitor.setBackgroundColor(colors.red)
- end
- if X == 2 and Y > 1 then
- if Y-1 == 1 then
- if offsetY > 0 then
- monitor.write("U")
- else
- monitor.write("1")
- end
- elseif Y == sizeY then
- monitor.write("D")
- elseif (Y-1+offsetY) % gridUnit == 0 then
- monitor.write(string.sub(Y-1+offsetY, 1))
- end
- elseif X > 2 and Y == 1 then
- if X-2 == 1 then
- if offsetX > 0 then
- monitor.write("L")
- else
- monitor.write("1")
- end
- elseif X == sizeX then
- monitor.write("R")
- elseif (X-2+offsetX) % gridUnit == 0 then
- monitor.write(string.sub(X-2+offsetX, 1))
- end
- elseif (X > 2 and Y > 1) and (((X-2+offsetX) % gridUnit == 0 and (Y-1+offsetY) % gridUnit == 0) or (Y == 1 and X == 3) or (X-1 == 1 and Y == 2)) then
- monitor.write(" ")
- end
- end
- end
- end
- function draw()
- bgWindow.redraw()
- drawGridNumbers()
- monitor.setBackgroundColor(colors.white)
- for X = 3, sizeX do
- for Y = 2, sizeY do
- if tmpBp[X-2+offsetX][Y-1+offsetY] ~= nil then
- monitor.setBackgroundColor(toolColors[tmpBp[X-2+offsetX][Y-1+offsetY]])
- monitor.setCursorPos(X, Y)
- monitor.write(" ")
- end
- end
- end
- monitor.setBackgroundColor(colors.black)
- end
- while running do
- -- Drawing
- draw()
- -- Catching Events
- event, side, xPos, yPos = os.pullEvent()
- if xPos ~= nil and yPos ~= nil then
- if event == eventType then
- if xPos > 2 and yPos > 1 then
- tmpBp = deepcopy(blueprint[currentLayer])
- if tmpBp[xPos-2+offsetX][yPos-1+offsetY] ~= nil and tmpBp[xPos-2+offsetX][yPos-1+offsetY] == toolColor then
- clicAction = nil
- else
- clicAction = toolColor
- end
- tmpBp[xPos-2+offsetX][yPos-1+offsetY] = clicAction
- blueprint[currentLayer] = deepcopy(tmpBp)
- lastXPos = xPos + offsetX
- lastYPos = yPos + offsetY
- elseif xPos == 1 and yPos == 2 and currentLayer < 98 then -- Layer +
- currentLayer = currentLayer + 1
- if blueprint[currentLayer] == nil then
- blueprint[currentLayer] = {}
- blueprint[currentLayer] = deepcopy(emptyLayer)
- end
- tmpBp = deepcopy(blueprint[currentLayer])
- elseif xPos == 1 and yPos == 3 and currentLayer > 1 then -- Layer -
- currentLayer = currentLayer - 1
- tmpBp = deepcopy(blueprint[currentLayer])
- elseif xPos == 1 and yPos == 4 then -- Layer copy
- copyLayer = deepcopy(blueprint[currentLayer])
- elseif xPos == 1 and yPos == 5 and copyLayer ~= nil then -- Layer paste
- tmpBp = deepcopy(copyLayer)
- blueprint[currentLayer] = deepcopy(copyLayer)
- elseif xPos == sizeX and yPos == 1 and offsetX + sizeX < 66 then -- offsetX +
- offsetX = offsetX + 1
- elseif xPos == 3 and yPos == 1 and offsetX > 0 then -- offsetX -
- offsetX = offsetX - 1
- elseif xPos == 2 and yPos == sizeY and offsetY + sizeY < 65 then -- offsetY +
- offsetY = offsetY + 1
- elseif xPos == 2 and yPos == 2 and offsetY > 0 then -- offsetY -
- offsetY = offsetY - 1
- elseif xPos == 2 and yPos == 1 then -- offsetY -
- offsetX = 0
- offsetY = 0
- elseif xPos == 1 and yPos == 7 then -- offsetY -
- tool = not tool
- elseif xPos == 1 and yPos == 8 then -- offsetY -
- if side == 1 then
- if toolColor < #toolColors then
- toolColor = toolColor + 1
- else
- toolColor = 1
- end
- else
- if toolColor > 1 then
- toolColor = toolColor - 1
- else
- toolColor = #toolColors
- end
- end
- elseif xPos == 1 and yPos == sizeY-2 then -- offsetY -
- Load()
- elseif xPos == 1 and yPos == sizeY-1 then -- offsetY -
- Save()
- elseif xPos == 1 and yPos == sizeY then -- offsetY -
- cleanQuit()
- end
- elseif event == "mouse_drag" then
- if xPos > 2 and yPos > 1 then
- tmpBp = deepcopy(blueprint[currentLayer])
- local tmpX = lastXPos
- local tmpXmax = xPos + offsetX
- local tmpY = lastYPos
- local tmpYmax = yPos + offsetY
- if tmpX > tmpXmax then
- tmpX = xPos + offsetX
- tmpXmax = lastXPos
- end
- if tmpY > tmpYmax then
- tmpY = yPos + offsetY
- tmpYmax = lastYPos
- end
- if tool then
- -- Cercle
- if tmpXmax - tmpX > tmpYmax - tmpY then
- x = 0
- y = tmpXmax - tmpX
- else
- x = 0
- y = tmpYmax - tmpY
- end
- m = 5 - 4 * y
- -- while x <= y do -- make empty circles
- -- if x+lastXPos-2 > 0 and y+lastYPos-1 > 0 and x+lastXPos-2 < 65 and y+lastYPos-1 < 65 then
- -- tmpBp[x+lastXPos-2][y+lastYPos-1] = clicAction
- -- end
- -- if x+lastYPos-1 > 0 and y+lastXPos-2 > 0 and x+lastYPos-1 < 65 and y+lastXPos-2 < 65 then
- -- tmpBp[y+lastXPos-2][x+lastYPos-1] = clicAction
- -- end
- -- if -x+lastXPos-2 > 0 and y+lastYPos-1 > 0 and -x+lastXPos-2 < 65 and y+lastYPos-1 < 65 then
- -- tmpBp[-x+lastXPos-2][y+lastYPos-1] = clicAction
- -- end
- -- if x+lastYPos-1 > 0 and -y+lastXPos-2 > 0 and x+lastYPos-1 < 65 and -y+lastXPos-2 < 65 then
- -- tmpBp[-y+lastXPos-2][x+lastYPos-1] = clicAction
- -- end
- -- if x+lastXPos-2 > 0 and -y+lastYPos-1 > 0 and x+lastXPos-2 < 65 and -y+lastYPos-1 < 65 then
- -- tmpBp[x+lastXPos-2][-y+lastYPos-1] = clicAction
- -- end
- -- if -x+lastYPos-1 > 0 and y+lastXPos-2 > 0 and -x+lastYPos-1 < 65 and y+lastXPos-2 < 65 then
- -- tmpBp[y+lastXPos-2][-x+lastYPos-1] = clicAction
- -- end
- -- if -x+lastXPos-2 > 0 and -y+lastYPos-1 > 0 and -x+lastXPos-2 < 65 and -y+lastYPos-1 < 65 then
- -- tmpBp[-x+lastXPos-2][-y+lastYPos-1] = clicAction
- -- end
- -- if -x+lastYPos-1 > 0 and -y+lastXPos-2 > 0 and -x+lastYPos-1 < 65 and -y+lastXPos-2 < 65 then
- -- tmpBp[-y+lastXPos-2][-x+lastYPos-1] = clicAction
- -- end
- -- if m > 0 then
- -- y = y - 1
- -- m = m - 8 * y
- -- end
- -- x = x + 1
- -- m = m + 8 * x + 4
- -- end
- while x <= y do
- for fillY = 1, y do -- make plain circles
- if x+lastXPos-2 > 0 and fillY+lastYPos-1 > 0 and x+lastXPos-2 < 65 and fillY+lastYPos-1 < 65 then
- tmpBp[x+lastXPos-2][fillY+lastYPos-1] = clicAction
- end
- if x+lastYPos-1 > 0 and fillY+lastXPos-2 > 0 and x+lastYPos-1 < 65 and fillY+lastXPos-2 < 65 then
- tmpBp[fillY+lastXPos-2][x+lastYPos-1] = clicAction
- end
- if -x+lastXPos-2 > 0 and fillY+lastYPos-1 > 0 and -x+lastXPos-2 < 65 and fillY+lastYPos-1 < 65 then
- tmpBp[-x+lastXPos-2][fillY+lastYPos-1] = clicAction
- end
- if x+lastYPos-1 > 0 and -fillY+lastXPos-2 > 0 and x+lastYPos-1 < 65 and -fillY+lastXPos-2 < 65 then
- tmpBp[-fillY+lastXPos-2][x+lastYPos-1] = clicAction
- end
- if x+lastXPos-2 > 0 and -fillY+lastYPos-1 > 0 and x+lastXPos-2 < 65 and -fillY+lastYPos-1 < 65 then
- tmpBp[x+lastXPos-2][-fillY+lastYPos-1] = clicAction
- end
- if -x+lastYPos-1 > 0 and fillY+lastXPos-2 > 0 and -x+lastYPos-1 < 65 and fillY+lastXPos-2 < 65 then
- tmpBp[fillY+lastXPos-2][-x+lastYPos-1] = clicAction
- end
- if -x+lastXPos-2 > 0 and -fillY+lastYPos-1 > 0 and -x+lastXPos-2 < 65 and -fillY+lastYPos-1 < 65 then
- tmpBp[-x+lastXPos-2][-fillY+lastYPos-1] = clicAction
- end
- if -x+lastYPos-1 > 0 and -fillY+lastXPos-2 > 0 and -x+lastYPos-1 < 65 and -fillY+lastXPos-2 < 65 then
- tmpBp[-fillY+lastXPos-2][-x+lastYPos-1] = clicAction
- end
- end
- if m > 0 then
- y = y - 1
- m = m - 8 * y
- end
- x = x + 1
- m = m + 8 * x + 4
- end
- else
- -- Pavé
- for x = tmpX-2, tmpXmax-2 do
- for y = tmpY-1, tmpYmax-1 do
- if xPos > 2 and yPos > 1 then
- tmpBp[x][y] = clicAction
- end
- end
- end
- end
- end
- if xPos == sizeX and offsetX + sizeX < 66 then
- offsetX = offsetX + 1
- elseif xPos < 3 and offsetX > 0 then -- offsetX -
- offsetX = offsetX - 1
- elseif yPos == sizeY and offsetY + sizeY < 65 then
- offsetY = offsetY + 1
- elseif yPos < 2 and offsetY > 0 then -- offsetY -
- offsetY = offsetY - 1
- end
- elseif event == "mouse_up" then
- blueprint[currentLayer] = deepcopy(tmpBp)
- end
- end
- sleep(0)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement