Advertisement
Marikc0

[DL] Marik's CC/OpenP API

Nov 21st, 2013
4,630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.90 KB | None | 0 0
  1. -- Marik's CC/OpenPeripherals API
  2.  
  3. function clearPos(x, y, side)
  4. -- clears a specific line @ x, y and side
  5.  
  6.     m = peripheral.wrap(side)
  7.  
  8.     m.setCursorPos(x,y)
  9.     m.clearLine()
  10.  
  11. end
  12.  
  13. function appendString(side, color, text, clear, x, y)
  14. -- side: check program config (ie: left, right, aux_1)
  15. -- color: computercraft color codes
  16. -- text: the text to append
  17. -- clear: true or false (clear the line when you append the string?)
  18. -- x or y: if either is zero it uses the getCursorPos() x or y
  19.  
  20.     local m = peripheral.wrap(side)
  21.     local a,b = m.getCursorPos()
  22.    
  23.     if x == 0 and y ~= 0 then
  24.         m.setCursorPos(a,y)
  25.     elseif x ~= 0 and y == 0 then
  26.         -- set x position
  27.         m.setCursorPos(x,b)
  28.     elseif x ~= 0 and y ~= 0 then
  29.         m.setCursorPos(x,y)
  30.     elseif x == 0 and y == 0 then
  31.         m.setCursorPos(a,b)
  32.     end
  33.    
  34.     m.setTextColor(color)
  35.    
  36.     if clear == true then
  37.         m.clearLine()
  38.     end
  39.  
  40.     m.write(text)
  41.  
  42. end
  43.  
  44. function cString(x, y, color, side, text)
  45. -- x, y: position coords
  46. -- color: computercraft color codes
  47. -- side: check program config (ie: left, right, aux_1)
  48. -- text: any string
  49.  
  50. local m = peripheral.wrap(side)
  51.  
  52.     m.setCursorPos(x,y)
  53.     m.setTextColor(color)
  54.     m.write(text)
  55.    
  56. end
  57.  
  58. function getTank(tankPeriph)
  59.  
  60. local tableInfo = tankPeriph.getTankInfo("unknown")[1]
  61.  
  62. local fluidRaw, fluidName, fluidAmount, fluidCapacity, fluidID
  63.  
  64.     fluidRaw = tableInfo.rawName
  65.     fluidName = tableInfo.name
  66.     fluidAmount = tableInfo.amount
  67.     fluidCapacity = tableInfo.capacity
  68.     fluidID = tableInfo.id
  69.  
  70. return fluidRaw, fluidName, fluidAmount, fluidCapacity, fluidID
  71. end
  72.  
  73. function getBuckets(fluidAmount)
  74. -- Convert millibuckets to buckets
  75.  
  76.     fluidBuckets = fluidAmount / 1000
  77.  
  78. return fluidBuckets
  79. end
  80.  
  81. function numOfMachines()
  82.  
  83.     local machines = peripheral.getNames()
  84.     count = #machines
  85.     table.sort(machines)
  86.     n = 0
  87.    
  88.     for i=1, #machines do
  89.         if string.find(machines[i], "mfsu")
  90.         or string.find(machines[i], "mfe")
  91.         or string.find(machines[i], "cesu")  
  92.         or string.find(machines[i], "batbox") then
  93.             n = n + 1
  94.         end
  95.     end
  96.    
  97. return n, count
  98. end
  99.  
  100. function comma(number)
  101. -- Formats long numbers with commas
  102.    
  103.     if string.len(number) < 6 then
  104.         -- leave as is
  105.     elseif string.len(number) == 6 then
  106.         number = string.gsub(number, "^(-?%d+)(%d%d%d)", '%1,%2')
  107.     elseif string.len(number) >= 7 and string.len(number) <= 9 then
  108.         number = string.gsub(number, "^(-?%d+)(%d%d%d)(%d%d%d)", '%1,%2,%3')
  109.     elseif string.len(number) == 10 then -- need to fix
  110.         number = string.gsub(number, "^(-?%d+)(%d%d%d)(%d%d%d)(%d%d%d)", '%1,%2,%3,%4')
  111.     else
  112.         -- nothing
  113.     end
  114.  
  115. return number
  116. end
  117.  
  118. function itemRename(itemName)
  119. -- Shortens some long IC2/MFR/AE names to a more readable format
  120. -- This could be done with a lot less lines, but I'm still working on it.
  121.  
  122.     if (string.find(itemName, "ic2.item",1) == 1) then
  123.         itemName = string.sub(itemName, 9)
  124.         itemName = removeDots(itemName)
  125.  
  126.             if string.find(itemName, "%s") then
  127.                 -- If there's a space in the itemName like "Iron Ore" don't do anything to it!
  128.                 -- It's "probably" correct.
  129.             else
  130.                 -- It's probably "CrushedIronOre" or something similar.
  131.                 -- Fix it.
  132.                 itemName = spaceAtSecondUpper(itemName)
  133.             end        
  134.  
  135.     elseif (string.find(itemName, "ic2.",1) == 1) then
  136.         itemName = string.sub(itemName, 5)
  137.         itemName = removeDots(itemName)
  138.  
  139.             if string.find(itemName, "%s") then
  140.                 -- end
  141.             else
  142.                 itemName = spaceAtSecondUpper(itemName)
  143.             end    
  144.  
  145.     elseif (string.find(itemName, "tile.mfr.",1) == 1) then
  146.         itemName = string.sub(itemName, 10)
  147.     elseif (string.find(itemName, "appeng.materials.",1) == 1)
  148.         or (string.find(itemName, "AppEng.Materials.",1) == 1) then
  149.         itemName = string.sub(itemName, 18)
  150.         itemName = removeDots(itemName)
  151.         itemName = spaceAtSecondUpper(itemName)
  152.     elseif (string.find(itemName, "item.mfr.",1) == 1) then
  153.         itemName = string.sub(itemName, 10)
  154.         itemName = removeDots(itemName)
  155.     elseif (string.find(itemName, "rubberwood.log.name",1) == 1) then
  156.         itemName = removeDots(itemName)
  157.     end
  158.  
  159.  
  160. return itemName
  161. end
  162.  
  163. function removeDots(itemName)
  164. -- remove any "." in itemName and replaces with spaces
  165. -- also remove any occurence of "name" in the ore
  166.  
  167.     itemName = string.gsub(itemName, "%.", " ") -- change rubber.raw.name to "rubber raw name"
  168.     itemName = string.gsub(itemName, "%name", "") -- change "rubber raw name" to "rubber raw"
  169.     itemName = string.gsub(itemName, "block", "") -- remove "block" from the name
  170.  
  171. return itemName
  172. end
  173.  
  174. function spaceAtSecondUpper(itemName)
  175. -- Insert a space before each occurence of a capital letter
  176. -- Remove the leading space
  177. -- This is to change ore names like "PurifiedCrushedCopperOre" to "Purified Crushed Copper Ore"
  178.  
  179.     itemName = itemName:gsub("(%S)(%u)", "%1 %2")
  180.     itemName = itemName:gsub("^%s*(.-)%s*$", "%1")
  181.  
  182. return itemName
  183. end
  184.  
  185. function GPSinfo()
  186.  
  187. local x,y,z = gps.locate(5)
  188.     if x == nil then
  189.         return false
  190.     else
  191.         return true
  192.     end
  193. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement