Skip to content

Instantly share code, notes, and snippets.

@znepb
Last active January 16, 2023 07:14
Show Gist options
  • Save znepb/72981ce3e7a2e1f4e3423ed0b2ca9510 to your computer and use it in GitHub Desktop.
Save znepb/72981ce3e7a2e1f4e3423ed0b2ca9510 to your computer and use it in GitHub Desktop.
Tuttle: A simple ComputerCraft turtle pathfinding API.
-- A simple turtle pathfinding API for ComputerCraft.
--[[
Copyright 2023 Marcus Wenzel
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.
]]
local api = {}
api.PICKAXE = "minecraft:diamond_pickaxe"
api.BLOCK_SCANNER = "plethora:module_scanner"
api.ENTITY_SENSOR = "plethora:module_sensor"
api.heading = nil
local function doTimes(f, times)
for i = 1, times do
f()
end
end
--- Equips an item
-- @tparam item string The item to add to equip. Some constants are defined in this API, see lines 3-5.
function api.equip(item)
for i = 1, 16 do
if turtle.getItemDetail(i) and turtle.getItemDetail(i).name == item then
turtle.select(i)
turtle.equipLeft()
return true
end
end
return false
end
--- Selects an item
-- @tparam item string The item ID to select.
function api.select(item)
for i = 1, 16 do
if turtle.getItemDetail(i) and turtle.getItemDetail(i).name == item then
turtle.select(i)
return true, i
end
end
return false
end
--- Scans for blocks
-- @returns table A table containing all scanned blocks.
function api.scan()
api.equip(api.BLOCK_SCANNER)
return peripheral.call("left", "scan")
end
--- Sense for entities
-- @returns table A table containing all scanned entities.
function api.sense()
api.equip(api.ENTITY_SENSOR)
return peripheral.call("left", "sense")
end
--- Digs in a direction
-- @tparam string direction Down, up, or a cardinal direction, this defines what direction the turtle will move in.
-- @tparam[opt] int times The amount of blocks to dig
function api.dig(direction, times)
api.equip(api.PICKAXE)
if direction == "down" then
doTimes(turtle.digDown, times or 1)
return
elseif direction == "up" then
doTimes(turtle.digUp, times or 1)
return
end
doTimes(turtle.dig, times or 1)
end
--- Gets the turtle's current heading
-- @returns string A cardinal direction defining the direction the turtle is currently heading in.
function api.getHeading()
if api.heading then return api.heading end
local blocks = api.scan()
for i, v in pairs(blocks) do
if v.x == 0 and v.y == 0 and v.z == 0 then
api.heading = v.state.facing
return v.state.facing
end
end
end
local headingVariables = {north = 1, east = 2, south = 3, west = 4}
--- Set the turtle's heading
-- @tparam to string The direction to set heading to.
-- @tparam[opt] loose boolean If set to true, `true` will be returned if the target direction is opposite the current direction, signaling to move backwards
function api.setHeading(to, loose)
local currentHeading = api.getHeading()
if currentHeading == to then return end
local headingDifference = headingVariables[currentHeading] - headingVariables[to]
-- Same heading
if headingDifference == 0 then return
-- Opposite heading
elseif math.abs(headingDifference) == 2 then
if loose then return true end
turtle.turnRight()
turtle.turnRight()
-- left heading
elseif headingDifference == 1 or headingDifference == -3 then
turtle.turnLeft()
-- Right heading
elseif headingDifference == -1 or headingDifference == 3 then
turtle.turnRight()
end
api.heading = to
end
--- Gets directions relative to the turtle's position.
-- @tparam tX number The target X position
-- @tparam tY number The target Y position
-- @tparam tZ number The target Z position
function api.getDirectionsRelative(tX, tY, tZ)
local x, y, z = nil, nil, nil
if tX < 0 then
x = "west"
elseif tX > 0 then
x = "east"
end
if tY > 0 then
y = "up"
elseif tY < 0 then
y = "down"
end
if tZ < 0 then
z = "north"
elseif tZ > 0 then
z = "south"
end
return x, y, z
end
--- Gets directions to a world position from the turtle's position.
-- @tparam tX number The target X position
-- @tparam tY number The target Y position
-- @tparam tZ number The target Z position
function api.getDirections(tX, tY, tZ)
local cX, cY, cZ = gps.locate()
return api.getDirectionsRelative(tX - cX, tY - cY, tZ - cZ)
end
--- Move in a direction
-- @tparam dir string The direction to move in.
-- @tparam[opt] times int The amount of times to move.
-- @tparam[opt] loose boolean If not set, turtles will always face the direction they are moving in.
function api.move(dir, times, loose)
if dir == "up" then
for i = 1, math.abs(times) do
api.dig("up")
turtle.up()
end
return
elseif dir == "down" then
for i = 1, math.abs(times) do
api.dig("down")
turtle.down()
end
return
end
local useBack = api.setHeading(dir, loose)
if loose and useBack then
for i = 1, math.abs(times) do
turtle.back()
end
else
for i = 1, math.abs(times) do
api.dig()
turtle.forward()
end
end
end
--- Goes to absolute coordinates in the world.
-- @tparam tX number The target X position
-- @tparam tY number The target Y position
-- @tparam tZ number The target Z position
function api.goToCoordinates(tX, tY, tZ, loose)
local cX, cY, cZ = gps.locate()
local dX, dY, dZ = tX - cX, tY - cY, tZ - cZ
local hX, hY, hZ = api.getDirections(tX, tY, tZ)
if hX then
api.move(hX, math.abs(dX), loose)
end
if hZ then
api.move(hZ, math.abs(dZ), loose)
end
if hY then
api.move(hY, math.abs(dY), loose)
end
end
return api
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment