Electric Grid

by 0n0w1c

An overhaul of the electric network, engineer an Electric Grid! The overhaul is optional.

Overhaul
17 days ago
2.0
1.85K
Power

i (Resolved) On how to circumvent quality

8 months ago

Hi again. I have taken an interest in this problem of hard-coded quality, and by taking inspiration from various mods (like Quality Wagons), I have come upon the idea of a script which replaces any higher quality entities of a particular name with a normal variant right when it gets built. Here is an example of what it could look like (in control.lua):

function table_contains_value(table, element)
    for key, value in pairs(table) do
        if value == element then return true end
    end
    return false
end

local poles = {"small-electric-pole", "medium-electric-pole", "big-electric-pole"}

local function on_built(event)
    local entity = event.entity or event.destination
    local surface = entity.surface
    if entity.quality.level == 0 then return end
    if not table_contains_value(poles, entity.name) then return end
    local info = {
        name = entity.name,
        position = entity.position,
        -- quality simply ignored
        force = entity.force,
        fast_replace = true,
        player = entity.last_user,
    }
    entity.destroy()
    surface.create_entity(info)
end

script.on_event({
    defines.events.on_built_entity,
    defines.events.on_robot_built_entity,
    defines.events.on_space_platform_built_entity,
    defines.events.script_raised_built,
    defines.events.script_raised_revive,
    defines.events.on_entity_cloned -- uses event.destination
},  on_built
)

This way, quality won't have to be entirely disabled for the sake of this mod. Performance also seems to be no issue. Hope you find it useful!

8 months ago
(updated 8 months ago)

I love such suggestions!
I have considered an option such as this, it is a great way for most entities. Unfortunately, electric poles bring a complication... drag placement. If you destroy() the pole (immediately), it breaks drag placement. Now, if you are interested in that problem... take a look at my underground substation and control.lua and jobs_queue.lua. Maybe you will find something interesting there. Not like it is rocket science, I just have not seen it done before. it sort of makes a pseudo-spoilage for an entity. It enqueues the pole swap for 3 seconds after placement, time enough to have completed the drag placement of connecting poles.

Could I do something like this for all of the poles? Probably... but if a player can't use the Quality poles, they will not be pleased with the mod. As things stand now, sadly it comes down to Quality or my overhaul.

8 months ago

Ah, of course, it seems you have already looked deeply into this issue. To be honest, I only looked at this mod on a fairly superficial level, since there is a lot of code and it seems quite sophisticated, and I entirely missed that. That is a clever solution! But yeah, there are clearly many things to consider. I hope they update their API. Thanks for your explanation, I shall edify myself with further study!

7 months ago

Happy to report that the issue between this mod and Quality has been resolved with 2.0.29.

7 months ago

Well, that is something at least! Thanks for info.

New response