Posted 01 March 2012 - 05:41 PM
Modified Go
I modified the go program on my server - I added digging and placement, and better yet - loops.
For example "go loop 4 loop 4 fpd 3 r end u end" will build a 4x4 set of walls, 4 high
State API
I realize there's some other state Apis - I've been enjoying making my own libraries - I'm sharing them here mostly so the other programs I post will work
tMove API
My personal movement library
Pave
I've used this to make floors of a tower, and then go back over it and place torches.
It operates left to right - in the direction it is facing to start. It does not handle state changes (reboots, etc)
Frame Eater
This will 'eat' a buildcraft frame. (After it's done) Dig out the bottom left corner of the frame and place the turtle there. As long as there are no oddities in the frame (blocks placed within the frame area, etc), it will learn how large the frame is and dismantle it for you
I modified the go program on my server - I added digging and placement, and better yet - loops.
For example "go loop 4 loop 4 fpd 3 r end u end" will build a 4x4 set of walls, 4 high
Spoiler
local tArgs = { ... }
local nArg = 1
if #tArgs < 1 then
print( "Usage: go <direction> <distance>" )
print( "Movement: f, u, d, b Turns: l, r" )
print( "Digging: df, du, dd")
print( "Place & move: pb, pu, pd")
print( "(i.e. place front, move backwards)")
print( "Move/place - above: pfu below: pfd")
return
end
local tMoveHandlers = {
["f"] = turtle.forward,
["b"] = turtle.back,
["u"] = turtle.up,
["d"] = turtle.down,
["l"] = turtle.turnLeft,
["r"] = turtle.turnRight,
["df"] = turtle.forward,
["du"] = turtle.up,
["dd"] = turtle.down,
["fpd"] = turtle.forward,
["fpu"] = turtle.forward,
["bpd"] = turtle.back,
["bpf"] = turtle.back,
["bpu"] = turtle.back,
["upf"] = turtle.up,
["upd"] = turtle.up,
["pd"] = turtle.down,
["pfu"] = turtle.forward,
["pfd"] = turtle.forward,
}
local tPreHandler = {
["df"] = turtle.dig,
["du"] = turtle.digUp,
["dd"] = turtle.digDown,
}
local tPostHandler = {
["fpd"] = turtle.placeDown,
["bpd"] = turtle.placeDown,
["upd"] = turtle.placeDown,
["pd"] = turtle.placeDown,
["pfd"] = turtle.place,
["bpf"] = turtle.place,
["upf"] = turtle.place,
["fpu"] = turtle.placeUp,
["bpu"] = turtle.placeUp,
["pfu"] = turtle.placeUp,
}
function doLoop(count, argStart)
for i = 1, count do
nArg = argStart
doAction()
end
end
function doAction()
while nArg <= #tArgs do
local sDirection = tArgs[nArg]
local nDistance = 1
if nArg < #tArgs then
local num = tonumber( tArgs[nArg + 1] )
if num then
nDistance = num
nArg = nArg + 1
end
end
nArg = nArg + 1
if (sDirection == "loop") then
doLoop(nDistance, nArg)
elseif (sDirection == "end") then
return
end
local fnHandler = tMoveHandlers[string.lower(sDirection)]
if fnHandler then
local fnPreHandler = tPreHandler[string.lower(sDirection)]
local fnPostHandler = tPostHandler[string.lower(sDirection)]
for n=1,nDistance do
if (fnPreHandler) then
fnPreHandler()
end
fnHandler( nArg )
if (fnPostHandler) then
fnPostHandler()
end
end
end
end
end
doAction()
State API
I realize there's some other state Apis - I've been enjoying making my own libraries - I'm sharing them here mostly so the other programs I post will work
Spoiler
local statePath = "/state/"
if not fs.exists(statePath) then
fs.makeDir(statePath)
end
function init(module, variable, defaultValue)
fileName = statePath .. module .. "/" .. variable
if not fs.exists(statePath .. module) then
fs.makeDir(statePath .. module)
end
content = defaultValue
if (fs.exists(fileName)) then
content = get(module, variable)
else
set(module, variable, defaultValue)
end
return content
end
function set(module, variable, value)
fileName = statePath .. module .. "/" .. variable
file = io.open(fileName, "w")
if (file) then
file:write(value)
file:close()
else
end
end
function get(module, variable)
fileName = statePath .. module .. "/" .. variable
content = nil
if (fs.exists(fileName)) then
file = io.open(fileName, "r")
content = file:read()
file:close()
end
return content
end
function clear(module, variable)
fileName = statePath .. module .. "/" .. variable
if (fs.exists(fileName)) then
fs.delete(fileName)
end
end
tMove API
My personal movement library
Spoiler
os.loadAPI("rom/apis/state")
local module = "tMove"
local xPos = tonumber(state.init(module, "xPos", 0))
local yPos = tonumber(state.init(module, "yPos", 0))
local height = tonumber(state.init(module, "height", 0))
local xDir = tonumber(state.init(module, "xDir", 0))
local yDir = tonumber(state.init(module, "yDir", 1))
local function _setPos(x, y, h, xD, yD)
xPos = x
yPos = y
height = h
xDir = xD
yDir = yD
state.set(module, "xPos", xPos)
state.set(module, "yPos", yPos)
state.set(module, "height", height)
state.set(module, "xDir", xDir)
state.set(module, "yDir", yDir)
end
function setPos(x, y, h, xD, yD)
_setPos(x or 0, y or 0, h or 0, xD or 0, yD or 1)
end
function getPos()
return xPos, yPos, height, xDir, yDir
end
function turnLeft()
turtle.turnLeft()
xDir, yDir = -yDir, xDir
state.set(module, "xDir", xDir)
state.set(module, "yDir", yDir)
end
function turnRight()
turtle.turnRight()
xDir, yDir = yDir, -xDir
state.set(module, "xDir", xDir)
state.set(module, "yDir", yDir)
end
function setFace(tXDir, tYDir)
while ((tXDir ~= xDir) or (tYDir ~= yDir)) do
if ((xDir == tYDir) and (yDir == -tXDir)) then
turnLeft()
else
turnRight()
end
end
end
function forwardOne()
local success = turtle.forward()
if (success) then
xPos = xPos + xDir
yPos = yPos + yDir
state.set(module, "xPos", xPos)
state.set(module, "yPos", yPos)
end
return success
end
function forward(count)
while (count > 0) do
if (forwardOne()) then
count = count - 1
end
end
end
function upOne()
local success = turtle.up()
if (success) then
height = height + 1
state.set(module, "height", height)
end
return success
end
function up(count)
while (count > 0) do
if (upOne()) then
count = count - 1
end
end
end
function downOne()
local success = turtle.down()
if (success) then
height = height - 1
state.set(module, "height", height)
end
return success
end
function down(count)
while (count > 0) do
if (downOne()) then
count = count - 1
end
end
end
function heightTo(tHeight)
dist = math.abs(tHeight - height)
dir = (tHeight - height) / dist
if (dir == 1) then
up(dist)
else
down(dist)
end
end
-- This function should move the turtle to the specified coordinates
-- xPrimary is a true/false value which should represent which axis to move along first.
-- If true, x Axis will be used first, otherwise Y
function to(tX, tY, xPrimary)
local dist = 0
if (xPrimary and (tX ~= xPos)) then
dist = math.abs(tX - xPos)
dir = (tX - xPos) / dist
setFace(dir, 0)
forward(dist)
end
if (tY ~= yPos) then
dist = math.abs(tY - yPos)
dir = (tY - yPos) / dist
setFace(0, dir)
forward(dist)
end
if (tX ~= xPos) then
dist = math.abs(tX - xPos)
dir = (tX - xPos) / dist
setFace(dir, 0)
forward(dist)
end
end
function displayPos()
print("X: " .. xPos .. " Y: " .. yPos .. " H: " .. height)
print("XDir: " .. xDir .. " YDir: " .. yDir)
end
Pave
I've used this to make floors of a tower, and then go back over it and place torches.
It operates left to right - in the direction it is facing to start. It does not handle state changes (reboots, etc)
Spoiler
Frame Eater
This will 'eat' a buildcraft frame. (After it's done) Dig out the bottom left corner of the frame and place the turtle there. As long as there are no oddities in the frame (blocks placed within the frame area, etc), it will learn how large the frame is and dismantle it for you
Spoiler
os.loadAPI("rom/apis/turtle/tmove")
local module = "frameEater"
tMove.setPos(0, 0, 0)
local xPos, yPos, height, xDir, yDir = 0, 0, 0 , 0, 0
local stage = 1
local maxX = 0
local maxY = 0
local maxH = 0
local tArgs = { ... }
-- Stage 4
local function home()
print("Going home")
tMove.heightTo(0)
tMove.to(0, 0, true)
tMove.heightTo(0)
tMove.setFace(0, 1)
end
local function getPos()
xPos, yPos, height, xDir, yDir = tMove.getPos()
end
local function mineToNextVertical()
repeat
turtle.dig()
tMove.forward(1)
getPos()
if (turtle.getItemCount(9) == 64) then
return false
end
until turtle.detectUp()
return true
end
local function mineUpToHorizontal()
repeat
turtle.digUp()
tMove.up(1)
getPos()
if (turtle.getItemCount(9) == 64) then
return false
end
until turtle.detect()
return true
end
local function digUp(count)
while (count > 0) do
turtle.digUp()
tMove.up(1)
count = count - 1
getPos()
if (turtle.getItemCount(9) == 64) then
return false
end
end
return true
end
local function digForward(count)
while (count > 0) do
turtle.dig()
tMove.forward(1)
count = count - 1
getPos()
if (turtle.getItemCount(9) == 64) then
return false
end
end
return true
end
while ((stage > 0) and (stage <= 12)) do
print("Main loop - stage: " .. stage)
state.set(module, "stage", stage)
if (stage == 1) then
-- Mine to the first vertical
if (mineToNextVertical()) then
maxY = yPos
tMove.turnRight()
stage = stage + 1
else
return
end
elseif (stage == 2) then
if (mineUpToHorizontal()) then
maxH = height
tMove.heightTo(0)
stage = stage + 1
else
return
end
elseif (stage == 3) then
-- Mine to the first vertical
if (mineToNextVertical()) then
maxX = xPos
tMove.turnRight()
stage = stage + 1
else
return
end
elseif ((stage == 4) or (stage == 6)) then
if (digUp(maxH)) then
tMove.heightTo(0)
stage = stage + 1
else
return
end
elseif (stage == 8) then
if (digUp(maxH)) then
stage = stage + 1
else
return
end
elseif ((stage == 5) or (stage == 9) or (stage == 11)) then
if (digForward(maxY)) then
tMove.turnRight()
stage = stage + 1
else
return
end
elseif ((stage == 7) or (stage == 10) or (stage == 12)) then
if (digForward(maxX)) then
tMove.turnRight()
stage = stage + 1
else
return
end
end
end
home()