I made a recipe planner in python!

Fan Creation

DISCLAIMER: I know that there are many amazing factory planners as mods and otherwise, but in the spirit of figuring out/doing everything myself, I figured I would make my own!

I got annoyed with constantly doing math on paper to get the perfect ratios, especially when it came down to factoring in modules, so I wanted a tool that would help me plan out exactly what I need for each quantity of item I wanted to make.

I started off by choosing python and using tkinter because I kind of felt like learning tkinter. I don't regret it, but using something more robust like QT would have made alot of the coding much easier. And my final result much prettier.

Then, I decided to manually import each item into python by painstakingly writing down their properties (e.g., names, crafting times, ingredients, etc.). This was chosen more by stubbornness/laziness than anything, as I am sure I could have imported this information directly from the game files in some way or another. But I was too lazy to figure out how and it was kind of relaxing to listen to music and manually enter in all the information. I haven't finished porting in every ingredient, mainly just the ones required for science are done.

Next, I needed a way to structure the information in an easy to use format. I decided to make an object called a "Plan". a Plan has the following properties:

  • Desired Ingredient: the item you want to make together with how many per second
  • Recipe: The recipe that you want to use. A recipe is a list of Ingredients
  • Building: The building used to make the desired ingredient. A building has a crafting speed as well as potentially having module slots
  • Beacon Modules: You can add beacon modules to the plan

Once the plan is set up, it has various functions to calculate helpful amounts, e.g.,

  • How many buildings you need to produce the desired amount (taking into account modules, of course)
  • How many input ingredients you need

Now that I had a way to represent a single Plan, I wanted a way to customize it further. I wanted to be able to modify all parts of it, e.g., if my plan is advanced circuits, I may want to think about how much iron I need instead of how many electronic circuits I need. To do this, I created a tree-structure called a "PlanNode".

A PlanNode is simply a Plan with a list of children Plans (one for each required ingredient) as well as a parent Plan. With this tree structure, it is very easy to recursively navigate the whole stack of Plans in order to compile handy metrics like the total amount of X ingredient needed to make your desired ingredient.

With that structure done, all I had to do was make (relatively) pretty GUI objects that let me do things like:

  • Change the desired ingredient
  • Change the recipe used for the ingredient
  • Add or remove modules to buildings and beacons
  • Show the total amount of input ingredients required (these are the sum of all the leaves of the tree)
  • View the sub-ingredients and modify their respective plans

There are still many features to add to make it more polished and have more quality of life features, but overall I am very happy with its current state! The biggest feature that I will be adding next is a way to apply module settings to the entire graph: as it is right now, modules have to be added one by one for each ingredient, which is obviously not too fun for something like space science.

I'm not sure the best way to share this with people, but I uploaded it to git so maybe people can download it if they are curious to try it. If there is a better way let me know. Overall I just thought I would share what I did because I am pretty proud of it. https://github.com/JayJackman/factorio

Here are some images that show it in action! https://imgur.com/a/ChYRsTA

Posted Oct 7, 2022, 1:51 AM UTC · edited

4017
sweetcornwhiskey•

Looks great! How does it perform with complex systems like advanced oil?

5
ComplexMuffinOP•

Right now it’s ok but not amazing. You can choose the recipes that you want to use for each product that you need, but it doesn’t have any fancy math to figure out what to do with extra outputs. It just adds the extra outputs to the overall outputs and then I can figure out what to do with them. Eventually I want to add a “smart” recipe for things like petroleum where I can do advanced oil cracking and convert the heavy and light down into petroleum. As it stands right now if I choose advanced oil processing I will just have the heavy and light as extra outputs.

I added a picture onto the imgur link so you can see how it looks!

4
sweetcornwhiskey•

Well it's a great program so far. I'm sure it'll be better as you improve on it over time :)

2
someone8192•

Imho it needs mod support. Just base game items will frustrate everyone who gets used to it. Most people switch to some mods eventually

2
ComplexMuffinOP•

Actually I started it by playing with space exploration! If you notice, the module icons are space exploration images instead of the vanilla ones. But then I switched back to a vanilla play through so I changed it to vanilla, but there are space exploration recipes in the code files (out of date I’m sure).

But yah to get mod support I will have to learn how to read the game files and import the ingredients/recipes, which I’m sure isn’t hard. It would be a fun exercise that I’ll probably do at some point!

1
someone8192•

easiest way to do it would be your own mod (they are written in lua) which export the final game data for your python tool.

i guess someone could even fireup a webserver in lua and provide a live api. that way you could also include current research state

2
ComplexMuffinOP•

I might just go and do that! Would make it easy to add new mods and keep up to date with any updates

1
lettsten•

There's an error here. I recommend not having identifiers that are so similar to each other. In this case it would help to refer to the static variable using its full qualifier, i.e. Module.moduleTypes, like you do on line 17.

I didn't look too much at the code, but at first glance it seems clean, very nice 👌🏽

2
ComplexMuffinOP•

Good catch! I’ll patch it in and give you some credit in the commit :P and yah, using the full qualifier is a great idea for clarity!

1
dddontshoot•

For some reason I'm suspicious of calculators like this. I can't help wondering if the calculator has data from the the most resent version of Factorio. What about mods, does the calculator include them? Are they up to date?

I spend to much energy double checking everything, and just give up and design scalable factories instead.

If we figure out how to export the recipe data from Factorio, your program could solve all of that because it can be fed with exactly the right information for whatever version or mods I'm using at the time.

1
dddontshoot•
2
dddontshoot•

This mod doesn't look at recipes, it exports the location of each entity in your base.

I could write a similar one for recipes.

2
Physical_Florentin•

No need for a mod. See this post for a command to export recipe data to JSON.

2
ComplexMuffinOP•

Well this looks quite helpful!

1
Proxy_PlayerHDSupremus Avaritia•

i mean with ingame calculator mods like Factory Planner, it wouldn't make any sense to use a seperate list of recipes when it just has direct access to them from the game itself. that's why those kinds of calculators are pretty much always correct and compatibile with most if not all mods.

only external calculators like OP's, or this one: https://kirkmcdonald.github.io/calc.html have the chance to be incorrect

2
ComplexMuffinOP•

Yah I agree, I actually did find a recipe error that came from out of date recipes a couple days ago. So my method of manually importing the recipes was definitely not good. Based on the feedback I think my next task will be to write an import function! I don’t think it should be hard, just need to know the place where the files are kept and then figure out a way to parse them, I would imagine. The fact that mods exist leads me to believe that the item information shouldn’t be too hard to find.

1