1 # Copyright (c) 2011-2020 Philip Pavlick
3 # <swashdev@pm.me> wrote this file. Feel free to do whatever you want
4 # with it so long as you don't hold me liable for any damages; there is no
5 # warranty. In exchange, if you ever find yourself thinking "I can't do
6 # this," or "I'll never be that good," I want you to stop, take a deep breath,
7 # and say "Yes I can." Then prove you can. Don't prove it to me; don't prove
8 # it to your friends and family; don't prove it to your boss; prove it to
9 # yourself. This software is already free; now free yourself.
12 # For more information about the rationale behind this licensing, see
13 # https://www.pavlick.net/fyl/
15 # Get the levels for Orange Guy's Quest from a text file
16 # This is how the game originally got its level data, but like an idiot I
17 # decided to hard-code it before saving the program and forgetting it forever.
18 # Now I have to basically rebuild this whole thing, but hopefully I now know
19 # a better way to do it...
21 # Returns a list of strings.
22 def get_levels( level_file_path
, DEBUG
= False ):
25 print "Load: Opening level file"
27 # Attempt to open the level file as a read-only file
28 level_file
= open( level_file_path
, 'r' )
30 # If we fail, we must abort
31 raise SystemExit, "Unable to initialize orange.py--level file " \
32 + level_file_path
+ " does not exist."
34 # Assuming success, we read the entire file in one fell swoop so that we
35 # don't have to read it more than once.
38 print "Load: Reading level file"
39 levels
= level_file
.read()
42 print "Load: Closing level file"
46 print "Work: Splitting level data"
47 # Separate the levels into a list; we are using "\n;\n" as a delimiter.
48 levels
= levels
.split( "\n;\n" )
51 print "Work: Checking level data"
52 # Before we pass this list up, we first have to check each level to make
53 # sure it has a player start and an exit.
54 level_has_player
= False
55 level_has_exit
= False
56 level_has_walls
= False
59 while index
>= 0 and index
< len( levels
):
60 if levels
[index
] == "":
62 print "Work: Empty level found--removing..."
67 while character
>= 0 and character
< len( levels
[index
] ):
68 ch
= levels
[index
][character
]
74 level_has_player
= True
78 level_has_walls
= True
79 #elif ch not in " abcdefghijklmnopqrstuvwxyzSM78946123!@#$^&*(":
80 # # Treat all invalid characters as a space
81 # levels[index].replace( ch, ' ' )
82 if level_has_player
and level_has_exit
and level_has_walls
:
85 character
= character
+ 1
86 if level_has_player
and level_has_exit
and level_has_walls
:
90 print "Work: Invalid level found--removing..."
92 level_has_player
= False
93 level_has_exit
= False
94 level_has_walls
= False
97 print "Work: Butchering levels"
98 # Before we can use this list of levels, the levels themselves need to be
99 # cut into parts separated by newlines.
101 while index
< len( levels
):
102 level
= levels
[index
]
103 levels
[index
] = level
.split( '\n' )
106 # What we should have now is a list of lists of strings. Ai ai ai!
107 # What was I thinking when I was going through college, man?
109 print "Work: Passing levels up to main program"
116 def test_level_file( level_file_path
):
119 level_file
= open( level_file_path
, 'r' )
121 return "No such level file: " + level_file_path
122 test_data
= level_file
.read()
126 def test_file_levels( level_file_path
):
129 level_file
= open( level_file_path
, 'r' )
131 return "No such level file: " + level_file_path
132 test_data
= level_file
.read().split( ';' )
136 def test_file_level_splits( level_file_path
):
139 level_file
= open( level_file_path
, 'r' )
141 return "No such level file: " + level_file_path
142 levels
= level_file
.read().split( ';' )
145 while index
< len( levels
):
146 level
= levels
[index
]
147 levels
[index
] = level
.split( '\n' )