Advertisement
asweigart

runbakery

Aug 3rd, 2016
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. -- Robotic Bakery program
  2. -- By Al Sweigart
  3. -- turtleappstore.com/AlSweigart
  4. -- Runs a bakery.
  5.  
  6. --[[
  7. Bakery blueprint:
  8.  
  9. e.M.S.E.W (back)
  10. 123456789
  11. CCssssssm (front)
  12.  
  13. Key:
  14. e = Chest of empty buckets
  15. M = Chest of milk buckets
  16. S = Chest of sugar
  17. E = Chest of eggs
  18. W = Chest of wheat
  19. C = Chest of cakes (and empty buckets)
  20. s = Shelf for cakes
  21. m = Monitor
  22. . = Empty space
  23. Numbers = Number positions
  24.  
  25. (Leave number positions empty!)
  26. Pos 9 and facing m is start pos.
  27. --]]
  28.  
  29. os.loadAPI('hare')
  30.  
  31. -- constants for ingredient positions
  32. EMPTY_BUCKETS_POS = 1
  33. CAKE_CHEST_POS = 2
  34. MILK_BUCKETS_POS = 3
  35. SUGAR_POS = 5
  36. EGGS_POS = 7
  37. WHEAT_POS = 9
  38. MONITOR_POS = 9
  39.  
  40. turtleFacing = nil
  41. turtlePosition = nil
  42.  
  43. if hare.findBlock('computercraft:CC-Peripheral') then
  44. turtleFacing = 'front'
  45. turtlePosition = 9
  46. else
  47. print('ERROR: Please position turtle next to')
  48. print('the monitor and re-run this program.')
  49. end
  50.  
  51.  
  52. -- moveTo() moves the turtle to the
  53. -- numbered and faces it either 'front'
  54. -- or 'back' of the bakery.
  55. local function moveTo(toPosition, toFacing)
  56. if turtlePosition == nil or turtleFacing == nil then
  57. print('ERROR: Don\'t know where turtle is.')
  58. return false
  59. end
  60.  
  61. -- move to the destination
  62. if toPosition > turtlePosition then
  63. -- move towards higher numbered positions
  64. if turtleFacing == 'front' then
  65. turtle.turnLeft()
  66. else
  67. turtle.turnRight()
  68. end
  69.  
  70. for i = 1, (toPosition - turtlePosition) do
  71. if turtle.forward() == false then
  72. error('Movement obstructed.')
  73. end
  74. end
  75.  
  76. if toFacing == 'front' then
  77. turtle.turnRight() -- face front
  78. else
  79. turtle.turnLeft() -- face back
  80. end
  81. elseif toPosition < turtlePosition then
  82. -- move towards lower numbred positions
  83. if turtleFacing == 'front' then
  84. turtle.turnRight()
  85. else
  86. turtle.turnLeft()
  87. end
  88.  
  89. for i = 1, (turtlePosition - toPosition) do
  90. if turtle.forward() == false then
  91. error('Movement obstructed.')
  92. end
  93. end
  94.  
  95. if toFacing == 'front' then
  96. turtle.turnLeft() -- face front
  97. else
  98. turtle.turnRight() -- face back
  99. end
  100. else
  101. if toFacing ~= turtleFacing then
  102. -- turn around
  103. turtle.turnLeft()
  104. turtle.turnLeft()
  105. end
  106. end
  107.  
  108. turtlePosition = toPosition
  109. turtleFacing = toFacing
  110. end
  111.  
  112.  
  113. -- getCakeCount() returns the number
  114. -- in the cakecount file
  115. local function getCakeCount()
  116. local fo = fs.open('cakecount', 'r')
  117.  
  118. if fo == nil then return 0 end
  119.  
  120. local count = fo.readAll()
  121. fo.close()
  122. return tonumber(count)
  123. end
  124.  
  125.  
  126. -- setCakeCount() set the number in
  127. -- the cakecount file
  128. local function setCakeCount(count)
  129. local fo = fs.open('cakecount', 'w')
  130. fo.write(tostring(count))
  131. fo.close()
  132. end
  133.  
  134.  
  135. -- pickUpIngredient() moves to the
  136. -- chest with the ingredient and gets
  137. -- one for each slot in 'slots'.
  138. local function pickUpIngredient(ingredient, position, facing, slots)
  139. print('Getting ' .. ingredient .. '...')
  140. moveTo(position, facing) -- go to ingredient's chest
  141. local index, slot
  142. for index, slot in pairs(slots) do
  143. -- pick up ingredient
  144. turtle.select(slot)
  145. if turtle.suck(1) == false then
  146. print('Out of ' .. ingredient .. '!')
  147. print('Sleeping until chest is refilled...')
  148. while turtle.suck(1) == false do
  149. os.sleep(10)
  150. end
  151. end
  152. end
  153. end
  154.  
  155.  
  156. -- bakeCake() will do everything to
  157. -- make a cake
  158. local function bakeCake()
  159. -- TODO - do a check to make sure all inventory slots are empty?
  160. pickUpIngredient('wheat', WHEAT_POS, 'back', {9, 10, 11})
  161. pickUpIngredient('eggs', EGGS_POS, 'back', {6})
  162. pickUpIngredient('sugar', SUGAR_POS, 'back', {5, 7})
  163. pickUpIngredient('milk', MILK_BUCKETS_POS, 'back', {1, 2, 3})
  164.  
  165. print('Making a cake...')
  166. turtle.select(16) -- craft cake in slot 16
  167. turtle.craft()
  168.  
  169. -- drop off empty buckets
  170. print('Dropping off empty buckets...')
  171. moveTo(EMPTY_BUCKETS_POS, 'back')
  172. for slot = 1, 3 do
  173. turtle.select(slot)
  174. turtle.drop() -- put bucket in chest
  175. end
  176.  
  177. -- put cake on shelf, if there's space
  178. turtle.select(16) -- select cake
  179. local i
  180. local placedOnShelf = false
  181. for i = 8, 3, -1 do
  182. -- try shelf at positions 8 to 3
  183. moveTo(i, 'front')
  184. if not turtle.detect() then
  185. turtle.place()
  186. placedOnShelf = true
  187. break
  188. end
  189. end
  190.  
  191. if placedOnShelf then
  192. print('Cake placed on shelf.')
  193. else
  194. moveTo(CAKE_CHEST_POS, 'front')
  195. turtle.drop()
  196. print('Cake placed in cake chest.')
  197. end
  198.  
  199. -- update cake count file
  200. local cakesBaked = getCakeCount() + 1
  201. setCakeCount(cakesBaked)
  202. print('I have baked ' .. cakesBaked .. ' cakes!')
  203.  
  204. -- update the status monitor
  205. moveTo(MONITOR_POS, 'front')
  206. mon = peripheral.wrap('front')
  207. if mon == nil then
  208. error('No monitor in front of turtle!')
  209. end
  210. mon.clear()
  211. mon.setCursorPos(1, 1)
  212. mon.write('Cakes')
  213. mon.setCursorPos(1, 2)
  214. mon.write('baked:')
  215. mon.setCursorPos(3, 3)
  216. mon.write(tostring(cakesBaked))
  217. end
  218.  
  219.  
  220. while true do
  221. bakeCake()
  222. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement