Romanok2805

beer.lua

May 26th, 2018 (edited)
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. local r = require("robot")
  3. local component = require("component")
  4. local inventory = component.inventory_controller
  5.  
  6. local inventorySize = r.inventorySize()
  7. local myPosition = {x = 1, y = 1}
  8. local positionForReturn = {x = 1, y = 1}
  9. local slot = 0
  10. local barrel = {hop = 0, wheat = 0, capsules = 30, timeInHours = 0, x = 1, y = 1, quantity = 0, remainingQuantity = 0}
  11.  
  12. --Функции
  13. local function errorAndPrint(reason)
  14.   print("Произошла ошибка: "..reason.."\nПозиции робота:\nX: "..myPosition.x.."\nY: "..myPosition.y)
  15.   os.exit()
  16. end
  17.  
  18. local function putInABarrel()
  19.   for i = 1 + slot, 9 + slot, 4 do -- Будет брать 1, 5, 9 слоты, потом 2, 6, 10 и т.д.
  20.     r.select(i)
  21.     if r.count() ~= 0 then
  22.       inventory.equip()
  23.       if r.use() ~= nil then
  24.         errorAndPrint("Не удалось положить ингредиенты\nв бочку.")
  25.       end
  26.     end
  27.   end
  28.   if slot ~= 3 then
  29.     slot = slot + 1
  30.   else
  31.     slot = 0
  32.   end
  33. end
  34.  
  35. local function nextFreeBarrel()
  36.   if myPosition.x == barrel.x then
  37.     positionForReturn.x, positionForReturn.y = 1, myPosition.y + 1
  38.   else
  39.     positionForReturn.x, positionForReturn.y = myPosition.x + 1, myPosition.y
  40.   end
  41. end
  42.  
  43. local function suckUpAndCheck(quantity, slot)
  44.   r.select(slot)
  45.   while quantity ~= r.count() do
  46.     if not r.suckUp(quantity - r.count()) then
  47.       errorAndPrint("Недостаточно предметов.")
  48.     end
  49.   end
  50. end
  51.  
  52. local function goForward(steps) -- Ходить сколько нужно, больше одного раза.
  53.   for i = 1, steps do
  54.     r.forward()
  55.   end
  56. end
  57.  
  58. local function goBack(steps)
  59.   for i = 1, steps do
  60.     r.back()
  61.   end
  62. end
  63.  
  64. local function getItemsOnBase(quantityStacks) --От 1 до 4
  65.   for i = 1, quantityStacks do
  66.     suckUpAndCheck(barrel.hop, i)
  67.   end
  68.   r.turnLeft()
  69.   r.forward()
  70.   for i = 5, quantityStacks + 4 do
  71.     suckUpAndCheck(barrel.wheat, i)
  72.   end
  73.   r.forward()
  74.   for i = 9, quantityStacks + 8 do
  75.     suckUpAndCheck(barrel.capsules, i)
  76.   end
  77.   goBack(2)
  78.   r.turnRight()
  79.   r.forward()
  80. end
  81.  
  82. local function moveToX1()
  83.   if myPosition.x ~= 1 then
  84.     r.turnRight()
  85.     goForward(myPosition.x - 1)
  86.     r.turnLeft()
  87.     myPosition.x = 1
  88.   end
  89. end
  90.  
  91. local function GoToBase()
  92.   nextFreeBarrel()
  93.   moveToX1()
  94.   for i = 1, myPosition.y - 1 do
  95.     r.down()
  96.   end
  97.   myPosition.y = 1
  98.   r.back()
  99. end
  100.  
  101. local function GoBackToBarrel()
  102.   for i = 1, positionForReturn.y - 1 do
  103.     r.up()
  104.   end
  105.   if positionForReturn.x ~= 1 then
  106.     r.turnLeft()
  107.     goForward(positionForReturn.x - 1)
  108.     r.turnRight()
  109.   end
  110.   myPosition.x, myPosition.y = positionForReturn.x, positionForReturn.y
  111. end
  112.  
  113. local function goLeft()
  114.   r.turnLeft()
  115.   r.forward()
  116.   r.turnRight()
  117.   myPosition.x = myPosition.x + 1
  118. end
  119.  
  120. local function goUp()
  121.   r.up()
  122.   myPosition.y = myPosition.y + 1
  123. end
  124.  
  125. local function dropUpAll()
  126.   for i = 1, inventorySize do
  127.     r.select(i)
  128.     if not r.dropUp() then
  129.       break
  130.     end
  131.   end
  132. end
  133.  
  134.  
  135. --Спрашиваем про сорт
  136. print([[Выберите сорт пива:
  137. 1 - Soup (Похлёбка)
  138. 2 - White (Белое)
  139. 3 - Без префикса
  140. 4 - Dark (Тёмноё)
  141. 5 - Black (Чёрное)
  142. 6 - Black Stuff]])
  143.  
  144. local sort = {0, 3, 5, 7, 8, 10}
  145. barrel.hop = sort[tonumber(io.read())]
  146. barrel.wheat = 10 - barrel.hop
  147. sort = nil
  148. if barrel.hop == nil then
  149.   errorAndPrint("введены неправильные данные.")
  150. end
  151.  
  152. print([[Введите соотношение пшеницы + хмеля/капсулы воды
  153. 1 - Black Stuff
  154. 2 - Thick
  155. 3 - Strong (плохо работает)
  156. 4 - Без названия
  157. 5 - Lite
  158. 6 - Stodge]])
  159.  
  160. local temp = tonumber(io.read())
  161. local koff = {9, 6, 4.5, 3, 2, 1}
  162. if koff[temp] ~= nil then
  163.   barrel.hop = math.min(math.ceil(barrel.hop * koff[temp]), 64)
  164.   barrel.wheat = math.min(math.ceil(barrel.wheat * koff[temp]), 64)
  165.   koff = nil
  166. else
  167.   koff = nil
  168.   errorAndPrint("введены неправильные данные.")
  169. end
  170.  
  171. print([[Введите время настаивания.
  172. 1 - Brew 0 часов
  173. 2 - Youngster 0.5 часа
  174. 3 - Beer 2 часа
  175. 4 - Ale 12 часов
  176. 5 - Dragonblood 24 часа]])
  177.  
  178. local time = {0, 0.5, 2, 12, 24}
  179. barrel.timeInHours = time[tonumber(io.read())]
  180. time = nil
  181. if barrel.timeInHours == nil then
  182.   errorAndPrint("введены неправильные данные.")
  183. end
  184.  
  185. print([[Точка отсчёта начинается с нижнего правого угла
  186. С права на лево - x
  187. С низу на вверх - y
  188. Пример:
  189. ######
  190. ######
  191. ######
  192. Будет записыватся как x = 6, y = 3
  193. Так же если ввести x = 1, y = 1,
  194. то робот будет работать только
  195. с одной бочкой спереди его
  196. Введите кол-во бочек:]])
  197.  
  198. io.write("x = ")
  199. barrel.x = tonumber(io.read())
  200. io.write("y = ")
  201. barrel.y = tonumber(io.read())
  202.  
  203. if barrel.x == nil or barrel.y == nil then
  204.   errorAndPrint("введены не правильные данные.")
  205. end
  206.  
  207. barrel.quantity = barrel.x * barrel.y
  208. barrel.remainingQuantity = barrel.quantity
  209.  
  210. print("Итого на одну бочку:"..
  211. "\nХмеля: "..barrel.hop..
  212. "\nПшеницы: "..barrel.wheat..
  213. "\nКапсул: 30"..
  214. "\n\nИтого для всех бочек:"..
  215. "\nБочек: "..barrel.quantity..
  216. "\nХмеля: "..barrel.quantity * barrel.hop..
  217. "\nПшеницы: "..barrel.quantity * barrel.wheat..
  218. "\nКапсул: "..barrel.x * barrel.y * 30 ..
  219. "\nВремя: "..barrel.timeInHours.." часов"..
  220. "\nУбедитесь в наличии всех ресурсов."..
  221. "Начать? (введите 0 для выхода)")
  222.  
  223. if io.read() == "0" then os.exit() end
  224.  
  225. repeat
  226.   local numberOfWork = math.min(barrel.remainingQuantity, 4)
  227.   getItemsOnBase(numberOfWork)
  228.   GoBackToBarrel()
  229.   for i = 1, numberOfWork do
  230.     putInABarrel()
  231.     if i ~= numberOfWork then
  232.       if myPosition.x ~= barrel.x then
  233.         goLeft()
  234.       else
  235.         moveToX1()
  236.         if myPosition.y ~= barrel.y then
  237.           goUp()
  238.         end
  239.       end
  240.     end
  241.   end
  242.   GoToBase()
  243.   barrel.remainingQuantity = barrel.remainingQuantity - numberOfWork
  244. until barrel.remainingQuantity == 0
  245.  
  246. print("Ингредиенты загружены.")
  247. if barrel.timeInHours ~= 0 then
  248.   for i = 0.5, barrel.timeInHours, 0.5 do
  249.     print("Осталось: "..barrel.timeInHours-i.." часов")
  250.     os.sleep(1800)
  251.   end
  252.   os.sleep(300) --5 минут для того, чтобы всё точно сварилось
  253. end
  254.  
  255. barrel.remainingQuantity = barrel.quantity
  256. r.select(1)
  257. positionForReturn.x, positionForReturn.y = 1, 1
  258.  
  259. repeat
  260.   local numberOfWork = math.min(barrel.remainingQuantity, inventorySize - 1)
  261.   r.forward()
  262.   GoBackToBarrel()
  263.   for i = 1, numberOfWork do
  264.     r.swing()
  265.     if i ~= numberOfWork then
  266.       if myPosition.x ~= barrel.x then
  267.         goLeft()
  268.       else
  269.         moveToX1()
  270.         if myPosition.y ~= barrel.y then
  271.           goUp()
  272.         end
  273.       end
  274.     end
  275.   end
  276.   GoToBase()
  277.   r.turnLeft()
  278.   goForward(3)
  279.   dropUpAll()
  280.   goBack(3)
  281.   r.turnRight()
  282.   barrel.remainingQuantity = barrel.remainingQuantity - numberOfWork
  283. until barrel.remainingQuantity == 0
  284.  
  285. print("Пиво сварено и положено в сундук!")
Add Comment
Please, Sign In to add comment