Advertisement
FlyPei

asdf

Jul 17th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.46 KB | None | 0 0
  1. print("---Loading Crafty module---")
  2.  
  3. Crafty = {}
  4. Crafty.table = { nil, nil, nil,
  5. nil, nil, nil,
  6. nil, nil, nil
  7. }
  8.  
  9. Crafty.trans = { 6, 7, 8,
  10. 10, 11, 12,
  11. 14, 15, 16
  12. }
  13.  
  14. Crafty.vacant = { 1, 2, 3, 4, 5, 9, 13 }
  15.  
  16. Crafty.timeout = nil
  17. Crafty.pos = 0
  18. Crafty.total = 0
  19. Crafty.totalmax = 1000
  20.  
  21. Crafty.dropSide = nil
  22. Crafty.dropFunc = nil
  23. Crafty.dropTotal = 0
  24. Crafty.dropTotalMax = 100000
  25. Crafty.selected = 1
  26.  
  27. function Crafty:new( s1, s2, s3, s4, s5, s6, s7, s8, s9, sout, mtimeout)
  28.  
  29. o = {}
  30. setmetatable(o, self)
  31. self.__index = self
  32.  
  33.  
  34.  
  35. if mtimeout and mtimeout >= 0 then
  36. o.timeout = mtimeout
  37. end
  38.  
  39. o:set(s1, 1)
  40. o:set(s2, 2)
  41. o:set(s3, 3)
  42. o:set(s4, 4)
  43. o:set(s5, 5)
  44. o:set(s6, 6)
  45. o:set(s7, 7)
  46. o:set(s8, 8)
  47. o:set(s9, 9)
  48.  
  49. if sout == "top" then
  50. o.dropSide = nil
  51. o.dropFunc = turtle.dropUp
  52.  
  53. elseif sout == "bottom" then
  54. o.dropSide = nil
  55. o.dropFunc = turtle.dropDown
  56. else
  57. o.dropFunc = turtle.drop
  58.  
  59. if sout == "front" then
  60. o.dropSide = 0
  61.  
  62. elseif sout == "right" then
  63. o.dropSide = 1
  64.  
  65. elseif sout == "back" then
  66. o.dropSide = 2
  67.  
  68. elseif sout == "left" then
  69. o.dropSide = 3
  70. end
  71. end
  72.  
  73. return o
  74.  
  75. end
  76.  
  77. function Crafty:run()
  78. print("Initializing...")
  79. if not self:findAnchor() then
  80. print("\t\t--Anchor me, silly!")
  81. return
  82. end
  83.  
  84. self:clean()
  85. print("Clearing data...")
  86. self.dropTotal = 0
  87. print("Ready to craft!")
  88.  
  89. while true do
  90. self:replenish()
  91. self:craft()
  92. self:clean()
  93. print()
  94. end
  95. end
  96.  
  97. function Crafty:select(n)
  98. turtle.select(n)
  99. self.selected = n
  100. end
  101.  
  102.  
  103. function Crafty:findAnchor()
  104.  
  105. local ret = false
  106. print("\t-Finding my anchor...")
  107.  
  108. redstone.setOutput("front", false)
  109. redstone.setOutput("right", false)
  110. redstone.setOutput("back", false)
  111. redstone.setOutput("left", false)
  112.  
  113. sleep(1)
  114.  
  115. for i=1,4 do
  116. if redstone.getInput("front") then
  117. self.pos = 0
  118. ret = true
  119. print("\t\t--Found!")
  120. break
  121. else
  122. turtle.turnLeft()
  123. end
  124. end
  125. return ret
  126. end
  127.  
  128.  
  129. function Crafty:clean()
  130. i = 1
  131. local total = 0
  132. print("\t-Cleaning inventory...")
  133. print("\t\t-Pass "..i..":")
  134. temptotal = self:empty()
  135.  
  136. while temptotal ~= 0 do
  137. i = i + 1
  138. total = total + temptotal
  139. print("\t\t-Pass "..i..":")
  140. temptotal = self:empty()
  141. end
  142.  
  143. print("\t\t-Dropped: "..total)
  144.  
  145. if (self.dropTotal + total) < self.dropTotalMax then
  146. self.dropTotal = self.dropTotal + total
  147. print("Total overall: "..self.dropTotal)
  148. else
  149. print("Total overall: "..self.dropTotalMax.."+")
  150. end
  151. return total
  152. end
  153.  
  154.  
  155. function Crafty:empty()
  156.  
  157. local dropped = 0
  158. for i=1,table.getn(self.vacant) do
  159. slot = self.vacant[i]
  160.  
  161. if turtle.getItemCount(slot) ~= 0 then
  162.  
  163. print("\t\t\t-Dropping slot: "..slot)
  164. self:select(slot)
  165.  
  166. if self.dropSide then
  167. self:turn(self.dropSide)
  168. end
  169.  
  170. dropped = dropped + turtle.getItemCount(slot)
  171.  
  172. while turtle.getItemCount(slot) ~= 0 do
  173.  
  174.  
  175. if (not self.dropFunc()) and self.timeout then
  176. sleep(self.timeout)
  177. end
  178. sleep(0)
  179. end
  180.  
  181. end
  182. end
  183. return dropped
  184. end
  185.  
  186.  
  187. function Crafty:helpme()
  188. print("----------Help!----------")
  189. print("[Backspace] - exit\n[Enter] - resume")
  190. local side = nil
  191.  
  192. if redstone.getInput("front") then
  193. side = "front"
  194.  
  195. elseif redstone.getInput("right") then
  196. side = "right"
  197.  
  198. elseif redstone.getInput("back") then
  199. side = "back"
  200.  
  201. elseif redstone.getInput("left") then
  202. side = "left"
  203. end
  204.  
  205. if side then
  206. redstone.setOutput(side, true)
  207. end
  208.  
  209. while true do
  210. sleep(0)
  211. tp, key = os.pullEvent()
  212.  
  213. if tp == "redstone" then
  214. if redstone.getOutput(side) then
  215. redstone.setOutput(side, false)
  216. else
  217. redstone.setOutput(side, true)
  218. end
  219.  
  220. elseif tp == "key" then
  221.  
  222. if key and key == 14 then
  223.  
  224. if side then
  225. redstone.setOutput(side, false)
  226. end
  227. error("Aborted by user.")
  228.  
  229. elseif key == 28 then
  230. if side then
  231. redstone.setOutput(side, false)
  232. end
  233.  
  234. print("----------Resumed!----------")
  235. break
  236. end
  237. end
  238. end
  239. end
  240.  
  241.  
  242. function Crafty:craft()
  243.  
  244. print("\t-Craftying...")
  245. self:select(1)
  246.  
  247. if not turtle.craft() then
  248. self:helpme()
  249. end
  250.  
  251. if self.total < self.totalmax then
  252. self.total = self.total + 1
  253. print("\t\t-Craftyed: "..self.total)
  254. else
  255. print("\t\t-Craftyed: "..self.totalmax.."+")
  256. end
  257. end
  258.  
  259. function Crafty:replenish()
  260. print("\t-Refilling inventory...")
  261. for i=1,9 do
  262. if self.table[i] ~= nil then
  263. if turtle.getItemCount(self.trans[i]) == 0 then
  264. print("\t\t-Refilling: "..i.." (slot "..self.trans[i]..")")
  265. self:select(self.trans[i])
  266. self.table[i](self)
  267. end
  268. end
  269. end
  270. end
  271.  
  272. function Crafty:set(side, num)
  273. if side and (num >= 1) and (num <= 9) then
  274.  
  275. local func = nil
  276.  
  277. if side == "top" then
  278. func = self.suckUp
  279.  
  280. elseif side == "bottom" then
  281. func = self.suckDown
  282.  
  283. elseif side == "front" then
  284. func = self.suckFront
  285.  
  286. elseif side == "right" then
  287. func = self.suckRight
  288.  
  289. elseif side == "left" then
  290. func = self.suckLeft
  291.  
  292. elseif side == "back" then
  293. func = self.suckBack
  294.  
  295. else
  296. func = nil
  297.  
  298. end
  299. self.table[num] = func
  300. end
  301. end
  302.  
  303. function Crafty:suck(func, caller)
  304.  
  305. if not (func() or self:search(caller)) then
  306.  
  307. while not func() do
  308. sleep(0)
  309. if self.timeout then
  310. sleep(self.timeout)
  311. end
  312. end
  313. end
  314. end
  315.  
  316. function Crafty:search(caller)
  317. local ret = false
  318. local count = {}
  319. for i=1,9 do
  320. if self.table[i] == caller and turtle.getItemCount(self.trans[i]) > 1 then
  321. count[i] = turtle.getItemCount(self.trans[i])
  322. ret = true
  323. end
  324. end
  325.  
  326. if ret then
  327.  
  328. local max = 0
  329. local index = 0
  330. for i=1,9 do
  331. if count[i] and count[i] > max then
  332. max = count[i]
  333. index = i
  334. end
  335. end
  336.  
  337. local xfer = math.floor(count[index]/2)
  338. local oldn = self.selected
  339. local newn = self.trans[index]
  340. print("\t\t\t---Transferring "..xfer.." to "..oldn.." from "..newn..".")
  341. self:select(newn)
  342. turtle.transferTo(oldn, xfer)
  343. self:select(oldn)
  344.  
  345. return true
  346.  
  347. else
  348. return false
  349. end
  350.  
  351. end
  352.  
  353. function Crafty:suckLeft()
  354. self:turn(3)
  355. self:suck(turtle.suck, self.suckLeft)
  356. end
  357.  
  358. function Crafty:suckRight()
  359. self:turn(1)
  360. self:suck(turtle.suck, self.suckRight)
  361. end
  362.  
  363. function Crafty:suckFront()
  364. self:turn(0)
  365. self:suck(turtle.suck, self.suckFront)
  366. end
  367.  
  368. function Crafty:suckBack()
  369. self:turn(2)
  370. self:suck(turtle.suck, self.suckBack)
  371. end
  372.  
  373. function Crafty:suckUp()
  374. self:suck(turtle.suckUp, self.suckUp)
  375. end
  376.  
  377. function Crafty:suckDown()
  378. self:suck(turtle.suckDown, self.suckDown)
  379. end
  380.  
  381. function Crafty:turn(s)
  382. local diff = (self.pos - s) % 4
  383.  
  384. if diff == 0 then
  385. return
  386. end
  387.  
  388. if diff == 1 then
  389. turtle.turnLeft()
  390.  
  391. elseif diff == 2 then
  392. turtle.turnLeft()
  393. turtle.turnLeft()
  394.  
  395. elseif diff == 3 then
  396. turtle.turnRight()
  397.  
  398. end
  399. self.pos = s
  400.  
  401. end
  402.  
  403. print("---Loaded Crafty v1.0---")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement