hunter3216

mixerBoi.lua

Mar 24th, 2023 (edited)
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.60 KB | None | 0 0
  1. -- This script automates the mixer in create
  2. -- Assumptions
  3. -- Constant mechanical power is provided to the mixer
  4. -- inputs will come in from the top
  5. -- outputs will go out the back
  6.  
  7. ----- CONFIG VALUES -----
  8. local heatTime = 0
  9. local craftTime = 1.85
  10.  
  11. local heatOverride = true -- If creative blaze cakes are being used set to true
  12. -------------------------
  13.  
  14. -- Program parameters
  15. ioSide = "back"
  16. medSide = "top"
  17. fuelSide = "bottom"
  18.  
  19. -- Robot initialization
  20. robot.init()
  21. robot.initAutomata()
  22. robot.setRefuelSlots({16})
  23. robot.setMinFuelLevel(800)
  24. robot.setFuelConsumptionRate(5) -- allows the automata to operate once a second at the cost of 8 fuel
  25.  
  26. -- I have to do this so the IDE knows about the API
  27. pcall(function()
  28. robot = require("APIs/robot.lua")
  29. end)
  30.  
  31. -- structure of recipes
  32. -- recipes
  33. -- recipes[i].inputs (inputs to recipe)
  34. -- recipes[i].inputs[j].name (name of ingredient)
  35. -- recipes[i].inputs[j].count (number of ingredient per craft)
  36. -- recipes[i].inputs[j].liquid (is the input a liquid - may be more than one bucket)
  37. -- recipes[i].outputs (outputs of recipes)
  38. -- recipes[i].outputs[j].name (name of output)
  39. -- recipes[i].outputs[j].count (number of the output)
  40. -- recipes[i].outputs[j].liquid (is the output a liquid - may be more than one bucket)
  41. -- recipes[i].heated (if the item is heated - false, true, and blazed are possible values)
  42. -- recipes[i].craftNum (number of times the machines operates to craft the recipe - will pull items out and put new items in for each craftNum)
  43.  
  44. -- Sets the robot its correct position facing the basin
  45. function initPosition()
  46. while robot.down() do end
  47. robot.up()
  48.  
  49. local _,block = robot.inspect()
  50.  
  51. while block == nil or block.name ~= "create:basin" do
  52. robot.turnRight()
  53. end
  54. end
  55.  
  56. -- This function loads the recipes.txt file from pastebin and refreshes the recipes
  57. -- This function gets run when the an unknown recipe shows up
  58. function loadRecipes()
  59. -- open file for reading
  60. local file = io.open("recipes.txt", "r")
  61.  
  62. if file ~= nil then
  63. -- read file contents into a string
  64. local contents = file:read("*all")
  65.  
  66. -- close file
  67. file:close()
  68.  
  69. -- load contents as a Lua chunk and execute it to set the recipes variable
  70. recipes = load("return " .. contents)()
  71. end
  72. end
  73.  
  74. -- Initializes the chests
  75. function initChests()
  76. ioChest = peripheral.wrap(ioSide)
  77. medChest = peripheral.wrap(medSide)
  78. robot.down()
  79. fuelChest = peripheral.wrap(fuelSide)
  80. turtle.select(16)
  81. turtle.suckDown(64-turtle.getItemCount())
  82. robot.up()
  83. end
  84.  
  85. -- Clears the items from the basin (also breaks it so clear and liquid)
  86. function clearItems()
  87. robot.getEmptySlot()
  88. print("using on block")
  89. robot.useOnBlock(true)
  90. print("done")
  91. robot.getEmptySlot()
  92. robot.dig()
  93. robot.place()
  94.  
  95. robot.select(1)
  96. while robot.getItemCount() ~= 0 and robot.getSelectedSlot() ~= 16 do
  97. robot.dropUp()
  98. robot.select(robot.getSelectedSlot() + 1)
  99. end
  100. robot.select(1)
  101.  
  102. local medItems = medChest.list()
  103.  
  104. for k,_ in pairs(medItems) do
  105. medChest.pushItems(ioSide, k)
  106. end
  107.  
  108. end
  109.  
  110. -- checks if the recipe matches the items
  111. -- courtesy of GPT3
  112. function checkRecipe(items, recipe)
  113. -- First, we need to check if the tables have the same number of elements
  114. if #items ~= #recipe then
  115. return false
  116. end
  117.  
  118. -- Next, we need to create a copy of recipe to keep track of the elements that we've already matched
  119. local recipe_copy = {}
  120. for i, v in ipairs(recipe) do
  121. recipe_copy[i] = v
  122. end
  123.  
  124. -- Then, we loop through each element in items
  125. for _, element1 in ipairs(items) do
  126. local match_found = false
  127.  
  128. -- We loop through each element in recipe_copy to find a match for element1
  129. for i, element2 in ipairs(recipe_copy) do
  130. if element1.name == element2.name and element1.count == element2.count then
  131. -- We found a match, so we remove element2 from recipe_copy and move on to the next element in items
  132. table.remove(recipe_copy, i)
  133. match_found = true
  134. break
  135. end
  136. end
  137.  
  138. -- If we didn't find a match for element1 in recipe, then the tables don't match
  139. if not match_found then
  140. return false
  141. end
  142. end
  143.  
  144. -- If we got through all of the elements in items without finding any mismatches, and recipe_copy is now empty, then the tables match
  145. return #recipe_copy == 0
  146. end
  147.  
  148.  
  149. -- returns the recipe corresponding to the items in the input chest
  150. -- first checks if the correct recipe is the previous recipe
  151. function findRecipe(inputItems, prevRecipe)
  152. if prevRecipe == nil then
  153. prevRecipe = recipes[1] -- sets prevRecipe to the first recipe if there wasn't a prev recipe.
  154. end
  155.  
  156. if checkRecipe(inputItems, prevRecipe.inputs) then
  157. return prevRecipe
  158. end
  159.  
  160. rIx = nil
  161. for i = 1, #recipes do
  162. if checkRecipe(inputItems, recipes[i].inputs) then
  163. rIx = i
  164. break
  165. end
  166. end
  167.  
  168. if rIx ~= nil then
  169. return recipes[rIx]
  170. else
  171. return nil
  172. end
  173. end
  174.  
  175. -- Takes the items from the IO chest and gets them to the turtle
  176. -- Collects the items in the same order as the in the recipe
  177. function collectIngredients(ioItems, recipe)
  178. -- create a copy of ioItems to keep track of the elements that we've already matched
  179. local itemsCopy = {}
  180. for i, v in ipairs(ioItems) do
  181. itemsCopy[i] = v
  182. end
  183.  
  184. local slot = 1
  185. robot.select(1)
  186. for _,v1 in pairs(recipe.inputs) do
  187. for i,v2 in ipairs(itemsCopy) do
  188. if v1.name == v2.name then
  189. medChest.pullItems(ioSide, slot)
  190. slot = slot + 1
  191. turtle.suckUp()
  192. table.remove(itemsCopy, i)
  193. robot.select(robot.getSelectedSlot()+1)
  194. break
  195. end
  196. end
  197. end
  198. end
  199.  
  200. -- heats the blaze burner if needed
  201. function heat()
  202. if (heatTime - os.clock()) < 16 then
  203. print("heating")
  204. robot.down()
  205. local curSlot = robot.getSelectedSlot()
  206. robot.select(16)
  207. robot.useOnBlock(true)
  208. heatTime = 64 + math.max(heatTime, os.clock())
  209. robot.select(curSlot)
  210. robot.up()
  211. end
  212. end
  213.  
  214. function getFuel()
  215. -- Refuel here if needed
  216. if robot.getItemCount(16) < 32 then
  217. local downFlag = robot.down()
  218. local curSlot = robot.getSelectedSlot()
  219. robot.select(16)
  220. while robot.getItemCount() < 64 do
  221. robot.suckDown(64 - robot.getItemCount())
  222. end
  223.  
  224. if downFlag then robot.up() end
  225. robot.select(curSlot)
  226. end
  227. end
  228.  
  229. -- With all of the ingredients ready, crafts the items
  230. function craft(recipe)
  231. for i = 1, recipe.craftNum do
  232. if recipe.heated == true then
  233. heat()
  234. end
  235.  
  236. for k,v in pairs(recipe.inputs) do
  237. robot.select(k)
  238.  
  239. if string.find(v.name, "bucket") and not string.find(v.name,"minecraft:bucket") then
  240. robot.useOnBlock(true)
  241. elseif v.name == "create:blaze_cake" then
  242. robot.down()
  243. robot.useOnBlock(true)
  244. robot.up()
  245. else
  246. robot.drop(v.count/recipe.craftNum)
  247. end
  248. end
  249.  
  250. os.sleep(craftTime)
  251. end
  252. robot.useOnBlock(true)
  253. end
  254.  
  255. -- Take all of the results and puts them in the ioChest
  256. function depositResult()
  257. outputIx = 0
  258. for i = 1, 15 do
  259. if robot.getItemCount(i) > 0 then
  260. robot.select(i)
  261. robot.dropUp()
  262. medChest.pushItems(ioSide, 1)
  263. end
  264. end
  265. end
  266.  
  267. -- Initializes the computer for mixer automation
  268. function init()
  269. if heatOverride then
  270. heatTime = 2147483648
  271. end
  272.  
  273. initPosition()
  274. loadRecipes()
  275. initChests()
  276. clearItems()
  277. end
  278.  
  279. -- The main function - does the things
  280. function main()
  281. init()
  282.  
  283. local prevRecipe = nil
  284. while true do
  285. local ioItems = ioChest.list()
  286.  
  287. if ioItems[1] ~= nil then
  288. recipe = findRecipe(ioItems, prevRecipe)
  289.  
  290. if recipe ~= nil then
  291. collectIngredients(ioItems, recipe)
  292. craft(recipe)
  293. depositResult()
  294. prevRecipe = recipe
  295. end
  296. end
  297.  
  298. os.sleep(0) -- sleep to prevent 'Too long without yielding' error
  299. end
  300. end
  301.  
  302. main()
Advertisement
Add Comment
Please, Sign In to add comment