Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Self replicating Turtle
- local x,y,z = 0,0,0 --Relative to First position
- local Orien = 0 --0:Forward; 1:Right; 2:Back; 3:Left
- local Heading = {x=0,y=0,z=0}
- local Query
- local IsFull,CanReplicate = false,false
- function MoveUp(amount)
- if (amount==0) then return end
- if (amount<0) then amount=amount*-1 end
- for i=1,amount,1 do
- turtle.digUp()
- if (turtle.up()) then
- y=y+1
- term.setCursorPos(1,2)
- term.write("I am at: " .. x .. " " .. y .. " " .. z)
- end
- end
- end
- function MoveDown(amount)
- if (amount==0) then return end
- if (amount<0) then amount=amount*-1 end
- for i=1,amount,1 do
- turtle.digDown()
- if (turtle.down()) then
- y=y-1
- term.setCursorPos(1,2)
- term.write("I am at: " .. x .. " " .. y .. " " .. z)
- end
- end
- end
- function MoveForward(amount)
- if (amount==0) then return end
- if (amount<0) then amount=amount*-1 end
- for i=1,amount,1 do
- turtle.dig()
- turtle.digDown()
- if (turtle.forward()) then
- if (Orien==0)then
- z=z-1
- elseif (Orien==1)then
- x=x+1
- elseif (Orien==2)then
- z=z+1
- elseif (Orien==3)then
- x=x-1
- end
- term.setCursorPos(1,2)
- term.write("I am at: " .. x .. " " .. y .. " " .. z)
- end
- end
- end
- function SetFacing(direction)
- if (direction==3 and Orien==0) then
- turtle.turnLeft()
- Orien=Orien-1
- return
- end
- while(true) do
- if (direction==Orien) then
- break
- end
- if (direction>Orien) then
- if (turtle.turnRight()) then
- Orien = Orien+1
- if (Orien>3) then Orien=0 end
- end
- elseif (direction<Orien) then
- if (turtle.turnLeft()) then
- Orien = Orien-1
- if (Orien<-1) then Orien=3 end
- end
- end
- end
- end
- function Movement()
- --X
- if (x>Heading.x) then
- SetFacing(3)
- elseif (x<Heading.x) then
- SetFacing(1)
- end
- MoveForward(Heading.x - x)
- --Y
- if (y>Heading.y) then
- MoveDown(Heading.y - y)
- elseif (y<Heading.y) then
- MoveUp(Heading.y - y)
- end
- --Z
- if (z>Heading.z) then
- SetFacing(0)
- elseif (z<Heading.z) then
- SetFacing(2)
- end
- MoveForward(Heading.z - z)
- end
- function Search()
- turtle.select(2)
- turtle.equipRight() --Equip scanner
- local Scanner = peripheral.wrap("right")
- local scan = Scanner.scan(5)
- if (scan ~= nil) then
- for i, data in ipairs(scan) do
- if (string.match(data.name, Query)) then
- Heading.x = data.x+x
- Heading.y = data.y+y
- Heading.z = data.z+z
- turtle.equipRight(2) --UnEquip scanner
- return
- end
- end
- --Wander North
- Heading.x = x
- Heading.y = 0
- Heading.z = z-5
- turtle.equipRight(2) --UnEquip scanner
- else
- --Wander North
- Heading.x = x
- Heading.y = 0
- Heading.z = z-5
- turtle.equipRight(2) --UnEquip scanner
- end
- end
- function Refuel()
- turtle.select(1) --Select bucket
- local success, data = turtle.inspect()
- if (success and data.name=="minecraft:lava") then
- turtle.place()
- end
- success, data = turtle.inspectUp()
- if (success and data.name=="minecraft:lava") then
- turtle.placeUp()
- end
- success, data = turtle.inspectDown()
- if (success and data.name=="minecraft:lava") then
- turtle.placeDown()
- end
- end
- function Inventory()
- local Gold,Wood,Redstone,Glass = false,false,false,false
- --1 and 2 are the bucket and the scanner, so DON'T drop them
- IsFull = true
- for i=3,16,1 do
- local data = turtle.getItemDetail(i)
- if (data~=nil) then
- if (string.match(data.name, Query)~=false) then -- Missing to count if items are full stacks
- turtle.select(i)
- turtle.drop()
- IsFull = false
- end
- if (string.match(data.name, "gold") and data.count>=14) then Gold=true end
- if (string.match(data.name, "log") and data.count>=2) then Wood=true end
- if (string.match(data.name, "redstone") and data.count>=1) then Redstone=true end
- if (string.match(data.name, "glass") and data.count>=6) then Glass=true end
- end
- end
- if (Gold and Wood and Redstone and Glass) then CanReplicate=true end
- end
- function GoHome()
- local FuelNeeded = ((x+y+z)*2)+100 -- Imagine is a straight line and then think it has to dig all the way
- -- (+100 just in case there is gravel)
- if (turtle.getFuelLevel()<=FuelNeeded or ISFull==true) then
- Heading.x=0
- Heading.y=0
- Heading.z=0
- Movement()
- return true
- end
- return false
- end
- function Replicate()
- end
- -- Main --
- term.setTextColor(colors.white)
- term.setCursorPos(1,1)
- term.write("Place a bucket in the first slot,")
- term.setCursorPos(1,2)
- term.write("then a geo scanner in the seccond,")
- term.setCursorPos(1,3)
- term.write("then equip a pickaxe on the right side")
- term.setCursorPos(1,4)
- term.write("and a chunk loader on the left.")
- term.setTextColor(colors.green)
- term.setCursorPos(1,6)
- term.write("Search for: ")
- Query = read()
- while(true) do
- term.clear()
- term.setCursorPos(1,1)
- term.write(Query .. " at: " .. Heading.x .. " " .. Heading.y .. " " .. Heading.z)
- if (GoHome()==false) then
- Search()
- Movement()
- Refuel()
- Inventory()
- Replicate()
- else
- term.clear()
- term.setCursorPos(1,1)
- term.write("Heading home.")
- break
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement