Advertisement
qwertyMAN_rus

Redcontrol (minecraft)

May 7th, 2016 (edited)
734
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.82 KB | None | 0 0
  1. --Redcontrol v1.1 by qwertyMAN
  2. local term = require("term")
  3. local event = require("event")
  4. local component = require("component")
  5. local redstone = component.redstone
  6. local gpu = component.gpu
  7. local disp = {gpu.getResolution()}
  8. local Foreground = gpu.getForeground()
  9. local Background = gpu.getBackground()
  10. local string = string
  11. local my_address = 1
  12. local address = {}
  13. local red = {}
  14. local bundled = {}
  15. local save_red = {}
  16. local save_bundled = {}
  17. local name = "Redcontrol v1.1 by qwertyMAN"
  18. local exit = false
  19. local width = 87
  20. local height = 20
  21. local timeout = 1
  22. local color = {
  23.     [0] = 0xffffff,  
  24.     [1] = 0xff7f00,
  25.     [2] = 0xff00ff,
  26.     [3] = 0x7f7fff,
  27.     [4] = 0xffff00,
  28.     [5] = 0x7fff00,
  29.     [6] = 0xff7fff,
  30.     [7] = 0x7f7f7f,
  31.     [8] = 0xbfbfbf,
  32.     [9] = 0x007fff,
  33.     [10] = 0x7f007f,
  34.     [11] = 0x0000ff,
  35.     [12] = 0x7f3f00,
  36.     [13] = 0x00ff00,
  37.     [14] = 0xff0000,
  38.     [15] = 0x000000,
  39.     back = 0x000000,
  40.     foreg = 0xffffff
  41. }
  42. local sides = {
  43.     [0] = "down ",
  44.     [1] = "up   ",
  45.     [2] = "back ",
  46.     [3] = "front",
  47.     [4] = "left ",
  48.     [5] = "right"
  49. }
  50.  
  51. local function draw()
  52.     term.clear()
  53.     gpu.set(2, 1, name)
  54.     local n,m = string.len(#address),string.len(my_address)
  55.     gpu.set(width-10-n-m, 1, tostring(my_address .. "/" .. #address))
  56.     gpu.setBackground(color[4])
  57.     gpu.setForeground(color[15])
  58.     gpu.set(width-8, 1, "<")
  59.     gpu.set(width-6, 1, ">")
  60.     gpu.setBackground(color[14])
  61.     gpu.set(width-4, 1, "   X")
  62.     gpu.setBackground(color.back)
  63.     gpu.setForeground(color.foreg)
  64.     for i=0,5 do
  65.         local y = 4+i*3
  66.         red[i] = redstone.getInput(i)
  67.         bundled[i] = redstone.getBundledInput(i)
  68.         save_red[i] = red[i]
  69.         save_bundled[i] = {}
  70.         gpu.set(2, y-1, sides[i])
  71.         gpu.setBackground(color[14])
  72.         if redstone.getOutput(i)==0 then
  73.             gpu.set(2, y, " ")
  74.         else
  75.             gpu.setForeground(color[15])
  76.             gpu.set(2, y, "G")
  77.         end
  78.         gpu.setBackground(color.back)
  79.         gpu.setForeground(color.foreg)
  80.         if red[i]==0 then
  81.             gpu.set(3, y, "  ")
  82.         else
  83.             gpu.setBackground(color[14])
  84.             gpu.set(3, y, "  ")
  85.             gpu.set(3, y, tostring(red[i]))
  86.             gpu.setBackground(color.back)
  87.         end
  88.         for j=0, 15 do
  89.             save_bundled[i][j] = bundled[i][j]
  90.             gpu.setBackground(color[j])
  91.             if redstone.getBundledOutput(i,j)==0 then
  92.                 gpu.set(7+j*5, y, " ")
  93.             else
  94.                 if j==10 or j==11 or j==15 then
  95.                     gpu.setForeground(color[0])
  96.                 else
  97.                     gpu.setForeground(color[15])
  98.                 end
  99.                 gpu.set(7+j*5, y, "G")
  100.             end
  101.             gpu.setBackground(color.back)
  102.             gpu.setForeground(color.foreg)
  103.             if bundled[i][j]==0 then
  104.                 gpu.set(8+j*5, y, "   ")
  105.             else
  106.                 gpu.setBackground(color[j])
  107.                 gpu.set(8+j*5, y, "   ")
  108.                 if not (j==10 or j==11 or j==15) then
  109.                     gpu.setForeground(color[15])
  110.                 end
  111.                 gpu.set(8+j*5, y, tostring(bundled[i][j]))
  112.             end
  113.         end
  114.         gpu.setBackground(color.back)
  115.         gpu.setForeground(color.foreg)
  116.     end
  117. end
  118.  
  119. local function changed(_,add,side)
  120.     if redstone.address == add then
  121.         red[side] = redstone.getInput(side)
  122.         bundled[side] = redstone.getBundledInput(side)
  123.         local y = 4+side*3
  124.         if red[side]~= save_red[side] then
  125.             if red[side]==0 then
  126.                 gpu.set(3, y, "  ")
  127.             else
  128.                 gpu.setBackground(color[14])
  129.                 gpu.set(3, y, "  ")
  130.                 gpu.set(3, y, tostring(red[side]))
  131.                 gpu.setBackground(color.back)
  132.             end
  133.         else
  134.             for i=0, #bundled[side] do
  135.                 if bundled[side][i]~=save_bundled[side][i] then
  136.                     if bundled[side][i]==0 then
  137.                         gpu.set(8+i*5, y, "   ")
  138.                     else
  139.                         gpu.setBackground(color[i])
  140.                         gpu.set(8+i*5, y, "   ")
  141.                         if not (i==10 or i==11 or i==15) then
  142.                             gpu.setForeground(color[15])
  143.                         end
  144.                         gpu.set(8+i*5, y, tostring(bundled[side][i]))
  145.                         gpu.setForeground(color.foreg)
  146.                         gpu.setBackground(color.back)
  147.                     end
  148.                     break
  149.                 end
  150.             end
  151.         end
  152.         save_red[side] = red[side]
  153.         save_bundled[side] = {}
  154.         for i=0, 15 do
  155.             save_bundled[side][i] = bundled[side][i]
  156.         end
  157.     end
  158. end
  159.  
  160. local function added(_,add)
  161.     table.insert(address,add)
  162.     local n,m = string.len(#address),string.len(my_address)
  163.     gpu.set(width-10-n-m, 1, tostring(my_address .. "/" .. #address))
  164. end
  165.  
  166. local function removed(_,add)
  167.     for i=1, #address do
  168.         if address[i]==add then
  169.             if redstone.address == add then
  170.                 if i == 1 then
  171.                     redstone = component.proxy(address[my_address+1])
  172.                 else
  173.                     my_address = my_address - 1
  174.                     redstone = component.proxy(address[my_address])
  175.                 end
  176.                 draw()
  177.             end
  178.             table.remove(address,i)
  179.             break
  180.         end
  181.     end
  182.     local n,m = string.len(#address),string.len(my_address)
  183.     gpu.set(width-10-n-m, 1, tostring(my_address .. "/" .. #address))
  184. end
  185.  
  186. local function touch(_,_,x,y)
  187.     local side = (y-4)/3
  188.     if side==math.modf(side) then
  189.         if x<5 and x>1 then
  190.             gpu.setBackground(color[14])
  191.             if redstone.getOutput(side)==0 then
  192.                 redstone.setOutput(side,15)
  193.                 gpu.setForeground(color[15])
  194.                 gpu.set(2, y, "G")
  195.             else
  196.                 redstone.setOutput(side,0)
  197.                 gpu.set(2, y, " ")
  198.             end
  199.             gpu.setBackground(color.back)
  200.         elseif y==1 then
  201.             if x==width-8 then
  202.                 if my_address == 1 then
  203.                     my_address = #address
  204.                 else
  205.                     my_address = my_address-1
  206.                 end
  207.                 redstone = component.proxy(address[my_address])
  208.                 draw()
  209.             elseif x==width-6 then
  210.                 if my_address == #address then
  211.                     my_address = 1
  212.                 else
  213.                     my_address = my_address+1
  214.                 end
  215.                 redstone = component.proxy(address[my_address])
  216.                 draw()
  217.             elseif x>width-5 then
  218.                 exit = true
  219.             end
  220.         else
  221.             local n = math.modf((x-7)/5)
  222.             gpu.setBackground(color[n])
  223.             if redstone.getBundledOutput(side,n)==0 then
  224.                 redstone.setBundledOutput(side,n,255)
  225.                 if n==10 or n==11 or n==15 then
  226.                     gpu.setForeground(color[0])
  227.                 else
  228.                     gpu.setForeground(color[15])
  229.                 end
  230.                 gpu.set(7+n*5, y, "G")
  231.             else
  232.                 redstone.setBundledOutput(side,n,0)
  233.                 gpu.set(7+n*5, y, " ")
  234.             end
  235.         end
  236.         gpu.setBackground(color.back)
  237.         gpu.setForeground(color.foreg)
  238.     end
  239. end
  240.  
  241. local function start()
  242.     event.shouldInterrupt = function() return false end
  243.     event.listen("touch", touch)
  244.     event.listen("redstone_changed", changed)
  245.     event.listen("component_added", added, nil,"redstone")
  246.     event.listen("component_removed", removed, nil,"redstone")
  247.     gpu.setBackground(color.back)
  248.     gpu.setForeground(color.foreg)
  249.     gpu.setResolution(width, height)
  250.     for add in component.list("redstone") do
  251.         table.insert(address,add)
  252.     end
  253.     redstone = component.proxy(address[my_address])
  254.     for i=1, #address do
  255.         if address[i] == redstone.address then
  256.             my_address = i
  257.             break
  258.         end
  259.     end
  260.     draw()
  261.     while true do
  262.         os.sleep(timeout)
  263.         if exit then
  264.             break
  265.         end
  266.     end
  267.     event.ignore("touch", touch)
  268.     event.ignore("redstone_changed", changed)
  269.     event.ignore("component_added", added)
  270.     event.ignore("component_removed", removed)
  271.     term.clear()
  272.     print("Good bye!")
  273.     os.sleep(1)
  274.     gpu.setBackground(Background)
  275.     gpu.setForeground(Foreground)
  276.     gpu.setResolution(disp[1], disp[2])
  277.     term.clear()
  278. end
  279.  
  280. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement