Advertisement
S3mpx

axoturtle_installer

May 21st, 2024 (edited)
916
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.17 KB | None | 0 0
  1. local configDir = "AxoTurtleConfig"
  2. local devOffFile = configDir .. "/dev_off"
  3. local configFile = configDir .. "/config"
  4.  
  5. -- Function to create a directory if it doesn't exist
  6. local function createDirIfNotExists(dir)
  7.     if not fs.exists(dir) then
  8.         fs.makeDir(dir)
  9.     end
  10. end
  11.  
  12. -- Function to write the development status file
  13. local function writeDevStatus(status)
  14.     local file = fs.open(devOffFile, "w")
  15.     file.write(status)
  16.     file.close()
  17. end
  18.  
  19. -- Function to read the configuration file
  20. local function readConfig()
  21.     if not fs.exists(configFile) then
  22.         return {}
  23.     end
  24.     local file = fs.open(configFile, "r")
  25.     local content = file.readAll()
  26.     file.close()
  27.     return textutils.unserialize(content)
  28. end
  29.  
  30. -- Function to write the configuration file
  31. local function writeConfig(config)
  32.     local file = fs.open(configFile, "w")
  33.     file.write(textutils.serialize(config))
  34.     file.close()
  35. end
  36.  
  37. -- Function to get the turtle's current location using GPS
  38. local function getLocation()
  39.     local x, y, z = gps.locate()
  40.     return {x = x, y = y, z = z}
  41. end
  42.  
  43. -- Function to move the turtle to a specific location (assuming GPS is available)
  44. local function goTo(targetLocation)
  45.     local currentLocation = getLocation()
  46.     local xDiff = targetLocation.x - currentLocation.x
  47.     local yDiff = targetLocation.y - currentLocation.y
  48.     local zDiff = targetLocation.z - currentLocation.z
  49.  
  50.     -- Adjust the turtle's Y position (up/down) first
  51.     if yDiff > 0 then
  52.         for i = 1, yDiff do
  53.             turtle.up()
  54.         end
  55.     elseif yDiff < 0 then
  56.         for i = 1, -yDiff do
  57.             turtle.down()
  58.         end
  59.     end
  60.  
  61.     -- Adjust the turtle's X position (east/west)
  62.     if xDiff > 0 then
  63.         turtle.turnTo(0) -- east
  64.         for i = 1, xDiff do
  65.             turtle.forward()
  66.         end
  67.     elseif xDiff < 0 then
  68.         turtle.turnTo(2) -- west
  69.         for i = 1, -xDiff do
  70.             turtle.forward()
  71.         end
  72.     end
  73.  
  74.     -- Adjust the turtle's Z position (north/south)
  75.     if zDiff > 0 then
  76.         turtle.turnTo(1) -- south
  77.         for i = 1, zDiff do
  78.             turtle.forward()
  79.         end
  80.     elseif zDiff < 0 then
  81.         turtle.turnTo(3) -- north
  82.         for i = 1, -zDiff do
  83.             turtle.forward()
  84.         end
  85.     end
  86. end
  87.  
  88. -- Function to create a base
  89. local function createBase()
  90.     print("Creating base...")
  91.     -- Dig below and place a crafting table
  92.     turtle.digDown()
  93.     turtle.select(1) -- Assuming the crafting table is in slot 1
  94.     turtle.placeDown()
  95.  
  96.     -- Place a chest south of the crafting table
  97.     turtle.turnRight()
  98.     turtle.turnRight()
  99.     turtle.forward()
  100.     turtle.select(2) -- Assuming the chest is in slot 2
  101.     turtle.place()
  102.  
  103.     -- Save base location to config
  104.     config.baseLocation = getLocation()
  105.     writeConfig(config)
  106.     print("Base created and location saved.")
  107. end
  108.  
  109. -- Function to restock fuel
  110. local function restock()
  111.     local fuelLevel = turtle.getFuelLevel()
  112.     local requiredFuel = 500 -- Example threshold
  113.  
  114.     if fuelLevel < requiredFuel then
  115.         print("Refueling...")
  116.         turtle.select(3) -- Assuming coal is in slot 3
  117.         while turtle.getFuelLevel() < requiredFuel and turtle.getItemCount() > 0 do
  118.             turtle.refuel(1)
  119.         end
  120.  
  121.         if turtle.getFuelLevel() < requiredFuel then
  122.             print("Searching for more coal in base chest...")
  123.             -- Assuming the chest is south of the base location
  124.             goTo(config.baseLocation)
  125.             turtle.turnRight()
  126.             turtle.turnRight()
  127.             turtle.forward()
  128.             turtle.suck() -- Attempt to take coal from the chest
  129.             turtle.turnRight()
  130.             turtle.turnRight()
  131.             turtle.back()
  132.             turtle.select(3)
  133.             while turtle.getFuelLevel() < requiredFuel and turtle.getItemCount() > 0 do
  134.                 turtle.refuel(1)
  135.             end
  136.         end
  137.  
  138.         print("Refuel complete. Current fuel level:", turtle.getFuelLevel())
  139.     else
  140.         print("Fuel level sufficient:", turtle.getFuelLevel())
  141.     end
  142. end
  143.  
  144. -- Function to scavenge for ores
  145. local function scavenge()
  146.     local baseLocation = config.baseLocation
  147.     if not baseLocation then
  148.         print("Base location not set. Create a base first.")
  149.         return
  150.     end
  151.  
  152.     print("Scavenging for ores...")
  153.     while true do
  154.         if turtle.getFuelLevel() < turtle.getFuelLimit() / 4 then
  155.             print("Low fuel. Returning to base...")
  156.             goTo(baseLocation)
  157.             turtle.turnRight()
  158.             turtle.turnRight()
  159.             turtle.forward()
  160.             turtle.drop()
  161.             restock()
  162.             return
  163.         end
  164.  
  165.         turtle.forward()
  166.         -- Check for ores and collect
  167.         local success, data = turtle.inspect()
  168.         if success and (data.name == "minecraft:iron_ore" or data.name == "minecraft:coal_ore") then
  169.             turtle.dig()
  170.             turtle.suck()
  171.         end
  172.  
  173.         -- Return to base if inventory is full
  174.         if turtle.getItemCount(16) > 0 then
  175.             print("Inventory full. Returning to base...")
  176.             goTo(baseLocation)
  177.             turtle.turnRight()
  178.             turtle.turnRight()
  179.             turtle.forward()
  180.             turtle.drop()
  181.             restock()
  182.             return
  183.         end
  184.     end
  185. end
  186.  
  187. -- Function to handle user commands
  188. local function handleCommand(command)
  189.     if command == "create base" then
  190.         createBase()
  191.     elseif command == "restock" then
  192.         restock()
  193.     elseif command == "scavenge" then
  194.         scavenge()
  195.     else
  196.         print("Unknown command:", command)
  197.     end
  198. end
  199.  
  200. -- Phase 1: Launch Phase
  201. local function launchPhase()
  202.     print(config.launchMessage or "Running Launch Phase...")
  203.     -- Insert launch phase logic here
  204. end
  205.  
  206. -- Phase 2: Idle Phase
  207. local function idlePhase()
  208.     print(config.idleMessage or "Entering Idle Phase...")
  209.     while true do
  210.         -- Wait for user input or other events
  211.         local event, param = os.pullEvent("key")
  212.         if event == "key" then
  213.             local key = keys.getName(param)
  214.             if key == "enter" then
  215.                 print("Enter command:")
  216.                 local command = read()
  217.                 handleCommand(command)
  218.             end
  219.         end
  220.     end
  221. end
  222.  
  223. -- Phase 3: Action Phase
  224. local function actionPhase()
  225.     print(config.actionMessage or "Performing Action...")
  226.     -- Insert action phase logic here
  227. end
  228.  
  229. -- Create configuration directory and dev_off file
  230. createDirIfNotExists(configDir)
  231. writeDevStatus("off")
  232.  
  233. -- Initial configuration
  234. local initialConfig = {
  235.     launchMessage = "Welcome to AxoTurtle!",
  236.     idleMessage = "Waiting for input...",
  237.     actionMessage = "Action performed!",
  238.     baseLocation = nil
  239. }
  240. writeConfig(initialConfig)
  241.  
  242. -- Modify the startup script
  243. local startupScript = "/startup"
  244. local startupCode = [[
  245. -- Existing startup code if any
  246. if fs.exists("startup_backup") then
  247.     shell.run("startup_backup")
  248. end
  249.  
  250. -- AxoTurtle startup additions
  251. local devStatusFile = "]] .. devOffFile .. [["
  252. local configFile = "]] .. configFile .. [["
  253.  
  254. local function getDevStatus()
  255.     if not fs.exists(devStatusFile) then
  256.         return "off"
  257.     end
  258.     local file = fs.open(devStatusFile, "r")
  259.     local status = file.readAll()
  260.     file.close()
  261.     return status:match("^%s*(.-)%s*$") -- trim whitespace
  262. end
  263.  
  264. local function readConfig()
  265.     if not fs.exists(configFile) then
  266.         return {}
  267.     end
  268.     local file = fs.open(configFile, "r")
  269.     local content = file.readAll()
  270.     file.close()
  271.     return textutils.unserialize(content)
  272. end
  273.  
  274. local function writeConfig(config)
  275.     local file = fs.open(configFile, "w")
  276.     file.write(textutils.serialize(config))
  277.     file.close()
  278. end
  279.  
  280. local function getLocation()
  281.     local x, y, z = gps.locate()
  282.     return {x = x, y = y, z = z}
  283. end
  284.  
  285. local function goTo(targetLocation)
  286.     local currentLocation = getLocation()
  287.     local xDiff = targetLocation.x - currentLocation.x
  288.     local yDiff = targetLocation.y - currentLocation.y
  289.     local zDiff = targetLocation.z - currentLocation.z
  290.  
  291.     -- Adjust the turtle's Y position (up/down) first
  292.     if yDiff > 0 then
  293.         for i = 1, yDiff do
  294.             turtle.up()
  295.         end
  296.     elseif yDiff < 0 then
  297.         for i = 1, -yDiff do
  298.             turtle.down()
  299.         end
  300.     end
  301.  
  302.     -- Adjust the turtle's X position (east/west)
  303.     if xDiff > 0 then
  304.         turtle.turnTo(0) -- east
  305.         for i = 1, xDiff do
  306.             turtle.forward()
  307.         end
  308.     elseif xDiff < 0 then
  309.         turtle.turnTo(2) -- west
  310.         for i = 1, -xDiff do
  311.             turtle.forward()
  312.         end
  313.     end
  314.  
  315.     -- Adjust the turtle's Z position (north/south)
  316.     if zDiff > 0 then
  317.         turtle.turnTo(1) -- south
  318.         for i = 1, zDiff do
  319.             turtle.forward()
  320.         end
  321.     elseif zDiff < 0 then
  322.         turtle.turnTo(3) -- north
  323.         for i = 1, -zDiff do
  324.             turtle.forward()
  325.         end
  326.     end
  327. end
  328.  
  329. local function createBase()
  330.     print("Creating base...")
  331.     turtle.digDown()
  332.     turtle.select(1)
  333.     turtle.placeDown()
  334.     turtle.turnRight()
  335.     turtle.turnRight()
  336.     turtle.forward()
  337.     turtle.select(2)
  338.     turtle.place()
  339.     config.baseLocation = getLocation()
  340.     writeConfig(config)
  341.     print("Base created and location saved.")
  342. end
  343.  
  344. local function restock()
  345.     local fuelLevel = turtle.getFuelLevel()
  346.     local requiredFuel = 500
  347.     if fuelLevel < requiredFuel then
  348.         print("Refueling...")
  349.         turtle.select(3)
  350.         while turtle.getFuelLevel() < requiredFuel and turtle.getItemCount() > 0 do
  351.             turtle.refuel(1)
  352.         end
  353.         if turtle.getFuelLevel() < requiredFuel then
  354.             print("Searching for more coal in base chest...")
  355.             goTo(config.baseLocation)
  356.             turtle.turnRight()
  357.             turtle.turnRight()
  358.             turtle.forward()
  359.             turtle.suck()
  360.             turtle.turnRight()
  361.             turtle.turnRight()
  362.             turtle.back()
  363.             turtle.select(3)
  364.             while turtle.getFuelLevel() < requiredFuel and turtle.getItemCount() > 0 do
  365.                 turtle.refuel(1)
  366.             end
  367.         end
  368.         print("Refuel complete. Current fuel level:", turtle.getFuelLevel())
  369.     else
  370.         print("Fuel level sufficient:", turtle.getFuelLevel())
  371.     end
  372. end
  373.  
  374. local function scavenge()
  375.     local baseLocation = config.baseLocation
  376.     if not baseLocation then
  377.         print("Base location not set. Create a base first.")
  378.         return
  379.     end
  380.     print("Scavenging for ores...")
  381.     while true do
  382.         if turtle.getFuelLevel() < turtle.getFuelLimit() / 4 then
  383.             print("Low fuel. Returning to base...")
  384.             goTo(baseLocation)
  385.             turtle.turnRight()
  386.             turtle.turnRight()
  387.             turtle.forward()
  388.             turtle.drop()
  389.             restock()
  390.             return
  391.         end
  392.         turtle.forward()
  393.         local success, data = turtle.inspect()
  394.         if success and (data.name == "minecraft:iron_ore" or data.name == "minecraft:coal_ore") then
  395.             turtle.dig()
  396.             turtle.suck()
  397.         end
  398.         if turtle.getItemCount(16) > 0 then
  399.             print("Inventory full. Returning to base...")
  400.             goTo(baseLocation)
  401.             turtle.turnRight()
  402.             turtle.turnRight()
  403.             turtle.forward()
  404.             turtle.drop()
  405.             restock()
  406.             return
  407.         end
  408.     end
  409. end
  410.  
  411. local function handleCommand(command)
  412.     if command == "create base" then
  413.         createBase()
  414.     elseif command == "restock" then
  415.         restock()
  416.     elseif command == "scavenge" then
  417.         scavenge()
  418.     else
  419.         print("Unknown command:", command)
  420.     end
  421. end
  422.  
  423. local function launchPhase()
  424.     print(config.launchMessage or "Running Launch Phase...")
  425. end
  426.  
  427. local function idlePhase()
  428.     print(config.idleMessage or "Entering Idle Phase...")
  429.     while true do
  430.         local event, param = os.pullEvent("key")
  431.         if event == "key" then
  432.             local key = keys.getName(param)
  433.             if key == "enter" then
  434.                 print("Enter command:")
  435.                 local command = read()
  436.                 handleCommand(command)
  437.             end
  438.         end
  439.     end
  440. end
  441.  
  442. local function actionPhase()
  443.     print(config.actionMessage or "Performing Action...")
  444. end
  445.  
  446. createDirIfNotExists("]] .. configDir .. [[")
  447. writeDevStatus("off")
  448. local initialConfig = {
  449.     launchMessage = "Welcome to AxoTurtle!",
  450.     idleMessage = "Waiting for input...",
  451.     actionMessage = "Action performed!",
  452.     baseLocation = nil
  453. }
  454. writeConfig(initialConfig)
  455.  
  456. local devStatus = getDevStatus()
  457. local config = readConfig()
  458. if devStatus == "on" then
  459.     print("Development mode active")
  460. else
  461.     print("Production mode active")
  462.     launchPhase()
  463.     idlePhase()
  464. end
  465. ]]
  466.  
  467. -- Backup existing startup script
  468. if fs.exists(startupScript) then
  469.     fs.copy(startupScript, "startup_backup")
  470. end
  471.  
  472. -- Write new startup script
  473. local file = fs.open(startupScript, "w")
  474. file.write(startupCode)
  475. file.close()
  476.  
  477. print("AxoTurtle installed successfully!")
  478.  
Advertisement
Comments
  • bairi
    23 days
    # text 0.09 KB | 0 0
    1. can u add me bro i have questions
    2. https://steamcommunity.com/profiles/76561199091249086
Add Comment
Please, Sign In to add comment
Advertisement