Jyzarc

mining program

Jan 19th, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.55 KB | None | 0 0
  1. --Configs
  2. --What
  3. local MODEM_CHANNEL = 127
  4. --Amount of attempts to break block (if none set to nil or false)
  5. local MAX_ATTEMPTS = 25
  6. --Slot with enderchest (if none set to nil or false)
  7. local ENDER_CHEST_SLOT = 16
  8.  
  9. --[[ Legal stuff:
  10.     Literally do whatever you want, modify, redistribute, claim it as yours and sell it for dogecoins.
  11.     Modification is encouraged.
  12.     You can use any of my code for your own projects (Not recommended, my code is pretty sloppy)
  13. ]]
  14.  
  15. --Not configs
  16. --Dont change this stuff unless you know what you are doing, basically.
  17.  
  18. --Program Object
  19. local program = {
  20.     name = shell.getRunningProgram();
  21.     args = {...};
  22.     version = "1.0.0";
  23.     pastebinCode = "5bUfs7P0";
  24.     cmdHistory = {}
  25. }
  26.  
  27. --JyNet Object
  28. local JyNet = {
  29.     channel = MODEM_CHANNEL;
  30.     --If is wireless turtle or not
  31.     active = false;
  32. }
  33.  
  34. --Bot Object
  35. local bot = {
  36.     ID = os.getComputerID();
  37.     --Direction turtle is facing
  38.     --X,Y,Z coordinates relative to where it started
  39.     dir = 0;
  40.     xPos;
  41.     yPos;
  42.     zPos;
  43. }
  44.  
  45. --Mining Quarry
  46. local quarry = {
  47.     --Number of blocks that have been mined this cycle
  48.     blocksMined = 1;
  49.     --Will dump items into enderchest
  50.     enderChest = false;
  51.     x = tonumber(program.args[1]);
  52.     y = tonumber(program.args[2]);
  53.     z = tonumber(program.args[3]);
  54. }
  55. local eventData = {}
  56. local help = [[
  57. Ill add stuff later
  58. ]]
  59.  
  60. --Clears screen then exits program
  61. function program.exit()
  62.     --Clear screen
  63.     term.clear()
  64.     --Set cursor pos to top left
  65.     term.setCursorPos(1,1)
  66.     --Exits program
  67.     error()
  68. end
  69.  
  70. function program.uninstall()
  71.     --Clear screen
  72.     term.clear()
  73.     --Set cursor pos to top left
  74.     term.setCursorPos(1,1)
  75.     print("Are you sure you would like to uninstall?")
  76.     program.input = read()
  77.     --If player answer is yes, then uninstall program
  78.     if program.input == "yes"
  79.     or program.input == "YES"
  80.     or program.input == "y"
  81.     or program.input == "Y"
  82.     or program.input == "true"
  83.     or program.input == "TRUE" then
  84.         --Deletes current program file
  85.         fs.delete(program.name)
  86.         print("Program uninstalled")
  87.         --Waits for key press
  88.         os.pullEvent("key")
  89.         --Exits program
  90.         program.exit()
  91.     end
  92. end
  93.  
  94. --Reinstalls/updates the currently running program
  95. function program.reinstall()
  96.     if http then
  97.         --Deletes current program file
  98.         fs.delete(program.name)
  99.         --Runs built it pastebin program (too lazy to write my own function)
  100.         shell.run("pastebin get "..program.pastebinCode.." "..program.name)
  101.         print("Program reinstalled, press any key to continue")
  102.         --Waits for key press
  103.         os.pullEvent("key")
  104.         --Exits program
  105.         program.exit()
  106.     else
  107.         print("Http not enabled")
  108.         --Waits for key press
  109.         os.pullEvent("key")
  110.     end
  111. end
  112.  
  113. function program.edit()
  114.     --Opens file to be edited
  115.     shell.run("edit "..program.name)
  116.     --Exits program
  117.     program.exit()
  118. end
  119.  
  120. function program.printHelp()
  121.     --Clears screen
  122.     term.clear()
  123.     --Sets cursor pos to top left
  124.     term.setCursorPos(1,1)
  125.     print(help)
  126.     --Waits for key press
  127.     os.pullEvent("key")
  128. end
  129.  
  130. function program.cmd()
  131.     term.clear()
  132.     term.setCursorPos(1,1)
  133.     while true do
  134.         term.write("> ")
  135.         program.input = read(nil,program.cmdHistory)
  136.         program.cmdHistory[#program.cmdHistory+1] = program.input
  137.         if program.input == "left" then
  138.             bot.turn("left")
  139.         elseif program.input == "right" then
  140.             bot.turn("right")
  141.         elseif program.input == "forward" then
  142.             bot.forward()
  143.         elseif program.input == "down" then
  144.             bot.down()
  145.         elseif program.input == "up" then
  146.             bot.up()
  147.         elseif program.input == "refuel" then
  148.             bot.refuel()
  149.         elseif program.input == "back" then
  150.             bot.turn("back")
  151.         elseif program.input == "exit" then
  152.             program.cmdHistory = {}
  153.             return
  154.         elseif program.input == "help" then
  155.             print("Commands = \"left\",\"right\",\"down\",\"up\", and\"forward\"")
  156.         else
  157.             print("Not valid command")
  158.         end
  159.     end
  160. end
  161.  
  162. function program.errorCheck()
  163.     --Beefy error checking code (if any)
  164. end
  165.  
  166. --This runs on startup and gets everything set up
  167. function program.startup()
  168.     turtle.select(1)
  169.     program.errorCheck()
  170.     if tonumber(program.args[1])
  171.     and tonumber(program.args[2])
  172.     and tonumber(program.args[3]) then
  173.         quarry.run()
  174.     end
  175. end
  176.  
  177. function bot.refuel(sNum)
  178.     if sNum == nil then
  179.         sNum = 0
  180.     end
  181.     term.clear()
  182.     term.setCursorPos(1,1)
  183.     print("Refueling")
  184.     for i = 1,16 do
  185.         --Selects slot
  186.         turtle.select(i)
  187.         --Refuels from slot
  188.         turtle.refuel()
  189.     end
  190.     turtle.select(1)
  191.     return turtle.getFuelLevel()
  192. end
  193.  
  194. --Moves forward and breaks blocks in front of it
  195. function bot.forward(sNum)
  196.     bot.attemptCounter = 0
  197.     if sNum == nil then
  198.         sNum = 1
  199.     end
  200.     --Breaks blocks until it can move forward
  201.     for i = 1,sNum do
  202.         while not turtle.forward() do
  203.             if turtle.dig() then
  204.                 quarry.blocksMined = quarry.blocksMined + 1
  205.             end
  206.             bot.attemptCounter = bot.attemptCounter + 1
  207.             if bot.attemptCounter > 1
  208.             and bot.attemptCounter <= MAX_ATTEMPTS then
  209.                 --Attacks entities
  210.                 turtle.attack()
  211.             elseif bot.attemptCounter > MAX_ATTEMPTS then
  212.                 return false
  213.             end
  214.         end
  215.     end
  216.     return true
  217. end
  218.  
  219. function bot.up(sNum)
  220.     attemptCounter = 0
  221.     if sNum == nil then
  222.         sNum = 1
  223.     end
  224.     --Breaks blocks until it can move forward
  225.     for i = 1,sNum do
  226.         while not turtle.up() do
  227.             if turtle.digUp() then
  228.                 quarry.blocksMined = quarry.blocksMined + 1
  229.             end
  230.             attemptCounter = attemptCounter + 1
  231.             if attemptCounter > 1
  232.             and attemptCounter <= MAX_ATTEMPTS then
  233.                 --Attacks entities
  234.                 turtle.attackUp()
  235.             elseif attemptCounter > MAX_ATTEMPTS then
  236.                 return false
  237.             end
  238.         end
  239.     end
  240.     return true
  241. end
  242.  
  243. function bot.down(sNum)
  244.     attemptCounter = 0
  245.     if sNum == nil then
  246.         sNum = 1
  247.     end
  248.     --Breaks blocks until it can move forward
  249.     for i = 1,sNum do
  250.         while not turtle.down() do
  251.             if turtle.digDown() then
  252.                 quarry.blocksMined = quarry.blocksMined + 1
  253.             end
  254.             attemptCounter = attemptCounter + 1
  255.             if attemptCounter > 1
  256.             and attemptCounter <= MAX_ATTEMPTS then
  257.                 --Attacks entities
  258.                 turtle.attackDown()
  259.             elseif attemptCounter > MAX_ATTEMPTS then
  260.                 return false
  261.             end
  262.         end
  263.     end
  264.     return true
  265. end
  266.  
  267. --Changes turtle orientation
  268. function bot.turn(sDir)
  269.     if sDir == "left" then
  270.         turtle.turnLeft()
  271.     elseif sDir == "right" then
  272.         turtle.turnRight()
  273.     elseif sDir == "back" then
  274.         turtle.turnRight()
  275.         turtle.turnRight()
  276.     end
  277. end
  278.  
  279. function quarry.checkFuel()
  280.     quarry.reqFuel = ((quarry.x*quarry.y)*quarry.z)
  281.     bot.fuelLevel = turtle.getFuelLevel()
  282.     while bot.fuelLevel < quarry.reqFuel do
  283.         print("Fuel = "..bot.fuelLevel.." of "..quarry.reqFuel)
  284.         quarry.event = os.pullEvent()
  285.         if quarry.event == "key" or quarry.event == "turtle_inventory" then
  286.             bot.fuelLevel = turtle.refuel()
  287.         end
  288.     end
  289. end
  290.  
  291. function quarry.startCycle()
  292.     bot.forward()
  293.     bot.turn("right")
  294. end
  295.  
  296. function quarry.digX()
  297.     bot.forward(quarry.x-1)
  298. end
  299.  
  300. function quarry.digY()
  301.     for i = 1,quarry.y do
  302.         quarry.digX()
  303.         if i ~= quarry.y then
  304.             bot.down()
  305.             bot.turn("back")
  306.         end
  307.     end
  308. end
  309.  
  310. function quarry.endCycle()
  311.     if quarry.y%2 == 1 then
  312.         bot.turn("left")
  313.         quarry.digX()
  314.     end
  315.     bot.turn("right")
  316.     bot.up(quarry.y-1)
  317. end
  318.  
  319. function quarry.digZ()
  320.     for i = 1,quarry.z do
  321.         quarry.startCycle()
  322.         quarry.digY()
  323.         quarry.endCycle()
  324.     end
  325. end
  326.  
  327. function quarry.shutDown()
  328.     quarry.x,quarry.y,quarry.z = nil
  329. end
  330.  
  331. function quarry.startup()
  332.     --clear screen
  333.     term.clear()
  334.     --set cursor pos to top left
  335.     term.setCursorPos(1,1)
  336.     --Checks if running on a turtle
  337.     if term.getSize() > 39 then
  338.         --Exits with error message
  339.         error("This program must run on a turtle")
  340.     end
  341.     if tonumber(ENDER_CHEST_SLOT)
  342.     and turtle.getItemCount(ENDER_CHEST_SLOT) > 0 then
  343.         quarry.enderChest = true
  344.     end
  345.     --Get X,Y,and Z num from player
  346.     if not quarry.x
  347.     or not quarry.y
  348.     or not quarry.y then
  349.         term.write("x: ")
  350.         program.input = read()
  351.         quarry.x = math.abs(math.floor(tonumber(program.input)))
  352.         term.write("y: ")
  353.         program.input = read()
  354.         quarry.y = math.abs(math.floor(tonumber(program.input)))
  355.         term.write("z: ")
  356.         program.input = read()
  357.         quarry.z = math.abs(math.floor(tonumber(program.input)))
  358.     end
  359. end
  360.  
  361. --Run the quarry
  362. function quarry.run()
  363.     quarry.startup()
  364.     quarry.digZ()
  365.     quarry.shutDown()
  366. end
  367.  
  368. --Wraps modem as peripheral
  369. function JyNet.wrapModem()
  370.     --Checks for modem
  371.     if peripheral.getType("right") == "modem" then
  372.         --Creates modem object
  373.         JyNet.modem = peripheral.wrap("right")
  374.         JyNet.active = true
  375.         return true
  376.     else
  377.         return false
  378.     end
  379. end
  380.  
  381. --Send Messages through modems
  382. function JyNet.send(sMessage)
  383.     --Checks if modem is connected
  384.     if JyNet.active then
  385.         --Sends message
  386.         JyNet.modem.transmit(JyNet.channel,bot.ID,sMessage)
  387.         return true
  388.     else
  389.         return false
  390.     end
  391. end
  392.  
  393. -- //Menu code
  394.  
  395. local menu = {}
  396. menu.pos = 3
  397. menu.backColor = colors.black
  398. menu.programName = program.name
  399. menu.programVersion = program.version
  400. menu.programDeveloper = "Jyzarc27"
  401. menu.eventData = {}
  402. menu.mainTextColor = colors.red
  403. menu.items = {
  404.     [1] = { text = "help", handler = program.printHelp, color = 1, bounds = {} };
  405.     [2] = { text = "refuel", handler = bot.refuel, color = 1, bounds = {} };
  406.     [3] = { text = "run", handler = quarry.run, color = 1, bounds = {} };
  407.     [4] = { text = "edit", handler = program.edit, color = 1, bounds = {} };
  408.     [5] = { text = "exit", handler = program.exit, color = 1, bounds = {} };
  409.     [6] = { text = "cmd", handler = program.cmd, color = 1, bounds = {} };
  410.     [7] = { text = "reinstall", handler = program.reinstall, color = 1, bounds = {} };
  411.     [8] = { text = "uninstall", handler = program.uninstall, color = 1, bounds = {} };
  412. }
  413.  
  414. function menu.print()
  415.     if term.isColor() then
  416.         term.setBackgroundColor(menu.backColor)
  417.     else
  418.         term.setBackgroundColor(colors.black)
  419.     end
  420.     term.clear()
  421.     term.setCursorPos(1,1)
  422.     if term.isColor() then
  423.         term.setTextColor(menu.mainTextColor)
  424.     else
  425.         term.setTextColor(colors.white)
  426.     end
  427.     print(menu.programName.." "..menu.programVersion.." by "..menu.programDeveloper)
  428.     menu.x,menu.y = term.getSize()
  429.     menu.x = (menu.x/2)
  430.     menu.y = (menu.y/2) - (#menu.items/2)
  431.     for i = 1,#menu.items do
  432.         if term.isColor() then
  433.             term.setTextColor(menu.items[i].color)
  434.         else
  435.             term.setTextColor(colors.white)
  436.         end
  437.         term.setCursorPos(menu.x-(string.len(menu.items[i].text)/2),menu.y+(i))
  438.         menu.items[i].bounds.x1,menu.items[i].bounds.y = term.getCursorPos()
  439.         if menu.pos == i then
  440.             print("<"..menu.items[i].text..">")
  441.         else
  442.             print(" "..menu.items[i].text)
  443.         end
  444.         menu.items[i].bounds.x2 = menu.items[i].bounds.x1 + string.len(menu.items[i].text)
  445.     end
  446. end
  447.  
  448. function menu.run()
  449.     menu.items[menu.pos].handler()
  450. end
  451.  
  452. function menu.checkClick(x,y)
  453.     for i = 1,#menu.items do
  454.         if x >= menu.items[i].bounds.x1
  455.         and x <= menu.items[i].bounds.x2
  456.         and y == menu.items[i].bounds.y then
  457.             return i
  458.         end
  459.     end
  460. end
  461.  
  462. program.startup()
  463.  
  464. --Main Loop
  465. while true do
  466.     menu.print(menu.pos)
  467.     menu.eventData[1],menu.eventData[2],menu.eventData[3],menu.eventData[4] = os.pullEvent()
  468.     if menu.eventData[1] == "key" then
  469.         if menu.eventData[2] == 208
  470.         or menu.eventData[2] == 205 then
  471.             menu.pos = menu.pos + 1
  472.         elseif menu.eventData[2] == 200
  473.         or menu.eventData[2] == 203 then
  474.             menu.pos = menu.pos - 1
  475.         elseif menu.eventData[2] == 28 then
  476.             menu.run()
  477.         end
  478.     elseif menu.eventData[1] == "mouse_scroll" then
  479.         menu.pos = menu.pos + menu.eventData[2]
  480.     elseif menu.eventData[1] == "mouse_click" then
  481.         if menu.eventData[2] == 1 then
  482.             menu.temp = menu.checkClick(menu.eventData[3],menu.eventData[4])
  483.             if menu.temp ~= nil then
  484.                 menu.pos = menu.temp
  485.                 menu.run()
  486.             end
  487.         end
  488.     end
  489.     if menu.pos > #menu.items then
  490.         menu.pos = 1
  491.     elseif menu.pos < 1 then
  492.         menu.pos = #menu.items
  493.     end
  494. end
Advertisement
Add Comment
Please, Sign In to add comment