Advertisement
Guest User

main

a guest
Nov 27th, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.54 KB | None | 0 0
  1. term.clear()
  2. term.setCursorPos(1,1)
  3. rednet.open("right")
  4. if turtle.getFuelLevel() == 0 then
  5.   turtle.refuel()
  6. end
  7.  
  8. direction = nil
  9.  
  10. function rec()
  11.   local id, name, msg, content
  12.   id,name = rednet.receive()
  13.   f = fs.open(name, "w")
  14.   id,content = rednet.receive()
  15.   f.write(content)
  16.   f.close()
  17.   print("File "..name.." received!")
  18.   rednet.send(id, name)
  19. end
  20.  
  21. function getDirection()
  22.   local loc1 = vector.new(gps.locate(2,false))
  23.   if not turtle.forward() then
  24.     if not turtle.back() then
  25.       turtle.turnRight()
  26.       return getDirection()
  27.     end
  28.     loc1 = vector.new(gps.locate(2,false))
  29.     turtle.forward()
  30.   end
  31.   local loc2 = vector.new(gps.locate(2,false))
  32.   local heading = loc2-loc1
  33.   if heading.x == 1 then return 4
  34.   elseif heading.x == -1 then return 2
  35.   elseif heading.y == 1 then return 1
  36.   elseif heading.y == -1 then return 3
  37.   else print("ERR Direction") return -1 end  
  38.   --return ((heading.x + math.abs(heading.x)*2)+(heading.y+math.abs(heading.y)*3))
  39. end
  40.  
  41. function turnTo(dir)
  42.   while direction ~= dir do
  43.     local val = 1
  44.     if math.abs(dir-direction) == 3 then
  45.       val = -1
  46.     end
  47.     if direction < dir then turn(1*val)
  48.     elseif direction > dir then turn(-1*val) end
  49.   end
  50. end
  51.  
  52. function turn(dir)
  53.   if dir < 0 then
  54.     turtle.turnLeft()
  55.   elseif dir > 0 then
  56.     turtle.turnRight()
  57.   end
  58.   direction = direction + dir
  59.   if direction > 4 then direction = 1
  60.   elseif direction < 1 then direction = 4 end
  61. end
  62.  
  63.  
  64. function err(message)
  65.   if compId == nil then
  66.     shell.run("/connect")
  67.   end
  68.   rednet.send(compId, "ERR")
  69.   rednet.send(compId, message)
  70. end
  71.  
  72. function moveTo(target)
  73. local loc = vector.new(gps.locate(2,false))
  74.  
  75. print(target.x)
  76. local xdist = target.x-loc.x
  77. local ydist = target.y-loc.y
  78. local zdist = target.z-loc.z
  79.   while loc~=target do
  80.     sleep(1)
  81.     loc = vector.new(gps.locate(2,false))
  82.     xdist = target.x-loc.x
  83.     ydist = target.y-loc.y
  84.     zdist = target.z-loc.z
  85.     print("--")
  86.     print(xdist)
  87.     print(ydist)
  88.     print(zdist)
  89.                
  90.     if math.abs(xdist)>=math.abs(ydist) and math.abs(xdist)>=math.abs(ydist) and xdist~= 0 then
  91.       turnTo(3+xdist/math.abs(xdist))
  92.       if turtle.forward() == false then turtle.dig() end
  93.     elseif math.abs(ydist)>=math.abs(xdist) and math.abs(ydist)>=math.abs(zdist) and ydist~= 0 then
  94.       turnTo(2-ydist/math.abs(ydist))
  95.       if turtle.forward() == false then turtle.dig() end
  96.     else
  97.       if zdist>0 then if turtle.up() == false then turtle.digUp() end
  98.       elseif zdist<0 then if turtle.down() == false then turtle.digDown() end end
  99.     end  
  100.    
  101.    
  102.      
  103.   end
  104.  
  105. end
  106.  
  107. --Direction: 1 = x-
  108. --           2 = y-
  109. --           3 = x+
  110. --           4 = y+
  111.  
  112. function init()
  113.   if compId == nil then
  114.     shell.run("/connect")
  115.   end
  116.   direction = getDirection()
  117.   write("Direction: ")
  118.   print(direction)
  119. end
  120.  
  121. init()
  122.  
  123. while 1 == 1 do
  124.   if compId == nil then
  125.     shell.run("/connect")
  126.   end
  127.   local id, message = rednet.receive()
  128.   if id == compId then
  129.     if message == "RECEIVE" then
  130.       rec()
  131.     elseif message == "RUN" then
  132.       id, message = rednet.receive()
  133.       shell.run(message)
  134.     elseif message == "MOVE" then
  135.       local vec = nil
  136.       id, vec = rednet.receive(5)
  137.       if vec == nil then
  138.         print("Missed move vector")
  139.         err(message)
  140.       else
  141.         moveTo(vec)
  142.       end
  143.     elseif message == "TEST" then
  144.       id,message = rednet.receive()
  145.       sleep(2)
  146.       turnTo(message)
  147.     end  
  148.     rednet.send(compId, "DONE")
  149.   end  
  150. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement