Advertisement
Vodka51200

testblocksmined

Jul 10th, 2024 (edited)
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.61 KB | Gaming | 0 0
  1.  
  2. local fuel_level = turtle.getFuelLevel()
  3. local modem = peripheral.find("modem")
  4. modem.open(1)
  5.  
  6. local longueurBase = 7  -- Longueur de base pour les caractères fixes ("- ", " (Type: ", " x")
  7. local typeLength = 6   -- Length of " (Type: "
  8. local variantLength = 9  -- Length of " (Variant: "
  9. local oretypLenght = 9 --* Length of " (Oretype: "
  10. local test1 = longueurBase + typeLength
  11. local test2 = longueurBase + variantLength
  12. local test3 = 0
  13. local newsom = 0
  14.  
  15. local screenWidth, screenHeight = term.getSize()
  16.  
  17. term.setBackgroundColor(colors.gray)
  18. term.clear()
  19.  
  20. function printText(txt1, color1)
  21.   local oldColor = term.getTextColor() -- sauvegarde de la couleur courante
  22.   term.setTextColor(color1)
  23.   print(txt1)
  24.   term.setTextColor(oldColor) -- restauration de la couleur courante
  25. end
  26.  
  27. function printCenter(txt,color)
  28.   local w, h = term.getSize()
  29.   paintutils.drawFilledBox(math.floor(w/2-txt:len()/2+.5-1),6,math.floor(w/2+txt:len()/2+.5),8,colors.cyan)
  30.   term.setCursorPos(math.floor(w/2-txt:len()/2+.5),h/2+1)
  31.   local oldColorTxt = term.getTextColor() -- sauvegarde de la couleur courante
  32.   --local oldColorBackground = term.getBackgroundColor() -- sauvegarde de la couleur courante
  33.   term.setTextColor(color)
  34.   print(txt)
  35.   sleep(2)
  36.   term.setBackgroundColor(colors.gray)
  37.   term.setTextColor(oldColorTxt) -- restauration de la couleur courante
  38. end
  39.  
  40. local blockList = {}
  41.  
  42. -- fonction pour afficher les blocs et leur quantité sans pagination
  43. local function displayBlocks(blocks)
  44.   local page = 1
  45.   local pageSize = 8
  46.   local blockList = {}
  47.  
  48.   print("Blocks Mined:")
  49.   for block, data in pairs(blocks) do
  50.     local colonIndex = string.find(block, ":", 1, true)
  51.     local name = string.sub(block, 1, colonIndex - 1)
  52.     local metadata = string.sub(block, colonIndex + 1)
  53.     --local metadata2 = string.match(block, "^(.*):(.*)$") -- Capture everything before and after colon :
  54.     if name == "tconstruct" or name == "biomesoplenty" then
  55.     --if metadata then
  56.       newname = string.sub(metadata, string.len(string.match(metadata, "^([^:]*):")) + 2) -- Extract text after colon
  57.     end
  58.     --end
  59.     local count = data.count
  60.  
  61.     if name == "tconstruct" then
  62.       test3 = test1
  63.       print("- " .. name .. " (Type: " .. metadata .. ") x" .. count)
  64.     elseif name == "biomesoplenty" then
  65.       test3 = test2
  66.       print("- " .. name .. " (Variant: " .. metadata .. ") x" .. count)
  67.     elseif name == "deepresonance" then
  68.       test3 = test2
  69.       print("- " .. name .. " (Oretype: " .. metadata .. ") x" .. count)
  70.     else
  71.       test3 = 7
  72.       print("- " .. name .. " (" .. metadata .. ") x" .. count)
  73.     end
  74.  
  75.     -- Récupérer les longueurs des chaînes variables
  76.     longueurNom = string.len(name)
  77.     longueurType = string.len(metadata)
  78.     longueurQuantite = string.len(tostring(data.count))  -- Convertir la quantité en chaîne de caractères
  79.    
  80.     -- Calculer la longueur totale estimée
  81.     local longueurTotale = test3 + longueurNom + longueurType + longueurQuantite --longueurBase + longueurNom + longueurType + longueurQuantite
  82.     if longueurTotale > newsom then
  83.       newsom = longueurTotale
  84.     end
  85.  
  86.     if name == "tconstruct" or name == "biomesoplenty" then
  87.       table.insert(blockList, "- " .. newname .. " x" .. count)
  88.     elseif name == "deepresonance" then
  89.       table.insert(blockList, "- " .. metadata .. " x" .. count)
  90.     --elseif name == "minecraft" and metadata ~= nil then
  91.       --table.insert(blockList, "- " .. metadata .. " x" .. count)
  92.     else
  93.       table.insert(blockList, "- " .. name .. " (" .. metadata .. ") x" .. count)
  94.     end
  95.   end
  96.   sleep(5)
  97.   while true do
  98.   local totalPages = math.ceil(#blockList / pageSize)
  99.     term.clear()
  100.     term.setCursorPos(1,1)
  101.     print("Blocks Mined (Page " .. page .. " of " .. totalPages .. "):")
  102.     for i = (page-1)*pageSize+1,page*pageSize do
  103.       if blockList[i] then
  104.         print(blockList[i]) --a changer si type variant or pas
  105.       else
  106.         break
  107.       end
  108.     end
  109.     term.setCursorPos(1,11)
  110.     printText("press 'enter' for print menu",colors.yellow)
  111.     term.setCursorPos(1,12)
  112.     printText("press 'q' for quit",colors.red)
  113.     --attendre une entrée clavier pour passer à la page suivante ou précédente
  114.    
  115.     local event, key = os.pullEvent("key")
  116.     if key == keys.right and #blockList > page*pageSize then
  117.       page = page + 1
  118.     elseif key == keys.left and page > 1 then
  119.       page = page - 1
  120.     elseif key == keys.enter then
  121.     -- Appeler la fonction pour imprimer les pages
  122.     printCenter("Success",colors.green)
  123.     sleep(1)
  124.     --sendBlocks(blocks)
  125.     --else
  126.     elseif key == keys.q then
  127.       printCenter("Terminer",colors.red)
  128.       sleep(2)
  129.       term.setBackgroundColor(colors.black)
  130.       term.clear()
  131.       term.setCursorPos(1, screenHeight)
  132.       break
  133.       --return displayMenu()
  134.     end
  135.   end
  136.   --print(blockList)
  137.   --print("Longueur type estimée :", test1)
  138.   --print("Longueur variant estimée :", test2)
  139.   --print("Longueur block estimée :", test3)
  140.   --print("Longueur nom estimée :", longueurNom or 0)
  141.   --print("Longueur type estimée :", longueurType or 0)
  142.   --print("Longueur quantite estimée :", longueurQuantite or 0)
  143.   --Afficher la longueur maximale calculée
  144.   --print("Longueur maximale estimée :", newsom or 0)
  145.  
  146. end
  147.  
  148. -- fonction pour envoyer les données au moniteur
  149. local function sendBlocks(blocks)
  150.   local message = textutils.serializeJSON(blocks)
  151.   modem.transmit(1, 1, message)
  152. end
  153.  
  154. -- initialiser le tableau des blocs minés
  155. local blocks = {}
  156.  
  157. function CheckInventory() -- Fonction pour vérifier l'inventaire de la turtle
  158.   for i=1,16 do
  159.     local itemDetail = turtle.getItemDetail(i)
  160.     if itemDetail then
  161.     local colonIndex = string.find(itemDetail.name, ":", 1, true)
  162.     local name = string.sub(itemDetail.name, colonIndex + 1)
  163.       print("Slot " .. i .. ": " .. itemDetail.count .. "x " .. name)
  164.     end
  165.   end
  166. end
  167.  
  168. -- Vérifie le niveau de carburant de la turtle
  169. function check_fuel_level()
  170.   local fuel_level = turtle.getFuelLevel()
  171.   if fuel_level == "unlimited" then
  172.     print("Le niveau de carburant est illimité.")
  173.     return
  174.   end
  175.  
  176.   print("Niveau de carburant : " .. fuel_level)
  177.   if fuel_level < 10 then
  178.     print("Le niveau de carburant est faible, recherche de carburant dans l'inventaire...")
  179.     for i = 1, 16 do
  180.       turtle.select(i)
  181.       if turtle.refuel(0) then
  182.         local fuel_count = turtle.getItemCount()
  183.         local needed_fuel = math.ceil((10 - fuel_level) / 80)
  184.         if fuel_count >= needed_fuel then
  185.           turtle.refuel(needed_fuel)
  186.           print("Carburant trouvé et rechargé.")
  187.           return
  188.         end
  189.       end
  190.     end
  191.  
  192.     print("Aucun carburant trouvé, en attente...")
  193.     while turtle.getFuelLevel() < 10 do
  194.       os.pullEvent("turtle_inventory")
  195.     end
  196.     print("Carburant ajouté.")
  197.   end
  198. end
  199.  
  200. --[[function MineLayer() -- Fonction pour miner une ligne
  201.   for i=1,10 do
  202.     check_fuel_level()
  203.   -- miner le bloc et l'ajouter au tableau des blocs minés
  204.     local success, data = turtle.inspect()
  205.     if success then
  206.     local block = data.name
  207.     local metadata = data.state -- Récupère les métadonnées
  208.     if metadata and metadata.type then
  209.       print(metadata.type)
  210.     end
  211.     blocks[block] = (blocks[block] or 0) + 1
  212.   end
  213.   turtle.dig()
  214.     turtle.forward()
  215.   end
  216. end]]
  217.  
  218. function MineLayer() -- Fonction pour miner une ligne
  219.     for i=1,5 do
  220.         check_fuel_level()
  221.         -- Miner le bloc et l'ajouter au tableau des blocs minés
  222.         local success, data = turtle.inspect()
  223.         if success then
  224.             local block = data.name
  225.             if data.state and data.state.type then
  226.               block = block .. ":" .. data.state.type
  227.             elseif data.state and data.state.variant then
  228.               block = block .. ":" .. data.state.variant
  229.             elseif data.state and data.state.oretype then
  230.               block = block .. ":" .. data.state.oretype
  231.             end
  232.             if not blocks[block] then
  233.                 blocks[block] = { count = 0 }
  234.             end
  235.             blocks[block].count = blocks[block].count + 1
  236.         end
  237.         turtle.dig()
  238.         turtle.forward()
  239.     end
  240. end
  241.  
  242. function RetourDepart()
  243.   -- retourne au point de départ
  244.   for i=1,5 do
  245.   check_fuel_level()
  246.   turtle.back()
  247.   end
  248. end
  249.  
  250. -- Main Program
  251. --for u =1,10 do
  252. MineLayer()
  253. RetourDepart()
  254. --print(u)
  255. --end
  256.  
  257. -- afficher les blocs et leur quantité
  258. displayBlocks(blocks)
  259. --CheckInventory()
  260.  
  261. -- envoyer les données au moniteur
  262. sendBlocks(blocks)
  263.  
  264. --print("Niveau de carburant : " .. turtle.getFuelLevel())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement