Skip to content

Commit

Permalink
Add a system to ensure some items always spawn in the dungeon.
Browse files Browse the repository at this point in the history
  • Loading branch information
Julioevm committed Jan 3, 2024
1 parent b01b0f7 commit fa6fa5d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
12 changes: 12 additions & 0 deletions game_world.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import utils
from typing import TYPE_CHECKING, List
from exceptions import Impossible
from map_gen import procgen
from map_gen.debug_room import create_debug_room
from map_gen.generate_cave import generate_cave
from map_gen.generate_dungeon import generate_dungeon
Expand Down Expand Up @@ -45,6 +47,14 @@ def __init__(
self.current_floor = current_floor
self.floors: List[GameMap] = []

def set_fixed_items(self, items) -> None:
items = list(items)
fixed_items = procgen.get_fixed_items(self.current_floor)

# replace some items with the items in fixed_items if there are any
if len(fixed_items) > 0:
utils.replace_items_in_list(items, 0, fixed_items)

def generate_floor(self) -> None:
"""Generate a new floor, using the corresponding floor generator function."""
if self.engine.debug_mode:
Expand All @@ -67,6 +77,8 @@ def generate_floor(self) -> None:
# Generate the game map using the chosen generator function
game_map = floor_generator(**additional_params)

self.set_fixed_items(game_map.items)

self.floors.append(game_map)
self.engine.game_map = game_map

Expand Down
19 changes: 19 additions & 0 deletions map_gen/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,22 @@
(actor_factories.werewolf, 10),
],
}


fixed_item_by_floor: Dict[int, List[Tuple[Entity, int]]] = {
1: [
(item_factories.health_potion, 1),
],
2: [
(item_factories.health_potion, 2),
],
3: [
(item_factories.health_potion, 2),
],
4: [
(item_factories.great_health_potion, 1),
],
5: [
(item_factories.great_health_potion, 1),
],
}
7 changes: 7 additions & 0 deletions map_gen/procgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ def tunnel_between(
yield x, y


def get_fixed_items(floor: int) -> List[Entity]:
items_with_counts = parameters.fixed_item_by_floor.get(floor, [])
result = [item for item, count in items_with_counts for _ in range(count)]
return result


def place_entities(room: Room, dungeon: GameMap, floor: int):
"""
Place entities in a given room of a cave.
Expand All @@ -89,6 +95,7 @@ def place_entities(room: Room, dungeon: GameMap, floor: int):
monsters = get_entities_at_random(parameters.enemy_chances, num_monsters, floor)
items = get_entities_at_random(parameters.item_chances, num_items, floor)


for entity in monsters + items:
placed = False
while not placed:
Expand Down
8 changes: 8 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def replace_items_in_list(target_list, start_index, items_to_replace):
# Ensure the start_index + number of items does not exceed the length of the target_list
end_index = min(start_index + len(items_to_replace), len(target_list))

# Replace elements in target_list with elements from items_to_replace
target_list[start_index:end_index] = items_to_replace[: end_index - start_index]

return target_list

0 comments on commit fa6fa5d

Please sign in to comment.