Myros27

mineBot

May 21st, 2025 (edited)
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 27.14 KB | None | 0 0
  1. -- Advanced Mining Turtle for CC: Tweaked
  2.  
  3. -- Configuration
  4. local DEBUG_MODE = false
  5. local LOG_FILE = "miner_log.txt"
  6. local LOG_CYCLE_LIMIT = 10
  7. local main_loop_cycle_count_for_log = 0
  8.  
  9. local FUEL_ENDER_CHEST_NBT = nil
  10. local ITEM_DUMP_ENDER_CHEST_NBT = "12dc70dc0c75dbe36569479175a42608"
  11.  
  12. local FUEL_ALERT_PERCENTAGE = 40
  13. local MIN_FREE_SLOTS_FOR_OPERATION = 2
  14. local WORLD_BOTTOM_Y_CALIBRATION_DEPTH = 317
  15.  
  16. local ENDER_CHEST_ITEM_NAME = "enderstorage:ender_chest"
  17. local COAL_BLOCK_ITEM_NAME = "minecraft:coal_block"
  18. local CHUNK_CONTROLLER_ITEM_NAME = "advancedperipherals:chunk_controller"
  19. local BEDROCK_ITEM_NAME = "minecraft:bedrock"
  20. local AIR_ITEM_NAME = "minecraft:air"
  21.  
  22. local is_y_calibrated = false
  23. local dig_and_move_actions_before_check = 0
  24. local current_y_relative = 0
  25.  
  26. --------------------------------------------------------------------------------
  27. -- Logging, Error Handling, and Table Serialization
  28. --------------------------------------------------------------------------------
  29. local function should_log_details() return DEBUG_MODE and (main_loop_cycle_count_for_log < LOG_CYCLE_LIMIT or main_loop_cycle_count_for_log == 0) end
  30. local function serialize_table(tbl,indent_level)indent_level=indent_level or 0;local i_s=string.rep("  ",indent_level);local p={};if type(tbl)~="table"then return tostring(tbl)end;table.insert(p,"{");local f=true;for k,v in pairs(tbl)do if not f then table.insert(p,",")end;f=false;table.insert(p,"\n"..string.rep("  ",indent_level+1));if type(k)=="string"then table.insert(p,k.." = ")else table.insert(p,"["..tostring(k).."] = ")end;if type(v)=="table"then table.insert(p,serialize_table(v,indent_level+1))elseif type(v)=="string"then table.insert(p,"\""..tostring(v):gsub("\"","\\\"").."\"")else table.insert(p,tostring(v))end end;table.insert(p,"\n"..i_s.."}");return table.concat(p)end
  31. local function log_message(msg,data_tbl) if not should_log_details() and msg:find("Cycle #")==nil and msg:find("FATAL ERROR")==nil and msg:find("--- Initializing")==nil and msg:find("--- Sys Check")==nil then return end; local ts_msg=os.date("%Y-%m-%d %H:%M:%S").." - "..msg;if data_tbl and should_log_details()then ts_msg=ts_msg.."\nData: "..serialize_table(data_tbl)end;if DEBUG_MODE then local f,e=fs.open(LOG_FILE,"a");if f then f.writeLine(ts_msg);f.close()else print("ERR: No log file: "..tostring(e))end end;print(msg);if data_tbl and should_log_details()then print("Data:\n"..serialize_table(data_tbl))end end
  32. local function error_stop(msg,data_tbl)log_message("FATAL ERROR: "..msg,data_tbl);print("Stopping.");error(msg,0)end
  33.  
  34. --------------------------------------------------------------------------------
  35. -- Helper Functions (Unchanged from previous)
  36. --------------------------------------------------------------------------------
  37. local function find_item_in_inventory(f,dbg_ctx)if dbg_ctx and should_log_details()then log_message("find_item_in_inventory for: "..dbg_ctx)end;for i=1,16 do local d=turtle.getItemDetail(i)if d and d.name~=AIR_ITEM_NAME then if dbg_ctx and should_log_details()then log_message("  Chk slot "..i.." for '"..dbg_ctx.."':",d)end;if f(d,i)then if dbg_ctx and should_log_details()then log_message("    Found match slot "..i.." for '"..dbg_ctx.."'")end;return i,d end end end;if dbg_ctx and should_log_details()then log_message("  No match for '"..dbg_ctx.."'")end;return nil,nil end
  38. local function count_free_slots()local c=0;for i=1,16 do if turtle.getItemCount(i)==0 then c=c+1 end end;return c end
  39. local function turn_around()turtle.turnLeft();turtle.turnLeft()end
  40. local function get_item_from_hand_temporarily(h_s)log_message("Check "..h_s.." hand...");local e_s=nil;for i=16,1,-1 do if turtle.getItemCount(i)==0 then e_s=i;break end end;if not e_s then error_stop("No empty slot for hand check.")return nil end;log_message("Temp slot "..e_s.." for "..h_s);local o_s=turtle.getSelectedSlot();turtle.select(e_s);local u_ok;if h_s=="left"then u_ok=turtle.equipLeft()elseif h_s=="right"then u_ok=turtle.equipRight()else turtle.select(o_s);error_stop("Invalid hand: "..h_s)return nil end;local i_d=nil;if u_ok then i_d=turtle.getItemDetail(e_s);if i_d and i_d.name~=AIR_ITEM_NAME then log_message(h_s.." hand had:",i_d);turtle.select(e_s);local r_ok;if h_s=="left"then r_ok=turtle.equipLeft()else r_ok=turtle.equipRight()end;if not r_ok then error_stop("CRIT: Fail re-equip "..i_d.name.." to "..h_s)end;log_message("Item "..i_d.name.." re-equipped.")else log_message(h_s.." hand empty/air.");i_d=nil end else log_message(h_s.." already empty.");i_d=nil end;turtle.select(o_s);return i_d end
  41. local function safe_dig_generic(d_f,dir_name,insp_f)if not d_f()then local insp_s,blk_d=insp_f();if insp_s then if blk_d.name:lower()==BEDROCK_ITEM_NAME:lower()then log_message("Cannot dig "..dir_name..", bedrock.");return false end;error_stop("Failed dig "..dir_name.." (block: "..blk_d.name.."). Tool issue?")else log_message("Attempted dig "..dir_name.." but no block (inspect failed: "..tostring(blk_d)..").");return true end end;return true end
  42. local function safe_dig()return safe_dig_generic(turtle.dig,"fwd",turtle.inspect)end;local function safe_dig_up()return safe_dig_generic(turtle.digUp,"up",turtle.inspectUp)end;local function safe_dig_down()return safe_dig_generic(turtle.digDown,"down",turtle.inspectDown)end
  43. local function safe_move_generic(m_f,dir)if not m_f()then error_stop("Failed move "..dir)end;return true end
  44. local function safe_forward()return safe_move_generic(turtle.forward,"fwd")end;local function safe_back()return safe_move_generic(turtle.back,"back")end;local function safe_up()return safe_move_generic(turtle.up,"up")end;local function safe_down()return safe_move_generic(turtle.down,"down")end
  45. local function safe_place(s)if not s then error_stop("safe_place: no slot.")end;local o=turtle.getSelectedSlot();turtle.select(s);if not turtle.place()then turtle.select(o);error_stop("Failed place "..s)end;turtle.select(o);return true end
  46. local function retrieve_specific_ender_chest_item(nbt,name_for_log)log_message("Confirming item "..name_for_log.." with NBT: "..tostring(nbt).." is in inventory.");os.sleep(0.1);local s,d=find_item_in_inventory(function(it)return it.name:lower()==ENDER_CHEST_ITEM_NAME:lower()and it.nbt==nbt end,"Confirm "..name_for_log);if not s then error_stop("CRIT: "..name_for_log.." NBT "..tostring(nbt).." not found after expecting it.")end;log_message(name_for_log.." NBT "..tostring(nbt).." confirmed in slot "..s);return s,d end
  47. local function decrement_dig_move_actions()dig_and_move_actions_before_check=dig_and_move_actions_before_check-1 end
  48. local function perform_initial_setup(dump_ec_s,coal_s,cc_s,pick_s)log_message("SP1 Setup...");log_message("Refuel coal slot "..coal_s);turtle.select(coal_s);local r=turtle.refuel(1);if r==false then error_stop("Fail initial refuel (false).")elseif type(r)=="number"then if r>0 then log_message("Refueled "..r)else log_message("WARN: Refuel 0 (full/no fuel).")end elseif r==true then log_message("Initial coal consumed, 0 fuel (full). OK.")else error_stop("Unexpected refuel(1) ret: "..tostring(r))end;log_message("Equip CC slot "..cc_s);turtle.select(cc_s);if not turtle.equipRight()then error_stop("Fail equip CC.")end;log_message("CC equipped.");log_message("Equip Pickaxe slot "..pick_s);turtle.select(pick_s);if not turtle.equipLeft()then error_stop("Fail equip pick.")end;log_message("Pickaxe equipped.");turtle.select(1);local items_l=0;local found_d=false;for i=1,16 do local it=turtle.getItemDetail(i)if it and it.name~=AIR_ITEM_NAME then items_l=items_l+1;if it.name:lower()==ENDER_CHEST_ITEM_NAME:lower()and it.nbt==ITEM_DUMP_ENDER_CHEST_NBT then found_d=true end end end;if not found_d then error_stop("ItemDump EC gone after SP1 equip.")end;if items_l>1 then error_stop("Too many items after SP1 equip: "..items_l)end;log_message("ItemDump EC is only item in inv.");log_message("Expecting Fuel EC front. Inspecting...");local insp_s,front_d=turtle.inspect();if not insp_s then error_stop("No block front for Fuel EC. Reason: "..tostring(front_d))end;log_message("Block front:",front_d);if front_d.name:lower()~=ENDER_CHEST_ITEM_NAME:lower()then error_stop("Block front not EC. Found: "..front_d.name)end;log_message("Block front confirmed EC by name.");if not turtle.suck(64)then log_message("WARN: Fail suck front Fuel EC.")else log_message("Sucked from front Fuel EC.")end;local coal_after_s=nil;for i=1,16 do local it=turtle.getItemDetail(i)if it and it.name:lower()==COAL_BLOCK_ITEM_NAME:lower()and not(it.name:lower()==ENDER_CHEST_ITEM_NAME:lower()and it.nbt==ITEM_DUMP_ENDER_CHEST_NBT)then coal_after_s=i;break end end;if coal_after_s then turtle.select(coal_after_s);local ref_suck_r=turtle.refuel();if ref_suck_r==false then log_message("WARN: turtle.refuel() false after suck.")elseif type(ref_suck_r)=="number"then if ref_suck_r>0 then log_message("Refueled "..ref_suck_r.." from sucked.")else log_message("No fuel from sucked (full/no fuel).")end elseif ref_suck_r==true then log_message("Sucked consumed 0 fuel (full). OK.")else log_message("WARN: Unexpected refuel() ret: "..tostring(ref_suck_r))end else log_message("No coal after suck.")end;turtle.select(1);log_message("Digging front Fuel EC.");if not safe_dig()then error_stop("Fail dig front Fuel EC.")end;os.sleep(0.5);log_message("Finding NBT of dug Fuel EC item...");local fuel_ec_s,fuel_ec_d=nil,nil;for i=1,16 do local it=turtle.getItemDetail(i)if it and it.name:lower()==ENDER_CHEST_ITEM_NAME:lower()then if should_log_details()then log_message("  SP1: Chk inv slot "..i.." for new Fuel EC:",it)end;if it.nbt~=ITEM_DUMP_ENDER_CHEST_NBT then if fuel_ec_s then error_stop("Multiple 'new' EC items after dig front.")end;fuel_ec_s=i;fuel_ec_d=it;if should_log_details()then log_message("    Candidate Fuel EC item slot "..i)end else if should_log_details()then log_message("    Slot "..i.." is ItemDump EC, skip.")end end end end;if not fuel_ec_d or not fuel_ec_d.nbt then error_stop("Could not find Fuel EC item with NBT in inv after dig, or no NBT.",fuel_ec_d)end;FUEL_ENDER_CHEST_NBT=fuel_ec_d.nbt;log_message("Fuel EC NBT from item: "..FUEL_ENDER_CHEST_NBT);if FUEL_ENDER_CHEST_NBT==ITEM_DUMP_ENDER_CHEST_NBT then error_stop("Fuel EC has same NBT as ItemDump EC.")end;log_message("SP1 complete. Two ECs secured.")end
  49. local function update_dig_move_action_count() dig_and_move_actions_before_check = count_free_slots(); if should_log_details() then log_message("Inv space allows for " .. dig_and_move_actions_before_check .. " digs.") end end
  50. local function fuel_check()if should_log_details()then log_message("Fuel check...")end;local cur_f=turtle.getFuelLevel();local max_f=turtle.getFuelLimit();if max_f=="unlimited" or max_f==0 then if should_log_details()then log_message("Fuel unlimited/NA.")end;return end;local perc=(cur_f/max_f)*100; if should_log_details()then log_message("Fuel: "..cur_f.."/"..max_f.." ("..string.format("%.1f",perc).."%)")end;if perc<FUEL_ALERT_PERCENTAGE then log_message("Fuel low. Refueling...");if not FUEL_ENDER_CHEST_NBT then error_stop("No Fuel EC NBT for refueling.")end;local ec_s,_=find_item_in_inventory(function(it)return it.name:lower()==ENDER_CHEST_ITEM_NAME:lower()and it.nbt==FUEL_ENDER_CHEST_NBT end,"Fuel EC for refueling placement");if not ec_s then error_stop("Fuel EC (NBT "..tostring(FUEL_ENDER_CHEST_NBT)..") not in inv for refueling.")end;turn_around();safe_dig();safe_place(ec_s);log_message("Placed Fuel EC.");if not turtle.suck(1)then log_message("WARN: Failed suck Fuel EC. Retrieving.");if not safe_dig()then error_stop("CRIT: Fail dig Fuel EC (failed suck).")end;if not turtle.suck()then log_message("WARN: Suck after dig (failed suck) failed.")end;retrieve_specific_ender_chest_item(FUEL_ENDER_CHEST_NBT,"Fuel EC (failed suck)");turn_around();error_stop("Failed to suck fuel (initial attempt).")end;log_message("Sucked fuel item.");local coal_s=nil;for i=1,16 do local it=turtle.getItemDetail(i)if it and it.name:lower()==COAL_BLOCK_ITEM_NAME:lower()and not(it.name:lower()==ENDER_CHEST_ITEM_NAME:lower()and(it.nbt==ITEM_DUMP_ENDER_CHEST_NBT or it.nbt==FUEL_ENDER_CHEST_NBT))then coal_s=i;break end end;if not coal_s then log_message("No coal after suck. Retrieving Fuel EC.");if not safe_dig()then error_stop("CRIT: Fail dig Fuel EC (no coal).")end;if not turtle.suck()then log_message("WARN: Suck after dig (no coal) failed.")end;retrieve_specific_ender_chest_item(FUEL_ENDER_CHEST_NBT,"Fuel EC (no coal after suck)");turn_around();error_stop("No coal after suck for fuel.")end;local o_s=turtle.getSelectedSlot();turtle.select(coal_s);local ref_res=turtle.refuel();turtle.select(o_s);if ref_res==false then log_message("Refuel false. Retrieving EC.");if not safe_dig()then error_stop("CRIT: Fail dig Fuel EC (refuel fail).")end;if not turtle.suck()then log_message("WARN: Suck after dig (refuel fail) failed.")end;retrieve_specific_ender_chest_item(FUEL_ENDER_CHEST_NBT,"Fuel EC (refuel fail)");turn_around();error_stop("Fail refuel with sucked (false).")elseif type(ref_res)=="number"then if ref_res>0 then log_message("Refueled "..ref_res)else log_message("WARN: Refuel 0.")end elseif ref_res==true then log_message("Sucked consumed 0 fuel (full). OK.")else error_stop("Unexpected refuel() ret: "..tostring(ref_res))end;log_message("Digging Fuel EC to retrieve.");if not safe_dig()then error_stop("Fail dig Fuel EC after refuel.")end;if not turtle.suck()then log_message("WARN: Fail suck Fuel EC item after dig.")end;retrieve_specific_ender_chest_item(FUEL_ENDER_CHEST_NBT,"Fuel EC");turn_around();log_message("Refueling complete.")else if should_log_details()then log_message("Fuel level OK.")end end end
  51. local function item_deposit_check() if should_log_details()then log_message("Item deposit check...")end;local free=count_free_slots();if should_log_details()then log_message("Free slots: "..free)end;if free<MIN_FREE_SLOTS_FOR_OPERATION then log_message("Not enough free ("..free.."). Depositing...");if not ITEM_DUMP_ENDER_CHEST_NBT or not FUEL_ENDER_CHEST_NBT then error_stop("EC NBTs missing for deposit.")end;local dump_s,_=find_item_in_inventory(function(it)return it.name:lower()==ENDER_CHEST_ITEM_NAME:lower()and it.nbt==ITEM_DUMP_ENDER_CHEST_NBT end,"Item-Dump EC for deposit placement");if not dump_s then error_stop("Item-Dump EC (NBT "..tostring(ITEM_DUMP_ENDER_CHEST_NBT)..") not found.")end;turn_around();safe_dig();safe_place(dump_s);log_message("Placed Item-Dump EC.");local o_s=turtle.getSelectedSlot();for i=1,16 do turtle.select(i);local it_d=turtle.getItemDetail(i);if it_d and it_d.name~=AIR_ITEM_NAME then if not(it_d.name:lower()==ENDER_CHEST_ITEM_NAME:lower()and it_d.nbt==FUEL_ENDER_CHEST_NBT)then log_message("Depositing slot "..i..": "..it_d.name.." x"..it_d.count);local to_drop=it_d.count;for _=1,to_drop do if turtle.getItemCount(i)==0 then break end;if not turtle.drop()then log_message("WARN: Fail drop slot "..i..". Chest full?");break end end;if turtle.getItemCount(i)==0 then log_message("Slot "..i.." emptied.")else log_message("WARN: Slot "..i.." not empty. Rem: "..turtle.getItemCount(i))end else log_message("Skipping Fuel EC slot "..i)end end end;turtle.select(o_s);log_message("Digging Item-Dump EC.");if not safe_dig()then error_stop("Fail dig Item-Dump EC after deposit.")end;if not turtle.suck()then log_message("WARN: Fail suck Item-Dump EC after dig.")end;retrieve_specific_ender_chest_item(ITEM_DUMP_ENDER_CHEST_NBT,"Item-Dump Ender Chest");turn_around();log_message("Deposit done. Free: "..count_free_slots())else if should_log_details()then log_message("Sufficient free.")end end;update_dig_move_action_count()end
  52. local function system_check() log_message("--- Sys Check ---");fuel_check();item_deposit_check();log_message("--- Sys Check Done ---");if dig_and_move_actions_before_check<=0 then log_message("WARN: No actions after sys check.")end end
  53. local function calibrate_y_level() log_message("Y-cal: Digging to bedrock...");local d=0;while true do local b_s,b_d=turtle.inspectDown();if b_s and b_d.name:lower()==BEDROCK_ITEM_NAME:lower()then log_message("Bedrock below.");break end;if not safe_dig_down()then log_message("Bedrock/unbreakable by dig.");break end;if not safe_down()then error_stop("Y-cal: Failed move down.")end;d=d+1;if d%20==0 and should_log_details()then log_message("Y-cal: Descended "..d)end;if d>WORLD_BOTTOM_Y_CALIBRATION_DEPTH+200 then error_stop("Y-cal: Descended too far.")end end;log_message("Y-cal: Reached bedrock after "..d);log_message("Y-cal: Ascending "..WORLD_BOTTOM_Y_CALIBRATION_DEPTH);for i=1,WORLD_BOTTOM_Y_CALIBRATION_DEPTH do if not safe_dig_up()then error_stop("Y-cal: Failed dig up "..i)end;if not safe_up()then error_stop("Y-cal: Failed move up "..i)end;if i%20==0 and should_log_details()then log_message("Y-cal: Ascended "..i)end end;current_y_relative=0;is_y_calibrated=true;log_message("Y-cal: Complete. Y=0.") end
  54. local function mine_shaft_down() if should_log_details()then log_message("Mine Down: Y="..current_y_relative)end;while true do if dig_and_move_actions_before_check<=0 then system_check()if dig_and_move_actions_before_check<=0 then error_stop("No actions after syscheck in mine_shaft_down.")end end;local b_s,b_d=turtle.inspectDown();if b_s and b_d.name:lower()==BEDROCK_ITEM_NAME:lower()then if should_log_details()then log_message("Mine Down: Bedrock below.")end;break end;if not safe_dig_down()then if should_log_details()then log_message("Mine Down: Bedrock/unbreakable by dig.")end;break end;decrement_dig_move_actions();if not safe_down()then error_stop("Mine Down: Failed move down. Y="..current_y_relative)end;current_y_relative=current_y_relative-1;if current_y_relative<-(WORLD_BOTTOM_Y_CALIBRATION_DEPTH+250)then log_message("WARN: Mine Down: Very low Y "..current_y_relative);break end end;if should_log_details()then log_message("Mine Down: Finished. Y="..current_y_relative)end end
  55. local function mine_shaft_up() if should_log_details()then log_message("Mine Up: To Y=0 from Y="..current_y_relative)end;while current_y_relative<0 do if dig_and_move_actions_before_check<=0 then system_check()if dig_and_move_actions_before_check<=0 then error_stop("No actions after syscheck in mine_shaft_up.")end end;if not safe_dig_up()then error_stop("Mine Up: Failed dig up. Y="..current_y_relative)end;decrement_dig_move_actions();if not safe_up()then error_stop("Mine Up: Failed move up. Y="..current_y_relative)end;current_y_relative=current_y_relative+1;if current_y_relative>50 then log_message("WARN: Mine Up: Too far up "..current_y_relative);break end end;if current_y_relative==0 then if should_log_details()then log_message("Mine Up: Reached Y=0.")end else log_message("WARN: Mine Up: Not Y=0. Y="..current_y_relative)end end
  56.  
  57. --------------------------------------------------------------------------------
  58. -- Main Program Logic: Startup & Loop -- ***** CORRECTED MAIN LOOP DIG BEFORE MOVE *****
  59. --------------------------------------------------------------------------------
  60. log_message("--- Initializing Miner: Determining Start Scenario ---")
  61. local inv_item_dump_ec_slot=nil; local inv_coal_block_slot=nil; local inv_chunk_loader_slot=nil
  62. local inv_pickaxe_candidate_slots={}; local inv_ender_chest_details={}
  63. log_message("Scanning inventory slots 1-16 for initial items...")
  64. for i=1,16 do local item=turtle.getItemDetail(i)
  65.     if item and item.name~=AIR_ITEM_NAME then if should_log_details()then log_message("  Slot "..i..":", item)end
  66.         if item.name:lower()==ENDER_CHEST_ITEM_NAME:lower() then table.insert(inv_ender_chest_details,{slot=i,detail=item})
  67.             if item.nbt==ITEM_DUMP_ENDER_CHEST_NBT then if inv_item_dump_ec_slot then error_stop("SP Scan: Duplicate Item-Dump ECs.")end;inv_item_dump_ec_slot=i;if should_log_details()then log_message("    Found Item-Dump EC in slot "..i)end end
  68.         elseif item.name:lower()==COAL_BLOCK_ITEM_NAME:lower() then if inv_coal_block_slot then if should_log_details()then log_message("  WARN: Multiple coal blocks.")end else inv_coal_block_slot=i end
  69.         elseif item.name:lower()==CHUNK_CONTROLLER_ITEM_NAME:lower() then if inv_chunk_loader_slot then if should_log_details()then log_message("  WARN: Multiple chunk controllers.")end else inv_chunk_loader_slot=i end
  70.         end
  71.         if item.maxCount==1 then if should_log_details()then log_message("    Slot "..i.." ("..item.name..") maxCount==1. Add to raw pick candidates.")end;table.insert(inv_pickaxe_candidate_slots,i)
  72.         elseif string.find(item.name:lower(),"pickaxe")then if should_log_details()then log_message("    Slot "..i.." ("..item.name..") name has 'pickaxe' (maxCount "..tostring(item.maxCount).."). Add to raw pick candidates.")end;table.insert(inv_pickaxe_candidate_slots,i)end
  73.     end
  74. end
  75. if should_log_details()then log_message("  Raw inv_pickaxe_candidate_slots: {"..table.concat(inv_pickaxe_candidate_slots,", ").."}")end
  76. local actual_pickaxe_slot_for_sp1=nil
  77. if #inv_pickaxe_candidate_slots>0 then local filt={};if should_log_details()then log_message("  Filtering pickaxe candidates for SP1:")end
  78.     for _,p_s in ipairs(inv_pickaxe_candidate_slots)do local it_d=turtle.getItemDetail(p_s);local c_n=it_d and it_d.name or "Unk";if should_log_details()then log_message("    Cand slot "..p_s.." ("..c_n.."):")end
  79.         local is_dump=(inv_item_dump_ec_slot and p_s==inv_item_dump_ec_slot);local is_cc=(inv_chunk_loader_slot and p_s==inv_chunk_loader_slot)
  80.         if should_log_details()then log_message("      IsDumpEC? "..tostring(is_dump).." IsCC? "..tostring(is_cc))end
  81.         if not(is_dump or is_cc)then table.insert(filt,p_s);if should_log_details()then log_message("      Added slot "..p_s)end else if should_log_details()then log_message("      Skipped slot "..p_s)end end
  82.     end;if should_log_details()then log_message("  Final filtered: {"..table.concat(filt,", ").."}")end
  83.     if #filt==1 then actual_pickaxe_slot_for_sp1=filt[1]elseif #filt>1 then log_message("WARN: Multi pickaxe cands for SP1: "..table.concat(filt,", "))else if should_log_details()then log_message("INFO: No suitable pickaxe cand after filter SP1.")end end
  84. else if should_log_details()then log_message("  No raw pickaxe cands found.")end end
  85. log_message("Initial inventory scan results summary:")
  86. log_message("  Item-Dump EC (NBT "..ITEM_DUMP_ENDER_CHEST_NBT..") in inv slot: "..tostring(inv_item_dump_ec_slot))
  87. log_message("  Coal Block in inv slot: "..tostring(inv_coal_block_slot))
  88. log_message("  Chunk Controller in inv slot: "..tostring(inv_chunk_loader_slot))
  89. log_message("  Actual Pickaxe candidate for SP1 in inv slot: "..tostring(actual_pickaxe_slot_for_sp1))
  90. log_message("  Total Ender Chests in inventory: "..#inv_ender_chest_details)
  91. for idx,ec_e in ipairs(inv_ender_chest_details)do log_message("    EC #"..idx.." slot "..ec_e.slot..": "..ec_e.detail.name.." (NBT: "..tostring(ec_e.detail.nbt)..")")end
  92. log_message("Checking equipped items (left/right hand)...")
  93. local eq_left_detail=get_item_from_hand_temporarily("left"); local eq_right_detail=get_item_from_hand_temporarily("right")
  94. local is_pick_eq=(eq_left_detail and eq_left_detail.name~=AIR_ITEM_NAME and (eq_left_detail.maxCount==1 or string.find(eq_left_detail.name:lower(),"pickaxe")))
  95. local is_cc_eq=(eq_right_detail and eq_right_detail.name:lower()==CHUNK_CONTROLLER_ITEM_NAME:lower())
  96. log_message("Equipped items check summary:")
  97. log_message("  Pickaxe effectively in left hand: "..tostring(is_pick_eq)..(is_pick_eq and(" ("..eq_left_detail.name..")")or""))
  98. log_message("  Chunk Loader effectively in right hand: "..tostring(is_cc_eq)..(is_cc_eq and(" ("..eq_right_detail.name..")")or""))
  99. local sp1_met=inv_item_dump_ec_slot and inv_coal_block_slot and inv_chunk_loader_slot and actual_pickaxe_slot_for_sp1 and #inv_ender_chest_details==1 and(inv_ender_chest_details[1]and inv_ender_chest_details[1].slot==inv_item_dump_ec_slot)and not is_pick_eq and not is_cc_eq
  100. if sp1_met then log_message("Start Point 1 conditions met. Proceeding...")
  101.     perform_initial_setup(inv_item_dump_ec_slot,inv_coal_block_slot,inv_chunk_loader_slot,actual_pickaxe_slot_for_sp1)
  102. else log_message("Start Point 1 conditions NOT met. Checking for Start Point 2...")
  103.     local fuel_ec_d_sp2=nil; local dump_ec_pres_sp2=false
  104.     if #inv_ender_chest_details==2 then for _,ec_e in ipairs(inv_ender_chest_details)do
  105.         if ec_e.detail.nbt==ITEM_DUMP_ENDER_CHEST_NBT then dump_ec_pres_sp2=true
  106.         elseif ec_e.detail.nbt and ec_e.detail.nbt~=ITEM_DUMP_ENDER_CHEST_NBT then if fuel_ec_d_sp2 then error_stop("SP2: Multi candidate Fuel ECs.")end;fuel_ec_d_sp2=ec_e.detail end
  107.     end end
  108.     local sp2_met=is_pick_eq and is_cc_eq and dump_ec_pres_sp2 and fuel_ec_d_sp2
  109.     if sp2_met then log_message("Start Point 2 conditions met.")
  110.         FUEL_ENDER_CHEST_NBT=fuel_ec_d_sp2.nbt; log_message("Fuel EC NBT (SP2 inv): "..FUEL_ENDER_CHEST_NBT)
  111.         if not find_item_in_inventory(function(it)return it.name:lower()==ENDER_CHEST_ITEM_NAME:lower()and it.nbt==ITEM_DUMP_ENDER_CHEST_NBT end,"SP2 ItemDump EC Sanity Check")then error_stop("SP2 Sanity: Item-Dump EC unfindable.")end
  112.         log_message("Start Point 2 confirmed.")
  113.     else log_message("Start Point 2 conditions NOT met.")
  114.         log_message("--- Start Scenario Debug Dump ---")
  115.         log_message("  SP1 Met: "..tostring(sp1_met)); log_message("  SP2 Met: "..tostring(sp2_met))
  116.         log_message("  Inv DumpEC Slot: "..tostring(inv_item_dump_ec_slot)); log_message("  Inv Coal Slot: "..tostring(inv_coal_block_slot))
  117.         log_message("  Inv CC Slot: "..tostring(inv_chunk_loader_slot)); log_message("  Inv Pickaxe SP1 Slot: "..tostring(actual_pickaxe_slot_for_sp1))
  118.         log_message("  Inv #ECs: "..#inv_ender_chest_details); log_message("  is_pick_eq: "..tostring(is_pick_eq)); log_message("  is_cc_eq: "..tostring(is_cc_eq))
  119.         log_message("  SP2 DumpEC Pres: "..tostring(dump_ec_pres_sp2)); log_message("  SP2 FuelEC Detail: "..((fuel_ec_d_sp2 and fuel_ec_d_sp2.name.." NBT:"..tostring(fuel_ec_d_sp2.nbt))or"nil"))
  120.         error_stop("Unable to determine valid start scenario. Check inv/equip and log.")
  121.     end
  122. end
  123.  
  124. system_check()
  125. if not is_y_calibrated then calibrate_y_level(); system_check(); end
  126.  
  127. log_message("--- Starting Main Mining Loop ---")
  128. main_loop_cycle_count_for_log = 0
  129. while true do
  130.     main_loop_cycle_count_for_log = main_loop_cycle_count_for_log + 1
  131.     log_message("\n--- Cycle #"..main_loop_cycle_count_for_log.." ---")
  132.     if should_log_details() then log_message("Y: "..current_y_relative..". Actions before check: "..dig_and_move_actions_before_check) end
  133.    
  134.     if dig_and_move_actions_before_check <= 0 then system_check(); if dig_and_move_actions_before_check <= 0 then error_stop("No actions after syscheck main loop start.")end end
  135.    
  136.     if should_log_details() then log_message("Digging fwd then moving fwd (at Y="..current_y_relative..").") end
  137.     safe_dig() -- Dig before moving forward at current Y. Returns true if air/success, false if bedrock.
  138.     if not safe_forward()then error_stop("Fail fwd at mining Y-level after dig attempt.")end
  139.     decrement_dig_move_actions()
  140.  
  141.     mine_shaft_down()
  142.    
  143.     if dig_and_move_actions_before_check <= 0 then system_check(); if dig_and_move_actions_before_check <= 0 then error_stop("No actions after syscheck bedrock fwd.")end end
  144.    
  145.     if should_log_details() then log_message("Digging fwd then moving fwd (at bedrock Y="..current_y_relative..").") end
  146.     safe_dig() -- Dig before moving forward at bedrock Y.
  147.     if not safe_forward()then error_stop("Fail fwd at bedrock level after dig attempt.")end
  148.     decrement_dig_move_actions()
  149.  
  150.     mine_shaft_up()
  151.     log_message("Cycle #"..main_loop_cycle_count_for_log.." completed.")
  152. end
Add Comment
Please, Sign In to add comment