Exemple #1
0
def test_ignore_irrelevant_tags():
    system = CountTagsScoreSystem()
    count_tags_score_components = {
        "counter": CountTagsScore(
            addTo="player",
            scoreType="constant",
            score=1,
            tags={
                "tagOne": 1,
                "tagTwo": 2
            }
        )
    }
    position_components = PositionDict({
        "counter": Position(x=0, y=0),
        "tag_holder": Position(x=0, y=0)
    })
    tags_components = {"tag_holder": [
        "tagOne", "tagTwo", "tagTwo", "tagThree"]}
    label_components = LabelDict({"p": "player"})
    scores = system.get_constant_tag_scores(
        count_tags_score_components, position_components, tags_components,
        label_components
    )
    assert scores == {"p": 1}
Exemple #2
0
def test_multiple_counters_add_score():
    system = CountTagsScoreSystem()
    count_tags_score_components = {
        "counter_one": CountTagsScore(
            addTo="player",
            scoreType="constant",
            score=1,
            tags={}
        ),
        "counter_two": CountTagsScore(
            addTo="player",
            scoreType="constant",
            score=1,
            tags={}
        ),
    }
    position_components = PositionDict({
        "counter_one": Position(x=0, y=0),
        "counter_two": Position(x=1, y=0)
    })
    tags_components = {}
    label_components = LabelDict({"p": "player"})
    scores = system.get_constant_tag_scores(
        count_tags_score_components, position_components, tags_components,
        label_components
    )
    assert scores == {"p": 2}
def test_move_direction(direction, x, y):
    system = MovementSystem()
    actions = {"a": Move(direction=direction)}
    position_components = PositionDict({"a": Position(x=0, y=0)})
    blocks_movement_components = {}
    tags = {}
    swappable_components = {}
    system.move_entities(actions, position_components,
                         blocks_movement_components, tags,
                         swappable_components)
    assert position_components["a"] == Position(x=x, y=y)
Exemple #4
0
def test_attack_invulnerable():
    actions = {"attacker": Attack(direction="right")}
    position_components = PositionDict({
        "attacker": Position(x=0, y=0),
        "target": Position(x=1, y=0)
    })
    vulnerable_components = {}
    system = AttackSystem()
    removed_entities = system.do_attacks(actions, position_components,
                                         vulnerable_components)
    assert len(removed_entities) == 0
def test_drop_item():
    system = DropSystem()
    inventory_components = {
        "a": Inventory(
            items=["item"]
        )
    }
    actions = {"a": Drop(index=0)}
    position_components = PositionDict({"a": Position(x=0, y=0)})
    system.drop_items(inventory_components, actions, position_components)
    assert position_components["item"] == Position(x=0, y=0)
    assert len(inventory_components["a"].items) == 0
Exemple #6
0
def test_attack_vulnerable(direction, target_x, target_y):
    actions = {"attacker": Attack(direction=direction)}
    position_components = PositionDict({
        "attacker":
        Position(x=0, y=0),
        "target":
        Position(x=target_x, y=target_y)
    })
    vulnerable_components = {"target": Vulnerable}
    system = AttackSystem()
    removed_entities = system.do_attacks(actions, position_components,
                                         vulnerable_components)
    assert "target" in removed_entities
def test_blocker_cannot_move_into_others_space():
    system = MovementSystem()
    actions = {"a": Move(direction="right")}
    position_components = PositionDict({
        "a": Position(x=0, y=0),
        "b": Position(x=1, y=0)
    })
    blocks_movement_components = {"a": BlocksMovement()}
    tags = {}
    swappable_components = {}
    system.move_entities(actions, position_components,
                         blocks_movement_components, tags,
                         swappable_components)
    assert position_components["a"] == Position(x=0, y=0)
def test_automatic_pickup():
    system = PickUpSystem()
    pickupper_components = {"pickupper": Pickupper(mode="auto")}
    inventory_components = {}
    actions = {}
    position_components = PositionDict({
        "pickupper": Position(x=0, y=0),
        "item": Position(x=0, y=0)
    })
    pickup_components = {"item": VanishPickup(kind="vanish")}
    score_components = {}
    removed_entities = system.pick_up_items(
        pickupper_components, actions, position_components,
        pickup_components, score_components, inventory_components)
    assert removed_entities == set(["item"])
def test_swap():
    system = MovementSystem()
    actions = {"a": Move(direction="right")}
    position_components = PositionDict({
        "a": Position(x=0, y=0),
        "b": Position(x=1, y=0)
    })
    blocks_movement_components = {}
    tags = {}
    swappable_components = {"b": Swappable()}
    system.move_entities(actions, position_components,
                         blocks_movement_components, tags,
                         swappable_components)
    assert position_components["a"] == Position(x=1, y=0)
    assert position_components["b"] == Position(x=0, y=0)
def test_score_pickup_only_vanish_if_no_score():
    system = PickUpSystem()
    pickupper_components = {"pickupper": Pickupper(mode="action")}
    inventory_components = {}
    actions = {"pickupper": PickUp()}
    position_components = PositionDict({
        "pickupper": Position(x=0, y=0),
        "item": Position(x=0, y=0)
    })
    pickup_components = {"item": ScorePickup(kind="addScore", score=1)}
    score_components = {}
    removed_entities = system.pick_up_items(
        pickupper_components, actions, position_components,
        pickup_components, score_components, inventory_components)
    assert removed_entities == set(["item"])
    assert score_components == {}
def fill_room(room, wall_entity, width, height, free_locations):
    for x in range(width):
        for y in range(height):
            if (x, y) not in free_locations:
                new_entity = wall_entity.copy(deep=True)
                new_entity.position = Position(x=x, y=y)
                room.add_entity(new_entity)
def test_blocks_movement_right_tags():
    system = MovementSystem()
    actions = {"a": Move(direction="right")}
    position_components = PositionDict({
        "a": Position(x=0, y=0),
        "b": Position(x=1, y=0)
    })
    blocks_movement_components = {
        "b": BlocksMovement(passableForTags=["pass"])
    }
    tags = {"a": ["pass"]}
    swappable_components = {}
    system.move_entities(actions, position_components,
                         blocks_movement_components, tags,
                         swappable_components)
    assert position_components["a"] == Position(x=1, y=0)
 def create_room(self):
     random_generator = Random(self.seed)
     free_locations = get_free_locations(self.width, self.height,
                                         self.bypass_neighbour_chance,
                                         random_generator)
     wall_entities = translate_definition_symbol(self.wall,
                                                 self.definitions)
     if self.room_seed is not None:
         room = Room(randomSeed=self.room_seed)
     else:
         room = Room()
     for entity in wall_entities:
         fill_room(room, entity, self.width, self.height, free_locations)
     symbols_to_place = []
     for symbol, placement_details in self.stuff.items():
         if placement_details.amount:
             symbols_to_place += [symbol] * placement_details.amount
     locations = random_generator.sample(free_locations,
                                         len(symbols_to_place))
     for location, symbol in zip(locations, symbols_to_place):
         entities = translate_definition_symbol(symbol, self.definitions)
         for entity in entities:
             new_entity = entity.copy(deep=True)
             new_entity.position = Position(x=location[0], y=location[1])
             room.add_entity(new_entity)
     return room
def test_pickup_item():
    system = PickUpSystem()
    pickupper_components = {"pickupper": Pickupper(mode="action")}
    inventory_components = {"pickupper": Inventory()}
    actions = {"pickupper": PickUp()}
    position_components = PositionDict({
        "pickupper": Position(x=0, y=0),
        "item": Position(x=0, y=0)
    })
    pickup_components = {"item": ItemPickup(kind="item")}
    score_components = {}
    removed_entities = system.pick_up_items(
        pickupper_components, actions, position_components,
        pickup_components, score_components, inventory_components)
    assert inventory_components["pickupper"] == Inventory(items=["item"])
    assert "item" not in position_components
    assert removed_entities == set()
def test_cannot_swap_if_other_entity_is_blocked():
    system = MovementSystem()
    actions = {"a": Move(direction="right")}
    position_components = PositionDict({
        "a": Position(x=0, y=0),
        "b": Position(x=1, y=0),
        "c": Position(x=0, y=0)
    })
    blocks_movement_components = {
        "c": BlocksMovement(passableForTags=["pass"])
    }
    tags = {"a": ["pass"]}
    swappable_components = {"b": Swappable()}
    system.move_entities(actions, position_components,
                         blocks_movement_components, tags,
                         swappable_components)
    assert position_components["a"] == Position(x=0, y=0)
    assert position_components["b"] == Position(x=1, y=0)
Exemple #16
0
def test_percept_include_entities():
    system = PerceptSystem()
    perception_components = {"perceptor": Perception()}
    position_components = PositionDict({
        "perceptor": Position(x=0, y=0),
        "otherEntity": Position(x=1, y=1)
    })
    looks_like_components = {"otherEntity": "coin"}
    inventory_components = {}
    percepts = system.get_percepts(perception_components, position_components,
                                   looks_like_components, inventory_components)
    assert percepts == {
        "perceptor": {
            "entities": [{
                "x": 1,
                "y": 1,
                "looks_like": "coin"
            }]
        }
    }
Exemple #17
0
    def move_entities(self, actions, position_components,
                      blocks_movement_components, tags, swappable_components):
        for mover_id, action in actions.items():
            if action.action_type != "move":
                continue

            dx = dy = 0
            if action.direction == "up":
                dy = 1
            elif action.direction == "down":
                dy = -1
            elif action.direction == "left":
                dx = -1
            elif action.direction == "right":
                dx = 1
            else:
                raise RuntimeError(f"Unknown direction {action.direction}")

            x = position_components[mover_id].x + dx
            y = position_components[mover_id].y + dy
            colliding_entities = position_components.get_entities_at(x, y)
            if all(
                    can_coexist(mover_id, e, blocks_movement_components, tags)
                    for e in colliding_entities):
                old_x = position_components[mover_id].x
                old_y = position_components[mover_id].y
                to_swap_with = [
                    e for e in position_components.get_entities_at(x, y)
                    if e in swappable_components
                ]
                in_old_location = [
                    e
                    for e in position_components.get_entities_at(old_x, old_y)
                    if e != mover_id
                ]
                if all(
                        can_coexist(e1, e2, blocks_movement_components, tags)
                        for e1 in to_swap_with for e2 in in_old_location):
                    position_components[mover_id] = Position(x=x, y=y)
                    for e in to_swap_with:
                        position_components[e] = Position(x=old_x, y=old_y)
Exemple #18
0
def test_percept_exclude_entities_beyond_max_distance():
    system = PerceptSystem()
    perception_components = {"perceptor": Perception(distance=3)}
    position_components = PositionDict({
        "perceptor": Position(x=0, y=0),
        "otherEntity": Position(x=3, y=0),
        "farEntityOne": Position(x=3, y=1),
        "farEntityTwo": Position(x=3, y=-1),
        "farEntityThree": Position(x=-3, y=1),
        "farEntityFour": Position(x=-3, y=-1),
    })
    looks_like_components = {
        "otherEntity": "coin",
        "farEntityOne": "wall",
        "farEntityTwo": "wall",
        "farEntityThree": "wall",
        "farEntityFour": "wall",
    }
    inventory_components = {}
    percepts = system.get_percepts(perception_components, position_components,
                                   looks_like_components, inventory_components)
    assert percepts == {
        "perceptor": {
            "entities": [{
                "x": 3,
                "y": 0,
                "looks_like": "coin"
            }]
        }
    }
def test_drop_nonexistent_index_does_nothing():
    system = DropSystem()
    inventory_components = {
        "a": Inventory(
            mode="action",
            items=["item"]
        )
    }
    actions = {"a": Drop(index=1)}
    position_components = PositionDict({"a": Position(x=0, y=0)})
    system.drop_items(inventory_components, actions, position_components)
    assert "item" not in position_components
    assert len(inventory_components["a"].items) == 1
Exemple #20
0
def test_get_percept_include_inventory():
    system = PerceptSystem()
    perception_components = {"perceptor": Perception()}
    position_components = PositionDict({"perceptor": Position(x=0, y=0)})
    looks_like_components = {"itemOne": "coin", "itemTwo": "evilCoin"}
    inventory_components = {
        "perceptor": Inventory(items=["itemOne", "itemTwo"])
    }
    percepts = system.get_percepts(perception_components, position_components,
                                   looks_like_components, inventory_components)
    assert percepts == {
        "perceptor": {
            "entities": [],
            "inventory": ["coin", "evilCoin"]
        }
    }
Exemple #21
0
def test_percept_include_position():
    system = PerceptSystem()
    perception_components = {"perceptor": Perception(includePosition=True)}
    position_components = PositionDict({"perceptor": Position(x=0, y=0)})
    looks_like_components = {}
    inventory_components = {}
    percepts = system.get_percepts(perception_components, position_components,
                                   looks_like_components, inventory_components)
    assert percepts == {
        "perceptor": {
            "entities": [],
            "position": {
                "x": 0,
                "y": 0
            }
        }
    }
Exemple #22
0
def test_no_tags_always_give_score():
    system = CountTagsScoreSystem()
    count_tags_score_components = {
        "counter": CountTagsScore(
            addTo="player",
            scoreType="constant",
            score=1,
            tags={}
        )
    }
    position_components = PositionDict({
        "counter": Position(x=0, y=0)
    })
    tags_components = {}
    label_components = LabelDict({"p": "player"})
    scores = system.get_constant_tag_scores(
        count_tags_score_components, position_components, tags_components,
        label_components
    )
    assert scores == {"p": 1}
Exemple #23
0
def test_additive_score_type_no_effect_if_no_score():
    system = CountTagsScoreSystem()
    count_tags_score_components = {
        "counter": CountTagsScore(
            addTo="player",
            scoreType="additive",
            score=1,
            tags={}
        )
    }
    position_components = PositionDict({
        "counter": Position(x=0, y=0)
    })
    tags_components = {}
    label_components = LabelDict({"p": "player"})
    score_components = {}
    system.add_tag_scores(
        count_tags_score_components, position_components, tags_components,
        label_components, score_components
    )
    assert score_components == {}
Exemple #24
0
 def create_room(self):
     if self.random_seed is not None:
         new_room = Room(randomSeed=self.random_seed)
     else:
         new_room = Room()
     if isinstance(self.room, str):
         layers = [self.room]
     else:
         layers = self.room
     height = max(layer.count("\n") + 1 for layer in layers)
     for layer in layers:
         lines = [line for line in layer.split("\n")]
         for y, line in enumerate(lines):
             for x, symbol in enumerate(line):
                 if symbol == " ":
                     pass
                 else:
                     for entity in translate_definition_symbol(
                             symbol, self.definitions):
                         new_entity = entity.copy(deep=True)
                         new_entity.position = Position(x=x,
                                                        y=height - y - 1)
                         new_room.add_entity(new_entity)
     return new_room