abc123mewot

Utils API - Computercraft

Dec 11th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.98 KB | None | 0 0
  1. --Gets a specified capacitor bank from an id
  2. function cb(num)
  3.   return peripheral.wrap("capacitor_bank_" .. num)
  4. end
  5. --Gets a specified monitor and id
  6. function mon(num)
  7.   return peripheral.wrap("monitor_" .. num)
  8. end
  9. --Gets a specific terminal bridge from an ID
  10. function tbr(num)
  11.   return peripheral.wrap("openperipherals_bridge_" .. num);
  12. end
  13. --Gets a specified pnematiccraft compressor
  14. function fcmpr(num)
  15.   return peripheral.wrap("fluxCompressor_" .. num)
  16. end
  17. --Gets a specified computer
  18. function comp(num)
  19.   return peripheral.wrap("computer_" .. num)
  20. end
  21. --Maps a value of range to a new range
  22. function map(val, min, max, nmin, nmax)
  23.   return ((val - min) / (max - min)) * (nmax - nmin) + nmin
  24. end
  25. --Clamps a value to a range
  26. function clamp(val, min, max)
  27.   if val < min then return min end
  28.   if val > max then return max end
  29.   return val
  30. end
  31. --Clamps a value to a range (0, max)
  32. function clampMax(val, max)
  33.   if val < 0 then return 0 end
  34.   if val > max then return max end
  35.   return val
  36. end
  37. --Clamps a value to a range, in this case 0 to 1
  38. function clamp01(val)
  39.   if val < 0 then return 0 end
  40.   if val > 1 then return 1 end
  41.   return val
  42. end
  43. --Similar to blit, writes to monitor and colors the text
  44. function monWrt(mon, text, color)
  45.   if color ~= nil then mon.setTextColor(color) end
  46.   mon.write(text)
  47. end
  48. --Monitor newline
  49. function monNL(mon)
  50.   local x, y = mon.getCursorPos()
  51.   y = y + 1
  52.   mon.setCursorPos(x, y)
  53. end
  54. --Monitor carriage return
  55. function monCR(mon)
  56.   local x, y = mon.getCursorPos()
  57.   mon.setCursorPos(1, y)
  58. end
  59. -- Monitor newline and carriage return
  60. function monNLCR(mon)
  61.   local x, y = mon.getCursorPos()
  62.   y = y + 1
  63.   mon.setCursorPos(1, y)
  64. end
  65. --Reset monitor to defaults
  66. function rstMon(mon)
  67.   mon.clear()
  68.   mon.setCursorPos(1, 1)
  69.   mon.setCursorBlink(false)
  70.   mon.setTextScale(1)
  71.   mon.setBackgroundColor(colors.black)
  72.   mon.setTextColor(colors.white)
  73. end
  74. --Monitor write fixed length
  75. function monWrtFxdLen(mon, len, text, color)
  76.   local suff = ""
  77.   local msgLen = string.len(text)
  78.   local diff = len - msgLen
  79.   local x, y = mon.getCursorPos()
  80.   if msgLen > len then
  81.     monWrt(mon, string.sub(text, 0, len), color)
  82.   else
  83.     monWrt(mon, text, color)
  84.     mon.setCursorPos(x + len, y)
  85.   end
  86. end
  87. --Prints to the monitor centered, also adds a lne
  88. function printCentLn(mon, text)
  89.   local x, y = mon.getCursorPos()
  90.   local w2 = (mon.getSize()) / 2
  91.   local tl2 = string.len(text) / 2
  92.   mon.setCursorPos(w2 - tl2, y)
  93.   mon.write(text)
  94.   mon.setCursorPos(x, y + 1)
  95. end
  96. --Rounds a number to n places
  97. function round(num, detail)
  98.   local m = 10 ^ (detail or 0)
  99.   return math.floor(num * m + 0.5) / m
  100. end
  101. --Formats a rf power string into a nice, clean format
  102. function getPwrStr(pwr, perT, digits)
  103.   local d = math.floor(math.log10(pwr))
  104.   local suff = "RF"
  105.   if d >= 9 then
  106.     pwr = pwr / 1000000000
  107.     suff = "GRF"
  108.   elseif d >= 6 then
  109.     pwr = pwr / 1000000
  110.     suff = "MRF"
  111.   elseif d >= 3 then
  112.     pwr = pwr / 1000
  113.     suff = "KRF"
  114.   elseif d < 0 then d = 1 end
  115.   if perT then suff = suff .. "/t" end
  116.   local pwrStr = round(pwr, digits)
  117.   return pwrStr .. suff
  118. end
  119. --Checks if position is in bounds
  120. function chkBds(x, y, x1, y1, x2, y2)
  121.   if((x >= x1 and x <= x2) and (y >= y1 and y <= y2)) then
  122.     return true    
  123.   else
  124.     return false  
  125.   end
  126. end
  127. --Converts kelvin to celcius
  128. function kelvinToCelcius(kelvin)
  129.   return kelvin - 273.15
  130. end
  131. --Converts kelvin to celcius
  132. function celciusToKelvin(celcius)
  133.   return celcius + 273.15
  134. end
  135. --Converts farenheit to celcius
  136. function farenheitToCelcius(faren)
  137.   return (faren - 32.0) * (5.0 / 9.0)
  138. end
  139. --Converts celcius to farenheit
  140. function celciusToFarenheit(celcius)
  141.   return (celcius * (9.0 / 5.0)) + 32.0
  142. end
  143. --Converts kelvin to farenheit
  144. function kelvinToFarenheit(kelvin)
  145.   return celciusToFarenheit(kelvinToCelcius(kelvin))
  146. end
  147. --Converts farenheit to kelvin
  148. function farenheitToKelvin(faren)
  149.   return celciusToKelvin(farenheitToCelcius(faren))
  150. end
Add Comment
Please, Sign In to add comment