#Tech removal on death

1 messages ยท Page 1 of 1 (latest)

lethal badger
#

Hm, I might have been to quick to give you a solution. When you say descendants, what do you mean?

velvet forge
#

what I mean is that, for example, if you remove the military 4 science, it should clear spidertron, uranium ammo, atomic bomb, etc.

lethal badger
#

Yup, that is something different D:

velvet forge
#

maybe checking every tech that has the selected tech as a dependency?

lethal badger
velvet forge
#

prerequisite*

#

๐Ÿ’ก

lethal badger
#

Question, do you want these two also un-researched? or just the single row?

#

Else, only the ones in the rect will be un-researched

#

Test this out whenever you have the chance:

local function get_tech_and_neighbour(force, tech_name)
    local tech = force.technologies[tech_name]
    local main_req
    for _, data in pairs(tech.prerequisites) do
        main_req = data.name
        break
    end

    for _, data in pairs(force.technologies) do
        for _, req in pairs(data.prerequisites) do
            if main_req and req.name == main_req then
                force.technologies[data.name].researched = false
            end
        end
    end
end

script.on_event(
    defines.events.on_player_died,
    function(event)
        local player_index = event.player_index
        local player = game.get_player(player_index)
        local force = player.force

        local techs = {}
        for _, data in pairs(force.technologies) do
            if data.researched then
                techs[#techs + 1] = data.name
            end
        end

        local count = #techs

        if count > 0 then
            local tech_name = techs[math.random(1, count)]
            if tech_name then
                get_tech_and_neighbour(force, tech_name)
            end
        end
    end
)
velvet forge
#

damn, you cooked

#

thanks

lethal badger
#

A bit evil to unresearch that many techs, but a gameplay is a gameplay ๐Ÿ‘€

#

np!

velvet forge
#

I'll eventually add more death events, like an atomic bomb

#

or a behemoth biter spawning

velvet forge
#

pls

#

uhh.. it... didn't work?

lethal badger
#

Right, I just clear the single row

#

To clear the children is somewhat different

velvet forge
#

I'm going to sleep right now, I'll continue working on this tomorrow ๐Ÿ‘‹

lethal badger
#

Final version perhaps? yep

local function clear_tech(force, tech_name)
    local t = {}

    local function get_tech_and_neighbour(filtered_tech)
        -- iterate over all technologies and check if they have the filtered tech as a prerequisite
        -- if they do, set them to not researched
        for _, data in pairs(force.technologies) do
            for _, req in pairs(data.prerequisites) do
                if req.name == filtered_tech then
                    force.technologies[data.name].researched = false
                    force.technologies[req.name].researched = false
                    t[#t + 1] = data.name
                end
            end
        end
    end

    get_tech_and_neighbour(tech_name)

    if #t > 0 then
        for _, tech in pairs(t) do
            get_tech_and_neighbour(tech)
        end
    end
end

script.on_event(
    defines.events.on_player_died,
    function(event)
        local player_index = event.player_index
        local player = game.get_player(player_index)
        local force = player.force

        local techs = {}
        for _, data in pairs(force.technologies) do
            -- only add researched technologies to the list
            if data.researched then
                techs[#techs + 1] = data.name
            end
        end

        local count = #techs

        if count then
            local tech_name = techs[math.random(1, count)]
            if tech_name then
                -- clear the tech and all its prerequisites
                clear_tech(force, tech_name)
                game.print('Cleared ' .. tech_name .. ' for ' .. force.name)
            end
        end
    end
)
velvet forge
#

woah it's so cool thanks so much @lethal badger !

#

it works great!

velvet forge
#

Hey @lethal badger , sorry to ask you that but could you explain to me the last code you sent, as I'm currently re-writing it to add more features?

#

and when I started this project I didn't really know what I was copy-pasting

lethal badger
#

wavey

All the code does is, whenever a player dies it removes a technology recursive, meaning if a high tech was picked then all the sub-techs will be unresearched

velvet forge
#

I got this, I just don't understand lines 5, 6, 7, 19 and 33

lethal badger
#

Added some comments, let me know if it helps you