UNOBTANIUM

eltrutAPI

May 18th, 2013
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.47 KB | None | 0 0
  1. --ELTRUT 0.1.0
  2. --by unobtanium
  3. --Fixed by AxxiD
  4.  
  5. -- PRINT
  6.  
  7. OSwidth, OSheight = term.getSize()
  8.  
  9. -- SCREEN
  10.  
  11. function clearScreen()
  12.  term.clear()
  13.  term.setCursorPos(1,1)
  14. end
  15.  
  16. function cS() -- shortcut for clearScreen()
  17.   clearScreen()
  18. end
  19.  
  20. function clearLine(line)
  21.  printCentered(string.rep(" ", OSwidth), line)
  22. end
  23.  
  24.  
  25. function clearColumn(column)
  26.  for i=1, OSheight, 1 do
  27.   term.setCursorPos(column,i)
  28.   term.write(" ")
  29.  end
  30. end
  31.  
  32. function clearColumnFromTo(column, start, last)
  33.  for i=start, last do
  34.   term.setCursorPos(column,i)
  35.   term.write(" ")
  36.  end
  37. end
  38.  
  39. function centerCursor(line)
  40.  term.setCursorPos(OSwidth/2,line)
  41. end
  42.  
  43. function leftCursor(line)
  44.  term.setCursorPos(1,line)
  45. end
  46.  
  47. function rightCursor(line)
  48.  term.setCursorPos(OSwidth,line)
  49. end
  50.  
  51. -- PRINT
  52.  
  53. function print(text, line, column)
  54.  term.setCursorPos(column,line)
  55.  term.write(text)
  56. end
  57.  
  58. function printCentered(text, line)
  59.  term.setCursorPos(OSwidth/2,line)
  60.  term.write(text)
  61. end
  62.  
  63. function printCenteredR(text,line)
  64.  clearLine(line)
  65.  printCentered(text,line)
  66. end
  67.  
  68. function printRight(text, line)
  69.  term.setCursorPos(OSwidth-#text,line)
  70.  term.write(text)
  71. end
  72.  
  73. function printRightR(text, line)
  74.  clearLine(line)
  75.  printRight(text,line)
  76. end
  77.  
  78. function printLeft(text, line)
  79.  term.setCursorPos(1,line)
  80.  term.write(text)
  81. end
  82.  
  83. function printLeftR(text, line)
  84.  clearLine(line)
  85.  printLeft(text, line)
  86. end
  87.  
  88. function printVertical(text, column)
  89.  for i=1, #text do
  90.   term.setCursorPos(column,i)
  91.   term.write(string.sub(text,i,i))
  92.  end
  93. end
  94.  
  95. function printVerticalR(text, column)
  96.  clearColumn(column)
  97.  printVertical(text, column)
  98. end
  99.  
  100. function printHeader(text,line)
  101.  printCentered(text, line)
  102.  printCentered(string.rep("-", OSwidth), line+1)
  103. end
  104.  
  105. -- FUEL
  106.  
  107. function refuel(slot ,level)
  108.  turtle.select(slot)
  109.  while turtle.getFuelLevel() <= level do
  110.   if turtle.refuel(0) then
  111.    turtle.refuel(1)
  112.   else
  113.    sleep(1)
  114.   end
  115.  end
  116. end
  117.  
  118.  
  119. --INVENTORY
  120.  
  121. function countSlots(first, last)
  122.  local amount = 0
  123.  for i=first, last do
  124.   local slotAmount = turtle.getItemCount(i)
  125.   if slotAmount > 0 then
  126.    amount = amount + slotAmount
  127.   end
  128.  end
  129.  return amount
  130. end
  131.  
  132. function countInventory()
  133.  return countSlots(1,16)
  134. end
  135.  
  136. function compareToSlot(first, second)
  137.  turtle.select(first)
  138.  return turtle.compareTo(second)
  139. end
  140.  
  141. function compareToSlots(selected, first, last)
  142.  local same = true
  143.  turtle.select(selected)
  144.  for i=first, last do
  145.   local check = turtle.compareTo(i)
  146.   if not check then same = false end
  147.  end
  148.  return same
  149. end
  150.  
  151. function transferTo(first, second, quantity)
  152.  quantity = quantity or 64
  153.  turtle.select(first)
  154.  turtle.transferTo(second,quantity)
  155. end
  156.  
  157. function transferToSlots(start, last, startTo)
  158.  for i=0, last-start-1 do
  159.   local startSlot = start + i
  160.   if startSlot > 16 then
  161.    startSlot = startSlot - 16
  162.   end
  163.   if turtle.getItemCount(startSlot) > 0 then
  164.    local slot = startTo + i
  165.    if slot > 16 then
  166.     slot = slot - 16
  167.    end
  168.    transferTo(startSlot, slot)
  169.   end
  170.  end
  171.  if countSlots(start, last) > 0 then
  172.   return false
  173.  end
  174.  return true
  175. end
  176.  
  177. -- DROP
  178.  
  179. function drop(slot,quantity)
  180.  quantitiy = quantity or 64
  181.  turtle.select(slot)
  182.  return turtle.drop(quantity)
  183. end
  184.  
  185. function dropUp(slot, quantity)
  186.  quantitiy = quantity or 64
  187.  turtle.select(slot)
  188.  return turtle.dropUp(quantity)
  189. end
  190.  
  191. function dropDown(slot, quantity)
  192.  quantity = quantity or 64
  193.  turtle.select(slot)
  194.  return turtle.dropDown(quantity)
  195. end
  196.  
  197. function dropSlots(dStart, dEnd)
  198.  local success = true
  199.  for i=dStart, dEnd do
  200.   if turtle.getItemCount(i) > 0 then
  201.    turtle.select(i)
  202.    local var = turtle.drop()
  203.    if not var then success = false end
  204.   end
  205.  end
  206.  return success
  207. end
  208.  
  209. function dropUpSlots(dStart, dEnd)
  210.  local success = true
  211.  for i=dStart, dEnd do
  212.   if turtle.getItemCount(i) > 0 then
  213.    turtle.select(i)
  214.    local var = turtle.dropUp()
  215.    if not var then success = false end
  216.   end
  217.  end
  218.  return success
  219. end
  220.  
  221. function dropDownSlots(dStart, dEnd)
  222.  local success = true
  223.  for i=dStart, dEnd do
  224.   if turtle.getItemCount(i) > 0 then
  225.    turtle.select(i)
  226.    local var = turtle.dropDown()
  227.    if not var then success = false end
  228.   end
  229.  end
  230.  return success
  231. end
  232.  
  233. function dropSameTo(slot)
  234.  local amount = 0
  235.  turtle.select(slot)
  236.  for i=1, 16 do
  237.   if turtle.getItemCount(i) > 0 and turtle.compareTo(i) and not slot == i then
  238.    turtle.select(i)
  239.    amount = amount + turtle.getItemCount(i)
  240.    turtle.drop()
  241.   end
  242.  end
  243.  return amount
  244. end
  245.  
  246. function dropUpSameTo(slot)
  247.  local amount = 0
  248.  turtle.select(slot)
  249.  for i=1, 16 do
  250.   if turtle.getItemCount(i) > 0 and turtle.compareTo(i) and not slot == i then
  251.    turtle.select(i)
  252.    amount = amount + turtle.getItemCount(i)
  253.    turtle.dropUp()
  254.   end
  255.  end
  256.  return amount
  257. end
  258.  
  259. function dropDownSameTo(slot)
  260.  local amount = 0
  261.  turtle.select(slot)
  262.  for i=1, 16 do
  263.   if turtle.getItemCount(i) > 0 and turtle.compareTo(i) and not slot == i then
  264.    turtle.select(i)
  265.    amount = amount + turtle.getItemCount(i)
  266.    turtle.dropDown()
  267.   end
  268.  end
  269.  return amount
  270. end
  271.  
  272.  
  273. -- SUCK
  274.  
  275. function suck(slot)
  276.  turtle.select(slot)
  277.  turtle.suck()
  278.  return turtle.getItemCount(slot)
  279. end
  280.  
  281. function suckUp(slot)
  282.  turtle.select(slot)
  283.  turtle.suckUp()
  284.  return turtle.getItemCount(slot)
  285. end
  286.  
  287. function suckDown(slot)
  288.  turtle.select(slot)
  289.  turtle.suckDown()
  290.  return turtle.getItemCount(slot)
  291. end
  292.  
  293. function suckSlots(sStart, sEnd)
  294.  local amount = countSlots(sStart, sEnd)
  295.  for i=sStart, sEnd do
  296.   if turtle.getItemCount(i) == 0 then
  297.    turtle.select(i)
  298.    turtle.suck()
  299.   end
  300.  end
  301.  return countSlots(sStart, sEnd) - amount
  302. end
  303.  
  304. function suckUpSlots(sStart, sEnd)
  305.  local amount = countSlots(sStart, sEnd)
  306.  for i=sStart, sEnd do
  307.   if turtle.getItemCount(i) == 0 then
  308.    turtle.select(i)
  309.    turtle.suckUp()
  310.   end
  311.  end
  312.  return countSlots(sStart, sEnd) - amount
  313. end
  314.  
  315. function suckDownSlots(sStart, sEnd)
  316.  local amount = countSlots(sStart, sEnd)
  317.  for i=sStart, sEnd do
  318.   if turtle.getItemCount(i) == 0 then
  319.    turtle.select(i)
  320.    turtle.suckDown()
  321.   end
  322.  end
  323.  return countSlots(sStart, sEnd) - amount
  324. end
  325.  
  326. -- DIG
  327.  
  328. function dig()
  329.  while turtle.detect() do
  330.   turtle.dig()
  331.   sleep(0.4)
  332.  end
  333. end
  334.  
  335. function digUp()
  336.  while turtle.detectUp() do
  337.   turtle.digUp()
  338.   if turtle.detectUp() then
  339.    sleep(0.4)
  340.   end
  341.  end
  342. end
  343.  
  344. function digDown()
  345.  while turtle.detectDown() do
  346.   turtle.digDown()
  347.   if turtle.detectDown() then
  348.    sleep(0.4)
  349.   end
  350.  end
  351. end
  352.  
  353. -- MOVEMENT
  354.  
  355. function turnAround()
  356.  if math.ceil(math.random()*2) == 1 then
  357.   turtle.turnLeft()
  358.   turtle.turnLeft()
  359.  else
  360.   turtle.turnRight()
  361.   turtle.turnRight()
  362.  end
  363. end
  364.  
  365. function forward(length)
  366.  for i=1,length do
  367.   while not turtle.forward() do sleep(1) end
  368.  end
  369. end
  370.  
  371. function forwardDig(length)
  372.  length = length or 1
  373.  for i=1,length do
  374.   while not turtle.forward() do turtle.dig() end
  375.  end
  376. end
  377.  
  378. function forwardExtreme(length)
  379.  length = length or 1
  380.  for i=1,length do
  381.   while not turtle.forward() do
  382.    if turtle.detect() then
  383.     dig()
  384.    else
  385.     turtle.attack()
  386.    end
  387.   end
  388.  end
  389. end
  390.  
  391. function back(length)
  392.  length = length or 1
  393.  for i=1,length do
  394.   while not turtle.back() do sleep(1) end
  395.  end
  396. end
  397.  
  398. function backDig(length)
  399.  length = length or 1
  400.  for i=1,length do
  401.   while not turtle.back() do
  402.    turnAround()
  403.    dig()
  404.    turnAround()
  405.   end
  406.  end
  407. end
  408.  
  409. function backExtreme(length)
  410.  length = length or 1
  411.  for i=1,length do
  412.   while not turtle.back() do
  413.    turnAround()
  414.    if turtle.detect() then
  415.     turtle.dig()
  416.    else
  417.     turtle.attack()
  418.    end
  419.    turnAround()
  420.   end
  421.  end
  422. end
  423.  
  424. function up(length)
  425.  length = length or 1
  426.  for i=1,length do
  427.   while not turtle.up() do sleep(1) end
  428.  end
  429. end
  430.  
  431. function upDig(length)
  432.  length = length or 1
  433.  for i=1,length do
  434.   while not turtle.up() do turtle.digUp() end
  435.  end
  436. end
  437.  
  438. function upExtreme(length)
  439.  length = length or 1
  440.  for i=1,length do
  441.   while not turtle.up() do
  442.    if turtle.detectUp() then
  443.     digUp()
  444.    else
  445.     turtle.attackUp()
  446.    end
  447.   end
  448.  end
  449. end
  450.  
  451. function down(length)
  452.  length = length or 1
  453.  for i=1,length do
  454.   while not turtle.down() do sleep(1) end
  455.  end
  456. end
  457.  
  458. function downDig(length)
  459.  length = length or 1
  460.  for i=1,length do
  461.   while not turtle.down() do turtle.digDown() end
  462.  end
  463. end
  464.  
  465. function downExtreme(length)
  466.  length = length or 1
  467.  for i=1,length do
  468.   while not turtle.down() do
  469.    if turtle.detectDown() then
  470.     turtle.digDown()
  471.    else
  472.     turtle.attackDown()
  473.    end
  474.   end
  475.  end
  476. end
Advertisement
Add Comment
Please, Sign In to add comment