Ejemplo n.º 1
0
def test_teleporting_to_an_undiscovered_level_doesnt_link():
    m = Map(level(1), 1, 1)

    first = flexmock(Level(1))
    m.current = first

    first.should_receive("add_stairs").never
    flexmock(Level).new_instances(flexmock(Level(5))).once

    m.travel_by_teleport(5, (5, 5))

    assert m.current.dlvl == 5
    assert len(m.levels) == 2
Ejemplo n.º 2
0
def test_teleporting_to_an_undiscovered_level_doesnt_link():
    m = Map(level(1), 1, 1)

    first = flexmock(Level(1))
    m.current = first

    first.should_receive("add_stairs").never
    flexmock(Level).new_instances(flexmock(Level(5))).once

    m.travel_by_teleport(5, (5, 5))

    assert m.current.dlvl == 5
    assert len(m.levels) == 2 
Ejemplo n.º 3
0
class Dungeon:
    """
    The dungeon keeps track of various dungeon states that are helpful to know.
    It remembers where shops are, remembers where it heard sounds and what they
    mean, and probably some other stuff that I'll think of in the future.
    """
    def __init__(self, events):
        self.graph = Map(Level(1), 0, 0)
        self.level = 1
        self.went_through_lvl_tel = False
        self.events = events

    def __getstate__(self):
        d = self.__dict__.copy()
        del d['events']
        return d

    def listen(self):
        self.events.listen("level-changed", self._level_changed)
        self.events.listen("branch-changed", self._branch_changed)
        self.events.listen("feature-found", self._feature_found)
        self.events.listen("shop-entered", self._shop_entered)
        self.events.listen("level-teleported", self._level_teleported)
        self.events.listen("trap-door-fell", self._level_teleported)
        self.events.listen("moved", self._moved)

    def _moved(self, _, cursor):
        self.graph.move(*cursor)

    def _shop_entered(self, _, shop_type):
        if "shop" not in self.current_level().features:
            self.current_level().features.add("shop")
        self.current_level().shops.add(shops.types[shop_type])

    def _branch_changed(self, _, branch):
        # I'm really reluctant to put logic in here, beyond just a basic event
        # handler. However, until Brain is refactored into some more meaningful
        # structure, there's a couple edge cases where a level can be detected
        # as "mines" even though it's clearly not the mines.
        #
        # Specifically: When in the upper levels of sokoban and traveling
        # downward. Mines obviously only exists off of "main" or "not sure", it
        # can never come out of "sokoban". Enforcing that here is the easiest
        # way to fix weird branching craziness.
        if branch == "mines" and \
           self.current_level().branch not in ["main", "not sure"]:
            pass
        else:
            self.graph.change_branch_to(branch)

    def _feature_found(self, _, feature):
        self.current_level().features.add(feature)

    def _level_teleported(self, _):
        self.went_through_lvl_tel = True

    def _level_changed(self, _, level, from_pos, to_pos):
        if self.level == level:
            # This seems like it's probably an error. The brain, or whoever is
            # doing the even dispatching should know not to dispatch a level
            # change event when, in fact, we clearly have not changed levels.
            return

        if abs(self.level - level) > 1 or self.went_through_lvl_tel:
            self.graph.travel_by_teleport(level, to_pos)
            self.went_through_lvl_tel = False
        else:
            self.graph.travel_by_stairs(level, to_pos)

        # Update our current position
        self.level = level

    def current_level(self):
        """ Return the level that the player is currently on """
        return self.graph.current
Ejemplo n.º 4
0
class Dungeon:
    """
    The dungeon keeps track of various dungeon states that are helpful to know.
    It remembers where shops are, remembers where it heard sounds and what they
    mean, and probably some other stuff that I'll think of in the future.
    """

    def __init__(self):
        self.graph = Map(Level(1), 0, 0)
        self.level = 1
        self.went_through_lvl_tel = False

    def listen(self):
        dispatcher.add_event_listener("level-change", 
                                      self._level_change_handler)
        dispatcher.add_event_listener("branch-change",
                                      self._branch_change_handler)
        dispatcher.add_event_listener("level-feature",
                                      self._level_feature_handler)
        dispatcher.add_event_listener("shop-type",
                                      self._shop_type_handler)
        dispatcher.add_event_listener("level-teleport",
                                      self._level_teleport_handler)
        dispatcher.add_event_listener("trap-door", 
                                      self._level_teleport_handler)
        dispatcher.add_event_listener("move", self._map_move_handler)

    def _map_move_handler(self, _, cursor):
        self.graph.move(*cursor)

    def _shop_type_handler(self, _, shop_type):
        if "shop" not in self.current_level().features:
            self.current_level().features.add("shop")
        self.current_level().shops.add(shops.types[shop_type]) 

    def _branch_change_handler(self, _, branch):
        # I'm really reluctant to put logic in here, beyond just a basic event
        # handler. However, until Brain is refactored into some more meaningful
        # structure, there's a couple edge cases where a level can be detected
        # as "mines" even though it's clearly not the mines.
        #
        # Specifically: When in the upper levels of sokoban and traveling 
        # downward. Mines obviously only exists off of "main" or "not sure", it
        # can never come out of "sokoban". Enforcing that here is the easiest
        # way to fix weird branching craziness.
        if branch == "mines" and \
           self.current_level().branch not in ["main", "not sure"]:
            pass
        else:
            self.graph.change_branch_to(branch)

    def _level_feature_handler(self, _, feature):
        self.current_level().features.add(feature)

    def _level_teleport_handler(self, _):
        self.went_through_lvl_tel = True 

    def _level_change_handler(self, _, level, from_pos, to_pos):
        if self.level == level:
            # This seems like it's probably an error. The brain, or whoever is
            # doing the even dispatching should know not to dispatch a level
            # change event when, in fact, we clearly have not changed levels.
            return

        if abs(self.level - level) > 1 or self.went_through_lvl_tel:
            self.graph.travel_by_teleport(level, to_pos)
            self.went_through_lvl_tel = False
        else:
            self.graph.travel_by_stairs(level, to_pos)

        # Update our current position
        self.level = level

    def current_level(self):
        """ Return the level that the player is currently on """
        return self.graph.current