1
0
mirror of https://github.com/abakh/nbsdgames synced 2025-05-17 15:49:33 -04:00
nbsdgames/bubbob/save_rnglevel.py

126 lines
3.2 KiB
Python
Raw Normal View History

2021-02-04 08:37:39 +01:00
#
# This script outputs the random levels in a format you can
# save into a file in the levels-directory and bub'n'bros
# will be able to use it.
#
# this accepts the following parameters:
# -seed N use random seed N for the generation
#
import sys
import random
import string
sys.path.append('..')
sys.path.append('../common')
n_lvls = 1
idx = 0
while idx < len(sys.argv):
arg = sys.argv[idx]
idx += 1
if arg == '-seed':
arg = sys.argv[idx]
idx += 1
2022-02-03 13:46:22 +03:30
print("# Using seed: " + arg + "\n")
2021-02-04 08:37:39 +01:00
random.seed(arg)
def printlvl(level):
mons = {}
monconv = {}
tmpmons = {}
# Populate monster tables
for y in range(0,level.HEIGHT):
for x in range(0,level.WIDTH):
wm = level.wmap[y][x]
if wm >= 'a':
m = getattr(level, wm)
if m.dir == 1:
dir = 'L'
else:
dir = 'R'
s = dir + m.cls.__name__
2022-02-03 13:46:22 +03:30
if s in tmpmons:
2021-02-04 08:37:39 +01:00
tmpmons[s].append(wm)
else:
tmpmons[s] = [wm]
# Build monster character conversion tables
lettr = 'a'
for m in tmpmons:
for n in tmpmons[m]:
monconv[n] = lettr
mons[lettr] = m
lettr = chr(ord(lettr) + 1)
# Build walls, replacing monsters from mons[]
walls = ""
for y in range(0,level.HEIGHT-1):
walls += "##"
for x in range(0,level.WIDTH):
wm = level.wmap[y][x]
if wm >= 'a':
2022-02-03 13:46:22 +03:30
if wm in monconv:
2021-02-04 08:37:39 +01:00
walls += monconv[wm]
else:
walls += '?'
else:
walls += wm
walls += "##\n"
walls += "##"
for x in range(0,level.WIDTH):
if level.wmap[0][x] == '#' or level.wmap[level.HEIGHT-1][x] == '#':
walls += "#"
else:
walls += " "
walls += "##\n"
# Build winds
winds = ""
for y in range(0,level.HEIGHT):
for x in range(0,level.WIDTH+4):
winds += level.winds[y][x]
winds += "\n"
for m in mons:
2022-02-03 13:46:22 +03:30
print(" " + m + " = " + mons[m])
2021-02-04 08:37:39 +01:00
if level.letter:
2022-02-03 13:46:22 +03:30
print(" letter = 1")
2021-02-04 08:37:39 +01:00
if level.fire:
2022-02-03 13:46:22 +03:30
print(" fire = 1")
2021-02-04 08:37:39 +01:00
if level.lightning:
2022-02-03 13:46:22 +03:30
print(" lightning = 1")
2021-02-04 08:37:39 +01:00
if level.water:
2022-02-03 13:46:22 +03:30
print(" water = 1")
2021-02-04 08:37:39 +01:00
if level.top:
2022-02-03 13:46:22 +03:30
print(" top = 1")
2021-02-04 08:37:39 +01:00
2022-02-03 13:46:22 +03:30
print(" walls = \"\"\"\n" + walls + "\"\"\"")
print(" winds = \"\"\"\n" + winds + "\"\"\"")
2021-02-04 08:37:39 +01:00
for i in range(n_lvls):
2022-02-03 13:46:22 +03:30
print("""
2021-02-04 08:37:39 +01:00
import boarddef, mnstrmap, random
from boarddef import LNasty, LMonky, LGhosty, LFlappy
from boarddef import LSpringy, LOrcy, LGramy, LBlitzy
from boarddef import RNasty, RMonky, RGhosty, RFlappy
from boarddef import RSpringy, ROrcy, RGramy, RBlitzy
2022-02-03 13:46:22 +03:30
""")
2021-02-04 08:37:39 +01:00
d = {'__name__': 'RandomLevels'}
2022-02-03 13:46:22 +03:30
exec(compile(open('levels/RandomLevels.py', "rb").read(), 'levels/RandomLevels.py', 'exec'), d)
2021-02-04 08:37:39 +01:00
for i, Lvl in enumerate(d['GenerateLevels']()):
level = Lvl(i)
if level.monsters:
2022-02-03 13:46:22 +03:30
print("\n\nclass level%02d(boarddef.Level):" % (i+1))
2021-02-04 08:37:39 +01:00
else:
2022-02-03 13:46:22 +03:30
print("\n\nclass levelFinal(boarddef.Level):")
2021-02-04 08:37:39 +01:00
printlvl(level)
2022-02-03 13:46:22 +03:30
print()