ceskyDJ

ComputerCraft - Tezebni program (Normal turtles)

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