This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
cryzed's profile picture

Slightly improved Turtle API

Started by cryzed, 19 November 2012 - 07:47 AM
cryzed #1
Posted 19 November 2012 - 08:47 AM
Hey there, I stumbled across ComputerCraft today thanks to a friend of mine and started playing around with it. I thought that the native turtle API was quite cumbersome and started implementing some helper functions that ease the general use of ComputerCraft. The code/"API" can be found here: https://gist.github.com/4107076. If there are questions regarding the code ask them and I'll try my best to answer them.

My code implements the following:
  • _set_default_table_value(table, default): Allows to change a Lua table's metatable quickly so that "default"-values can be set
  • get_input(prompt): Just a more comfortable way to get certain user input.
  • rotate(degrees): Degrees can be positive and negative, 90° will cause the turtle to turn right, -90° left etc.
  • _on_move_fail(direction, move): Callback used by the move function. If your version of this callback does not return true the move function returns.
  • _on_move_success(direction, move): Callback used by the move function.
  • move(direction, blocks, on_success, on_fail): Allows the turtle to move forward, back, up, down, left and right – For left and right the orientation is automatically adjusted. For each traversed block the on_success callback (defaults to _on_move_success) is called, in case the turtle can't traverse a block the on_fail callback (defaults to _on_move_fail) is called.
  • _on_dig_fail(direction, dug): Callback used by the dig function. If your version of this callback returns true the move function returns.
  • _on_dig_success(direction, dug): Callback used by the dig function.
  • dig(direction, blocks, move_to_last_block, on_success, on_fail): Allows the turtle to dig forward, back, up, down, left and right – For left and right the orientation is automatically adjusted. For each dug block the on_success callback (defaults to _on_dig_success) is called, in case the turtle can't dig a block the on_fail callback (defaults to _on_dig_fail) is called. move_to_last_block defaults to false and if set to true will cause the turtle to not only dig the last block in the given direction but also move to it afterwards.
  • refuel(slot, quantity): Uses an item in the given slot and refuels itself with the given quantity.
Maybe it's useful for some of you!
cryzed #2
Posted 20 November 2012 - 02:22 PM
os.loadAPI('cryzed')


function swap_value(value, x, y)
  if value == x then return y end
  return x
end

function main()
  local width = tonumber(cryzed.get_input('Width: ')) - 1
  local depth = tonumber(cryzed.get_input('Depth: '))
  local height = tonumber(cryzed.get_input('Height: '))

  local horizontal_direction = 'right'
  local vertical_direction = 'up'
  cryzed.dig('forward', 1, true)

  for y = 1, depth, 1 do
    for z = 1, height, 1 do
      cryzed.dig(horizontal_direction, width, true)
      if height - z > 0 then cryzed.dig(vertical_direction, 1, true) end
      if horizontal_direction == 'right' then cryzed.rotate(-90) else cryzed.rotate(90) end
      horizontal_direction = swap_value(horizontal_direction, 'left', 'right')
    end

    vertical_direction = swap_value(vertical_direction, 'up', 'down')
    if depth - y > 0 then cryzed.dig('forward', 1, true) end
  end
end


main()

This is a small example on how to use the improved "API" to easily dig out a box with configurable dimensions, starting at (0, 0, 0) and ending at (x, y, z). Next to no fuel is wasted due to the constantly changing directions (left, right, up down). I also changed the "API" code slightly, mainly to stop the annoying output from the default callbacks: https://gist.github.com/4107076.