Advertisement
Romanok2805

beer.lua

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