Advertisement
Guest User

lazy

a guest
Mar 31st, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.76 KB | None | 0 0
  1. --==========
  2. --this is lazy API
  3. --it's shit, made by adsmz (adsmz.com)
  4. --I don't know lua worth shit
  5. --it basically just has a few
  6. --functions I don't wanna type a million times
  7. --it's licensed with a Creative Commons Attribution-ShareAlike 4.0 license
  8. --the full text of which can be found at
  9. --  creativecommons.org/licenses/by-sa/4.0/legalcode
  10. --with a summary of your rights at
  11. --  creativecommons.org/licenses/by-sa/4.0
  12.  
  13. --==========
  14. -- Maths
  15. -- mathy functions that are useful for mathy things
  16.  
  17. -- round - round your floats!
  18. --Takes two arguments, value and places
  19. --value is the value to round
  20. --places is the number of decimal places to round to
  21. function round(value,places)
  22.   if places then
  23.     return math.floor((value * 10^places) + 0.5) / (10^places)
  24.   else
  25.     return math.floor(value + 0.5)
  26.   end
  27. end
  28.  
  29. -- percent - find your percents!
  30. --Takes two arguments, value and maxValue
  31. --value is the number that's a percentage of maxValue
  32. --that makes sense, right? You'll figure it out
  33. function percent(value,maxValue)
  34.   return (value / maxValue) * 100
  35. end
  36.  
  37. -- roundPercent - round your percents!
  38. --takes three arguments, value, maxValue, and place
  39. --value is the number that's a percentage of maxValue
  40. --place is how many places to round to
  41. --this is just a combination of roundomatic and percentomatic
  42. --since I use the two together like this all the time
  43. function roundPercent(value,maxValue,place)
  44.   return round(percent(value,maxValue),place)
  45. end
  46.  
  47. --==========
  48. -- Others
  49. -- functions that aren't mathy
  50.  
  51. -- find - find peripherals on any side!
  52. --Takes one argument, peripheralName
  53. --peripheralName is the peripheral to look for
  54. --It finds and wraps the desired peripheral
  55. function find(peripheralName)
  56.   local sides = {"top","bottom","left","right","front","back"}
  57.   for i = 1, #sides do --I put on my robe and wizard hat
  58.     if peripheral.isPresent(sides[i]) then
  59.       local type = peripheral.getType(sides[i])
  60.       if type == peripheralName then
  61.         return sides[i]
  62.       end
  63.     end
  64.   end
  65. end
  66.  
  67. -- wait - waits for an event!
  68. --Takes two arguments, eventType and timeout
  69. --eventType is the type of event to listen for
  70. --timeout is how long to listen
  71. --if timeout is nil, it will wait indefinitely
  72. --it returns a table of the event's parameters
  73. --it returns "timeout" if the timeout is reached without an input
  74. --for now, it doesn't look for specific messages, just event types
  75. function wait(eventType,timeout)
  76.   if timeout then
  77.     os.startTimer(timeout)
  78.     local event,p1,p2,p3,p4,p5 = os.pullEvent(eventType)
  79.     if event ~= eventType then
  80.       return "timeout"
  81.     else
  82.       return {event,p1,p2,p3,p4,p5}
  83.     end
  84.   else
  85.     local event,p1,p2,p3,p4,p5 = os.pullEvent(eventType)
  86.     return {event,p1,p2,p3,p4,p5}
  87.   end
  88. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement