Advertisement
Romanok2805

beer.lua

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