Skip to content

Instantly share code, notes, and snippets.

@ckhrysze
Last active August 29, 2015 14:01
Show Gist options
  • Save ckhrysze/7682752ec562084bfac3 to your computer and use it in GitHub Desktop.
Save ckhrysze/7682752ec562084bfac3 to your computer and use it in GitHub Desktop.
Misc scripts for computercraft, installed with "pastebin get i7xwQWNU gist"
--
-- author: Ckhrysze
--
-- Assumptions
-- * turtle is already fueled up
function spiralIn(length, width, center)
while true do
if length > 1 or center then
replantLine(length)
turtle.turnRight()
end
length = length - 1
width = width - 1
if width == 0 then return end
replantLine(width)
turtle.turnRight()
if length == 0 then return end
end
end
function replantLine(blocks)
print("Planting "..blocks.." blocks.")
for i=1, blocks do
turtle.forward()
turtle.digDown()
turtle.placeDown()
end
end
function triggerLiquiCrafter(times)
for i=1,times do
rs.setOutput('bottom', true)
os.sleep(0.2)
rs.setOutput('bottom', false)
os.sleep(0.2)
end
-- make sure its done before returning
os.sleep(0.5)
end
function bog()
-- make the bog earth
turtle.down()
triggerLiquiCrafter(4)
turtle.suckDown()
turtle.up()
turtle.up()
spiralIn(5, 5, false)
-- return to starting point
turtle.turnLeft()
turtle.forward()
turtle.forward()
turtle.turnRight()
turtle.back()
turtle.back()
turtle.down()
end
for i=1,4 do
bog()
turtle.turnRight()
end
-- dump items, assuming an itemduct
turtle.down()
rs.setOutput('front', true)
while (
turtle.getItemCount(1) > 0 or
turtle.getItemCount(2) > 0 or
turtle.getItemCount(3) > 0 or
turtle.getItemCount(4) > 0
) do
os.sleep(0.5)
end
rs.setOutput('front', false)
turtle.up()
function build_column(blocks)
turtle.select(1)
for i=1, blocks do
turtle.up()
turtle.placeDown()
end
turtle.back()
for i=1, blocks do
turtle.down()
end
end
build_column(42)
function spiralIn(length, width, center)
while true do
if length > 1 or center then
harvestLine(length, length%2==0)
turtle.turnRight()
end
length = length - 1
width = width - 1
if width == 0 then return end
harvestLine(width, true)
turtle.turnRight()
if length == 0 then return end
end
end
function harvestLine(blocks, even)
for i=1, blocks do
turtle.forward()
if i%2 == 0 and even then
grabCactus()
elseif i%2 == 1 and not even then
grabCactus()
end
end
end
function grabCactus()
turtle.digDown()
turtle.down()
turtle.digDown()
turtle.up()
end
spiralIn(5, 5, true)
turtle.back()
turtle.back()
turtle.turnRight()
for i=1,3 do turtle.forward() end
function place_line(blocks)
for i=1, blocks do
for j=1, 16 do
if turtle.getItemCount(j) > 0 then
turtle.select(j)
break
end
end
turtle.placeDown()
turtle.forward()
end
end
function spiral_in(length, width, center)
while true do
if length > 1 or center then
place_line(length)
turtle.turnRight()
end
length = length - 1
width = width - 1
if width == 0 then return end
place_line(width)
turtle.turnRight()
if length == 0 then return end
end
end
args = {...}
width = tonumber(args[1])
depth = tonumber(args[2])
if not width or not depth then
print("Please provide the width and depth of the floor")
return
end
spiral_in(width, depth, true)
function list_files_at(gist_d)
JSON = (loadfile "JSON.lua")()
local gist_doc = http.get('https://api.github.com/gists/'..gist_id..'/files')
local gist_files = JSON:decode(gist_doc.readAll())
return gist_files.files
end
local cmds = {
update = function()
print("Updating")
local gist_files = list_files_at(gist_id)
local gist_script_url = gist_files['gist.lua'].raw_url
local gist_script = http.get(gist_script_url)
local gist_file = fs.open("/gist", "w")
gist_file.write("local gist_id = '"..gist_id.."'\n")
gist_file.write(gist_script.readAll())
gist_file.close()
end,
install = function(args)
name = args[2]
print("Installing "..name)
local gist_files = list_files_at(gist_id)
local script_url = gist_files[name..'.lua'].raw_url
local script = http.get(script_url)
local f = fs.open("/"..name, "w")
f.write(script.readAll())
f.close()
end
}
arg = {...}
if not arg[1] then
print("Must supply at least 1 argument")
return
end
local method_name = arg[1]
method = cmds[method_name]
if method then
method(arg)
else
print("No method " .. method_name)
end
function switchRow()
turtle.turnRight()
turtle.up()
for i=1,3 do turtle.forward() end
turtle.turnRight()
turtle.down()
end
function harvest(blocks)
for i=1, blocks do
turtle.dig()
turtle.forward()
end
switchRow()
for i=1, blocks do
turtle.dig()
turtle.forward()
end
switchRow()
end
args = {...}
if not args[1] then
print("Please provide the number of 4x4 wall segments to fill out")
return
elseif not tonumber(args[1]) then
print("The first argument should be a number")
return
end
blocks = tonumber(args[1])
harvest(blocks)
-- auther: Ckhrysze
--
-- assumptions:
-- - there are arg/7 torches in slot 1
-- - the turtle already has enough fuel
local function forciblyForward(attempts)
local dug = true
while dug do
dug = turtle.dig()
end
local struck = true
while struck do
struck = turtle.dig()
end
local moved = turtle.forward()
if not moved then
attempts = attempts or 1
attempts = attempts + 1
if attempts > 5 then
print("Cannot move forward")
else
forciblyForward(attempts)
end
end
end
local function mine(blocks)
for i=1,blocks do
forciblyForward()
turtle.digUp()
if i%7 == 0 then
turtle.select(1)
turtle.placeUp()
end
end
turtle.turnRight()
turtle.turnRight()
for i=1,blocks do
forciblyForward()
end
end
local args = { ... }
if not tonumber(args[1]) then
print("Please provide the number of blocks to move forward")
return
end
mine(tonumber(args[1]))
--
-- author: Ckhrysze
--
--[[
Helper functions
--]]
function fuelUp()
turtle.select(16)
if turtle.refuel(1) then
while turtle.getFuelLevel() < 5000 do
turtle.refuel(1)
end
return true
else
print("No fuel in slot 16")
return false
end
end
function digDown(depth)
for i=1,depth do
turtle.digDown()
if i ~= depth then turtle.down() end
end
end
function rise(height)
for i=1,height do
turtle.up()
end
end
function forward(distance)
for i=1, distance do
if turtle.detect() then turtle.dig() end
turtle.forward()
end
end
function left()
turtle.turnLeft()
end
function right()
turtle.turnRight()
end
function nextRow(func)
func()
forward(1)
func()
end
function corner(func)
turtle.forward()
nextRow(func)
end
function placeBlocks(distance)
for i=1, distance do
turtle.placeDown()
if turtle.getItemCount(3) > 0 and turtle.compareTo(3) then
turtle.select(3)
turtle.transferTo(1)
turtle.select(1)
elseif turtle.getItemCount(2) > 0 and turtle.compareTo(2) then
turtle.select(2)
turtle.transferTo(1)
turtle.select(1)
end
if i ~= distance then forward(1) end
end
end
function clearLand(distance, depth)
turtle.select(9)
for i=1, distance do
digDown(depth)
rise(depth-1)
if turtle.detect() then turtle.dig() end
if i ~= distance then forward(1) end
end
end
function countFloorBlocks()
if not turtle.getItemCount(1) == 64 then
print("slot 1 lacks resources")
error()
end
if not turtle.getItemCount(2) == 64 then
print("slot 2 lacks resources")
error()
end
if not turtle.getItemCount(3) > 28 then
print("slot 3 lacks resources")
error()
end
return true
end
--[[
Main functions
--]]
function clearForFarmBlocks()
-- move to the corner of the farm block area
forward(2)
nextRow(left)
clearLand(5, 4)
nextRow(left)
clearLand(5, 4)
nextRow(right)
clearLand(5, 4)
end
function clearForFarmLand()
forward(6)
turtle.turnRight()
---[[ clear the top triangle
for i=1,6 do
clearLand(i*2+1, 2)
if i % 2 == 1 then
corner(right)
else
corner(left)
end
end
--]]
for i=1,5 do
clearLand(6, 2)
forward(4)
clearLand(6, 2)
if i % 2 == 1 then
nextRow(right)
else
nextRow(left)
end
end
forward(1)
for i=6,1,-1 do
clearLand(i*2+1, 2)
if i % 2 == 1 then
nextRow(right)
else
nextRow(left)
end
forward(1)
end
end
function placeFarmLandBlocks()
-- if not pcall(countFloorBlocks) then
-- print("Need 64, 64, 28+ blocks in slots 1, 2, 3")
-- error()
-- end
corner(right)
turtle.down()
turtle.select(1)
for i=1,6 do
placeBlocks(i*2+1)
if i % 2 == 1 then
corner(left)
else
corner(right)
end
end
for i=1,5 do
placeBlocks(6)
forward(4)
placeBlocks(6)
if i % 2 == 1 then
nextRow(left)
else
nextRow(right)
end
end
forward(1)
for i=6,1,-1 do
placeBlocks(i*2+1)
if i % 2 == 1 then
nextRow(left)
else
nextRow(right)
end
forward(1)
end
end
function placeFarmBlocks()
end
function makeFarm()
if not fuelUp() then return end
-- clearForFarmBlocks()
-- fuelUp()
-- clearForFarmLand()
-- fuelUp()
placeFarmLandBlocks()
fuelUp()
placeFarmBlocks()
end
makeFarm()
arg = {...}
if not arg[1] then
print("Must supply a gist id")
return
end
gist_id = arg[1]
print("Setup using gist repo: "..gist_id)
local json_file = fs.open("/JSON.lua", "w")
local json_lib = http.get("http://pastebin.com/raw.php?i=3ABWgBMy")
json_file.write(json_lib.readAll())
json_file.close()
JSON = (loadfile "JSON.lua")()
local gist_doc = http.get('https://api.github.com/gists/'..gist_id..'/files')
local gist_files = JSON:decode(gist_doc.readAll())
print("Hello")
print(gist_files)
local gist_script_url = gist_files.files['gist.lua'].raw_url
print(gist_script_url)
local gist_script = http.get(gist_script_url)
local gist_file = fs.open("/gist", "w")
gist_file.write("local gist_id = '"..gist_id.."'\n")
gist_file.write(gist_script.readAll())
gist_file.close()
function left()
turtle.turnLeft()
end
function right()
turtle.turnRight()
end
function uturn(func)
func()
harvest_row(1)
func()
end
function harvest_row(blocks)
for i=1, blocks do
turtle.dig()
turtle.forward()
turtle.digDown()
end
end
function move(n, func)
for i=1, n do
turtle[func]()
end
end
-- get into position
move(3, 'up')
left()
move(5, 'forward')
-- harvest
harvest_row(10)
uturn(right)
harvest_row(10)
uturn(left)
harvest_row(10)
uturn(right)
harvest_row(10)
uturn(left)
harvest_row(10)
-- return to beginning and drop off the goods
left()
move(4, 'forward')
left()
move(15, 'forward')
left()
move(3, 'down')
turtle.dropDown()
-- auther: Ckhrysze
--
-- for testing
-- require 'turtle'
local function curry(func, arg)
return function() func(arg) end
end
local cmdMap = {
f = turtle.forward,
b = turtle.back,
u = turtle.up,
d = turtle.down,
l = turtle.turnLeft,
r = turtle.turnRight,
dg = turtle.dig,
du = turtle.digUp,
dd = turtle.digDown,
pl = turtle.place,
pu = turtle.placeUp,
pd = turtle.placeDown,
sk = turtle.suck,
su = turtle.suckUp,
sd = turtle.suckDown,
mv = turtle.drop,
mu = turtle.dropUp,
md = turtle.dropDown,
sl = turtle.select,
fuel = turtle.refuel
}
local cmdRegex = '^(%d*)([%a()]+)(%d*)$'
local function createCmdList(terms)
local commands = {}
local term = table.remove(terms, 1)
while type(term) == 'string' do
-- print(term)
local reps, cmd, arg = string.match(term, cmdRegex)
if not tonumber(reps) then reps = 1 end
if cmd == ')' then
return commands
elseif cmd == '(' then
local sublist = createCmdList(terms)
if not sublist then return "Error" end
for i=1,reps do
for _,subcmd in ipairs(sublist) do
table.insert(commands, subcmd)
end
end
else
func = cmdMap[cmd]
if not func then
print( "No command found for " .. cmd )
return nil
end
if tonumber(arg) then func = curry(func, arg) end
for i=1,reps do
table.insert(commands, func)
end
end
term = table.remove(terms, 1)
end
return commands
end
local function execute(commands)
for _,command in ipairs(commands) do
command()
end
end
local args = { ... }
-- local args = { '4(', 'f', 'pd', 'fuel', ')', 'l', 'f', 'l', '4f'}
if not args[1] then
print("Please supply at least 1 command")
return
end
commandList = createCmdList(args)
if commandList then
execute(commandList)
end
function up()
turtle.up()
end
function down()
turtle.down()
end
function right()
turtle.turnRight()
end
function strafe(turn)
turn()
turtle.forward()
for i=1,3 do turn() end
end
function check_inventory()
for i=1,16 do
if turtle.getItemCount(i) == 0 then
print("Please have blocks in all item slots")
return false
end
end
return true
end
function place(slot)
turtle.select(slot)
turtle.place()
end
function move_place(slot, move)
move()
place(slot)
end
function place_move(slot, move)
place(slot)
move()
end
function build_wall()
for i=13, 1, -4 do place_move(i, up) end
strafe(right)
for i=2, 14, 4 do move_place(i, down) end
strafe(right)
for i=15, 3, -4 do place_move(i, up) end
strafe(right)
for i=4, 16, 4 do move_place(i, down) end
strafe(right)
end
args = {...}
if not args[1] then
print("Please provide the number of 4x4 wall segments to fill out")
return
elseif not tonumber(args[1]) then
print("The first argument should be a number")
return
end
segments = tonumber(args[1])
if segments > 64 then
print("Cannot create more than 64 segments in a single run")
return
end
for i=1, segments do build_wall() end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment