uepyon

receivepos2.lua

Apr 8th, 2024 (edited)
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local function sendFuel()
  2.   nm = string.sub(os.getComputerLabel(), 2)
  3.   if tonumber(nm) < 10 then
  4.     nm = "0"..nm
  5.   end
  6.   rednet.send(1, nm..":"..turtle.getFuelLevel())
  7. end
  8.  
  9. local function sendAlert(msg)
  10.   nm = string.sub(os.getComputerLabel(), 2)
  11.   if tonumber(nm) < 10 then
  12.     nm = "0"..nm
  13.   end
  14.   rednet.send(1, nm..":"..msg)
  15. end
  16.  
  17. local function getFaceTo()
  18.   local pos0 = vector.new(gps.locate())
  19.   turtle.forward()
  20.   local pos1 = vector.new(gps.locate())
  21.   local dist = pos1 - pos0
  22.   turtle.back()
  23.   local faceto = ""
  24.   if dist.x > 0 then
  25.     faceto = "west"
  26.   elseif dist.x < 0 then
  27.     faceto = "east"
  28.   elseif dist.z > 0 then
  29.     faceto = "north"
  30.   else
  31.     faceto = "south"
  32.   end
  33.   return faceto
  34. end
  35.  
  36. local function setFaceTo(dire)
  37.   local pos0 = vector.new(gps.locate())
  38.   turtle.forward()
  39.   local pos1 = vector.new(gps.locate())
  40.   local dist = pos1 - pos0
  41.   turtle.back()
  42.   local faceto = ""
  43.   if dist.x > 0 then
  44.     faceto = "west"
  45.     if dire == "north" then
  46.       turtle.turnRight()
  47.     elseif dire == "south" then
  48.       turtle.turnLeft()
  49.     elseif dire == "east" then
  50.       turtle.turnRight()
  51.       turtle.turnRight()
  52.     end
  53.   elseif dist.x < 0 then
  54.     faceto = "east"
  55.     if dire == "north" then
  56.       turtle.turnLeft()
  57.     elseif dire == "south" then
  58.       turtle.turnRight()
  59.     elseif dire == "west" then
  60.       turtle.turnRight()
  61.       turtle.turnRight()
  62.     end
  63.   elseif dist.z > 0 then
  64.     faceto = "north"
  65.     if dire == "east" then
  66.       turtle.turnRight()
  67.     elseif dire == "south" then
  68.       turtle.turnRight()
  69.       turtle.turnRight()
  70.     elseif dire == "west" then
  71.       turtle.turnLeft()
  72.     end
  73.   else
  74.     faceto = "south"
  75.     if dire == "east" then
  76.       turtle.turnLeft()
  77.     elseif dire == "north" then
  78.       turtle.turnRight()
  79.       turtle.turnRight()
  80.     elseif dire == "west" then
  81.       turtle.turnRight()
  82.     end
  83.   end
  84. end
  85.  
  86. local function forBack(dist)
  87.   d = dist
  88.   if d < 0 then
  89.     d = -d
  90.   end  
  91.   for i=1, d do
  92.     if dist > 0 then
  93.       turtle.forward()
  94.     else
  95.       turtle.back()
  96.     end
  97.   end
  98. end
  99.  
  100. local function upDown(dist)
  101.   d = dist
  102.   if d < 0 then
  103.     d = -d
  104.   end  
  105.   for i=1, d do
  106.     if dist > 0 then
  107.       turtle.up()
  108.     else
  109.       turtle.down()
  110.     end
  111.   end
  112. end
  113.  
  114. local function moveAxisX(faceto, curpos, target)
  115.   local dist = target - curpos
  116.   if faceto == "north" then
  117.     turtle.turnLeft()
  118.     forBack(dist.x)
  119.     turtle.turnRight()
  120.   elseif faceto == "south" then
  121.     turtle.turnRight()
  122.     forBack(dist.x)
  123.     turtle.turnLeft()
  124.   elseif faceto == "east" then
  125.     turtle.turnLeft()
  126.     turtle.turnLeft()
  127.     forBack(dist.x)
  128.     turtle.turnRight()
  129.     turtle.turnRight()
  130.   else
  131.     forBack(dist.x)
  132.   end
  133. end
  134.  
  135. local function moveAxisZ(faceto, curpos, target)
  136.   local dist = target - curpos
  137.   if faceto == "north" then
  138.     forBack(dist.z)
  139.   elseif faceto == "south" then
  140.     turtle.turnLeft()
  141.     turtle.turnLeft()
  142.     forBack(dist.z)
  143.     turtle.turnRight()
  144.     turtle.turnRight()
  145.   elseif faceto == "east" then
  146.     turtle.turnLeft()
  147.     forBack(dist.z)
  148.     turtle.turnRight()
  149.   else
  150.     turtle.turnRight()
  151.     forBack(dist.z)
  152.     turtle.turnLeft()
  153.   end
  154. end
  155.  
  156. local function moveAxisY(curpos, target)
  157.   local dist = target - curpos
  158.   upDown(dist.y)
  159. end
  160.  
  161. local function dispStatus(pos, target, faceto)
  162.   print("pos:"..tostring(pos))
  163.   print("faceTo:"..faceto)
  164.   print("dist:"..tostring(target - pos))
  165. end
  166.  
  167. local function suckChest()
  168.   turtle.turnRight()
  169.   turtle.turnRight()
  170.   turtle.select(2)
  171.   turtle.suck()
  172.   turtle.turnLeft()
  173.   turtle.turnLeft()
  174. end
  175.  
  176. local function makedots(points)
  177.   for i = 1, #points do
  178.     if turtle.detect() == true then
  179.       sendAlert("Obstacle")
  180.       break
  181.     else
  182.       turtle.forward()
  183.     end
  184.     if string.sub(points, i, i) == "1" then
  185.       turtle.turnLeft()
  186.       turtle.select(2)
  187.       turtle.place()
  188.       turtle.turnRight()
  189.     end
  190.   end
  191. end
  192.  
  193. local function erasedots(wide)
  194.   for i = 1, tonumber(wide) do
  195.     if turtle.detect() == true then
  196.       sendAlert("Obstacle")
  197.       break
  198.     else
  199.       turtle.forward()
  200.     end
  201.     turtle.turnLeft()
  202.     local ret, msg = turtle.dig()
  203.     if ret == False then
  204.       print("msg:"..msg)
  205.     end
  206.     turtle.turnRight()
  207.   end
  208. end
  209.  
  210. local function gotoxyz(name, x, y, z)
  211.   print(name..":"..x..":"..y..":"..z)
  212.   local curpos = vector.new(gps.locate())
  213.   local faceto = getFaceTo()
  214.   local target = vector.new(tonumber(x), tonumber(y), tonumber(z))
  215.   moveAxisZ(faceto, curpos, target)
  216.   moveAxisY(curpos, target)
  217.   moveAxisX(faceto, curpos, target)
  218.   curpos = vector.new(gps.locate())
  219.   faceto = getFaceTo()
  220.   target = vector.new(tonumber(x), tonumber(y), tonumber(z))
  221.   local dist = target - curpos
  222.   if dist.x ~= 0 or dist.y ~= 0 or dist.z ~= 0 then
  223.     moveAxisZ(faceto, curpos, target)
  224.     moveAxisY(curpos, target)
  225.     moveAxisX(faceto, curpos, target)
  226.   end
  227. end
  228.  
  229. rednet.open("left")
  230. print("name:"..os.getComputerLabel())
  231. print("fuel:"..turtle.getFuelLevel())
  232.  
  233. while true do
  234.   if turtle.getFuelLevel() < 80 then
  235.     turtle.refuel()
  236.   end
  237.  
  238.   local sid, msg, dis = rednet.receive()
  239.   name, px, py, pz = msg:match("([^,]+),([^,]+),([^,]+),([^,]+)")
  240.  
  241.   if name == os.getComputerLabel() then
  242.     if px == "suck" then
  243.       print(name..":"..px)
  244.       suckChest()
  245.     elseif px == "dots" then
  246.       print(name..":"..px..":"..py)
  247.       makedots(py)
  248.     elseif px == "erase" then
  249.       print(name..":"..px..":"..py)
  250.       erasedots(py)
  251.     elseif px == "faceto" then
  252.       print(name..":"..px..":"..py)
  253.       setFaceTo(py)
  254.     elseif px == "fuel" then
  255.       print(name..":"..px..":"..turtle.getFuelLevel())
  256.       sendFuel()
  257.     else
  258.       gotoxyz(name, px, py, pz)
  259.     end
  260.   end
  261. end
  262.  
  263.  
Tags: ccTweaked
Advertisement
Add Comment
Please, Sign In to add comment