| 
									
										
										
										
											2021-04-01 11:40:58 +02:00
										 |  |  | # Factorio technologies are imported from a .json document in /data | 
					
						
							| 
									
										
										
										
											2021-04-05 15:37:15 +02:00
										 |  |  | from typing import Dict, Set | 
					
						
							| 
									
										
										
										
											2021-04-01 11:40:58 +02:00
										 |  |  | import json | 
					
						
							|  |  |  | import Utils | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-05 15:37:15 +02:00
										 |  |  | factorio_id = 2 ** 17 | 
					
						
							| 
									
										
										
										
											2021-04-01 11:40:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | source_file = Utils.local_path("data", "factorio", "techs.json") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | with open(source_file) as f: | 
					
						
							|  |  |  |     raw = json.load(f) | 
					
						
							|  |  |  | tech_table = {} | 
					
						
							| 
									
										
										
										
											2021-04-05 15:37:15 +02:00
										 |  |  | technology_table = {} | 
					
						
							|  |  |  | requirements = {}  # tech_name -> Set[required_technologies] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Technology():  # maybe make subclass of Location? | 
					
						
							|  |  |  |     def __init__(self, name, ingredients): | 
					
						
							|  |  |  |         self.name = name | 
					
						
							|  |  |  |         global factorio_id | 
					
						
							|  |  |  |         self.factorio_id = factorio_id | 
					
						
							|  |  |  |         factorio_id += 1 | 
					
						
							|  |  |  |         self.ingredients = ingredients | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def get_required_technologies(self): | 
					
						
							|  |  |  |         requirements = set() | 
					
						
							|  |  |  |         for ingredient in self.ingredients: | 
					
						
							|  |  |  |             if ingredient in recipe_sources: # no source likely means starting item | 
					
						
							|  |  |  |                 requirements |= recipe_sources[ingredient] # technically any, not all, need to improve later | 
					
						
							|  |  |  |         return requirements | 
					
						
							| 
									
										
										
										
											2021-04-01 11:40:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-05 15:37:15 +02:00
										 |  |  |     def __hash__(self): | 
					
						
							|  |  |  |         return self.factorio_id | 
					
						
							| 
									
										
										
										
											2021-04-01 11:40:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # recipes and technologies can share names in Factorio | 
					
						
							| 
									
										
										
										
											2021-04-05 15:37:15 +02:00
										 |  |  | for technology_name in sorted(raw): | 
					
						
							|  |  |  |     data = raw[technology_name] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-01 11:40:58 +02:00
										 |  |  |     factorio_id += 1 | 
					
						
							| 
									
										
										
										
											2021-04-05 15:37:15 +02:00
										 |  |  |     # not used yet | 
					
						
							|  |  |  |     # if data["requires"]: | 
					
						
							|  |  |  |     #     requirements[technology] = set(data["requires"]) | 
					
						
							|  |  |  |     current_ingredients = set(data["ingredients"]) | 
					
						
							|  |  |  |     technology = Technology(technology_name, current_ingredients) | 
					
						
							|  |  |  |     tech_table[technology_name] = technology.factorio_id | 
					
						
							|  |  |  |     technology_table[technology_name] = technology | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-01 11:40:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-05 15:37:15 +02:00
										 |  |  | recipe_sources = {}  # recipe_name -> technology source | 
					
						
							| 
									
										
										
										
											2021-04-01 11:40:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | for technology, data in raw.items(): | 
					
						
							| 
									
										
										
										
											2021-04-05 15:37:15 +02:00
										 |  |  |     for recipe in data["unlocks"]: | 
					
						
							|  |  |  |         recipe_sources.setdefault(recipe, set()).add(technology) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-01 11:40:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-05 15:37:15 +02:00
										 |  |  | del (raw) | 
					
						
							|  |  |  | lookup_id_to_name: Dict[int, str] = {item_id: item_name for item_name, item_id in tech_table.items()} |