-
Notifications
You must be signed in to change notification settings - Fork 0
/
orange_levels.py
141 lines (127 loc) · 4.72 KB
/
orange_levels.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# This is free and unencumbered software released into the public domain.
# Anyone is free to copy, modify, publish, use, compile, sell, or distribute
# this software, for any purpose, commercial or non-commercial, and by any
# means.
# For more information, please refer to UNLICENSE or to <http://unlicense.org>
# Get the levels for Orange Guy's Quest from a text file
# This is how the game originally got its level data, but like an idiot I
# decided to hard-code it before saving the program and forgetting it forever.
# Now I have to basically rebuild this whole thing, but hopefully I now know
# a better way to do it...
# Returns a list of strings.
def get_levels( level_file_path, DEBUG = False ):
level_file = None
if DEBUG:
print "Load: Opening level file"
try:
# Attempt to open the level file as a read-only file
level_file = open( level_file_path, 'r' )
except:
# If we fail, we must abort
raise SystemExit, "Unable to initialize orange.py--level file " \
+ level_file_path + " does not exist."
# Assuming success, we read the entire file in one fell swoop so that we
# don't have to read it more than once.
if DEBUG:
print "Load: Reading level file"
levels = level_file.read()
if DEBUG:
print "Load: Closing level file"
level_file.close()
if DEBUG:
print "Work: Splitting level data"
# Separate the levels into a list; we are using "\n;\n" as a delimiter.
levels = levels.split( "\n;\n" )
if DEBUG:
print "Work: Checking level data"
# Before we pass this list up, we first have to check each level to make
# sure it has a player start and an exit.
level_has_player = False
level_has_exit = False
level_has_walls = False
index = 0
while index >= 0 and index < len( levels ):
if levels[index] == "":
if DEBUG:
print "Work: Empty level found--removing..."
del levels[index]
continue
else:
character = 0
while character >= 0 and character < len( levels[index] ):
ch = levels[index][character]
if ch == '\0':
index = -1
character = -1
break
elif ch == 'P':
level_has_player = True
elif ch in ['E','A']:
level_has_exit = True
elif ch == 'W':
level_has_walls = True
#elif ch not in " abcdefghijklmnopqrstuvwxyzSM78946123!@#$^&*(":
# # Treat all invalid characters as a space
# levels[index].replace( ch, ' ' )
if level_has_player and level_has_exit and level_has_walls:
character = -1
break
character = character + 1
if level_has_player and level_has_exit and level_has_walls:
index = index + 1
else:
if DEBUG:
print "Work: Invalid level found--removing..."
del levels[index]
level_has_player = False
level_has_exit = False
level_has_walls = False
if DEBUG:
print "Work: Butchering levels"
# Before we can use this list of levels, the levels themselves need to be
# cut into parts separated by newlines.
index = 0
while index < len( levels ):
level = levels[index]
levels[index] = level.split( '\n' )
index = index + 1
# What we should have now is a list of lists of strings. Ai ai ai!
# What was I thinking when I was going through college, man?
if DEBUG:
print "Work: Passing levels up to main program"
return levels
##################
# Test functions #
##################
def test_level_file( level_file_path ):
level_file = None
try:
level_file = open( level_file_path, 'r' )
except:
return "No such level file: " + level_file_path
test_data = level_file.read()
level_file.close()
return test_data
def test_file_levels( level_file_path ):
level_file = None
try:
level_file = open( level_file_path, 'r' )
except:
return "No such level file: " + level_file_path
test_data = level_file.read().split( ';' )
level_file.close()
return test_data
def test_file_level_splits( level_file_path ):
level_file = None
try:
level_file = open( level_file_path, 'r' )
except:
return "No such level file: " + level_file_path
levels = level_file.read().split( ';' )
level_file.close()
index = 0
while index < len( levels ):
level = levels[index]
levels[index] = level.split( '\n' )
index = index + 1
return levels