(Underdepths) Pass 6
This commit is contained in:
28 files changed
+5639
-31
No files matched your search
@@ -0,0 +1,325 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
import random
|
||||
|
||||
from data.constants import (
|
||||
BOSS_BREATH_DAMAGE,
|
||||
BOSS_BREATH_INTERVAL,
|
||||
FLEE_HP_FRACTION,
|
||||
KITE_MAX_DISTANCE,
|
||||
RANGED_HIT_PENALTY_PER_TILE,
|
||||
RANGED_MIN_DISTANCE,
|
||||
SIGHT_RADIUS,
|
||||
SPLIT_COPIES,
|
||||
SPLIT_STAT_DIVISOR,
|
||||
TILE_WALL,
|
||||
WANDER_WAIT_CHANCE,
|
||||
)
|
||||
from engine.combat import attack, log_message
|
||||
from engine.map_gen import GameMap
|
||||
from engine.pathfinding import astar, random_step
|
||||
from entities.enemy import Enemy
|
||||
from entities.player import Player
|
||||
|
||||
Position = tuple[int, int]
|
||||
|
||||
_STEPS: tuple[tuple[int, int], ...] = ((0, -1), (0, 1), (-1, 0), (1, 0))
|
||||
|
||||
|
||||
def _distance(a: Position, b: Position) -> float:
|
||||
return math.hypot(a[0] - b[0], a[1] - b[1])
|
||||
|
||||
|
||||
def _is_adjacent(enemy: Enemy, player: Player) -> bool:
|
||||
return abs(enemy.x - player.x) + abs(enemy.y - player.y) == 1
|
||||
|
||||
|
||||
def _line_of_sight(game_map: GameMap, start: Position, end: Position) -> bool:
|
||||
"""Bresenham ray; walls on intermediate tiles block sight."""
|
||||
x, y = start
|
||||
x1, y1 = end
|
||||
dx = abs(x1 - x)
|
||||
dy = -abs(y1 - y)
|
||||
sx = 1 if x < x1 else -1
|
||||
sy = 1 if y < y1 else -1
|
||||
err = dx + dy
|
||||
while (x, y) != (x1, y1):
|
||||
e2 = 2 * err
|
||||
if e2 >= dy:
|
||||
err += dy
|
||||
x += sx
|
||||
if e2 <= dx:
|
||||
err += dx
|
||||
y += sy
|
||||
if (x, y) == (x1, y1):
|
||||
return True
|
||||
if game_map.tile_at(x, y) == TILE_WALL:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def sees_player(enemy: Enemy, player: Player, game_map: GameMap) -> bool:
|
||||
if player.is_invisible:
|
||||
return False
|
||||
return _distance(enemy.position, player.position) <= SIGHT_RADIUS and _line_of_sight(
|
||||
game_map, enemy.position, player.position
|
||||
)
|
||||
|
||||
|
||||
def _blocked_positions(game_map: GameMap, enemy: Enemy, player: Player) -> set[Position]:
|
||||
blocked = {player.position}
|
||||
blocked.update(
|
||||
entity.position
|
||||
for entity in game_map.entities
|
||||
if entity is not enemy and entity.blocks_movement
|
||||
)
|
||||
return blocked
|
||||
|
||||
|
||||
def _walkable_options(
|
||||
enemy: Enemy, game_map: GameMap, blocked: set[Position]
|
||||
) -> list[Position]:
|
||||
return [
|
||||
(enemy.x + dx, enemy.y + dy)
|
||||
for dx, dy in _STEPS
|
||||
if game_map.is_walkable(enemy.x + dx, enemy.y + dy)
|
||||
and (enemy.x + dx, enemy.y + dy) not in blocked
|
||||
]
|
||||
|
||||
|
||||
def _act_wander(
|
||||
enemy: Enemy, game_map: GameMap, blocked: set[Position], rng: random.Random
|
||||
) -> None:
|
||||
if rng.random() < WANDER_WAIT_CHANCE:
|
||||
return
|
||||
options = _walkable_options(enemy, game_map, blocked)
|
||||
if options:
|
||||
enemy.x, enemy.y = rng.choice(options)
|
||||
|
||||
|
||||
def _move_toward(
|
||||
enemy: Enemy,
|
||||
target: Position,
|
||||
game_map: GameMap,
|
||||
blocked: set[Position],
|
||||
rng: random.Random,
|
||||
) -> None:
|
||||
path = astar(game_map, enemy.position, target, blocked)
|
||||
if path:
|
||||
enemy.x, enemy.y = path[0]
|
||||
return
|
||||
fallback = random_step(game_map, enemy.position)
|
||||
if fallback is not None and fallback not in blocked:
|
||||
enemy.x, enemy.y = fallback
|
||||
|
||||
|
||||
def _move_away(
|
||||
enemy: Enemy,
|
||||
player: Player,
|
||||
game_map: GameMap,
|
||||
blocked: set[Position],
|
||||
rng: random.Random,
|
||||
) -> None:
|
||||
options = _walkable_options(enemy, game_map, blocked)
|
||||
if options:
|
||||
enemy.x, enemy.y = max(
|
||||
options,
|
||||
key=lambda pos: (_distance(pos, player.position), rng.random()),
|
||||
)
|
||||
|
||||
|
||||
def _pursue_last_known(
|
||||
enemy: Enemy,
|
||||
game_map: GameMap,
|
||||
blocked: set[Position],
|
||||
rng: random.Random,
|
||||
) -> None:
|
||||
if enemy.last_known_position is None:
|
||||
_act_wander(enemy, game_map, blocked, rng)
|
||||
return
|
||||
if enemy.position == enemy.last_known_position:
|
||||
enemy.last_known_position = None
|
||||
_act_wander(enemy, game_map, blocked, rng)
|
||||
return
|
||||
_move_toward(enemy, enemy.last_known_position, game_map, blocked, rng)
|
||||
|
||||
|
||||
def _act_chase(
|
||||
enemy: Enemy,
|
||||
player: Player,
|
||||
game_map: GameMap,
|
||||
log: list[str],
|
||||
rng: random.Random,
|
||||
) -> None:
|
||||
if _is_adjacent(enemy, player):
|
||||
attack(enemy, player, log, rng=rng)
|
||||
return
|
||||
blocked = _blocked_positions(game_map, enemy, player)
|
||||
if sees_player(enemy, player, game_map):
|
||||
enemy.last_known_position = player.position
|
||||
_move_toward(enemy, player.position, game_map, blocked, rng)
|
||||
else:
|
||||
_pursue_last_known(enemy, game_map, blocked, rng)
|
||||
|
||||
|
||||
def _act_flee(
|
||||
enemy: Enemy,
|
||||
player: Player,
|
||||
game_map: GameMap,
|
||||
log: list[str],
|
||||
rng: random.Random,
|
||||
) -> None:
|
||||
if enemy.hp < enemy.max_hp * FLEE_HP_FRACTION:
|
||||
_move_away(enemy, player, game_map, _blocked_positions(game_map, enemy, player), rng)
|
||||
return
|
||||
_act_chase(enemy, player, game_map, log, rng)
|
||||
|
||||
|
||||
def _act_ranged(
|
||||
enemy: Enemy,
|
||||
player: Player,
|
||||
game_map: GameMap,
|
||||
log: list[str],
|
||||
rng: random.Random,
|
||||
) -> None:
|
||||
dist = _distance(enemy.position, player.position)
|
||||
blocked = _blocked_positions(game_map, enemy, player)
|
||||
if sees_player(enemy, player, game_map):
|
||||
enemy.last_known_position = player.position
|
||||
if dist <= KITE_MAX_DISTANCE:
|
||||
_move_away(enemy, player, game_map, blocked, rng)
|
||||
elif dist > RANGED_MIN_DISTANCE:
|
||||
power = enemy.ranged_power or enemy.power
|
||||
attack(
|
||||
enemy,
|
||||
player,
|
||||
log,
|
||||
rng=rng,
|
||||
power_override=power,
|
||||
hit_modifier=-RANGED_HIT_PENALTY_PER_TILE * dist,
|
||||
)
|
||||
else:
|
||||
_move_toward(enemy, player.position, game_map, blocked, rng)
|
||||
else:
|
||||
_pursue_last_known(enemy, game_map, blocked, rng)
|
||||
|
||||
|
||||
def _act_phase(
|
||||
enemy: Enemy,
|
||||
player: Player,
|
||||
game_map: GameMap,
|
||||
log: list[str],
|
||||
rng: random.Random,
|
||||
) -> None:
|
||||
if _is_adjacent(enemy, player):
|
||||
attack(enemy, player, log, rng=rng)
|
||||
return
|
||||
blocked = _blocked_positions(game_map, enemy, player)
|
||||
dx, dy = player.x - enemy.x, player.y - enemy.y
|
||||
sx = (dx > 0) - (dx < 0)
|
||||
sy = (dy > 0) - (dy < 0)
|
||||
x_first = abs(dx) >= abs(dy)
|
||||
candidates = (
|
||||
(enemy.x + (sx if x_first else 0), enemy.y + (sy if not x_first else 0)),
|
||||
(enemy.x + (0 if x_first else sx), enemy.y + (sy if x_first else 0)),
|
||||
)
|
||||
for step in candidates:
|
||||
if step != enemy.position and game_map.in_bounds(*step) and step not in blocked:
|
||||
enemy.x, enemy.y = step
|
||||
return
|
||||
|
||||
|
||||
def dragon_breath(dragon: Enemy, player: Player, game_map: GameMap, log: list[str]) -> None:
|
||||
"""Breath weapon: flat damage to everything on the Bresenham line toward
|
||||
the player. Stops at walls; the player takes BOSS_BREATH_DAMAGE if caught."""
|
||||
log_message(log, "The Ancient Dragon breathes a torrent of flame!")
|
||||
x, y = dragon.position
|
||||
px, py = player.position
|
||||
dx = abs(px - x)
|
||||
dy = -abs(py - y)
|
||||
sx = 1 if x < px else -1
|
||||
sy = 1 if y < py else -1
|
||||
err = dx + dy
|
||||
while (x, y) != (px, py):
|
||||
e2 = 2 * err
|
||||
if e2 >= dy:
|
||||
err += dy
|
||||
x += sx
|
||||
if e2 <= dx:
|
||||
err += dx
|
||||
y += sy
|
||||
if (x, y) == (px, py):
|
||||
break
|
||||
if game_map.tile_at(x, y) == TILE_WALL:
|
||||
return
|
||||
player.hp -= BOSS_BREATH_DAMAGE
|
||||
log_message(log, f"You are caught in the flames and take {BOSS_BREATH_DAMAGE} damage!")
|
||||
|
||||
|
||||
|
||||
def take_turn(
|
||||
enemy: Enemy,
|
||||
player: Player,
|
||||
game_map: GameMap,
|
||||
turn: int,
|
||||
log: list[str],
|
||||
rng: random.Random | None = None,
|
||||
) -> None:
|
||||
"""Dispatch one enemy turn. Pure with respect to globals: everything it
|
||||
reads or mutates is passed in (enemy, map, log, turn)."""
|
||||
rng = rng or random
|
||||
if enemy.speed > 1 and turn % enemy.speed != 0:
|
||||
return
|
||||
if enemy.behavior == "chase":
|
||||
_act_chase(enemy, player, game_map, log, rng)
|
||||
elif enemy.behavior == "flee":
|
||||
_act_flee(enemy, player, game_map, log, rng)
|
||||
elif enemy.behavior == "ranged":
|
||||
_act_ranged(enemy, player, game_map, log, rng)
|
||||
elif enemy.behavior == "phase":
|
||||
_act_phase(enemy, player, game_map, log, rng)
|
||||
elif enemy.behavior == "split":
|
||||
_act_chase(enemy, player, game_map, log, rng)
|
||||
elif enemy.behavior == "boss":
|
||||
if (
|
||||
turn % BOSS_BREATH_INTERVAL == 0
|
||||
and sees_player(enemy, player, game_map)
|
||||
and not _is_adjacent(enemy, player)
|
||||
):
|
||||
dragon_breath(enemy, player, game_map, log)
|
||||
else:
|
||||
_act_chase(enemy, player, game_map, log, rng)
|
||||
else:
|
||||
_act_wander(enemy, game_map, _blocked_positions(game_map, enemy, player), rng)
|
||||
|
||||
|
||||
def split_on_death(enemy: Enemy) -> list[Enemy]:
|
||||
"""Split death hook: two half-stat copies, spawned adjacent to the corpse.
|
||||
Copies cannot split again. Returns [] when no splits remain."""
|
||||
if enemy.splits_remaining <= 0:
|
||||
return []
|
||||
half_hp = max(1, enemy.max_hp // SPLIT_STAT_DIVISOR)
|
||||
half_power = max(1, enemy.power // SPLIT_STAT_DIVISOR)
|
||||
copies = [
|
||||
Enemy(
|
||||
x=enemy.x + dx,
|
||||
y=enemy.y,
|
||||
key=enemy.key,
|
||||
name=f"Small {enemy.name}",
|
||||
sprite_key=enemy.sprite_key,
|
||||
hp=half_hp,
|
||||
max_hp=half_hp,
|
||||
power=half_power,
|
||||
armor=enemy.armor,
|
||||
accuracy=enemy.accuracy,
|
||||
evasion=enemy.evasion,
|
||||
crit_chance=enemy.crit_chance,
|
||||
speed=enemy.speed,
|
||||
behavior=enemy.behavior,
|
||||
xp_reward=max(1, enemy.xp_reward // SPLIT_STAT_DIVISOR),
|
||||
splits_remaining=enemy.splits_remaining - 1,
|
||||
)
|
||||
for dx in (-1, 1)
|
||||
]
|
||||
return copies[:SPLIT_COPIES]
|
||||
Reference in new issue
Block a user