Advertisement
NanoBob

Turtle

Feb 28th, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.47 KB | None | 0 0
  1. local sendChannel=666
  2.  
  3. if fs.exists("events")==false then shell.run("pastebin get L7KYC10V events") end
  4. if os.loadAPI("events")==false then error("Failed to load events API") end
  5.  
  6. if fs.exists("data")==false then shell.run("pastebin get 5FpayqrQ data") end
  7. if os.loadAPI("data")==false then error("Failed to load data API") end
  8.  
  9. if fs.exists("utils")==false then shell.run("pastebin get c4CBkgKV utils") end
  10. if os.loadAPI("utils")==false then error("Failed to load utils API") end
  11.  
  12. local self={
  13.     facing="north",
  14.     pos={x=0,y=0,z=0},
  15. }
  16.  
  17. local localChannel
  18. local task={}
  19.  
  20. function self.updatePos()
  21.     data.set("x",self.pos.x,"db/position")
  22.     data.set("y",self.pos.y,"db/position")
  23.     data.set("z",self.pos.z,"db/position")
  24. end
  25.  
  26. function self.move(direction,ammount,digging)
  27.     if ammount==nil then ammount=1 end
  28.     local moveFunction=nil
  29.     local digFunction=nil
  30.     local directionalAxis=""
  31.     if direction=="up" then
  32.         moveFunction=turtle.up
  33.         digFunction=turtle.digUp
  34.         directionalAxis="y|+1"
  35.     elseif direction=="down" then
  36.         moveFunction=turtle.down
  37.         digFunction=turtle.digDown
  38.         directionalAxis="y|-1"
  39.     else
  40.         if direction=="back" then
  41.             self.turn("right",2)
  42.         elseif direction=="right" then
  43.             self.turn("right")
  44.         elseif direction=="left" then
  45.             self.turn("left")
  46.         end
  47.         moveFunction=turtle.forward
  48.         digFunction=turtle.dig
  49.         if self.facing=="north" then
  50.             directionalAxis="z|-1"
  51.         elseif self.facing=="east" then
  52.             directionalAxis="x|1"
  53.         elseif self.facing=="south" then
  54.             directionalAxis="z|1"
  55.         elseif self.facing=="west" then
  56.             directionalAxis="x|-1"
  57.         end
  58.     end
  59.     for i=1,ammount do
  60.         if digging==true then
  61.             digFunction()
  62.         end
  63.         local axis=utils.split(directionalAxis,"|",1)
  64.         local delta=tonumber(utils.split(directionalAxis,"|",2))
  65.         local returned=moveFunction()
  66.         if returned==true then
  67.             self.pos[axis]=self.pos[axis]+delta
  68.             self.updatePos()
  69.         end
  70.         if returned==false then return returned end
  71.     end
  72. end
  73.  
  74. local facing={
  75.     [0]="west","north","east","south","west","north",
  76.     north=1,east=2,south=3,west=4,
  77. }
  78.  
  79. function self.turn(direction,ammount)
  80.     for i=1,ammount do
  81.         if direction=="right" then
  82.             turtle.turnRight()
  83.             self.facing=facing[ facing[self.facing]+1 ]
  84.         else
  85.             turtle.turnLeft()
  86.             self.facing=facing[ facing[self.facing]-1 ]
  87.         end
  88.     end
  89.     data.set("facing",facing,"db/position")
  90. end
  91.  
  92. function self.setFacing(targetFacing)
  93.     local targetFacing=string.lower(targetFacing)
  94.     local currentFacing=string.lower(self.facing)
  95.     if targetFacing==currentFacing then
  96.         return
  97.     else
  98.         local currentFacingIndex=facing[currentFacing]
  99.         local facingIndex=facing[facing]
  100.         local turns=facingIndex-currentFacingIndex
  101.         if turns<0 then
  102.             turns=turns+4
  103.         end
  104.         if turns==3 then
  105.             self.turn("left",1)
  106.         else
  107.             for i=1,turns do
  108.                 self.turn("right",1)
  109.             end
  110.         end
  111.     end
  112. end
  113.  
  114. function self.goto(x,y,z)
  115.     print("going from "..tostring(self.pos.x)..","..tostring(self.pos.y)..","..tostring(self.pos.z).."\n to "..tostring(x)..","..tostring(y)..","..tostring(z))
  116.     local yDistance=(self.pos.y-y)*-1
  117.     print("Y axis travel distance : "..yDistance)
  118.     local yTravel=true
  119.     if yDistance>0 then
  120.         local yTravel=self.move("up",yDistance)
  121.     elseif yDistance<0 then
  122.         local yTravel=self.move("down",yDistance*-1)
  123.     end
  124.     local xDistance=(self.pos.x-x)*-1
  125.     print("X axis travel distance : "..xDistance)
  126.     local xTravel=true
  127.     if xDistance>0 then
  128.         self.setFacing("east")
  129.     elseif xDistance<0 then
  130.         self.setFacing("west")
  131.         xDistance=xDistance*-1
  132.     end
  133.     xTravel=self.move("forward",xDistance)
  134.     local zDistance=(self.pos.z-z)*-1
  135.     print("Z axis travel distance : "..zDistance)
  136.     local zTravel=true
  137.     if zDistance>0 then
  138.         self.setFacing("south")
  139.     elseif zDistance<0 then
  140.         self.setFacing("north")
  141.         zDistance=zDistance*-1
  142.     end
  143.     zTravel=move("forward",zDistance)
  144.     if xTravel==false or zTravel==false or yTravel==false then
  145.         pathFind(x,y,z)
  146.     end
  147. end
  148.  
  149. function initPos()
  150.     local x=data.get("x","db/position")
  151.     local y=data.get("y","db/position")
  152.     local z=data.get("z","db/position")
  153.     if x==nil or y==nil or z==nil then
  154.         print("Please input coordinates:")
  155.         io.write("x: ")
  156.         x=tonumber(read())
  157.         data.set("x",x,"db/position")
  158.         io.write("y: ")
  159.         y=tonumber(read())
  160.         data.set("y",y,"db/position")
  161.         io.write("z: ")
  162.         z=tonumber(read())
  163.         data.set("z",z,"db/position")
  164.     end
  165.     self.pos.x,self.pos.y,self.pos.z=x,y,z
  166.     local facing=data.get("facing","db/position")
  167.     if facing==nil then
  168.         print("Please input facing:")
  169.         io.write("facing: ")
  170.         facing=read()
  171.         data.set("facing",facing,"db/position")
  172.     end
  173.     self.facing=facing
  174. end
  175.  
  176. function initConnection()
  177.     wireless=peripheral.wrap("left")
  178.     localChannel=math.random(5000)
  179.     wireless.open(localChannel)
  180.     repeat
  181.         wireless.transmit(sendChannel,localChannel,"assign")
  182.         local timer=os.startTimer(5)
  183.         repeat
  184.             event,var,sender,receiver,message=os.pullEvent()
  185.             print(localChannel.." | "..event.." : "..tostring(message))
  186.         until event=="modem_message" or event=="timer"
  187.         if message=="denied" then
  188.             wireless.close(localChannel)
  189.             localChannel=math.random(5000)
  190.             wireless.open(localChannel)
  191.         end
  192.     until message=="confirmed"
  193. end
  194.  
  195. function init()
  196.     initPos()
  197.     initConnection()
  198. end
  199. init()
  200.  
  201.  
  202. local messageHandles={
  203.     ["ping"]=function(replyChannel,message)
  204.         return "pong"
  205.     end,
  206.     ["get"]=function(replyChannel,message)
  207.         wireless.transmit(replyChannel,localChannel,"status|working")
  208.         local item=utils.split(message,"|",1)
  209.         local count=tonumber(utils.split(message,"|",2))
  210.         local pos=utils.split(message,"|",3)
  211.         if pos==nil then return end
  212.         local x=utils.split(pos,",",1)
  213.         local y=utils.split(pos,",",2)
  214.         local z=utils.split(pos,",",3)
  215.         self.goto(x,y,z)
  216.         self.pickup(item,count)
  217.         return("status|idle")
  218.     end,
  219.     ["goto"]=function(replyChannel,message)
  220.         wireless.transmit(replyChannel,localChannel,"status|working")
  221.         local pos=utils.split(message,"|",2)
  222.         local x=tonumber(utils.split(pos,",",1))
  223.         local y=tonumber(utils.split(pos,",",2))
  224.         local z=tonumber(utils.split(pos,",",3))
  225.         self.goto(x,y,z)
  226.         return("status|idle")
  227.     end,
  228.    
  229. }
  230.  
  231. function handleMessages(side,channel,replyChannel,message)
  232.     print(replyChannel.." : "..message)
  233.     local messageType=utils.split(message,"|",1)
  234.     if messageHandles[messageType]==nil then return end
  235.     local returnMessage=messageHandles[messageType](replyChannel,message)
  236.     wireless.transmit(replyChannel,localChannel,returnMessage)
  237. end
  238. events.addHandler("modem_message",handleMessages)
  239.  
  240.  
  241. while true do
  242.     events.handleCCEvents()
  243. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement