Rename license, readme, todo files
[orange-guys-quest.git] / orange_levels.py
blob61387f28254f56002f3a13bc479cb6d282f39a11
1 # Copyright (c) 2011-2020 Philip Pavlick
2 #
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.
10 # - Philip Pavlick
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 ):
23 level_file = None
24 if DEBUG:
25 print "Load: Opening level file"
26 try:
27 # Attempt to open the level file as a read-only file
28 level_file = open( level_file_path, 'r' )
29 except:
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.
37 if DEBUG:
38 print "Load: Reading level file"
39 levels = level_file.read()
41 if DEBUG:
42 print "Load: Closing level file"
43 level_file.close()
45 if DEBUG:
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" )
50 if DEBUG:
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
58 index = 0
59 while index >= 0 and index < len( levels ):
60 if levels[index] == "":
61 if DEBUG:
62 print "Work: Empty level found--removing..."
63 del levels[index]
64 continue
65 else:
66 character = 0
67 while character >= 0 and character < len( levels[index] ):
68 ch = levels[index][character]
69 if ch == '\0':
70 index = -1
71 character = -1
72 break
73 elif ch == 'P':
74 level_has_player = True
75 elif ch in ['E','A']:
76 level_has_exit = True
77 elif ch == 'W':
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:
83 character = -1
84 break
85 character = character + 1
86 if level_has_player and level_has_exit and level_has_walls:
87 index = index + 1
88 else:
89 if DEBUG:
90 print "Work: Invalid level found--removing..."
91 del levels[index]
92 level_has_player = False
93 level_has_exit = False
94 level_has_walls = False
96 if DEBUG:
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.
100 index = 0
101 while index < len( levels ):
102 level = levels[index]
103 levels[index] = level.split( '\n' )
104 index = index + 1
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?
108 if DEBUG:
109 print "Work: Passing levels up to main program"
110 return levels
112 ##################
113 # Test functions #
114 ##################
116 def test_level_file( level_file_path ):
117 level_file = None
118 try:
119 level_file = open( level_file_path, 'r' )
120 except:
121 return "No such level file: " + level_file_path
122 test_data = level_file.read()
123 level_file.close()
124 return test_data
126 def test_file_levels( level_file_path ):
127 level_file = None
128 try:
129 level_file = open( level_file_path, 'r' )
130 except:
131 return "No such level file: " + level_file_path
132 test_data = level_file.read().split( ';' )
133 level_file.close()
134 return test_data
136 def test_file_level_splits( level_file_path ):
137 level_file = None
138 try:
139 level_file = open( level_file_path, 'r' )
140 except:
141 return "No such level file: " + level_file_path
142 levels = level_file.read().split( ';' )
143 level_file.close()
144 index = 0
145 while index < len( levels ):
146 level = levels[index]
147 levels[index] = level.split( '\n' )
148 index = index + 1
149 return levels