luckdemon

XpTurtleV3

May 21st, 2013
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.54 KB | None | 0 0
  1. --wrapping peripheral and init variables
  2. enableMonitor = peripheral.isPresent("bottom")
  3. local xp = peripheral.wrap("left")
  4. local anvil = peripheral.wrap("right")
  5. xp.setAutoCollect(true)
  6. xpPickup = true
  7.  
  8. mode = 1
  9. statusText = "Booting"
  10. desiredLvl = 30
  11. modeStatus = "Booting"
  12. done = false
  13. manual = false
  14. help = false
  15.  
  16.  
  17. --make the book
  18. function enchant(dLvl)
  19. turtle.select(1)
  20. xp.enchant(dLvl)
  21. turtle.drop()
  22. end
  23.  
  24.  
  25. --get the slot number that books are in
  26. function findbooks()
  27. for i=2,14 do
  28. if turtle.getItemCount(i) > 0 then
  29.     return i
  30.     end
  31. end
  32. if turtle.getItemCount(14) == 0 then
  33.     print("no books") return false
  34.     else error("Error getting book count")
  35.     end
  36. end
  37.  
  38.  
  39. --check for books in inventory, and place 1 in first slot
  40. function bookcheck()
  41.     if turtle.getItemCount(1) == 0 then
  42.     turtle.select(findbooks())
  43.     turtle.transferTo(1,1)
  44.     end
  45. end
  46.  
  47.  
  48. --print usage information
  49. function startup()
  50. term.clear()
  51. term.setCursorPos(1,1)
  52. textutils.slowPrint("Lucky Auto Enchant and Anvil v1", 20)
  53. sleep(3)
  54. term.clear()
  55. term.setCursorPos(1,1)
  56. end
  57.  
  58.  
  59. -- monitor updates
  60. function updateScreen()
  61. if mode == 4 then
  62. dispHelp()
  63. else
  64. term.clear()
  65. term.setCursorPos(1,1)
  66.     print("Lucky Auto Enchant & Anvil v1.0")
  67.     term.setCursorPos(1,3)
  68.     print("Xp lvl: " .. xp.getLevels())
  69.     term.setCursorPos(1,4)
  70.     print("Mode:" .. statusText .. "     ")
  71.     term.setCursorPos(1,7)
  72.     print("Status: " .. modeStatus)
  73.     term.setCursorPos(1,8)
  74.     if mode == 1 or mode == 2 then
  75.         print("Desired lvl: " .. desiredLvl)
  76.         elseif mode == 3 then
  77.         print("Anvil Cost: " .. anvil.getRepairCost(15,16))
  78.         term.setCursorPos(1,9)
  79.         print("Repair slot 15 with slot 16")
  80.         end
  81.     term.setCursorPos(20,3)
  82.     print("Press H for Help")
  83.     term.setCursorPos(1,10)
  84.     print("Xp Collection: " .. tostring(xpPickup))
  85. end
  86. end
  87.  
  88.  
  89. function autoEnchant()
  90. bookcheck()
  91. desiredLvl = 30
  92. sleep(0.1)
  93.     if xp.getLevels() >= desiredLvl then
  94.     enchant(desiredLvl)
  95.     modeStatus = "Enchanted!"
  96.     else
  97.     modeStatus = "Waiting"
  98.     end
  99. end
  100.  
  101. function manEnchant()
  102. sleep(0.1)
  103.     if xp.getLevels() >= desiredLvl then
  104.     modeStatus = "Ready!"
  105.     else
  106.     modeStatus = "Waiting"
  107.     end
  108. end
  109.  
  110. function manAnvil()
  111. sleep(0.1)
  112. end
  113.  
  114. function dispHelp()
  115. term.clear()
  116. term.setCursorPos(1,1)
  117. term.write("Lucky Help Menu")
  118. term.setCursorPos(1,3)
  119. term.write("Press M for Manual Enchant")
  120. term.setCursorPos(1,4)
  121. term.write("Press D for Xp pickup")
  122. term.setCursorPos(1,5)
  123. term.write("Press A for Anvil Mode")
  124. term.setCursorPos(1,6)
  125. term.write("Press S for Auto Enchant")
  126. term.setCursorPos(1,7)
  127. term.write("Press Enter for manual anvil/enchant")
  128. term.setCursorPos(1,8)
  129. term.write("Arrow keys change desired lvl")
  130. term.setCursorPos(1,9)
  131. term.write("Press H to exit help")
  132. term.setCursorPos(1,10)
  133. term.write("Press Q to quit")
  134. sleep(0.5)
  135. end
  136.  
  137. function eventHandle()
  138. while true do
  139. timer1 = os.startTimer(1)
  140. id,p1,p2,p3 = os.pullEvent()
  141.     if id == "key" and p1 == 200 and manual then
  142.     desiredLvl = desiredLvl + 1
  143.         if desiredLvl > 30 then desiredLvl = 30 end
  144.     elseif id == "key" and p1 == 208 and manual then
  145.     desiredLvl = desiredLvl - 1
  146.         if desiredLvl < 1 then desiredLvl = 1 end
  147.     elseif id == "key" and p1 == 50 then
  148.         mode = 2
  149.     elseif id == "key" and p1 == 31 then
  150.         mode = 1
  151.     elseif id == "key" and p1 == 30 then
  152.         mode = 3
  153.     elseif id == "key" and p1 == 16 then
  154.         error("Program Shutdown")
  155.     elseif id == "key" and p1 == 32 then
  156.         if xpPickup == true then xpPickup = false
  157.         else xpPickup = true
  158.         end
  159.         xp.setAutoCollect(xpPickup)
  160.     elseif id == "key" and p1 == 28 and manual then
  161.         if mode == 3 then
  162.         anvil.repair(15,16)
  163.         else
  164.         enchant(desiredLvl)
  165.         end
  166.     elseif id == "key" and p1 == 35 then
  167.         if mode == 4 then mode = 1
  168.         else mode = 4
  169.         end
  170.     elseif id == "timer" and p1 == "timer1" then
  171.         timer1 = os.startTimer(1)
  172.     end
  173. end
  174. end
  175.  
  176. function modeHandle()
  177. while true do
  178.     sleep(0.1)
  179.     updateScreen()
  180.     if mode == 1 then
  181.     manual = false
  182.     statusText = "Automatic"
  183.     autoEnchant()
  184.     elseif mode == 2 then
  185.     statusText = "Manual"
  186.     manual = true
  187.     manEnchant()
  188.     elseif mode == 3 then
  189.     statusText = "Anvil"
  190.     manual = true
  191.     manAnvil()
  192.     elseif mode == 4 then
  193.     statusText = "Help"
  194.     end
  195. end
  196. end
  197.  
  198. startup()
  199. while true do
  200. parallel.waitForAny(eventHandle, modeHandle)
  201. end
Advertisement
Add Comment
Please, Sign In to add comment