Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- gapLength=25
- coloffset=8
- package.path = package.path .. ";../?.lua"
- require("mockturtle")
- function run()
- jogLeft(1)
- digCube(7, 3, 3, -1)
- forward(1)
- up(1)
- turnLeft()
- forward()
- digCube(gapLength, 2, 5, -1)
- forward(1)
- jogRight(1)
- down(2)
- digCube(gapLength - 2, 2, 3, -1)
- up(2)
- turnAround()
- forward(5)
- jogRight(1)
- digCube(gapLength, 2, -5, -1)
- forward(1)
- jogLeft(1)
- down(2)
- digCube(gapLength - 2, 2, -3, -1)
- up(2)
- turnAround()
- forward(1)
- turnLeft()
- forward(2)
- turnLeft()
- down(1)
- digCube(coloffset, 3, 5, -1)
- forward(coloffset + 3)
- digCube(gapLength - coloffset - 3, 3, 5, -1)
- back(coloffset + 5)
- turnLeft()
- -- forward(2)
- -- down(1)
- -- jogLeft(2)
- -- forward(1)
- -- turnRight()
- -- showPos(" done!")
- end
- -- *********************** STOCK ***********************
- _debug = true
- fuelSlot = 1
- _xPos, _yPos, _zPos = 0, 0, 0
- NORTH, EAST, SOUTH, WEST = 0, 1, 2, 3
- _heading = NORTH
- function getOption(val, t, default)
- if type(val) == t then
- return val
- else
- return default
- end
- end
- function headingToString(heading)
- if _heading == NORTH then
- return 'north'
- elseif _heading == SOUTH then
- return 'south'
- elseif _heading == EAST then
- return 'east'
- elseif _heading == WEST then
- return 'west'
- else
- return 'UNKNOWN'
- end
- end
- function showPos(message)
- print(string.format("(%d,%d,%d) - %s - %s", _xPos, _yPos, _zPos, headingToString(_heading), message))
- end
- function clearForward()
- while turtle.detect() do turtle.dig() end
- end
- function clearDown()
- if turtle.detect() then turtle.dig() end
- end
- function clearUp()
- while (turtle.detectUp()) do
- turtle.digUp()
- end
- end
- function refuel()
- if turtle.getFuelLevel() < 15 then
- print("refueling")
- turtle.select(fuelSlot)
- turtle.refuel(5)
- end
- end
- function selectSlot(slot, overflow)
- while turtle.getItemCount(slot) == 0 do
- slot = slot + 1
- if not overflow or slot > overflow then
- return
- end
- end
- turtle.select(slot)
- end
- function place(slot, overflow)
- selectSlot(slot, overflow)
- turtle.place()
- end
- function placeDown(slot, overflow)
- selectSlot(slot, overflow)
- turtle.placeDown()
- end
- function placeUp(slot, overflow)
- selectSlot(slot, overflow)
- turtle.placeUp()
- end
- function loopUntil(name, f)
- local remaining = 10
- refuel()
- while remaining > 0 and not f() do
- remaining = remaining - 1
- os.sleep(1)
- refuel()
- end
- if remaining == 0 then
- print("Failed to invoke " .. name .. " after 10 tries. Halted")
- read()
- return false
- else
- return true
- end
- end
- function forward(distance)
- if not distance then
- distance = 1
- end
- if _debug then print(" -- forward: " ..distance.."/"..headingToString(_heading)) end
- for i=1,distance do
- local success = false
- while not success do
- -- make sure the way is clear
- while turtle.detect() do
- turtle.dig()
- end
- success = loopUntil("forward", function() return turtle.forward() end)
- end
- -- update position
- if _heading == NORTH then
- _xPos = _xPos + 1
- elseif _heading == SOUTH then
- _xPos = _xPos - 1
- elseif _heading == EAST then
- _yPos = _yPos + 1
- elseif _heading == WEST then
- _yPos = _yPos - 1
- else
- print("UNKNOWN HEADING: " .. headingToString(_heading))
- end
- end
- end
- function back(distance)
- if not distance then distance = 1 end
- if _debug then print(" -- back: " ..distance) end
- for i=1,distance do
- loopUntil("back", function() return turtle.back() end)
- -- update position
- if _heading == NORTH then
- _xPos = _xPos - 1
- elseif _heading == SOUTH then
- _xPos = _xPos + 1
- elseif _heading == EAST then
- _yPos = _yPos - 1
- elseif _heading == WEST then
- _yPos = _yPos + 1
- else
- print("UNKNOWN HEADING: " .. headingToString(_heading))
- end
- end
- end
- function up(distance)
- if _debug then print(" -- up: " ..distance) end
- if not distance then distance = 1 end
- for i = 1, distance do
- local success = false
- while not success do
- while turtle.detectUp() do
- turtle.digUp()
- end
- refuel()
- success = loopUntil("up", function() return turtle.up() end)
- end
- _zPos = _zPos + 1
- end
- end
- function down(distance)
- if _debug then print(" -- down: " ..distance) end
- if not distance then distance = 1 end
- for i = 1, distance do
- local success = false
- while not success do
- while turtle.detectDown() do
- turtle.digDown()
- end
- refuel()
- success = loopUntil("down", function() return turtle.down() end)
- end
- _zPos = _zPos - 1
- end
- end
- function turnLeft()
- turtle.turnLeft();
- _heading = (_heading + 3) % 4
- if _debug then print(" -- turnLeft: " .. headingToString(_heading)) end
- end
- function turnRight()
- turtle.turnRight();
- _heading = (_heading + 1) % 4
- if _debug then print(" -- turnRight: " .. headingToString(_heading)) end
- end
- function set_heading(new_heading)
- -- this is stupid, but I'm lazy
- while _heading ~= new_heading do
- turnRight()
- end
- end
- function turnAround()
- turnRight()
- turnRight()
- end
- function jogRight(distance)
- turnRight()
- forward(distance)
- turnLeft()
- end
- function jogLeft(distance)
- turnLeft()
- forward(distance)
- turnRight()
- end
- function boolToString(b)
- if b then return "true" else return "false" end
- end
- function go(options)
- local distance = getOption(options.distance, "number", 1)
- local digUp = getOption(options.digUp, "boolean", false)
- local digDown = getOption(options.digDown, "boolean", false)
- local fillFloorSlot = getOption(options.fillFloorSlot, "number", -1)
- showPos(string.format("go(dist=%d,up=%s,down=%s,fill=%d)",
- distance,
- boolToString(digUp),
- boolToString(digDown),
- fillFloorSlot))
- if options.dir then
- set_heading(options.dir)
- end
- while distance > 0 do
- -- dig up and down in the starting place
- if digUp then
- clearUp()
- end
- if digDown and turtle.detectDown() then
- turtle.digDown()
- end
- if fillFloorSlot > 0 then
- turtle.select(fillFloorSlot)
- turtle.placeDown()
- end
- -- move forward
- forward()
- distance = distance - 1
- end
- -- dig up and down after moving
- if digUp then
- clearUp()
- end
- if digDown and turtle.detectDown() then
- turtle.digDown()
- end
- if fillFloorSlot > 0 then
- turtle.select(fillFloorSlot)
- turtle.placeDown()
- end
- end
- function digVerticalSegment(depth, height, fillFloorSlot)
- if not floorFillSlot then floorFillSlot = -1 end
- local startZ = _zPos
- showPos(string.format("digVerticalSegment(depth=%d,height=%s,fill=%d)",
- depth, height, floorFillSlot))
- go{distance = depth - 1, digUp = height > 1, digDown = false, fillFloorSlot = fillFloorSlot}
- back(depth - 1)
- local rowsLeft = height - 2;
- -- smart people would only take one pass, but that requires actual thought
- while rowsLeft > 0 do
- if rowsLeft > 2 then
- up(3)
- go{distance = depth - 1, digUp = rowsLeft > 2, digDown = true}
- rowsLeft = rowsLeft - 3 -- technically wrong, but will exit the loop
- else
- up(2)
- go{distance = depth - 1, digUp = rowsLeft > 2, digDown = false}
- rowsLeft = rowsLeft - 2 -- technically wrong, but will exit the loop
- end
- back(depth - 1)
- end
- -- return to start pos
- down(_zPos - startZ)
- end
- function digCube(depth, height, width, fillFloorSlot)
- if not fillFloorSlot then fillFloorSlot = -1 end
- showPos(string.format("digCube(depth=%d, height=%d, width=%d, ff=%d)",
- depth, height, width, fillFloorSlot))
- local indexFn
- if width > 0 then indexFn = jogRight else indexFn = jogLeft end
- digVerticalSegment(depth, height, fillFloorSlot)
- for i = 1, math.abs(width) - 1 do
- -- move to next segment
- if width > 0 then
- indexFn(1)
- else
- indexFn(1)
- end
- digVerticalSegment(depth, height, fillFloorSlot)
- end
- -- return to _yPos 0
- if width > 0 then
- jogLeft(math.abs(width) - 1)
- else
- jogRight(math.abs(width) - 1)
- end
- showPos(" digCube done")
- end
- -- *********************** STOCK ***********************
- run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement