Skip to content

Commit

Permalink
Factorio: more cleanup of code. Makes it easier to add a max liquids …
Browse files Browse the repository at this point in the history
…allowed option.
  • Loading branch information
CaitSith2 committed Nov 26, 2021
1 parent 8af5855 commit b0bf66b
Showing 1 changed file with 23 additions and 28 deletions.
51 changes: 23 additions & 28 deletions worlds/factorio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,27 @@ def stage_write_spoiler(cls, world, spoiler_handle):
for recipe in world.worlds[player].custom_recipes.values():
spoiler_handle.write(f"\n{recipe.name} ({name}): {recipe.ingredients} -> {recipe.products}")

@staticmethod
def get_category(category: str, liquids: int) -> str:
categories = {1: "crafting-with-fluid",
2: "chemistry"}
return categories.get(liquids, category)

def make_quick_recipe(self, original: Recipe, pool: list, allow_liquids: int = 2) -> Recipe:
new_ingredients = {}
liquids_used = 0
for _ in original.ingredients:
new_ingredient = pool.pop()
if new_ingredient in liquids:
while liquids_used == allow_liquids and new_ingredient in liquids:
# liquids already at max for current recipe. Return the liquid to the pool, shuffle, and get a new ingredient.
pool.append(new_ingredient)
self.world.random.shuffle(pool)
new_ingredient = pool.pop()
liquids_used += 1
new_ingredients[new_ingredient] = 1
return Recipe(original.name, self.get_category(original.category, liquids_used), new_ingredients, original.products, original.energy)

def make_balanced_recipe(self, original: Recipe, pool: list, factor: float = 1, allow_liquids: int = 2) -> \
Recipe:
"""Generate a recipe from pool with time and cost similar to original * factor"""
Expand All @@ -197,7 +218,6 @@ def make_balanced_recipe(self, original: Recipe, pool: list, factor: float = 1,
remaining_num_ingredients = target_num_ingredients
fallback_pool = []
liquids_used = 0
category = original.category

# fill all but one slot with random ingredients, last with a good match
while remaining_num_ingredients > 0 and pool:
Expand Down Expand Up @@ -237,10 +257,6 @@ def make_balanced_recipe(self, original: Recipe, pool: list, factor: float = 1,
remaining_energy -= num * ingredient_energy
remaining_num_ingredients -= 1
if ingredient in liquids:
if liquids_used == 0:
category = "crafting-with-fluid"
elif liquids_used == 1:
category = "chemistry"
liquids_used += 1

# fill failed slots with whatever we got
Expand Down Expand Up @@ -269,16 +285,12 @@ def make_balanced_recipe(self, original: Recipe, pool: list, factor: float = 1,
remaining_energy -= num * ingredient_energy
remaining_num_ingredients -= 1
if ingredient in liquids:
if liquids_used == 0:
category = "crafting-with-fluid"
elif liquids_used == 1:
category = "chemistry"
liquids_used += 1

if remaining_num_ingredients > 1:
logging.warning("could not randomize recipe")

return Recipe(original.name, category, new_ingredients, original.products, original.energy)
return Recipe(original.name, self.get_category(original.category, liquids_used), new_ingredients, original.products, original.energy)

def set_custom_technologies(self):
custom_technologies = {}
Expand All @@ -305,24 +317,7 @@ def set_custom_recipes(self):
valid_pool += sorted(science_pack_pools[pack])
self.world.random.shuffle(valid_pool)
if pack in recipes: # skips over space science pack
original = recipes[pack]
new_ingredients = {}
liquids_used = 0
category = original.category
for _ in original.ingredients:
new_ingredient = valid_pool.pop()
if new_ingredient in liquids:
while liquids_used == 2 and new_ingredient in liquids:
valid_pool.append(new_ingredient)
self.world.random.shuffle(valid_pool)
new_ingredient = valid_pool.pop()
if liquids_used == 0:
category = "crafting-with-fluid"
elif liquids_used == 1:
category = "chemistry"
liquids_used += 1
new_ingredients[new_ingredient] = 1
new_recipe = Recipe(pack, category, new_ingredients, original.products, original.energy)
new_recipe = self.make_quick_recipe(recipes[pack], valid_pool)
self.custom_recipes[pack] = new_recipe

if self.world.silo[self.player].value == Silo.option_randomize_recipe \
Expand Down

0 comments on commit b0bf66b

Please sign in to comment.