Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local configDir = "AxoTurtleConfig"
- local devOffFile = configDir .. "/dev_off"
- local configFile = configDir .. "/config"
- -- Function to create a directory if it doesn't exist
- local function createDirIfNotExists(dir)
- if not fs.exists(dir) then
- fs.makeDir(dir)
- end
- end
- -- Function to write the development status file
- local function writeDevStatus(status)
- local file = fs.open(devOffFile, "w")
- file.write(status)
- file.close()
- end
- -- Function to read the configuration file
- local function readConfig()
- if not fs.exists(configFile) then
- return {}
- end
- local file = fs.open(configFile, "r")
- local content = file.readAll()
- file.close()
- return textutils.unserialize(content)
- end
- -- Function to write the configuration file
- local function writeConfig(config)
- local file = fs.open(configFile, "w")
- file.write(textutils.serialize(config))
- file.close()
- end
- -- Function to get the turtle's current location using GPS
- local function getLocation()
- local x, y, z = gps.locate()
- return {x = x, y = y, z = z}
- end
- -- Function to move the turtle to a specific location (assuming GPS is available)
- local function goTo(targetLocation)
- local currentLocation = getLocation()
- local xDiff = targetLocation.x - currentLocation.x
- local yDiff = targetLocation.y - currentLocation.y
- local zDiff = targetLocation.z - currentLocation.z
- -- Adjust the turtle's Y position (up/down) first
- if yDiff > 0 then
- for i = 1, yDiff do
- turtle.up()
- end
- elseif yDiff < 0 then
- for i = 1, -yDiff do
- turtle.down()
- end
- end
- -- Adjust the turtle's X position (east/west)
- if xDiff > 0 then
- turtle.turnTo(0) -- east
- for i = 1, xDiff do
- turtle.forward()
- end
- elseif xDiff < 0 then
- turtle.turnTo(2) -- west
- for i = 1, -xDiff do
- turtle.forward()
- end
- end
- -- Adjust the turtle's Z position (north/south)
- if zDiff > 0 then
- turtle.turnTo(1) -- south
- for i = 1, zDiff do
- turtle.forward()
- end
- elseif zDiff < 0 then
- turtle.turnTo(3) -- north
- for i = 1, -zDiff do
- turtle.forward()
- end
- end
- end
- -- Function to create a base
- local function createBase()
- print("Creating base...")
- -- Dig below and place a crafting table
- turtle.digDown()
- turtle.select(1) -- Assuming the crafting table is in slot 1
- turtle.placeDown()
- -- Place a chest south of the crafting table
- turtle.turnRight()
- turtle.turnRight()
- turtle.forward()
- turtle.select(2) -- Assuming the chest is in slot 2
- turtle.place()
- -- Save base location to config
- config.baseLocation = getLocation()
- writeConfig(config)
- print("Base created and location saved.")
- end
- -- Function to restock fuel
- local function restock()
- local fuelLevel = turtle.getFuelLevel()
- local requiredFuel = 500 -- Example threshold
- if fuelLevel < requiredFuel then
- print("Refueling...")
- turtle.select(3) -- Assuming coal is in slot 3
- while turtle.getFuelLevel() < requiredFuel and turtle.getItemCount() > 0 do
- turtle.refuel(1)
- end
- if turtle.getFuelLevel() < requiredFuel then
- print("Searching for more coal in base chest...")
- -- Assuming the chest is south of the base location
- goTo(config.baseLocation)
- turtle.turnRight()
- turtle.turnRight()
- turtle.forward()
- turtle.suck() -- Attempt to take coal from the chest
- turtle.turnRight()
- turtle.turnRight()
- turtle.back()
- turtle.select(3)
- while turtle.getFuelLevel() < requiredFuel and turtle.getItemCount() > 0 do
- turtle.refuel(1)
- end
- end
- print("Refuel complete. Current fuel level:", turtle.getFuelLevel())
- else
- print("Fuel level sufficient:", turtle.getFuelLevel())
- end
- end
- -- Function to scavenge for ores
- local function scavenge()
- local baseLocation = config.baseLocation
- if not baseLocation then
- print("Base location not set. Create a base first.")
- return
- end
- print("Scavenging for ores...")
- while true do
- if turtle.getFuelLevel() < turtle.getFuelLimit() / 4 then
- print("Low fuel. Returning to base...")
- goTo(baseLocation)
- turtle.turnRight()
- turtle.turnRight()
- turtle.forward()
- turtle.drop()
- restock()
- return
- end
- turtle.forward()
- -- Check for ores and collect
- local success, data = turtle.inspect()
- if success and (data.name == "minecraft:iron_ore" or data.name == "minecraft:coal_ore") then
- turtle.dig()
- turtle.suck()
- end
- -- Return to base if inventory is full
- if turtle.getItemCount(16) > 0 then
- print("Inventory full. Returning to base...")
- goTo(baseLocation)
- turtle.turnRight()
- turtle.turnRight()
- turtle.forward()
- turtle.drop()
- restock()
- return
- end
- end
- end
- -- Function to handle user commands
- local function handleCommand(command)
- if command == "create base" then
- createBase()
- elseif command == "restock" then
- restock()
- elseif command == "scavenge" then
- scavenge()
- else
- print("Unknown command:", command)
- end
- end
- -- Phase 1: Launch Phase
- local function launchPhase()
- print(config.launchMessage or "Running Launch Phase...")
- -- Insert launch phase logic here
- end
- -- Phase 2: Idle Phase
- local function idlePhase()
- print(config.idleMessage or "Entering Idle Phase...")
- while true do
- -- Wait for user input or other events
- local event, param = os.pullEvent("key")
- if event == "key" then
- local key = keys.getName(param)
- if key == "enter" then
- print("Enter command:")
- local command = read()
- handleCommand(command)
- end
- end
- end
- end
- -- Phase 3: Action Phase
- local function actionPhase()
- print(config.actionMessage or "Performing Action...")
- -- Insert action phase logic here
- end
- -- Create configuration directory and dev_off file
- createDirIfNotExists(configDir)
- writeDevStatus("off")
- -- Initial configuration
- local initialConfig = {
- launchMessage = "Welcome to AxoTurtle!",
- idleMessage = "Waiting for input...",
- actionMessage = "Action performed!",
- baseLocation = nil
- }
- writeConfig(initialConfig)
- -- Modify the startup script
- local startupScript = "/startup"
- local startupCode = [[
- -- Existing startup code if any
- if fs.exists("startup_backup") then
- shell.run("startup_backup")
- end
- -- AxoTurtle startup additions
- local devStatusFile = "]] .. devOffFile .. [["
- local configFile = "]] .. configFile .. [["
- local function getDevStatus()
- if not fs.exists(devStatusFile) then
- return "off"
- end
- local file = fs.open(devStatusFile, "r")
- local status = file.readAll()
- file.close()
- return status:match("^%s*(.-)%s*$") -- trim whitespace
- end
- local function readConfig()
- if not fs.exists(configFile) then
- return {}
- end
- local file = fs.open(configFile, "r")
- local content = file.readAll()
- file.close()
- return textutils.unserialize(content)
- end
- local function writeConfig(config)
- local file = fs.open(configFile, "w")
- file.write(textutils.serialize(config))
- file.close()
- end
- local function getLocation()
- local x, y, z = gps.locate()
- return {x = x, y = y, z = z}
- end
- local function goTo(targetLocation)
- local currentLocation = getLocation()
- local xDiff = targetLocation.x - currentLocation.x
- local yDiff = targetLocation.y - currentLocation.y
- local zDiff = targetLocation.z - currentLocation.z
- -- Adjust the turtle's Y position (up/down) first
- if yDiff > 0 then
- for i = 1, yDiff do
- turtle.up()
- end
- elseif yDiff < 0 then
- for i = 1, -yDiff do
- turtle.down()
- end
- end
- -- Adjust the turtle's X position (east/west)
- if xDiff > 0 then
- turtle.turnTo(0) -- east
- for i = 1, xDiff do
- turtle.forward()
- end
- elseif xDiff < 0 then
- turtle.turnTo(2) -- west
- for i = 1, -xDiff do
- turtle.forward()
- end
- end
- -- Adjust the turtle's Z position (north/south)
- if zDiff > 0 then
- turtle.turnTo(1) -- south
- for i = 1, zDiff do
- turtle.forward()
- end
- elseif zDiff < 0 then
- turtle.turnTo(3) -- north
- for i = 1, -zDiff do
- turtle.forward()
- end
- end
- end
- local function createBase()
- print("Creating base...")
- turtle.digDown()
- turtle.select(1)
- turtle.placeDown()
- turtle.turnRight()
- turtle.turnRight()
- turtle.forward()
- turtle.select(2)
- turtle.place()
- config.baseLocation = getLocation()
- writeConfig(config)
- print("Base created and location saved.")
- end
- local function restock()
- local fuelLevel = turtle.getFuelLevel()
- local requiredFuel = 500
- if fuelLevel < requiredFuel then
- print("Refueling...")
- turtle.select(3)
- while turtle.getFuelLevel() < requiredFuel and turtle.getItemCount() > 0 do
- turtle.refuel(1)
- end
- if turtle.getFuelLevel() < requiredFuel then
- print("Searching for more coal in base chest...")
- goTo(config.baseLocation)
- turtle.turnRight()
- turtle.turnRight()
- turtle.forward()
- turtle.suck()
- turtle.turnRight()
- turtle.turnRight()
- turtle.back()
- turtle.select(3)
- while turtle.getFuelLevel() < requiredFuel and turtle.getItemCount() > 0 do
- turtle.refuel(1)
- end
- end
- print("Refuel complete. Current fuel level:", turtle.getFuelLevel())
- else
- print("Fuel level sufficient:", turtle.getFuelLevel())
- end
- end
- local function scavenge()
- local baseLocation = config.baseLocation
- if not baseLocation then
- print("Base location not set. Create a base first.")
- return
- end
- print("Scavenging for ores...")
- while true do
- if turtle.getFuelLevel() < turtle.getFuelLimit() / 4 then
- print("Low fuel. Returning to base...")
- goTo(baseLocation)
- turtle.turnRight()
- turtle.turnRight()
- turtle.forward()
- turtle.drop()
- restock()
- return
- end
- turtle.forward()
- local success, data = turtle.inspect()
- if success and (data.name == "minecraft:iron_ore" or data.name == "minecraft:coal_ore") then
- turtle.dig()
- turtle.suck()
- end
- if turtle.getItemCount(16) > 0 then
- print("Inventory full. Returning to base...")
- goTo(baseLocation)
- turtle.turnRight()
- turtle.turnRight()
- turtle.forward()
- turtle.drop()
- restock()
- return
- end
- end
- end
- local function handleCommand(command)
- if command == "create base" then
- createBase()
- elseif command == "restock" then
- restock()
- elseif command == "scavenge" then
- scavenge()
- else
- print("Unknown command:", command)
- end
- end
- local function launchPhase()
- print(config.launchMessage or "Running Launch Phase...")
- end
- local function idlePhase()
- print(config.idleMessage or "Entering Idle Phase...")
- while true do
- local event, param = os.pullEvent("key")
- if event == "key" then
- local key = keys.getName(param)
- if key == "enter" then
- print("Enter command:")
- local command = read()
- handleCommand(command)
- end
- end
- end
- end
- local function actionPhase()
- print(config.actionMessage or "Performing Action...")
- end
- createDirIfNotExists("]] .. configDir .. [[")
- writeDevStatus("off")
- local initialConfig = {
- launchMessage = "Welcome to AxoTurtle!",
- idleMessage = "Waiting for input...",
- actionMessage = "Action performed!",
- baseLocation = nil
- }
- writeConfig(initialConfig)
- local devStatus = getDevStatus()
- local config = readConfig()
- if devStatus == "on" then
- print("Development mode active")
- else
- print("Production mode active")
- launchPhase()
- idlePhase()
- end
- ]]
- -- Backup existing startup script
- if fs.exists(startupScript) then
- fs.copy(startupScript, "startup_backup")
- end
- -- Write new startup script
- local file = fs.open(startupScript, "w")
- file.write(startupCode)
- file.close()
- print("AxoTurtle installed successfully!")
Advertisement
Comments
-
- can u add me bro i have questions
- https://steamcommunity.com/profiles/76561199091249086
Add Comment
Please, Sign In to add comment
Advertisement