moved files according to new project structure
This commit is contained in:
131
dig/bin/dig
Normal file
131
dig/bin/dig
Normal file
@@ -0,0 +1,131 @@
|
||||
|
||||
|
||||
local dug = false
|
||||
local length = 0
|
||||
local tArgs = { ... }
|
||||
local orientation = 1
|
||||
local offset = { 0, 0, 0 }
|
||||
|
||||
|
||||
function rotate(dir)
|
||||
-- rotate
|
||||
if dir == 'left' then
|
||||
turtle.turnLeft()
|
||||
orientation = orientation - 1
|
||||
elseif dir == 'right' then
|
||||
turtle.turnRight()
|
||||
orientation = orientation + 1
|
||||
end
|
||||
|
||||
-- fix orientation if invalid
|
||||
if(orientation < 1) then orientation = orientation + 4 end
|
||||
if(orientation > 4) then orientation = orientation - 4 end
|
||||
end
|
||||
|
||||
function advanceOffset()
|
||||
if orientation == 1 then offset[1] = offset[1] + 1
|
||||
elseif orientation == 2 then offset[2] = offset[2] + 1
|
||||
elseif orientation == 3 then offset[1] = offset[1] - 1
|
||||
elseif orientation == 4 then offset[2] = offset[2] - 1
|
||||
end
|
||||
end
|
||||
|
||||
function move()
|
||||
local attempt = 0;
|
||||
while not turtle.forward() do
|
||||
print("attempting move at offset:",offset[1],offset[2],offset[3])
|
||||
-- print(offset)
|
||||
attempt = attempt + 1
|
||||
end
|
||||
advanceOffset()
|
||||
end
|
||||
|
||||
function usage()
|
||||
return "dig <length>"
|
||||
end
|
||||
|
||||
function checkInv()
|
||||
local emptySpace = false
|
||||
for i=1,16 do
|
||||
if turtle.getItemDetail(i) == nil then
|
||||
print("found empty space")
|
||||
emptySpace = true
|
||||
break
|
||||
end
|
||||
end
|
||||
return emptySpace
|
||||
end
|
||||
|
||||
local waypoint = { 0, 0, 0}
|
||||
function setWaypoint()
|
||||
waypoint = offset
|
||||
end
|
||||
|
||||
function offsetEq(o1, o2)
|
||||
if o1[1] == o2[1] and o1[2] == o2[2] and o1[3] == o2[3] then
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
function returnHome()
|
||||
while not (orientation == 3) do
|
||||
rotate("left")
|
||||
end
|
||||
while not offsetEq(offset, {0,0,0}) do
|
||||
print("offset is not {0,0,0}","proof:",offset[1],offset[2],offset[3])
|
||||
move()
|
||||
end
|
||||
end
|
||||
|
||||
function emptyInv()
|
||||
for i=1,16 do
|
||||
turtle.select(i)
|
||||
turtle.drop()
|
||||
end
|
||||
end
|
||||
|
||||
function returnToWaypoint()
|
||||
while not (orientation == 1) do
|
||||
rotate("right")
|
||||
end
|
||||
while not offsetEq(offset, waypoint) do
|
||||
move()
|
||||
end
|
||||
end
|
||||
|
||||
function dig()
|
||||
-- dig out next position
|
||||
turtle.dig()
|
||||
move()
|
||||
turtle.digUp()
|
||||
turtle.digDown()
|
||||
end
|
||||
|
||||
function doDig()
|
||||
for i=1,length do
|
||||
-- check if we still have free inventory space
|
||||
spaceleft = checkInv()
|
||||
|
||||
if spaceleft then
|
||||
dig()
|
||||
else
|
||||
setWaypoint()
|
||||
returnHome()
|
||||
emptyInv()
|
||||
returnToWaypoint()
|
||||
dig()
|
||||
end
|
||||
print(i)
|
||||
end
|
||||
returnHome()
|
||||
emptyInv()
|
||||
end
|
||||
|
||||
if tArgs[1] and type(tonumber(tArgs[1])) == "number" then
|
||||
length = tonumber(tArgs[1]);
|
||||
doDig()
|
||||
else
|
||||
print(usage())
|
||||
end
|
||||
12
dig/packageinfo.ccpt
Normal file
12
dig/packageinfo.ccpt
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
name = "dig",
|
||||
comment = "Turtle Tunnel Digging Program",
|
||||
author = "Hion-V",
|
||||
website = "https://git.subsonics.nl/pootercraft/dig",
|
||||
newestversion = "1.0",
|
||||
install = {
|
||||
scripturl = "https://git.subsonics.nl/pootercraft/dig/raw/branch/master/packageinstall.lua",
|
||||
type = "script",
|
||||
},
|
||||
dependencies = {},
|
||||
}
|
||||
143
dig/packageinstall.lua
Normal file
143
dig/packageinstall.lua
Normal file
@@ -0,0 +1,143 @@
|
||||
--[[
|
||||
Dig Program Installer
|
||||
Author: Hion-V
|
||||
Version: 1.0
|
||||
Lines of Code: 161; Characters: 5541
|
||||
]]
|
||||
|
||||
-- INSTALL PACKAGE INFO
|
||||
packageName = "dig"
|
||||
packageURL = "https://git.subsonics.nl/pootercraft/dig/raw/branch/master/bin/dig"
|
||||
packageBinPath = "/bin/"
|
||||
packageFilename = "dig"
|
||||
|
||||
-- Read arguments
|
||||
args = {...}
|
||||
|
||||
-- FILE MANIPULATION FUNCTIONS --
|
||||
--[[ Checks if file exists
|
||||
@param String filepath: Filepath to check
|
||||
@return boolean: Does the file exist?
|
||||
--]]
|
||||
function file_exists(filepath)
|
||||
local f=io.open(filepath,"r")
|
||||
if f~=nil then
|
||||
io.close(f)
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
--[[ Stores a file in a desired location
|
||||
@param String filepath: Filepath where to create file (if file already exists, it gets overwritten)
|
||||
@param String content: Content to store in file
|
||||
--]]
|
||||
function storeFile(filepath,content)
|
||||
writefile = fs.open(filepath,"w")
|
||||
writefile.write(content)
|
||||
writefile.close()
|
||||
end
|
||||
|
||||
--[[ Reads a file from a desired location
|
||||
@param String filepath: Filepath to the file to read
|
||||
@param String createnew: (Optional) Content to store in new file and return if file does not exist. Can be nil.
|
||||
@return String|boolean content|error: Content of the file; If createnew is nil and file doesn't exist boolean false is returned
|
||||
--]]
|
||||
function readFile(filepath,createnew)
|
||||
readfile = fs.open(filepath,"r")
|
||||
if readfile == nil then
|
||||
if not (createnew==nil) then
|
||||
storeFile(filepath,createnew)
|
||||
return createnew
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
content = readfile.readAll()
|
||||
readfile.close()
|
||||
return content
|
||||
end
|
||||
|
||||
--[[ Stores a table in a file
|
||||
@param String filepath: Filepath where to create file (if file already exists, it gets overwritten)
|
||||
@param Table data: Table to store in file
|
||||
--]]
|
||||
function storeData(filepath,data)
|
||||
storeFile(filepath,textutils.serialize(data):gsub("\n",""))
|
||||
end
|
||||
|
||||
--[[ Reads a table from a file in a desired location
|
||||
@param String filepath: Filepath to the file to read
|
||||
@param boolean createnew: If true, an empty table is stored in new file and returned if file does not exist.
|
||||
@return Table|boolean content|error: Table thats stored in the file; If createnew is false and file doesn't exist boolean false is returned
|
||||
--]]
|
||||
function readData(filepath,createnew)
|
||||
if createnew then
|
||||
return textutils.unserialize(readFile(filepath,textutils.serialize({}):gsub("\n","")))
|
||||
else
|
||||
return textutils.unserialize(readFile(filepath,nil))
|
||||
end
|
||||
end
|
||||
|
||||
-- HTTP FETCH FUNCTIONS --
|
||||
--[[ Gets result of HTTP URL
|
||||
@param String url: The desired URL
|
||||
@return Table|boolean result|error: The result of the request; If the URL is not reachable, an error is printed in the terminal and boolean false is returned
|
||||
--]]
|
||||
function gethttpresult(url)
|
||||
if not http.checkURL(url) then
|
||||
print("ERROR: Url '" .. url .. "' is blocked in config. Unable to fetch data.")
|
||||
return false
|
||||
end
|
||||
result = http.get(url)
|
||||
if result == nil then
|
||||
print("ERROR: Unable to reach '" .. url .. "'")
|
||||
return false
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
--[[ Download file HTTP URL
|
||||
@param String filepath: Filepath where to create file (if file already exists, it gets overwritten)
|
||||
@param String url: The desired URL
|
||||
@return nil|boolean nil|error: nil; If the URL is not reachable, an error is printed in the terminal and boolean false is returned
|
||||
--]]
|
||||
function downloadfile(filepath,url)
|
||||
result = gethttpresult(url)
|
||||
if result == false then
|
||||
return false
|
||||
end
|
||||
storeFile(filepath,result.readAll())
|
||||
end
|
||||
|
||||
-- MISC HELPER FUNCTIONS --
|
||||
--[[ Checks wether a String starts with another one
|
||||
@param String haystack: String to check wether is starts with another one
|
||||
@param String needle: String to check wether another one starts with it
|
||||
@return boolean result: Wether the firest String starts with the second one
|
||||
]]--
|
||||
function startsWith(haystack,needle)
|
||||
return string.sub(haystack,1,string.len(needle))==needle
|
||||
end
|
||||
|
||||
-- MAIN PROGRAMM --
|
||||
if (args[1]=="install") or (args[1]==nil) then
|
||||
print("[Installer] Installing",packageName)
|
||||
if downloadfile(packageBinPath..packageFilename,packageURL)==false then
|
||||
return false
|
||||
end
|
||||
print("[Installer] Successfully installed '"..packageName.."'!")
|
||||
elseif args[1]=="update" then
|
||||
print("[Installer] Updating "..packageName.."...")
|
||||
if downloadfile("bin/dig",packageURL)==false then
|
||||
fs.move()
|
||||
return false
|
||||
end
|
||||
elseif args[1]=="remove" then
|
||||
print("[Installer] Uninstalling '"..packageName.."'...")
|
||||
fs.delete("/bin/dig")
|
||||
print("[Installer] So long, and thanks for all the fish!")
|
||||
else
|
||||
print("[Installer] Invalid argument: " .. args[1])
|
||||
end
|
||||
Reference in New Issue
Block a user