Example #1
0
    def setup(self):
        """Setup variables."""
        self.egg = Item(666, "egg")

        self.wooden_axe = Tool(18, "wooden_axe")
        self.wood = Item(17, "wood", required_tools=[None, self.wooden_axe])

        self.wooden_pickaxe = Tool(18, "wooden_pickaxe")
        self.stone = Item(1, "stone", required_tools=[self.wooden_pickaxe])

        self.forest = Zone(
            zone_id=0,
            name="forest",
            properties={"has_crafting": False, "has_furnace": False},
            items=[self.egg, self.wood, self.stone],
        )
Example #2
0
    def _build_zones(n_zones: int,
                     items_per_zones: List[List[Item]]) -> List[Zone]:
        """Build random zones filled with given items

        Args:
            n_zones: Number of zones.
            items_per_zones: Items to fill each zones with.

        Returns:
            List of random zones.

        """
        zones = []
        for zone_id in range(n_zones):
            zone_items = items_per_zones[zone_id]
            new_zone = Zone(zone_id, "zone", items=zone_items)
            zones.append(new_zone)
        return zones
Example #3
0
    def craft(self, inventory: Inventory, zone: Zone) -> bool:
        """Use the recipe using an inventory in a zone

        Args:
            inventory: Inventory to transform.
            zone: Zone in which the crafting is done.

        Returns:
            True if successful, False otherwise

        """
        if not self.can_craft(inventory, zone):
            return False

        inventory += self._operation

        if self.added_properties is not None:
            for property_name, value in self.added_properties.items():
                zone.properties[property_name] = value

        return True
Example #4
0
def forest():
    return Zone(zone_id=0, name="forest", items={})
Example #5
0
# Crafting a gym-environment to simultate inventory managment
# Copyright (C) 2021-2022 Mathïs FEDERICO <https://www.gnu.org/licenses/>

""" Minecraft Zones

Abstract zones to simulate simply a Minecraft environment.

"""

from crafting.world.zones import Zone
from crafting.examples.minecraft.items import *
from crafting.examples.minecraft.tools import *

# Zones

FOREST = Zone(0, "forest", [WOOD, DIRT, STONE])  #: FOREST
SWAMP = Zone(1, "swamp", [DIRT, REEDS])  #: SWAMP
MEADOW = Zone(2, "meadow", [DIRT, STONE, LEATHER, EGG])  #: MEADOW
UNDERGROUND = Zone(3, "underground", [DIRT, STONE, IRON_ORE, GOLD_ORE])  #: UNDERGROUND
BEDROCK = Zone(
    4, "bedrock", [DIRT, STONE, IRON_ORE, GOLD_ORE, DIAMOND_ORE, REDSTONE_ORE, OBSIDIAN]
)  #: BEDROCK

MC_ZONES = [
    FOREST,
    SWAMP,
    MEADOW,
    UNDERGROUND,
    BEDROCK,
]
Example #6
0
def underground(stone, dirt):
    return Zone(1, "underground", items=[stone, dirt])
Example #7
0
def forest(wood):
    return Zone(0, "forest", items=[wood])
Example #8
0
class TestZone:
    "Zone"

    @pytest.fixture(autouse=True)
    def setup(self):
        """Setup variables."""
        self.egg = Item(666, "egg")

        self.wooden_axe = Tool(18, "wooden_axe")
        self.wood = Item(17, "wood", required_tools=[None, self.wooden_axe])

        self.wooden_pickaxe = Tool(18, "wooden_pickaxe")
        self.stone = Item(1, "stone", required_tools=[self.wooden_pickaxe])

        self.forest = Zone(
            zone_id=0,
            name="forest",
            properties={"has_crafting": False, "has_furnace": False},
            items=[self.egg, self.wood, self.stone],
        )

    def test_zone_str(self):
        """should be converted to string correctly."""
        check.equal(str(self.forest), "Forest(0)")

    def test_zone_repr(self):
        """should be represented correctly."""
        expected_repr = (
            "Forest(0)"
            + "{'has_crafting': False, 'has_furnace': False}"
            + "[Egg(666), Wood(17), Stone(1)]"
        )
        check.equal(repr(self.forest), expected_repr)

    def test_zone_search_with_required(self):
        """should allow finding with proper tools, but not without."""
        findings_with_pick = self.forest.search_for(self.stone, self.wooden_pickaxe)
        check.equal(
            findings_with_pick[0].item_id,
            self.stone.item_id,
            msg=f"Unexpected findings with wooden_pickaxe {findings_with_pick}",
        )

        findings_with_none = self.forest.search_for(self.stone, None)
        check.equal(
            len(findings_with_none),
            0,
            msg=f"Unexpected findings with None {findings_with_none}",
        )

    def test_zone_search_with_none_required(self):
        """should allow finding without proper tool if None is requried."""

        # When required_tools is None
        findings_with_pick = self.forest.search_for(self.egg, None)
        check.equal(
            findings_with_pick[0].item_id,
            self.egg.item_id,
            msg=f"Unexpected findings with None {findings_with_pick}",
        )

        # When None is in required_tools
        findings_with_pick = self.forest.search_for(self.wood, None)
        check.equal(
            findings_with_pick[0].item_id,
            self.wood.item_id,
            msg=f"Unexpected findings with None {findings_with_pick}",
        )
Example #9
0
    added_properties={"has_crafting": True},
)

R_WOODEN_PICKAXE = Recipe(
    recipe_id=3,
    inputs=[ItemStack(WOOD_PLANK, 3),
            ItemStack(STICK, 2)],
    outputs=[ItemStack(WOODEN_PICKAXE)],
    needed_properties={"has_crafting": True},
    added_properties=None,
)

ITEMS = [DIRT, WOOD, STONE, WOOD_PLANK, STICK, AIR, WOODEN_PICKAXE]
RECIPES = [R_WOOD_PLANK, R_CRAFTING_TABLE, R_STICK, R_WOODEN_PICKAXE]

FOREST = Zone(0, "forest", [WOOD])
UNDERGROUND = Zone(1, "underground", [STONE, DIRT])


@pytest.fixture
def world():
    return World(ITEMS, RECIPES, [FOREST, UNDERGROUND])


@pytest.fixture
def inv():
    return Inventory(ITEMS)


class DummyPlayer(Player):
    def choose_tool(self, item):