Beispiel #1
0
class TestTool:
    """Tool"""

    @pytest.fixture(autouse=True)
    def setup(self):
        """Setup reused variables."""
        self.tool_id = 18
        self.tool_name = "tool_name"
        self.tool = Tool(self.tool_id, self.tool_name)

    def test_init(self):
        """should instanciate correctly."""
        check.equal(self.tool.item_id, self.tool_id)
        check.equal(self.tool.name, self.tool_name)

    def test_use_item(self, item):
        """should find item on use on item."""
        findings = self.tool.use(item)
        check.equal(findings[0].item_id, item.item_id)
        check.equal(findings[0].size, 1)

    def test_use_none(self):
        """should find nothing on use on None."""
        findings = self.tool.use(None)
        check.equal(findings, [])

    def test_str(self):
        """should be converted to str correctly."""
        check.equal(str(self.tool), f"{self.tool_name.capitalize()}({self.tool_id})")
Beispiel #2
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],
        )
Beispiel #3
0
    def build_world(
        self,
        n_items: int,
        n_tools: int,
        n_findables: int,
        n_zones: int,
        n_required_tools: List[float],
        n_inputs_per_craft: List[float],
    ) -> Tuple[World, Zone]:
        """Build a random world.

        Args:
            n_items: Total number of items to generate.
            n_tools: Number of random tools to generate.
            n_findables: Number of random findable items to generate.
            n_zones: Number of random zones to generate.
            n_required_tools: List of probabilities of having x requirements where x is the index.
            n_inputs_per_craft: List of probabilities of having x+1 inputs where x is the index.

        Returns:
            A randomly built World.

        """

        if n_items < n_tools + n_findables:
            raise ValueError("n_items must be >= n_tools + n_findables")

        tools = [Tool(i, "tool") for i in range(n_tools)]
        findables, items_per_zones, items_per_tool = self._build_findables(
            n_findables,
            n_required_tools,
            tools,
            n_zones,
            offset_id=n_items - n_findables,
        )
        zones = self._build_zones(n_zones, items_per_zones)

        craftables = [
            Item(n_tools + i, "item")
            for i in range(n_items - n_findables - n_tools)
        ]
        items = tools + craftables + findables
        recipes = self._build_recipes(items, findables, n_inputs_per_craft,
                                      items_per_tool)

        world = World(zones=zones, items=items, recipes=recipes)
        return world, zones[0]
Beispiel #4
0
    def search_for(self, item: Item, tool: Tool = None) -> List[ItemStack]:
        """Searches for the given item using a tool

        Args:
            item: The item to look for.
            tool: The tool to use.

        Return:
            The found item stacks.

        """
        if item in self.items:
            required_tools = item.required_tools

            # If no tool is usable, just gather item
            if required_tools is None:
                return [ItemStack(item)]
            # If a tool is needed, gather items relative to used tool
            if tool is not None and tool in required_tools:
                return tool.use(item)
            # If no tool is needed anyway, just gather item
            if None in required_tools:
                return [ItemStack(item)]
        return []
Beispiel #5
0
def stone_pickaxe():
    return Tool(274, "stone_pickaxe")
Beispiel #6
0
def wooden_pickaxe():
    return Tool(18, "wooden_pickaxe")
Beispiel #7
0
def air():
    return Tool(0, "air")
Beispiel #8
0
 def setup(self):
     """Setup reused variables."""
     self.tool_id = 18
     self.tool_name = "tool_name"
     self.tool = Tool(self.tool_id, self.tool_name)
Beispiel #9
0
# Copyright (C) 2021-2022 Mathïs FEDERICO <https://www.gnu.org/licenses/>

import pytest
import numpy as np

from crafting.env import CraftingEnv

from crafting.world.world import World
from crafting.world.items import Item, Tool, ItemStack
from crafting.world.recipes import Recipe
from crafting.world.zones import Zone

from crafting.player.inventory import Inventory
from crafting.player.player import Player

WOODEN_PICKAXE = Tool(18, "wooden_pickaxe")
AIR = Tool(0, "air")
DIRT = Item(3, "dirt", required_tools=[AIR])
WOOD = Item(17, "wood", required_tools=[AIR])
STICK = Item(280, "stick")
WOOD_PLANK = Item(5, "plank")
STONE = Item(1, "stone", required_tools=[WOODEN_PICKAXE])

R_WOOD_PLANK = Recipe(
    recipe_id=5,
    inputs=[ItemStack(WOOD, 1)],
    outputs=[ItemStack(WOOD_PLANK, 4)],
    needed_properties=None,
    added_properties=None,
)