Advertisement
Romanok2805

Untitled

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