Esempio n. 1
0
    def create_item(self, item_key, modifier_key=None):
        """
        Function to create and initialize a new Item.
        :param item_key: string that identifies the item
        :param modifier_key: string that identifies the item modifier
        :return: Item object
        """
        # Load the monster data from the config
        item_data = self.item_index[item_key]

        # Create the correct type of item
        base_item = BaseItem(item_data)
        item_class = eval(base_item.type)
        new_item = item_class and item_class(base_item) or None
        if new_item is None:
            raise GameError('Failed to create item with key: ' + item_key + '; unknown item type: ' + item_data['type'])

        if modifier_key is not None:
            modifier_data = self.modifier_index[modifier_key]
            mod = ItemModifier(modifier_data)
            if base_item.type == mod.type:
                new_item.modifiers.append(mod)
            else:
                raise GameError("Incompatible item modifier type. Can not apply " + modifier_key + " to " + item_key)

        # register the new item
        self.items.append(new_item)
        return new_item
Esempio n. 2
0
 def registerWithLevel(self, level):
     """
     This function registers this actor with the provided level.
     It has to be overridden in the Actor subclasses to ensure that the
     actor correctly registers with the level.
     """
     raise GameError('Missing implementation registerWithLevel()')
Esempio n. 3
0
    def try_use_item(self, item, target=None):
        """
        Player attempts to use an item.
        This function is meant to be called from the GUI.
        :param item: Item object to be used
        :param target: Target for the item to be used on (can be None)
        :return: None
        """
        if isinstance(item, Consumable):
            # try to use the consumable
            if not item.isConsumed:
                if target is None:
                    # apply to self
                    item.applyTo(self)
                else:
                    # apply to target
                    item.applyTo(target)
            # remove the item it is used up
            if item.isConsumed == True:
                self.removeItem(item)

        elif isinstance(item, Equipment):
            if item.isEquiped:
                #unequip the item
                self.unEquipItem(item)
            else:
                #equip the item
                self.equipItem(item)
        else:
            raise GameError("Missing implementation to use item")
Esempio n. 4
0
 def get_random_modifier(self, max_modifier_level):
     # Determine max modifier level at which modifiers are available
     modifier_level = max_modifier_level
     while modifier_level not in self.modifier_level_index.keys():
         modifier_level -= 1
         if modifier_level <= 0:
             raise GameError("No modifiers available below the give modifier level")
     # Determine possibilities
     possibilities = []
     possibilities.extend(self.modifier_level_index[modifier_level])
     if modifier_level + 1 in self.modifier_level_index.keys():
         possibilities.extend(self.modifier_level_index[modifier_level + 1])
     if modifier_level - 1 in self.modifier_level_index.keys():
         possibilities.extend(self.modifier_level_index[modifier_level - 1])
     if modifier_level - 2 in self.modifier_level_index.keys():
         possibilities.extend(self.modifier_level_index[modifier_level - 2])
     # Include negative modifiers
     for key in self.modifier_level_index.keys():
         if key <= 0:
             possibilities.extend(self.modifier_level_index[key])
     # Make a random choice
     selection = random.choice(possibilities)
     # Create the item
     modifier = ItemModifier(selection)
     return modifier
Esempio n. 5
0
 def get_random_item(self, max_item_level):
     # Determine max item level at which items are available
     item_level = max_item_level
     while item_level not in self.item_level_index.keys():
         item_level -= 1
         if item_level <= 0:
             raise GameError("No items available below the give item level")
     # Determine possibilities
     possibilities = []
     possibilities.extend(self.item_level_index[item_level])
     if item_level + 1 in self.item_level_index.keys():
         possibilities.extend(self.item_level_index[item_level + 1])
     if item_level - 1 in self.item_level_index.keys():
         possibilities.extend(self.item_level_index[item_level - 1])
     if item_level - 2 in self.item_level_index.keys():
         possibilities.extend(self.item_level_index[item_level - 2])
     # Make a random choice
     selection = random.choice(possibilities)
     # Create the item
     new_item = self.create_item(selection.key)
     # Apply modifiers
     max_modifier_level = max_item_level - item_level + 1
     if max_modifier_level > 0:
         modifier = self.get_random_modifier(max_modifier_level)
         if new_item.type == modifier.type:
             new_item.modifiers.append(modifier)
     return new_item
Esempio n. 6
0
    def create_monster(self, monster_key):
        """
        Function to create and initialize a new Monster.
        :param monster_key: string that identifies a monster in the config file.
        :return: Monster
        """
        # Load the monster data from the config
        base_monster = self.monster_index[monster_key]

        # do not create multiple unique monsters
        if base_monster.unique:
            unique_keys = []
            for unique_monster in self.unique_monsters:
                unique_keys.append(unique_monster.key)
            if monster_key in unique_keys:
                # This unique was already created, do nothing
                raise GameError('Unique monster' + monster_key + ' already exists.')

        # Create monster
        new_monster = Monster(base_monster)

        # register the monster
        if base_monster.unique:
            self.unique_monsters.append(new_monster)
            # Avoid randomly recreating the same unique monster in the future
            self.challenge_index[base_monster.challengeRating].remove(base_monster)
            if len(self.challenge_index[base_monster.challengeRating]) == 0:
                del self.challenge_index[base_monster.challengeRating]
        else:
            self.regular_monsters.append(new_monster)
        return new_monster
Esempio n. 7
0
 def take_turn(self):
     """
     Take one turn
     """
     raise GameError(
         "Class AI does not have implementation for takeTurn(), please use one of the subclasses"
     )
Esempio n. 8
0
 def applyTo(self, target):
     """
     Healing effect will be applied to target character.
     :target: Character object
     :return: None
     """
     if not isinstance(target, WarrensGame.Actors.Character):
         raise GameError("Can not apply healing effect to " + str(target))
     self.actors.append(target)
     target.state_healing_animation_id = id(self)
Esempio n. 9
0
 def get_random_monster(self, max_challenge_rating):
     # Determine possibilities
     while max_challenge_rating not in self.challenge_index.keys():
         max_challenge_rating -= 1
         if max_challenge_rating <= 0:
             raise GameError("No monsters available below the give challenge rating")
     # Make a random choice
     possibilities = self.challenge_index[max_challenge_rating]
     selection = random.choice(possibilities)
     # create the monster
     monster = self.create_monster(selection.key)
     return monster
Esempio n. 10
0
 def applyTo(self, target):
     """
     Damage area is circular around center.
     If this effect is targeted, the center will be the given target.
     If this effect is not targeted, the center will be the source of the effect.
     All actors on the tiles in the area of effect will be damaged.
     :target: Actor or Tile Object
     :return: None
     """
     # Determine center tile for the area of effect
     if isinstance(target, Tile):
         self._centerTile = target
     elif isinstance(target, WarrensGame.Actors.Actor):
         # Actor could be located on a tile
         if target.tile is not None:
             self._centerTile = target.tile
         # Actor could be located in an inventory
         elif target.owner is not None:
             self._centerTile = target.owner.tile
         else:
             # Illegal situation since we expect the target to be either on a tile or in an inventory
             raise GameError("Can't find a tile for Actor " + str(target))
     else:
         raise GameError("Can not apply damage effect to " + str(target))
     # find all tiles that are in the damage area
     x = self.centerTile.x
     y = self.centerTile.y
     radius = self.effectRadius
     full_circle = True
     exclude_blocked_tiles = True
     self._tiles = self.centerTile.map.getCircleTiles(
         x, y, radius, full_circle, exclude_blocked_tiles)
     # in case this is an untargeted effect
     if not self.targeted:
         # exclude the center of the nova
         self.tiles.remove(self.centerTile)
Esempio n. 11
0
 def applyTo(self, target):
     """
     Confuse effect will be applied to target monster.
     :target: Monster object
     :return: None
     """
     if not isinstance(target, WarrensGame.Actors.Monster):
         raise GameError("Can not apply confuse effect to " + str(target))
     confused_turns = self.effectDuration
     WarrensGame.AI.ConfusedMonsterAI(self, target, confused_turns)
     target.level.active_effects.append(self)
     self.actors.append(target)
     message(
         target.name + ' is confused for ' + str(confused_turns) +
         ' turns.', "GAME")
Esempio n. 12
0
 def removeItem(self, item):
     if self.locked:
         raise GameError(
             "Chest is locked, please unlock before removing items.")
     else:
         self.inventory.remove(item)