Advertisement
DjSapsan

priem

Dec 9th, 2014
1,056
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.20 KB | None | 0 0
  1. term.clear()
  2.  stat=""
  3.  isConnect=false
  4.  x=0
  5.  y=0
  6.  z=0
  7.  oldX=0
  8.  oldY=0
  9.  id=0
  10.  com=""
  11.  startX=0
  12.  startY=0
  13.  startZ=0
  14.  axisX,axisY=0,0    --ориентация
  15. myId=os.computerID()
  16. label=os.getComputerLabel()
  17. slot=turtle.getSelectedSlot()
  18. col=turtle.getItemCount(slot)
  19. fuel=turtle.getFuelLevel()
  20.  
  21. function sendStat()
  22.     while true do
  23.         os.sleep(1)
  24.         slot=turtle.getSelectedSlot()
  25.         col=turtle.getItemCount(slot)
  26.         fuel=turtle.getFuelLevel()
  27.        
  28.         x,y,z=gps.locate(1)
  29.        
  30.         if x==nil then
  31.             rednet.send(id,"No GPS! \nFuel("..tostring(fuel)..") Slot("..tostring(slot)..") Items("..tostring(col)..")")
  32.             else
  33.             rednet.send(id,"Pos("..tostring(x)..", "..tostring(y)..", "..tostring(z)..")\nFuel("..tostring(fuel)..") Slot("..tostring(slot)..") Items("..tostring(col)..")")
  34.         end
  35.     end
  36. end
  37.  
  38. function start()
  39.     rednet.open("left")
  40.     term.clear()                --очистка экрана
  41.     term.setCursorPos(1,1)
  42.     print("--------------")
  43.     print("My id: "..myId)
  44.     if label ~= nil then print("My name: "..label) end
  45.     print("--------------")
  46.     print("searching GPS...")
  47.     os.sleep(1)
  48.     x,y,z=gps.locate(1)         --поиск gps
  49.     if x==nil then print("no GPS!") else print("GPS found") end
  50.     print("Wait radiosignal...")
  51.     id,com=rednet.receive(60)
  52.     if com=="dj" then print("I'm ready") isConnect=true
  53.     rednet.send(id,"1")
  54.     else print("No radio-signal") end
  55.     os.sleep(1)
  56. end
  57.  
  58. function goToGps()
  59.     h=0 --набраная высота
  60.    
  61.     axisX,axisY=0,0                             --поворот по осям х,у
  62.     target=string.sub(com,2,#com)               --получение точки назначения
  63.     targetPos=textutils.unserialize(target)     --точка назначения
  64.     print("Call to "..targetPos[1]..", "..targetPos[2]..", "..targetPos[3])
  65.    
  66.     x,y,z=gps.locate(1)                 --выясняем местоположение
  67.     oldX=x oldZ=z                       --сохраняем положение
  68.  
  69.     while not turtle.forward() do turtle.dig() end      --двигаемся для выяснения ориентации
  70.     os.sleep(1)
  71.    
  72.     x,y,z=gps.locate(1)                 --выясняем местоположение
  73.    
  74.     if x>oldX then axisX=1 elseif x<oldX then axisX=-1 end  --по изменении координат выясняем ориентацию
  75.     if z>oldZ then axisZ=1 elseif z<oldZ then axisZ=-1 end
  76.        
  77.     startX=x startY=y startZ=z          --стартовая точка движения
  78.    
  79.     dx=targetPos[1]-startX      --разница в кордах
  80.     dy=targetPos[2]-startY      --разница в кордах
  81.     dz=targetPos[3]-startZ      --разница в кордах
  82.    
  83.     dx=math.floor(dx)   --округляем
  84.     dy=math.floor(dy)   --округляем
  85.     dz=math.floor(dz)   --округляем
  86.    
  87.     if dy>0 then                            --если точка выше черепахи, то
  88.         for i=1,math.abs(dy) do
  89.             while not turtle.up() do turtle.digUp() end         --двигатся вверх
  90.         end
  91.     elseif dy<0 then                        --иначе если точка ниже, то
  92.         for i=1,math.abs(dy) do
  93.             while not turtle.down() do turtle.digDown() end     --двигатся вниз
  94.         end
  95.     end
  96.    
  97.     if ((axisX<0) and (dx>0)) or ((axisX>0) and (dx<0)) then turtle.turnLeft() turtle.turnLeft() axisX=1 elseif         --поворачиваем в нужную сторону
  98.             (axisZ>0) and (dx>0) then turtle.turnLeft() axisZ=0 axisX=1 elseif
  99.                 (axisZ>0) and (dx<0) then turtle.turnRight() axisZ=0 axisX=-1 elseif
  100.                     (axisZ<0) and (dx<0) then turtle.turnLeft() axisZ=0 axisX=-1 elseif
  101.                         (axisZ<0) and (dx>0) then turtle.turnRight() axisZ=0 axisX=1
  102.            
  103.     end
  104.  
  105.    
  106.     for n=1, math.abs(dx) do                --двигаемся по оси х
  107.         if (turtle.detect()) and not (turtle.detectUp()) and (h<10) then turtle.up() h=h+1 else     --если спереди препятсвие, сверху нет препятсвия и набранная высота<10 то поднятся вверх и h+1
  108.         while not turtle.forward() do turtle.dig()  end end
  109.        
  110.     end
  111.    
  112.     if (axisX>0) and (dz<0) then turtle.turnLeft()  elseif  --поворачиваем в нужную сторону
  113.             (axisX>0) and (dz>0) then turtle.turnRight() elseif
  114.                 (axisX<0) and (dz>0) then turtle.turnLeft()  elseif
  115.                     (axisX<0) and (dz<0) then turtle.turnRight() end
  116.    
  117.     for n=1,  math.abs(dz) do
  118.         if (turtle.detect()) and not (turtle.detectUp()) and (h<10) then turtle.up() h=h+1 else
  119.         while not turtle.forward() do turtle.dig() end end
  120.     end
  121.    
  122.     for n=1,h do
  123.         while not turtle.down() do turtle.digDown() end
  124.     end
  125.    
  126. end
  127.  
  128. function priem()
  129.     while true do
  130.         I_,com=rednet.receive(1)
  131.            if com=="exit" then return 0 elseif
  132.             com=="up" then  turtle.digUp() turtle.up() elseif
  133.             com=="down" then turtle.digDown() turtle.down() elseif
  134.             com=="front" then turtle.dig() turtle.forward() elseif
  135.             com=="back" then turtle.back() elseif
  136.             com=="left" then turtle.turnLeft() elseif
  137.             com=="right" then turtle.turnRight() elseif
  138.             com~=nil and    string.sub(com,1,1)=="!" then goToGps() elseif
  139.             com=="dig" then turtle.dig() elseif
  140.             com==">" then slot=slot+1 if slot>16 then slot=1 end turtle.select(slot) elseif
  141.             com=="<" then slot=slot-1 if slot<1 then slot=16 end turtle.select(slot) elseif
  142.             com=="place" then turtle.placeDown()
  143.          end
  144.     end
  145. end
  146.  
  147. start()
  148. if isConnect then parallel.waitForAny(priem,sendStat) end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement