Advertisement
Corbinhol

Compact Machines Automator

Feb 28th, 2023 (edited)
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.52 KB | None | 0 0
  1. --To Do List
  2. --[[
  3. Robot/Computer Communication
  4. Computer API
  5. Better Inventory Management
  6. Error Handling
  7. ]]
  8.  
  9. local robot = require("robot");
  10. local component = require("component");
  11. local computer = component.computer;
  12. local sides = require("sides");
  13. local look_vars = false;
  14. local serialization = require("serialization");
  15. local filesystem = require("filesystem");
  16. local term = require("term");
  17. local event = require("event");
  18.  
  19. robotStatus = "Starting Up...";
  20. local run = true;
  21. --Configuration
  22. local layout = {
  23. {1, 1, 1, 1, 1, -- bottom layer
  24. 1, 1, 1, 1, 1,
  25. 1, 1, 1, 1, 1,
  26. 1, 1, 1, 1, 1,
  27. 1, 1, 1, 1, 1,
  28. },
  29.  
  30. {1, 1, 1, 1, 1, -- middle layer
  31. 1, 0, 0, 0, 1,
  32. 1, 0, 0, 0, 1,
  33. 1, 0, 0, 0, 1,
  34. 1, 1, 1, 1, 1,
  35. },
  36.  
  37. {1, 1, 1, 1, 1, -- middle layer
  38. 1, 0, 0, 0, 1,
  39. 1, 0, 0, 0, 1,
  40. 1, 0, 0, 0, 1,
  41. 1, 1, 1, 1, 1,
  42. },
  43.  
  44. {1, 1, 1, 1, 1, -- middle layer
  45. 1, 0, 0, 0, 1,
  46. 1, 0, 0, 0, 1,
  47. 1, 0, 0, 0, 1,
  48. 1, 1, 1, 1, 1,
  49. },
  50.  
  51. {1, 1, 1, 1, 1, -- top layer
  52. 1, 1, 1, 1, 1,
  53. 1, 1, 1, 1, 1,
  54. 1, 1, 1, 1, 1,
  55. 1, 1, 1, 1, 1,
  56. }}
  57.  
  58. local dropSlot = 3 -- the slot where the drop item is
  59. local dropCount = 1 -- how many items should be dropped
  60.  
  61. local waitTime = 20 -- time between creations
  62. local rsSignalSide = sides.front; -- if no redstone signal, use nil
  63. local rsModeHigh = false; -- redstons signal means work
  64. local outputBottomRedstone = true; -- send a rs signal to the bottom
  65.  
  66. local beeps = true; -- turn off the beeps
  67. --Main Code
  68. local dimensions = #layout;
  69. local inventorySize = robot.inventorySize();
  70.  
  71. function randomBeep()
  72. if beeps then
  73. computer.beep(math.random(400, 2000))
  74. end
  75. end
  76.  
  77. function walk(steps) --Returns true is Successful, ends program if not.
  78. local success = true;
  79. if steps == nil then steps = 1; end;
  80. if steps > 0 then
  81. for _ = 1, steps do
  82. if robot.forward() == false then success = false; end
  83. end
  84. elseif steps < 0 then
  85. for _ = steps, -1 do
  86. if robot.back() == false then success = false; end
  87. end
  88. end
  89. if success == false then
  90. print("[ERROR] Couldn't Move. Ending Program. (type enableAutostart once you reset it.)");
  91. filesystem.setAutorunEnabled(false);
  92. os.exit();
  93. else
  94. return true;
  95. end
  96. end
  97.  
  98. local boolToNum = {[true]=1, [false]=0}
  99.  
  100. function isRedstoneSignal(side) --I don't know if I need this
  101. if side ~= nil then
  102. if pcall(function () _ = component.redstone end) then
  103. local redstone = component.redstone
  104. redstone.setOutput(sides.bottom, boolToNum[outputBottomRedstone])
  105. return rsModeHigh == (redstone.getInput(side) > 0)
  106. end
  107. end
  108. return true
  109. end
  110.  
  111. function countTable(input)
  112. local count = 0;
  113. for _, _ in pairs(input) do
  114. count = count + 1;
  115. end
  116. return count;
  117. end
  118.  
  119. function getNrItems()
  120. local itemCounts = {};
  121. for z = 1, dimensions do
  122. for i = 1, #layout[z] do
  123. local itemNr = layout[z][i]
  124. if itemNr ~= 0 then
  125. if itemCounts[itemNr] == nil then
  126. print("Found one");
  127. itemCounts[itemNr] = 1;
  128. else
  129. itemCounts[itemNr] = itemCounts[itemNr] + 1;
  130. end
  131. end
  132. end
  133. end
  134. if itemCounts[dropSlot] == nil then itemCounts[dropSlot] = dropCount; else itemCounts[dropSlot] = itemCounts[dropSlot] + dropCount; end
  135. return countTable(itemCounts), itemCounts
  136. end
  137.  
  138. local nrItems, itemCounts = getNrItems() --nrItems = Number Of Item Types, itemCounts = Number Of Each Item
  139.  
  140. function writeLine(line, text)
  141. if text == nil then text = line; _, line = term.getCursor(); line = line + 1; if line == 17 then line = 1; end end
  142. term.setCursor(1, line);
  143. term.write(text .. string.rep(" ", 50), false);
  144. end
  145.  
  146. function updateDisplay()
  147. while true do
  148. writeLine(1, string.rep("=", 50))
  149. writeLine(" MARA 2.0 | Running");
  150. writeLine(" Status: " .. robotStatus);
  151. writeLine(string.rep("=", 50))
  152. os.sleep(0);
  153. end
  154. end
  155. term.clear();
  156. local thread = require("thread");
  157. thread.create(updateDisplay);
  158.  
  159. function closeListener(_, _, key)
  160. if key == nil then key = 96; end
  161. if key == 96 then
  162. print("Killing Program...");
  163. run = false;
  164. filesystem.setAutorunEnabled(false)
  165. event.ignore("key_up", closeListener);
  166. end
  167. end
  168. event.listen("key_up", closeListener);
  169.  
  170. function getItems()
  171. local count = 1;
  172. for i, _ in pairs(itemCounts) do
  173. robot.turnLeft();
  174. robot.select();
  175. local itemsLeft = itemCounts[i];
  176. while itemsLeft > 0 do
  177. itemsLeft = itemsLeft - robot.suck(itemsLeft);
  178. end
  179. robot.turnRight();
  180. if count ~= nrItems then
  181. walk();
  182. end
  183. count = count + 1;
  184. end
  185. end
  186.  
  187. local function placeDown(slot)
  188. if slot == 0 then
  189. return true
  190. end
  191. robot.select(slot)
  192. return robot.placeDown()
  193. end
  194.  
  195. local function buildOneLayer(layer, layerNr)
  196. for i = 1, #layer do
  197. if layerNr == 1 and i == math.ceil(#layer/2) then
  198. robot.select(inventorySize)
  199. robot.suckDown()
  200. randomBeep()
  201. end
  202. if not placeDown(layer[i]) then
  203. for t=robot.select(),inventorySize-robot.select() do
  204. if robot.count(robot.select() + t) > 0 then robot.select(layer[i]+t); break; end
  205. end
  206. robot.transferTo(layer[i])
  207. placeDown(layer[i])
  208. end
  209. robot.forward()
  210. if i % dimensions == 0 and i ~= dimensions^2 then
  211. if i % (dimensions*2) == 0 then
  212. robot.turnLeft()
  213. else
  214. robot.turnRight()
  215. end
  216. robot.forward()
  217. if i % (dimensions*2) == 0 then
  218. robot.turnLeft()
  219. else
  220. robot.turnRight()
  221. end
  222. robot.forward()
  223. end
  224. end
  225. randomBeep()
  226. end
  227.  
  228. local function buildLayers()
  229. for i = 1, dimensions do
  230. robot.turnAround()
  231. robot.up()
  232. robot.forward()
  233. buildOneLayer(layout[i], i)
  234. end
  235. randomBeep()
  236. end
  237.  
  238. robotStatus = "Waiting...";
  239. local activate = true;
  240. while run do
  241. if activate then
  242. robotStatus = "Grabbing Items";
  243. walk(-1); --Pull out of charging bay.
  244. robot.turnRight();
  245. walk(); --Get in Inventory Collecting Position
  246. getItems(); --Get All the Required Items
  247. walk((#layout + 1)-nrItems) --Walk to starting position.
  248. robot.turnLeft();
  249. robotStatus = "Building";
  250. buildLayers();
  251. robotStatus = "Activating";
  252. walk(-1);
  253. robot.turnLeft();
  254. walk(-2);
  255. while not robot.detectDown() do robot.down() end
  256. robot.select(dropSlot);
  257. robot.drop(dropCount);
  258. robot.turnLeft();
  259. robotStatus = "Returning Home"
  260. robot.select(1);
  261. while not robot.detect() do walk(1); end
  262. robotStatus = "Waiting For Craft To Complete";
  263. os.sleep(waitTime);
  264. robotStatus = "Waiting...";
  265. end
  266. os.sleep(0);
  267. end
  268.  
  269. --term.clear();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement