Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- modem1 = 6565
- modem2 = 6566
- modem3 = 6567
- modem4 = 6568
- local channel = 1
- local modems = { peripheral.find("modem") }
- if #modems == 0 then error("No modems found") end
- local x = gps.locate()
- if x == nil then while true do print("Failed to connect to GPS servers") local x = gps.locate() if x ~= nil then break end end end
- local modem = modems[1]
- local map = {} -- Store chests
- local location = {}
- if not fs.exists(".map") then
- print("Map file does not exist.")
- print("Give chest location:")
- print("X: ")
- local x = read()
- print("Y: ")
- local y = read()
- print("Z: ")
- local z = read()
- if tonumber(x) ~= nil and tonumber(y) ~= nil and tonumber(z) ~= nil then
- map.chest = {}
- map.chest.x = tonumber(x)
- map.chest.y = tonumber(y)
- map.chest.z = tonumber(z)
- map.chest.type = "inputChest"
- print("Ensure all 'row' turtles are ON. Press enter")
- read()
- print("Transmitting...")
- modem.transmit(modem4,7547,"commence")
- modem.open(7547)
- os.startTimer(4)
- while true do
- local e,side,chnl,rChnl,msg,dist = os.pullEvent()
- if e == "modem_message" and type(msg) == "table" and msg.type == "setupResponse" then
- if msg.success then
- id = msg.id
- if id == "" then
- for i = 1,100 do --Generate random ID to be shown for this
- if map["empty"..i] == nil then
- id = "empty"..i
- break
- end
- end
- end
- print("Success: "..id.." is at ("..msg.x..", "..msg.y..", "..msg.z..")")
- map[id] = {}
- map[id].x = msg.x
- map[id].y = msg.y
- map[id].z = msg.z
- map[id].type = "chest"
- else
- print("Fail: "..msg.error)
- end
- elseif e == "timer" then break end
- end
- print("Completed")
- local mapFile = fs.open(".map","w")
- mapFile.write(textutils.serialize(map))
- mapFile.close()
- else
- print("One or more coords is not a number. Try again.") --Redo this with a better message
- sleep(5)
- return
- end
- else
- local mapFile = fs.open(".map","r")
- local nmap = textutils.unserialize(mapFile.readAll())
- mapFile.close()
- if nmap == nil then error("failed to unserialize .map")end
- map = nmap
- end
- local pos1x,pos1y,pos1z = gps.locate()
- local pos2x,pos2y,pos2z
- while true do
- if turtle.forward() then
- pos2x,pos2y,pos2z = gps.locate()
- break
- else
- turtle.turnRight()
- end
- end
- location.x = pos2x
- location.y = pos2y
- location.z = pos2z
- local xdif = pos2x-pos1x
- local zdif = pos2z-pos1z
- if zdif == -1 then
- location.facing = "north"
- elseif xdif == 1 then
- location.facing = "east"
- elseif zdif == 1 then
- location.facing = "south"
- elseif xdif == -1 then
- location.facing = "west"
- end
- print("Currently at ("..location.x..", "..location.y..", "..location.z..") facing "..location.facing)
- local function selSlot(slot)
- if turtle.getSelectedSlot() ~= slot then turtle.select(slot) end
- end
- local oldForward = turtle.forward
- local oldBack = turtle.back
- local oldRight = turtle.turnRight
- local oldLeft = turtle.turnLeft
- function turtle.forward(...)
- local f = oldForward(unpack({...}))
- local lx,ly,lz = gps.locate()
- location.x = lx
- location.y = ly
- location.z = lz
- return f
- end
- function turtle.back(...)
- local b = oldBack(unpack({...}))
- local lx,ly,lz = gps.locate()
- location.x = lx
- location.y = ly
- location.z = lz
- return b
- end
- function turtle.turnRight(...)
- local r = oldRight(unpack({...}))
- if location.facing == "north" then
- location.facing = "east"
- elseif location.facing == "east" then
- location.facing = "south"
- elseif location.facing == "south" then
- location.facing = "west"
- elseif location.facing == "west" then
- location.facing = "north"
- end
- return r
- end
- function turtle.turnLeft(...)
- local l = oldLeft(unpack({...}))
- if location.facing == "north" then
- location.facing = "west"
- elseif location.facing == "west" then
- location.facing = "south"
- elseif location.facing == "south" then
- location.facing = "east"
- elseif location.facing == "east" then
- location.facing = "north"
- end
- return l
- end
- local ttNum = {north=1,east=2,south=3,west=4}--Instead of doing ttNum[1] to get north, I wanna do ttNum.north to get 1
- function turnTo(direction)
- if location.facing == direction then return end
- if ttNum[direction]-ttNum[location.facing] == -1 or (location.facing == "north" and direction == "west") then
- turtle.turnLeft() return
- end
- if ttNum[direction]-ttNum[location.facing] == 1 or (location.facing == "west" and direction == "north") then
- turtle.turnRight() return
- end
- if math.abs(ttNum[direction]-ttNum[location.facing]) == 2 then
- turtle.turnRight() turtle.turnRight() return
- end
- end
- local xConv = {[-1]="west",[1]="east"}
- local zConv = {[-1]="north",[1]="south"}
- function go(x,y,z)
- if location.z ~= z then
- for nz = location.z,z,(z-location.z >= 0 and 1 or -1) do
- if z-location.z ~= 0 then
- turnTo(zConv[(z-location.z >= 0 and 1 or -1)])
- while not turtle.forward() do end
- end
- end
- end
- if location.x ~= x then
- for nx = location.x,x,(x-location.x >= 0 and 1 or -1) do
- if x-location.x ~= 0 then
- turnTo(xConv[(x-location.x >= 0 and 1 or -1)])
- while not turtle.forward() do end
- end
- end
- end
- end
- function normalOperation()
- while true do
- for slot = 1,16 do
- if turtle.getItemDetail(slot) ~= nil and map[turtle.getItemDetail(slot).name] ~= nil then
- print("Going to "..turtle.getItemDetail(slot).name)
- loc = map[turtle.getItemDetail(slot).name]
- go(loc.x,loc.y,loc.z)
- print("Going to "..loc.x..", "..loc.y..", "..loc.z)
- print("Ended at "..location.x..", "..location.y..", "..location.z)
- local name = turtle.getItemDetail(slot).name
- local bool,itm = turtle.inspectDown()
- if bool and (itm.name == "minecraft:chest" or itm.name == "minecraft:trapped_chest") then
- for slot2 = 1,16 do
- if turtle.getItemDetail(slot2) ~= nil and turtle.getItemDetail(slot2).name == name then
- turtle.select(slot2)
- turtle.dropDown()
- end
- end
- end
- end
- end
- go(map.chest.x,map.chest.y,map.chest.z)
- local b,item = turtle.inspect()
- if not b or not (item.name == "minecraft:chest" or item.name == "minecraft:trapped_chest") then
- while not b or not (item.name == "minecraft:chest" or item.name == "minecraft:trapped_chest") do
- turtle.turnRight()
- b,item = turtle.inspect()
- end
- end
- while turtle.getItemCount(16) == 0 do selSlot(1) turtle.suck() end
- sleep(3)
- end
- end
- function modemListener()
- while true do
- local e,side,chnl,rChnl,msg,dist = os.pullEvent("modem_message")
- if chnl == modem1 then
- local x,y,z = gps.locate()
- local inv = {}
- for slot = 1,16 do
- local detail = turtle.getItemDetail(slot)
- if detail == nil then
- inv[slot] = {}
- else
- inv[slot] = detail
- end
- end
- modem.transmit(rChnl,chnl,{type="infoStockerResponse",fuel=turtle.getFuelLevel(),location={x=x,y=y,z=z},inventory=inv})
- print("Sent info")
- end
- end
- end
- modem.open(modem1)
- parallel.waitForAny(normalOperation,modemListener)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement