Advertisement
Guest User

startup

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