satisfactory/graph.py

67 lines
1.5 KiB
Python
Raw Normal View History

2019-04-27 18:50:04 +10:00
#!/usr/bin/env python3
2019-07-14 13:25:37 +10:00
"""
This script takes a target item and produces GraphViz formatted output that
describes the gross production dependencies required to produce the item.
"""
import satisfactory
2019-04-27 18:50:04 +10:00
import os
from typing import Iterable
def graph(cookbook: satisfactory.Cookbook, targets: Iterable[str]):
2019-07-14 13:25:37 +10:00
"""
Output the directed graph representing the production chain needed to
produce all the provided targets.
:param cookbook:
:param targets:
:return:
"""
print("digraph G {")
seen = set()
remain = set(targets)
while remain:
output = remain.pop()
for need in cookbook.recipes(output)[0].input:
print(f"{need} -> {output}")
if need not in seen:
remain.add(need)
seen.add(output)
2019-07-14 13:25:37 +10:00
print("}")
if __name__ == '__main__':
def main():
import argparse
root = os.path.dirname(__file__)
recipe_root = os.path.join(root, 'data', 'recipes')
parser = argparse.ArgumentParser()
parser.add_argument('--data', type=str, default=recipe_root)
2019-07-14 13:25:19 +10:00
parser.add_argument('--target', type=str, nargs="*")
parser.add_argument('--prefer', type=str, nargs="*")
args = parser.parse_args()
recipes = satisfactory.Cookbook(args.data)
if args.prefer:
for alternative in args.prefer:
recipes.prefer(alternative)
if args.target:
2019-07-14 13:25:19 +10:00
graph(recipes, args.target)
else:
graph(recipes, recipes.components())
main()