Skip to content

Commit

Permalink
Merge pull request #7 from Nuno-Jesus/feature/flood-fill-map-generation
Browse files Browse the repository at this point in the history
Enhanced map generation
  • Loading branch information
Nuno-Jesus authored Feb 7, 2023
2 parents 56abb7e + 257a69f commit 6cb7444
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 49 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ PY = python3 -B
MKFLAGS = --no-print-directory

#_/=\_/=\_/=\_/=\_/=\_/=\_/=\_/=\_/=\_/=\_ FOLDERS _/=\_/=\_/=\_/=\_/=\_/=\_/=\_/=\_/=\_/=\_
PROJ = ../so_long
PROJ = ../andreia

#_/=\_/=\_/=\_/=\_/=\_/=\_/=\_/=\_/=\_/=\_ RULES _/=\_/=\_/=\_/=\_/=\_/=\_/=\_/=\_/=\_/=\_
all: setup

setup:
echo "Checking for updates"
git pull origin main
echo "[$(CYAN) Git $(RESET)] Checking for updates..."
# git pull origin main
sh tester.sh $(PROJ)

clean:
Expand Down
113 changes: 79 additions & 34 deletions generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,87 @@
LGREEN = Fore.LIGHTGREEN_EX

# OTHER MACROS
ENTITIES = "01C"
GEN_PATH = "maps/generated"
WALL_PROBABILITY = 0.80
COIN_PROBABILITY = 0.80
HORIZONTAL = 0
VERTICAL = 1
DIRECTIONS = [HORIZONTAL, VERTICAL]

def generate_coins(contents, x, y) -> None:
num_coins = random.randint(0, max(x, y) * COIN_PROBABILITY)
print(f"Num coins: {num_coins}")

for i in range(0, num_coins):
cy = random.randint(1, y - 2)
cx = random.randint(1, x - 2)

#print(f"Coin (X/Y): {[cx, cy]}")
if contents[cy][cx] == '0':
contents[cy][cx] = 'C'

def generate_player_and_exit(contents, x, y) -> None:
py = random.randint(1, y - 2)
px = random.randint(1, x - 2)

def generate_entity() -> str:
n = random.randint(1, 100)
print(f"Player (X/Y): {[px, py]}")
ey = random.randint(1, y - 2)
ex = random.randint(1, x - 2)

if n in range(1, 70):
return '0'
elif n in range(71, 95):
return '1'
else:
return 'C'
print(f"Exit (X/Y): {[ex, ey]}")
contents[py][px] = 'P'
contents[ey][ex] = 'E'

def flood_fill(contents, point, dims, prob):
if contents[point[1]][point[0]] != '0' or \
point[1] not in range(0, dims[1] - 1) or \
point[0] not in range(0, dims[0] - 1):
return

n = random.randint(1, 100)
if n in range(0, 100 - prob):
contents[point[1]][point[0]] = '1'
return
flood_fill(contents, [point[0] - 1, point[1]], dims, int(prob * WALL_PROBABILITY)) #L
flood_fill(contents, [point[0] + 1, point[1]], dims, int(prob * WALL_PROBABILITY)) #R
flood_fill(contents, [point[0], point[1] - 1], dims, int(prob * WALL_PROBABILITY)) #U
flood_fill(contents, [point[0], point[1] + 1], dims, int(prob * WALL_PROBABILITY)) #D

def generate_walls(contents, x, y):
for line in contents[2::2]:
px = random.randint(0, x - 1)
width = random.randint(0, x - 1)

#print(f'{line} -> x = {px} width = {width}')
i = 0
for i in range (0, x - 1):
if line[i] == '0' and i in range(px, px + width):
line[i] = '1'
i += 1

for i in range(0, x - 1, 3):
py = random.randint(0, y - 1)
width = random.randint(0, y - 1)

#print(f'col -> y = {py} width = {width}')
for k in range (0, y - 1):
if contents[k][i] == '0' and k in range(py, py + width):
contents[k][i] = '1'

#flood_fill(contents, [px, py], [x, y], 100)


def generate_map(filename, x, y) -> None:
contents = []

contents.append(['1' for _ in range(0, x)] + ['\n'])
for _ in range(0, y - 2):
contents.append(['1'] + [generate_entity() for _ in range(0, x - 2)] + ['1', '\n'])
contents.append(['1'] + list((x - 2) * "0") + ['1', '\n'])
contents.append(['1' for _ in range(0, x)] + ['\n'])

py = random.randint(1, y - 2)
px = random.randint(1, x - 2)

ey = random.randint(1, y - 2)
ex = random.randint(1, x - 2)

contents[py][px] = 'P'
contents[ey][ex] = 'E'
generate_player_and_exit(contents, x, y)
generate_coins(contents, x, y)
generate_walls(contents, x, y)

f = open(filename, "x")
for line in contents:
Expand All @@ -49,26 +100,20 @@ def generate_map(filename, x, y) -> None:
f.close()
print(f'The map was saved in {LRED}{filename}{RESET}.')


def generate_file() -> str:
n = 1

while os.path.exists(f'{GEN_PATH}/map{n}.ber'):
n += 1

return (f'{GEN_PATH}/map{n}.ber')

if __name__ == "__main__" :
x = -1
y = -1
n = 1
x, y = -1, -1

while y < 0 or x < 0:
while y < 3 or x < 3:
try:
x = int(input(f'Width: '))
y = int(input(f'Height: '))
except ValueError:
print(f'\n\t===== {LRED}INPUT MUST BE BIGGER THAN 3{RESET} =====\n')

random.seed()
generate_map(generate_file(), x, y)
print(f'\n\t===== {LRED}INPUT MUST BE BIGGER THAN 2{RESET} =====\n')

while os.path.exists(f'{GEN_PATH}/map{n}.ber'):
n += 1

random.seed()
generate_map(f'{GEN_PATH}/map{n}.ber', x, y)

4 changes: 0 additions & 4 deletions maps/generated/map1.ber

This file was deleted.

6 changes: 0 additions & 6 deletions maps/generated/map2.ber

This file was deleted.

4 changes: 2 additions & 2 deletions tester.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ do
echo "\n\t--------------------- TEST $i ---------------------\n" >> $LOGFILE
echo -n "File: \"$test_file\" " >> $LOGFILE
$1/so_long $TESTS/$test_file > $TEMPFILE 2>&1

if [ $(grep Error $TEMPFILE) ]; then
if [ "$(grep Error $TEMPFILE)" ]; then
echo -n "$GREEN"OK"$RESET " && echo>> $LOGFILE
echo "-------- START OF OUTPUT --------" >> $LOGFILE
cat -e $TEMPFILE >> $LOGFILE
Expand Down

0 comments on commit 6cb7444

Please sign in to comment.