SHARE
TWEET

Totoro Elevator [OC+RC]

MoonlightOwl Oct 30th, 2014 (edited) 146 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local event = require('event')
  2. local sides = require('sides')
  3. local term = require('term')
  4. local com = require('component')
  5.  
  6. --   Константы   --
  7. TRACK_SIDE = 'right'
  8.  
  9. --     Цвета     --
  10. backcolor = 0x000000
  11. forecolor = 0xFFFFFF
  12. infocolor = 0x0066FF
  13. errorcolor = 0xFF0000
  14. helpcolor = 0x006600
  15. graycolor = 0x080808
  16. goldcolor = 0xFFDF00
  17. --      ***      --
  18.  
  19. -- списки этажей (пульты и контроллеры)
  20. local scr = {}
  21. local red = {}
  22.  
  23. -- первый экран - цокольный этаж
  24. local cg = com.gpu
  25. table.insert(scr, com.screen.address)
  26.  
  27. width, height = cg.getResolution()
  28. cols = 1
  29. rows = 1
  30.  
  31. -- добавляем еще один этаж
  32. function add(screen, redstone)
  33.   if screen ~= nil then
  34.     table.insert(scr, screen)
  35.     cols = math.ceil(math.sqrt(#scr/3))
  36.     rows = math.ceil(#scr/cols)
  37.  
  38.     for c=1, #scr do
  39.       cg.bind(scr[c])
  40.       drawButtons()
  41.     end
  42.   end
  43.   if redstone ~= nil then
  44.     table.insert(red, redstone)
  45.   end
  46. end
  47. -- отправляем вагонетку на нужный этаж
  48. function send(number)
  49.   for c=1, #red do
  50.     local redstone = com.proxy(red[c])
  51.     if redstone ~= nil then
  52.       if c < number then
  53.         redstone.setOutput(sides[TRACK_SIDE], 1)
  54.       else
  55.         redstone.setOutput(sides[TRACK_SIDE], 0)
  56.       end
  57.     end
  58.   end
  59. end
  60.  
  61. -- динамично рисуем кнопки по числу этажей
  62. function drawButtons()
  63.   cg.setResolution(cols*6, rows)
  64.   term.clear()
  65.   cg.setBackground(helpcolor)
  66.   for x = 0, cols-1 do
  67.     for y = 0, rows-1 do
  68.       local num = 1+x*rows+y
  69.       if num > #scr then break end
  70.       cg.set(1+x*6, 1+y, '[   ]')
  71.       cg.set(3+x*6, 1+y, tostring(num))
  72.     end
  73.   end
  74.   cg.setBackground(backcolor)
  75. end
  76.  
  77. drawButtons(11)
  78.  
  79. -- работаем
  80. while true do
  81.   name, addres, t, y = event.pull(10)
  82.   if name == 'key_down' then break
  83.   elseif name == 'component_added' then
  84.     if t == 'screen' then
  85.       add(addres, nil)
  86.     elseif t == 'redstone' then
  87.       add(nil, addres)
  88.     end
  89.   elseif name == 'touch' then
  90.     local x = math.floor(t/6)
  91.     local num = x*rows+y
  92.     send(num)
  93.   end
  94. end
  95.  
  96. cg.setResolution(width, height)
  97. term.clear()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top