NielsUtrecht

hydraApi

Sep 8th, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.32 KB | None | 0 0
  1. -- Returns a table with the sides and peripheral types
  2. function getPeripheralList()
  3.     local sides = peripheral.getNames()
  4.     local pTable = {}
  5.    
  6.     for k,side in pairs(sides) do
  7.         pTable[side] = peripheral.getType(side)
  8.     end
  9.    
  10.     return pTable
  11. end
  12.  
  13. -- finds the first peripheral of type 'peripheralType' and wraps it
  14. function getPeripheral(peripheralType)
  15.     local pTable = getPeripheralList()
  16.     for side, pType in pairs(pTable) do
  17.         if(peripheralType == pType) then
  18.             return peripheral.wrap(side)
  19.         end
  20.     end
  21.     return nil
  22. end
  23.  
  24. -- finds all peripherals of type 'peripheralType' and wraps them
  25. function getAllPeripherals(peripheralType)
  26.     local pTable = getPeripheralList()
  27.     local wrappedTable = {}
  28.     for side, pType in pairs(pTable) do
  29.         if(peripheralType == pType) then
  30.             wrappedTable[side] = peripheral.wrap(side)
  31.         end
  32.     end
  33.     return wrappedTable
  34. end
  35.  
  36. -- finds the first modem and wraps it
  37. function getModem(wireless)
  38.     local modem = getAllPeripherals("modem")
  39.     for k, m in pairs(modem) do
  40.         if(m.isWireless() == wireless) then
  41.             return m
  42.         end
  43.     end
  44. end
  45.  
  46. -- finds the first monitor and wraps it
  47. function getMonitor()
  48.     return getPeripheral("monitor")
  49. end
  50.  
  51. -- finds the first crafting terminal and wraps it
  52. function getCraftingTerminal()
  53.     return getPeripheral("me_crafting_terminal")
  54. end
  55.  
  56. -- finds the first ME bridge and wraps it
  57. function getMeBridge()
  58.     return getPeripheral("meBridge")
  59. end
  60.  
  61. -- finds all redstone energy cells and wraps them
  62. function getAllEnergyCells()
  63.     return getAllPeripherals("redstone_energy_cell")
  64. end
  65.  
  66. -- finds all redstone energy cells and wraps them
  67. function getAllAlvearies()
  68.     return getAllPeripherals("alveary")
  69. end
  70.  
  71. -- String split function, splits pString on pPattern
  72. function split(pString, pPattern)
  73.    local Table = {}  -- NOTE: use {n = 0} in Lua-5.0
  74.    local fpat = "(.-)" .. pPattern
  75.    local last_end = 1
  76.    local s, e, cap = pString:find(fpat, 1)
  77.    while s do
  78.       if s ~= 1 or cap ~= "" then
  79.      table.insert(Table,cap)
  80.       end
  81.       last_end = e+1
  82.       s, e, cap = pString:find(fpat, last_end)
  83.    end
  84.    if last_end <= #pString then
  85.       cap = pString:sub(last_end)
  86.       table.insert(Table, cap)
  87.    end
  88.    return Table
  89. end
  90.  
  91. -- Debug function: prints a table
  92. function printTable(t)
  93.     if(t == nil) then
  94.         print("**nil**")
  95.         return
  96.     end
  97.     for k,v in pairs(t) do
  98.         print(tostring(k) .. "=" .. tostring(v))
  99.     end
  100. end
  101.  
  102. function formatLargeNumber(amount)
  103.         local postfix
  104.  
  105.         if(amount >= 1000000000) then
  106.                 amount = math.floor(amount / 100000000) / 10
  107.                 postFix = "G"
  108.         elseif(amount >= 1000000) then
  109.                 amount = math.floor(amount / 100000) / 10
  110.                 postFix = "M"
  111.         elseif(amount >= 1000) then
  112.                 amount = math.floor(amount / 100) / 10
  113.                 postFix = "k"
  114.         else
  115.                 postFix = ""        
  116.         end
  117.        
  118.         return string.format("%3.1f", amount) .. postFix
  119. end
  120.  
  121. function formatEnergy(amount)
  122.     return formatLargeNumber(amount) .. "MJ"
  123. end
  124.  
  125. function formatPercent(fraction)
  126.     percent = math.floor(fraction * 1000) / 10
  127.     return string.format("%3.1f", percent) .. "%"
  128. end
  129.  
  130. -- Debug function: prints all connected peripherals
  131. function printPeripherals()
  132.     printTable(getPeripheralList())
  133. end
Advertisement
Add Comment
Please, Sign In to add comment