-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_walls.py
More file actions
61 lines (45 loc) · 2.42 KB
/
Copy pathextract_walls.py
File metadata and controls
61 lines (45 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from glob import glob
from os import makedirs
from argparse import ArgumentParser
import numpy as np
def main(input_directory: str, output_directory: str, verbose: bool = False):
iteration = 0
files = glob(f"{input_directory}/*")
makedirs(output_directory, exist_ok=True)
for filepath in files:
with open(filepath, 'r') as file:
# Extract the entire board from the file
board = np.array([list(line[:-1]) for line in file.readlines()])
if verbose:
print('\n'.join(map(''.join, board)))
# Add the outside walls to fill the gaps until the actual stage begins
for i, first_wall in enumerate(np.argmax(board == '#', 1)):
board[i, :first_wall] = "#"
for i, last_wall in enumerate(np.argmax((board == '#')[:, ::-1], 1)):
board[i, -(last_wall + 1):] = "#"
for i, first_wall in enumerate(np.argmax(board == '#', 0)):
board[:first_wall, i] = "#"
for i, last_wall in enumerate(np.argmax((board == '#')[:, ::-1], 0)):
board[-(last_wall + 1):, i] = "#"
if verbose:
print('\n'.join(map(''.join, board)))
print('-' * 80)
# Extract just the walls
walls = np.array(np.where(board == '#')).T + 1
walls = np.ascontiguousarray(walls)
size = board.shape[::-1]
# Output the target filetype
with open(f"{output_directory}/{iteration:04d}.txt", 'w') as output_file:
print(" ".join(map(str, size)), file=output_file)
print(len(walls), end = ' ', file=output_file)
print(' '.join(map(str, walls.ravel())), file=output_file)
print(0, file=output_file)
print(0, file=output_file)
print("0 0", end="", file=output_file)
iteration += 1
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("input_directory", type=str, help="Location with the map files to parse.")
parser.add_argument("output_directory", type=str, help="Location to place the output txt files.")
parser.add_argument('-v', "--verbose", action='store_true', help="Print the boards while creating them.")
main(**parser.parse_args().__dict__)