forked from Darkest-Teddy/DynamicMarioBros
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_level.py
More file actions
327 lines (239 loc) · 8.78 KB
/
Copy pathgenerate_level.py
File metadata and controls
327 lines (239 loc) · 8.78 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import sys
import os
import random
import re
print("🎮 Mario Level Generator - Overwriting World Files")
print("="*60)
# Check dependencies
try:
import google.generativeai as genai
except ImportError:
print("❌ Run: pip install google-generativeai")
sys.exit(1)
# Config
API_KEY = "AIzaSyAxlfwHUMQ5cxlfW2EiLpznh1QBf4BgsfY"
MAPS_DIR = "Maps"
MAPS_DIR1="maps1"
if not os.path.exists(MAPS_DIR):
print(f"❌ {MAPS_DIR} folder not found")
sys.exit(1)
# Get world numbers to generate (e.g., "12" "13" "14")
if len(sys.argv) > 1:
# User specified which worlds (e.g., python generate_level.py 12 13 14)
world_numbers = sys.argv[1:]
else:
# Default: regenerate World12-14 (first level set)
world_numbers = ["12", "13", "14"]
print(f"Will overwrite: {', '.join(['World' + w + '.js' for w in world_numbers])}\n")
# Setup Gemini
try:
genai.configure(api_key=API_KEY)
model = genai.GenerativeModel('gemini-2.0-flash-exp')
print("✓ Gemini 2.0 Flash configured\n")
except Exception as e:
print(f"❌ Gemini setup failed: {e}")
sys.exit(1)
# Load MULTIPLE examples for variety
print("Loading example maps...")
examples = {}
# Map actual files from your Maps folder to themes
example_files = {
'Overworld': ['World13.js', 'World53.js', 'World23.js', 'World73.js'],
'Castle': ['World24.js', 'World54.js', 'World64.js', 'World74.js', 'World34.js'],
'Underwater': ['World22.js', 'World72.js'],
'Overworld Night': ['World61.js', 'World33.js', 'World32.js', 'World31.js']
}
# Load one example for each theme
for theme, filenames in example_files.items():
for filename in filenames:
filepath = os.path.join(MAPS_DIR1, filename)
if os.path.exists(filepath):
with open(filepath, 'r') as f:
examples[theme] = f.read()
print(f"✓ Loaded {filename} ({theme})")
break
if not examples:
print("❌ No example files found")
sys.exit(1)
print()
# Determine themes based on world numbers
def get_theme_for_world(world_num):
"""Determine theme based on world number pattern"""
last_digit = int(world_num) % 10
if last_digit == 1:
return random.choice(['Overworld', 'Overworld Night'])
elif last_digit == 2:
return random.choice(['Underwater', 'Overworld'])
elif last_digit == 3:
return random.choice(['Overworld', 'Overworld Night'])
elif last_digit == 4:
return 'Castle'
else:
return random.choice(list(examples.keys()))
def generate_level(attempt, theme):
"""Generate one level of specific theme"""
# Use the example for this theme
example_code = examples.get(theme, list(examples.values())[0])
theme_name = theme
prompt = f"""Generate a NEW Super Mario {theme_name} level based on this working example:
```javascript
{example_code}
```
CREATE A SIMILAR {theme_name.upper()} LEVEL with these changes:
1. Change ALL X coordinates by +/- {random.randint(50, 200)} pixels
2. Use different enemy combinations appropriate for {theme_name}
3. Change structure heights and block positions
4. Add {random.randint(5, 15)} more enemies
5. Add {random.randint(3, 8)} more coin groups
6. Make level {random.randint(1200, 1800)} pixels long
THEME-SPECIFIC REQUIREMENTS:
- Overworld: Use pushPreTree, Goomba, Koopa, pipes
- Castle: Use startCastleInside(), Podoboo, Beetle, makeCeilingCastle()
- Underwater: Use goUnderWater(), Blooper, CheepCheep, Coral
- Sky: Use Platform with movement, Koopa (flying)
KEEP THIS EXACT STRUCTURE:
- Start: map.time, map.locs, map.areas
- Use: new Area("{theme_name}", function() {{
- First floor: pushPreFloor(0, 0, X) where X >= 60
- End: endCastleOutside(...) or endCastleInside(...)
- Use same function names as example
Output ONLY JavaScript code, NO explanations or markdown:"""
try:
response = model.generate_content(
prompt,
generation_config=genai.types.GenerationConfig(
temperature=0.9,
top_p=0.95,
)
)
return response.text
except Exception as e:
print(f" ❌ API error: {e}")
return None
def clean_code(code):
"""Clean up generated code and GUARANTEE spawn floor coverage"""
if not code:
return None
# Remove markdown
code = re.sub(r'```javascript\s*', '', code)
code = re.sub(r'```\s*', '', code)
code = code.strip()
# Must start with map
if not code.startswith('map.'):
lines = code.split('\n')
for i, line in enumerate(lines):
if 'map.' in line:
code = '\n'.join(lines[i:])
break
# 🚨 SIMPLE BRUTE FORCE FIX: Just ensure first pushPreFloor is correct
lines = code.split('\n')
# Find the VERY FIRST pushPreFloor command
first_floor_line_index = -1
for i, line in enumerate(lines):
if 'pushPreFloor' in line:
first_floor_line_index = i
break
if first_floor_line_index >= 0:
line = lines[first_floor_line_index]
match = re.search(r'pushPreFloor\((\d+),\s*(\d+),\s*(\d+)\)', line)
if match:
x, y, w = int(match.group(1)), int(match.group(2)), int(match.group(3))
# If first floor is wrong, FORCE fix it
if x != 0 or y != 0 or w < 200:
print(f" 🔧 Forcing spawn floor fix: ({x},{y},{w}) → (0,0,200)")
lines[first_floor_line_index] = line.replace(
f'pushPreFloor({x}, {y}, {w})',
'pushPreFloor(0, 0, 200)'
)
else:
# NO FLOOR AT ALL - insert one right after setLocationGeneration
print(" 🔧 NO FLOOR FOUND - inserting spawn floor")
for i, line in enumerate(lines):
if 'setLocationGeneration' in line:
lines.insert(i + 1, " ")
lines.insert(i + 2, " pushPreFloor(0, 0, 200);")
lines.insert(i + 3, " ")
break
return '\n'.join(lines)
def validate(code):
"""Simple validation focusing on spawn floor"""
if not code:
return False, "Empty code"
if 'map.locs' not in code:
return False, "Missing map.locs"
if 'map.areas' not in code:
return False, "Missing map.areas"
if 'endCastle' not in code:
return False, "Missing ending"
# Find FIRST floor
floor_match = re.search(r'pushPreFloor\((\d+),\s*(\d+),\s*(\d+)\)', code)
if not floor_match:
return False, "No floor found"
x = int(floor_match.group(1))
y = int(floor_match.group(2))
w = int(floor_match.group(3))
# STRICT CHECK: First floor must be at (0,0) with width >= 150
if x != 0:
return False, f"First floor at x={x}, must be 0"
if y != 0:
return False, f"First floor at y={y}, must be 0"
if w < 150:
return False, f"First floor width={w}, need 150+"
# Check for some content
enemies = len(re.findall(r'Goomba|Koopa|Beetle', code))
if enemies < 2:
return False, f"Only {enemies} enemies"
return True, f"✓ Spawn at (0,0,{w}), {enemies} enemies"
def save_level(code, world_number):
"""Save to specific World file"""
filename = f"World{world_number}.js"
filepath = os.path.join(MAPS_DIR, filename)
# Check if file exists
exists = os.path.exists(filepath)
with open(filepath, 'w') as f:
f.write(code)
action = "OVERWRITTEN" if exists else "CREATED"
return filename, action
# Generate levels
print(f"{'='*60}\n")
success_count = 0
overwrite_count = 0
create_count = 0
for world_num in world_numbers:
# Determine theme for this world number
theme = get_theme_for_world(world_num)
print(f"World{world_num}.js ({theme}):")
level_saved = False
for attempt in range(1, 4):
print(f" Attempt {attempt}...", end=" ", flush=True)
# Generate with specific theme
code = generate_level(attempt, theme)
if not code:
print("failed")
continue
# Clean - FORCE correct spawn floor
code = clean_code(code)
# Validate - STRICT spawn check
is_valid, message = validate(code)
if not is_valid:
print(f"❌ {message}")
continue
# Save - OVERWRITE specific World file
filename, action = save_level(code, world_num)
print(f"✅ {filename} [{action}] ({message})")
success_count += 1
if action == "OVERWRITTEN":
overwrite_count += 1
else:
create_count += 1
level_saved = True
break
if not level_saved:
print(f" ❌ Failed after 3 attempts")
print()
print(f"{'='*60}")
print(f"RESULTS: {success_count}/{len(world_numbers)} successful")
print(f" 📝 Overwritten: {overwrite_count} files")
print(f" ✨ Created new: {create_count} files")
print(f"Saved in: {MAPS_DIR}/World*.js")
print(f"{'='*60}")