Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local FILENAME = "qData.dat" --savefile name
- local floorHeight = 0
- local homePos = {0,floorHeight,0} --robot initial placement offset in regards to everything else
- local dumpPos = {0,floorHeight,-5}
- local refuelPos = {0,floorHeight,5}
- comp = require 'component'
- event = require 'event'
- term = require 'term'
- robot = require 'robot'
- computer = require 'computer'
- net = comp.proxy(comp.modem.address)
- port = 1337 --the port on your server
- str = 500 --500's generally enough to reach anything within player chunk loading radius
- home_address = nil --leave nil
- patience = 11 --for how long to not bash the head of someone/something standing in fronth of the robot, in seconds.
- _x = 0
- _y = floorHeight
- _z = 0
- _facing = 3
- dir = {["west"] = 1, ["north"] = 2, ["east"] = 3, ["south"] = 4, ["up"] = 5, ["down"] = 6}
- --[[
- function pw()
- return math.floor(32768*2 - math.sin(math.ceil((os.time())*10))*32768)
- end
- --]]
- function listen(timeout)
- term.write("lsn call: ")
- if timeout == nil then
- local arg = {}
- _, arg["address"], _, arg["port"], arg["distance"], _,
- arg[1], arg[2], arg[3], arg[4] = event.pull("modem_message")
- local a = arg[1] local b = arg[2] local c = arg[3] local d = arg[4]
- if a == nil then a = "nil" end
- if b == nil then b = "nil" end
- if c == nil then c = "nil" end
- if d == nil then d = "nil" end
- term.write("returned_a "..a.." "..b.." "..c.." "..d.."\n")
- return arg
- else
- local arg = {}
- _, arg["address"], _, arg["port"], arg["distance"], _,
- arg[1], arg[2], arg[3], arg[4] = event.pull(timeout,"modem_message")
- local a = arg[1] local b = arg[2] local c = arg[3] local d = arg[4]
- if a == nil then a = "nil" end
- if b == nil then b = "nil" end
- if c == nil then c = "nil" end
- if d == nil then d = "nil" end
- term.write("returned_a "..a.." "..b.." "..c.." "..d.."\n")
- return arg
- end
- end
- function init()
- net.open(port)
- net.setStrength(str)
- net.broadcast(port,"init")
- local dissatisfied = true
- while dissatisfied do
- local lsn = listen()
- print("init: lsn-1 = "..lsn[1])
- if lsn[1] == "shake" then
- term.write("shake received. \n")
- home_address = lsn["address"]
- dissatisfied = false
- end
- end
- end
- function forward()
- if robot.forward() then
- if _facing == 1 then _x = _x - 1 end
- if _facing == 2 then _z = _z - 1 end
- if _facing == 3 then _x = _x + 1 end
- if _facing == 4 then _z = _z + 1 end
- --send(sr.serialize({_x,_y,_z,_facing}))
- return true
- else
- return false
- end
- end
- function back()
- if robot.back() then
- if _facing == 1 then _x = _x + 1 end
- if _facing == 2 then _z = _z + 1 end
- if _facing == 3 then _x = _x - 1 end
- if _facing == 4 then _z = _z - 1 end
- --send(sr.serialize({_x,_y,_z,_facing}))
- return true
- else
- return false
- end
- end
- function up()
- if robot.up() then
- _y = _y + 1
- --send(sr.serialize({_x,_y,_z,_facing}))
- return true
- else
- return false
- end
- end
- function down()
- if robot.down() then
- _y = _y - 1
- --send(sr.serialize({_x,_y,_z,_facing}))
- return true
- else
- return false
- end
- end
- function turnLeft()
- _facing = _facing - 1
- if _facing == 0 then _facing = 4 end
- robot.turnLeft()
- --send(sr.serialize({_x,_y,_z,_facing}))
- end
- function turnRight()
- _facing = _facing + 1
- if _facing == 5 then _facing = 1 end
- robot.turnRight()
- --send(sr.serialize({_x,_y,_z,_facing}))
- end
- function send(p1, p2, p3, p4, p5, p6, p7, p8, p9)
- net.broadcast(port,p1, p2, p3, p4, p5, p6, p7, p8, p9)
- end
- function faceWest()
- if _facing == 1 then return true end
- if _facing == 2 then turnLeft() return true end
- if _facing == 3 then turnLeft() turnLeft() return true end
- if _facing == 4 then turnRight() return true end
- end
- function faceNorth()
- if _facing == 1 then turnRight() return true end
- if _facing == 2 then return true end
- if _facing == 3 then turnLeft() return true end
- if _facing == 4 then turnLeft() turnLeft() return true end
- end
- function faceEast()
- if _facing == 1 then turnRight() turnRight() return true end
- if _facing == 2 then turnRight() return true end
- if _facing == 3 then return true end
- if _facing == 4 then turnLeft() end
- end
- function faceSouth()
- if _facing == 1 then turnLeft() return true end
- if _facing == 2 then turnLeft() turnLeft() return true end
- if _facing == 3 then turnRight() return true end
- if _facing == 4 then return true end
- end
- function faceSet(n)
- if n == 1 then faceWest() end
- if n == 2 then faceNorth() end
- if n == 3 then faceEast() end
- if n == 4 then faceSouth() end
- if n == 5 or n == 6 then return true end
- return true
- end
- function getPos()
- return _x, _y, _z
- end
- function dodge(obstacleDir,maxtries)
- print("dodging "..obstacleDir.."!\n")
- local dpm = {0,0,0} --Total displacement after dodge {+East/-West, +Up/-Down, +South/-North}
- local initFacing = _facing
- if tonumber(maxtries) == nil then maxtries = 64 end
- if obstacleDir == "up" then
- local tries = 0
- while robot.detectUp() do
- local rand = math.random(4)
- if rand % 2 == 1 then
- faceEast()
- if rand < 3 then
- if forward(1) then dpm[1] = dpm[1] + 1 end
- else
- if back(1) then dpm[1] = dpm[1] - 1 end
- end
- faceSet(initFacing)
- else
- faceSouth()
- if rand < 3 then
- if forward(1) then dpm[3] = dpm[3] + 1 end
- else
- if back(1) then dpm[3] = dpm[3] - 1 end
- end
- faceSet(initFacing)
- end
- tries = tries + 1
- if tries >= maxtries then return false end
- end
- end
- if obstacleDir == "down" then
- local tries = 0
- while robot.detectDown() do
- local rand = math.random(4)
- if rand % 2 == 1 then
- faceEast()
- if rand < 3 then
- if forward(1) then dpm[1] = dpm[1] + 1 end
- else
- if back(1) then dpm[1] = dpm[1] - 1 end
- end
- faceSet(initFacing)
- else
- faceSouth()
- if rand < 3 then
- if forward(1) then dpm[3] = dpm[3] + 1 end
- else
- if back(1) then dpm[3] = dpm[3] - 1 end
- end
- faceSet(initFacing)
- end
- tries = tries + 1
- if tries >= maxtries then return false end
- end
- end
- if obstacleDir == "south" or obstacleDir == "north" then
- local tries = 0
- while robot.detect() do
- local rand = math.random(4)
- if rand % 2 == 1 then
- faceEast()
- if rand < 3 then
- if forward(1) then dpm[1] = dpm[1] + 1 end
- else
- if back(1) then dpm[1] = dpm[1] - 1 end
- end
- faceSet(initFacing)
- else
- if rand < 3 then
- if up(1) then dpm[2] = dpm[2] + 1 end
- else
- if down(1) then dpm[2] = dpm[2] - 1 end
- end
- end
- tries = tries + 1
- if tries >= maxtries then return false end
- end
- end
- if obstacleDir == "east" or obstacleDir == "west" then
- local tries = 0
- while robot.detect() do
- local rand = math.random(4)
- if rand % 2 == 1 then
- faceSouth()
- if rand < 3 then
- if forward(1) then dpm[3] = dpm[3] + 1 end
- else
- if back(1) then dpm[3] = dpm[3] - 1 end
- end
- faceSet(initFacing)
- else
- if rand < 3 then
- if up(1) then dpm[2] = dpm[2] + 1 end
- else
- if down(1) then dpm[2] = dpm[2] - 1 end
- end
- end
- tries = tries + 1
- if tries >= maxtries then return false end
- end
- end
- --{+East/-West, +Up/-Down, +South/-North}
- if obstacleDir == "up" then
- if up() then dpm[2] = dpm[2] + 1 end
- if up() then dpm[2] = dpm[2] + 1 end
- end
- if obstacleDir == "down" then
- if down() then dpm[2] = dpm[2] - 1 end
- if down() then dpm[2] = dpm[2] - 1 end
- end
- if obstacleDir == "south" then
- faceSouth()
- if forward() then dpm[3] = dpm[3] + 1 end
- if forward() then dpm[3] = dpm[3] + 1 end
- end
- if obstacleDir == "north" then
- faceNorth()
- if forward() then dpm[3] = dpm[3] - 1 end
- if forward() then dpm[3] = dpm[3] - 1 end
- end
- if obstacleDir == "east" then
- faceEast()
- if forward() then dpm[1] = dpm[1] + 1 end
- if forward() then dpm[1] = dpm[1] + 1 end
- end
- if obstacleDir == "west" then
- faceWest()
- if forward() then dpm[1] = dpm[1] - 1 end
- if forward() then dpm[1] = dpm[1] - 1 end
- end
- return dpm[1],dpm[2],dpm[3]
- end
- function dist(x1, y1, z1, x2, y2, z2)
- return math.sqrt((x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2)
- end
- function goToPos(a, b, c, mode, stopdist)
- if stopdist == nil then stopdist = 0 end
- print("goToPos("..a..","..b..","..c..")\n")
- if tonumber(stopdist) == nil then stopdist = 0 end
- local initFacing = _facing
- while a ~= _x or b ~= _y or c ~= _z do
- while a ~= _x do
- if a>=_x then
- if a ~= _x then
- faceEast()
- while mode and robot.detect() do robot.swing() os.sleep(0.2) end
- if not forward(1) then
- if dist(_x,_y,_z,a,b,c) > stopdist then
- local a1, b1, c1 = dodge("east")
- --a = a + a1
- --b = b + b1
- --c = c + c1
- else
- return "stopdist"
- end
- end
- end
- else
- faceWest()
- while mode and robot.detect() do robot.swing() os.sleep(0.2) end
- if not forward(1) then
- if dist(_x,_y,_z,a,b,c) > stopdist then
- local a1, b1, c1 = dodge("west")
- --a = a + a1
- --b = b + b1
- --c = c + c1
- else
- return "stopdist"
- end
- end
- end
- end
- while c ~= _z do
- if c>=_z then
- if c ~= _z then
- faceSouth()
- while mode and robot.detect() do robot.swing() os.sleep(0.2) end
- if not forward(1) then
- if dist(_x,_y,_z,a,b,c) > stopdist then
- local a1, b1, c1 = dodge("south")
- --a = a + a1
- --b = b + b1
- --c = c + c1
- else
- return "stopdist"
- end
- end
- end
- else
- faceNorth()
- while mode and robot.detect() do robot.swing() os.sleep(0.2) end
- if not forward(1) then
- if dist(_x,_y,_z,a,b,c) > stopdist then
- local a1, b1, c1 = dodge("north")
- --a = a + a1
- --b = b + b1
- --c = c + c1
- else
- return "stopdist"
- end
- end
- end
- end
- while b ~= _y do
- if b>=_y then
- if b ~= _y then
- while mode and robot.detectUp() do turtle.swingUp() os.sleep(0.2) end
- if not up() then
- local a1, b1, c1 = dodge("up")
- --a = a + a1
- --b = b + b1
- --c = c + c1
- end
- end
- else
- while mode and robot.detectDown() do robot.swingDown() os.sleep(0.2) end
- if not down() then
- local a1, b1, c1 = dodge("down")
- --a = a + a1
- --b = b + b1
- --c = c + c1
- end
- end
- end
- faceSet(initFacing)
- end
- return true
- end
- local function printError(cIn)
- print(" Err: "..cIn)
- end
- local function mineForward(xLen)
- for i=1,xLen do
- while robot.detect() do
- robot.swing()
- end
- robot.swingUp()
- robot.swingDown()
- forward()
- end
- return true
- end
- function refuel(n)
- gen = comp.proxy(comp.generator.address)
- if gen ~= nil then
- local sel = robot.select()
- robot.select(16)
- gen.insert(n)
- robot.select(sel)
- return true
- else
- return false
- end
- end
- function getBlockDir(x,y,z) --Gets the major cardinal direction towards Pos. (West/North/Up..)
- if tonumber(x) and tonumber(y) and tonumber(z) then else return false end
- x = x - _x
- y = y - _y
- z = z - _z
- local m = math.max(math.abs(x),math.abs(y),math.abs(z))
- if m == math.abs(x) and m ~= 0 then
- if x>0 then return "east" else return "west" end
- else
- if m == math.abs(z) and m ~= 0 then
- if z>0 then return "south" else return "north" end
- else
- if m == math.abs(y) and m ~= 0 then
- if y>0 then return "up" else return "down" end
- end
- end
- end
- return false,"YouJustBrokeTheGame"
- end
- function facePos(x,y,z)
- faceSet(dir[getBlockDir(x,y,z)])
- end
- function dumpToPos(x,y,z)
- while goToPos(x,y,z,false,4) == "stopdist" do
- if dist(x,y,z,_x,_y,_z) == 1 then break end
- os.sleep(2)
- end
- local blockDir = getBlockDir(x,y,z)
- local sel = robot.select()
- if blockDir ~= "up" and blockDir ~= "down" then
- facePos(x,y,z)
- for i=1,15 do robot.select(i) robot.drop() end
- else
- if blockDir == "up" then
- for i=1,15 do robot.select(i) robot.dropUp() end
- end
- if blockDir == "down" then
- for i=1,15 do robot.select(i) robot.dropDown() end
- end
- end
- robot.select(sel)
- end
- function recharge(x,y,z)
- while goToPos(x,y,z,false,4) == "stopdist" do
- if dist(x,y,z,_x,_y,_z) == 1 then break end
- os.sleep(2)
- end
- local uncharged = true
- while uncharged do
- local eLeft = computer.maxEnergy() - computer.energy()
- if eLeft < 10 then uncharged = false
- else os.sleep(1) end
- end
- end
- function resupply()
- local x, y, z = getPos()
- local cFacing = _facing
- goToPos(x,floorHeight+1,z,false,0)
- dumpToPos(dumpPos[1],dumpPos[2],dumpPos[3])
- recharge(refuelPos[1],refuelPos[2],refuelPos[3])
- goToPos(x,floorHeight+1,z,false,0)
- goToPos(x,y,z,false,0)
- faceSet(cFacing)
- end
- function override(minfuel)
- if computer.energy() < minfuel then
- refuel()
- end
- if robot.count(15) ~= 0 then
- resupply()
- end
- end
- local function turn(i,invert)
- local test
- if invert == true then test = "true" else test = "false" end
- print("turn("..i..","..test..")")
- if invert == nil then invert = false end
- if invert == false then
- print("turn: invert - false")
- if i % 2 == 1 then
- turnRight()
- else
- turnLeft()
- end
- else
- print("turn: invert - true")
- if i % 2 == 1 then
- turnLeft()
- else
- turnRight()
- end
- end
- end
- function minePlane(xLen, yLen, pinvert) --Brace yourself for lots of redundancy, re-using updated old code. "Dis gon' get ugly."
- robot.swingDown()
- for i=0,yLen-1 do
- local xInvert
- local notxInvert
- if pinvert == true then
- xInvert = true
- notxInvert = false
- else
- xInvert = false
- notxInvert = true
- end
- if xLen % 2 == 1 and yLen % 2 == 1 then
- xInvert = false
- notxInvert = true
- end
- turn(i,xInvert)
- mineForward(xLen-1)
- turn(i,notxInvert)
- if i ~= yLen - 1 then
- mineForward(1)
- end
- end
- end
- function mineQuarry(xLen, yLen, zLen, invert)
- local zrem = 0
- while robot.detectDown() == false do
- down()
- zrem = zrem + 1
- end
- zLen = zLen - zrem
- zLenTrunc = math.floor(zLen/3)
- robot.swingDown()
- down()
- robot.swingDown()
- down()
- local xMirror = invert
- local xInvert
- for i=0,zLenTrunc-1 do
- if xMirror then
- if i % 2 == 1 then
- xInvert = true
- else
- xInvert = false
- end
- else
- if i % 2 == 1 then
- xInvert = false
- else
- xInvert = true
- end
- end
- minePlane(xLen, yLen, xInvert)
- if i < zLenTrunc-1 then
- robot.swingDown()
- down()
- robot.swingDown()
- down()
- robot.swingDown()
- down()
- turnRight()
- turnRight()
- end
- end
- end
- function deploy(x,y,z,dir,xq,yq,zq) --Notice: (1,*,*) = forward, (*,*,-1) = right
- if tonumber(x) ~= nil and tonumber(y) ~= nil and tonumber(z) ~= nil and dir ~= nil and tonumber(xq) ~= nil and tonumber(yq) ~= nil and tonumber(zq) ~= nil
- then
- local xc, yc, zc = getPos()
- goToPos(x,y,z,false,0)
- if dir == "left" then faceWest() else faceEast() end
- mineQuarry(xq, yq, zq, false, minfuel)
- local xc2, yc2, zc2 = getPos()
- goToPos(xc2,floorHeight+1,zc2,false,0)
- dumpToPos(dumpPos[1],dumpPos[2],dumpPos[3])
- recharge(refuelPos[1],refuelPos[2],refuelPos[3])
- send("done")
- else
- printError("deploy: missing/incorrect vars.")
- end
- end
- sr = require 'serialization'
- function main()
- init()
- while true do
- if net.isOpen(port) == false then net.open(port) end
- lsn = listen()
- print("from "..lsn["address"]..":")
- print(" "..lsn[1])
- if lsn["address"] == home_address then --deploy(x,y,z,dir,xq,yq,zq)
- if lsn[1] == "deploy" then
- send("deployed")
- local aTable = sr.unserialize(lsn[2])
- if lsn[6] == "true" then bool = true else bool = false end
- local x = tonumber(aTable[1])
- local y = tonumber(aTable[2])
- local z = tonumber(aTable[3])
- local xq = tonumber(aTable[5])
- local yq = tonumber(aTable[6])
- local zq = tonumber(aTable[7])
- print("Deploying to: ("..x..","..y..","..z..")")
- deploy(x,y,z,aTable[4],xq,yq,zq)
- end
- if lsn[1] == "park" then
- send("parking")
- goToPos(x,y,z,false,0)
- faceEast()
- end
- end
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment