2021-01-10 10:25:53 +00:00
|
|
|
# Copyright (C) 2020-2021 by ÿnérant, eichhornchen, nicomarg, charlse
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-12-18 16:29:59 +00:00
|
|
|
from random import choice, shuffle
|
2020-11-27 15:56:22 +00:00
|
|
|
|
2021-01-10 17:04:33 +00:00
|
|
|
from .items import Bomb, Item
|
2021-01-10 10:25:53 +00:00
|
|
|
from .monsters import Monster
|
|
|
|
from .player import Player
|
|
|
|
from ..interfaces import Entity, FightingEntity, FriendlyEntity, \
|
|
|
|
InventoryHolder, Map
|
|
|
|
from ..translations import gettext as _
|
|
|
|
|
2020-12-01 16:12:22 +00:00
|
|
|
|
2020-12-11 16:20:50 +00:00
|
|
|
class Merchant(InventoryHolder, FriendlyEntity):
|
2020-11-27 15:56:22 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
The class of merchants in the dungeon.
|
2020-11-27 15:56:22 +00:00
|
|
|
"""
|
2020-12-05 20:43:13 +00:00
|
|
|
def keys(self) -> list:
|
2020-12-03 23:27:25 +00:00
|
|
|
"""
|
2020-12-18 16:29:59 +00:00
|
|
|
Returns a friendly entitie's specific attributes.
|
2020-12-03 23:27:25 +00:00
|
|
|
"""
|
2020-12-09 14:32:37 +00:00
|
|
|
return super().keys() + ["inventory", "hazel"]
|
2020-11-27 15:56:22 +00:00
|
|
|
|
2020-12-07 20:13:55 +00:00
|
|
|
def __init__(self, name: str = "merchant", inventory: list = None,
|
2021-01-08 22:32:47 +00:00
|
|
|
hazel: int = 75, maxhealth: int = 8, *args, **kwargs):
|
2021-01-08 21:30:30 +00:00
|
|
|
super().__init__(name=name, maxhealth=maxhealth, *args, **kwargs)
|
2020-12-11 16:20:50 +00:00
|
|
|
self.inventory = self.translate_inventory(inventory or [])
|
2020-11-27 17:35:52 +00:00
|
|
|
self.hazel = hazel
|
2020-12-07 20:48:56 +00:00
|
|
|
if not self.inventory:
|
|
|
|
for i in range(5):
|
|
|
|
self.inventory.append(choice(Item.get_all_items())())
|
2020-11-27 15:56:22 +00:00
|
|
|
|
2020-12-07 20:22:06 +00:00
|
|
|
def talk_to(self, player: Player) -> str:
|
2020-11-27 15:56:22 +00:00
|
|
|
"""
|
|
|
|
This function is used to open the merchant's inventory in a menu,
|
2020-12-13 20:29:25 +00:00
|
|
|
and allows the player to buy/sell objects.
|
2020-11-27 15:56:22 +00:00
|
|
|
"""
|
2020-12-07 20:13:55 +00:00
|
|
|
return _("I don't sell any squirrel")
|
2020-12-07 20:22:06 +00:00
|
|
|
|
2020-12-11 15:49:17 +00:00
|
|
|
def change_hazel_balance(self, hz: int) -> None:
|
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Changes the number of hazel the merchant has by hz.
|
2020-12-11 15:49:17 +00:00
|
|
|
"""
|
|
|
|
self.hazel += hz
|
2020-12-07 20:48:56 +00:00
|
|
|
|
2021-01-08 22:32:47 +00:00
|
|
|
|
2021-01-08 22:15:48 +00:00
|
|
|
class Chest(InventoryHolder, FriendlyEntity):
|
|
|
|
"""
|
|
|
|
A class of chest inanimate entities which contain objects.
|
|
|
|
"""
|
2021-01-10 17:04:33 +00:00
|
|
|
annihilated: bool
|
|
|
|
|
2021-01-08 22:15:48 +00:00
|
|
|
def __init__(self, name: str = "chest", inventory: list = None,
|
|
|
|
hazel: int = 0, *args, **kwargs):
|
|
|
|
super().__init__(name=name, *args, **kwargs)
|
|
|
|
self.hazel = hazel
|
|
|
|
self.inventory = self.translate_inventory(inventory or [])
|
2021-01-10 17:04:33 +00:00
|
|
|
self.annihilated = False
|
2021-01-08 22:15:48 +00:00
|
|
|
if not self.inventory:
|
|
|
|
for i in range(3):
|
|
|
|
self.inventory.append(choice(Item.get_all_items())())
|
|
|
|
|
|
|
|
def talk_to(self, player: Player) -> str:
|
|
|
|
"""
|
|
|
|
This function is used to open the chest's inventory in a menu,
|
|
|
|
and allows the player to take objects.
|
|
|
|
"""
|
|
|
|
return _("You have opened the chest")
|
|
|
|
|
2021-01-08 22:32:47 +00:00
|
|
|
def take_damage(self, attacker: Entity, amount: int) -> str:
|
2021-01-08 22:15:48 +00:00
|
|
|
"""
|
|
|
|
A chest is not living, it can not take damage
|
|
|
|
"""
|
2021-01-10 17:04:33 +00:00
|
|
|
if isinstance(attacker, Bomb):
|
2021-01-10 15:31:16 +00:00
|
|
|
self.die()
|
2021-01-10 17:04:33 +00:00
|
|
|
self.annihilated = True
|
2021-01-10 15:31:16 +00:00
|
|
|
return _("The chest exploded")
|
2021-01-08 22:15:48 +00:00
|
|
|
return _("It's not really effective")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def dead(self) -> bool:
|
|
|
|
"""
|
|
|
|
Chest can not die
|
|
|
|
"""
|
2021-01-10 17:04:33 +00:00
|
|
|
return self.annihilated
|
2021-01-08 22:15:48 +00:00
|
|
|
|
2020-12-07 20:22:06 +00:00
|
|
|
|
|
|
|
class Sunflower(FriendlyEntity):
|
2020-11-27 17:35:52 +00:00
|
|
|
"""
|
2020-12-18 16:29:59 +00:00
|
|
|
A friendly sunflower.
|
2020-11-27 17:35:52 +00:00
|
|
|
"""
|
2021-01-10 15:31:16 +00:00
|
|
|
def __init__(self, maxhealth: int = 20,
|
2020-11-27 17:35:52 +00:00
|
|
|
*args, **kwargs) -> None:
|
2020-12-07 20:22:06 +00:00
|
|
|
super().__init__(name="sunflower", maxhealth=maxhealth, *args, **kwargs)
|
2020-12-12 16:39:12 +00:00
|
|
|
|
|
|
|
@property
|
2020-12-12 17:12:37 +00:00
|
|
|
def dialogue_option(self) -> list:
|
2020-12-13 20:29:25 +00:00
|
|
|
"""
|
|
|
|
Lists all that a sunflower can say to the player.
|
|
|
|
"""
|
2020-12-12 16:39:12 +00:00
|
|
|
return [_("Flower power!!"), _("The sun is warm today")]
|
2020-12-18 16:29:59 +00:00
|
|
|
|
2020-12-18 16:46:38 +00:00
|
|
|
|
2020-12-31 13:51:17 +00:00
|
|
|
class Familiar(FightingEntity):
|
2020-12-18 16:29:59 +00:00
|
|
|
"""
|
|
|
|
A friendly familiar that helps the player defeat monsters.
|
|
|
|
"""
|
2020-12-18 16:46:38 +00:00
|
|
|
def __init__(self, maxhealth: int = 25,
|
2020-12-18 16:29:59 +00:00
|
|
|
*args, **kwargs) -> None:
|
2020-12-18 16:46:38 +00:00
|
|
|
super().__init__(maxhealth=maxhealth, *args, **kwargs)
|
2020-12-18 16:29:59 +00:00
|
|
|
self.target = None
|
2020-12-18 16:46:38 +00:00
|
|
|
|
2020-12-31 14:00:20 +00:00
|
|
|
# @property
|
|
|
|
# def dialogue_option(self) -> list:
|
|
|
|
# """
|
|
|
|
# Debug function (to see if used in the real game)
|
|
|
|
# """
|
|
|
|
# return [_("My target is"+str(self.target))]
|
2020-12-21 13:23:58 +00:00
|
|
|
|
2020-12-18 16:46:38 +00:00
|
|
|
def act(self, p: Player, m: Map) -> None:
|
2020-12-18 16:29:59 +00:00
|
|
|
"""
|
|
|
|
By default, the familiar tries to stay at distance at most 2 of the
|
|
|
|
player and if a monster comes in range 3, it focuses on the monster
|
|
|
|
and attacks it.
|
|
|
|
"""
|
2020-12-18 16:46:38 +00:00
|
|
|
if self.target is None:
|
2020-12-31 14:00:20 +00:00
|
|
|
# If the previous target is dead(or if there was no previous target)
|
|
|
|
# the familiar tries to get closer to the player.
|
2020-12-21 13:23:58 +00:00
|
|
|
self.target = p
|
|
|
|
elif self.target.dead:
|
2020-12-18 16:29:59 +00:00
|
|
|
self.target = p
|
|
|
|
if self.target == p:
|
2020-12-31 14:00:20 +00:00
|
|
|
# Look for monsters around the player to kill TOFIX : if monster is
|
|
|
|
# out of range, continue targetting player.
|
2020-12-18 16:29:59 +00:00
|
|
|
for entity in m.entities:
|
2020-12-21 13:23:58 +00:00
|
|
|
if (p.y - entity.y) ** 2 + (p.x - entity.x) ** 2 <= 9 and\
|
2020-12-18 16:29:59 +00:00
|
|
|
isinstance(entity, Monster):
|
|
|
|
self.target = entity
|
2020-12-18 16:46:38 +00:00
|
|
|
entity.paths = dict() # Allows the paths to be calculated.
|
2020-12-18 16:29:59 +00:00
|
|
|
break
|
2020-12-18 16:46:38 +00:00
|
|
|
|
2020-12-18 16:29:59 +00:00
|
|
|
# Familiars move according to a Dijkstra algorithm
|
|
|
|
# that targets their target.
|
|
|
|
# If they can not move and are already close to their target,
|
|
|
|
# they hit, except if their target is the player.
|
|
|
|
if self.target and (self.y, self.x) in self.target.paths:
|
|
|
|
# Moves to target player by choosing the best available path
|
|
|
|
for next_y, next_x in self.target.paths[(self.y, self.x)]:
|
|
|
|
moved = self.check_move(next_y, next_x, True)
|
|
|
|
if moved:
|
|
|
|
break
|
|
|
|
if self.distance_squared(self.target) <= 1 and \
|
2020-12-18 16:46:38 +00:00
|
|
|
not isinstance(self.target, Player):
|
2020-12-18 16:29:59 +00:00
|
|
|
self.map.logs.add_message(self.hit(self.target))
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
# Moves in a random direction
|
|
|
|
# If the direction is not available, tries another one
|
|
|
|
moves = [self.move_up, self.move_down,
|
|
|
|
self.move_left, self.move_right]
|
|
|
|
shuffle(moves)
|
|
|
|
for move in moves:
|
|
|
|
if move():
|
|
|
|
break
|
|
|
|
|
2020-12-18 16:46:38 +00:00
|
|
|
|
|
|
|
class Trumpet(Familiar):
|
2020-12-18 16:29:59 +00:00
|
|
|
"""
|
|
|
|
A class of familiars.
|
|
|
|
"""
|
|
|
|
def __init__(self, name: str = "trumpet", strength: int = 3,
|
2021-01-10 15:31:16 +00:00
|
|
|
maxhealth: int = 30, *args, **kwargs) -> None:
|
2020-12-18 16:29:59 +00:00
|
|
|
super().__init__(name=name, strength=strength,
|
|
|
|
maxhealth=maxhealth, *args, **kwargs)
|