Skip to content

Instantly share code, notes, and snippets.

@SquidLord
Last active February 6, 2016 18:16
Show Gist options
  • Save SquidLord/4755840 to your computer and use it in GitHub Desktop.
Save SquidLord/4755840 to your computer and use it in GitHub Desktop.
turtleLib: A core set of tool libraries for ComputerCraft turtles
-- turtleLib: A core set of tool libraries for ComputerCraft turtles
-- by Alexander "SquidLord" Williams (SaladinVrai)
-- Gist: https://gist.github.com/SquidLord/4755840
-- Put in turtle startup file with:
-- os.loadAPI("squid/turtleLib")
-- 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.
-- goodStr
-- String containing list of all good characters for directions
goodStr = "fblrudLR"
-- makeMoveTable(Lib) => Table
-- takes a Lib (egps, turtle) and builds a map of String->move function associations
function makeMoveTable(Lib)
MT = {}
MT.f = Lib.forward
MT.b = Lib.back
MT.l = Lib.turnLeft
MT.r = Lib.turnRight
MT.u = Lib.up
MT.d = Lib.down
MT.L = function()
Lib.turnLeft()
Lib.forward()
Lib.turnRight()
end
MT.R = function()
Lib.turnRight()
Lib.forward()
Lib.turnLeft()
end
return MT
end
-- StrToDirection(Cha, MoveTable) => Func
-- Takes a single-character string and returns the turtle
-- movement function associated with that direction
function chaToDirection(Cha, MoveTable)
if not (string.find(goodStr, Cha)) then
return false
else
return MoveTable[Cha]
end
end
-- stringToMoves(String, Table, [Table]): => Table
-- Takes a string, a generated MoveTable, and an optional table in progress
-- Returns an ordered list of function calls
function stringToMoves(Str, MoveTable, OutTable)
if (OutTable == nil) then -- If this is the first call of the func, init OutTable
OutTable = {}
end
if (Str == "") then -- If there are no more cha's to process, return
return OutTable
else
table.insert(OutTable, MoveTable[string.sub(Str, 1, 1)])
return stringToMoves(string.sub(Str, 2),
MoveTable,
OutTable)
end
end
-- executeMoveTable(Table) => Bool
-- Executes a series of turtle moves as given in an ordered Table
function executeMoves(MoveTable)
for _,Move in ipairs(MoveTable) do
Move()
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment