Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function GpsPos()
- local x, y, z = gps.locate()
- return {x, y, z}
- end
- local args = { ... }
- if (#args ~= 2) then
- print("Gib depth then gib width plsss")
- error()
- end
- nb_blocks = 2
- movement = 1
- base_forward_movement = tonumber(args[1]) - 1
- forward_movement_left = base_forward_movement
- lateral_movement_left = tonumber(args[2])
- function Sign(p_int)
- if (p_int < 0) then
- return -1
- end
- return 1
- end
- function Abs(p_int)
- return p_int * Sign(p_int)
- end
- function UpdateMovement()
- --[[ Determine if we have gone far enough --]]
- if (forward_movement_left == 0) then
- print("Bounds detected")
- forward_movement_left = base_forward_movement
- lateral_movement_left = lateral_movement_left - 1
- if (lateral_movement_left == 0) then
- return
- end
- --[[ Lateral movement --]]
- if (movement == 1) then
- turtle.turnRight()
- turtle.forward()
- turtle.turnRight()
- else
- turtle.turnLeft()
- turtle.forward()
- turtle.turnLeft()
- end
- UpdateBlocks(GpsPos())
- --[[ Reverse current direction --]]
- movement = -movement
- end
- turtle.forward()
- forward_movement_left = forward_movement_left - 1
- end
- function DistanceManhattan(xA, zA, xB, zB)
- return Abs(xB - xA) + Abs(zB - zA)
- end
- function Distance(xA, zA, xB, zB)
- local dx = xB - xA
- local dz = zB - zA
- return sqrt(dx * dx + dy * dy)
- end
- function GetBlockId(p_pos)
- if (tonumber(Distance(0, 0, p_pos[1], p_pos[3])) == 10) then
- return 1
- end
- if (tonumber(Distance(12, 12, p_pos[1], p_pos[3]) == 6)) then
- return 1
- end
- if (tonumber(Distance(-12, 12, p_pos[1], p_pos[3]) == 6)) then
- return 1
- end
- if (tonumber(Distance(12, -12, p_pos[1], p_pos[3]) == 6)) then
- return 1
- end
- if (tonumber(Distance(-12, -12, p_pos[1], p_pos[3]) == 6)) then
- return 1
- end
- return 2
- end
- function UpdateBlocks(p_pos)
- Reload()
- --[[ Determine what to place below --]]
- print("Position is " .. p_pos[1] .. " " .. p_pos[3])
- turtle.select(GetBlockId(p_pos))
- --[[ Check if below is already the block we want to place --]]
- if (not turtle.compareDown()) then
- if (turtle.detectDown()) then
- turtle.digDown()
- end
- turtle.placeDown()
- end
- end
- function GetBlockFromNextSlot(slot_id)
- local slot_to_take_from = slot_id + nb_blocks
- print("Checking number of blocks of slot " .. slot_id)
- --[[ If the slot we want to take blocks from doesn't have them, ask them to fetch them --]]
- if (turtle.getItemCount(slot_to_take_from) < 32) then
- GetBlockFromNextSlot(slot_to_take_from)
- end
- turtle.select(slot_to_take_from)
- turtle.transferTo(slot_id, 32)
- end
- function Reload()
- local i = 1
- while (i < nb_blocks + 1) do
- print("We have " .. turtle.getItemCount(i) .. " items in slot")
- if (turtle.getItemCount(i) == 0) then
- GetBlockFromNextSlot(i)
- end
- i = i + 1
- end
- end
- function Update()
- UpdateBlocks(GpsPos())
- UpdateMovement()
- end
- function GoForwardFor(fwd)
- while (fwd > 0) do
- fwd = fwd - 1
- turtle.forward()
- end
- end
- while(lateral_movement_left > 0) do
- Update()
- end
- print("Let's go back home")
- if (args[2] % 2 == 0) then
- turtle.turnRight()
- GoForwardFor(tonumber(args[2]) - 1)
- turtle.turnRight()
- else
- turtle.turnLeft()
- GoForwardFor(tonumber(args[2]) - 1)
- turtle.turnLeft()
- GoForwardFor(tonumber(args[1]) - 1)
- turtle.turnLeft()
- turtle.turnLeft()
- end
- print("Job's done!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement