2020-12-11 01:19:59 +00:00
|
|
|
# Copyright (C) 2020 by ÿnérant, eichhornchen, nicomarg, charlse
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-12-06 22:55:57 +00:00
|
|
|
from enum import auto, Enum
|
2020-12-04 15:02:48 +00:00
|
|
|
from random import choice, random, randint
|
2020-12-11 01:19:59 +00:00
|
|
|
from typing import Tuple
|
|
|
|
|
2020-12-11 01:17:00 +00:00
|
|
|
from ..interfaces import Map, Tile
|
2020-12-04 15:02:48 +00:00
|
|
|
|
|
|
|
|
2020-12-11 01:19:59 +00:00
|
|
|
DEFAULT_PARAMS = {
|
|
|
|
"split_chance": .15,
|
|
|
|
"turn_chance": .5,
|
|
|
|
"death_chance": .1,
|
|
|
|
"max_walkers": 15,
|
|
|
|
"width": 100,
|
|
|
|
"height": 100,
|
|
|
|
"fill": .4,
|
|
|
|
}
|
2020-12-04 17:01:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Directions(Enum):
|
|
|
|
up = auto()
|
|
|
|
down = auto()
|
|
|
|
left = auto()
|
|
|
|
right = auto()
|
|
|
|
|
|
|
|
|
|
|
|
class Walker:
|
2020-12-11 01:19:59 +00:00
|
|
|
def __init__(self, x: int, y: int):
|
2020-12-04 17:01:54 +00:00
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
self.dir = choice(list(Directions))
|
|
|
|
|
2020-12-11 01:19:59 +00:00
|
|
|
def random_turn(self) -> None:
|
2020-12-04 17:01:54 +00:00
|
|
|
self.dir = choice(list(Directions))
|
|
|
|
|
2020-12-11 01:19:59 +00:00
|
|
|
def next_pos(self) -> Tuple[int, int]:
|
2020-12-04 17:01:54 +00:00
|
|
|
if self.dir == Directions.up:
|
|
|
|
return self.x, self.y + 1
|
|
|
|
elif self.dir == Directions.down:
|
|
|
|
return self.x, self.y - 1
|
|
|
|
elif self.dir == Directions.right:
|
|
|
|
return self.x + 1, self.y
|
|
|
|
elif self.dir == Directions.left:
|
|
|
|
return self.x - 1, self.y
|
|
|
|
|
2020-12-11 01:19:59 +00:00
|
|
|
def move_in_bounds(self, width: int, height: int) -> None:
|
2020-12-04 17:01:54 +00:00
|
|
|
nx, ny = self.next_pos()
|
|
|
|
if 0 < nx < width and 0 < ny < height:
|
|
|
|
self.x, self.y = nx, ny
|
|
|
|
|
2020-12-11 01:19:59 +00:00
|
|
|
def split(self) -> "Walker":
|
2020-12-04 17:03:41 +00:00
|
|
|
child = Walker(self.x, self.y)
|
|
|
|
child.dir = self.dir
|
|
|
|
return child
|
|
|
|
|
2020-12-04 17:01:54 +00:00
|
|
|
|
|
|
|
class Generator:
|
2020-12-11 01:19:59 +00:00
|
|
|
def __init__(self, params: dict = None):
|
|
|
|
if params is None:
|
|
|
|
params = DEFAULT_PARAMS
|
2020-12-04 15:02:48 +00:00
|
|
|
self.params = params
|
|
|
|
|
2020-12-11 01:19:59 +00:00
|
|
|
def run(self) -> Map:
|
2020-12-04 15:02:48 +00:00
|
|
|
width, height = self.params["width"], self.params["height"]
|
2020-12-11 01:19:59 +00:00
|
|
|
walkers = [Walker(width // 2, height // 2)]
|
2020-12-11 00:09:49 +00:00
|
|
|
grid = [[Tile.EMPTY for _ in range(width)] for _ in range(height)]
|
2020-12-04 15:02:48 +00:00
|
|
|
count = 0
|
2020-12-11 01:19:59 +00:00
|
|
|
while count < self.params["fill"] * width * height:
|
2020-12-04 15:02:48 +00:00
|
|
|
# because we can't add or remove walkers while looping over the pop
|
|
|
|
# we need lists to keep track of what will be the walkers for the
|
|
|
|
# next iteration of the main loop
|
|
|
|
next_walker_pop = []
|
|
|
|
|
|
|
|
for walker in walkers:
|
2020-12-11 00:09:49 +00:00
|
|
|
if grid[walker.y][walker.x] == Tile.EMPTY:
|
2020-12-04 15:02:48 +00:00
|
|
|
count += 1
|
2020-12-11 00:09:49 +00:00
|
|
|
grid[walker.y][walker.x] = Tile.FLOOR
|
2020-12-04 15:02:48 +00:00
|
|
|
if random() < self.params["turn_chance"]:
|
|
|
|
walker.random_turn()
|
|
|
|
walker.move_in_bounds(width, height)
|
|
|
|
if random() > self.params["death_chance"]:
|
|
|
|
next_walker_pop.append(walker)
|
|
|
|
|
2020-12-06 23:24:31 +00:00
|
|
|
# we make sure to never kill all walkers
|
2020-12-11 01:19:59 +00:00
|
|
|
if not next_walker_pop:
|
2020-12-11 00:24:20 +00:00
|
|
|
next_walker_pop.append(choice(walkers))
|
2020-12-06 23:24:31 +00:00
|
|
|
|
2020-12-11 01:19:59 +00:00
|
|
|
# we use a second loop for spliting so we're not bothered by cases
|
2020-12-04 15:02:48 +00:00
|
|
|
# like a walker not spliting because we hit the population cap even
|
|
|
|
# though the next one would have died and freed a place
|
|
|
|
# not a big if it happened though
|
|
|
|
for walker in walkers:
|
|
|
|
if len(next_walker_pop) < self.params["max_walkers"]:
|
|
|
|
if random() < self.params["split_chance"]:
|
|
|
|
next_walker_pop.append(walker.split())
|
|
|
|
walkers = next_walker_pop
|
|
|
|
|
2020-12-11 01:19:59 +00:00
|
|
|
start_x, start_y = randint(0, width - 1), randint(0, height - 1)
|
2020-12-11 00:09:49 +00:00
|
|
|
while grid[start_y][start_x] != Tile.FLOOR:
|
2020-12-11 01:19:59 +00:00
|
|
|
start_x, start_y = randint(0, width - 1), randint(0, height - 1)
|
2020-12-04 15:02:48 +00:00
|
|
|
|
|
|
|
return Map(width, height, grid, start_x, start_y)
|