2021-01-10 09:46:17 +00:00
|
|
|
# Copyright (C) 2020-2021 by ÿnérant, eichhornchen, nicomarg, charlse
|
2020-11-27 15:33:17 +00:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2021-01-10 10:25:53 +00:00
|
|
|
from copy import deepcopy
|
|
|
|
from enum import auto, Enum
|
|
|
|
from functools import reduce
|
2021-01-07 15:36:54 +00:00
|
|
|
from math import ceil, sqrt
|
2020-12-18 14:31:23 +00:00
|
|
|
from queue import PriorityQueue
|
2021-01-10 10:25:53 +00:00
|
|
|
from random import choice, choices, randint
|
|
|
|
from typing import Any, Dict, List, Optional, Tuple
|
2020-11-06 16:43:30 +00:00
|
|
|
|
2020-11-27 19:42:19 +00:00
|
|
|
from .display.texturepack import TexturePack
|
|
|
|
from .translations import gettext as _
|
2020-11-06 16:43:30 +00:00
|
|
|
|
2020-10-09 16:17:41 +00:00
|
|
|
|
2020-11-19 11:03:05 +00:00
|
|
|
class Logs:
|
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
The logs object stores the messages to display. It encapsulates a list
|
2020-11-19 11:03:05 +00:00
|
|
|
of such messages, to allow multiple pointers to keep track of it even if
|
|
|
|
the list was to be reassigned.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self.messages = []
|
|
|
|
|
|
|
|
def add_message(self, msg: str) -> None:
|
|
|
|
self.messages.append(msg)
|
|
|
|
|
|
|
|
def add_messages(self, msg: List[str]) -> None:
|
|
|
|
self.messages += msg
|
|
|
|
|
|
|
|
def clear(self) -> None:
|
|
|
|
self.messages = []
|
|
|
|
|
|
|
|
|
2020-12-11 18:23:21 +00:00
|
|
|
class Slope():
|
|
|
|
X: int
|
|
|
|
Y: int
|
|
|
|
|
|
|
|
def __init__(self, y: int, x: int) -> None:
|
|
|
|
self.Y = y
|
|
|
|
self.X = x
|
|
|
|
|
2020-12-18 16:04:45 +00:00
|
|
|
def compare(self, other: "Slope") -> int:
|
|
|
|
y, x = other.Y, other.X
|
2020-12-11 18:23:21 +00:00
|
|
|
return self.Y * x - self.X * y
|
|
|
|
|
2020-12-18 16:04:45 +00:00
|
|
|
def __lt__(self, other: "Slope") -> bool:
|
2020-12-11 18:23:21 +00:00
|
|
|
return self.compare(other) < 0
|
|
|
|
|
2020-12-18 16:04:45 +00:00
|
|
|
def __eq__(self, other: "Slope") -> bool:
|
2020-12-11 18:23:21 +00:00
|
|
|
return self.compare(other) == 0
|
|
|
|
|
2020-12-18 16:04:45 +00:00
|
|
|
def __gt__(self, other: "Slope") -> bool:
|
|
|
|
return self.compare(other) > 0
|
|
|
|
|
|
|
|
def __le__(self, other: "Slope") -> bool:
|
|
|
|
return self.compare(other) <= 0
|
|
|
|
|
|
|
|
def __ge__(self, other: "Slope") -> bool:
|
|
|
|
return self.compare(other) >= 0
|
|
|
|
|
2020-12-11 18:23:21 +00:00
|
|
|
|
2020-10-09 16:17:41 +00:00
|
|
|
class Map:
|
2020-10-16 16:05:49 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
The Map object represents a with its width, height
|
2020-11-18 11:19:27 +00:00
|
|
|
and tiles, that have their custom properties.
|
2020-10-16 16:05:49 +00:00
|
|
|
"""
|
2021-01-06 14:17:02 +00:00
|
|
|
floor: int
|
2020-10-09 16:17:41 +00:00
|
|
|
width: int
|
|
|
|
height: int
|
2020-11-11 15:09:03 +00:00
|
|
|
start_y: int
|
|
|
|
start_x: int
|
2020-11-10 20:41:54 +00:00
|
|
|
tiles: List[List["Tile"]]
|
2020-12-11 18:23:21 +00:00
|
|
|
visibility: List[List[bool]]
|
2020-12-18 20:21:00 +00:00
|
|
|
seen_tiles: List[List[bool]]
|
2020-11-10 20:47:36 +00:00
|
|
|
entities: List["Entity"]
|
2020-11-19 11:03:05 +00:00
|
|
|
logs: Logs
|
2020-11-06 20:15:09 +00:00
|
|
|
# coordinates of the point that should be
|
|
|
|
# on the topleft corner of the screen
|
|
|
|
currentx: int
|
|
|
|
currenty: int
|
2020-10-09 16:17:41 +00:00
|
|
|
|
2021-01-08 13:23:57 +00:00
|
|
|
def __init__(self, width: int = 0, height: int = 0, tiles: list = None,
|
|
|
|
start_y: int = 0, start_x: int = 0):
|
2021-01-06 14:17:02 +00:00
|
|
|
self.floor = 0
|
2020-10-09 16:17:41 +00:00
|
|
|
self.width = width
|
|
|
|
self.height = height
|
2020-11-11 15:09:03 +00:00
|
|
|
self.start_y = start_y
|
|
|
|
self.start_x = start_x
|
2021-01-08 13:23:57 +00:00
|
|
|
self.tiles = tiles or []
|
|
|
|
self.visibility = [[False for _ in range(len(self.tiles[0]))]
|
|
|
|
for _ in range(len(self.tiles))]
|
2020-12-18 20:21:00 +00:00
|
|
|
self.seen_tiles = [[False for _ in range(len(tiles[0]))]
|
2021-01-08 13:23:57 +00:00
|
|
|
for _ in range(len(self.tiles))]
|
2020-11-06 16:59:19 +00:00
|
|
|
self.entities = []
|
2020-11-19 19:02:44 +00:00
|
|
|
self.logs = Logs()
|
2020-11-06 16:59:19 +00:00
|
|
|
|
|
|
|
def add_entity(self, entity: "Entity") -> None:
|
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Registers a new entity in the map.
|
2020-11-06 16:59:19 +00:00
|
|
|
"""
|
2020-12-18 16:46:38 +00:00
|
|
|
if entity.is_familiar():
|
|
|
|
self.entities.insert(1, entity)
|
|
|
|
else:
|
2020-12-18 16:29:59 +00:00
|
|
|
self.entities.append(entity)
|
2020-11-06 16:59:19 +00:00
|
|
|
entity.map = self
|
2020-10-09 16:17:41 +00:00
|
|
|
|
2020-11-10 21:44:53 +00:00
|
|
|
def remove_entity(self, entity: "Entity") -> None:
|
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Unregisters an entity from the map.
|
2020-11-10 21:44:53 +00:00
|
|
|
"""
|
2020-12-09 15:57:46 +00:00
|
|
|
if entity in self.entities:
|
|
|
|
self.entities.remove(entity)
|
2020-11-10 21:44:53 +00:00
|
|
|
|
2020-11-18 23:33:50 +00:00
|
|
|
def find_entities(self, entity_class: type) -> list:
|
|
|
|
return [entity for entity in self.entities
|
|
|
|
if isinstance(entity, entity_class)]
|
|
|
|
|
2020-11-10 20:41:54 +00:00
|
|
|
def is_free(self, y: int, x: int) -> bool:
|
|
|
|
"""
|
2020-12-03 23:27:25 +00:00
|
|
|
Indicates that the tile at the coordinates (y, x) is empty.
|
2020-11-10 20:41:54 +00:00
|
|
|
"""
|
|
|
|
return 0 <= y < self.height and 0 <= x < self.width and \
|
|
|
|
self.tiles[y][x].can_walk() and \
|
|
|
|
not any(entity.x == x and entity.y == y for entity in self.entities)
|
2020-12-07 20:22:06 +00:00
|
|
|
|
2020-12-03 23:27:25 +00:00
|
|
|
def entity_is_present(self, y: int, x: int) -> bool:
|
|
|
|
"""
|
2020-12-07 20:22:06 +00:00
|
|
|
Indicates that the tile at the coordinates (y, x) contains a killable
|
2020-12-13 20:29:25 +00:00
|
|
|
entity.
|
2020-12-03 23:27:25 +00:00
|
|
|
"""
|
|
|
|
return 0 <= y < self.height and 0 <= x < self.width and \
|
2020-12-07 20:22:06 +00:00
|
|
|
any(entity.x == x and entity.y == y and entity.is_friendly()
|
|
|
|
for entity in self.entities)
|
2020-11-10 20:41:54 +00:00
|
|
|
|
2020-10-09 16:17:41 +00:00
|
|
|
@staticmethod
|
2020-11-10 20:41:54 +00:00
|
|
|
def load(filename: str) -> "Map":
|
2020-10-16 16:05:49 +00:00
|
|
|
"""
|
2020-12-18 16:46:38 +00:00
|
|
|
Reads a file that contains the content of a map,
|
|
|
|
and builds a Map object.
|
2020-10-16 16:05:49 +00:00
|
|
|
"""
|
2020-10-09 16:17:41 +00:00
|
|
|
with open(filename, "r") as f:
|
|
|
|
file = f.read()
|
|
|
|
return Map.load_from_string(file)
|
|
|
|
|
|
|
|
@staticmethod
|
2020-11-10 20:41:54 +00:00
|
|
|
def load_from_string(content: str) -> "Map":
|
2020-10-16 16:05:49 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Loads a map represented by its characters and builds a Map object.
|
2020-10-16 16:05:49 +00:00
|
|
|
"""
|
2020-10-09 16:17:41 +00:00
|
|
|
lines = content.split("\n")
|
2020-11-11 15:09:03 +00:00
|
|
|
first_line = lines[0]
|
|
|
|
start_y, start_x = map(int, first_line.split(" "))
|
|
|
|
lines = [line for line in lines[1:] if line]
|
2020-10-09 16:17:41 +00:00
|
|
|
height = len(lines)
|
2020-10-09 16:24:13 +00:00
|
|
|
width = len(lines[0])
|
2020-11-06 19:18:27 +00:00
|
|
|
tiles = [[Tile.from_ascii_char(c)
|
2020-10-16 13:41:25 +00:00
|
|
|
for x, c in enumerate(line)] for y, line in enumerate(lines)]
|
2020-10-23 13:55:30 +00:00
|
|
|
|
2020-11-11 15:09:03 +00:00
|
|
|
return Map(width, height, tiles, start_y, start_x)
|
2020-10-16 13:41:25 +00:00
|
|
|
|
2020-11-16 00:01:18 +00:00
|
|
|
@staticmethod
|
2020-11-18 14:02:30 +00:00
|
|
|
def load_dungeon_from_string(content: str) -> List[List["Tile"]]:
|
2020-11-16 00:01:18 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Transforms a string into the list of corresponding tiles.
|
2020-11-16 00:01:18 +00:00
|
|
|
"""
|
|
|
|
lines = content.split("\n")
|
|
|
|
tiles = [[Tile.from_ascii_char(c)
|
|
|
|
for x, c in enumerate(line)] for y, line in enumerate(lines)]
|
|
|
|
return tiles
|
|
|
|
|
2020-11-06 16:43:30 +00:00
|
|
|
def draw_string(self, pack: TexturePack) -> str:
|
2020-10-16 16:05:49 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Draws the current map as a string object that can be rendered
|
2020-10-16 16:05:49 +00:00
|
|
|
in the window.
|
|
|
|
"""
|
2020-11-06 16:43:30 +00:00
|
|
|
return "\n".join("".join(tile.char(pack) for tile in line)
|
2020-10-16 14:41:38 +00:00
|
|
|
for line in self.tiles)
|
2020-10-16 12:00:38 +00:00
|
|
|
|
2020-11-10 20:41:54 +00:00
|
|
|
def spawn_random_entities(self, count: int) -> None:
|
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Puts randomly {count} entities on the map, only on empty ground tiles.
|
2020-11-10 20:41:54 +00:00
|
|
|
"""
|
2020-12-11 18:23:21 +00:00
|
|
|
for _ignored in range(count):
|
2020-11-10 20:41:54 +00:00
|
|
|
y, x = 0, 0
|
|
|
|
while True:
|
|
|
|
y, x = randint(0, self.height - 1), randint(0, self.width - 1)
|
|
|
|
tile = self.tiles[y][x]
|
|
|
|
if tile.can_walk():
|
|
|
|
break
|
2021-01-06 17:02:58 +00:00
|
|
|
entity = choices(Entity.get_all_entity_classes(),
|
|
|
|
weights=Entity.get_weights(), k=1)[0]()
|
2020-11-11 15:23:27 +00:00
|
|
|
entity.move(y, x)
|
|
|
|
self.add_entity(entity)
|
2020-11-10 20:41:54 +00:00
|
|
|
|
2021-01-08 16:26:56 +00:00
|
|
|
def is_visible_from(self, starty: int, startx: int, desty: int, destx: int,
|
|
|
|
max_range: int) -> bool:
|
|
|
|
oldvisibility = deepcopy(self.visibility)
|
2021-01-10 18:41:51 +00:00
|
|
|
oldseen = deepcopy(self.seen_tiles)
|
2021-01-08 16:26:56 +00:00
|
|
|
self.compute_visibility(starty, startx, max_range)
|
|
|
|
result = self.visibility[desty][destx]
|
|
|
|
self.visibility = oldvisibility
|
2021-01-10 18:41:51 +00:00
|
|
|
self.seen_tiles = oldseen
|
2021-01-08 16:26:56 +00:00
|
|
|
return result
|
|
|
|
|
2020-12-11 18:23:21 +00:00
|
|
|
def compute_visibility(self, y: int, x: int, max_range: int) -> None:
|
|
|
|
"""
|
|
|
|
Sets the visible tiles to be the ones visible by an entity at point
|
|
|
|
(y, x), using a twaked shadow casting algorithm
|
|
|
|
"""
|
|
|
|
|
|
|
|
for line in self.visibility:
|
|
|
|
for i in range(len(line)):
|
|
|
|
line[i] = False
|
2020-12-18 20:21:00 +00:00
|
|
|
self.set_visible(0, 0, 0, (y, x))
|
2020-12-11 18:23:21 +00:00
|
|
|
for octant in range(8):
|
|
|
|
self.compute_visibility_octant(octant, (y, x), max_range, 1,
|
|
|
|
Slope(1, 1), Slope(0, 1))
|
|
|
|
|
|
|
|
def crop_top_visibility(self, octant: int, origin: Tuple[int, int],
|
|
|
|
x: int, top: Slope) -> int:
|
|
|
|
if top.X == 1:
|
|
|
|
top_y = x
|
|
|
|
else:
|
2020-12-18 16:04:45 +00:00
|
|
|
top_y = ceil(((x * 2 - 1) * top.Y + top.X) / (top.X * 2))
|
2020-12-11 18:23:21 +00:00
|
|
|
if self.is_wall(top_y, x, octant, origin):
|
2021-01-07 15:31:39 +00:00
|
|
|
top_y += top >= Slope(top_y * 2 + 1, x * 2) and not \
|
|
|
|
self.is_wall(top_y + 1, x, octant, origin)
|
2020-12-11 18:23:21 +00:00
|
|
|
else:
|
|
|
|
ax = x * 2
|
2021-01-07 15:31:39 +00:00
|
|
|
ax += self.is_wall(top_y + 1, x + 1, octant, origin)
|
|
|
|
top_y += top > Slope(top_y * 2 + 1, ax)
|
2020-12-11 18:23:21 +00:00
|
|
|
return top_y
|
|
|
|
|
|
|
|
def crop_bottom_visibility(self, octant: int, origin: Tuple[int, int],
|
|
|
|
x: int, bottom: Slope) -> int:
|
|
|
|
if bottom.Y == 0:
|
|
|
|
bottom_y = 0
|
|
|
|
else:
|
2020-12-18 16:04:45 +00:00
|
|
|
bottom_y = ceil(((x * 2 - 1) * bottom.Y + bottom.X)
|
|
|
|
/ (bottom.X * 2))
|
2021-01-07 15:31:39 +00:00
|
|
|
bottom_y += bottom >= Slope(bottom_y * 2 + 1, x * 2) and \
|
|
|
|
self.is_wall(bottom_y, x, octant, origin) and \
|
|
|
|
not self.is_wall(bottom_y + 1, x, octant, origin)
|
2020-12-11 18:23:21 +00:00
|
|
|
return bottom_y
|
|
|
|
|
|
|
|
def compute_visibility_octant(self, octant: int, origin: Tuple[int, int],
|
|
|
|
max_range: int, distance: int, top: Slope,
|
|
|
|
bottom: Slope) -> None:
|
2020-12-18 16:04:45 +00:00
|
|
|
for x in range(distance, max_range + 1):
|
2020-12-11 18:23:21 +00:00
|
|
|
top_y = self.crop_top_visibility(octant, origin, x, top)
|
|
|
|
bottom_y = self.crop_bottom_visibility(octant, origin, x, bottom)
|
|
|
|
was_opaque = -1
|
|
|
|
for y in range(top_y, bottom_y - 1, -1):
|
2020-12-18 16:04:45 +00:00
|
|
|
if x + y > max_range:
|
2020-12-11 18:23:21 +00:00
|
|
|
continue
|
|
|
|
is_opaque = self.is_wall(y, x, octant, origin)
|
|
|
|
is_visible = is_opaque\
|
2021-01-08 16:26:56 +00:00
|
|
|
or ((y != top_y or top >= Slope(y, x))
|
2020-12-18 16:04:45 +00:00
|
|
|
and (y != bottom_y
|
2021-01-08 16:26:56 +00:00
|
|
|
or bottom <= Slope(y, x)))
|
2020-12-18 20:21:00 +00:00
|
|
|
# is_visible = is_opaque\
|
|
|
|
# or ((y != top_y or top >= Slope(y, x))
|
|
|
|
# and (y != bottom_y or bottom <= Slope(y, x)))
|
2020-12-11 18:23:21 +00:00
|
|
|
if is_visible:
|
|
|
|
self.set_visible(y, x, octant, origin)
|
|
|
|
if x == max_range:
|
|
|
|
continue
|
|
|
|
if is_opaque and was_opaque == 0:
|
|
|
|
nx, ny = x * 2, y * 2 + 1
|
2021-01-07 15:31:39 +00:00
|
|
|
nx -= self.is_wall(y + 1, x, octant, origin)
|
2020-12-18 16:04:45 +00:00
|
|
|
if top > Slope(ny, nx):
|
2020-12-11 18:23:21 +00:00
|
|
|
if y == bottom_y:
|
|
|
|
bottom = Slope(ny, nx)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
self.compute_visibility_octant(
|
|
|
|
octant, origin, max_range, x + 1, top,
|
|
|
|
Slope(ny, nx))
|
2021-01-07 15:31:39 +00:00
|
|
|
elif y == bottom_y: # pragma: no cover
|
|
|
|
return
|
2020-12-11 18:23:21 +00:00
|
|
|
elif not is_opaque and was_opaque == 1:
|
|
|
|
nx, ny = x * 2, y * 2 + 1
|
2021-01-07 15:31:39 +00:00
|
|
|
nx += self.is_wall(y + 1, x + 1, octant, origin)
|
|
|
|
if bottom >= Slope(ny, nx): # pragma: no cover
|
2020-12-11 18:23:21 +00:00
|
|
|
return
|
2020-12-18 16:04:45 +00:00
|
|
|
top = Slope(ny, nx)
|
2020-12-11 18:23:21 +00:00
|
|
|
was_opaque = is_opaque
|
|
|
|
if was_opaque != 0:
|
|
|
|
break
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def translate_coord(y: int, x: int, octant: int,
|
|
|
|
origin: Tuple[int, int]) -> Tuple[int, int]:
|
|
|
|
ny, nx = origin
|
|
|
|
if octant == 0:
|
2020-12-18 16:04:45 +00:00
|
|
|
return ny - y, nx + x
|
2020-12-11 18:23:21 +00:00
|
|
|
elif octant == 1:
|
2020-12-18 16:04:45 +00:00
|
|
|
return ny - x, nx + y
|
2020-12-11 18:23:21 +00:00
|
|
|
elif octant == 2:
|
2020-12-18 16:04:45 +00:00
|
|
|
return ny - x, nx - y
|
2020-12-11 18:23:21 +00:00
|
|
|
elif octant == 3:
|
2020-12-18 16:04:45 +00:00
|
|
|
return ny - y, nx - x
|
2020-12-11 18:23:21 +00:00
|
|
|
elif octant == 4:
|
2020-12-18 16:04:45 +00:00
|
|
|
return ny + y, nx - x
|
2020-12-11 18:23:21 +00:00
|
|
|
elif octant == 5:
|
2020-12-18 16:04:45 +00:00
|
|
|
return ny + x, nx - y
|
2020-12-11 18:23:21 +00:00
|
|
|
elif octant == 6:
|
2020-12-18 16:04:45 +00:00
|
|
|
return ny + x, nx + y
|
2020-12-11 18:23:21 +00:00
|
|
|
elif octant == 7:
|
2020-12-18 16:04:45 +00:00
|
|
|
return ny + y, nx + x
|
2020-12-11 18:23:21 +00:00
|
|
|
|
|
|
|
def is_wall(self, y: int, x: int, octant: int,
|
|
|
|
origin: Tuple[int, int]) -> bool:
|
|
|
|
y, x = self.translate_coord(y, x, octant, origin)
|
2020-12-18 16:04:45 +00:00
|
|
|
return 0 <= y < len(self.tiles) and 0 <= x < len(self.tiles[0]) and \
|
|
|
|
self.tiles[y][x].is_wall()
|
2020-12-11 18:23:21 +00:00
|
|
|
|
|
|
|
def set_visible(self, y: int, x: int, octant: int,
|
|
|
|
origin: Tuple[int, int]) -> None:
|
|
|
|
y, x = self.translate_coord(y, x, octant, origin)
|
2020-12-18 16:04:45 +00:00
|
|
|
if 0 <= y < len(self.tiles) and 0 <= x < len(self.tiles[0]):
|
|
|
|
self.visibility[y][x] = True
|
2020-12-18 20:21:00 +00:00
|
|
|
self.seen_tiles[y][x] = True
|
2020-12-11 18:23:21 +00:00
|
|
|
|
2020-12-18 16:29:59 +00:00
|
|
|
def tick(self, p: Any) -> None:
|
2020-11-10 21:34:12 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Triggers all entity events.
|
2020-11-10 21:34:12 +00:00
|
|
|
"""
|
|
|
|
for entity in self.entities:
|
2020-12-18 16:29:59 +00:00
|
|
|
if entity.is_familiar():
|
|
|
|
entity.act(p, self)
|
2020-12-18 16:46:38 +00:00
|
|
|
else:
|
2020-12-18 16:29:59 +00:00
|
|
|
entity.act(self)
|
2020-11-10 21:34:12 +00:00
|
|
|
|
2020-11-16 00:01:18 +00:00
|
|
|
def save_state(self) -> dict:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Saves the map's attributes to a dictionary.
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-11-16 00:01:18 +00:00
|
|
|
d = dict()
|
|
|
|
d["width"] = self.width
|
|
|
|
d["height"] = self.height
|
|
|
|
d["start_y"] = self.start_y
|
|
|
|
d["start_x"] = self.start_x
|
|
|
|
d["currentx"] = self.currentx
|
|
|
|
d["currenty"] = self.currenty
|
2020-11-18 21:42:46 +00:00
|
|
|
d["entities"] = []
|
2020-11-16 00:01:18 +00:00
|
|
|
for enti in self.entities:
|
2020-11-18 23:10:37 +00:00
|
|
|
d["entities"].append(enti.save_state())
|
2020-11-16 00:01:18 +00:00
|
|
|
d["map"] = self.draw_string(TexturePack.ASCII_PACK)
|
2021-01-08 13:23:57 +00:00
|
|
|
d["seen_tiles"] = self.seen_tiles
|
2020-11-16 00:01:18 +00:00
|
|
|
return d
|
|
|
|
|
2021-01-08 13:23:57 +00:00
|
|
|
def load_state(self, d: dict) -> "Map":
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Loads the map's attributes from a dictionary.
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-11-16 00:01:18 +00:00
|
|
|
self.width = d["width"]
|
|
|
|
self.height = d["height"]
|
2020-11-18 13:54:21 +00:00
|
|
|
self.start_y = d["start_y"]
|
2020-11-16 00:01:18 +00:00
|
|
|
self.start_x = d["start_x"]
|
|
|
|
self.currentx = d["currentx"]
|
|
|
|
self.currenty = d["currenty"]
|
2020-11-18 14:02:30 +00:00
|
|
|
self.tiles = self.load_dungeon_from_string(d["map"])
|
2021-01-08 13:23:57 +00:00
|
|
|
self.seen_tiles = d["seen_tiles"]
|
|
|
|
self.visibility = [[False for _ in range(len(self.tiles[0]))]
|
|
|
|
for _ in range(len(self.tiles))]
|
2020-11-18 21:42:46 +00:00
|
|
|
self.entities = []
|
2020-11-18 23:10:37 +00:00
|
|
|
dictclasses = Entity.get_all_entity_classes_in_a_dict()
|
|
|
|
for entisave in d["entities"]:
|
|
|
|
self.add_entity(dictclasses[entisave["type"]](**entisave))
|
2020-11-18 13:56:59 +00:00
|
|
|
|
2021-01-08 13:23:57 +00:00
|
|
|
return self
|
|
|
|
|
2020-10-16 13:41:25 +00:00
|
|
|
|
|
|
|
class Tile(Enum):
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
The internal representation of the tiles of the map.
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-11-06 16:43:30 +00:00
|
|
|
EMPTY = auto()
|
|
|
|
WALL = auto()
|
|
|
|
FLOOR = auto()
|
2020-12-25 23:52:47 +00:00
|
|
|
LADDER = auto()
|
2020-11-06 16:43:30 +00:00
|
|
|
|
2020-11-16 00:01:18 +00:00
|
|
|
@staticmethod
|
|
|
|
def from_ascii_char(ch: str) -> "Tile":
|
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Maps an ascii character to its equivalent in the texture pack.
|
2020-11-16 00:01:18 +00:00
|
|
|
"""
|
2020-11-06 19:18:27 +00:00
|
|
|
for tile in Tile:
|
|
|
|
if tile.char(TexturePack.ASCII_PACK) == ch:
|
|
|
|
return tile
|
|
|
|
raise ValueError(ch)
|
|
|
|
|
2020-11-06 16:43:30 +00:00
|
|
|
def char(self, pack: TexturePack) -> str:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-11-18 13:56:59 +00:00
|
|
|
Translates a Tile to the corresponding character according
|
2020-12-13 20:29:25 +00:00
|
|
|
to the texture pack.
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-12-26 13:02:35 +00:00
|
|
|
val = getattr(pack, self.name)
|
2021-01-06 16:21:17 +00:00
|
|
|
return val[0] if isinstance(val, tuple) else val
|
2020-12-26 13:02:35 +00:00
|
|
|
|
2021-01-07 15:49:40 +00:00
|
|
|
def visible_color(self, pack: TexturePack) -> Tuple[int, int]:
|
|
|
|
"""
|
|
|
|
Retrieve the tuple (fg_color, bg_color) of the current Tile
|
|
|
|
if it is visible.
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2021-01-07 15:49:40 +00:00
|
|
|
val = getattr(pack, self.name)
|
|
|
|
return (val[2], val[4]) if isinstance(val, tuple) else \
|
|
|
|
(pack.tile_fg_visible_color, pack.tile_bg_color)
|
|
|
|
|
|
|
|
def hidden_color(self, pack: TexturePack) -> Tuple[int, int]:
|
2020-12-26 13:02:35 +00:00
|
|
|
"""
|
|
|
|
Retrieve the tuple (fg_color, bg_color) of the current Tile.
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-12-26 13:02:35 +00:00
|
|
|
val = getattr(pack, self.name)
|
2021-01-07 15:49:40 +00:00
|
|
|
return (val[1], val[3]) if isinstance(val, tuple) else \
|
2021-01-06 16:21:17 +00:00
|
|
|
(pack.tile_fg_color, pack.tile_bg_color)
|
2020-10-16 13:41:25 +00:00
|
|
|
|
2020-10-16 15:47:52 +00:00
|
|
|
def is_wall(self) -> bool:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
Is this Tile a wall?
|
|
|
|
"""
|
2020-10-16 15:47:52 +00:00
|
|
|
return self == Tile.WALL
|
|
|
|
|
2020-12-25 23:52:47 +00:00
|
|
|
def is_ladder(self) -> bool:
|
|
|
|
"""
|
|
|
|
Is this Tile a ladder?
|
|
|
|
"""
|
|
|
|
return self == Tile.LADDER
|
|
|
|
|
2020-10-16 15:47:52 +00:00
|
|
|
def can_walk(self) -> bool:
|
2020-10-16 16:05:49 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Checks if an entity (player or not) can move in this tile.
|
2020-10-16 16:05:49 +00:00
|
|
|
"""
|
2020-11-10 20:41:54 +00:00
|
|
|
return not self.is_wall() and self != Tile.EMPTY
|
2020-10-16 15:47:52 +00:00
|
|
|
|
2020-10-09 16:17:41 +00:00
|
|
|
|
2020-10-11 13:24:51 +00:00
|
|
|
class Entity:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
An Entity object represents any entity present on the map.
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-10-16 13:41:25 +00:00
|
|
|
y: int
|
2020-11-06 15:13:28 +00:00
|
|
|
x: int
|
2020-11-06 20:15:09 +00:00
|
|
|
name: str
|
2020-11-18 13:54:21 +00:00
|
|
|
map: Map
|
2020-12-18 14:31:23 +00:00
|
|
|
paths: Dict[Tuple[int, int], Tuple[int, int]]
|
2020-10-09 16:17:41 +00:00
|
|
|
|
2020-11-18 23:10:37 +00:00
|
|
|
# noinspection PyShadowingBuiltins
|
|
|
|
def __init__(self, y: int = 0, x: int = 0, name: Optional[str] = None,
|
|
|
|
map: Optional[Map] = None, *ignored, **ignored2):
|
|
|
|
self.y = y
|
|
|
|
self.x = x
|
|
|
|
self.name = name
|
|
|
|
self.map = map
|
2020-12-18 14:31:23 +00:00
|
|
|
self.paths = None
|
2020-11-06 15:13:28 +00:00
|
|
|
|
2020-11-06 16:59:19 +00:00
|
|
|
def check_move(self, y: int, x: int, move_if_possible: bool = False)\
|
|
|
|
-> bool:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Checks if moving to (y,x) is authorized.
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-11-10 20:41:54 +00:00
|
|
|
free = self.map.is_free(y, x)
|
|
|
|
if free and move_if_possible:
|
2020-11-06 16:59:19 +00:00
|
|
|
self.move(y, x)
|
2020-11-10 20:41:54 +00:00
|
|
|
return free
|
2020-11-06 16:59:19 +00:00
|
|
|
|
2020-11-10 21:44:53 +00:00
|
|
|
def move(self, y: int, x: int) -> bool:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Moves an entity to (y,x) coordinates.
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-10-11 13:24:51 +00:00
|
|
|
self.y = y
|
2020-11-06 15:13:28 +00:00
|
|
|
self.x = x
|
2020-11-10 21:44:53 +00:00
|
|
|
return True
|
2020-11-06 14:33:26 +00:00
|
|
|
|
2020-11-10 21:10:28 +00:00
|
|
|
def move_up(self, force: bool = False) -> bool:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Moves the entity up one tile, if possible.
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-11-10 21:10:28 +00:00
|
|
|
return self.move(self.y - 1, self.x) if force else \
|
|
|
|
self.check_move(self.y - 1, self.x, True)
|
|
|
|
|
|
|
|
def move_down(self, force: bool = False) -> bool:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Moves the entity down one tile, if possible.
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-11-10 21:10:28 +00:00
|
|
|
return self.move(self.y + 1, self.x) if force else \
|
|
|
|
self.check_move(self.y + 1, self.x, True)
|
|
|
|
|
|
|
|
def move_left(self, force: bool = False) -> bool:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Moves the entity left one tile, if possible.
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-11-10 21:10:28 +00:00
|
|
|
return self.move(self.y, self.x - 1) if force else \
|
|
|
|
self.check_move(self.y, self.x - 1, True)
|
|
|
|
|
|
|
|
def move_right(self, force: bool = False) -> bool:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Moves the entity right one tile, if possible.
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-11-10 21:10:28 +00:00
|
|
|
return self.move(self.y, self.x + 1) if force else \
|
|
|
|
self.check_move(self.y, self.x + 1, True)
|
|
|
|
|
2020-12-18 16:29:59 +00:00
|
|
|
def recalculate_paths(self, max_distance: int = 12) -> None:
|
2020-12-18 14:31:23 +00:00
|
|
|
"""
|
|
|
|
Uses Dijkstra algorithm to calculate best paths for other entities to
|
|
|
|
go to this entity. If self.paths is None, does nothing.
|
|
|
|
"""
|
2020-12-18 16:46:38 +00:00
|
|
|
if self.paths is None:
|
2020-12-18 14:31:23 +00:00
|
|
|
return
|
|
|
|
distances = []
|
|
|
|
predecessors = []
|
|
|
|
# four Dijkstras, one for each adjacent tile
|
|
|
|
for dir_y, dir_x in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
|
|
|
|
queue = PriorityQueue()
|
|
|
|
new_y, new_x = self.y + dir_y, self.x + dir_x
|
|
|
|
if not 0 <= new_y < self.map.height or \
|
|
|
|
not 0 <= new_x < self.map.width or \
|
|
|
|
not self.map.tiles[new_y][new_x].can_walk():
|
|
|
|
continue
|
|
|
|
queue.put(((1, 0), (new_y, new_x)))
|
|
|
|
visited = [(self.y, self.x)]
|
|
|
|
distances.append({(self.y, self.x): (0, 0), (new_y, new_x): (1, 0)})
|
|
|
|
predecessors.append({(new_y, new_x): (self.y, self.x)})
|
|
|
|
while not queue.empty():
|
|
|
|
dist, (y, x) = queue.get()
|
|
|
|
if dist[0] >= max_distance or (y, x) in visited:
|
|
|
|
continue
|
|
|
|
visited.append((y, x))
|
|
|
|
for diff_y, diff_x in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
|
|
|
|
new_y, new_x = y + diff_y, x + diff_x
|
|
|
|
if not 0 <= new_y < self.map.height or \
|
|
|
|
not 0 <= new_x < self.map.width or \
|
|
|
|
not self.map.tiles[new_y][new_x].can_walk():
|
|
|
|
continue
|
|
|
|
new_distance = (dist[0] + 1,
|
|
|
|
dist[1] + (not self.map.is_free(y, x)))
|
|
|
|
if not (new_y, new_x) in distances[-1] or \
|
|
|
|
distances[-1][(new_y, new_x)] > new_distance:
|
|
|
|
predecessors[-1][(new_y, new_x)] = (y, x)
|
|
|
|
distances[-1][(new_y, new_x)] = new_distance
|
|
|
|
queue.put((new_distance, (new_y, new_x)))
|
|
|
|
# For each tile that is reached by at least one Dijkstra, sort the
|
|
|
|
# different paths by distance to the player. For the technical bits :
|
|
|
|
# The reduce function is a fold starting on the first element of the
|
|
|
|
# iterable, and we associate the points to their distance, sort
|
|
|
|
# along the distance, then only keep the points.
|
|
|
|
self.paths = {}
|
|
|
|
for y, x in reduce(set.union,
|
|
|
|
[set(p.keys()) for p in predecessors], set()):
|
|
|
|
self.paths[(y, x)] = [p for d, p in sorted(
|
|
|
|
[(distances[i][(y, x)], predecessors[i][(y, x)])
|
|
|
|
for i in range(len(distances)) if (y, x) in predecessors[i]])]
|
2020-12-18 16:46:38 +00:00
|
|
|
|
2020-11-06 14:33:26 +00:00
|
|
|
def act(self, m: Map) -> None:
|
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Defines the action the entity will do at each tick.
|
2020-11-06 14:33:26 +00:00
|
|
|
By default, does nothing.
|
|
|
|
"""
|
2020-10-23 16:02:57 +00:00
|
|
|
pass
|
2020-10-16 15:58:00 +00:00
|
|
|
|
2020-11-10 21:59:02 +00:00
|
|
|
def distance_squared(self, other: "Entity") -> int:
|
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Gives the square of the distance to another entity.
|
|
|
|
Useful to check distances since taking the square root takes time.
|
2020-11-10 21:59:02 +00:00
|
|
|
"""
|
|
|
|
return (self.y - other.y) ** 2 + (self.x - other.x) ** 2
|
|
|
|
|
|
|
|
def distance(self, other: "Entity") -> float:
|
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Gives the cartesian distance to another entity.
|
2020-11-10 21:59:02 +00:00
|
|
|
"""
|
|
|
|
return sqrt(self.distance_squared(other))
|
|
|
|
|
2020-11-11 15:47:19 +00:00
|
|
|
def is_fighting_entity(self) -> bool:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
Is this entity a fighting entity?
|
|
|
|
"""
|
2020-11-11 15:47:19 +00:00
|
|
|
return isinstance(self, FightingEntity)
|
|
|
|
|
|
|
|
def is_item(self) -> bool:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
Is this entity an item?
|
|
|
|
"""
|
2020-11-19 01:18:08 +00:00
|
|
|
from squirrelbattle.entities.items import Item
|
2020-11-11 15:47:19 +00:00
|
|
|
return isinstance(self, Item)
|
|
|
|
|
2020-11-27 17:35:52 +00:00
|
|
|
def is_friendly(self) -> bool:
|
|
|
|
"""
|
|
|
|
Is this entity a friendly entity?
|
|
|
|
"""
|
|
|
|
return isinstance(self, FriendlyEntity)
|
|
|
|
|
2020-12-18 16:29:59 +00:00
|
|
|
def is_familiar(self) -> bool:
|
|
|
|
"""
|
|
|
|
Is this entity a familiar?
|
|
|
|
"""
|
|
|
|
from squirrelbattle.entities.friendly import Familiar
|
|
|
|
return isinstance(self, Familiar)
|
|
|
|
|
2020-12-05 20:43:13 +00:00
|
|
|
def is_merchant(self) -> bool:
|
|
|
|
"""
|
|
|
|
Is this entity a merchant?
|
|
|
|
"""
|
2020-12-07 19:54:53 +00:00
|
|
|
from squirrelbattle.entities.friendly import Merchant
|
2020-12-05 20:43:13 +00:00
|
|
|
return isinstance(self, Merchant)
|
|
|
|
|
2021-01-08 22:15:48 +00:00
|
|
|
def is_chest(self) -> bool:
|
|
|
|
"""
|
|
|
|
Is this entity a chest?
|
|
|
|
"""
|
|
|
|
from squirrelbattle.entities.friendly import Chest
|
|
|
|
return isinstance(self, Chest)
|
|
|
|
|
2020-11-27 21:33:58 +00:00
|
|
|
@property
|
|
|
|
def translated_name(self) -> str:
|
2020-12-13 20:29:25 +00:00
|
|
|
"""
|
|
|
|
Translates the name of entities.
|
|
|
|
"""
|
2020-11-27 21:33:58 +00:00
|
|
|
return _(self.name.replace("_", " "))
|
|
|
|
|
2020-11-11 15:23:27 +00:00
|
|
|
@staticmethod
|
2020-12-07 19:54:53 +00:00
|
|
|
def get_all_entity_classes() -> list:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Returns all entities subclasses.
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-12-06 10:43:48 +00:00
|
|
|
from squirrelbattle.entities.items import BodySnatchPotion, Bomb, Heart
|
2020-11-20 17:02:08 +00:00
|
|
|
from squirrelbattle.entities.monsters import Tiger, Hedgehog, \
|
2021-01-05 18:40:11 +00:00
|
|
|
Rabbit, TeddyBear, GiantSeaEagle
|
2020-12-18 16:29:59 +00:00
|
|
|
from squirrelbattle.entities.friendly import Merchant, Sunflower, \
|
2021-01-08 22:15:48 +00:00
|
|
|
Trumpet, Chest
|
2021-01-10 15:31:46 +00:00
|
|
|
return [BodySnatchPotion, Bomb, Chest, GiantSeaEagle, Heart,
|
2021-01-10 17:04:33 +00:00
|
|
|
Hedgehog, Merchant, Rabbit, Sunflower, TeddyBear, Tiger,
|
2021-01-10 15:31:46 +00:00
|
|
|
Trumpet]
|
2021-01-05 18:40:11 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_weights() -> list:
|
|
|
|
"""
|
|
|
|
Returns a weigth list associated to the above function, to
|
|
|
|
be used to spawn random entities with a certain probability.
|
|
|
|
"""
|
2021-01-10 15:31:46 +00:00
|
|
|
return [30, 80, 50, 1, 100, 100, 60, 70, 70, 20, 40, 40]
|
2020-11-11 15:23:27 +00:00
|
|
|
|
2020-11-18 21:42:46 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_all_entity_classes_in_a_dict() -> dict:
|
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Returns all entities subclasses in a dictionary.
|
2020-11-18 21:42:46 +00:00
|
|
|
"""
|
2020-11-19 01:18:08 +00:00
|
|
|
from squirrelbattle.entities.player import Player
|
2020-11-20 17:02:08 +00:00
|
|
|
from squirrelbattle.entities.monsters import Tiger, Hedgehog, Rabbit, \
|
2021-01-05 18:40:11 +00:00
|
|
|
TeddyBear, GiantSeaEagle
|
2020-12-18 17:13:39 +00:00
|
|
|
from squirrelbattle.entities.friendly import Merchant, Sunflower, \
|
2021-01-08 22:15:48 +00:00
|
|
|
Trumpet, Chest
|
2020-12-07 20:48:56 +00:00
|
|
|
from squirrelbattle.entities.items import BodySnatchPotion, Bomb, \
|
2021-01-08 10:54:39 +00:00
|
|
|
Heart, Sword, Shield, Chestplate, Helmet, RingCritical, RingXP, \
|
2021-01-08 22:41:21 +00:00
|
|
|
ScrollofDamage, ScrollofWeakening, Ruler, Bow, FireBallStaff, \
|
|
|
|
Monocle
|
2020-11-18 23:10:37 +00:00
|
|
|
return {
|
2021-01-08 22:41:21 +00:00
|
|
|
"BodySnatchPotion": BodySnatchPotion,
|
2020-11-18 23:10:37 +00:00
|
|
|
"Bomb": Bomb,
|
2021-01-08 22:41:21 +00:00
|
|
|
"Bow": Bow,
|
|
|
|
"Chest": Chest,
|
2021-01-08 14:48:12 +00:00
|
|
|
"Chestplate": Chestplate,
|
|
|
|
"Eagle": GiantSeaEagle,
|
2021-01-08 22:41:21 +00:00
|
|
|
"FireBallStaff": FireBallStaff,
|
2020-11-18 23:10:37 +00:00
|
|
|
"Heart": Heart,
|
|
|
|
"Hedgehog": Hedgehog,
|
2021-01-06 10:13:17 +00:00
|
|
|
"Helmet": Helmet,
|
2020-11-27 17:35:52 +00:00
|
|
|
"Merchant": Merchant,
|
2021-01-08 14:48:12 +00:00
|
|
|
"Monocle": Monocle,
|
2021-01-08 22:41:21 +00:00
|
|
|
"Player": Player,
|
|
|
|
"Rabbit": Rabbit,
|
2021-01-06 10:44:52 +00:00
|
|
|
"RingCritical": RingCritical,
|
|
|
|
"RingXP": RingXP,
|
2021-01-08 17:06:26 +00:00
|
|
|
"Ruler": Ruler,
|
2021-01-08 10:54:39 +00:00
|
|
|
"ScrollofDamage": ScrollofDamage,
|
2021-01-08 15:14:40 +00:00
|
|
|
"ScrollofWeakening": ScrollofWeakening,
|
2021-01-08 22:41:21 +00:00
|
|
|
"Shield": Shield,
|
2020-11-27 17:35:52 +00:00
|
|
|
"Sunflower": Sunflower,
|
2020-12-07 20:48:56 +00:00
|
|
|
"Sword": Sword,
|
2020-12-18 17:13:39 +00:00
|
|
|
"Trumpet": Trumpet,
|
2021-01-08 14:48:12 +00:00
|
|
|
"TeddyBear": TeddyBear,
|
|
|
|
"Tiger": Tiger,
|
2020-11-18 23:10:37 +00:00
|
|
|
}
|
2020-11-18 21:42:46 +00:00
|
|
|
|
2020-11-16 00:01:18 +00:00
|
|
|
def save_state(self) -> dict:
|
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Saves the coordinates of the entity.
|
2020-11-16 00:01:18 +00:00
|
|
|
"""
|
|
|
|
d = dict()
|
|
|
|
d["x"] = self.x
|
|
|
|
d["y"] = self.y
|
2020-11-18 23:10:37 +00:00
|
|
|
d["type"] = self.__class__.__name__
|
2020-11-16 00:01:18 +00:00
|
|
|
return d
|
|
|
|
|
2020-11-06 14:33:26 +00:00
|
|
|
|
2020-10-16 15:58:00 +00:00
|
|
|
class FightingEntity(Entity):
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
A FightingEntity is an entity that can fight, and thus has a health,
|
2020-12-13 20:29:25 +00:00
|
|
|
level and stats.
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-10-16 15:58:00 +00:00
|
|
|
maxhealth: int
|
|
|
|
health: int
|
|
|
|
strength: int
|
2020-11-06 17:08:10 +00:00
|
|
|
intelligence: int
|
|
|
|
charisma: int
|
|
|
|
dexterity: int
|
|
|
|
constitution: int
|
|
|
|
level: int
|
2021-01-05 18:07:15 +00:00
|
|
|
critical: int
|
2021-01-10 16:10:00 +00:00
|
|
|
confused: int # Seulement 0 ou 1
|
2020-10-16 15:58:00 +00:00
|
|
|
|
2020-11-18 23:10:37 +00:00
|
|
|
def __init__(self, maxhealth: int = 0, health: Optional[int] = None,
|
|
|
|
strength: int = 0, intelligence: int = 0, charisma: int = 0,
|
|
|
|
dexterity: int = 0, constitution: int = 0, level: int = 0,
|
2021-01-05 18:07:15 +00:00
|
|
|
critical: int = 0, *args, **kwargs) -> None:
|
2020-11-18 23:10:37 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.maxhealth = maxhealth
|
|
|
|
self.health = maxhealth if health is None else health
|
|
|
|
self.strength = strength
|
|
|
|
self.intelligence = intelligence
|
|
|
|
self.charisma = charisma
|
|
|
|
self.dexterity = dexterity
|
|
|
|
self.constitution = constitution
|
|
|
|
self.level = level
|
2021-01-05 18:07:15 +00:00
|
|
|
self.critical = critical
|
2021-01-08 21:25:00 +00:00
|
|
|
self.effects = [] # effects = temporary buff or weakening of the stats.
|
2021-01-10 16:10:00 +00:00
|
|
|
self.confused = 0
|
2020-11-18 13:29:54 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def dead(self) -> bool:
|
2020-12-13 20:29:25 +00:00
|
|
|
"""
|
|
|
|
Is this entity dead ?
|
|
|
|
"""
|
2020-11-18 13:29:54 +00:00
|
|
|
return self.health <= 0
|
2020-10-16 15:58:00 +00:00
|
|
|
|
2021-01-08 15:14:40 +00:00
|
|
|
def act(self, m: Map) -> None:
|
|
|
|
"""
|
|
|
|
Refreshes all current effects.
|
|
|
|
"""
|
|
|
|
for i in range(len(self.effects)):
|
|
|
|
self.effects[i][2] -= 1
|
|
|
|
|
2021-01-08 21:25:00 +00:00
|
|
|
copy = self.effects[:]
|
|
|
|
for i in range(len(copy)):
|
|
|
|
if copy[i][2] <= 0:
|
|
|
|
setattr(self, copy[i][0],
|
|
|
|
getattr(self, copy[i][0]) - copy[i][1])
|
|
|
|
self.effects.remove(copy[i])
|
|
|
|
|
2020-11-19 11:03:05 +00:00
|
|
|
def hit(self, opponent: "FightingEntity") -> str:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
The entity deals damage to the opponent
|
|
|
|
based on their respective stats.
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2021-01-10 16:10:00 +00:00
|
|
|
if self.confused:
|
|
|
|
return _("{name} is confused, it can not hit {opponent}.")\
|
2021-01-10 17:04:33 +00:00
|
|
|
.format(name=_(self.translated_name.capitalize()),
|
|
|
|
opponent=_(opponent.translated_name))
|
2021-01-06 16:39:13 +00:00
|
|
|
diceroll = randint(1, 100)
|
2021-01-08 15:14:40 +00:00
|
|
|
damage = max(0, self.strength)
|
2021-01-05 18:07:15 +00:00
|
|
|
string = " "
|
2021-01-06 16:39:13 +00:00
|
|
|
if diceroll <= self.critical: # It is a critical hit
|
2021-01-05 18:07:15 +00:00
|
|
|
damage *= 4
|
2021-01-08 01:00:22 +00:00
|
|
|
string = " " + _("It's a critical hit!") + " "
|
2020-11-27 19:42:19 +00:00
|
|
|
return _("{name} hits {opponent}.")\
|
2020-11-27 21:33:58 +00:00
|
|
|
.format(name=_(self.translated_name.capitalize()),
|
2021-01-05 18:07:15 +00:00
|
|
|
opponent=_(opponent.translated_name)) + string + \
|
|
|
|
opponent.take_damage(self, damage)
|
2020-11-06 14:33:26 +00:00
|
|
|
|
2020-11-19 11:03:05 +00:00
|
|
|
def take_damage(self, attacker: "Entity", amount: int) -> str:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
The entity takes damage from the attacker
|
|
|
|
based on their respective stats.
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2021-01-10 20:41:45 +00:00
|
|
|
damage = 0
|
2021-01-10 20:44:45 +00:00
|
|
|
if amount != 0:
|
2021-01-10 20:15:42 +00:00
|
|
|
damage = max(1, amount - self.constitution)
|
2021-01-05 18:18:25 +00:00
|
|
|
self.health -= damage
|
2020-10-16 15:58:00 +00:00
|
|
|
if self.health <= 0:
|
|
|
|
self.die()
|
2021-01-05 18:18:25 +00:00
|
|
|
return _("{name} takes {damage} damage.")\
|
|
|
|
.format(name=self.translated_name.capitalize(), damage=str(damage))\
|
2020-11-27 21:33:58 +00:00
|
|
|
+ (" " + _("{name} dies.")
|
|
|
|
.format(name=self.translated_name.capitalize())
|
2020-11-27 19:42:19 +00:00
|
|
|
if self.health <= 0 else "")
|
2020-11-06 14:33:26 +00:00
|
|
|
|
2020-10-16 15:58:00 +00:00
|
|
|
def die(self) -> None:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
If a fighting entity has no more health, it dies and is removed.
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-11-10 21:44:53 +00:00
|
|
|
self.map.remove_entity(self)
|
2020-11-16 00:01:18 +00:00
|
|
|
|
|
|
|
def keys(self) -> list:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Returns a fighting entity's specific attributes.
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-12-06 10:43:48 +00:00
|
|
|
return ["name", "maxhealth", "health", "level", "strength",
|
2020-11-18 13:56:59 +00:00
|
|
|
"intelligence", "charisma", "dexterity", "constitution"]
|
2020-11-16 00:01:18 +00:00
|
|
|
|
|
|
|
def save_state(self) -> dict:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Saves the state of the entity into a dictionary.
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-11-16 00:01:18 +00:00
|
|
|
d = super().save_state()
|
|
|
|
for name in self.keys():
|
2020-11-18 23:10:37 +00:00
|
|
|
d[name] = getattr(self, name)
|
2020-11-16 00:01:18 +00:00
|
|
|
return d
|
2020-11-20 16:42:56 +00:00
|
|
|
|
2020-12-07 20:22:06 +00:00
|
|
|
|
2020-12-03 23:27:25 +00:00
|
|
|
class FriendlyEntity(FightingEntity):
|
2020-11-20 16:42:56 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Friendly entities are living entities which do not attack the player.
|
2020-11-20 16:42:56 +00:00
|
|
|
"""
|
2020-12-07 20:22:06 +00:00
|
|
|
dialogue_option: list
|
2020-11-20 16:42:56 +00:00
|
|
|
|
2020-12-07 20:22:06 +00:00
|
|
|
def talk_to(self, player: Any) -> str:
|
2020-12-12 16:39:12 +00:00
|
|
|
return _("{entity} said: {message}").format(
|
|
|
|
entity=self.translated_name.capitalize(),
|
|
|
|
message=choice(self.dialogue_option))
|
2020-12-01 16:07:40 +00:00
|
|
|
|
2020-12-07 20:22:06 +00:00
|
|
|
def keys(self) -> list:
|
2020-12-03 23:27:25 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Returns a friendly entity's specific attributes.
|
2020-12-03 23:27:25 +00:00
|
|
|
"""
|
2020-12-09 14:32:37 +00:00
|
|
|
return ["maxhealth", "health"]
|
2020-12-11 16:20:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
class InventoryHolder(Entity):
|
|
|
|
hazel: int # Currency of the game
|
|
|
|
inventory: list
|
|
|
|
|
|
|
|
def translate_inventory(self, inventory: list) -> list:
|
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Translates the JSON save of the inventory into a list of the items in
|
2020-12-11 16:20:50 +00:00
|
|
|
the inventory.
|
|
|
|
"""
|
|
|
|
for i in range(len(inventory)):
|
|
|
|
if isinstance(inventory[i], dict):
|
2020-12-18 16:30:03 +00:00
|
|
|
inventory[i] = self.dict_to_item(inventory[i])
|
2020-12-11 16:20:50 +00:00
|
|
|
return inventory
|
|
|
|
|
2020-12-18 16:30:03 +00:00
|
|
|
def dict_to_item(self, item_dict: dict) -> Entity:
|
2020-12-11 16:20:50 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Translates a dictionnary that contains the state of an item
|
2020-12-11 16:20:50 +00:00
|
|
|
into an item object.
|
|
|
|
"""
|
|
|
|
entity_classes = self.get_all_entity_classes_in_a_dict()
|
|
|
|
|
|
|
|
item_class = entity_classes[item_dict["type"]]
|
|
|
|
return item_class(**item_dict)
|
|
|
|
|
|
|
|
def save_state(self) -> dict:
|
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
The inventory of the merchant is saved in a JSON format.
|
2020-12-11 16:20:50 +00:00
|
|
|
"""
|
|
|
|
d = super().save_state()
|
|
|
|
d["hazel"] = self.hazel
|
|
|
|
d["inventory"] = [item.save_state() for item in self.inventory]
|
|
|
|
return d
|
|
|
|
|
|
|
|
def add_to_inventory(self, obj: Any) -> None:
|
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Adds an object to the inventory.
|
2020-12-11 16:20:50 +00:00
|
|
|
"""
|
2020-12-18 16:30:03 +00:00
|
|
|
if obj not in self.inventory:
|
|
|
|
self.inventory.append(obj)
|
2020-12-11 16:20:50 +00:00
|
|
|
|
|
|
|
def remove_from_inventory(self, obj: Any) -> None:
|
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Removes an object from the inventory.
|
2020-12-11 16:20:50 +00:00
|
|
|
"""
|
2020-12-18 16:30:03 +00:00
|
|
|
if obj in self.inventory:
|
|
|
|
self.inventory.remove(obj)
|
2020-12-11 16:20:50 +00:00
|
|
|
|
|
|
|
def change_hazel_balance(self, hz: int) -> None:
|
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Changes the number of hazel the entity has by hz. hz is negative
|
|
|
|
when the entity loses money and positive when it gains money.
|
2020-12-11 16:20:50 +00:00
|
|
|
"""
|
|
|
|
self.hazel += hz
|