Advertisement
Guest User

smartMoving

a guest
Aug 24th, 2015
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.15 KB | None | 0 0
  1. local moving={}
  2. local component = require("component")
  3. local geo = component.geolyzer
  4. local robot=require('robot')
  5. local x=0
  6. local y=0
  7. local z=0
  8. local direct='N'
  9. function getX()
  10.   return x
  11. end
  12. function getY()
  13.   return y
  14. end
  15. function getZ()
  16.   return z
  17. end
  18. function getCoordinates()
  19.   return x,y,z,direct
  20. end
  21. function smartTurnLeft()
  22.   robot.turnLeft()
  23.   if direct=='N' then
  24.     direct='W'
  25.   elseif direct=='W' then
  26.     direct='S'
  27.   elseif direct=='S' then
  28.     direct='E'
  29.   elseif direct=='E' then
  30.     direct='N'
  31.   end
  32. end
  33. function smartTurnRight()
  34.   robot.turnRight()
  35.   if direct=='N' then
  36.     direct='E'
  37.   elseif direct=='E' then
  38.     direct='S'
  39.   elseif direct=='S' then
  40.     direct='W'
  41.   elseif direct=='W' then
  42.     direct='N'
  43.   end
  44. end
  45. function smartTurnAround()
  46.   robot.turnAround()
  47.   if direct=='N' then
  48.     direct='S'
  49.   elseif direct=='W' then
  50.     direct='E'
  51.   elseif direct=='S' then
  52.     direct='N'
  53.   elseif direct=='E' then
  54.     direct='W'
  55.   end
  56. end
  57. function turnNorth()
  58.   if direct=='S' then
  59.     robot.turnAround()
  60.   elseif direct=='W' then
  61.     robot.turnRight()
  62.   elseif direct=='E' then
  63.     robot.turnLeft()
  64.   end
  65.   direct='N'
  66. end
  67. function turnWest()
  68.   if direct=='E' then
  69.     robot.turnAround()
  70.   elseif direct=='S' then
  71.     robot.turnRight()
  72.   elseif direct=='N' then
  73.     robot.turnLeft()
  74.   end
  75.   direct='W'
  76. end
  77. function turnSouth()
  78.   if direct=='N' then
  79.     robot.turnAround()
  80.   elseif direct=='E' then
  81.     robot.turnRight()
  82.   elseif direct=='W' then
  83.     robot.turnLeft()
  84.   end
  85.   direct='S'
  86. end
  87. function turnEast()
  88.   if direct=='W' then
  89.     robot.turnAround()
  90.   elseif direct=='N' then
  91.     robot.turnRight()
  92.   elseif direct=='S' then
  93.     robot.turnLeft()
  94.   end
  95.   direct='E'
  96. end
  97. function goNorth()
  98.   if direct=='S' then
  99.     smartTurnAround()
  100.   elseif direct=='W' then
  101.     smartTurnRight()
  102.   elseif direct=='E' then
  103.     smartTurnLeft()
  104.   end
  105.   robot.swing()
  106.   a=robot.forward()
  107.   if a then
  108.     z=z-1
  109.   end
  110.   return a
  111. end
  112. function goSouth()
  113.   if direct=='N' then
  114.     smartTurnAround()
  115.   elseif direct=='E' then
  116.     smartTurnRight()
  117.   elseif direct=='W' then
  118.     smartTurnLeft()
  119.   end
  120.   robot.swing()
  121.   a=robot.forward()
  122.   if a then
  123.     z=z+1
  124.   end
  125.   return a
  126. end
  127. function goWest()
  128.   if direct=='E' then
  129.     smartTurnAround()
  130.   elseif direct=='S' then
  131.     smartTurnRight()
  132.   elseif direct=='N' then
  133.     smartTurnLeft()
  134.   end
  135.   robot.swing()
  136.   a=robot.forward()
  137.   if a then
  138.     x=x-1
  139.   end
  140.   return a
  141. end
  142. function goEast()
  143.   if direct=='W' then
  144.     smartTurnAround()
  145.   elseif direct=='N' then
  146.     smartTurnRight()
  147.   elseif direct=='S' then
  148.     smartTurnLeft()
  149.   end
  150.   robot.swing()
  151.   a=robot.forward()
  152.   if a then
  153.     x=x+1
  154.   end
  155.   return a
  156. end
  157. function goUp()
  158.   robot.swingUp()
  159.   a = robot.up()
  160.   if a then
  161.     y=y+1
  162.   end
  163.   return a
  164. end
  165. function goDown()
  166.   robot.swingDown()
  167.   a = robot.down()
  168.   if a then
  169.     y=y-1
  170.   end
  171.   return a
  172. end
  173. local function analize(f)
  174.   an = geo.analyze(f)
  175.   if an ~= nil and (an.name == "Forestry:beehives" or an.name == "minecraft:bedrock") then
  176.     return true
  177.   else
  178.     return false
  179.   end
  180. end
  181. function goZ(go_z)
  182.   if go_z<z then
  183.     while z>go_z do
  184.       if not moving.goNorth() and analize(3) then
  185.         print("Утей, итить его налево!!!... или бедрок...")
  186.         return false
  187.       end
  188.     end
  189.   elseif go_z>z then
  190.     while z<go_z do
  191.       if not moving.goSouth() and analize(3) then
  192.         print("Утей, итить его налево!!!... или бедрок...")
  193.         return false
  194.       end
  195.     end
  196.   end
  197.   return true
  198. end
  199. function goY(go_y)
  200.   if go_y<y then
  201.     while y>go_y do
  202.       if not moving.goDown() and analize(0) then
  203.         print("Утей, итить его налево!!!... или бедрок...")
  204.         return false
  205.       end
  206.     end
  207.   elseif go_y>y then
  208.     while y<go_y do
  209.       if not moving.goUp() and analize(1) then
  210.         print("Утей, итить его налево!!!... или бедрок...")
  211.         return false
  212.       end
  213.     end
  214.   end
  215.   return true
  216. end
  217. function goX(go_x)
  218.   if go_x<x then
  219.     while x>go_x do
  220.       if not moving.goWest() and analize(3) then
  221.         print("Утей, итить его налево!!!... или бедрок...")
  222.         return false        
  223.       end
  224.     end
  225.   elseif go_x>x then
  226.     while x<go_x do
  227.       if not moving.goEast() and analize(3) then
  228.         print("Утей, итить его налево!!!... или бедрок...")
  229.         return false
  230.       end
  231.     end
  232.   end
  233.   return true
  234. end
  235. moving['getX']=getX
  236. moving['getY']=getY
  237. moving['getZ']=getZ
  238. moving['smartTurnAround']=smartTurnAround
  239. moving['smartTurnLeft']=smartTurnLeft
  240. moving['smartTurnRight']=smartTurnRight
  241. moving['turnNorth']=turnNorth
  242. moving['turnEast']=turnEast
  243. moving['turnSouth']=turnSouth
  244. moving['turnWest']=turnWest
  245. moving['goNorth']=goNorth
  246. moving['goSouth']=goSouth
  247. moving['goEast']=goEast
  248. moving['goWest']=goWest
  249. moving['goUp']=goUp
  250. moving['goDown']=goDown
  251. moving['getCoordinates']=getCoordinates
  252. moving['goX']=goX
  253. moving['goY']=goY
  254. moving['goZ']=goZ
  255. package.loaded['moving']=moving
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement