This is a simple Python script which generates an analysis of each recipe into its component parts, and delivers insight into the balance and progression of a mod.
The .csv files used as input are generated in Factorio, by loading a new game with only ~base~ and your chosen mod enabled, and running the following Lua code:
This is a literate script. The source code is embedded in these code blocks, and tangled into the Python script using org-babel. This allows me to write a description of what I want to do, and comment on it without resorting to ~// /* */~ ugly comments.
Better yet, I can include [[Lua code generating a .csv file containing all recipes using the items in the whitelist.][links directly to places within the file]] and easily cross-reference what I'm doing with the documentation/ design.
If you open this org file raw (e.g. by clicking [[https://git.bajsicki.com/phil/factorio-recipe-analyzer/raw/branch/main/README.org][here]]), you will see that there are a number of different blocks.
The easiest way to generate outputs from here is to open this file in Emacs, and run ~M-x org-babel-tangle~. If you're using [[https://github.com/doomemacs/doomemacs][Doom Emacs]], the default key binding is ~C-c C-v C-t~.
*Important note:* the files in ~./mod-recipes~ are working files. They're not intrinsic parts of the software - they're there primarily for testing and development.
I don't own the source csv files generated by Factorio, nor the mods the script is pulling from. The Python script itself is GPLv3, with the exception of third-party libraries licensed otherwise.
Create an org-mode file with all the calculations included, and complete, for a clear overview of recipe progression, subgroups, item inputs and outputs, and total cost.
For this, we'll likely want to include some metadata, like creation date, mod name, number of ingredients, maybe the total amount of raw mats needed to make one of everything?
** Make a list of products
That's the first column in the .csv file.
Read first column of the csv file and insert it into the .org (output) file.
2. Find (next) ingredient lines in this product section.
3. Pass the product and each direct input item and its number to ~raw-ingredients~. /(We can distinguish direct inputs from raw ingredients easily because org-mode supports multiple characters for defining lists. So we can just look for lines beginning with ~-~ and not really think about anything else.)/
This is not for analysis as much as helping Galdoc out with creating compat layers for [[https://github.com/Orion351/galdocs_manufacturing][Galdoc's Manufacturing.]]
Take above csv data dump from Factorio, then:
1. Figure out appropriate categories. This is the starting point we have:
- Telescoping, (inserters, belts, things that reach)
- Metalworking,
- Plastic,
- Wood,
- Stone,
- Glass,
- Electronics.
- And possibly in the future:
- Motors,
- Agriculture,
- Chemicals,
- Small Arms / Equipment.
This has to be done manually. The csv file already includes an empty first column, which lets us manually go over it and add the tags to each item.
2. Then pull the output template CSV file, which should have the following structure:
This structure allows for unambiguous selection of the relevant data from the csv file, for the following reasons:
1. Amounts and categories are paired by the matching number at the end of the column name.
2. These cannot be confused with the amounts themselves, because the column names include [a-zA-Z] characters.
3. The first column makes it easy to find whether an item belongs to a particular column or not.
4. Additionally, this way we can automate creating the output .csv template, since we can check what number of columns we need for each ingredient category.
- One line per product, each product getting its own copy of all the recipes that make it (gg slag).
- Each product gets assigned a clear category. No issues, no conflicts.
- Problem: differing amounts between the recipes, so amounts need to be stated in a sep. column.
- If we use an unusual separator symbol for fields with more than one value (e.g. long lists of ingredients), we can then parse them very easily and remove ambiguity with a simple [whitespace, comma or +] test. Then situations like 'tin' being found in 'tinned wire' won't be a problem.
/Note:/ If you have this open in Emacs, move your cursor into the code block and hit ~C-c C-c~ to execute the code. It's very handy if you have commands that you use often.
#+name: move the output from the above command to the mod-recipes directory for easy access.
From here on, all changes will take place on the basis of the ~intermediate.csv~ file, and outputs will be directed to ~output.csv~. This will prevent time loss in case of a mistyped command.
At the same time, we have 2843 entries to deal with. That's a lot, so let's start with the raw ingredients, and assign those using the following rough rules:
For this reason, it is important that we first cover the base materials.
1. If the item exists in vanilla as a raw ingredient, it should be treated as such in the compatibility layer as well. If it is made craftable by a mod, its ingredients must also be tagged as raw materials.
2. The ingredient in question may not have a recipe of its own. E.g. iron ore in vanilla Factorio, or stiratite in AngelBob. In this case, it is a raw ingredient.