2020-11-27 15:33:17 +00:00
|
|
|
# Copyright (C) 2020 by ÿnérant, eichhornchen, nicomarg, charlse
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-12-02 15:04:43 +00:00
|
|
|
from random import shuffle
|
2020-11-10 21:34:12 +00:00
|
|
|
|
2020-11-10 21:59:02 +00:00
|
|
|
from .player import Player
|
2020-11-06 14:33:26 +00:00
|
|
|
from ..interfaces import FightingEntity, Map
|
|
|
|
|
2020-10-16 15:58:00 +00:00
|
|
|
|
2020-10-23 14:51:48 +00:00
|
|
|
class Monster(FightingEntity):
|
2020-11-18 11:27:59 +00:00
|
|
|
"""
|
2020-11-18 13:54:21 +00:00
|
|
|
The class for all monsters in the dungeon.
|
2020-12-13 20:29:25 +00:00
|
|
|
All specific monster classes overwrite this class,
|
|
|
|
and the parameters are given in the __init__ function.
|
2020-11-18 13:54:21 +00:00
|
|
|
An example of the specification of a monster that has a strength of 4
|
|
|
|
and 20 max HP:
|
|
|
|
|
|
|
|
class MyMonster(Monster):
|
|
|
|
def __init__(self, strength: int = 4, maxhealth: int = 20,
|
|
|
|
*args, **kwargs) -> None:
|
|
|
|
super().__init__(name="my_monster", strength=strength,
|
|
|
|
maxhealth=maxhealth, *args, **kwargs)
|
|
|
|
|
2020-12-13 20:29:25 +00:00
|
|
|
With that way, attributes can be overwritten when the entity is created.
|
2020-11-18 11:27:59 +00:00
|
|
|
"""
|
2020-11-18 13:54:21 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2020-11-06 15:13:28 +00:00
|
|
|
def act(self, m: Map) -> None:
|
2020-11-10 21:34:12 +00:00
|
|
|
"""
|
|
|
|
By default, a monster will move randomly where it is possible
|
2020-12-13 20:29:25 +00:00
|
|
|
If the player is closeby, the monster runs to the player.
|
2020-11-10 21:34:12 +00:00
|
|
|
"""
|
2020-11-10 21:59:02 +00:00
|
|
|
target = None
|
|
|
|
for entity in m.entities:
|
|
|
|
if self.distance_squared(entity) <= 25 and \
|
|
|
|
isinstance(entity, Player):
|
|
|
|
target = entity
|
2020-11-10 21:34:12 +00:00
|
|
|
break
|
2020-10-23 14:51:48 +00:00
|
|
|
|
2020-12-13 20:29:25 +00:00
|
|
|
# Monsters move according to a Dijkstra algorithm
|
|
|
|
# that targets the player.
|
|
|
|
# If they can not move and are already close to the player,
|
|
|
|
# they hit.
|
2020-11-11 14:25:50 +00:00
|
|
|
if target and (self.y, self.x) in target.paths:
|
2020-12-13 20:29:25 +00:00
|
|
|
# Moves to target player by choosing the best available path
|
2020-12-10 21:21:09 +00:00
|
|
|
for next_y, next_x in target.paths[(self.y, self.x)]:
|
|
|
|
moved = self.check_move(next_y, next_x, True)
|
|
|
|
if moved:
|
|
|
|
break
|
|
|
|
if self.distance_squared(target) <= 1:
|
|
|
|
self.map.logs.add_message(self.hit(target))
|
|
|
|
break
|
2020-11-10 21:59:02 +00:00
|
|
|
else:
|
2020-12-13 20:29:25 +00:00
|
|
|
# Moves in a random direction
|
|
|
|
# If the direction is not available, tries another one
|
2020-12-02 15:04:43 +00:00
|
|
|
moves = [self.move_up, self.move_down,
|
|
|
|
self.move_left, self.move_right]
|
|
|
|
shuffle(moves)
|
|
|
|
for move in moves:
|
|
|
|
if move():
|
2020-11-10 21:59:02 +00:00
|
|
|
break
|
2020-12-18 16:46:38 +00:00
|
|
|
|
|
|
|
def move(self, y: int, x: int) -> None:
|
2020-12-18 16:29:59 +00:00
|
|
|
"""
|
|
|
|
Overwrites the move function to recalculate paths.
|
|
|
|
"""
|
|
|
|
super().move(y, x)
|
|
|
|
self.recalculate_paths()
|
2020-11-06 14:33:26 +00:00
|
|
|
|
2020-12-18 16:46:38 +00:00
|
|
|
|
2020-11-20 17:02:08 +00:00
|
|
|
class Tiger(Monster):
|
2020-11-18 11:27:59 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
A tiger monster.
|
2020-11-18 11:27:59 +00:00
|
|
|
"""
|
2020-12-05 13:25:22 +00:00
|
|
|
def __init__(self, name: str = "tiger", strength: int = 2,
|
|
|
|
maxhealth: int = 20, *args, **kwargs) -> None:
|
|
|
|
super().__init__(name=name, strength=strength,
|
2020-11-18 13:54:21 +00:00
|
|
|
maxhealth=maxhealth, *args, **kwargs)
|
2020-11-11 16:39:48 +00:00
|
|
|
|
|
|
|
|
2020-11-10 20:47:36 +00:00
|
|
|
class Hedgehog(Monster):
|
2020-11-18 11:27:59 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
A really mean hedgehog monster.
|
2020-11-18 11:27:59 +00:00
|
|
|
"""
|
2020-12-05 13:25:22 +00:00
|
|
|
def __init__(self, name: str = "hedgehog", strength: int = 3,
|
|
|
|
maxhealth: int = 10, *args, **kwargs) -> None:
|
|
|
|
super().__init__(name=name, strength=strength,
|
2020-11-18 13:54:21 +00:00
|
|
|
maxhealth=maxhealth, *args, **kwargs)
|
2020-11-11 16:39:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Rabbit(Monster):
|
2020-11-18 11:27:59 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
A rabbit monster.
|
2020-11-18 11:27:59 +00:00
|
|
|
"""
|
2020-12-05 13:25:22 +00:00
|
|
|
def __init__(self, name: str = "rabbit", strength: int = 1,
|
|
|
|
maxhealth: int = 15, *args, **kwargs) -> None:
|
|
|
|
super().__init__(name=name, strength=strength,
|
2020-11-18 13:54:21 +00:00
|
|
|
maxhealth=maxhealth, *args, **kwargs)
|
2020-11-11 16:39:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TeddyBear(Monster):
|
2020-11-18 11:27:59 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
A cute teddybear monster.
|
2020-11-18 11:27:59 +00:00
|
|
|
"""
|
2020-12-05 13:25:22 +00:00
|
|
|
def __init__(self, name: str = "teddy_bear", strength: int = 0,
|
|
|
|
maxhealth: int = 50, *args, **kwargs) -> None:
|
|
|
|
super().__init__(name=name, strength=strength,
|
2020-11-18 13:54:21 +00:00
|
|
|
maxhealth=maxhealth, *args, **kwargs)
|