Guest User

startup

a guest
Apr 16th, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.39 KB | None | 0 0
  1.     --VARIABLES
  2.    
  3. local line                                      --line inside file
  4. local stringQuestionAsked                       --Question for the Menu
  5. local x,y = term.getSize()                      --Size of the screen
  6. local highlighted = 1                           --Chosen Element inside menu
  7. local choices = {}                              --Table of choices inside menu
  8. local chosenOne                                 --Chosen element of the menu
  9. local success, data
  10. local i
  11. local m
  12. local n
  13.    
  14.     --FILES
  15.    
  16.     --Names
  17.    
  18. local turtleDataFile = "turtleDataFile"         --Name of the File that saves 1: X coordinate, 2: Z coordinate, 3: Rotation, 4: Number of Holes
  19. local overseerIDFile = "overseerID"             --Name of the File that saves the IDs of every Overseer
  20. local trashBlockFile = "trashBlock"             --Name of the File that saves every Block that the Turtle shouldn't mine
  21. local trackingBlockFile = "trackingBlock"       --Name of the File that saves the Blocks that are supposed to be tracked
  22. local blockStatsFile = "BlockStats"             --Name of the File that keeps track of the number of Blocks mined of the Blocks it should keep track of
  23.  
  24.     --Tables
  25.    
  26. local turtleData = {}                           --Table to save the Data inside the turtleDataFile
  27. local overseerID = {}                           --Table to save the Data inside the overseerIDFile
  28. local trashBlock = {}                           --Table to save the Data inside the trachBlockFile
  29. local newTrashBlock = {}                        --Table to save the new Data of Trash Blocks
  30. local nameTrashBlock = {}
  31. local trackingBlock = {}                        --Table to save the Data inside the trackingBlockFile
  32. local blockStats = {}                           --Table to save the Data inside the blockStatsFile
  33.  
  34.     --FUNKTIONS
  35.  
  36. function load(fileName,arrayName)               --Loading data that is inside File
  37.     file = fs.open("Data/"..fileName,"r") -- Open a file for reading.
  38.     line = file.readLine() -- This function reads the next line in the file, until the end.
  39.     repeat -- Start a loop which will read lines into a table until no lines are left.
  40.         table.insert(arrayName,line) -- Puts the value of the current line into the table we have.
  41.         line = file.readLine() -- read the next line
  42.     until line == nil -- readLine() returns nil when the end of the file is reached.
  43.     file.close() -- Close up the file ready for use again.
  44. end
  45.  
  46. function createFile(File)                   --Function that is used for the sole purpose of creating a file without editing it
  47.     local file = fs.open("Data/"..File,"a")
  48.     file.close()
  49. end
  50.  
  51. function resetScreen()
  52.     term.clear()
  53.     term.setCursorPos(1,1)
  54. end
  55.  
  56.     --MENU
  57.  
  58. function writeChoices()         --writes the choices down made for the menu
  59.     for i=1,#choices do
  60.         if i == highlighted then
  61.             term.setCursorPos(((x/2)-(("<| "..choices[i].." |>"):len()/2)),i + 2)
  62.             term.clearLine()
  63.             term.write("<| "..choices[i].." |>")
  64.         else
  65.             term.setCursorPos(((x/2)-(choices[i]:len()/2)),i + 2)
  66.             term.clearLine()
  67.             term.write(choices[i])
  68.         end
  69.     end
  70. end
  71.  
  72. function menuSelection()                            --waits for key input and changes the highlighted menuElemnt
  73.     while true do
  74.         writeChoices()
  75.         local event, key = os.pullEvent( "key" )
  76.         if key == keys.down then
  77.             highlighted = highlighted + 1
  78.         end
  79.         if key == keys.up then
  80.             highlighted = highlighted - 1
  81.         end
  82.         if key == keys.enter then
  83.             chosenOne = choices[highlighted]
  84.             break
  85.         end
  86.         if highlighted == 0 then
  87.             highlighted = #choices
  88.         end
  89.         if highlighted == #choices + 1 then
  90.             highlighted = 1
  91.         end
  92.     end
  93. end
  94.  
  95. function Menu()                                     --Creates a menu
  96.     term.clear()
  97.     term.setCursorPos(math.ceil((x/2)-(stringQuestionAsked:len()/2)),1)
  98.     term.write(stringQuestionAsked)
  99.     menuSelection()
  100. end
  101.  
  102.  
  103.     --CODE
  104.    
  105.     --VOID START
  106.  
  107. fs.makeDir("Data") 
  108. createFile(turtleDataFile)
  109. createFile(overseerIDFile)
  110. createFile(trashBlockFile)
  111. createFile(trackingBlockFile)
  112. createFile(blockStatsFile)
  113.  
  114. load(turtleDataFile,turtleData)
  115. load(overseerIDFile,overseerID)
  116. load(trashBlockFile,trashBlock)
  117. load(trackingBlockFile,trackingBlock)
  118. load(blockStatsFile,blockStats)
  119.  
  120. turtleData = {}
  121. load(turtleDataFile,turtleData)
  122.  
  123. if turtleData[1] == nil then        --TurtleData first time installation
  124.  
  125.     while true do                   --Turtles X Coordinate
  126.         resetScreen()
  127.         term.write("What is the turtles X Coordinate?")
  128.         term.setCursorPos(1,2)
  129.         term.write("X: ")
  130.         local x = read()
  131.         highlighted = 1
  132.         stringQuestionAsked = "Is the X Coordinate really: "..x.."?"
  133.         choices ={"Yes it is","No it's not"}
  134.         Menu()
  135.         if choices[1] == chosenOne then
  136.             table.insert(turtleData,1,x)
  137.             break
  138.         end
  139.     end
  140.    
  141.     while true do                   --Turtles Z Coordinate
  142.         resetScreen()
  143.         term.write("What is the turtles Z Coordinate?")
  144.         term.setCursorPos(1,2)
  145.         term.write("Z: ")
  146.         local x = read()
  147.         stringQuestionAsked = "Is the Z Coordinate really: "..x.."?"
  148.         highlighted = 1
  149.         Menu()
  150.         if choices[1] == chosenOne then
  151.             table.insert(turtleData,2,x)
  152.             break
  153.         end
  154.     end
  155.    
  156.     while true do                   --Turtles Rotation
  157.         resetScreen()
  158.         stringQuestionAsked = "Which way is the turtle Facing?"
  159.         choices = {"North","East","South","West"}
  160.         highlighted = 1
  161.         Menu()
  162.         local x = chosenOne
  163.         stringQuestionAsked = "You sure the turtle is facing "..x.."?"
  164.         choices = {"Yes I am","No I'm not"}
  165.         highlighted = 1
  166.         Menu()
  167.         if choices[1] == chosenOne then
  168.             if x == "North" then
  169.                 x = "1"
  170.             elseif x == "East" then
  171.                 x = "2"
  172.             elseif x == "South" then
  173.                 x = "3"
  174.             elseif x == "West" then
  175.                 x = "4"
  176.             end
  177.            
  178.             table.insert(turtleData,3,x)
  179.             break
  180.         end
  181.     end
  182.    
  183.     table.insert(turtleData,4,"0")
  184.     local file = fs.open("Data/"..turtleDataFile,"a")
  185.     for i=1, #turtleData, 1 do
  186.         file.writeLine(turtleData[i])
  187.     end
  188.     file.close()
  189. end
  190.  
  191. overseerID = {}
  192. load(overseerIDFile,overseerID)
  193. if overseerID[1] == nil then
  194.     while true do
  195.         resetScreen()
  196.         term.write("What is the overseer's ID?")
  197.         term.setCursorPos(1,2)
  198.         term.write("Command: os.getComputerID()")
  199.         term.setCursorPos(1,3)
  200.         term.write("ID: ")
  201.         local x = read()
  202.         stringQuestionAsked = "Are you sure this is the ID "..x.."?"
  203.         choices = {"Yes I am","No I'm not"}
  204.         highlighted = 1
  205.         Menu()
  206.         if choices[1] == chosenOne then
  207.         local add = true
  208.         load(overseerIDFile,overseerID)
  209.             for i=1, #overseerID, 1 do
  210.                 if x == overseerID[i] then
  211.                     add = false
  212.                 end
  213.             end
  214.             if add == true then
  215.                 local file = fs.open("Data/"..overseerIDFile,"a")
  216.                 file.writeLine(x)
  217.                 file.close()
  218.             else
  219.                 resetScreen()
  220.                 term.write("That Overseer has already been added")
  221.                 os.sleep(1)
  222.             end
  223.             stringQuestionAsked = "Do you want to add another overseer?"
  224.             choices = {"Yes I do","No I don't"}
  225.             highlighted = 1
  226.             Menu()
  227.             if choices[2] == chosenOne then
  228.                 break
  229.             end
  230.         end
  231.     end
  232. end
  233.  
  234. trashBlock = {}
  235. load(trashBlockFile,trashBlock)
  236. if trashBlock[1] == nil then
  237.     while true do
  238.         stringQuestionAsked = "Add a new Block to ignore"
  239.         choices = {"Type in Code","Place block in front","Banned Blocks","Quit","Tutorial [start]"}
  240.         highlighted = 1
  241.         Menu()
  242.         if choices[1] == chosenOne then --Type in Code
  243.             while true do
  244.                 resetScreen()
  245.                 print("Type in the code of the Block")
  246.                 print("for example minecraft:stone")
  247.                 term.write("Code: ")
  248.                 local x = read()
  249.                 stringQuestionAsked = "Is "..x.." correct?"
  250.                 choices = {"Yes it is", "No it isn't"}
  251.                 highlighted = 1
  252.                 Menu()
  253.                 if choices[1] == chosenOne then
  254.                     local add = true
  255.                     trashBlock = {}
  256.                     load(trashBlockFile,trashBlock)
  257.                     for i = 1,#trashBlock,1 do
  258.                         if trashBlock[i] == x then
  259.                             add = false
  260.                         end
  261.                     end
  262.                     if add == true then
  263.                         local file = fs.open("Data/"..trashBlockFile,"a")
  264.                         file.writeLine(x)
  265.                         file.close()
  266.                         resetScreen()
  267.                         print(x.." won't get mined any more.")
  268.                         os.sleep(2)
  269.                     else
  270.                         resetScreen()
  271.                         term.write("That Block has already been added")
  272.                         os.sleep(2)
  273.                     end
  274.                     break
  275.                 end
  276.             end
  277.         elseif choices[2] == chosenOne then --Place a Block in front of turtle
  278.             while true do
  279.                 success, data = turtle.inspect()
  280.                 if data.name == nil then
  281.                     resetScreen()
  282.                     print("You have to place a Block in front of the turtle before starting this function!")
  283.                     os.sleep(3)
  284.                     break
  285.                 end
  286.                 stringQuestionAsked = "Is "..data.name.." correct?"
  287.                 choices = {"Yes it is", "No it isn't"}
  288.                 highlighted = 1
  289.                 Menu()
  290.                 if choices[1] == chosenOne then
  291.                     local add = true
  292.                     trashBlock = {}
  293.                     load(trashBlockFile,trashBlock)
  294.                     for i = 1,#trashBlock,1 do
  295.                         if trashBlock[i] == data.name then
  296.                             add = false
  297.                         end
  298.                     end
  299.                     if add == true then
  300.                         local file = fs.open("Data/"..trashBlockFile,"a")
  301.                         file.writeLine(data.name)
  302.                         file.close()
  303.                         resetScreen()
  304.                         print(data.name.." won't get mined any more.")
  305.                         os.sleep(2)
  306.                         break
  307.                     else
  308.                         resetScreen()
  309.                         term.write("That Block has already been added")
  310.                         os.sleep(2)
  311.                         break
  312.                     end
  313.                 end
  314.             end
  315.         elseif choices[3] == chosenOne then --Remove Block from List
  316.             trashBlock = {}
  317.             newTrashBlock = {}
  318.             load(trashBlockFile,trashBlock)
  319.             newTrashBlock = trashBlock
  320.             for i=1,#trashBlock do
  321.                 stringQuestionAsked = "Remove "..nameTrashBlock[i].."?"
  322.                 choices = {"Yes I will","No I won't"}
  323.                 highlighted = 2
  324.                 Menu()
  325.                 if choices[1] == chosenOne then
  326.                     table.remove(newTrashBlock,i)
  327.                 end
  328.             end
  329.             trashBlock = newTrashBlock
  330.             resetScreen()
  331.             print(#trashBlock)
  332.             os.sleep(5)
  333.             local file = fs.open("Data/"..trashBlockFile,"w")
  334.             for n=1,#trashBlock,1 do
  335.                 file.writeLine(trashBlock[n])
  336.             end
  337.             file.close()
  338.            
  339.         elseif choices[4] == chosenOne then --Quit
  340.         trashBlock = {}
  341.         load(trashBlockFile,trashBlock)
  342.             if trashBlock[1] == nil then
  343.                 local file = fs.open("Data/"..trashBlockFile,"a")
  344.                 file.writeLine(1)
  345.                 file.close()
  346.             end
  347.             break
  348.         elseif choices[5] == chosenOne then --Tutorial
  349.             while true do
  350.                 stringQuestionAsked = "Tutorial"
  351.                 choices = {"Type in Code","Place block in front","Banned Blocks","Quit","Tutorial [stop]"}
  352.                 highlighted = 1
  353.                 Menu()
  354.                 if choices[1] == chosenOne then
  355.                     resetScreen()
  356.                     print("Type in Code:")
  357.                     print("You will be able to type in a code id for any block in the game that the turtle is not supposed to mine. You will have to know the Code though. For example minecraft:stone for stone.")
  358.                     os.sleep(8)
  359.                 elseif choices[2] == chosenOne then
  360.                     resetScreen()
  361.                     print("Place block in front:")
  362.                     print("First Place the Block you want to add in front of the Turtle then Select this Button. This will add that Block to the list of Blocks that it won't mine")
  363.                     os.sleep(4)
  364.                 elseif choices[3] == chosenOne then
  365.                     resetScreen()
  366.                     print("Let's you remove previously added Blocks")
  367.                     os.sleep(2)
  368.                 elseif choices[4] == chosenOne then
  369.                     resetScreen()
  370.                     print("Quit:")
  371.                     term.write("Quits the Add new Block Menu")
  372.                     os.sleep(3)
  373.                 elseif choices[5] == chosenOne then
  374.                     print("Banned Blocks:")
  375.                     print("Displays a list of all of the Blocks that have been banned by the system")
  376.                     os.sleep(3)
  377.                 elseif choices[5] == chosenOne then
  378.                     break
  379.                 end
  380.             end
  381.         end
  382.        
  383.     end
  384. end
  385.  
  386. if trackingBlock[1] == nil then
  387.     while true do
  388.         stringQuestionAsked = "Add a new Block to Track"
  389.         choices = {"Type in Code","Place block in front","Quit"}
  390.         highlighted = 1
  391.         Menu()
  392.         if choices[1] == chosenOne then
  393.             while true do
  394.                 resetScreen()
  395.                 print("Type in the code of the Block")
  396.                 print("for example minecraft: minecraft:diamond_ore")
  397.                 term.write("Code: ")
  398.                 local x = read()
  399.                 stringQuestionAsked = "Is "..x.." correct?"
  400.                 choices = {"Yes it is", "No it isn't"}
  401.                 highlighted = 1
  402.                 Menu()
  403.                 if choices[1] == chosenOne then
  404.                     local add = true
  405.                     load(trackingBlockFile,trackingBlock)
  406.                     for i = 1,#trackingBlock,1 do
  407.                         if trackingBlock[i] == x then
  408.                             add = false
  409.                         end
  410.                     end
  411.                     if add == true then
  412.                         local file = fs.open("Data/"..trackingBlockFile,"a")
  413.                         file.writeLine(x)
  414.                         file.close()
  415.                         resetScreen()
  416.                         print(x.." will be tracked.")
  417.                         os.sleep(2)
  418.                     else
  419.                         resetScreen()
  420.                         term.write("That Block has already been added")
  421.                         os.sleep(2)
  422.                     end
  423.                     break
  424.                 end
  425.             end
  426.         elseif choices[2] == chosenOne then
  427.             while true do
  428.                 success, data = turtle.inspect()
  429.                 if data.name == nil then
  430.                     resetScreen()
  431.                     print("You have to place a Block in front of the turtle before starting this function!")
  432.                     os.sleep(3)
  433.                     break
  434.                 end
  435.                 stringQuestionAsked = "Is "..data.name.." correct?"
  436.                 choices = {"Yes it is", "No it isn't"}
  437.                 highlighted = 1
  438.                 Menu()
  439.                 if choices[1] == chosenOne then
  440.                     local add = true
  441.                     load(trackingBlockFile,trackingBlock)
  442.                     for i = 1,#trackingBlock,1 do
  443.                         if trackingBlock[i] == data.name then
  444.                             add = false
  445.                         end
  446.                     end
  447.                     if add == true then
  448.                         local file = fs.open("Data/"..trackingBlockFile,"a")
  449.                         file.writeLine(data.name)
  450.                         file.close()
  451.                         resetScreen()
  452.                         print(data.name.." will be tracked.")
  453.                         os.sleep(2)
  454.                         break
  455.                     else
  456.                         resetScreen()
  457.                         term.write("That Block has already been added")
  458.                         os.sleep(2)
  459.                         break
  460.                     end
  461.                 end
  462.             end
  463.         elseif choices[3] == chosenOne then
  464.         load(trackingBlockFile,trackingBlock)
  465.             if trackingBlock[1] == nil then
  466.                 local file = fs.open("Data/"..trackingBlockFile,"a")
  467.                 file.writeLine(1)
  468.                 file.close()
  469.             end
  470.             break
  471.         end
  472.        
  473.     end
  474. end
  475. turtleData={}
  476. load(turtleDataFile,turtleData)
  477. resetScreen()
  478. print(table.getn(turtleData))
  479. print(#turtleData)
Advertisement
Add Comment
Please, Sign In to add comment