Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function sendFuel()
- nm = string.sub(os.getComputerLabel(), 2)
- if tonumber(nm) < 10 then
- nm = "0"..nm
- end
- rednet.send(1, nm..":"..turtle.getFuelLevel())
- end
- local function sendAlert(msg)
- nm = string.sub(os.getComputerLabel(), 2)
- if tonumber(nm) < 10 then
- nm = "0"..nm
- end
- rednet.send(1, nm..":"..msg)
- end
- local function getFaceTo()
- local pos0 = vector.new(gps.locate())
- turtle.forward()
- local pos1 = vector.new(gps.locate())
- local dist = pos1 - pos0
- turtle.back()
- local faceto = ""
- if dist.x > 0 then
- faceto = "west"
- elseif dist.x < 0 then
- faceto = "east"
- elseif dist.z > 0 then
- faceto = "north"
- else
- faceto = "south"
- end
- return faceto
- end
- local function setFaceTo(dire)
- local pos0 = vector.new(gps.locate())
- turtle.forward()
- local pos1 = vector.new(gps.locate())
- local dist = pos1 - pos0
- turtle.back()
- local faceto = ""
- if dist.x > 0 then
- faceto = "west"
- if dire == "north" then
- turtle.turnRight()
- elseif dire == "south" then
- turtle.turnLeft()
- elseif dire == "east" then
- turtle.turnRight()
- turtle.turnRight()
- end
- elseif dist.x < 0 then
- faceto = "east"
- if dire == "north" then
- turtle.turnLeft()
- elseif dire == "south" then
- turtle.turnRight()
- elseif dire == "west" then
- turtle.turnRight()
- turtle.turnRight()
- end
- elseif dist.z > 0 then
- faceto = "north"
- if dire == "east" then
- turtle.turnRight()
- elseif dire == "south" then
- turtle.turnRight()
- turtle.turnRight()
- elseif dire == "west" then
- turtle.turnLeft()
- end
- else
- faceto = "south"
- if dire == "east" then
- turtle.turnLeft()
- elseif dire == "north" then
- turtle.turnRight()
- turtle.turnRight()
- elseif dire == "west" then
- turtle.turnRight()
- end
- end
- end
- local function forBack(dist)
- d = dist
- if d < 0 then
- d = -d
- end
- for i=1, d do
- if dist > 0 then
- turtle.forward()
- else
- turtle.back()
- end
- end
- end
- local function upDown(dist)
- d = dist
- if d < 0 then
- d = -d
- end
- for i=1, d do
- if dist > 0 then
- turtle.up()
- else
- turtle.down()
- end
- end
- end
- local function moveAxisX(faceto, curpos, target)
- local dist = target - curpos
- if faceto == "north" then
- turtle.turnLeft()
- forBack(dist.x)
- turtle.turnRight()
- elseif faceto == "south" then
- turtle.turnRight()
- forBack(dist.x)
- turtle.turnLeft()
- elseif faceto == "east" then
- turtle.turnLeft()
- turtle.turnLeft()
- forBack(dist.x)
- turtle.turnRight()
- turtle.turnRight()
- else
- forBack(dist.x)
- end
- end
- local function moveAxisZ(faceto, curpos, target)
- local dist = target - curpos
- if faceto == "north" then
- forBack(dist.z)
- elseif faceto == "south" then
- turtle.turnLeft()
- turtle.turnLeft()
- forBack(dist.z)
- turtle.turnRight()
- turtle.turnRight()
- elseif faceto == "east" then
- turtle.turnLeft()
- forBack(dist.z)
- turtle.turnRight()
- else
- turtle.turnRight()
- forBack(dist.z)
- turtle.turnLeft()
- end
- end
- local function moveAxisY(curpos, target)
- local dist = target - curpos
- upDown(dist.y)
- end
- local function dispStatus(pos, target, faceto)
- print("pos:"..tostring(pos))
- print("faceTo:"..faceto)
- print("dist:"..tostring(target - pos))
- end
- local function suckChest()
- turtle.turnRight()
- turtle.turnRight()
- turtle.select(2)
- turtle.suck()
- turtle.turnLeft()
- turtle.turnLeft()
- end
- local function makedots(points)
- for i = 1, #points do
- if turtle.detect() == true then
- sendAlert("Obstacle")
- break
- else
- turtle.forward()
- end
- if string.sub(points, i, i) == "1" then
- turtle.turnLeft()
- turtle.select(2)
- turtle.place()
- turtle.turnRight()
- end
- end
- end
- local function erasedots(wide)
- for i = 1, tonumber(wide) do
- if turtle.detect() == true then
- sendAlert("Obstacle")
- break
- else
- turtle.forward()
- end
- turtle.turnLeft()
- local ret, msg = turtle.dig()
- if ret == False then
- print("msg:"..msg)
- end
- turtle.turnRight()
- end
- end
- local function gotoxyz(name, x, y, z)
- print(name..":"..x..":"..y..":"..z)
- local curpos = vector.new(gps.locate())
- local faceto = getFaceTo()
- local target = vector.new(tonumber(x), tonumber(y), tonumber(z))
- moveAxisZ(faceto, curpos, target)
- moveAxisY(curpos, target)
- moveAxisX(faceto, curpos, target)
- curpos = vector.new(gps.locate())
- faceto = getFaceTo()
- target = vector.new(tonumber(x), tonumber(y), tonumber(z))
- local dist = target - curpos
- if dist.x ~= 0 or dist.y ~= 0 or dist.z ~= 0 then
- moveAxisZ(faceto, curpos, target)
- moveAxisY(curpos, target)
- moveAxisX(faceto, curpos, target)
- end
- end
- rednet.open("left")
- print("name:"..os.getComputerLabel())
- print("fuel:"..turtle.getFuelLevel())
- while true do
- if turtle.getFuelLevel() < 80 then
- turtle.refuel()
- end
- local sid, msg, dis = rednet.receive()
- name, px, py, pz = msg:match("([^,]+),([^,]+),([^,]+),([^,]+)")
- if name == os.getComputerLabel() then
- if px == "suck" then
- print(name..":"..px)
- suckChest()
- elseif px == "dots" then
- print(name..":"..px..":"..py)
- makedots(py)
- elseif px == "erase" then
- print(name..":"..px..":"..py)
- erasedots(py)
- elseif px == "faceto" then
- print(name..":"..px..":"..py)
- setFaceTo(py)
- elseif px == "fuel" then
- print(name..":"..px..":"..turtle.getFuelLevel())
- sendFuel()
- else
- gotoxyz(name, px, py, pz)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment