SMRs

by dorfl

Small modular nuclear reactors designed to be deployed off-grid at remote sites. SMRs are manufactured as sealed units with a fixed energy capacity, including both fuel and generator components. This means they can be quickly setup anywhere but once depleted must be returned to base for refurbishment.

Content
3 years ago
0.17 - 1.1
805
Power

b Bugfix for Nanobots & General Ghost Revival

3 years ago

First off I want to say I LOVE this mod so far, i'm playing through Space Exploration and excited to use these on remote bases. I play with Nanobots and there's an interaction issue with SMRs because we're missing a hook for ghost revives in your controller.

I took some time to play with the code and have a tested fix for you - the event looks a little different from an on_built, but still carries the entity & tags. I also wasn't sure if an SMR was supposed to be 'damaged' when placed given how many things perform auto-repairs, so there's a health set line at the end you may or may not want to keep. I'd appreciate if you could roll this in (or add support yourself, either way!)

I'm going to be playing with these either way, thank you again for a very clever mod!

local function tbl_find(t, val)
    for k,v in pairs(t) do 
        game.print(k .. "=" .. v)
        if k == val then return v end
    end
    return nil
end    

local function on_create_entity(event)
    local entity = event.created_entity or event.entity

    if entity and entity.valid and prefixed(entity.name, 'smr-generator-') then
        push(mod.entities, entity)

        local energy = nil
        local health = nil

        if event.stack then
            energy = event.stack.get_tag('buffer_energy')
            health = event.stack.get_tag('real_health')
        elseif event.tags then
            energy = tbl_find(event.tags, 'buffer_energy')
            health = tbl_find(event.tags, 'real_health')
        end

        if energy then
            entity.energy = tonumber(energy)
            entity.health = tonumber(health)
        else
            entity.energy = entity.prototype.electric_energy_source_prototype.buffer_capacity
            entity.health = entity.prototype.max_health -- Unsure if reduced HP on placement was intentional.
        end
    end
end

-- Change in attach_events to add handler for script_raised_revive
script.on_event({defines.events.on_built_entity, defines.events.on_robot_built_entity, defines.events.script_raised_revive}, on_create_entity)

New response