carmopereira / DU-Container-Monitor

This code is destined to Dual Universe game. This is a Container monitor for pure and ore materials. It can be used to monitor container percentages and it readouts. It was design to have a Container Hub element side each item name.
GNU General Public License v3.0
12 stars 10 forks source link

improvements #2

Open SvarogZ opened 3 years ago

SvarogZ commented 3 years ago

Please check all cycles and local variables across the code. Please see comments below

if core ~= nil then
     local elementsIdList = core.getElementIdList()
     for _,id in pairs(elementsIdList) do -- use ipairs instead
        local elementType = core.getElementTypeById(id):lower()
        if elementType:find("container") then
            name = core.getElementNameById(id) -- should be local
            if string.match(name, "CH_") or string.match(name, "C_") then
            temp = split(name, "_") -- should be local           
                table.insert(containersIdList , {id, temp[1], string.match(temp[2], "%d+"), string.sub(temp[2], -1), temp[3], hasValue(temp[4])})   
                --1: id, 2: type, 3: containercount, 4: containersize, 5: material, 6: description
        end                     
        end
     end
end

Improvement to color the line:

local function getColor(number)
    if number < 50 then
        return "rgb(255," .. utils.round(utils.map(number, 0, 40, 100, 255)) .. ",0)"
    else
        return "rgb(" .. utils.round(utils.map(number, 40, 100, 255, 155)) .. ",255,0)"
    end
end

This code cannot work with different containers in one hub. Please consider specifying the volume/capacity in the name of the hub.

carmopereira commented 3 years ago

Thanks for the input!

For the color function there's no no need to place more utils, rounds (more cpu) and take down the "minimum" "medium" and "max" threshold warning. For color conventions, I do endorse the use of hexs and web colors thru names. You can still use rgb, but it's a bit, duhhhh. (unless u need to do a transparency, and in that case, you should use an argb). I could make the color variables so you change them in parameters (if you please you can type rgb(x,y,z) if you like. If you don't like the 3 color settings, you can just workaround the settings.

For now I will not make different containers in one hub. We have character limit on the names (30), and it will be to damn confuse to accomplish that.

Last version brought the capacity of multiple "standalone" containers with the same ore, but but different sizes within one container hub is very tricky. CH_1L_3M_4S_IRON ?? Don't have space left...

For the temp variable in the first quote, i could make it local. It will prevent future unpleasantness if I reuse that name.

Thanks!

SvarogZ commented 3 years ago

Yes, the character limit does not allow to type everything. This is a brilliant idea and I started to use it in my code. However, I use direct entering "uid" of the hub. If this hub on the same screen it's easier. No neer to search it in a cycle. We have around 10k elements :)

Thank you!

P.S. if you add a formatted version of your code, it will be easier for visitors to give you feedback. Not everyone wants to copy this to the game or parse it.

SvarogZ commented 3 years ago

Alternatively, you can use the capacity instead: CH_196.0_IRON or CH_IRON_196.0

carmopereira commented 3 years ago

Added the locals on the core code. Don't quite understand why I should change from pairs to ipairs. other idea to avoid another cycle is to store the uids in the database. i'm uncertain if that helps, or how much miliseconds will be affected.