Last active
April 12, 2020 22:50
-
-
Save Kipjr/f4a6f3c671027a5fc2688e9ca5d43dd8 to your computer and use it in GitHub Desktop.
ComputerCraft/turtle_mine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
John Excavation Turtle Program(New) | |
Changelog | |
0.01 - Starting Of Rewriting | |
0.02 - More Writing | |
0.03 - Adding Fuel Code | |
0.04 - Small but many program error fixed | |
0.05 - Fully Test | |
0.06 - Fixing Minor Bug when add 3 to deep because firstdig had no 3 add to deepcount | |
0.07 - Fixing High Error | |
0.08 - Fix cobblestone dump | |
0.09 - Added repair wall function. | |
0.10a - Fix bug | |
0.11 - places torches. | |
0.12 - correct placement of torches and move up when end | |
1.0 - Working version | |
1.1 - fix find-function | |
1.2 - added gravel / dirt to drop | |
--]] | |
--[[ | |
long | |
refuel | |
chestdump | |
dumpcs | |
long | |
refuel | |
chestdump | |
dumpcs | |
--]] | |
-- Local Variables | |
-- Starting | |
local wide = 0 | |
local wideCount = 0 | |
local long = 0 | |
local longCount = 0 | |
local deep = 0 | |
local deepCount = 0 | |
local totalBlocks = 0 | |
local coalNeeded = 0 | |
local corrent = n | |
-- Inventory This Will Check At Start Code | |
local chest = 0 | |
local fuelCount = 0 | |
local noFuelNeed = 0 -- if Config has fuel no need on | |
local error = 0 -- Error Code | |
local reCheck = 0 -- Recheck Code | |
-- Others | |
local totalBlocks = 0 | |
local totalBlockDone = 0 -- How many Block Mined | |
local LSorWS = 0 -- Go Left or Go Right This is for Wide Code | |
local ALorAR = 0 -- align left Or align Right | |
local processRaw = 0 | |
local process = 0 | |
local enderChest = 0 -- TODO | |
local blockUp = 0 -- Fixing to Chest Probleem and moving probleem | |
local processRaw = 0 | |
local process = 0 | |
local name_cs = {"minecraft:cobblestone","minecraft:dirt","minecraft:gravel"} | |
local name_to = {"minecraft:torch"} | |
local name_ch = {"minecraft:chest"} | |
local name_fu = {"minecraft:coal","railcraft:fuel_coke"} | |
local item_cs = 0 | |
local item_to = 0 | |
local item_ch = 0 | |
local item_fu = 0 | |
local repairMode = 0 | |
local torchMode = 0 | |
local torchLen = 15 --interval of torches | |
local debug = 0 | |
--short arg startup | |
-- len, width deep, repair, torch | |
-- 20,1,3,0,0 | |
--input,input2,input3,input4,input5 | |
local params = {...} | |
input = params[1] -- parameter, if any. | |
input2 = params[2] -- parameter, if any. | |
input3 = params[3] -- parameter, if any. | |
input4 = params[4] -- parameter, if any. | |
input5 = params[5] -- parameter, if any. | |
input6 = params[6] -- parameter, if any. | |
num_params = #params -- number of parameters. | |
-- dumps cobblestone | |
local function dumpcs() | |
if debug==1 then print("dumpcs") end | |
for i=1,16,1 do | |
if turtle.getItemCount(i)>32 then | |
for _,v in pairs(name_cs) do | |
if turtle.getItemDetail(i).name == v then | |
item_var = i | |
break | |
end | |
end | |
end | |
end | |
end | |
--General find function | |
-- find(name_ch) | |
-- return INT item_ch | |
local function find(item_name,item_pos) | |
if debug==1 then print("find") end | |
item_pos=item_pos or 0 | |
--it had previous position | |
if item_pos~=0 then | |
if turtle.getItemCount(item_pos) > 2 then | |
-- nothing | |
end | |
-- default / not found | |
else | |
for i=1,16,1 do | |
if turtle.getItemCount(i)>0 then | |
for _,v in pairs(item_name) do | |
if turtle.getItemDetail(i).name == v then | |
item_pos = i | |
break | |
end | |
end | |
end | |
end | |
end | |
if item_pos == 0 then | |
print("[" .. table.concat(item_name,",") .. "] item not found") | |
end | |
return item_pos | |
end | |
--[[ | |
--find cobblestone in inventory | |
local function findcs() | |
-- or item_cs = find(name_cs) | |
if debug==1 then print("findcs") end | |
item_cs = 0 | |
for i=1,16,1 do | |
if turtle.getItemCount(i)>0 then | |
if turtle.getItemDetail(i).name==name_cs then | |
item_cs = i | |
break | |
end | |
end | |
end | |
if item_cs == 0 then | |
print("torches empty") | |
end | |
end | |
--find torch in inventory | |
local function findto() | |
-- or item_to = find(name_to) | |
if debug==1 then print("findto") end | |
item_to = 0 | |
for i=1,16,1 do | |
if turtle.getItemCount(i)>0 then | |
if turtle.getItemDetail(i).name==name_to then | |
item_to = i | |
break | |
end | |
end | |
end | |
if item_to == 0 then | |
print("torches empty") | |
end | |
end | |
--find chest in inventory | |
local function findch() | |
-- or item_ch = find(name_ch) | |
if debug==1 then print("findch") end | |
item_ch = 0 | |
for i=1,16,1 do | |
if turtle.getItemCount(i)>0 then | |
if turtle.getItemDetail(i).name==name_ch then | |
item_ch = i | |
break | |
end | |
end | |
end | |
if item_ch == 0 then | |
print("chests empty") | |
end | |
end | |
--find fuel in inventory | |
local function findfu() | |
-- or item_fu = find(name_fu) | |
if debug==1 then print("findfu") end | |
item_fu=0 | |
for i=1,16,1 do | |
if turtle.getItemCount(i)>0 then | |
if turtle.getItemDetail(i).name==name_fu or name_fu2 then | |
item_fu = i | |
break | |
end | |
end | |
end | |
if item_fu == 0 then | |
print("fuel empty") -- fix bug | |
end | |
end | |
--]] | |
local function repair() | |
if debug==1 then print("repair") end | |
item_cs=find(name_cs) | |
if item_cs ~=0 then | |
turtle.select(item_cs) | |
--top | |
if repairMode==1 then | |
turtle.up() | |
if not turtle.detectUp() then | |
turtle.placeUp() | |
end | |
turtle.down() | |
end | |
--top + left | |
if repairMode==2 then | |
turtle.up() | |
if not turtle.detectUp() then | |
turtle.placeUp() | |
end | |
turtle.turnLeft() | |
for j=1,3,1 do | |
if not turtle.detect() then | |
turtle.place() | |
end | |
turtle.down() | |
end | |
turtle.turnRight() | |
turtle.up() | |
end | |
--top + right | |
if repairMode==3 then | |
turtle.up() | |
if not turtle.detectUp() then | |
turtle.placeUp() | |
end | |
turtle.turnRight() | |
for j=1,3,1 do | |
if not turtle.detect() then | |
turtle.place() | |
end | |
turtle.down() | |
end | |
turtle.turnLeft() | |
turtle.up() | |
end | |
end | |
end | |
local function torch() | |
if debug==1 then print("torch") end | |
--places torch. | |
item_to=find(name_to) | |
if item_to ~=0 then | |
turtle.select(item_to) | |
--[[ if mining area | |
if deep ~= 3 then | |
turtle.turnLeft() | |
if turtle.detect() then | |
turtle.dig() | |
end | |
turtle.place() | |
turtle.turnRight() | |
end | |
-- if mining shaft ]] | |
--if deep == 3 then | |
turtle.turnLeft() | |
turtle.turnLeft() | |
turtle.down() | |
turtle.place() | |
turtle.up() | |
turtle.turnLeft() | |
turtle.turnLeft() | |
--end | |
end | |
end | |
-- Checking | |
local function check() | |
if debug==1 then print("check") end | |
if noFuelNeed == 0 then | |
if fuelCount == 0 then | |
print("Turtle has no fuel") | |
print("Put fuel in a slot") | |
error = 1 | |
else | |
print("Turtle has Fuel") | |
end | |
end | |
if chest == 0 then | |
print("No chest in Turtle") | |
print("Put chest in 1 slot") | |
error = 1 | |
else | |
print("Turtle has chest") | |
end | |
if error == 1 then | |
print("Items are missing please try again") | |
print("Turtle will recheck in 8 sec") | |
end | |
end | |
-- Recheck if user forget something turtle will check after 6 sec | |
local function reCheck() | |
if debug==1 then print("reCheck") end | |
chest = 0 | |
fuelCount = 0 | |
item_ch=find(name_ch) | |
item_fu=find(name_fu) | |
if item_ch ~= 0 then | |
chest = turtle.getItemCount(item_ch) | |
end | |
if item_fu ~=0 then | |
fuelCount = turtle.getItemCount(item_fu) | |
end | |
error = 0 | |
end | |
local function chestDump() | |
if debug==1 then print("chestdump") end | |
--first dump all cobblestone. | |
dumpcs() | |
if turtle.getItemCount(16)> 0 then -- If slot 16 in turtle has item slot 4 to 16 will go to chest | |
repeat -- The Fix to Gravel Chest Bug. It check if gravel above then it dig till it gone | |
turtle.digUp() | |
sleep(0.1) | |
if turtle.detectUp() then | |
turtle.digUp() | |
blockUp = 1 | |
else | |
blockUp = 0 | |
end | |
until blockUp == 0 | |
item_ch=find(name_ch, item_cs) | |
if debug==1 then print("chestdump/item_ch:"..item_ch) end | |
turtle.select(item_ch) | |
turtle.placeUp() | |
chest = chest - 1 | |
for slot = 1, 16 do | |
turtle.select(slot) | |
sleep(0.1) -- Small fix for slow pc because i had people problem with this | |
turtle.dropUp() | |
end | |
turtle.select(4) | |
if Chest == 0 then | |
print("Out Of Chest") | |
os.shutdown() | |
end | |
end | |
end | |
-- Refuel | |
local function refuel() | |
if debug==1 then print("refuel") end | |
if noFuelNeed == 0 then | |
repeat | |
if turtle.getFuelLevel() < 160 then | |
reCheck() | |
if fuelCount > 0 then | |
turtle.select(item_fu) --select 0 - out of range error if fuel is empty | |
turtle.refuel(1) | |
fuelCount = fuelCount - 1 | |
else | |
print("out of fuel") | |
os.shutdown() | |
end | |
end | |
until turtle.getFuelLevel() >= 160 | |
end | |
end | |
local function mineLong() | |
if debug==1 then print("long") end | |
if turtle.detect() then | |
turtle.dig() | |
end | |
if repairMode ~=0 then | |
repair() | |
end | |
if turtle.forward() then | |
longCount = longCount + 1 | |
else | |
repeat | |
turtle.dig() | |
sleep(0.1) | |
if turtle.forward() then | |
blockUp = 0 | |
else | |
blockUp = 1 | |
end | |
until blockUp == 0 | |
longCount = longCount + 1 | |
print(totalBlocks - totalBlockDone) | |
end | |
if turtle.detectUp() then | |
turtle.digUp() | |
end | |
if turtle.detectDown() then | |
turtle.digDown() | |
end | |
totalBlockDone = totalBlockDone + 3 | |
end | |
local function wideMineLeft() -- TODO | |
if debug==1 then print("left") end | |
turtle.turnLeft() | |
if turtle.detect() then | |
turtle.dig() | |
sleep(0.3) -- Minor bug fix if there is gravel | |
end | |
if turtle.forward() then | |
--notting | |
else | |
repeat | |
turtle.dig() | |
sleep(0.3) | |
if turtle.forward() then | |
blockUp = 0 | |
else | |
blockUp = 1 | |
end | |
until blockUp == 0 | |
end | |
if turtle.detectUp() then | |
turtle.digUp() | |
end | |
if turtle.detectDown() then | |
turtle.digDown() | |
end | |
turtle.turnLeft() | |
LSorWS = 0 | |
longCount = 0 | |
wideCount = wideCount + 1 | |
totalBlockDone = totalBlockDone + 3 | |
end | |
local function wideMineRight() -- TODO | |
if debug==1 then print("right") end | |
turtle.turnRight() | |
if turtle.detect() then | |
turtle.dig() | |
sleep(0.3) | |
end | |
if turtle.forward() then | |
--Notting | |
else | |
repeat | |
turtle.dig() | |
sleep(0.3) | |
if turtle.forward() then | |
blockUp = 0 | |
else | |
blockUp = 1 | |
end | |
until blockUp == 0 | |
end | |
if turtle.detectUp() then | |
turtle.digUp() | |
end | |
if turtle.detectDown() then | |
turtle.digDown() | |
end | |
turtle.turnRight() | |
LSorWS = 1 | |
longCount = 0 | |
wideCount = wideCount + 1 | |
totalBlockDone = totalBlockDone + 3 | |
end | |
local function deepMine() | |
if debug==1 then print("deep") end | |
turtle.digDown() | |
turtle.down() | |
turtle.digDown() | |
turtle.down() | |
turtle.digDown() | |
turtle.down() | |
turtle.digDown() | |
turtle.turnRight() | |
turtle.turnRight() | |
wideCount = 0 | |
longCount = 0 | |
deepCount = deepCount + 3 | |
totalBlockDone = totalBlockDone + 3 | |
end | |
local function main() | |
repeat --Repeat for each level | |
mineLong() | |
refuel() | |
chestDump() | |
if torchMode == 1 then | |
if longCount%torchLen == 1 and wideCount==0 and deepCount%torchLen==0 then | |
torch() | |
end | |
end | |
-- if at the end | |
if longCount == long then | |
--not equal to max width | |
if wideCount ~= wide then | |
process = totalBlockDone / totalBlocks * 100 | |
processRaw = totalBlocks - totalBlockDone | |
print("How Much Is Done: " .. math.floor(process+0.5) .. " %") | |
print("TotalBlocks Still Need To Dig Is " .. processRaw) | |
if LSorWS == 0 then | |
wideMineRight() | |
else | |
wideMineLeft() | |
end | |
--mine to max width, start with deep mine. | |
elseif wideCount >= wide then | |
torchMode = 0 | |
deepMine() | |
end | |
end | |
until deepCount >= deep | |
turtle.up() | |
item_cs=find(name_cs) | |
turtle.select(item_cs) | |
turtle.placeDown() | |
turtle.up() | |
turtle.placeDown() | |
turtle.up() | |
print("turtle is Done") | |
end | |
local function firstDig() | |
if debug==1 then print("firstdig") end | |
turtle.digDown() | |
turtle.down() | |
turtle.digDown() | |
turtle.down() | |
turtle.digDown() | |
wideCount = 0 | |
longCount = 0 | |
totalBlockDone = totalBlockDone + 3 | |
end | |
local function start() | |
if num_params < 5 or num_params>6 then | |
print("Welcome To Excavation Turtle Program") | |
print("Fill turtle with coal/charcoal & chests, optional torches") | |
print("How long(Lenght) you want") | |
input = io.read() | |
print("How wide(Width) you want") | |
input2 = io.read() | |
print("How Deep(Depth) You Want") | |
input3 = io.read() | |
print("Repair sidewalls and top? 1=top, 2=top+left, 3=top+right, 0=false (only for X,1,3 shafts") | |
input4 = io.read() | |
print("Place torches? 1=True 0=False") | |
input5 = io.read() | |
print("Debug 1=True, 0=False") | |
input6 = io.read() | |
end | |
long = tonumber(input) | |
long = long - 1 | |
wide = tonumber(input2) | |
wide = wide - 1 | |
deep = tonumber(input3) | |
repairMode = tonumber(input4) | |
input5b = tonumber(input5) | |
if input5b == 1 then | |
torchMode = 1 | |
end | |
input6b = tonumber(input6) | |
if input6b == 1 then | |
debug = 1 | |
end | |
print("Check:" .. "Len:" .. (long + 1) .. " Width:" .. (wide + 1) .. " Depth:" .. (deep) .. string.char(10) .. "repair:" .. (repairMode) .. " torch:" .. (torchMode) .. string.char(10) .. "debug:" .. (debug)) | |
print("Type y Or Y if it is correct and if not then n or N") | |
corrent = io.read() | |
if corrent == n or N then | |
os.reboot() | |
end | |
print("Okey Program Will Do Calculations") | |
totalBlocks = (wide + 1) * (long + 1) * deep -- 1 is add because above it removed for wide and long code | |
print("Total amount for block to mine is " .. totalBlocks) | |
coalNeeded = totalBlocks / 3 / 80 | |
print("Total amount for Coal needed is " .. math.floor(coalNeeded+0.5)) | |
if turtle.getFuelLevel() == "unlimited" then | |
print("Your turtle config does need fuel") | |
noFuelNeed = 1 | |
elseif turtle.getFuelLevel() < 160 then | |
turtle.select(2) | |
turtle.refuel(2) | |
end | |
reCheck() | |
check() | |
if error == 1 then | |
repeat | |
sleep(6) | |
reCheck() | |
check() | |
until error == 0 | |
end | |
print("Turtle will now start!") | |
firstDig() | |
main() | |
end | |
start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment