Advertisement
LDDestroier

drawing toy (ComputerCraft)

Nov 26th, 2016
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.34 KB | None | 0 0
  1. --[[
  2.  pastebin get tfyqv2ww toy
  3.  std pb tfyqv2ww toy
  4. --]]
  5.  
  6. local channel = 180
  7. local modem
  8. local scr_x, scr_y = term.getSize()
  9. local valchar = "#"      --character used for fading
  10. local hedchar = "@"      --character used for tip of line
  11. local s = {              --default color combinations
  12.     [1] = { --A classical black-and-white color fade, perfect for looking classy and pretentious.
  13.         colors.black,
  14.         colors.gray,
  15.         colors.lightGray,
  16.         colors.white,
  17.     },
  18.     [2] = { --If you're feeling blue, then this randomly selected set of four colors should make you feel even worse!
  19.         colors.black,
  20.         colors.blue,
  21.         colors.cyan,
  22.         colors.lightBlue,
  23.     },
  24.     [3] = { --This one's purple. Tha-that's it. Like purple? Good.
  25.         colors.black,
  26.         colors.red,
  27.         colors.magenta,
  28.         colors.pink,
  29.     },
  30.     [4] = { --I'll admit, the creativity is lacking in this color. I mean, what was I thinking?
  31.         colors.black,
  32.         colors.gray,
  33.         colors.green,
  34.         colors.lime,
  35.     },
  36.     [5] = { --NOBODY CALLS ME YELLOW
  37.         colors.black,
  38.         colors.brown,
  39.         colors.orange,
  40.         colors.yellow,
  41.     },
  42. }
  43. local p = math.random(1,#s)
  44. local g = function(num,sa) --This interprets the color palate and turns it into a fadey thing.
  45.     if not sa then sa = s[p] end
  46.     local values = {
  47.         [1] = {bg=sa[1], txt=sa[1], char=valchar},
  48.         [2] = {bg=sa[1], txt=sa[2], char=valchar},
  49.         [3] = {bg=sa[2], txt=sa[1], char=valchar},
  50.         [4] = {bg=sa[2], txt=sa[2], char=valchar},
  51.         [5] = {bg=sa[3], txt=sa[2], char=valchar},
  52.         [6] = {bg=sa[3], txt=sa[3], char=valchar},
  53.         [7] = {bg=sa[3], txt=sa[4], char=valchar},
  54.         [8] = {bg=sa[4], txt=sa[3], char=valchar},
  55.         [9] = {bg=sa[4], txt=sa[4], char=hedchar},
  56.     }
  57.     if not num then return #values end
  58.     return values[num]
  59. end
  60. local size = g()
  61. local grid = {}
  62. local ah = {}
  63. for b = 1, scr_x do
  64.     ah[b] = {v = 0, r = s[p]}
  65. end
  66. for b = 1, scr_y do
  67.     grid[b] = ah
  68. end
  69.  
  70. local between = function(num,min,max)
  71.     return (num > min and num or min) < max and num or max
  72. end
  73.  
  74. local getDotsInLine = function( startX, startY, endX, endY ) --graciously stolen from the paintutils, and PAIN
  75.     local out = {}
  76.     startX = math.floor(startX)
  77.     startY = math.floor(startY)
  78.     endX = math.floor(endX)
  79.     endY = math.floor(endY)
  80.     if startX == endX and startY == endY then
  81.         out = {{x=startX,y=startY}}
  82.         return out
  83.     end
  84.     local minX = math.min( startX, endX )
  85.     if minX == startX then
  86.         minY = startY
  87.         maxX = endX
  88.         maxY = endY
  89.     else
  90.         minY = endY
  91.         maxX = startX
  92.         maxY = startY
  93.     end
  94.     local xDiff = maxX - minX
  95.     local yDiff = maxY - minY
  96.     if xDiff > math.abs(yDiff) then
  97.         local y = minY
  98.         local dy = yDiff / xDiff
  99.         for x=minX,maxX do
  100.             table.insert(out,{x=x,y=math.floor(y+0.5)})
  101.             y = y + dy
  102.         end
  103.     else
  104.         local x = minX
  105.         local dx = xDiff / yDiff
  106.         if maxY >= minY then
  107.             for y=minY,maxY do
  108.                 table.insert(out,{x=math.floor(x+0.5),y=y})
  109.                 x = x + dx
  110.             end
  111.         else
  112.             for y=minY,maxY,-1 do
  113.                 table.insert(out,{x=math.floor(x+0.5),y=y})
  114.                 x = x - dx
  115.             end
  116.         end
  117.     end
  118.     return out
  119. end
  120. local getModemInput = function()
  121.     while true do
  122.         local _,side,freq,rfreq,msg,dist = os.pullEvent("modem_message")
  123.         if freq == channel then
  124.             if type(msg) == "table" then
  125.                 if type(msg.x) == "number" and type(msg.y) == "number" and type(msg.r) == "table" then
  126.                     if (msg.x >= 1 and msg.x <= scr_x) and (msg.y >= 1 and msg.y <= scr_y) and (#msg.r == 4) then
  127.                         grid[msg.y][msg.x] = {v = size, r = msg.r}
  128.                     end
  129.                 end
  130.             end
  131.         end
  132.     end
  133. end
  134. local render = function(grid)
  135.     local q
  136.     for y = 1, #grid do
  137.         for x = 1, #grid[y] do
  138.             q = grid[y][x]
  139.             if q then
  140.                 term.setCursorPos(x,y)
  141.                 term.setTextColor(g( between( q.v+1, 1, size ),  q.r ).txt )
  142.                 term.setBackgroundColor(g( between(q.v+1,1,size), q.r ).bg )
  143.                 term.write(g(between(q.v+1,1,size),q.r).char)
  144.             end
  145.         end
  146.     end
  147. end
  148. local downByOne = function(grid)
  149.     local output = {}
  150.     for y = 1, #grid do
  151.         output[y] = {}
  152.         for x = 1, #grid[y] do
  153.             output[y][x] = {}
  154.             if grid[y][x].v > 0 then
  155.                 output[y][x].v = grid[y][x].v - 1
  156.             else
  157.                 output[y][x].v = 0
  158.             end
  159.             output[y][x].r = grid[y][x].r
  160.         end
  161.     end
  162.     return output
  163. end
  164. local getInput = function()
  165.     local mx,my,oldx,oldy,dots
  166.     while true do
  167.         local evt = {os.pullEvent()}
  168.         modem = peripheral.find("modem")
  169.         if modem then modem.open(channel) end
  170.         if evt[1] == "key" then
  171.             if evt[2] == keys.q then
  172.                 sleep(0)
  173.                 return
  174.             end
  175.         elseif evt[1] == "mouse_click" or evt[1] == "mouse_drag" then
  176.             oldx,oldy = mx or evt[3],my or evt[4]
  177.             mx,my = evt[3],evt[4]
  178.             dots = getDotsInLine(oldx,oldy,mx,my)
  179.             for a = 1, #dots do
  180.                 grid[dots[a].y][dots[a].x] = {v = size, r = s[p]}
  181.                 if modem then
  182.                     modem.transmit(channel,channel,{x=dots[a].x, y=dots[a].y, r=s[p]})
  183.                 end
  184.             end
  185.         elseif evt[1] == "mouse_up" then
  186.             mx,my = nil,nil
  187.         end
  188.     end
  189. end
  190. local dothRendering = function()
  191.     local t = false --term.current().setVisible
  192.     while true do
  193.         if t then t(false) end
  194.         render(grid)
  195.         if t then t(true) end
  196.         grid = downByOne(grid)
  197.         sleep(0)
  198.     end
  199. end
  200.  
  201. local funclist = {
  202.     getInput,
  203.     dothRendering,
  204.     getModemInput,
  205. }
  206.  
  207. parallel.waitForAny(unpack(funclist))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement