--[[
Attempt at multifunctional program to perform tasks in a grid.
]]--

local args = { ... }
if #args == 0 then
	print( "Usage: grid <task> <dimensions> [intervals]" )
	return
end

function place_doubledown()
    tx.down()
    tx.digDown()
    tx.select(2)
    tx.placeDown()
    tx.up()
    tx.select(1)
    tx.placeDown()
end

function clear()
    tx.digUp()
    tx.digDown()
end

function prep()
    tx.digUp()
    tx.digDown()
    tx.select(1)
    tx.placeDown()
end

function lava_fuel()
    tx.place()
    tx.refuel(64)
    tx.placeDown()
    tx.refuel(64)
    print(turtle.getFuelLevel())
end

function placedown()
    while tx.getItemCount(tx.state.selected) == 0 do
        tx.select(tx.state.selected + 1)
    end
    tx.placeDown()
end

function replacedown()
    tx.select(9)
    tx.digDown()
    tx.select(1)
    while tx.getItemCount(tx.state.selected) == 0 do
        tx.select(tx.state.selected + 1)
    end
    tx.placeDown()
end

command = args[1]
gx = nil
gy = nil
gz = nil
dx = nil
dy = nil
dz = nil

commands = {
    ['clear']       = {clear, nil, true, 'dig'},
    ['placedown']   = {placedown, nil, true, 'dig'},
    ['placeup']     = {tx.placeUp, nil, true, 'dig'},
    ['replacedown'] = {replacedown, nil, true, 'dig'},
    ['doubledown']  = {place_doubledown, nil, true, 'dig'},
    ['lavarefuel']  = {nil, lava_fuel, true, 'dig'},
}



function split(s)
    local t = {}
    for n in s:gmatch('[-%d]+') do
        table.insert(t,tonumber(n))
    end
    return t
end

if commands[command] == nil then
    error('unknown command given')
end

hook = commands[command][1]
prefw_hook = commands[command][2]
go_home = commands[command][3]
tx.state.move_mode = commands[command][4]

g = split(args[2])
gx = g[1] 
gy = g[2] 
gz = g[3]          
if args[3] then
    d = split(args[3])
    dx = d[1] 
    dy = d[2] 
    dz = d[3]    
end 
tx.placemark('start')
tx.select(1)
tx.grid_move{gx=gx, gy=gy, gz=gz, dx=dx, dy=dy, dz=dz, hook=hook, prefw_hook=prefw_hook}
if go_home then
    print('going back to start')
    tx.goto_placemark('start')
end

--if task == 'clear' then
--    print('clearing ...')
--    hook = clear
--    go_home = true
--    tx.state.move_mode = 'dig'
--    g = split(args[2])
--    gx = g[1] 
--    gy = g[2] 
--    gz = g[3]          
--    if args[3] then
--        d = split(args[3])
--        dx = d[1] 
--        dy = d[2] 
--        dz = d[3]    
--    end 
--    tx.placemark('start')
--    tx.grid_move{gx=gx, gy=gy, gz=gz, dx=dx, dy=dy, dz=dz, hook=hook}
--    print('going back to start')
--    tx.goto_placemark('start')
--elseif task == 'placeup' then
--    print('placing up ...')
--    hook = tx.placeUp
--    go_home = true
--    tx.state.move_mode = 'dig'
--    g = split(args[2])
--    gx = g[1] 
--    gy = g[2] 
--    gz = g[3]     
--    d = split(args[3])
--    dx = d[1] 
--    dy = d[2] 
--    dz = d[3]     
--    tx.placemark('start')
--    tx.grid_move{gx=gx, gy=gy, dx=dx, dy=dy, hook=hook}
--    print('going back to start')
--    tx.goto_placemark('start')
--elseif task == 'placedown' then
--    print('placing down ...')
--    hook = tx.placeDown
--    go_home = true
--    tx.state.move_mode = 'dig'
--    g = split(args[2])
--    gx = g[1] 
--    gy = g[2] 
--    gz = g[3]     
--    if args[3] then
--        d = split(args[3])
--        dx = d[1] 
--        dy = d[2] 
--        dz = d[3]    
--    end 
--    tx.placemark('start')
--    tx.grid_move{gx=gx, gy=gy, dx=dx, dy=dy, hook=hook}
--    print('going back to start')
--    tx.goto_placemark('start')
--elseif task == 'doubledown' then
--    print('doubledown ...')
--    hook = place_doubledown
--    go_home = true
--    tx.state.move_mode = 'dig'
--    g = split(args[2])
--    gx = g[1] 
--    gy = g[2] 
--    gz = g[3]     
--    if args[3] then
--        d = split(args[3])
--        dx = d[1] 
--        dy = d[2] 
--        dz = d[3]    
--    end 
--    tx.placemark('start')
--    tx.grid_move{gx=gx, gy=gy, dx=dx, dy=dy, hook=hook}
--    print('going back to start')
--    tx.goto_placemark('start')
--end
