Advertisement
SpitefulJames

SpiteDEDialer.lib.lua

Apr 23rd, 2021
998
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.32 KB | None | 0 0
  1. -- Dialer, by SpitefulJames
  2. -- Using original code by Roy Curtis and KJ4IPS @
  3. -- files.haun.guru/ComputerCraft/
  4.  
  5. function debug(...)
  6.     if not debugging then return end
  7.     print('[Debug] ', unpack(arg) )
  8.   end
  9.  
  10.   function debugTable(tbl)
  11.     if not debugging then return end
  12.     for key, value in pairs(tbl) do
  13.       print(key, ': ', value)
  14.     end
  15.   end
  16.  
  17.   function discoverPeripherals()
  18.     pMonitor = waitForPeripheral('monitor')
  19.     pChest   = waitForPeripheral('chest')
  20.     pMusic   = waitForPeripheral('note_block')
  21.     debug('Discovered perhiperals')
  22.   end
  23.  
  24.   function waitForPeripheral(type)
  25.     unit = nil
  26.     repeat
  27.       unit = peripheral.find(type)
  28.  
  29.       if (unit == nil) then
  30.         if (pMusic ~= nil) then
  31.           pMusic.playSound('mob.bat.hurt', 1, 1)
  32.         end
  33.         print('Waiting for peripheral: ', type)
  34.         sleep(2.5)
  35.       end
  36.     until unit ~= nil
  37.     return unit
  38.   end
  39.  
  40.   function prepareUI()
  41.     pMonitor.setTextScale(state.scale)
  42.  
  43.     state.width,
  44.     state.height = pMonitor.getSize()
  45.     debug('UI prepared')
  46.   end
  47.  
  48.   function clearUI()
  49.     pMonitor.setBackgroundColor(colors.black)
  50.     pMonitor.clear()
  51.     debug('UI cleared')
  52.   end
  53.  
  54.   function adjustScale(delta)
  55.     state.scale = state.scale + delta
  56.     if     state.scale > 5.0 then state.scale = 5
  57.     elseif state.scale < 0.5 then state.scale = 0.5
  58.     end
  59.     prepareUI()
  60.     print('Adjusted scale to ', state.scale)
  61.   end
  62.  
  63.   function adjustColWidth(delta)
  64.     state.entryW = state.entryW + delta
  65.     if     state.entryW > 50 then state.entryW = 50
  66.     elseif state.entryW < 1  then state.entryW = 1
  67.     end
  68.     print('Adjusted col. width to ', state.entryW)
  69.   end
  70.  
  71.   function setPortal(book, slot)
  72.     -- Structure of a link book:
  73.     -- book.display_name
  74.     -- book.myst_book.destination
  75.     rs.setOutput('top',true)
  76.     sleep(2)
  77.     rs.setOutput('top',false)
  78.     state.selected = nil
  79.  
  80.     if (book == nil and slot == nil) then
  81.       debug('Portal reset')
  82.     else
  83.       pMusic.playSound('portal.travel', 1, 1)
  84.       pChest.pushItem('UP', slot)
  85.       state.selected = book
  86.       debug('Portal set to: ', book.display_name)
  87.     end
  88.   end
  89.  
  90.   function updateButtons()
  91.     state.entryX,
  92.     state.entryY  = 2, 5
  93.     state.buttons = {}
  94.    
  95.     -- Have to sort book items using indicies than
  96.     -- by table keys
  97.     stacks = pChest.getAllStacks()
  98.     books  = {}
  99.     for slot, stack in pairs(stacks) do
  100.       -- OpenPeripheralCore 1.0 returns a "query"
  101.       -- object rather than the itemstack itself.
  102.       -- .all() from that object fetches all the
  103.       -- properties as before.
  104.       if (stack.all ~= nil) then
  105.         stack = stack.all()
  106.       end
  107.  
  108.       if (stack.name == "teleporterMKI") then
  109.         stack.name = getBookName(stack)
  110.         stack.slot = slot
  111.         table.insert(books, stack)
  112.       end
  113.     end
  114.  
  115.     table.sort(books, sortBook)
  116.     for _, book in ipairs(books) do
  117.       addButton(book)
  118.     end
  119.     debug('Updated buttons')
  120.   end
  121.  
  122.   function getBookName(book)
  123.     if (book.display_name == "Charm of Dislocation") then
  124.         return book.display_name
  125.     end
  126.   end
  127.  
  128.   function sortBook(a, b)
  129.     return a.name:lower()
  130.          < b.name:lower()
  131.   end
  132.  
  133.   function addButton(book)
  134.     button   = {}
  135.  
  136.     button.x    = state.entryX
  137.     button.y    = state.entryY
  138.     button.name = book.name
  139.     button.lbl  = ' '..book.name:sub(0, 20)..' '
  140.     button.xEnd = state.entryX + state.entryW
  141.     button.book = book
  142.     button.slot = book.slot
  143.  
  144.     table.insert(state.buttons, button)
  145.     nextEntryPos()
  146.   end
  147.  
  148.   function blinkButton(button)
  149.     for i = 0,3 do
  150.       pMonitor.setCursorPos(button.x, button.y)
  151.  
  152.       if (i % 2 == 0) then
  153.         pMonitor.setBackgroundColor(colors.gray)
  154.       else
  155.         pMonitor.setBackgroundColor(colors.blue)
  156.       end
  157.  
  158.       pMonitor.write(button.lbl)
  159.       sleep(0.1)
  160.     end
  161.   end
  162.  
  163.   function nextEntryPos()
  164.     state.entryY = state.entryY + 2
  165.  
  166.     if (state.entryY >= state.height) then
  167.       state.entryX = state.entryX + state.entryW
  168.       state.entryY = 5
  169.     end
  170.   end
  171.  
  172.   function drawBanner(text)
  173.     pMonitor.setCursorPos(1, 1)
  174.     pMonitor.setBackgroundColor(colors.blue)
  175.     pMonitor.setTextColor(colors.white)
  176.  
  177.     drawFilledBox(pMonitor, 1, 1, state.width, 3)
  178.     pMonitor.setCursorPos(2, 2)
  179.  
  180.     if (text ~= nil) then
  181.       pMonitor.write(text)
  182.     elseif (state.selected ~= nil) then
  183.       pMonitor.write('Destination: '
  184.         .. state.selected.name)
  185.     else
  186.       pMonitor.write('Please select a destination')
  187.     end
  188.   end
  189.  
  190.   function drawButtons()
  191.     for _, button in ipairs(state.buttons) do
  192.       pMonitor.setBackgroundColor(colors.gray)
  193.  
  194.       -- Special cases
  195.       if     (button.name == 'The End') then
  196.         pMonitor.setTextColor(colors.lightBlue)
  197.       elseif (button.name == 'The Nether') then
  198.         pMonitor.setTextColor(colors.red)
  199.       elseif (button.name == 'The Deep Dark') then
  200.         pMonitor.setTextColor(colors.black)
  201.       elseif (button.name == 'The Last Millenium') then
  202.         pMonitor.setTextColor(colors.lightGray)
  203.       else
  204.         pMonitor.setTextColor(colors.white)
  205.       end
  206.  
  207.       pMonitor.setCursorPos(button.x, button.y)
  208.       pMonitor.write(button.lbl)
  209.     end  
  210.   end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement