import random

from ..room import Room

ROOM_WIDTH = 3
ROOM_HEIGHT = 3


class Scattered:
    def __init__(self, dungeon, _args):
        self._dg = dungeon

    # TODO: Enable different methods for room generation: scattered, packed, bsp, ...
    def create_rooms(self):
        room_area = ROOM_WIDTH * ROOM_HEIGHT
        map_area = self._dg.width * self._dg.height
        max_rooms = map_area // room_area

        for _ in range(max_rooms):
            room = Room(3, 3)
            self.emplace_room(room)

    def emplace_room(self, room):
        tentatives = 10
        while tentatives > 0:
            # Find a random place, and try to place it
            x = (random.randint(0, self._dg.width - room.width) // 2) * 2
            y = (random.randint(0, self._dg.height - room.height) // 2) * 2
            room.set_position(x, y)

            tentatives -= 1
            if self._dg.add_room(room):
                return
