Advertisement
Te-ki

ccArchitect

Aug 1st, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.81 KB | None | 0 0
  1. --ComputerCraft Architect by Teki 0.1
  2.  
  3. function deepcopy(orig) --copied/pasted from http://lua-users.org/wiki/CopyTable
  4.     local orig_type = type(orig)
  5.     local copy
  6.     if orig_type == 'table' then
  7.         copy = {}
  8.         for orig_key, orig_value in next, orig, nil do
  9.             copy[deepcopy(orig_key)] = deepcopy(orig_value)
  10.         end
  11.         setmetatable(copy, deepcopy(getmetatable(orig)))
  12.     else -- number, string, boolean, etc
  13.         copy = orig
  14.     end
  15.     return copy
  16. end
  17.  
  18. --Data
  19. local running = true
  20. local blueprint = {{{}}}
  21. local tmpBp = {{}}
  22. local emptyLayer = {{}}
  23. local copyLayer = nil
  24. local clicAction = false
  25. local toolColor = 1
  26.  
  27. local toolColors = {
  28.     colors.white,
  29.     colors.orange,
  30.     --colors.magenta,
  31.     colors.lightBlue,
  32.     colors.yellow,
  33.     colors.lime,
  34.     --colors.pink,
  35.     colors.cyan,
  36.     colors.purple,
  37.     colors.blue,
  38.     --colors.brown,
  39.     colors.green
  40. }
  41.  
  42.  
  43. local offsetX = 0
  44. local offsetY = 0
  45. local currentLayer = 1
  46. local tool = false
  47.  
  48. local lastXPos = 1
  49. local lastYPos = 1
  50.  
  51. local filePath = ""
  52.  
  53. -- Monitor
  54. local monitor
  55. local eventType = "mouse_click"
  56.  
  57. if peripheral.find("monitor") == nil then
  58.     monitor = term.current()
  59. else
  60.     monitor = peripheral.find("monitor")
  61.     monitor.setTextScale(0.5)
  62.     eventType = "monitor_touch"
  63.     term.redirect(monitor)
  64. end
  65.  
  66. local sizeX,sizeY = monitor.getSize()
  67.  
  68. if sizeX > 66 then sizeX = 66 end
  69. if sizeY > 65 then sizeY = 65 end
  70.  
  71. -- Initiate tables !!! dirty
  72. blueprint[currentLayer] = {}
  73. for X = 1, 64 do
  74.     for Y = 1, 64 do
  75.         if Y == 1 then emptyLayer[X] = {} end
  76.         emptyLayer[X][Y] = nil
  77.     end
  78. end
  79. blueprint[currentLayer] = deepcopy(emptyLayer)
  80. tmpBp = deepcopy(emptyLayer)
  81.  
  82. function Save()
  83.     paintutils.drawBox(5, 7, sizeX-4, 10, colors.green)
  84.     term.setCursorPos(6,7)
  85.     term.write("Donner un nom au fichier")
  86.     paintutils.drawBox(6, 8, sizeX-5, 9, colors.black)
  87.     term.setBackgroundColor(colors.white)
  88.     term.setTextColor(colors.black)
  89.     term.setCursorPos(6,9)
  90.     term.write("Annuler")
  91.     term.setCursorPos(sizeX-6,9)
  92.     term.write("OK")
  93.     term.setBackgroundColor(colors.black)
  94.     term.setTextColor(colors.white)
  95.     done = false
  96.     doneOk = false
  97.     while not done do
  98.         paintutils.drawBox(6, 8, sizeX-5, 8, colors.black)
  99.         term.setCursorPos(6,8)
  100.         term.write("blueprints/" .. filePath)
  101.         event, side, xPos, yPos = os.pullEvent()
  102.         if event == eventType then
  103.             if (xPos == sizeX-5 or xPos == sizeX-6) and yPos == 9 and filePath ~= "" then
  104.                 doneOk = true
  105.                 done = true
  106.             elseif xPos >= 6 and xPos <= 12 and yPos == 9 then
  107.                 done = true
  108.             end
  109.         elseif event == "char" then
  110.             filePath = filePath .. side
  111.         elseif event == "key" and filePath ~= "" then
  112.             if keys.getName(side) == "backspace" then
  113.                 filePath = string.sub(filePath, 1, string.len(filePath)-1)
  114.             elseif keys.getName(side) == "enter" then
  115.                 doneOk = true
  116.                 done = true
  117.             end
  118.         end
  119.     end
  120.    
  121.     if filePath ~= "" and doneOk == true then
  122.         if fs.exists("blueprints/" .. filePath) then
  123.             term.setBackgroundColor(colors.green)
  124.             term.setCursorPos(6,7)
  125.             term.write("Fichier existant, ecraser ?")
  126.             done = false
  127.             while not done do
  128.                 event, side, xPos, yPos = os.pullEvent()
  129.                 if event == eventType then
  130.                     if (xPos == sizeX-6 or xPos == sizeX-8) and yPos == 9 and filePath ~= "" then
  131.                         done = true
  132.                     elseif xPos >= 6 and xPos <= 12 and yPos == 9 then
  133.                         done = true
  134.                         doneOk = false
  135.                     end
  136.                 elseif event == "key" then
  137.                     if keys.getName(side) == "enter" then
  138.                         done = true
  139.                     end
  140.                 end
  141.             end
  142.         end
  143.         if doneOk then
  144.             savedFile = fs.open("blueprints/" .. filePath, "w")
  145.             savedFile.write(textutils.serialize(deepcopy(blueprint)))
  146.             savedFile.flush()
  147.             savedFile.close()
  148.         end
  149.     end
  150. end
  151.  
  152. function Load()
  153.     paintutils.drawBox(5, 7, sizeX-4, 10, colors.green)
  154.     term.setCursorPos(6,7)
  155.     term.write("Nom du fichier à ouvrir :")
  156.     paintutils.drawBox(6, 8, sizeX-5, 9, colors.black)
  157.     term.setBackgroundColor(colors.white)
  158.     term.setTextColor(colors.black)
  159.     term.setCursorPos(6,9)
  160.     term.write("Annuler")
  161.     term.setCursorPos(sizeX-6,9)
  162.     term.write("OK")
  163.     term.setBackgroundColor(colors.black)
  164.     term.setTextColor(colors.white)
  165.     done = false
  166.     doneOk = false
  167.     while not done do
  168.         paintutils.drawBox(6, 8, sizeX-5, 8, colors.black)
  169.         term.setCursorPos(6,8)
  170.         term.write("blueprints/" .. filePath)
  171.         event, side, xPos, yPos = os.pullEvent()
  172.         if event == eventType then
  173.             if (xPos == sizeX-5 or xPos == sizeX-6) and yPos == 9 and filePath ~= "" then
  174.                 doneOk = true
  175.                 done = true
  176.             elseif xPos >= 6 and xPos <= 12 and yPos == 9 then
  177.                 done = true
  178.             end
  179.         elseif event == "char" then
  180.             filePath = filePath .. side
  181.         elseif event == "key" and filePath ~= "" then
  182.             if keys.getName(side) == "backspace" then
  183.                 filePath = string.sub(filePath, 1, string.len(filePath)-1)
  184.             elseif keys.getName(side) == "enter" then
  185.                 doneOk = true
  186.                 done = true
  187.             end
  188.         end
  189.     end
  190.    
  191.     if filePath ~= "" and doneOk == true then
  192.         if fs.exists("blueprints/" .. filePath) then
  193.             savedFile = fs.open("blueprints/" .. filePath, "r")
  194.             blueprint = deepcopy(textutils.unserialize(savedFile.readAll()))
  195.             tmpBp = deepcopy(blueprint[currentLayer])
  196.             savedFile.close()
  197.         end
  198.     end
  199. end
  200.  
  201. function cleanQuit()
  202.     paintutils.drawFilledBox(5, 7, sizeX-4, 10, colors.green)
  203.     term.setCursorPos(6,7)
  204.     term.write("Voulez vous quitter sans")
  205.     term.setCursorPos(6,8)
  206.     term.write("sauvegarder ?")
  207.     paintutils.drawBox(6, 9, sizeX-5, 9, colors.black)
  208.     term.setBackgroundColor(colors.white)
  209.     term.setTextColor(colors.black)
  210.     term.setCursorPos(6,9)
  211.     term.write("Annuler")
  212.     term.setCursorPos(sizeX-6,9)
  213.     term.write("OK")
  214.     term.setBackgroundColor(colors.black)
  215.     term.setTextColor(colors.white)
  216.     done = false
  217.     doneOk = false
  218.     while not done do
  219.         event, side, xPos, yPos = os.pullEvent()
  220.         if event == eventType then
  221.             if (xPos == sizeX-5 or xPos == sizeX-6) and yPos == 9 then
  222.                 doneOk = true
  223.                 done = true
  224.             elseif xPos >= 6 and xPos <= 12 and yPos == 9 then
  225.                 done = true
  226.             end
  227.         elseif event == "key" then
  228.             if keys.getName(side) == "enter" then
  229.                 doneOk = true
  230.                 done = true
  231.             end
  232.         end
  233.     end
  234.     if doneOk then
  235.         term.clear()
  236.         term.setCursorPos(1,1)
  237.         running = false
  238.     end
  239. end
  240.  
  241. --Background
  242. local gridUnit = 8 -- 8 cut the grid by chunks of 8 blocks
  243.  
  244. local bgWindow = window.create(monitor, 1, 1, sizeX, sizeY, true)
  245. for X = 1, sizeX do
  246.     for Y = 1, sizeY do
  247.         bgWindow.setCursorPos(X, Y)
  248.         bgWindow.setBackgroundColor(colors.black)
  249.         if X > 1 then
  250.             bgWindow.setBackgroundColor(colors.lightGray)
  251.         end
  252.         if (X % 2 == 0 and Y % 2 ~= 0) or (X % 2 ~= 0 and Y % 2 == 0) and X > 1 then
  253.             bgWindow.setBackgroundColor(colors.gray)
  254.         end
  255.         bgWindow.write(" ")
  256.     end
  257. end
  258.  
  259. -- Buttons
  260. bgWindow.setBackgroundColor(colors.black)
  261. bgWindow.setCursorPos(1, 2)
  262. bgWindow.write("+")
  263. bgWindow.setCursorPos(1, 3)
  264. bgWindow.write("-")
  265. bgWindow.setCursorPos(1, 4)
  266. bgWindow.write("C")
  267. bgWindow.setCursorPos(1, 5)
  268. bgWindow.write("P")
  269. bgWindow.setCursorPos(1, sizeY-2)
  270. bgWindow.write("O")
  271. bgWindow.setCursorPos(1, sizeY-1)
  272. bgWindow.write("S")
  273. bgWindow.setCursorPos(1, sizeY)
  274. bgWindow.write("Q")
  275.        
  276. local function drawGridNumbers()
  277.     monitor.setBackgroundColor(colors.black)
  278.     monitor.setCursorPos(1, 1)
  279.     monitor.write(string.format("%02d", currentLayer))
  280.     bgWindow.setCursorPos(1, 7)
  281.     if tool then
  282.         bgWindow.write("o")
  283.     else
  284.         bgWindow.write("#")
  285.     end
  286.     bgWindow.setCursorPos(1, 8)
  287.     bgWindow.setBackgroundColor(toolColors[toolColor])
  288.     bgWindow.setTextColor(colors.black)
  289.     bgWindow.write(toolColor)
  290.     bgWindow.setTextColor(colors.white)
  291.     bgWindow.setBackgroundColor(colors.black)
  292.     for X = 1, sizeX do
  293.         for Y = 1, sizeY do
  294.             monitor.setCursorPos(X, Y)
  295.             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
  296.                 monitor.setBackgroundColor(colors.red)
  297.             end
  298.             if X == 2 and Y > 1 then
  299.                 if Y-1 == 1 then
  300.                     if offsetY > 0 then
  301.                         monitor.write("U")
  302.                     else
  303.                         monitor.write("1")
  304.                     end
  305.                 elseif Y == sizeY then
  306.                     monitor.write("D")
  307.                 elseif (Y-1+offsetY) % gridUnit == 0 then
  308.                     monitor.write(string.sub(Y-1+offsetY, 1))
  309.                 end
  310.             elseif X > 2 and Y == 1 then
  311.                 if X-2 == 1 then
  312.                     if offsetX > 0 then
  313.                         monitor.write("L")
  314.                     else
  315.                         monitor.write("1")
  316.                     end
  317.                 elseif X == sizeX then
  318.                     monitor.write("R")
  319.                 elseif (X-2+offsetX) % gridUnit == 0 then
  320.                     monitor.write(string.sub(X-2+offsetX, 1))
  321.                 end
  322.             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
  323.                 monitor.write(" ")
  324.             end
  325.         end
  326.     end
  327. end
  328.  
  329. function draw()
  330.     bgWindow.redraw()
  331.     drawGridNumbers()
  332.     monitor.setBackgroundColor(colors.white)
  333.     for X = 3, sizeX do
  334.         for Y = 2, sizeY do
  335.             if tmpBp[X-2+offsetX][Y-1+offsetY] ~= nil then
  336.                 monitor.setBackgroundColor(toolColors[tmpBp[X-2+offsetX][Y-1+offsetY]])
  337.                 monitor.setCursorPos(X, Y)
  338.                 monitor.write(" ")
  339.             end
  340.         end
  341.     end
  342.     monitor.setBackgroundColor(colors.black)
  343. end
  344.  
  345. while running do
  346.  
  347.     -- Drawing
  348.     draw()
  349.    
  350.     -- Catching Events
  351.     event, side, xPos, yPos = os.pullEvent()
  352.    
  353.     if xPos ~= nil and yPos ~= nil then
  354.         if event == eventType then
  355.             if xPos > 2 and yPos > 1 then
  356.                 tmpBp = deepcopy(blueprint[currentLayer])
  357.                 if tmpBp[xPos-2+offsetX][yPos-1+offsetY] ~= nil and tmpBp[xPos-2+offsetX][yPos-1+offsetY] == toolColor then
  358.                     clicAction = nil
  359.                 else
  360.                     clicAction = toolColor
  361.                 end
  362.                 tmpBp[xPos-2+offsetX][yPos-1+offsetY] = clicAction
  363.                 blueprint[currentLayer] = deepcopy(tmpBp)
  364.                 lastXPos = xPos + offsetX
  365.                 lastYPos = yPos + offsetY
  366.             elseif xPos == 1 and yPos == 2 and currentLayer < 98 then -- Layer +
  367.                 currentLayer = currentLayer + 1
  368.                 if blueprint[currentLayer] == nil then
  369.                     blueprint[currentLayer] = {}
  370.                     blueprint[currentLayer] = deepcopy(emptyLayer)
  371.                 end
  372.                 tmpBp = deepcopy(blueprint[currentLayer])
  373.             elseif xPos == 1 and yPos == 3 and currentLayer > 1 then -- Layer -
  374.                 currentLayer = currentLayer - 1
  375.                 tmpBp = deepcopy(blueprint[currentLayer])
  376.             elseif xPos == 1 and yPos == 4 then -- Layer copy
  377.                 copyLayer = deepcopy(blueprint[currentLayer])
  378.             elseif xPos == 1 and yPos == 5 and copyLayer ~= nil then -- Layer paste
  379.                 tmpBp = deepcopy(copyLayer)
  380.                 blueprint[currentLayer] = deepcopy(copyLayer)
  381.             elseif xPos == sizeX and yPos == 1 and offsetX + sizeX < 66 then -- offsetX +
  382.                 offsetX = offsetX + 1
  383.             elseif xPos == 3 and yPos == 1 and offsetX > 0 then -- offsetX -
  384.                 offsetX = offsetX - 1
  385.             elseif xPos == 2 and yPos == sizeY and offsetY + sizeY < 65 then -- offsetY +
  386.                 offsetY = offsetY + 1
  387.             elseif xPos == 2 and yPos == 2 and offsetY > 0 then -- offsetY -
  388.                 offsetY = offsetY - 1
  389.             elseif xPos == 2 and yPos == 1 then -- offsetY -
  390.                 offsetX = 0
  391.                 offsetY = 0
  392.             elseif xPos == 1 and yPos == 7 then -- offsetY -
  393.                 tool = not tool
  394.             elseif xPos == 1 and yPos == 8 then -- offsetY -
  395.                 if side == 1 then
  396.                     if toolColor < #toolColors then
  397.                         toolColor = toolColor + 1
  398.                     else
  399.                         toolColor = 1
  400.                     end
  401.                 else
  402.                     if toolColor > 1 then
  403.                         toolColor = toolColor - 1
  404.                     else
  405.                         toolColor = #toolColors
  406.                     end
  407.                 end
  408.             elseif xPos == 1 and yPos == sizeY-2 then -- offsetY -
  409.                 Load()
  410.             elseif xPos == 1 and yPos == sizeY-1 then -- offsetY -
  411.                 Save()
  412.             elseif xPos == 1 and yPos == sizeY then -- offsetY -
  413.                 cleanQuit()
  414.             end
  415.         elseif event == "mouse_drag" then
  416.             if xPos > 2 and yPos > 1 then
  417.                 tmpBp = deepcopy(blueprint[currentLayer])
  418.                 local tmpX = lastXPos
  419.                 local tmpXmax = xPos + offsetX
  420.                 local tmpY = lastYPos
  421.                 local tmpYmax = yPos + offsetY
  422.                
  423.                 if tmpX > tmpXmax then
  424.                     tmpX = xPos + offsetX
  425.                     tmpXmax = lastXPos
  426.                 end
  427.                 if tmpY > tmpYmax then
  428.                     tmpY = yPos + offsetY
  429.                     tmpYmax = lastYPos
  430.                 end
  431.                 if tool then
  432.                     -- Cercle
  433.                     if tmpXmax - tmpX > tmpYmax - tmpY then
  434.                         x = 0
  435.                         y = tmpXmax - tmpX
  436.                     else
  437.                         x = 0
  438.                         y = tmpYmax - tmpY
  439.                     end
  440.                     m = 5 - 4 * y
  441.                     -- while x <= y do -- make empty circles
  442.                         -- if x+lastXPos-2 > 0 and y+lastYPos-1 > 0 and x+lastXPos-2 < 65 and y+lastYPos-1 < 65 then
  443.                             -- tmpBp[x+lastXPos-2][y+lastYPos-1] = clicAction
  444.                         -- end
  445.                         -- if x+lastYPos-1 > 0 and y+lastXPos-2 > 0 and x+lastYPos-1 < 65 and y+lastXPos-2 < 65 then
  446.                             -- tmpBp[y+lastXPos-2][x+lastYPos-1] = clicAction
  447.                         -- end
  448.                         -- if -x+lastXPos-2 > 0 and y+lastYPos-1 > 0 and -x+lastXPos-2 < 65 and y+lastYPos-1 < 65 then
  449.                             -- tmpBp[-x+lastXPos-2][y+lastYPos-1] = clicAction
  450.                         -- end
  451.                         -- if x+lastYPos-1 > 0 and -y+lastXPos-2 > 0 and x+lastYPos-1 < 65 and -y+lastXPos-2 < 65 then
  452.                             -- tmpBp[-y+lastXPos-2][x+lastYPos-1] = clicAction
  453.                         -- end
  454.                         -- if x+lastXPos-2 > 0 and -y+lastYPos-1 > 0 and x+lastXPos-2 < 65 and -y+lastYPos-1 < 65 then
  455.                             -- tmpBp[x+lastXPos-2][-y+lastYPos-1] = clicAction
  456.                         -- end
  457.                         -- if -x+lastYPos-1 > 0 and y+lastXPos-2 > 0 and -x+lastYPos-1 < 65 and y+lastXPos-2 < 65 then
  458.                             -- tmpBp[y+lastXPos-2][-x+lastYPos-1] = clicAction
  459.                         -- end
  460.                         -- if -x+lastXPos-2 > 0 and -y+lastYPos-1 > 0 and -x+lastXPos-2 < 65 and -y+lastYPos-1 < 65 then
  461.                             -- tmpBp[-x+lastXPos-2][-y+lastYPos-1] = clicAction
  462.                         -- end
  463.                         -- if -x+lastYPos-1 > 0 and -y+lastXPos-2 > 0 and -x+lastYPos-1 < 65 and -y+lastXPos-2 < 65 then
  464.                             -- tmpBp[-y+lastXPos-2][-x+lastYPos-1] = clicAction
  465.                         -- end
  466.                         -- if m > 0 then
  467.                             -- y = y - 1
  468.                             -- m = m - 8 * y
  469.                         -- end
  470.                         -- x = x + 1
  471.                         -- m = m + 8 * x + 4
  472.                     -- end
  473.                     while x <= y do
  474.                         for fillY = 1, y do -- make plain circles
  475.                             if x+lastXPos-2 > 0 and fillY+lastYPos-1 > 0 and x+lastXPos-2 < 65 and fillY+lastYPos-1 < 65 then
  476.                                 tmpBp[x+lastXPos-2][fillY+lastYPos-1] = clicAction
  477.                             end
  478.                             if x+lastYPos-1 > 0 and fillY+lastXPos-2 > 0 and x+lastYPos-1 < 65 and fillY+lastXPos-2 < 65 then
  479.                                 tmpBp[fillY+lastXPos-2][x+lastYPos-1] = clicAction
  480.                             end
  481.                             if -x+lastXPos-2 > 0 and fillY+lastYPos-1 > 0 and -x+lastXPos-2 < 65 and fillY+lastYPos-1 < 65 then
  482.                                 tmpBp[-x+lastXPos-2][fillY+lastYPos-1] = clicAction
  483.                             end
  484.                             if x+lastYPos-1 > 0 and -fillY+lastXPos-2 > 0 and x+lastYPos-1 < 65 and -fillY+lastXPos-2 < 65 then
  485.                                 tmpBp[-fillY+lastXPos-2][x+lastYPos-1] = clicAction
  486.                             end
  487.                             if x+lastXPos-2 > 0 and -fillY+lastYPos-1 > 0 and x+lastXPos-2 < 65 and -fillY+lastYPos-1 < 65 then
  488.                                 tmpBp[x+lastXPos-2][-fillY+lastYPos-1] = clicAction
  489.                             end
  490.                             if -x+lastYPos-1 > 0 and fillY+lastXPos-2 > 0 and -x+lastYPos-1 < 65 and fillY+lastXPos-2 < 65 then
  491.                                 tmpBp[fillY+lastXPos-2][-x+lastYPos-1] = clicAction
  492.                             end
  493.                             if -x+lastXPos-2 > 0 and -fillY+lastYPos-1 > 0 and -x+lastXPos-2 < 65 and -fillY+lastYPos-1 < 65 then
  494.                                 tmpBp[-x+lastXPos-2][-fillY+lastYPos-1] = clicAction
  495.                             end
  496.                             if -x+lastYPos-1 > 0 and -fillY+lastXPos-2 > 0 and -x+lastYPos-1 < 65 and -fillY+lastXPos-2 < 65 then
  497.                                 tmpBp[-fillY+lastXPos-2][-x+lastYPos-1] = clicAction
  498.                             end
  499.                         end
  500.                         if m > 0 then
  501.                             y = y - 1
  502.                             m = m - 8 * y
  503.                         end
  504.                         x = x + 1
  505.                         m = m + 8 * x + 4
  506.                     end
  507.                 else
  508.                     -- Pavé
  509.                     for x = tmpX-2, tmpXmax-2 do
  510.                         for y = tmpY-1, tmpYmax-1 do
  511.                             if xPos > 2 and yPos > 1 then
  512.                                 tmpBp[x][y] = clicAction
  513.                             end
  514.                         end
  515.                     end
  516.                 end
  517.             end
  518.             if xPos == sizeX and offsetX + sizeX < 66 then
  519.                 offsetX = offsetX + 1
  520.             elseif xPos < 3 and offsetX > 0 then -- offsetX -
  521.                 offsetX = offsetX - 1
  522.             elseif yPos == sizeY and offsetY + sizeY < 65 then
  523.                 offsetY = offsetY + 1
  524.             elseif yPos < 2 and offsetY > 0 then -- offsetY -
  525.                 offsetY = offsetY - 1
  526.             end
  527.         elseif event == "mouse_up" then
  528.             blueprint[currentLayer] = deepcopy(tmpBp)
  529.         end
  530.     end
  531.    
  532.     sleep(0)
  533. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement