Skip to content

Commit

Permalink
Add check to not place entities if theres no player.
Browse files Browse the repository at this point in the history
This is made to debug map generators.
  • Loading branch information
Julioevm committed Feb 4, 2024
1 parent 12c5907 commit d17096b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
24 changes: 14 additions & 10 deletions map_gen/generate_cave.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Generator of cave type maps."""

from __future__ import annotations
from typing import List, TYPE_CHECKING
import random
Expand Down Expand Up @@ -76,23 +77,26 @@ def generate_cave(

if len(rooms) == 0:
# The first room, where the player starts.
player.place(*new_room.center, dungeon)
if player is not None:
player.place(*new_room.center, dungeon)
else: # All rooms after the first.
# Dig out a tunnel between this room and the previous one.
for x, y in tunnel_between(rooms[-1].center, new_room.center):
dungeon.tiles[x, y] = tile_types.dirt_floor

if (
current_encounters <= max_encounters
and len(rooms) > 0 # Skip first room
and random.random() < DUNGEON_ENCOUNTER_CHANCE
):
if place_encounter(new_room, dungeon, floor):
current_encounters += 1
# Only place encounters and entities if we have a player
if player is not None:
if (
current_encounters <= max_encounters
and len(rooms) > 0 # Skip first room
and random.random() < DUNGEON_ENCOUNTER_CHANCE
):
if place_encounter(new_room, dungeon, floor):
current_encounters += 1
else:
place_entities(new_room, dungeon, floor)
else:
place_entities(new_room, dungeon, floor)
else:
place_entities(new_room, dungeon, floor)

rooms.append(new_room)

Expand Down
24 changes: 13 additions & 11 deletions map_gen/generate_dungeon.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Generator of dungeon type maps."""

from __future__ import annotations

import random
Expand Down Expand Up @@ -65,7 +66,8 @@ def generate_dungeon(

if len(rooms) == 0:
# The first room, where the player starts.
player.place(*new_room.center, dungeon)
if player is not None:
player.place(*new_room.center, dungeon)
else: # All rooms after the first.
# Dig out a tunnel between this room and the previous one.
has_placed_first_door = False
Expand Down Expand Up @@ -95,18 +97,18 @@ def generate_dungeon(
if tile == tile_types.closed_door and not walkable_count <= 5:
continue
dungeon.tiles[x, y] = tile_types.floor

if (
current_encounters <= max_encounters
and len(rooms) > 0 # Skip first room
and random.random() < DUNGEON_ENCOUNTER_CHANCE
):
if place_encounter(new_room, dungeon, floor):
current_encounters += 1
if player is not None:
if (
current_encounters <= max_encounters
and len(rooms) > 0 # Skip first room
and random.random() < DUNGEON_ENCOUNTER_CHANCE
):
if place_encounter(new_room, dungeon, floor):
current_encounters += 1
else:
place_entities(new_room, dungeon, floor)
else:
place_entities(new_room, dungeon, floor)
else:
place_entities(new_room, dungeon, floor)

rooms.append(new_room)

Expand Down

0 comments on commit d17096b

Please sign in to comment.