5tar-Kaster

BluPrint

Jun 7th, 2013
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.56 KB | None | 0 0
  1. -- external info --
  2. local tArgs = { ... }
  3. local retPath = tArgs[1]
  4. --if #tArgs <= 0 then retPath = "disk/OS/OS" end
  5. function prevApp()
  6.     shell.run(retPath)
  7. end
  8.  
  9. -- BluPrint --
  10. local filename = "/programFiles/bluprints/test"
  11.  
  12. local running = true
  13. local layer = 1
  14. local termW, termH = term.getSize()
  15. local offsW, offsH = 0, 0
  16.  
  17. local doResize = false
  18.  
  19. --blocks[height][length][width]
  20. local blocks = {
  21.     [1] = {
  22.         [1 ] = { 0,0,0,0,0,0,0,0,0,0 },
  23.         [2 ] = { 0,0,0,0,0,0,0,0,0,0 },
  24.         [3 ] = { 0,0,0,0,0,0,0,0,0,0 },
  25.         [4 ] = { 0,0,0,0,0,0,0,0,0,0 },
  26.         [5 ] = { 0,0,0,0,0,0,0,0,0,0 },
  27.     }
  28. }
  29.  
  30. --dim for bluprint
  31. local height = #blocks
  32. local length = #blocks[1]
  33. local width = #blocks[1][1]    
  34.  
  35. local pallet = {
  36.     [0] = colours.blue,
  37.     [1] = colours.white,
  38.     [2] = colours.orange,
  39.     [3] = colours.magenta,
  40.     [4] = colours.lightBlue,
  41.     [5] = colours.yellow,
  42.     [6] = colours.green,
  43.     [7] = colours.pink,
  44.     [8] = colours.grey,
  45.     [9] = colours.lime,
  46.     [10] = colours.cyan,
  47.     [11] = colours.purple,
  48.     [12] = colours.brown,
  49. }
  50.  
  51. local paint = 1
  52.  
  53. function save()
  54.     file = fs.open(filename, "w")
  55.     file.write(textutils.serialize(blocks))
  56.     file.close()
  57. end
  58. function load()
  59.     file = fs.open(filename, "r")
  60.     data = file.readAll()
  61.     file.close()
  62.    
  63.     blocks = textutils.unserialize(data)
  64.    
  65.     height = #blocks
  66.     length = #blocks[1]
  67.     width = #blocks[1][1]  
  68. end
  69. local controls = {
  70.     ["save"] = {
  71.         ["name"] = "[SAVE]",
  72.         ["x"] = 40,
  73.         ["y"] = 17,
  74.         ["l"] = 5,
  75.         run = function() save() end
  76.     },
  77.     ["load"] = {
  78.         ["name"] = "[LOAD]",
  79.         ["x"] = 42,
  80.         ["y"] = 18,
  81.         ["l"] = 5,
  82.         run = function() load() end
  83.     },
  84.     ["exit"] = {
  85.         ["name"] = "[EXIT]",
  86.         ["x"] = 44,
  87.         ["y"] = 19,
  88.         ["l"] = 5,
  89.         run = function() running = false  end
  90.     },
  91.     ["wid+"] = {
  92.         ["name"] = "[+]",
  93.         ["x"] = 41,
  94.         ["y"] = 2,
  95.         ["l"] = 2,
  96.         run = function() width = width + 1 doResize = true end
  97.     },
  98.     ["wid-"] = {
  99.         ["name"] = "[-]",
  100.         ["x"] = 47,
  101.         ["y"] = 2,
  102.         ["l"] = 2,
  103.         run = function() width = width - 1 doResize = true end
  104.     },
  105.     ["len+"] = {
  106.         ["name"] = "[+]",
  107.         ["x"] = 41,
  108.         ["y"] = 5,
  109.         ["l"] = 2,
  110.         run = function() length = length + 1 doResize = true end
  111.     },
  112.     ["len-"] = {
  113.         ["name"] = "[-]",
  114.         ["x"] = 47,
  115.         ["y"] = 5,
  116.         ["l"] = 2,
  117.         run = function() length = length - 1 doResize = true end
  118.     },
  119.     ["hit+"] = {
  120.         ["name"] = "[+]",
  121.         ["x"] = 41,
  122.         ["y"] = 9,
  123.         ["l"] = 2,
  124.         run = function() height = height + 1 doResize = true end
  125.     },
  126.     ["hit-"] = {
  127.         ["name"] = "[-]",
  128.         ["x"] = 47,
  129.         ["y"] = 9,
  130.         ["l"] = 2,
  131.         run = function() height = height - 1 doResize = true end
  132.     },
  133.     ["lay+"] = {
  134.         ["name"] = "[+]",
  135.         ["x"] = 41,
  136.         ["y"] = 13,
  137.         ["l"] = 2,
  138.         run = function() layer = layer + 1 end
  139.     },
  140.     ["lay-"] = {
  141.         ["name"] = "[-]",
  142.         ["x"] = 47,
  143.         ["y"] = 13,
  144.         ["l"] = 2,
  145.         run = function() layer = layer - 1 end
  146.     }
  147. }
  148.  
  149. -- HELPER FUNCTIONS --
  150. function clear()
  151.     term.setBackgroundColour(colours.black)
  152.     term.clear()
  153.     term.setCursorPos(1, 1)
  154. end
  155. function pos(x, y)
  156.     term.setCursorPos(x, y)
  157. end
  158. function posCol(x, y, col)
  159.     term.setCursorPos(x, y)
  160.     term.setBackgroundColour(col)
  161.     write(" ")
  162. end
  163. function posWrt(x, y, text, textCol)
  164.     term.setCursorPos(x, y)
  165.     term.setTextColour(textCol)
  166.     write(text)
  167. end
  168.  
  169. -- DEPENDANT FUNCTIONS --
  170. function drawPallet()
  171.     for i=1, #pallet do
  172.         posCol(termW, i, pallet[i])
  173.     end
  174. end
  175. function drawCanvas()
  176.     for x=1, width+2 do
  177.         posCol(x + offsW, 1 + offsH, colours.red)
  178.         posCol(x + offsW, length+2 + offsH, colours.red)
  179.     end
  180.  
  181.  
  182.     for y=1, length+2 do
  183.         posCol(1 + offsW, y + offsH, colours.red)
  184.         posCol(width+2 + offsW, y + offsH, colours.red)
  185.     end
  186.  
  187.    
  188.     --[[
  189.     for x=2 + offsW, width+1 + offsW do
  190.         for y=2 + offsH, length+1 + offsH do
  191.             posCol(x, y, colours.blue)
  192.         end
  193.     end
  194.     ]]
  195.    
  196.     for x=1, width do
  197.         for y=1, length do
  198.             posCol(x+1 + offsW, y+1 + offsH, pallet[blocks[layer][y][x]])
  199.         end
  200.     end
  201.    
  202. end
  203. function drawControls()
  204.    
  205.     for y=1, termH do
  206.         for x=termW-12, termW do
  207.             posCol(x, y, colours.lightGrey)
  208.         end
  209.     end
  210.    
  211.     posWrt(41, 1, "WIDTH  "..width, colours.black)
  212.     posWrt(41, 4, "LENGTH "..length, colours.black)
  213.     posWrt(41, 8, "HEIGHT "..height, colours.black)
  214.     posWrt(41, 12, "LAYER  "..layer, colours.black)
  215.    
  216.     for i, control in pairs(controls) do
  217.         x = control.x
  218.         y = control.y
  219.         name = control.name
  220.         posWrt(x, y, name, colours.black)
  221.     end
  222. end
  223.  
  224. debugInfo = ""
  225. function resize()      
  226.     temp = { } -- temp table
  227.    
  228.     -- inserting all the height levels
  229.     for h=1, height do table.insert( temp , { } ) end
  230.    
  231.     -- inserting all the lengths
  232.     for h=1, height do
  233.         for l=1, length do table.insert( temp[h], { } ) end
  234.     end
  235.    
  236.     -- inserting all the width and defaulting them to 0
  237.     for h=1, height do
  238.         for l=1, length do
  239.             for w=1, width do table.insert( temp[h][l] , 0 ) end
  240.         end
  241.     end
  242.    
  243.     -- if the canvas size is increasing
  244.     if #blocks <= height then
  245.         if #blocks[1] <= length then
  246.             if #blocks[1][1] <= width then
  247.                 for h=1, #blocks do
  248.                     for l=1, #blocks[1] do
  249.                         for w=1, #blocks[1][1] do
  250.                             -- fill in data from blocks
  251.                             temp[h][l][w] = blocks[h][l][w]
  252.                         end
  253.                     end
  254.                 end
  255.             end
  256.         end
  257.     end
  258.    
  259.     --if the canvas size is decreasing
  260.     if #blocks >= height then
  261.         if #blocks[1] >= length then
  262.             if #blocks[1][1] >= width then
  263.                 for h=1, #temp do
  264.                     for l=1, #temp[1] do
  265.                         for w=1, #temp[1][1] do
  266.                             -- fill in data from blocks but not the last value
  267.                             temp[h][l][w] = blocks[h][l][w]
  268.                         end
  269.                     end
  270.                 end
  271.             end
  272.         end
  273.     end
  274.    
  275.     -- overwrite blocks with the new dimensions
  276.     blocks = temp
  277. end
  278. function doClick(event)
  279.     xPos = event[3]
  280.     yPos = event[4]
  281.     --debugInfo = event[1].." "..xPos.." "..yPos
  282.    
  283.    
  284.     for i, control in pairs(controls) do
  285.         x = control.x   y = control.y
  286.         l = control.x + control.l
  287.         name = control.name
  288.         if xPos >= x and xPos <= l and yPos == y then
  289.             control.run()
  290.             if name == "[SAVE]" then debugInfo = "SAVED FILE "..filename end
  291.             if name == "[LOAD]" then debugInfo = "LOADD FILE "..filename end
  292.         end
  293.     end
  294.    
  295.     if width <= 0 then width = 1 end
  296.     if length <= 0 then length = 1 end
  297.     if height <= 0 then height = 1 end
  298.     if layer <= 0 then layer = 1 end
  299.     if layer >= height then layer = height end
  300.    
  301.     if doResize then resize() end
  302.    
  303.     if xPos == termW and yPos <= #pallet then
  304.         paint = yPos
  305.     end
  306.    
  307.     if xPos >= 39 then return end
  308.    
  309.     if (xPos) >= (2 + offsW) and (xPos) <= (#blocks[1][1]+1 + offsW) then
  310.         if (yPos) >= (2 + offsH) and (yPos) <= (#blocks[1]+1 + offsH) then
  311.             if event[2] == 1 then
  312.                 blocks[layer][yPos-1 - offsH][xPos-1 - offsW] = paint
  313.             else
  314.                 blocks[layer][yPos-1 - offsH][xPos-1 - offsW] = 0
  315.             end
  316.         end
  317.     end
  318. end
  319. function doKeyPress(event)
  320.     key = event[2]
  321.  
  322.     if key == keys.right then   offsW = offsW + 1   end
  323.     if key == keys.left  then   offsW = offsW - 1   end
  324.     if key == keys.up    then   offsH = offsH - 1   end
  325.     if key == keys.down  then offsH = offsH + 1 end
  326.    
  327.     if key == keys.one   then   paint = 1    end
  328.     if key == keys.two   then   paint = 2    end
  329.     if key == keys.three then   paint = 3    end
  330.     if key == keys.four  then   paint = 4    end
  331.     if key == keys.five  then   paint = 5    end
  332.     if key == keys.six   then   paint = 6    end
  333.     if key == keys.seven then   paint = 7    end
  334.     if key == keys.eight then   paint = 8    end
  335.     if key == keys.nine  then   paint = 9    end
  336.     if key == keys.zero  then   paint = 10 end
  337.    
  338.     if key == keys.q   then layer = layer + 1 end
  339.     if key == keys.a   then layer = layer - 1 end
  340.     if layer <= 0      then layer = 1             end
  341.     if layer >= height then layer = height    end
  342. end
  343.  
  344. -- PROGRAM FUNCTIONS --
  345. function draw()
  346.     clear()
  347.     --posWrt(15, 11, paint, pallet[1])
  348.     drawCanvas()
  349.     drawControls()
  350.     drawPallet()
  351.     term.setBackgroundColour(colours.black)
  352.     posWrt(1, 19, debugInfo, colours.white)
  353. end
  354. function update()
  355.     --sleep(2)
  356.     --running = false
  357.     while true do
  358.         event = { os.pullEvent() }
  359.         if event[1] == "mouse_click" then
  360.             debugInfo = event[1].." "..event[3].." "..event[4]
  361.             doClick(event)
  362.             break
  363.         end
  364.         if event[1] == "key" then
  365.             debugInfo = event[1].." "..event[2]
  366.             doKeyPress(event)
  367.             break
  368.         end
  369.     end
  370. end
  371.  
  372.  
  373. -- RUN FUNCTION --
  374. function run()
  375.     clear()
  376.     while running do
  377.         draw()
  378.         update()
  379.     end
  380.     clear()
  381.     prevApp()
  382. end
  383. function startup()
  384.     clear()
  385.    
  386.     fs.makeDir("/programFiles/bluprints")
  387.     list = fs.list("/programFiles/bluprints")
  388.    
  389.     print("If you would like to load a file just type in the name below")
  390.     print()
  391.     print("LIST OF ALL AVAILABLE FILES")
  392.     textutils.pagedTabulate( list )
  393.    
  394.     filename = read()
  395.    
  396.     filename = "/programFiles/bluprints/"..filename
  397.    
  398.     if fs.exists(filename) then
  399.         load()
  400.     else
  401.         save()
  402.     end
  403. end
  404.  
  405. startup()
  406. run()
Advertisement
Add Comment
Please, Sign In to add comment