Skip to content

Instantly share code, notes, and snippets.

@SquidLord
Last active February 6, 2016 18:16
Show Gist options
  • Save SquidLord/4758568 to your computer and use it in GitHub Desktop.
Save SquidLord/4758568 to your computer and use it in GitHub Desktop.
linePlace: ComputerCraft turtle program to place items in inventory down or forward until you can proceed no further, then return to start
-- linePlace: ComputerCraft turtle program to place items in inventory down or forward
-- until you can proceed no further, then return to start.
-- Arguments are "f" for place forward, or "d" for place down
-- by Alexander "SquidLord" Williams (SaladinVrai)
-- Gist: https://gist.github.com/SquidLord/4758568
-- The MIT License (MIT)
-- Copyright (c) 2012 Alexander Williams
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
-- The above copyright notice and this permission notice shall be included in all
-- copies or substantial portions of the Software.
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
-- Requires squidlib and egps
os.loadAPI("squid/squidlib")
os.loadAPI("squid/egps")
ARGS = {...}
function usage()
squidlib.printTable({
"SquidOS LinePlace Program ...",
"",
" Place items in inventory down or forward until you can proceed no further,",
" then return to start..",
"",
" Arguments:",
" f - Place forward",
" d - Place down"})
return nil
end
-- placeItem(Slot, PlaceFunc) => Int/Bool
-- Takes the current inv slot and the function to place in the right direction
-- Returns false if out of inventory in all slots or an Int for the slot with materials in it
function placeItem(Slot, PlaceFunc)
if (Slot == 17) then
print(" Out of inventory")
return false
elseif (turtle.getItemCount(Slot) == 0) then -- There are slots left and we're out in this slot
return placeItem(Slot+1, PlaceFunc) -- Call yourself with the next slot selected
elseif not PlaceFunc() then -- there are items left in inv to place so if you STILL can't place the item
print(" Cannot place item")
return false -- Cannot continue; fail out
else
return Slot -- Return the slot number
end
end
-- scanPlaceMoveR(Func, Func, Func) => Bool
-- Take the Scan, Place, and Move functions and recourse until something throws false
-- Technically, this should always return false ... eventually
function scanMovePlaceR(ScanFunc, MoveFunc, PlaceFunc)
if ScanFunc() then -- If there's something in the way
print(" Way obstructed")
return false
elseif not MoveFunc() then -- If you can't move
print(" Cannot move onwards")
return false
elseif not PlaceFunc() then -- If you can't place the item it's already emitted an error
return false
else
scanMovePlaceR(ScanFunc, MoveFunc, PlaceFunc)
end
end
-- Main code branch
function main()
if not ARGS[1] then
return usage()
elseif not ((ARGS[1] == "f") or (ARGS[1] == "d")) then
return usage()
else -- Everything is groovy
Slot = 1 -- The current inv slot to start from
Loc = {} -- Put starting location in scope
MoveFunc = false -- Put direction of motion in scope
PlaceFunc = false -- Put direction to drill in scope
ScanFunc = false -- Put direction to look in scope
Loc.x, Loc.y, Loc.z, Loc.f = egps.setLocationFromGPS() -- Save the current location to return to
-- Identify
squidlib.printTable({
"SquidOS linePlace Program ...",
""})
if (ARGS[1] == "f") then -- Placing forward?
egps.forward()
MoveFunc = function()
return egps.forward()
end
PlaceFunc =
function() -- Must turn around to place things behind us
egps.turnLeft()
egps.turnLeft()
Slot = placeItem(Slot, turtle.place)
if not Slot then -- If you can't put it down, something is wrong
return false
end
egps.turnLeft() -- And turn back to the front
egps.turnLeft()
return true
end
ScanFunc = turtle.detect
print(" Placing forward")
else -- Place down
egps.down()
MoveFunc = egps.down
PlaceFunc =
function()
Slot = placeItem(Slot, turtle.placeUp)
if not Slot then -- If you can't put it down, something is wrong
return false
else
return true
end
end
ScanFunc = turtle.detectDown
print(" Placing down")
end
scanMovePlaceR(ScanFunc, MoveFunc, PlaceFunc)
print(" Nothing more to place; RTB")
egps.moveTo(Loc.x, Loc.y, Loc.z, Loc.f) -- Returning to start
print("Done")
end
end
---
--- Main
---
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment