Advertisement
Riewest14

riewestLIB

Apr 26th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.82 KB | None | 0 0
  1. --riewestLIB
  2. --Version "1.0.0"
  3. --MCVersion "1.10.2"
  4. --Writer: Riewest (AKA Riewest14)
  5.  
  6. function test()
  7.  
  8. end
  9.  
  10. ---------------
  11. --ITEM HANDLING
  12. ---------------
  13.  
  14. --Attempts to get fuel from a
  15. --inventory on side passed and fill
  16. --the slot passed. Works best if
  17. --The item is still in slot or
  18. --Chest only has fuel items
  19. function getFuel(slot, side)
  20.   if not slot then
  21.     slot = 16
  22.   end
  23.   if not side then
  24.     side = "front"
  25.   end
  26.   oldSlot = turtle.getSelectedSlot()
  27.   turtle.select(slot)
  28.   side = string.lower(side)
  29.   toGet = turtle.getItemSpace()
  30.   if side == "front" then
  31.     turtle.suck(toGet)
  32.   elseif side == "top"  then
  33.     turtle.suckUp(toGet)
  34.   elseif side == "bottom" then
  35.     turtle.suckDown(toGet)
  36.   end
  37.   turtle.select(oldSlot)
  38. end
  39.  
  40. --Tries to put all of the items from
  41. --the all other slots into the
  42. --passed slot
  43. function sortItem(slot)
  44.   if not slot then
  45.     return
  46.   end
  47.   oldSlot = turtle.getSelectedSlot()
  48.   turtle.select(slot)
  49.   if turtle.getItemCount() > 0 then
  50.     itemDetail = turtle.getItemDetail()
  51.     for i = 1, 16 do
  52.       turtle.select(i)
  53.       if not (i == slot) and (turtle.getItemCount() > 0) then
  54.         tempDetail = turtle.getItemDetail()
  55.         if (itemDetail.name == tempDetail.name) then
  56.           turtle.transferTo(slot)      
  57.         end
  58.       end
  59.     end
  60.   end
  61.   turtle.select(oldSlot)
  62. end
  63.  
  64. --Empty's the slots passed in through
  65. --the array.
  66. function emptyInv(slotsToEmpty, side)
  67.   oldSlot = turtle.getSelectedSlot()
  68.   if not side then
  69.     side = "back"
  70.   end
  71.   if not slotsToEmpty then
  72.     count = 16
  73.   else
  74.     count = table.getn(slotsToEmpty)
  75.   end
  76.   rotate(side)
  77.   for i = 1, count do
  78.     dropSide(slotsToEmpty[i], side)
  79.   end
  80.   reverseRotate(side)
  81.   turtle.select(oldSlot)
  82. end
  83.  
  84. --Empty's slots except the passed
  85. --in values
  86. function emptyInvExcept(slotsToKeep, side)
  87.   slotsToEmpty = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
  88.   table.sort(slotsToKeep)
  89.   for i = table.getn(slotsToKeep), 1, -1 do
  90.     table.remove(slotsToEmpty, slotsToKeep[i])
  91.   end
  92.   emptyInv(slotsToEmpty, side)
  93. end
  94.  
  95. --Drops all the items in slotNum
  96. --to the indicated side
  97. function dropSide(slotNum, side)
  98.   turtle.select(slotNum)
  99.   side = string.lower(side)
  100.   success = false
  101.   if side == "front" then
  102.     success = turtle.drop()
  103.   elseif side == "top" then
  104.     success = turtle.dropUp()
  105.   elseif side == "bottom" then
  106.     success = turtle.dropDown()
  107.   else
  108.     success = turtle.drop()
  109.   end
  110.   return success
  111. end
  112.  
  113. --Method to tell the turtle to suck
  114. --in front of it and the left and right
  115. function tSuck(distinctItems)
  116.   for i = 1, distinctItems do
  117.     turtle.suck()
  118.   end
  119.   turtle.turnLeft()
  120.   for i = 1, distinctItems do
  121.     turtle.suck()
  122.   end
  123.   turnAround()
  124.   for i = 1, distinctItems do
  125.     turtle.suck()
  126.   end
  127.   turtle.turnLeft()  
  128. end
  129.  
  130.  
  131. --Method to refuel the turtle
  132. --if its fuel level is below a
  133. --critical amount. Fuel slot is
  134. --passed in, as well as threshold
  135. function tRefuel(fSlot, thresh)
  136.   if not thresh then
  137.     thresh = 100
  138.   end
  139.   if not fSlot then
  140.     fSlot = 16
  141.   end
  142.   success = false
  143.   if turtle.getFuelLevel() <= thresh then
  144.     oldSlot = turtle.getSelectedSlot()
  145.     turtle.select(fSlot)
  146.     success = turtle.refuel(turtle.getItemCount() - 1)
  147.     turtle.select(oldSlot)
  148.   end
  149.   return success
  150. end
  151.  
  152. ------------------
  153. --MOVEMENT METHODS
  154. ------------------
  155.  
  156. --Just runs turtle.forward()
  157. --until the specified amount toMov
  158. --is met. Digs block if unable to move
  159. function tMoveF(toMov)
  160.   if not toMov then
  161.     toMov = 1
  162.   end
  163.   for i = 1, toMov do
  164.     while not (turtle.forward()) do
  165.       turtle.dig()
  166.     end
  167.   end
  168. end
  169.  
  170. --Runst turtle.back() toMov times
  171. function tMoveB(toMov)
  172.   if not toMov then
  173.     toMov = 1
  174.   end
  175.   for i = 1, toMov do
  176.     while not turtle.back() do
  177.       sleep(.1)
  178.     end
  179.   end
  180. end
  181.  
  182. --Runs turtle.up() toMov times
  183. --and digs up if unable to move up.
  184. function tMoveU(toMov)
  185.   if not toMov then
  186.     toMov = 1
  187.   end
  188.   for i = 1, toMov do
  189.     while not (turtle.up()) do
  190.       turtle.digUp()
  191.     end
  192.   end
  193. end
  194.  
  195. --Runs turtle.down() toMov times
  196. -- and digs down if unable to move down
  197. function tMoveD(toMov)
  198.   if not toMov then
  199.     toMov = 1
  200.   end
  201.   for i = 1, toMov do
  202.     while not (turtle.down()) do
  203.       turtle.digDown()
  204.     end
  205.   end
  206. end
  207.  
  208. --Turns the turtle 180 degrees
  209. function turnAround()
  210.   turtle.turnLeft()
  211.   turtle.turnLeft()
  212. end
  213.  
  214. --Turns the turtle to the passed side
  215. function rotate(side)
  216.   if not side then
  217.     return
  218.   elseif side == "back" then
  219.     turnAround()
  220.   elseif side == "left" then
  221.     turtle.turnLeft()
  222.   elseif side == "right" then
  223.     turtle.turnRight()
  224.   else
  225.   end
  226. end
  227.  
  228. --Turns the turtle opposite the
  229. -- passed side
  230. function reverseRotate(side)
  231.   if not side then
  232.     return
  233.   elseif side == "back" then
  234.     turnAround()
  235.   elseif side == "left" then
  236.     turtle.turnRight()
  237.   elseif side == "right" then
  238.     turtle.turnLeft()
  239.   else
  240.   end
  241. end
  242.  
  243. --------------
  244. --DATA METHODS
  245. --------------
  246.  
  247. --Displays fuel level in the upper
  248. --right corner of the screen
  249. function displayFuel()
  250.   fuelLevel = turtle.getFuelLevel()
  251.   oldX, oldY = term.getCursorPos()
  252.   displayY = 1
  253.   displayX = 34
  254.   term.setCursorPos(displayX, displayY)
  255.   print("Fuel:")
  256.   term.setCursorPos(displayX, displayY + 1)
  257.   print(fuelLevel)
  258.   term.setCursorPos(oldX, oldY)
  259. end
  260.  
  261.  
  262. --Function to find the slot number
  263. --Of the item containing passed in
  264. --String, Return bool, slot
  265. function findSlot(name)
  266.   found = false
  267.   slot = 0
  268.   for i = 1, 16 do
  269.     if turtle.getItemCount(i) > 0 then
  270.       itemDetail = turtle.getItemDetail(i)
  271.       if string.match(itemDetail.name, string.lower(name)) then
  272.         slot = i
  273.         found = true
  274.         break
  275.       end
  276.     end
  277.   end
  278.   return found, slot
  279. end
  280.  
  281.  
  282. test()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement