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}
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_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 __init__(self, entities=None, **kwargs): super().__init__(**kwargs) # Entities self.entity_ids = [] # Components self.position_components = PositionDict() self.ai_components = {} self.perception_components = {} self.score_components = {} self.blocks_movement_components = {} self.swappable_components = {} self.pickupper_components = {} self.inventory_components = {} self.pickup_components = {} self.looks_like_components = {} self.tags_components = {} self.label_components = LabelDict() self.vulnerable_components = {} self.count_tags_score_components = {} self.actions_components = {} # Systems self.percept_system = PerceptSystem() self.action_system = ActionSystem() self.tag_system = TagSystem() self.movement_system = MovementSystem() self.pick_up_system = PickUpSystem() self.drop_system = DropSystem() self.attack_system = AttackSystem() self.count_tags_score_system = CountTagsScoreSystem() if entities is not None: for identifier, entity in entities.items(): entity_obj = entity if isinstance(entity, Entity) else Entity( **entity) self.add_entity(entity_obj, entity_id=identifier)
def test_attack_empty_tile(): actions = {"attacker": Attack(direction="right")} position_components = PositionDict({"attacker": Position(x=0, y=0)}) vulnerable_components = {} system = AttackSystem() removed_entities = system.do_attacks(actions, position_components, vulnerable_components) assert len(removed_entities) == 0
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)
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
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_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
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_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"] } }
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 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 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 } } }
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)
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" }] } }
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}
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 == {}