Adding technologies to prerequisites

Place to post guides, observations, things related to modding that are not mods themselves.
Post Reply
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Adding technologies to prerequisites

Post by darkfrei »

Hi devs and modders!

Here is very interesting mod request: viewtopic.php?f=33&t=80802

I need to add to technology prerequisites all technologies, that must be opened before this technology opening.

Another way to see the problem: The technology C enables recipes C1, C2 and C3 and the technology C has only one technology prerequisite: Technology A, that opens recipes A1, A2, A3 to product items iA1, iA2, iA3.
The mod creator has changed the mod and recipes, technologies few dozen times and doesn't noticed that the recipes C1, C2, C3 need items iA1, iA2, iA3 AND iB1, iB2, iB3 and hasn't added technology B (that enables recipes B1, B2, B3 to product items iB1, iB2, iB3) to prerequisites by technology C.

Is here easy way to check if we can or if we are need to add prerequisites to technology C?

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Adding technologies to prerequisites

Post by darkfrei »

I've made few tables, for example links between technologies:

Code: Select all

 -- Table with technology names and links to technology names, 
 -- where it was used as prerequisite and what this technology uses as prerequisite technologies.
    ["basic-electronics"] = {used_in = {"basic-mapping", "electric-inserter"}, uses = {"basic-mining"}},
    ["basic-mining"] = {used_in = {"basic-electronics", "basic-logistics"}},
    ["electric-inserter"] = {uses = {"basic-electronics"}},
    ["basic-logistics"] = {uses = {"basic-mining"}}
With this links I can build the whole technology tree just as table.
Now I can check all recipes, that have links to technologies, that marked here as "used_in".
I need actually not recipes, but result items and fluids, that will be used as ingredient in this recipe.
Attachments
AnyPyTech_0.0.1.zip
premod 0.18
(32.56 KiB) Downloaded 54 times

User avatar
Deadlock989
Smart Inserter
Smart Inserter
Posts: 2528
Joined: Fri Nov 06, 2015 7:41 pm

Re: Adding technologies to prerequisites

Post by Deadlock989 »

Industrial Revolution does this. It builds a cache of every ingredient required to unlock the recipes in a technology, and every technology required to unlock a recipe's result - so it essentially it knows where new ingredients "come from". It then dynamically builds a "prerequisite tree" and generates a new set of prereqs for every tech. There are limitations that have to be guided "by hand", for example, "conceptual prerequisites" where you want technologies to happen in an order but there are no ingredients involved - for example you want electronics to come after your first electricity generation even though circuits don't have any boilers in them, etc. It wasn't easy to figure out because it's several recursive passes, but once I got it right, it's surprisingly brief code.

The result is that I can change a single recipe and the tech tree adjusts itself to match, no additional fingers lifted.

The downside is that other mods can introduce circular dependencies in the tree very easily, depends whether you care about that or not.
Image

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Adding technologies to prerequisites

Post by darkfrei »

Deadlock989 wrote:
Wed Feb 05, 2020 10:43 pm
Industrial Revolution does this. It builds a cache of every ingredient required to unlock the recipes in a technology, and every technology required to unlock a recipe's result - so it essentially it knows where new ingredients "come from". It then dynamically builds a "prerequisite tree" and generates a new set of prereqs for every tech.
How it goes through?
For me it's something like that:
1. Find all technologies and delete all prerequisites; if we can make them, we can make them all.
2. Make new table for every technology, the main value are recipes, that will be unlocked by them.
3. Find ingredients by this recipes.
4. Set new prerequisites dependents on links ingredients-results.

Also somewhere:
5. Check if one recipe was not enabled by two or more technologies.
6. Check if one item or fluid was not produced by two or more recipes.

User avatar
Deadlock989
Smart Inserter
Smart Inserter
Posts: 2528
Joined: Fri Nov 06, 2015 7:41 pm

Re: Adding technologies to prerequisites

Post by Deadlock989 »

Yes, that sounds similar.
Image

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Adding technologies to prerequisites

Post by darkfrei »

Deadlock989 wrote:
Thu Feb 06, 2020 10:44 am
Yes, that sounds similar.
What with 5 and 6?

The item iA1 is ingredient by recipe rC1, that will be enabled with technology tC.

5. If Technology tA enables recipe rA1 for item iA1, but also technology tB has enabled recipe rA1, must the technology tC get technologies tA and tB as prerequisites?
xor
6. If Technology tA enables recipe rA1 for item iA1, but also technology tB has enabled recipe rB1 for item iA1 as second result, must the technology tC get technologies tA and tB as prerequisites?

User avatar
Deadlock989
Smart Inserter
Smart Inserter
Posts: 2528
Joined: Fri Nov 06, 2015 7:41 pm

Re: Adding technologies to prerequisites

Post by Deadlock989 »

To be honest I couldn't be bothered to find a general solution to that. In the end I scaled it back from a general "solution for all mods" and just focused on the vanilla tech tree, because the earliest implementation was screwing other mods too hard and I don't really care about whether they are well-integrated anyway. IR now has a hardcoded list of vanilla techs and just focuses on them and its own ones. I think focusing on a subset of technologies in your own mod is probably the better approach, you still have to steer it with manually added prereqs and you have no idea what those would be in any random mod pulled off the portal.

Because of this, it was easier to just have an ignore list. There is only a short list of "enrichment" type vanilla technologies which provide the same items/fluids as a previous recipe but via a different route or with a better ratio. It boils down to four technologies: advanced oil processing, coal liquefaction, Kovarex enrichment and nuclear fuel processing. I simply stuck them on an ignore list. Other "advanced processing" techs within IR and a handful of other mods got added to that list later.

The general solution that I couldn't be bothered to implement would be to detect any items/fluids which are "provided" by more than one technology, and then determine which of those technologies has the "shortest" route to being unlocked from a new game start, and ignore all the others as "providers". Alternatively, if you can assume that any "enrichment" type tech always has the basic version as a prequisite - e.g. Advanced Oil Processing has Basic Oil Processing somewhere in its "ancestry", and they both provide the three oil products as recipe results - you could check each technology in the "duplicate providers" list and then eliminate any which have any of the others as "ancestors". (By "ancestors" I mean a prerequisite, or a prerequisite of a prerequisite, etc.)

Caching the "ancestry" tree was very useful, it cuts down the runtime of the algorithm massively.
Image

User avatar
mrudat
Fast Inserter
Fast Inserter
Posts: 229
Joined: Fri Feb 16, 2018 5:21 am
Contact:

Re: Adding technologies to prerequisites

Post by mrudat »

I just posted https://mods.factorio.com/mod/OR-Nodes which addresses the problem of technology a depending on (say) plastic-bar, but plastic-bar is unlocked by technology 1 or technology 2.

It creates a synthetic technology that represents "The ability to create a plastic bar", and unlocks the technology when either of its prerequisites is researched.

Post Reply

Return to “Modding discussion”