Skip to content

Commit

Permalink
added planet/surface info extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
redruin1 committed Dec 25, 2024
1 parent 2e4397d commit d33bb89
Show file tree
Hide file tree
Showing 17 changed files with 118 additions and 8 deletions.
4 changes: 2 additions & 2 deletions draftsman/_factorio_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# _factorio_version.py

__factorio_version__ = "2.0.27.0"
__factorio_version_info__ = (2, 0, 27, 0)
__factorio_version__ = "2.0.29.0"
__factorio_version_info__ = (2, 0, 29, 0)
Binary file modified draftsman/data/entities.pkl
Binary file not shown.
Binary file modified draftsman/data/fluids.pkl
Binary file not shown.
Binary file modified draftsman/data/instruments.pkl
Binary file not shown.
Binary file modified draftsman/data/items.pkl
Binary file not shown.
Binary file modified draftsman/data/mods.pkl
Binary file not shown.
Binary file modified draftsman/data/modules.pkl
Binary file not shown.
Binary file added draftsman/data/planets.pkl
Binary file not shown.
26 changes: 26 additions & 0 deletions draftsman/data/planets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# planets.py

import pickle

import importlib.resources as pkg_resources

from draftsman import data
from draftsman.classes.collision_set import CollisionSet
from draftsman.env import get_default_collision_mask
from draftsman.utils import PrimitiveAABB, AABB


try:
with pkg_resources.open_binary(data, "planets.pkl") as inp:
_data: dict = pickle.load(inp)

raw: dict[str, dict] = _data[0]

except FileNotFoundError:
raw = {}

def supports(object) -> bool:
"""
Checks to see if a
"""
pass
Binary file modified draftsman/data/recipes.pkl
Binary file not shown.
16 changes: 16 additions & 0 deletions draftsman/data/recipes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# recipes.py

import pickle
import math

import importlib.resources as pkg_resources

Expand Down Expand Up @@ -64,3 +65,18 @@ def get_recipe_ingredients(recipe_name: str, expensive: bool = False):
x[0] if isinstance(x, list) else x["name"]
for x in raw[recipe_name][cost_type]["ingredients"]
}

def is_usable_on(recipe: dict, surface: dict) -> bool:
if "surface_conditions" not in recipe:
return True

for condition in recipe["surface_conditions"]:
property_name = condition["property"]
if property_name in surface["surface_properties"]:
value = surface["surface_properties"][property_name]
min_val = condition.get("min", -math.inf)
max_val = condition.get("max", math.inf)
if not (min_val <= value <= max_val):
return False

return True
Binary file modified draftsman/data/signals.pkl
Binary file not shown.
Binary file modified draftsman/data/tiles.pkl
Binary file not shown.
16 changes: 15 additions & 1 deletion draftsman/environment/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,21 @@ def main():

elif args.operation == "factorio-version":
if args.desired_version is None:
print("Factorio {}".format(__factorio_version__))
# Grab and populate the repo, making sure its a git repo we expect
import git
repo = git.Repo(args.game_path)
repo.git.fetch()
assert (
repo.remotes.origin.url == "https://github.com/wube/factorio-data"
), "Targeted repo is not `wube/factorio-data`"

# Grab the currently checked out tag for this repo
# https://stackoverflow.com/a/32524783/8167625
current_tag = next(
(tag for tag in repo.tags if tag.commit == repo.head.commit), None
)

print("Factorio {}".format(current_tag))
else:
# This command will only work if we're pointing at a git repo of the
# correct type
Expand Down
22 changes: 18 additions & 4 deletions draftsman/environment/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,6 @@ def specify_factorio_version(
:param verbose: Whether or not to print status messages to stdout for
clarity/readability. When ``false``, this function behaves silently.
"""
if verbose:
print()

# Grab and populate the repo, making sure its a git repo we expect
repo = git.Repo(game_path)
repo.git.fetch()
Expand Down Expand Up @@ -1243,7 +1240,23 @@ def extract_modules(lua: lupa.LuaRuntime, draftsman_path: str, sort_tuple, verbo
# =============================================================================


def extract_recipes(lua: lupa.LuaRuntime, draftsman_path: str, sort_tuple, verbose: bool=False):
def extract_planets(lua: lupa.LuaRuntime, draftsman_path: str, verbose: bool=False) -> None:
data = lua.globals().data

planets = convert_table_to_dict(data.raw["planet"])

with open(os.path.join(draftsman_path, "data", "planets.pkl"), "wb") as out:
data = [planets]
pickle.dump(data, out, 4)

if verbose:
print("Extracted planets...")


# =============================================================================


def extract_recipes(lua: lupa.LuaRuntime, draftsman_path: str, sort_tuple, verbose: bool=False) -> None:
"""
Extracts the recipes to ``recipes.pkl`` in :py:mod:`draftsman.data`.
"""
Expand Down Expand Up @@ -1607,6 +1620,7 @@ def extract_data(lua: lupa.LuaRuntime, draftsman_path: str, verbose: bool=False)
extract_instruments(lua, draftsman_path, verbose)
extract_items(lua, draftsman_path, items, verbose)
extract_modules(lua, draftsman_path, items, verbose)
extract_planets(lua, draftsman_path, verbose)
extract_recipes(lua, draftsman_path, items, verbose)
extract_signals(lua, draftsman_path, items, verbose)
extract_tiles(lua, draftsman_path, verbose)
Expand Down
40 changes: 40 additions & 0 deletions examples/recipe_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# recipe_types.py
"""
Extracts different types of recipes into logical data structures and displays
their contents to stdout.
"""

from draftsman.data import planets, recipes
from draftsman.entity import AssemblingMachine

def main():
# All recipes where productivity modules are allowed
productivity_recipes = {}
# All recipes where quality modules are allowed
quality_recipes = {}
# Recipes allowed on specific planets
planet_recipes = {planet_name: {} for planet_name in planets.raw}

for recipe_name, recipe in recipes.raw.items():
if "allow_quality" in recipe:
productivity_recipes[recipe_name] = recipe
if "allow_quality" in recipe:
quality_recipes[recipe_name] = recipe
for planet_name, planet in planets.raw.items():
if recipes.is_usable_on(recipe, planet):
planet_recipes[planet_name][recipe_name] = recipe

print("\t", productivity_recipes.keys())
print("\t", quality_recipes.keys())
for planet_name in planet_recipes:
print("\t{}:\t{}".format(planet_name, planet_recipes[planet_name].keys()))

# Recipes available for crafting in machines separately
print("\t", recipes.for_machine["assembling-machine-1"])
# or, equivalently:
machine = AssemblingMachine("assembling-machine-1")
assert machine.allowed_recipes == recipes.for_machine[machine.name]


if __name__ == "__main__":
main()

0 comments on commit d33bb89

Please sign in to comment.