Print resistances of all enemy types

Place to get help with not working mods / modding interface.
Post Reply
vernir
Manual Inserter
Manual Inserter
Posts: 4
Joined: Thu Feb 08, 2024 3:06 am
Contact:

Print resistances of all enemy types

Post by vernir »

Hello. Never wrote for Factorio. Could you kick me in a right direction to help me write a script that prints all resistances of all enemy prototypes to the file? Or just how to iterate through all available Prototype/Units?

Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Print resistances of all enemy types

Post by Pi-C »

vernir wrote:
Thu Feb 08, 2024 3:15 am
Could you kick me in a right direction to help me write a script that prints all resistances of all enemy prototypes to the file? Or just how to iterate through all available Prototype/Units?
This can be done in two way:
  • Data stage: Create your own mod (containing info.json and data-final-fixes.lua) and run this in data-final-fixes.lua:

    Code: Select all

    local objects = {
    	 "unit",			-- Biters/spitters
    	 "unit-spawner", 	-- Spawners
    	 "turret",			-- Worms
     }
    for p, protos in pairs(objects) do
    	log("Type: "..protos) 
    	for p, proto in pairs(data.raw[protos] do
    		log(string.format("Ty %s:\tResistances: %s", p, serpent.block(proto.resistances))
    	end
    end
    
    Potential drawback: Mods that modify resistances in data-final-fixes.lua after your mod is done could falsify your results.
  • Directly in the game (all prototypes are final), you can run the following from the command line/chat console:

    Code: Select all

    /c protos = game.get_filtered_entity_prototypes{{filter = "type", type = { "unit", "unit-spawner", "turret"}}}
    for p, proto in pairs(protos) do 
    	log(string.format("Name: %s (%s)\tResistances: %s", p, proto.type, serpent.block(proto.resistances)))
    end
    
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

SoShootMe
Filter Inserter
Filter Inserter
Posts: 481
Joined: Mon Aug 03, 2020 4:16 pm
Contact:

Re: Print resistances of all enemy types

Post by SoShootMe »

Pi-C wrote:
Thu Feb 08, 2024 1:47 pm
Potential drawback: Mods that modify resistances in data-final-fixes.lua after your mod is done could falsify your results.
Instrument mode allows an instrument-after-data.lua that is guaranteed to run last.

There's also --dump-data (see Command line parameters).

Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Print resistances of all enemy types

Post by Pi-C »

SoShootMe wrote:
Thu Feb 08, 2024 3:45 pm
Instrument mode allows an instrument-after-data.lua that is guaranteed to run last.
Right, that would work. I've never used this mode before, though, so I didn't think of it. :-)
There's also --dump-data (see Command line parameters).
Definitely useful in general, but dumping all of data.raw seems a bit excessive in this particular case where the OP is interested only in one particular property of a subset of the game's prototypes.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

SoShootMe
Filter Inserter
Filter Inserter
Posts: 481
Joined: Mon Aug 03, 2020 4:16 pm
Contact:

Re: Print resistances of all enemy types

Post by SoShootMe »

Pi-C wrote:
Thu Feb 08, 2024 4:06 pm
SoShootMe wrote:
Thu Feb 08, 2024 3:45 pm
There's also --dump-data (see Command line parameters).
Definitely useful in general, but dumping all of data.raw seems a bit excessive in this particular case where the OP is interested only in one particular property of a subset of the game's prototypes.
I agree it is overkill, but the output will most likely also contain the answers to questions that follow "What are the resistances?" ;). (Also, it's much faster than loading the game and can easily be generated as part of an automated process.)

vernir
Manual Inserter
Manual Inserter
Posts: 4
Joined: Thu Feb 08, 2024 3:06 am
Contact:

Re: Print resistances of all enemy types

Post by vernir »

Pi-C wrote:
Thu Feb 08, 2024 1:47 pm
vernir wrote:
Thu Feb 08, 2024 3:15 am
Could you kick me in a right direction to help me write a script that prints all resistances of all enemy prototypes to the file? Or just how to iterate through all available Prototype/Units?
This can be done in two way:
  • Data stage: Create your own mod (containing info.json and data-final-fixes.lua) and run this in data-final-fixes.lua:

    Code: Select all

    local objects = {
    	 "unit",			-- Biters/spitters
    	 "unit-spawner", 	-- Spawners
    	 "turret",			-- Worms
     }
    for p, protos in pairs(objects) do
    	log("Type: "..protos) 
    	for p, proto in pairs(data.raw[protos] do
    		log(string.format("Ty %s:\tResistances: %s", p, serpent.block(proto.resistances))
    	end
    end
    
    Potential drawback: Mods that modify resistances in data-final-fixes.lua after your mod is done could falsify your results.
  • Directly in the game (all prototypes are final), you can run the following from the command line/chat console:

    Code: Select all

    /c protos = game.get_filtered_entity_prototypes{{filter = "type", type = { "unit", "unit-spawner", "turret"}}}
    for p, proto in pairs(protos) do 
    	log(string.format("Name: %s (%s)\tResistances: %s", p, proto.type, serpent.block(proto.resistances)))
    end
    
Thank you, that helps!

Post Reply

Return to “Modding help”