hunter3216

compBoi

Mar 31st, 2023 (edited)
45
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 compactor 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 = 0.5
  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.  
  60. -- open file for reading
  61. local file = io.open("recipes.txt", "r")
  62.  
  63. if file ~= nil then
  64. -- read file contents into a string
  65. local contents = file:read("*all")
  66.  
  67. -- close file
  68. file:close()
  69.  
  70. -- load contents as a Lua chunk and execute it to set the recipes variable
  71. recipes = load("return " .. contents)()
  72. end
  73. end
  74.  
  75. -- Initializes the chests
  76. function initChests()
  77. ioChest = peripheral.wrap(ioSide)
  78. medChest = peripheral.wrap(medSide)
  79. robot.down()
  80. fuelChest = peripheral.wrap(fuelSide)
  81. turtle.select(16)
  82. turtle.suckDown(64-turtle.getItemCount())
  83. robot.up()
  84. end
  85.  
  86. -- Clears the items from the basin (also breaks it so clear and liquid)
  87. function clearItems()
  88. robot.getEmptySlot()
  89. print("using on block")
  90. robot.useOnBlock(true)
  91. print("done")
  92. robot.getEmptySlot()
  93. robot.dig()
  94. robot.place()
  95.  
  96. robot.select(1)
  97. while robot.getItemCount() ~= 0 and robot.getSelectedSlot() ~= 16 do
  98. robot.dropUp()
  99. robot.select(robot.getSelectedSlot() + 1)
  100. end
  101. robot.select(1)
  102.  
  103. local medItems = medChest.list()
  104.  
  105. for k,_ in pairs(medItems) do
  106. medChest.pushItems(ioSide, k)
  107. end
  108.  
  109. end
  110.  
  111. -- checks if the recipe matches the items
  112. -- courtesy of GPT3
  113. function checkRecipe(items, recipe)
  114. -- First, we need to check if the tables have the same number of elements
  115. if #items ~= #recipe then
  116. return false
  117. end
  118.  
  119. -- Next, we need to create a copy of recipe to keep track of the elements that we've already matched
  120. local recipe_copy = {}
  121. for i, v in ipairs(recipe) do
  122. recipe_copy[i] = v
  123. end
  124.  
  125. -- Then, we loop through each element in items
  126. for _, element1 in ipairs(items) do
  127. local match_found = false
  128.  
  129. -- We loop through each element in recipe_copy to find a match for element1
  130. for i, element2 in ipairs(recipe_copy) do
  131. if element1.name == element2.name and element1.count == element2.count then
  132. -- We found a match, so we remove element2 from recipe_copy and move on to the next element in items
  133. table.remove(recipe_copy, i)
  134. match_found = true
  135. break
  136. end
  137. end
  138.  
  139. -- If we didn't find a match for element1 in recipe, then the tables don't match
  140. if not match_found then
  141. return false
  142. end
  143. end
  144.  
  145. -- If we got through all of the elements in items without finding any mismatches, and recipe_copy is now empty, then the tables match
  146. return #recipe_copy == 0
  147. end
  148.  
  149.  
  150. -- returns the recipe corresponding to the items in the input chest
  151. -- first checks if the correct recipe is the previous recipe
  152. function findRecipe(inputItems, prevRecipe)
  153. if prevRecipe == nil then
  154. prevRecipe = recipes[1] -- sets prevRecipe to the first recipe if there wasn't a prev recipe.
  155. end
  156.  
  157. if checkRecipe(inputItems, prevRecipe.inputs) then
  158. return prevRecipe
  159. end
  160.  
  161. rIx = nil
  162. for i = 1, #recipes do
  163. if checkRecipe(inputItems, recipes[i].inputs) then
  164. rIx = i
  165. break
  166. end
  167. end
  168.  
  169. if rIx ~= nil then
  170. return recipes[rIx]
  171. else
  172. return nil
  173. end
  174. end
  175.  
  176. -- Takes the items from the IO chest and gets them to the turtle
  177. -- Collects the items in the same order as the in the recipe
  178. function collectIngredients(ioItems, recipe)
  179. -- create a copy of ioItems to keep track of the elements that we've already matched
  180. local itemsCopy = {}
  181. for i, v in ipairs(ioItems) do
  182. itemsCopy[i] = v
  183. end
  184.  
  185. local slot = 1
  186. robot.select(1)
  187. for _,v1 in pairs(recipe.inputs) do
  188. for i,v2 in ipairs(itemsCopy) do
  189. if v1.name == v2.name then
  190. medChest.pullItems(ioSide, slot)
  191. slot = slot + 1
  192. turtle.suckUp()
  193. table.remove(itemsCopy, i)
  194. robot.select(robot.getSelectedSlot()+1)
  195. break
  196. end
  197. end
  198. end
  199. end
  200.  
  201. -- heats the blaze burner if needed
  202. function heat()
  203. if (heatTime - os.clock()) < 16 then
  204. print("heating")
  205. robot.down()
  206. local curSlot = robot.getSelectedSlot()
  207. robot.select(16)
  208. robot.useOnBlock(true)
  209. heatTime = 64 + math.max(heatTime, os.clock())
  210. robot.select(curSlot)
  211. robot.up()
  212. end
  213. end
  214.  
  215. function getFuel()
  216. -- Refuel here if needed
  217. if robot.getItemCount(16) < 32 then
  218. local downFlag = robot.down()
  219. local curSlot = robot.getSelectedSlot()
  220. robot.select(16)
  221. while robot.getItemCount() < 64 do
  222. robot.suckDown(64 - robot.getItemCount())
  223. end
  224.  
  225. if downFlag then robot.up() end
  226. robot.select(curSlot)
  227. end
  228. end
  229.  
  230. -- With all of the ingredients ready, crafts the items
  231. function craft(recipe)
  232. for i = 1, recipe.craftNum do
  233. if recipe.heated == true then
  234. heat()
  235. end
  236.  
  237. for k,v in pairs(recipe.inputs) do
  238. robot.select(k)
  239.  
  240. if string.find(v.name, "bucket") and not string.find(v.name,"minecraft:bucket") then
  241. robot.useOnBlock(true)
  242. elseif v.name == "create:blaze_cake" then
  243. robot.down()
  244. robot.useOnBlock(true)
  245. robot.up()
  246. else
  247. robot.drop(v.count/recipe.craftNum)
  248. end
  249. end
  250.  
  251. os.sleep(craftTime)
  252. end
  253. robot.useOnBlock(true)
  254. end
  255.  
  256. -- Take all of the results and puts them in the ioChest
  257. function depositResult()
  258. outputIx = 0
  259. for i = 1, 15 do
  260. if robot.getItemCount(i) > 0 then
  261. robot.select(i)
  262. robot.dropUp()
  263. medChest.pushItems(ioSide, 1)
  264. end
  265. end
  266. end
  267.  
  268. -- Initializes the computer for mixer automation
  269. function init()
  270. if heatOverride then
  271. heatTime = 2147483648
  272. end
  273.  
  274. initPosition()
  275. loadRecipes()
  276. initChests()
  277. clearItems()
  278. end
  279.  
  280. -- The main function - does the things
  281. function main()
  282. init()
  283.  
  284. local prevRecipe = nil
  285. while true do
  286. local ioItems = ioChest.list()
  287.  
  288. if ioItems[1] ~= nil then
  289. recipe = findRecipe(ioItems, prevRecipe)
  290.  
  291. if recipe ~= nil then
  292. collectIngredients(ioItems, recipe)
  293. craft(recipe)
  294. depositResult()
  295. prevRecipe = recipe
  296. end
  297. end
  298.  
  299. os.sleep(0) -- sleep to prevent 'Too long without yielding' error
  300. end
  301. end
  302.  
  303. main()
Advertisement
Add Comment
Please, Sign In to add comment