jille_Jr icon

CC: Grind stone display

jille_Jr | PRO | 05/08/14 07:41:14 PM UTC | 0 ⭐ | 468 👁️ | Never ⏰ | []
Lua |

2.95 KB

|

None

|

0 👍

/

0 👎

 
--(( Settings ))--
 
-- side relative to the computer
-- or path if you are using cables
local grindstone = peripheral.wrap("left")
or error("Unable to wrap the grind stone!",0)
 
local monitor = peripheral.wrap("top")
or error("Unable to wrap the monitor!",0)
 
-- colors
local BG = colors.black
local LOW = colors.red
local MED = colors.orange
local HIGH = colors.yellow
local TOP = colors.lime
 
-- how many times to draw per second
local updateRate = 5
 
--(( Variables ))--
 
local ores = 0
local dusts = 0
local total = 0
local oresPercentage = 0
local dustsPercentage = 0
 
local mW,mH = monitor.getSize()
local tW,tH = term.getSize()
 
--(( Functions ))--
 
local function getAllStacks( inv )
    local t = {}
    local size = inv.getInventorySize()
    for slot = 1,size do
        local item = inv.getStackInSlot(slot) or {}
 
        item.rawName = item.rawName or ""
        item.qty = item.qty or 0
        item.dmg = item.dmg or 0
        item.name = item.name or ""
        item.id = item.id or 0
 
        t[slot] = item
    end
    return t
end
 
local function updateItems()
    local items = getAllStacks(grindstone)
 
    ores = 0
    dusts = 0
    total = 0
    percentage = 0
 
    for slot,item in pairs(items) do
        -- ores
        if slot >= 1 and slot <= 3 then
            ores = ores + item.qty
        end
 
        -- dusts
        if slot >= 4 and slot <= 6 then
            dusts = dusts + item.qty
        end
    end
 
    total = ores + dusts
    oresPercentage = ores / total
    dustsPercentage = dusts / total
 
    if ores == 0 then oresPercentage = 0 end
    if dusts == 0 then dustsPercentage = 0 end
end
 
local function draw()
    -- drawing a big bar over the monitor
    -- drawing from left to right
 
    -- black area
    monitor.setBackgroundColor(BG)
    for column = math.ceil(dustsPercentage * mW),mW,1 do
        for row = 1,mH,1 do
            monitor.setCursorPos(column,row)
            monitor.write(" ")
        end
    end
 
    -- colored area
    if dustsPercentage > 0 then
        -- 0% to 33%
        if dustsPercentage < 1/3 then
            monitor.setBackgroundColor(LOW)
        end
        -- 33% to 66%
        if dustsPercentage >= 1/3
        and dustsPercentage < 2/3 then
            monitor.setBackgroundColor(MED)
        end
        -- 66% to 100%
        if dustsPercentage >= 2/3
        and dustsPercentage < 1 then
            monitor.setBackgroundColor(HIGH)
        end
        -- 100%
        if dustsPercentage == 1 then
            monitor.setBackgroundColor(TOP)
        end
    end
    for column = 1,math.floor(dustsPercentage * mW),1 do
        for row = 1,mH,1 do
            monitor.setCursorPos(column,row)
            monitor.write(" ")
        end
    end
 
    -- on screen info
    term.clear()
 
    local ore = "ore" if ores > 1 then ore = "ores" end
    local dust = "dust" if dusts > 1 then dust = "dusts" end
 
    local text1 = "Currently "..ores.." "..ore.." and ".. dusts.." "..dust
    local text2 = math.floor(dustsPercentage*100).."% done"
 
    term.setCursorPos(tW/2-#text1/2,tH/2-1)
    term.write(text1)
    term.setCursorPos(tW/2-#text2/2,tH/2+1)
    term.write(text2)
end
 
--(( Main program ))--
 
-- main loop
while true do
    updateItems()
    draw()
    sleep(1/updateRate)
end

Comments