Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local sendChannel=666
- if fs.exists("events")==false then shell.run("pastebin get L7KYC10V events") end
- if os.loadAPI("events")==false then error("Failed to load events API") end
- if fs.exists("data")==false then shell.run("pastebin get 5FpayqrQ data") end
- if os.loadAPI("data")==false then error("Failed to load data API") end
- if fs.exists("utils")==false then shell.run("pastebin get c4CBkgKV utils") end
- if os.loadAPI("utils")==false then error("Failed to load utils API") end
- local self={
- facing="north",
- pos={x=0,y=0,z=0},
- }
- local localChannel
- local task={}
- function self.updatePos()
- data.set("x",self.pos.x,"db/position")
- data.set("y",self.pos.y,"db/position")
- data.set("z",self.pos.z,"db/position")
- end
- function self.move(direction,ammount,digging)
- if ammount==nil then ammount=1 end
- local moveFunction=nil
- local digFunction=nil
- local directionalAxis=""
- if direction=="up" then
- moveFunction=turtle.up
- digFunction=turtle.digUp
- directionalAxis="y|+1"
- elseif direction=="down" then
- moveFunction=turtle.down
- digFunction=turtle.digDown
- directionalAxis="y|-1"
- else
- if direction=="back" then
- self.turn("right",2)
- elseif direction=="right" then
- self.turn("right")
- elseif direction=="left" then
- self.turn("left")
- end
- moveFunction=turtle.forward
- digFunction=turtle.dig
- if self.facing=="north" then
- directionalAxis="z|-1"
- elseif self.facing=="east" then
- directionalAxis="x|1"
- elseif self.facing=="south" then
- directionalAxis="z|1"
- elseif self.facing=="west" then
- directionalAxis="x|-1"
- end
- end
- for i=1,ammount do
- if digging==true then
- digFunction()
- end
- local axis=utils.split(directionalAxis,"|",1)
- local delta=tonumber(utils.split(directionalAxis,"|",2))
- local returned=moveFunction()
- if returned==true then
- self.pos[axis]=self.pos[axis]+delta
- self.updatePos()
- end
- if returned==false then return returned end
- end
- end
- local facing={
- [0]="west","north","east","south","west","north",
- north=1,east=2,south=3,west=4,
- }
- function self.turn(direction,ammount)
- for i=1,ammount do
- if direction=="right" then
- turtle.turnRight()
- self.facing=facing[ facing[self.facing]+1 ]
- else
- turtle.turnLeft()
- self.facing=facing[ facing[self.facing]-1 ]
- end
- end
- data.set("facing",facing,"db/position")
- end
- function self.setFacing(targetFacing)
- local targetFacing=string.lower(targetFacing)
- local currentFacing=string.lower(self.facing)
- if targetFacing==currentFacing then
- return
- else
- local currentFacingIndex=facing[currentFacing]
- local facingIndex=facing[facing]
- local turns=facingIndex-currentFacingIndex
- if turns<0 then
- turns=turns+4
- end
- if turns==3 then
- self.turn("left",1)
- else
- for i=1,turns do
- self.turn("right",1)
- end
- end
- end
- end
- function self.goto(x,y,z)
- print("going from "..tostring(self.pos.x)..","..tostring(self.pos.y)..","..tostring(self.pos.z).."\n to "..tostring(x)..","..tostring(y)..","..tostring(z))
- local yDistance=(self.pos.y-y)*-1
- print("Y axis travel distance : "..yDistance)
- local yTravel=true
- if yDistance>0 then
- local yTravel=self.move("up",yDistance)
- elseif yDistance<0 then
- local yTravel=self.move("down",yDistance*-1)
- end
- local xDistance=(self.pos.x-x)*-1
- print("X axis travel distance : "..xDistance)
- local xTravel=true
- if xDistance>0 then
- self.setFacing("east")
- elseif xDistance<0 then
- self.setFacing("west")
- xDistance=xDistance*-1
- end
- xTravel=self.move("forward",xDistance)
- local zDistance=(self.pos.z-z)*-1
- print("Z axis travel distance : "..zDistance)
- local zTravel=true
- if zDistance>0 then
- self.setFacing("south")
- elseif zDistance<0 then
- self.setFacing("north")
- zDistance=zDistance*-1
- end
- zTravel=move("forward",zDistance)
- if xTravel==false or zTravel==false or yTravel==false then
- pathFind(x,y,z)
- end
- end
- function initPos()
- local x=data.get("x","db/position")
- local y=data.get("y","db/position")
- local z=data.get("z","db/position")
- if x==nil or y==nil or z==nil then
- print("Please input coordinates:")
- io.write("x: ")
- x=tonumber(read())
- data.set("x",x,"db/position")
- io.write("y: ")
- y=tonumber(read())
- data.set("y",y,"db/position")
- io.write("z: ")
- z=tonumber(read())
- data.set("z",z,"db/position")
- end
- self.pos.x,self.pos.y,self.pos.z=x,y,z
- local facing=data.get("facing","db/position")
- if facing==nil then
- print("Please input facing:")
- io.write("facing: ")
- facing=read()
- data.set("facing",facing,"db/position")
- end
- self.facing=facing
- end
- function initConnection()
- wireless=peripheral.wrap("left")
- localChannel=math.random(5000)
- wireless.open(localChannel)
- repeat
- wireless.transmit(sendChannel,localChannel,"assign")
- local timer=os.startTimer(5)
- repeat
- event,var,sender,receiver,message=os.pullEvent()
- print(localChannel.." | "..event.." : "..tostring(message))
- until event=="modem_message" or event=="timer"
- if message=="denied" then
- wireless.close(localChannel)
- localChannel=math.random(5000)
- wireless.open(localChannel)
- end
- until message=="confirmed"
- end
- function init()
- initPos()
- initConnection()
- end
- init()
- local messageHandles={
- ["ping"]=function(replyChannel,message)
- return "pong"
- end,
- ["get"]=function(replyChannel,message)
- wireless.transmit(replyChannel,localChannel,"status|working")
- local item=utils.split(message,"|",1)
- local count=tonumber(utils.split(message,"|",2))
- local pos=utils.split(message,"|",3)
- if pos==nil then return end
- local x=utils.split(pos,",",1)
- local y=utils.split(pos,",",2)
- local z=utils.split(pos,",",3)
- self.goto(x,y,z)
- self.pickup(item,count)
- return("status|idle")
- end,
- ["goto"]=function(replyChannel,message)
- wireless.transmit(replyChannel,localChannel,"status|working")
- local pos=utils.split(message,"|",2)
- local x=tonumber(utils.split(pos,",",1))
- local y=tonumber(utils.split(pos,",",2))
- local z=tonumber(utils.split(pos,",",3))
- self.goto(x,y,z)
- return("status|idle")
- end,
- }
- function handleMessages(side,channel,replyChannel,message)
- print(replyChannel.." : "..message)
- local messageType=utils.split(message,"|",1)
- if messageHandles[messageType]==nil then return end
- local returnMessage=messageHandles[messageType](replyChannel,message)
- wireless.transmit(replyChannel,localChannel,returnMessage)
- end
- events.addHandler("modem_message",handleMessages)
- while true do
- events.handleCCEvents()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement