Advertisement
msmikesc

turtlecarpenter

Apr 26th, 2019
591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1.  
  2. jelly = "forestry:royal_jelly"
  3. wax = "forestry:beeswax"
  4. pollen = "forestry:pollen"
  5. planks = "minecraft:planks"
  6.  
  7. waxCount = 0
  8. pollenCount = 0
  9. jellyCount = 0
  10. planksCount = 0
  11.  
  12. totalPossible = 0
  13.  
  14. craftAmountPerCarpenter = 27
  15.  
  16. craftAmountRemaining = 0;
  17.  
  18. carpentersArray = {}
  19.  
  20. turtlePosition = 1
  21.  
  22. currentCarpenter = 1
  23.  
  24. realPositions = {}
  25.  
  26. chest = peripheral.wrap("bottom")
  27.  
  28. carpentersthiscycle = 0
  29.  
  30. function main()
  31. while true do
  32. os.sleep(1)
  33.  
  34. if next(chest.list()) ~= nil then
  35. os.sleep(20)
  36. processOneCraftingJob()
  37. end
  38. end
  39.  
  40. end
  41.  
  42. function processOneCraftingJob()
  43. setupArray()
  44. fillInventory()
  45. countItems()
  46.  
  47. while true do
  48.  
  49. if carpentersthiscycle == 8 then
  50. carpentersthiscycle = 0
  51. countItems()
  52. end
  53.  
  54. currentCarpenter = findEmptiestCarpenter()
  55.  
  56. if(craftAmountRemaining == 0) then
  57. restock();
  58. if(craftAmountRemaining == 0) then
  59. dumpInventory()
  60. break
  61. end
  62. end
  63.  
  64. goToCarpenter(currentCarpenter)
  65.  
  66. local itemsToCraftInThis = craftAmountPerCarpenter
  67.  
  68. if itemsToCraftInThis > craftAmountRemaining then
  69. itemsToCraftInThis = craftAmountRemaining
  70. end
  71.  
  72. print("crafting " .. itemsToCraftInThis .. " in carpenter " .. currentCarpenter)
  73. pushItems(itemsToCraftInThis,currentCarpenter)
  74. carpentersthiscycle = carpentersthiscycle + 1
  75.  
  76. end
  77. end
  78.  
  79. function findEmptiestCarpenter()
  80.  
  81. emptiest = 1
  82. lowestAmount = 10000
  83.  
  84. for i=1,8,1 do
  85. if carpentersArray[i] < lowestAmount then
  86. emptiest = i
  87. lowestAmount = carpentersArray[i]
  88.  
  89. if lowestAmount == 0 then
  90. break
  91. end
  92. end
  93. end
  94.  
  95. return emptiest
  96. end
  97.  
  98. function goToCarpenter(carpenterNumber)
  99.  
  100. carpenterNumber = realPositions[carpenterNumber]
  101.  
  102. if turtlePosition > carpenterNumber then
  103. turtle.turnLeft();
  104. while turtlePosition > carpenterNumber do
  105. turtle.forward();
  106. turtlePosition = turtlePosition - 1
  107. end
  108. turtle.turnRight();
  109. end
  110.  
  111. if turtlePosition < carpenterNumber then
  112. turtle.turnRight();
  113. while turtlePosition < carpenterNumber do
  114. turtle.forward();
  115. turtlePosition = turtlePosition + 1
  116. end
  117. turtle.turnLeft();
  118. end
  119. end
  120.  
  121. function restock()
  122. goToCarpenter(1)
  123. fillInventory()
  124. countItems()
  125. carpentersthiscycle = 0
  126. end
  127.  
  128. function setupArray()
  129.  
  130. for i=1,8,1 do
  131. carpentersArray[i] = 0
  132. end
  133.  
  134. realPositions[5] = 0
  135. realPositions[6] = -1
  136. realPositions[7] = -2
  137. realPositions[8] = -3
  138. realPositions[1] = 1
  139. realPositions[2] = 2
  140. realPositions[3] = 3
  141. realPositions[4] = 4
  142.  
  143. end
  144.  
  145. function countItems()
  146.  
  147. waxCount = 0
  148. pollenCount = 0
  149. jellyCount = 0
  150. planksCount = 0
  151.  
  152. for i=1,16,1 do
  153. local meta = turtle.getItemDetail(i)
  154. if(meta ~= nil and meta.name == jelly) then
  155. jellyCount = jellyCount + meta.count
  156. end
  157. if(meta ~= nil and meta.name == wax) then
  158. waxCount = waxCount + meta.count
  159. end
  160. if(meta ~= nil and meta.name == pollen) then
  161. pollenCount = pollenCount + meta.count
  162. end
  163. if(meta ~= nil and meta.name == planks) then
  164. planksCount = planksCount + meta.count
  165. end
  166. end
  167.  
  168. totalPossible = jellyCount
  169. if totalPossible > pollenCount then
  170. totalPossible = pollenCount
  171. end
  172. if totalPossible > (math.floor(waxCount / 2)) then
  173. totalPossible = (math.floor(waxCount / 2))
  174. end
  175. if totalPossible > (math.floor(planksCount / 3)) then
  176. totalPossible = (math.floor(planksCount / 3))
  177. end
  178.  
  179. craftAmountRemaining = totalPossible
  180. print ("craftAmountRemaining: " .. craftAmountRemaining)
  181.  
  182. craftAmountPerCarpenter = math.floor(craftAmountRemaining / 8)
  183.  
  184. if craftAmountPerCarpenter == 0 then
  185. craftAmountPerCarpenter = 1
  186. end
  187. print ("craftAmountPerCarpenter: " .. craftAmountPerCarpenter)
  188. end
  189.  
  190. function fillInventory()
  191. for i=1,16,1 do
  192. turtle.select(i)
  193. turtle.suckDown()
  194. end
  195. end
  196.  
  197. function dumpInventory()
  198. for i=1,16,1 do
  199. turtle.select(i)
  200. turtle.dropDown()
  201. end
  202. end
  203.  
  204. function findJelly()
  205. for i=1,16,1 do
  206. local meta = turtle.getItemDetail(i)
  207. if(meta ~= nil and meta.name == jelly) then
  208. return i
  209. end
  210. end
  211. end
  212.  
  213. function findWax()
  214. for i=1,16,1 do
  215. local meta = turtle.getItemDetail(i)
  216. if(meta ~= nil and meta.name == wax) then
  217. return i
  218. end
  219. end
  220. end
  221.  
  222. function findPollen()
  223. for i=1,16,1 do
  224. local meta = turtle.getItemDetail(i)
  225. if(meta ~= nil and meta.name == pollen) then
  226. return i
  227. end
  228. end
  229. end
  230.  
  231. function findPlanks()
  232. for i=1,16,1 do
  233. local meta = turtle.getItemDetail(i)
  234. if(meta ~= nil and meta.name == planks) then
  235. return i
  236. end
  237. end
  238. end
  239.  
  240. function pushJelly(amountToCraft)
  241.  
  242. local targetAmount = amountToCraft
  243.  
  244. while targetAmount > 0 do
  245.  
  246. local slot = findJelly()
  247. turtle.select(slot)
  248. local currentStack = turtle.getItemCount()
  249.  
  250. if currentStack > targetAmount then
  251. currentStack = targetAmount
  252. end
  253.  
  254. turtle.drop(currentStack)
  255.  
  256. targetAmount = targetAmount - currentStack
  257.  
  258. end
  259.  
  260. end
  261.  
  262. function pushPollen(amountToCraft)
  263.  
  264. local targetAmount = amountToCraft
  265.  
  266. while targetAmount > 0 do
  267.  
  268. local slot = findPollen()
  269. turtle.select(slot)
  270. local currentStack = turtle.getItemCount()
  271.  
  272. if currentStack > targetAmount then
  273. currentStack = targetAmount
  274. end
  275.  
  276. turtle.drop(currentStack)
  277.  
  278. targetAmount = targetAmount - currentStack
  279.  
  280. end
  281.  
  282. end
  283.  
  284. function pushWax(amountToCraft)
  285.  
  286. local targetAmount = amountToCraft * 2
  287.  
  288. while targetAmount > 0 do
  289.  
  290. local slot = findWax()
  291. turtle.select(slot)
  292. local currentStack = turtle.getItemCount()
  293.  
  294. if currentStack > targetAmount then
  295. currentStack = targetAmount
  296. end
  297.  
  298. turtle.drop(currentStack)
  299.  
  300. targetAmount = targetAmount - currentStack
  301.  
  302. end
  303.  
  304. end
  305.  
  306. function pushPlanks(amountToCraft)
  307.  
  308. local targetAmount = amountToCraft * 3
  309.  
  310. while targetAmount > 0 do
  311.  
  312. local slot = findPlanks()
  313. turtle.select(slot)
  314. local currentStack = turtle.getItemCount()
  315.  
  316. if currentStack > targetAmount then
  317. currentStack = targetAmount
  318. end
  319.  
  320. turtle.drop(currentStack)
  321.  
  322. targetAmount = targetAmount - currentStack
  323.  
  324. end
  325.  
  326. end
  327.  
  328. function pushItems(amountToCraft, carpenterNumber)
  329. pushJelly(amountToCraft)
  330. pushPlanks(amountToCraft)
  331. pushPollen(amountToCraft)
  332. pushWax(amountToCraft)
  333.  
  334.  
  335. carpentersArray[carpenterNumber] = carpentersArray[carpenterNumber] + amountToCraft
  336. print ("set carpenter " .. carpenterNumber .. " to " .. carpentersArray[carpenterNumber].. " items completed")
  337.  
  338. craftAmountRemaining = craftAmountRemaining - amountToCraft
  339. print ("craftAmountRemaining: " .. craftAmountRemaining)
  340.  
  341. end
  342.  
  343. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement