turtle-tracker v1.0.0

A turtle API client module for ComputerCraft that provides easy integration with the krawlet-api turtle tracking system. Features: Automatic stat tracking, position reporting, configurable API endpoint, periodic sync support, and simple state management for turtle data.

Installation

⚠️ Dependencies: This library requires: turtleApi
Using installer will automatically install all dependencies.

Recommended: Install via installer (handles dependencies automatically):

wget run https://raw.githubusercontent.com/Twijn/cc-misc/main/util/installer.lua turtle-tracker
View on GitHub →

This pattern downloads and runs libraries at runtime, automatically installing any that are missing:

-- Auto-install and require libraries
local libs = {"turtle-tracker"} -- Add more libraries as needed
local libDir = (fs.exists("disk") and "disk/lib/" or "/lib/")
local allExist = true

for _, lib in ipairs(libs) do
    if not fs.exists(libDir .. lib .. ".lua") then
        allExist = false
        break
    end
end

if not allExist then
    shell.run("wget", "run", "https://raw.githubusercontent.com/Twijn/cc-misc/main/util/installer.lua", table.unpack(libs))
end

local turtle-tracker = require(libDir .. "turtle-tracker")

-- Use the library
-- (your code here)

Examples

Using with Runtime Installation

This example shows how to download and use libraries with automatic installation:

-- Auto-install and require libraries
local libs = {"turtle-tracker"} -- Add more libraries as needed
local libDir = (fs.exists("disk") and "disk/lib/" or "/lib/")
local allExist = true

for _, lib in ipairs(libs) do
    if not fs.exists(libDir .. lib .. ".lua") then
        allExist = false
        break
    end
end

if not allExist then
    shell.run("wget", "run", "https://raw.githubusercontent.com/Twijn/cc-misc/main/util/installer.lua", table.unpack(libs))
end

local turtle-tracker = require(libDir .. "turtle-tracker")

-- Use the library

Usage Examples

local turtleApi = require("turtleApi")
-- Configure the API endpoint
turtleApi.setEndpoint("http://localhost:3000")
-- Initialize with turtle ID (defaults to os.getComputerID())
turtleApi.init()
-- Update stats
turtleApi.incrementStat("blocks_mined")
turtleApi.incrementStat("debris_found", 5)
-- Update position
turtleApi.setAbsolutePosition(100, 64, -200)
turtleApi.setRelativePosition(5, 0, 10)
-- Push data to server
turtleApi.sync()
-- Or use auto-sync (syncs every N seconds)
turtleApi.startAutoSync(30)

Functions

module.setEndpoint(endpoint)

View source on GitHub

Set the API endpoint URL

Parameters:

module.getEndpoint()

View source on GitHub

Get the current API endpoint

Returns: string # The current API endpoint URL

module.init(id, label)

View source on GitHub

Initialize the turtle API client

Parameters:

module.setDefault(stats)

View source on GitHub

Set default stats (similar to CC state.setDefault pattern)

Parameters:

module.getStats()

View source on GitHub

Get the current stats

Returns: TurtleStats # Current stats table

module.getStat(statName)

View source on GitHub

Get a specific stat value

Parameters:
Returns: number # The stat value

module.setStat(statName, value)

View source on GitHub

Set a specific stat value

Parameters:

module.incrementStat(statName, amount)

View source on GitHub

Increment a stat by a value (default 1)

Parameters:

module.decrementStat(statName, amount)

View source on GitHub

Decrement a stat by a value (default 1)

Parameters:

module.setAbsolutePosition(x, y, z)

View source on GitHub

Set the absolute position (world coordinates)

Parameters:

module.getAbsolutePosition()

View source on GitHub

Get the absolute position

Returns: TurtlePosition # Absolute position table

module.setRelativePosition(x, y, z)

View source on GitHub

Set the relative position (from starting point)

Parameters:

module.getRelativePosition()

View source on GitHub

Get the relative position

Returns: TurtlePosition # Relative position table

module.moveRelative(dx, dy, dz)

View source on GitHub

Update relative position by offset

Parameters:

module.moveAbsolute(dx, dy, dz)

View source on GitHub

Update absolute position by offset

Parameters:

module.setLabel(label)

View source on GitHub

Set the turtle label

Parameters:

module.getLabel()

View source on GitHub

Get the turtle label

Returns: string|nil # The turtle label

module.updateFuel()

View source on GitHub

Update fuel level from turtle API

module.setFuel(fuel)

View source on GitHub

Set fuel level manually

Parameters:

module.getFuel()

View source on GitHub

Get the current fuel level

Returns: number|nil # The fuel level

module.buildPayload()

View source on GitHub

Build the full data payload for syncing

Returns: table # The data payload

module.sync()

View source on GitHub

Sync all turtle data to the server

Returns: string|nil # Error message if sync failed

module.syncStats()

View source on GitHub

Sync only stats to the server

Returns: string|nil # Error message if sync failed

module.syncPosition()

View source on GitHub

Sync only position to the server

Returns: string|nil # Error message if sync failed

module.fetch()

View source on GitHub

Fetch turtle data from the server

Returns: table|nil # The turtle data or nil if not found

module.fetchAll()

View source on GitHub

Fetch all turtles from the server

Returns: table|nil # Array of turtle data or nil on error

module.delete()

View source on GitHub

Delete the current turtle from the server

Returns: string|nil # Error message if deletion failed

module.deleteById(id)

View source on GitHub

Delete a specific turtle by ID from the server

Parameters:
Returns: string|nil # Error message if deletion failed

module.startAutoSync(intervalSeconds)

View source on GitHub

Start automatic syncing at a given interval

Parameters:

module.stopAutoSync()

View source on GitHub

Stop automatic syncing

module.isAutoSyncRunning()

View source on GitHub

Check if auto-sync is running

Returns: boolean # True if auto-sync is active

module.forward()

View source on GitHub

Wrapper for turtle.forward() that updates relative position

Returns: boolean # True if movement succeeded

module.back()

View source on GitHub

Wrapper for turtle.back() that updates relative position

Returns: boolean # True if movement succeeded

module.up()

View source on GitHub

Wrapper for turtle.up() that updates relative position

Returns: boolean # True if movement succeeded

module.down()

View source on GitHub

Wrapper for turtle.down() that updates relative position

Returns: boolean # True if movement succeeded

module.dig()

View source on GitHub

Wrapper for turtle.dig() that increments blocks_mined stat

Returns: boolean # True if dig succeeded

module.digUp()

View source on GitHub

Wrapper for turtle.digUp() that increments blocks_mined stat

Returns: boolean # True if dig succeeded

module.digDown()

View source on GitHub

Wrapper for turtle.digDown() that increments blocks_mined stat

Returns: boolean # True if dig succeeded

module._debug()

View source on GitHub

Print debug information about the current state