mining_setup.lua
340 lines ยท 10.1 KB
eHydra Mining Setup System
Copy & run
wget https://perlytiara.github.io/turtles.tips/raw/programs/perlytiara/eHydra_wip/mining_setup.lua
| 1 | -- eHydra Mining Setup System |
| 2 | -- Complete mining infrastructure deployment with GPS positioning |
| 3 | |
| 4 | local SLOT_COUNT = 16 |
| 5 | |
| 6 | -- Infrastructure items needed for mining setup |
| 7 | local REQUIRED_ITEMS = { |
| 8 | turtles = {"turtle", 1}, |
| 9 | diskDrives = {"disk_drive", 1}, |
| 10 | disks = {"floppy_disk", 1}, |
| 11 | computers = {"computer", 1}, -- For monitoring/control |
| 12 | chests = {"chest", 3}, -- Coal chest, storage chest, spare chest |
| 13 | coal = {"coal", 64}, -- Fuel supply |
| 14 | enderChests = {"ender", 1} -- Optional for remote storage |
| 15 | } |
| 16 | |
| 17 | local function scanForMiningSetup() |
| 18 | print("๐ Scanning for complete mining setup components...") |
| 19 | |
| 20 | local inventory = {} |
| 21 | local missing = {} |
| 22 | |
| 23 | for category, requirement in pairs(REQUIRED_ITEMS) do |
| 24 | local pattern, minCount = requirement[1], requirement[2] |
| 25 | local found = {} |
| 26 | |
| 27 | for slot = 1, SLOT_COUNT do |
| 28 | local item = turtle.getItemDetail(slot) |
| 29 | if item and item.name:find(pattern) then |
| 30 | table.insert(found, {slot = slot, name = item.name, count = item.count}) |
| 31 | end |
| 32 | end |
| 33 | |
| 34 | inventory[category] = found |
| 35 | local totalCount = 0 |
| 36 | for _, item in ipairs(found) do |
| 37 | totalCount = totalCount + item.count |
| 38 | end |
| 39 | |
| 40 | if totalCount < minCount then |
| 41 | table.insert(missing, category .. " (need " .. minCount .. ", have " .. totalCount .. ")") |
| 42 | end |
| 43 | end |
| 44 | |
| 45 | return inventory, missing |
| 46 | end |
| 47 | |
| 48 | local function getGPSPosition() |
| 49 | print("๐ Getting GPS coordinates...") |
| 50 | local x, y, z = gps.locate(5, false) |
| 51 | |
| 52 | if x then |
| 53 | print("โ GPS Location: " .. x .. ", " .. y .. ", " .. z) |
| 54 | return {x = math.floor(x), y = math.floor(y), z = math.floor(z)} |
| 55 | else |
| 56 | print("โ GPS not available - using manual positioning") |
| 57 | return nil |
| 58 | end |
| 59 | end |
| 60 | |
| 61 | local function calculateMiningArea(basePos, width, depth, height) |
| 62 | if not basePos then |
| 63 | print("โ ๏ธ No GPS - please manually position setup") |
| 64 | return nil |
| 65 | end |
| 66 | |
| 67 | local area = { |
| 68 | start = {x = basePos.x, y = basePos.y - height, z = basePos.z}, |
| 69 | finish = {x = basePos.x + width - 1, y = basePos.y - 1, z = basePos.z + depth - 1}, |
| 70 | control = {x = basePos.x - 2, y = basePos.y + 1, z = basePos.z - 2}, -- Control station position |
| 71 | coalChest = {x = basePos.x - 1, y = basePos.y, z = basePos.z - 1}, |
| 72 | storageChest = {x = basePos.x - 1, y = basePos.y, z = basePos.z + 1} |
| 73 | } |
| 74 | |
| 75 | return area |
| 76 | end |
| 77 | |
| 78 | local function deployInfrastructure(inventory, area) |
| 79 | print("๐๏ธ Deploying mining infrastructure...") |
| 80 | |
| 81 | local currentX, currentY, currentZ = gps.locate() |
| 82 | if not currentX then |
| 83 | print("โ Cannot deploy without GPS") |
| 84 | return false |
| 85 | end |
| 86 | |
| 87 | -- Deploy coal chest |
| 88 | print("โฝ Placing coal chest...") |
| 89 | -- Navigate to coal chest position |
| 90 | -- Place chest and fill with coal |
| 91 | turtle.select(inventory.chests[1].slot) |
| 92 | turtle.place() |
| 93 | |
| 94 | -- Fill coal chest |
| 95 | if #inventory.coal > 0 then |
| 96 | turtle.select(inventory.coal[1].slot) |
| 97 | turtle.drop(32) -- Drop half the coal |
| 98 | print("โ Coal chest stocked") |
| 99 | end |
| 100 | |
| 101 | -- Deploy storage chest |
| 102 | print("๐ฆ Placing storage chest...") |
| 103 | -- Navigate to storage position and place |
| 104 | turtle.select(inventory.chests[2].slot) |
| 105 | turtle.place() |
| 106 | |
| 107 | -- Deploy control computer |
| 108 | print("๐ฅ๏ธ Placing control computer...") |
| 109 | turtle.select(inventory.computers[1].slot) |
| 110 | turtle.place() |
| 111 | |
| 112 | -- Setup disk with mining program |
| 113 | turtle.select(inventory.diskDrives[1].slot) |
| 114 | turtle.placeUp() |
| 115 | |
| 116 | turtle.select(inventory.disks[1].slot) |
| 117 | turtle.dropUp() |
| 118 | |
| 119 | return true |
| 120 | end |
| 121 | |
| 122 | local function createMiningProgram(area, miningProgram, parameters) |
| 123 | print("๐พ Creating mining program disk...") |
| 124 | |
| 125 | if not fs.exists("disk") then |
| 126 | print("โ Disk not mounted") |
| 127 | return false |
| 128 | end |
| 129 | |
| 130 | -- Create comprehensive mining startup |
| 131 | local startupFile = fs.open("disk/startup", "w") |
| 132 | if not startupFile then |
| 133 | return false |
| 134 | end |
| 135 | |
| 136 | startupFile.write(string.format([[ |
| 137 | -- eHydra Complete Mining Setup |
| 138 | print("๐๏ธ eHydra Mining Setup v1.0") |
| 139 | print("=============================") |
| 140 | |
| 141 | -- Get position |
| 142 | local x, y, z = gps.locate(3, false) |
| 143 | if x then |
| 144 | print("๐ Position: " .. x .. ", " .. y .. ", " .. z) |
| 145 | else |
| 146 | print("๐ GPS not available") |
| 147 | end |
| 148 | |
| 149 | -- Setup mining parameters |
| 150 | local miningArea = { |
| 151 | start = {x = %d, y = %d, z = %d}, |
| 152 | finish = {x = %d, y = %d, z = %d} |
| 153 | } |
| 154 | |
| 155 | -- Copy mining program if available |
| 156 | if fs.exists("disk/%s") then |
| 157 | fs.copy("disk/%s", "%s") |
| 158 | print("โ Mining program installed: %s") |
| 159 | end |
| 160 | |
| 161 | -- Setup fuel management |
| 162 | local function refuelFromChest() |
| 163 | -- Look for coal chest nearby |
| 164 | for _, direction in ipairs({"front", "back", "left", "right", "up", "down"}) do |
| 165 | if peripheral.isPresent(direction) and peripheral.getType(direction) == "minecraft:chest" then |
| 166 | local chest = peripheral.wrap(direction) |
| 167 | local items = chest.list() |
| 168 | for slot, item in pairs(items) do |
| 169 | if item.name:find("coal") then |
| 170 | chest.pushItems(peripheral.getName(turtle), slot, math.min(item.count, 32), 1) |
| 171 | turtle.select(1) |
| 172 | if turtle.refuel() then |
| 173 | print("โฝ Refueled from chest") |
| 174 | return true |
| 175 | end |
| 176 | end |
| 177 | end |
| 178 | end |
| 179 | end |
| 180 | return false |
| 181 | end |
| 182 | |
| 183 | -- Setup inventory management |
| 184 | local function depositToStorage() |
| 185 | -- Find storage chest and deposit items |
| 186 | for _, direction in ipairs({"front", "back", "left", "right", "up", "down"}) do |
| 187 | if peripheral.isPresent(direction) and peripheral.getType(direction) == "minecraft:chest" then |
| 188 | local chest = peripheral.wrap(direction) |
| 189 | for slot = 2, 16 do -- Keep slot 1 for fuel |
| 190 | local item = turtle.getItemDetail(slot) |
| 191 | if item and not item.name:find("coal") then |
| 192 | turtle.select(slot) |
| 193 | turtle.drop() |
| 194 | end |
| 195 | end |
| 196 | end |
| 197 | end |
| 198 | end |
| 199 | |
| 200 | -- Send status updates |
| 201 | local function sendStatus(message) |
| 202 | local modem = peripheral.find("modem") |
| 203 | if modem then |
| 204 | rednet.open(peripheral.getName(modem)) |
| 205 | rednet.broadcast({ |
| 206 | id = os.getComputerID(), |
| 207 | status = message, |
| 208 | position = {x = x, y = y, z = z}, |
| 209 | fuel = turtle.getFuelLevel(), |
| 210 | time = os.time() |
| 211 | }, "ehydra_mining") |
| 212 | print("๐ก Status sent: " .. message) |
| 213 | end |
| 214 | end |
| 215 | |
| 216 | -- Main mining operation |
| 217 | sendStatus("MINING_STARTED") |
| 218 | |
| 219 | -- Initial refuel |
| 220 | refuelFromChest() |
| 221 | |
| 222 | -- Start mining program |
| 223 | if fs.exists("%s") then |
| 224 | print("๐ Starting mining: %s %s") |
| 225 | shell.run("%s", "%s") |
| 226 | else |
| 227 | print("โ Mining program not found") |
| 228 | sendStatus("ERROR_NO_PROGRAM") |
| 229 | end |
| 230 | |
| 231 | sendStatus("MINING_COMPLETED") |
| 232 | ]], |
| 233 | area.start.x, area.start.y, area.start.z, |
| 234 | area.finish.x, area.finish.y, area.finish.z, |
| 235 | miningProgram, miningProgram, miningProgram, miningProgram, |
| 236 | miningProgram, miningProgram, parameters or "" |
| 237 | )) |
| 238 | |
| 239 | startupFile.close() |
| 240 | |
| 241 | -- Copy the actual mining program to disk |
| 242 | if fs.exists(miningProgram .. ".lua") then |
| 243 | fs.copy(miningProgram .. ".lua", "disk/" .. miningProgram) |
| 244 | print("โ Mining program copied to disk") |
| 245 | end |
| 246 | |
| 247 | return true |
| 248 | end |
| 249 | |
| 250 | local function deployCompleteMiningSetup(program, parameters, dimensions) |
| 251 | print("๐๏ธ eHydra Complete Mining Setup") |
| 252 | print("================================") |
| 253 | |
| 254 | -- Get current position |
| 255 | local basePos = getGPSPosition() |
| 256 | if not basePos then |
| 257 | print("โ GPS required for automated setup") |
| 258 | return false |
| 259 | end |
| 260 | |
| 261 | -- Scan inventory |
| 262 | local inventory, missing = scanForMiningSetup() |
| 263 | |
| 264 | if #missing > 0 then |
| 265 | print("โ Missing required items:") |
| 266 | for _, item in ipairs(missing) do |
| 267 | print(" โข " .. item) |
| 268 | end |
| 269 | return false |
| 270 | end |
| 271 | |
| 272 | print("โ All required items available") |
| 273 | |
| 274 | -- Calculate mining area |
| 275 | local width, depth, height = dimensions.width or 16, dimensions.depth or 16, dimensions.height or 10 |
| 276 | local area = calculateMiningArea(basePos, width, depth, height) |
| 277 | |
| 278 | print("๐ Mining Area Calculated:") |
| 279 | print(" Start: " .. area.start.x .. ", " .. area.start.y .. ", " .. area.start.z) |
| 280 | print(" Finish: " .. area.finish.x .. ", " .. area.finish.y .. ", " .. area.finish.z) |
| 281 | print(" Size: " .. width .. "x" .. depth .. "x" .. height) |
| 282 | |
| 283 | -- Deploy infrastructure |
| 284 | if not deployInfrastructure(inventory, area) then |
| 285 | print("โ Infrastructure deployment failed") |
| 286 | return false |
| 287 | end |
| 288 | |
| 289 | -- Create mining program |
| 290 | if not createMiningProgram(area, program, parameters) then |
| 291 | print("โ Mining program setup failed") |
| 292 | return false |
| 293 | end |
| 294 | |
| 295 | -- Deploy turtle |
| 296 | print("๐ข Deploying mining turtle...") |
| 297 | turtle.select(inventory.turtles[1].slot) |
| 298 | turtle.place() |
| 299 | peripheral.call("front", "turnOn") |
| 300 | |
| 301 | print("โ Complete mining setup deployed!") |
| 302 | print(" ๐ข Turtle: Deployed and starting") |
| 303 | print(" โฝ Coal chest: Stocked") |
| 304 | print(" ๐ฆ Storage chest: Ready") |
| 305 | print(" ๐ฅ๏ธ Control computer: Active") |
| 306 | print(" ๐ก Monitoring: Enabled") |
| 307 | |
| 308 | return true |
| 309 | end |
| 310 | |
| 311 | -- Parse command line arguments |
| 312 | local args = {...} |
| 313 | local program = args[1] or "quarry" |
| 314 | local width = tonumber(args[2]) or 16 |
| 315 | local depth = tonumber(args[3]) or 16 |
| 316 | local height = tonumber(args[4]) or 10 |
| 317 | local parameters = table.concat({table.unpack(args, 5)}, " ") |
| 318 | |
| 319 | print("๐๏ธ eHydra Mining Setup Configuration") |
| 320 | print("====================================") |
| 321 | print("Program: " .. program) |
| 322 | print("Dimensions: " .. width .. "x" .. depth .. "x" .. height) |
| 323 | if parameters ~= "" then |
| 324 | print("Parameters: " .. parameters) |
| 325 | end |
| 326 | |
| 327 | print() |
| 328 | write("Proceed with setup? (y/n) [y]: ") |
| 329 | local confirm = string.lower(read()) |
| 330 | |
| 331 | if confirm ~= "n" then |
| 332 | deployCompleteMiningSetup(program, parameters, { |
| 333 | width = width, |
| 334 | depth = depth, |
| 335 | height = height |
| 336 | }) |
| 337 | else |
| 338 | print("Setup cancelled") |
| 339 | end |
| 340 |