Advertisement
Inumaru

Sadovnik

Jun 21st, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.54 KB | None | 0 0
  1. local component = require("component")
  2. local robot = require("robot")
  3. local shell = require("shell")
  4. local sides = require("sides")
  5. local red = component.redstone
  6.  
  7. -- Проверка робот ли используется
  8. if not component.isAvailable("robot") then
  9.   io.stderr:write("can only run on robots")
  10.   return
  11. end
  12. -- Проверка параметров запуска
  13. local args, options = shell.parse(...)
  14. if #args < 2 then
  15.   io.write("Zapusk: sadovnik <dlina> <kol-vo> <rasstoyanie>\n")
  16.   return
  17. end
  18. -- Проверка корректности ввода размера при старте
  19. local dlina = tonumber(args[1])
  20. if not dlina then
  21.   io.stderr:write("dlina ne zadana")
  22.   return
  23. end
  24. local kolvo = tonumber(args[2])
  25. if not kolvo then
  26.   io.stderr:write("kolvo ne zadano")
  27.   return
  28. end
  29. local rasstoyanie = tonumber(args[3])
  30. if not rasstoyanie then
  31.   io.stderr:write("rasstoyanie ne zadano")
  32.   return
  33. end
  34. -- Переменные
  35. local x, y, z, f = 0, 0, 0, 0
  36. -- Осн функция
  37. function sadovnik()
  38.    
  39.     while y < kolvo do
  40.    
  41.         herachVpered()
  42.        
  43.         if y < kolvo - 1 then
  44.             robot.turnLeft()
  45.             paraShagov( rasstoyanie )
  46.             robot.turnLeft()
  47.         end
  48.        
  49.         y = y + 1
  50.     end
  51.    
  52.     robot.turnRight()
  53.     paraShagov( rasstoyanie * ( kolvo - 1 ) )
  54.     kidaiLut()
  55.     robot.turnRight()
  56.     print( robot.level() )
  57.     return true
  58. end
  59. -- Вываливаем все в сундук
  60. function kidaiLut()
  61.     for k = 2, 16 do
  62.         robot.select(k)
  63.         robot.drop()
  64.     end
  65.    
  66.     robot.select(1)
  67. end
  68. -- Делаем пару шагов
  69. function paraShagov( skolkoNado )
  70.     proideno = 0
  71.     while proideno < skolkoNado do
  72.         shag()
  73.         proideno = proideno + 1
  74.     end
  75. end
  76. -- Один шаг
  77. function shag()
  78.    
  79.     if robot.forward() == false then
  80.         shag()
  81.     end
  82.    
  83. end
  84. -- Фигачим линию огорода
  85. function herachVpered()
  86.     while x < dlina do
  87.         shagVpered('plus')
  88.         robot.turnLeft()
  89.         robot.swing()
  90.         robot.place()
  91.         robot.turnAround()
  92.         robot.swing()
  93.         robot.place()
  94.         robot.turnLeft()
  95.     end
  96.    
  97.     robot.turnAround()
  98.     while x ~= 0 do
  99.         shagVpered('minus')
  100.     end
  101. end
  102. -- Шаг вперед
  103. function shagVpered( action )
  104.     if robot.forward() == false then
  105.         sharVpered()
  106.     end
  107.    
  108.     if action == 'plus' then
  109.         x = x + 1
  110.     else
  111.         x = x - 1
  112.     end
  113. end
  114. -- Поиск сигнала красной пыли позади робота
  115. function redstoneFinder()
  116.     if red.getInput( sides.top ) > 0 then
  117.         x, y, z, f = 0, 0, 0, 0
  118.         sadovnik()
  119.     else
  120.         print('net signala na4ala')
  121.     end
  122.     return true
  123. end
  124.  
  125. repeat until not redstoneFinder()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement