ceskyDJ

ComputerCraft - Tezebni program (Advanced turtles)

Jul 25th, 2015
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.06 KB | None | 0 0
  1. -- <Práce s textem> --
  2.     -- Nastavení barvy textu
  3.     local function c(number)
  4.         term.setTextColor(number)
  5.     end
  6.  
  7.     -- Barvy
  8.     local green = 8192
  9.     local blue = 2048
  10.     local yellow = 16
  11.     local red = 16384
  12. -- </Práce s textem> --
  13.  
  14.  
  15. -- Získání parametrů
  16. local params = { ... }
  17. local lenght = params[1]
  18. local weight = params[2]
  19. local torches = params[3]
  20.  
  21. local function ClearTerminal()
  22.     term.clear()
  23.     term.setCursorPos(1,1)
  24. end
  25.  
  26. local function WriteWelcomeMessage()
  27.     ClearTerminal()
  28.    
  29.     c(green)
  30.     print("Mine")
  31.     c(blue)
  32.     print("====\n")
  33.     sleep(1)
  34. end
  35.  
  36. local function WriteParameters()
  37.     -- Vypsání parametrů
  38.     c(yellow)
  39.     print("Vybral jste\n***********")
  40.     print("Delka: ", lenght)
  41.     print("Sirka: ", weight)
  42.     print("Louce: ", torches)
  43.     print("")
  44. end
  45.  
  46. local function FuelController()
  47.     local fuelLevel = turtle.getFuelLevel()
  48.     local wantLevel = (lenght * weight) + weight + 5 -- Délka * šířka + dodatečný pohyb + rezerva
  49.    
  50.     if(fuelLevel >= wantLevel) then
  51.         c(green)
  52.         print("Zelva ma dost paliva")
  53.         return true
  54.     else
  55.         local missingFuel = wantLevel - fuelLevel
  56.         c(red)
  57.         print("Je treba doplnit ", missingFuel, " paliva!!")
  58.         c(yellow)
  59.         print("Palivo vlozte do posledniho slotu")
  60.        
  61.         turtle.select(16)
  62.        
  63.         while turtle.getFuelLevel() <= wantLevel do
  64.             local add = turtle.refuel(1)
  65.             if(add) then
  66.                 c(green)
  67.                 print("Palivo vlozeno")
  68.             else
  69.                 c(red)
  70.                 print("Nedostatek paliva!!")
  71.                 print("Vlozte jeste: ", wantLevel - turtle.getFuelLevel())
  72.                 sleep(3)
  73.             end
  74.         end
  75.        
  76.         c(green)
  77.         print("Zelva ma dost paliva")
  78.         return true
  79.     end
  80. end
  81.  
  82. local function TorchController()
  83.     if(torches == "ano") then
  84.         local wantCount = (lenght * weight) / 25 + 5 -- Délka * šířka / průměrný počet loučí + rezerva
  85.         local stackCount = math.ceil(wantCount / 64)
  86.         local torchCount = 0
  87.        
  88.         for i = 1, stackCount do
  89.             turtle.select(i)
  90.             local data = turtle.getItemDetail()
  91.            
  92.             if(data == nil) then
  93.                 data = ""
  94.             end
  95.            
  96.             if(data.name == "minecraft:torch") then
  97.                 torchCount = torchCount + turtle.getItemCount()
  98.             end
  99.         end
  100.        
  101.         if(torchCount >= wantCount) then
  102.             c(green)
  103.             print("Zelva ma dost louci")
  104.            
  105.             return true
  106.         else
  107.             while torchCount < wantCount do
  108.                 c(red)
  109.                 print("Nedostatek louci!!")
  110.                 print("Vlozte jeste: ", wantCount - torchCount)
  111.                
  112.                 for i = 1, stackCount do
  113.                     if(i == 1) then
  114.                         torchCount = 0
  115.                     end
  116.                
  117.                     turtle.select(i)
  118.                     local data = turtle.getItemDetail()
  119.                    
  120.                     if(data == nil) then
  121.                         data = ""
  122.                     end
  123.                    
  124.                     if(data.name == "minecraft:torch") then
  125.                         torchCount = torchCount + turtle.getItemCount()
  126.                     end
  127.                 end
  128.                
  129.                 sleep(3)
  130.             end
  131.            
  132.             if(torchCount >= wantCount) then
  133.                 c(green)
  134.                 print("Zelva ma dost louci")
  135.                
  136.                 return true
  137.             end
  138.         end
  139.     else
  140.         return true
  141.     end
  142. end
  143.  
  144. local function DropInventory()
  145.     for i = 1, 16 do
  146.         turtle.select(i)
  147.         data = turtle.getItemDetail()
  148.        
  149.         if(data == nil) then
  150.             data = ""
  151.         end
  152.        
  153.         if(data.name ~= "minecraft:torch") then
  154.             turtle.drop()
  155.         end
  156.     end
  157. end
  158.  
  159. local function Back(i)
  160.     if(i % 2 == 1) then
  161.         turtle.turnLeft()
  162.        
  163.         for a = 1, weight - 1 do
  164.             turtle.forward()
  165.         end
  166.        
  167.         turtle.turnLeft()
  168.        
  169.         for b = 1, lenght - 1 do
  170.             turtle.forward()
  171.         end
  172.        
  173.         turtle.down()
  174.         turtle.forward()
  175.         DropInventory()
  176.         turtle.turnLeft()
  177.         turtle.turnLeft()
  178.     else
  179.         turtle.turnRight()
  180.        
  181.         for a = 1, weight - 1 do
  182.             turtle.forward()
  183.         end
  184.        
  185.         turtle.turnLeft()
  186.         turtle.down()
  187.         turtle.forward()
  188.         DropInventory()
  189.         turtle.turnLeft()
  190.         turtle.turnLeft()
  191.     end
  192.    
  193.     -- Rozloučení
  194.     c(green)
  195.     print("Hotovo!!")
  196. end
  197.  
  198. local function CheckItemCount(i)
  199.     for j = 1, 16 do
  200.         turtle.select(j)
  201.         data = turtle.getItemDetail()
  202.        
  203.         if(turtle.getItemCount() == 64 and data.name ~= "minecraft:torch") then
  204.             -- Vrátit se na výchozí pozici a vyprádnit inventář
  205.             turtle.turnLeft()
  206.            
  207.             for a = 1, i - 1 do
  208.                 turtle.forward()
  209.             end
  210.                
  211.             turtle.turnLeft()
  212.            
  213.             for b = 1, lenght - 1 do
  214.                 turtle.forward()
  215.             end
  216.            
  217.             turtle.down()
  218.             turtle.forward()
  219.            
  220.             DropInventory()
  221.            
  222.             turtle.turnRight()
  223.             turtle.turnRight()
  224.            
  225.             -- Vrátit se do pozice, která byla opuštna pro vyprázdnění
  226.             turtle.forward()
  227.             turtle.up()
  228.            
  229.             for c = 1, lenght do
  230.                 turtle.forward()
  231.             end
  232.            
  233.             turtle.turnRight()
  234.            
  235.             for d = 1, weight - 1 do
  236.                 turtle.forward()
  237.             end
  238.            
  239.             turtle.turnLeft()
  240.         end
  241.     end
  242. end
  243.  
  244. local function Mine()
  245.     -- Vybrat první políčko
  246.     turtle.select(1)
  247.    
  248.     -- Úvodní text
  249.     ClearTerminal()
  250.     c(yellow)
  251.     print("Tezim...")
  252.  
  253.     -- Želva se dostane na výchozí pozici
  254.     turtle.dig()
  255.     turtle.forward()
  256.     turtle.digUp()
  257.     turtle.up()
  258.     turtle.digUp()
  259.    
  260.     for i = 1, weight do
  261.         c(yellow)
  262.         print("Rada cislo: ", i)
  263.        
  264.         -- Vytěž jednu řadu
  265.         for j = 1, lenght - 1 do
  266.             turtle.dig()
  267.             turtle.forward()
  268.             turtle.digDown()
  269.             turtle.digUp()
  270.            
  271.             if(j % 4 == 0 and i % 4 == 0 and torches == "ano") then
  272.                 wantSlot = 1
  273.                
  274.                 for k = 1, wantSlot do
  275.                     turtle.select(k)
  276.                     data = turtle.getItemDetail()
  277.                    
  278.                     if(data == nil) then
  279.                         data = ""
  280.                     end
  281.                    
  282.                     if(data.name == "minecraft:torch") then
  283.                         wantSlot = k - 1
  284.                         turtle.placeDown()
  285.                     else
  286.                         wantSlot = wantSlot + 1
  287.                     end
  288.                 end
  289.             end
  290.         end
  291.        
  292.         -- Vyprázdnění inventáře želvy
  293.         if(i < tonumber(weight) and i % 2 == 1) then
  294.             CheckItemCount(i)
  295.         end
  296.        
  297.         -- Otočení podle aktuální řady
  298.         if(i < tonumber(weight)) then
  299.             if(i == 1 or i % 2 == 1) then
  300.                 turtle.turnRight()
  301.                 turtle.dig()
  302.                 turtle.forward()
  303.                 turtle.turnRight()
  304.                 turtle.digUp()
  305.                 turtle.digDown()
  306.             elseif(i % 2 == 0) then
  307.                 turtle.turnLeft()
  308.                 turtle.dig()
  309.                 turtle.forward()
  310.                 turtle.turnLeft()
  311.                 turtle.digUp()
  312.                 turtle.digDown()
  313.             end
  314.         else
  315.             Back(i)
  316.         end
  317.     end
  318. end
  319.  
  320. -- Řídící místo
  321. WriteWelcomeMessage()
  322. WriteParameters()
  323. sleep(2)
  324. if(FuelController() and TorchController()) then
  325.     sleep(3)
  326.     Mine()
  327. end
Advertisement
Add Comment
Please, Sign In to add comment