Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- pastebin get 4SKNxjPs
- local pickSlot = 13
- local modemSlot = 14
- local geoSlot = 15
- local chestSlot = 16
- local pickName = "minecraft:diamond_pickaxe"
- local modemName = "computercraft:wireless_modem_advanced"
- local geoName = "advancedperipherals:geo_scanner"
- local minFuel = turtle.getFuelLimit() * 0.25
- local maxFuel = turtle.getFuelLimit() * 0.75
- local distanceWeight = -2
- local blockScoreWeight = 1
- local controllerID = 6
- local maxY = 9
- local minY = 8
- local blockScores = {
- ["minecraft:diamond_ore"] = 10,
- ["minecraft:gold_ore"] = 8,
- ["minecraft:iron_ore"] = 5,
- ["minecraft:coal_ore"] = 7,
- ["minecraft:redstone_ore"] = 2,
- ["minecraft:lapis_ore"] = 1,
- ["minecraft:emerald_ore"] = 11,
- ["minecraft:oak_log"] = 9,
- ["minecraft:dark_oak_log"] = 9,
- ["minecraft:birch_log"] = 9,
- ["minecraft:jungle_log"] = 9,
- ["minecraft:acaia_log"] = 9,
- ["minecraft:sand"] = 3
- }
- local validFuels = {
- ["minecraft:coal_block"] = true,
- ["minecraft:coal"] = true,
- ["minecraft:charcoal"] = true,
- ["minecraft:blaze_rod"] = true
- }
- sleep(1)--might need to increase to prevent server crash
- function Hold(s)
- if turtle.getItemCount(s) == 1 then
- turtle.select(s)
- turtle.equipLeft()
- local i = turtle.getItemDetail()
- if i ~= nil then
- if i.name == pickName then
- turtle.transferTo(pickSlot)
- elseif i.name == geoName then
- turtle.transferTo(geoSlot)
- elseif i.name == modemName then
- turtle.transferTo(modemSlot)
- else
- turtle.transferTo(1)
- end
- end
- turtle.select(1)
- end
- end
- function Network()
- Hold(modemSlot)
- local x, y, z = gps.locate()
- while x == nil do
- print("waiting for GPS")
- sleep(3)
- x, y, z = gps.locate()
- end
- rednet.open("left")
- function SendUpdate()
- rednet.send(controllerID, {
- ["timestamp"] = os.time(),
- ["id"] = os.getComputerID(),
- ["x"] = x,
- ["y"] = y,
- ["z"] = z,
- ["fuel"] = turtle.getFuelLevel()
- })
- end
- SendUpdate()
- local id, message = nil
- while true do
- print("Waiting for message from controller")
- id, message = rednet.receive(nil, 10)
- if id ~= controllerID then--if nothing send another update
- SendUpdate()
- else
- break
- end
- if message ~= nil and type(message) == "table" then
- --could be a broadcast for new controller
- if message["controllerID"] ~= nil and message["newControllerPassword"] == "dododo" then
- controllerID = message["controllerID"]
- end
- end
- end
- print("Message from controller received")
- if message["distanceWeight"] ~= nil then
- distanceWeight = message["distanceWeight"]
- end
- if message["blockScoreWeight"] ~= nil then
- blockScoreWeight = message["blockScoreWeight"]
- end
- if message["controllerID"] ~= nil then
- controllerID = message["controllerID"]
- end
- if message["blockScores"] ~= nil then
- blockScores = message["blockScores"]
- end
- if message["maxY"] ~= nil then
- maxY = message["maxY"]
- end
- if message["minY"] ~= nil then
- minY = message["minY"]
- end
- rednet.close("left")
- Hold(pickSlot)
- if y > maxY then
- for i = y-1, maxY, -1 do
- Down()
- end
- end
- if y < minY then
- for i = y+1, minY do
- Up()
- end
- end
- end
- function Scan()
- Hold(geoSlot)
- local geo = peripheral.find("geoScanner")
- local s, r = nil
- while s == nil do
- s, r = geo.scan(8)
- if r ~= nil then
- print(r)
- end
- sleep(2)
- end
- --print(textutils.serialize(s))
- return s
- end
- function ScoreScan(s, d)
- local hsi, hsv, hsx, hsy, hsz = nil
- for k, v in pairs(s) do
- if blockScores[v.name] ~= nil then
- local score = ((math.abs(v.x) + math.abs(v.y) + math.abs(v.z)) * distanceWeight) + (blockScores[v.name] * blockScoreWeight)
- if v.x == 0 and v.z == 0 then
- score = score * 0.7
- end
- --prefer routes with no turning
- if d == "north" and v.x == 0 and v.z <= 0 then
- score = score * 0.6
- end
- if d == "south" and v.x == 0 and v.z >= 0 then
- score = score * 0.6
- end
- if d == "east" and v.x >= 0 and v.z == 0 then
- score = score * 0.6
- end
- if d == "west" and v.x <= 0 and v.z == 0 then
- score = score * 0.6
- end
- if hsi == nil or score > hsv then
- hsi = k
- hsv = score
- hsx = v.x
- hsy = v.y
- hsz = v.z
- end
- end
- end
- return hsi, hsv, hsx, hsy, hsz
- end
- function Forward()
- while turtle.forward() == false do
- turtle.dig()
- end
- end
- function Up()
- while turtle.up() == false do
- turtle.digUp()
- end
- end
- function Down()
- while turtle.down() == false do
- turtle.digDown()
- end
- end
- function Dig()
- while turtle.detect() do
- turtle.dig()
- end
- end
- function DigUp()
- while turtle.detectUp() do
- turtle.digUp()
- end
- end
- function DigDown()
- while turtle.detectDown() do
- turtle.digDown()
- end
- end
- local dirs = {
- [1] = "north",
- [2] = "east",
- [3] = "south",
- [4] = "west"
- }
- function GetIndex(v, t)
- for tk, tv in pairs(t) do
- if tv == v then
- return tk
- end
- end
- end
- function Left(d)
- turtle.turnLeft()
- local cd = GetIndex(d, dirs)
- nd = cd - 1
- if nd == 0 then
- nd = 4
- end
- print("Facing " .. dirs[nd])
- return dirs[nd]
- end
- function Right(d)
- turtle.turnRight()
- local cd = GetIndex(d, dirs)
- nd = cd + 1
- if nd == 5 then
- nd = 1
- end
- print("Facing " .. dirs[nd])
- return dirs[nd]
- end
- function Face(d, nd)
- local cd = GetIndex(d, dirs)
- local td = GetIndex(nd, dirs)
- if cd == td then
- print("Already facing " .. nd)
- return nd
- end
- if cd == 1 and td == 4 then
- turtle.turnLeft()
- print("Facing " .. nd)
- return nd
- end
- if cd == 4 and td == 1 then
- turtle.turnRight()
- print("Facing " .. nd)
- return nd
- end
- if cd < td then
- for i = cd, td - 1 do
- turtle.turnRight()
- end
- end
- if cd > td then
- for i = cd, td + 1, -1 do
- turtle.turnLeft()
- end
- end
- print("Facing " .. nd)
- return nd
- end
- function Inv()
- Hold(pickSlot)
- DigUp()
- turtle.select(16)
- while turtle.placeUp() == false do
- DigUp()
- turtle.attackUp()
- end
- local c = peripheral.wrap("top")
- while turtle.getItemCount(6) > 0 or turtle.getItemCount(1) > 0 do
- for i = 1, 12 do
- turtle.select(i)
- turtle.dropUp(64)
- end
- end
- if turtle.getFuelLevel() < minFuel then
- local fuelAttempts = 0
- turtle.select(1)
- while turtle.getFuelLevel() < maxFuel do
- local cl = c.list()
- print(cl[1].name)
- if cl[1] ~= nil and validFuels[cl[1].name] then
- turtle.suckUp(cl[1].count - 1)
- turtle.refuel(64)
- else
- print("Waiting for fuel")
- end
- sleep(1)
- fuelAttempts = fuelAttempts + 1
- if fuelAttempts > 10 and turtle.getFuelLevel() > minFuel then
- print("Will try get more fule later")
- break
- end
- end
- end
- turtle.select(16)
- Hold(pickSlot)
- turtle.digUp()
- turtle.select(1)
- end
- function GetDir()
- Hold(modemSlot)
- local sx, sy, sz = gps.locate()
- while sx == nil do
- print("Coudn't get GPS")
- sleep(3)
- sx, sy, sz = gps.locate()
- end
- Hold(pickSlot)
- Forward()
- Hold(modemSlot)
- local nx, ny, nz = gps.locate()
- while nx == nil do
- print("Coudn't get GPS")
- sleep(3)
- nx, ny, nz = gps.locate()
- end
- turtle.turnLeft()
- turtle.turnLeft()
- Hold(pickSlot)
- Forward()
- turtle.turnLeft()
- turtle.turnLeft()
- if nz > sz then
- return "south"
- end
- if nz < sz then
- return "north"
- end
- if nx > sx then
- return "east"
- end
- if nx < sx then
- return "west"
- end
- end
- ---
- if turtle.getItemCount(16) == 0 then
- Hold(pickSlot)
- turtle.select(16)
- turtle.digUp()
- end
- Inv()
- local dir = GetDir()
- print("facing " .. dir)
- function GotoRel(rx, ry, rz)
- if ry >= 0 then
- for i = 1, ry do
- Up()
- end
- else
- for i = -1, ry, -1 do
- Down()
- end
- end
- if rx > 0 then
- dir = Face(dir, "east")
- for i = 1, rx do
- Forward()
- end
- elseif rx < 0 then
- dir = Face(dir, "west")
- for i = -1, rx, -1 do
- Forward()
- end
- end
- if rz > 0 then
- dir = Face(dir, "south")
- for i = 1, rz do
- Forward()
- end
- elseif rz < 0 then
- dir = Face(dir, "north")
- for i = -1, rz, -1 do
- Forward()
- end
- end
- end
- function Cycle()
- local scanned = Scan(8)
- Hold(pickSlot)
- for noScanTimes = 1, 20 do
- local hsi, hsv, hsx, hsy, hsz = ScoreScan(scanned)
- if turtle.getItemCount(6) > 0 or turtle.getFuelLevel() < minFuel then
- Inv()
- end
- turtle.select(1)
- -- if nothing found, move forward
- if hsi == nil then
- print("Nothing found")
- for i = 1, 8 do
- Forward()
- end
- return
- else
- print(hsi, hsv, hsx, hsy, hsz)
- GotoRel(hsx, hsy, hsz)
- --offset scanned by moved
- for k, v in pairs(scanned) do
- scanned[k].x = v.x - hsx
- scanned[k].y = v.y - hsy
- scanned[k].z = v.z - hsz
- if scanned[k].x == 0 and scanned[k].y == 0 and scanned[k].z == 0 then
- scanned[k] = nil
- end
- end
- end
- end
- end
- while true do
- Network()
- for c = 1, 2 do
- print("c: " .. tostring(c))
- Cycle()
- sleep(0)
- end
- end
Add Comment
Please, Sign In to add comment