ceskyDJ icon

ComputerCraft - Tezebni program (Normal turtles)

ceskyDJ | PRO | 07/25/15 05:58:48 PM UTC | 0 ⭐ | 495 👁️ | Never ⏰ | []
Lua |

5.65 KB

|

None

|

0 👍

/

0 👎

-- Získání parametrů
local params = { ... }
local lenght = params[1]
local weight = params[2]
local torches = params[3]
 
local function ClearTerminal()
    term.clear()
    term.setCursorPos(1,1)
end
 
local function WriteWelcomeMessage()
    ClearTerminal()
    
    print("Mine")
    print("====\n")
    sleep(1)
end
 
local function WriteParameters()
    -- Vypsání parametrů
    print("Vybral jste\n***********")
    print("Delka: ", lenght)
    print("Sirka: ", weight)
    print("Louce: ", torches)
    print("")
end
 
local function FuelController()
    local fuelLevel = turtle.getFuelLevel()
    local wantLevel = (lenght * weight) + weight + 5 -- Délka * šířka + dodatečný pohyb + rezerva
    
    if(fuelLevel >= wantLevel) then
        print("Zelva ma dost paliva")
        return true
    else
        local missingFuel = wantLevel - fuelLevel
        print("Je treba doplnit ", missingFuel, " paliva!!")
        print("Palivo vlozte do posledniho slotu")
        
        turtle.select(16)
        
        while turtle.getFuelLevel() <= wantLevel do
            local add = turtle.refuel(1)
            if(add) then
                print("Palivo vlozeno")
            else
                print("Nedostatek paliva!!")
                print("Vlozte jeste: ", wantLevel - turtle.getFuelLevel())
                sleep(3)
            end
        end
        
        print("Zelva ma dost paliva")
        return true
    end
end
 
local function TorchController()
    if(torches == "ano") then
        local wantCount = (lenght * weight) / 25 + 5 -- Délka * šířka / průměrný počet loučí + rezerva
        local stackCount = math.ceil(wantCount / 64)
        local torchCount = 0
        
        for i = 1, stackCount do
            turtle.select(i)
            local data = turtle.getItemDetail()
            
            if(data == nil) then
                data = ""
            end
            
            if(data.name == "minecraft:torch") then
                torchCount = torchCount + turtle.getItemCount()
            end
        end
        
        if(torchCount >= wantCount) then
            print("Zelva ma dost louci")
            
            return true
        else
            while torchCount < wantCount do
                print("Nedostatek louci!!")
                print("Vlozte jeste: ", wantCount - torchCount)
                
                for i = 1, stackCount do
                    if(i == 1) then
                        torchCount = 0
                    end
                
                    turtle.select(i)
                    local data = turtle.getItemDetail()
                    
                    if(data == nil) then
                        data = ""
                    end
                    
                    if(data.name == "minecraft:torch") then
                        torchCount = torchCount + turtle.getItemCount()
                    end
                end
                
                sleep(3)
            end
            
            if(torchCount >= wantCount) then
                print("Zelva ma dost louci")
                
                return true
            end
        end
    else
        return true
    end
end
 
local function DropInventory()
    for i = 1, 16 do
        turtle.select(i)
        data = turtle.getItemDetail()
        
        if(data == nil) then
            data = ""
        end
        
        if(data.name ~= "minecraft:torch") then
            turtle.drop()
        end
    end
end
 
local function Back(i)
    if(i % 2 == 1) then
        turtle.turnLeft()
        
        for a = 1, weight - 1 do
            turtle.forward()
        end
        
        turtle.turnLeft()
        
        for b = 1, lenght - 1 do
            turtle.forward()
        end
        
        turtle.down()
        turtle.forward()
        DropInventory()
        turtle.turnLeft()
        turtle.turnLeft()
    else
        turtle.turnRight()
        
        for a = 1, weight - 1 do
            turtle.forward()
        end
        
        turtle.turnLeft()
        turtle.down()
        turtle.forward()
        DropInventory()
        turtle.turnLeft()
        turtle.turnLeft()
    end
    
    -- Rozloučení
    print("Hotovo!!")
end
 
local function CheckItemCount(i)
    for j = 1, 16 do
        turtle.select(j)
        data = turtle.getItemDetail()
        
        if(turtle.getItemCount() == 64 and data.name ~= "minecraft:torch") then
            -- Vrátit se na výchozí pozici a vyprádnit inventář
            turtle.turnLeft()
            
            for a = 1, i - 1 do
                turtle.forward()
            end
                
            turtle.turnLeft()
            
            for b = 1, lenght - 1 do
                turtle.forward()
            end
            
            turtle.down()
            turtle.forward()
            
            DropInventory()
            
            turtle.turnRight()
            turtle.turnRight()
            
            -- Vrátit se do pozice, která byla opuštna pro vyprázdnění
            turtle.forward()
            turtle.up()
            
            for c = 1, lenght do
                turtle.forward()
            end
            
            turtle.turnRight()
            
            for d = 1, weight - 1 do
                turtle.forward()
            end
            
            turtle.turnLeft()
        end
    end
end
 
local function Mine()
    -- Vybrat první políčko
    turtle.select(1)
    
    -- Úvodní text
    ClearTerminal()
    print("Tezim...")
 
    -- Želva se dostane na výchozí pozici
    turtle.dig()
    turtle.forward()
    turtle.digUp()
    turtle.up()
    turtle.digUp()
    
    for i = 1, weight do
        print("Rada cislo: ", i)
        
        -- Vytěž jednu řadu
        for j = 1, lenght - 1 do
            turtle.dig()
            turtle.forward()
            turtle.digDown()
            turtle.digUp()
            
            if(j % 4 == 0 and i % 4 == 0 and torches == "ano") then
                wantSlot = 1
                
                for k = 1, wantSlot do
                    turtle.select(k)
                    data = turtle.getItemDetail()
                    
                    if(data == nil) then
                        data = ""
                    end
                    
                    if(data.name == "minecraft:torch") then
                        wantSlot = k - 1
                        turtle.placeDown()
                    else
                        wantSlot = wantSlot + 1
                    end
                end
            end
        end
        
        -- Vyprázdnění inventáře želvy
        if(i < tonumber(weight) and i % 2 == 1) then
            CheckItemCount(i)
        end
        
        -- Otočení podle aktuální řady
        if(i < tonumber(weight)) then
            if(i == 1 or i % 2 == 1) then
                turtle.turnRight()
                turtle.dig()
                turtle.forward()
                turtle.turnRight()
                turtle.digUp()
                turtle.digDown()
            elseif(i % 2 == 0) then
                turtle.turnLeft()
                turtle.dig()
                turtle.forward()
                turtle.turnLeft()
                turtle.digUp()
                turtle.digDown()
            end
        else
            Back(i)
        end
    end
end
 
-- Řídící místo
WriteWelcomeMessage()
WriteParameters()
sleep(2)
if(FuelController() and TorchController()) then
    sleep(3)
    Mine()
end

Comments