Grow maze size with level (7x7 -> 17x17, +2 per level)

The maze side length is now a runtime global 'map_size' (MAP_SIZE=7 at
level 1, +2 per level, capped at MAX_MAP_SIZE=17), while the maze array
is allocated with the MAX_MAP_SIZE bound. All modules (maze DFS, fog
rendering, player/enemy bounds, hatch and enemy-spawn placement) now use
map_size at runtime.

- globals: maze[MAX_MAP_SIZE][MAX_MAP_SIZE], map_size global, MAX_MAP_SIZE=17.
- maze.c: DFS on map_size; stack/valid arrays made static (WRAM) and sized
  for the max maze to avoid hardware-stack overflow; hatch min distance
  scales with map_size/2.
- engine.c: sets map_size from level before generate_maze; enemy spawn min
  distance scales with map_size/2.
- render.c: flush the full 32x32 background map (the old rows 2-17
  optimization broke for large mazes where the 5x5 fog window wraps outside
  that range); dynamic bounds; signed/unsigned casts.
- player_logic/enemy_logic: dynamic map_size bounds.

Verified via PyBoy: sizes 7,9,11,13,15,17,17 across levels 1-7, each with
a hatch; rendering and movement intact at 17x17 and at 7x7.
This commit is contained in:
2026-06-24 12:26:48 +02:00
parent 509f162380
commit 5e8709ade0
11 changed files with 112 additions and 90 deletions
+2 -2
View File
@@ -65,14 +65,14 @@ void update_enemy_logic(void) {
int16_t min_dist_sq = (int16_t)dx * dx + (int16_t)dy * dy;
// Controllo 1: Direzione Down-Right (+1 X)
if (enemy_lx + 1 < MAP_SIZE && maze[enemy_ly][enemy_lx + 1] != 0) {
if (enemy_lx + 1 < map_size && maze[enemy_ly][enemy_lx + 1] != 0) {
int8_t ndx = (int8_t)player_lx - (int8_t)(enemy_lx + 1);
int8_t ndy = (int8_t)player_ly - (int8_t)enemy_ly;
int16_t d_sq = (int16_t)ndx * ndx + (int16_t)ndy * ndy;
if (d_sq < min_dist_sq) { min_dist_sq = d_sq; best_nx = enemy_lx + 1; best_ny = enemy_ly; }
}
// Controllo 2: Direzione Down-Left (+1 Y)
if (enemy_ly + 1 < MAP_SIZE && maze[enemy_ly + 1][enemy_lx] != 0) {
if (enemy_ly + 1 < map_size && maze[enemy_ly + 1][enemy_lx] != 0) {
int8_t ndx = (int8_t)player_lx - (int8_t)enemy_lx;
int8_t ndy = (int8_t)player_ly - (int8_t)(enemy_ly + 1);
int16_t d_sq = (int16_t)ndx * ndx + (int16_t)ndy * ndy;
+16 -7
View File
@@ -81,7 +81,12 @@ void engine_init(void) {
// Usa il registro divisore hardware (DIV_REG) per seedare l'RNG. Garantisce labirinti diversi ogni volta.
initrand(DIV_REG);
// Dimensione del labirinto crescente col livello: +2 tile per livello a partire da 7,
// capped a MAX_MAP_SIZE (17). Sempre dispari (per il pattern stanza/muro del DFS).
map_size = MAP_SIZE + 2 * (level - 1);
if (map_size > MAX_MAP_SIZE) map_size = MAX_MAP_SIZE;
// DELEGA LA GENERAZIONE: Chiede al modulo maze.c di creare l'array della mappa
generate_maze();
@@ -106,8 +111,8 @@ void engine_init(void) {
// Fallback di sicurezza: Cerca la prima casella libera se 1,1 è occupato (teoricamente impossibile col DFS)
if (maze[player_ly][player_lx] == 0) {
uint8_t found = 0;
for (uint8_t y = 1; y < MAP_SIZE - 1; y++) {
for (uint8_t x = 1; x < MAP_SIZE - 1; x++) {
for (uint8_t y = 1; y < map_size - 1; y++) {
for (uint8_t x = 1; x < map_size - 1; x++) {
if (maze[y][x] == 1) {
player_lx = x;
player_ly = y;
@@ -120,17 +125,21 @@ void engine_init(void) {
}
// SPAWN DEL NEMICO
// Cerca randomicamente un punto lontano almeno 3 celle dal giocatore
// Cerca randomicamente un punto lontano almeno map_size/2 celle dal giocatore
// (scala con la dimensione del labirinto: in una mappa grande il fantasma parte
// lontano e non si attiva finché non entra nel cono visivo 5x5 del fog of war).
uint8_t enemy_min_dist = map_size / 2;
if (enemy_min_dist < 3) enemy_min_dist = 3;
while (1) {
uint8_t rx = rand() % MAP_SIZE;
uint8_t ry = rand() % MAP_SIZE;
uint8_t rx = rand() % map_size;
uint8_t ry = rand() % map_size;
if (maze[ry][rx] == 1) {
int8_t dx = (int8_t)rx - (int8_t)player_lx;
int8_t dy = (int8_t)ry - (int8_t)player_ly;
if (dx < 0) dx = -dx;
if (dy < 0) dy = -dy;
int8_t dist = (dx > dy) ? dx : dy;
if (dist >= 3) {
if (dist >= (int8_t)enemy_min_dist) {
enemy_lx = rx;
enemy_ly = ry;
break;
+2 -1
View File
@@ -4,7 +4,8 @@
volatile uint8_t game_over = 0;
volatile uint8_t game_over_timer = 0;
uint8_t maze[MAP_SIZE][MAP_SIZE];
uint8_t map_size = MAP_SIZE;
uint8_t maze[MAX_MAP_SIZE][MAX_MAP_SIZE];
uint8_t map_buffer[32 * 32];
uint8_t player_lx = 1;
+7 -7
View File
@@ -22,9 +22,11 @@ extern volatile uint8_t game_over;
extern volatile uint8_t game_over_timer;
// --- Map Data ---
#define MAP_SIZE 7
// The generated maze: 0 = Wall, 1 = Floor, 2 = Victory Tile
extern uint8_t maze[MAP_SIZE][MAP_SIZE];
#define MAP_SIZE 7 // initial maze size (level 1); must be odd
#define MAX_MAP_SIZE 17 // hard cap for maze growth (odd); used as the array bound
// The generated maze: 0 = Wall, 1 = Floor, 2 = Victory Tile (hatch)
extern uint8_t map_size; // current maze side length (grows with level, MAP_SIZE..MAX_MAP_SIZE)
extern uint8_t maze[MAX_MAP_SIZE][MAX_MAP_SIZE];
// --- Camera & Rendering ---
// Map buffer used to draw the isometric tiles into the Game Boy Background map
@@ -36,9 +38,7 @@ extern int8_t scroll_y;
extern uint8_t stairs_lx;
extern uint8_t stairs_ly;
// --- Mappa e Entità ---
// The generated maze: 0 = Wall, 1 = Floor, 2 = Victory Tile
extern uint8_t maze[MAP_SIZE][MAP_SIZE];
// --- Mappa e Entita ---
extern uint8_t player_lx;
extern uint8_t player_ly;
extern uint8_t player_dir; // 0=DR, 1=DL, 2=UL, 3=UR
@@ -74,4 +74,4 @@ extern int16_t enemy_start_px, enemy_start_py;
extern int16_t enemy_target_px, enemy_target_py;
extern uint8_t enemy_cooldown;
#endif
#endif
+60 -58
View File
@@ -3,49 +3,56 @@
#include <string.h>
#include <rand.h>
// Capacita' massima degli array di appoggio (in WRAM, statici per non saturare
// lo stack hardware del LR35902). Le stanze del DFS sono le celle dispari:
// al massimo (MAX_MAP_SIZE/2)^2 = 8*8 = 64 per un labirinto 17x17.
#define MAX_ROOMS ((MAX_MAP_SIZE / 2) * (MAX_MAP_SIZE / 2))
#define MAX_CELLS (MAX_MAP_SIZE * MAX_MAP_SIZE)
// Array di appoggio statici (in WRAM, non sullo stack hardware).
static uint8_t stack_x[MAX_ROOMS];
static uint8_t stack_y[MAX_ROOMS];
static uint8_t valid_x[MAX_CELLS];
static uint8_t valid_y[MAX_CELLS];
/**
* Generates a randomized maze using a Depth-First Search (DFS) algorithm with backtracking.
*
*
* Sviluppo & Scelte Architetturali:
* 1. Perché DFS? Questo algoritmo è perfetto per labirinti garantendo che ogni cella sia
* raggiungibile partendo dall'inizio (nessuna isola irraggiungibile).
* 2. Il labirinto è una griglia MAP_SIZE x MAP_SIZE in cui le celle dispari (es. 1,1 o 3,3)
* sono le stanze, mentre le celle pari sono i muri divisori. Il DFS scava i muri saltando
* di 2 in 2 e abbattendo il muro in mezzo.
* 3. Abbiamo aggiunto una fase post-DFS che rimuove casualmente qualche muro rimasto (con
* il 15% di probabilità). Questo spezza la struttura "perfetta" del labirinto creando
* dei percorsi ciclici (loop). I loop sono vitali in un gioco con un nemico che ti insegue,
* permettendo al giocatore di aggirare il nemico scappando in cerchio.
* 4. La casella della VITTORIA viene piazzata alla fine. Usiamo la "Distanza di Manhattan"
* (somma della distanza X e Y) dalla partenza (1,1) per assicurarci che il traguardo
* sia la casella più lontana in assoluto.
* 1. Perché DFS? Garantisce che ogni cella sia raggiungibile dalla partenza (nessuna isola).
* 2. Il labirinto è una griglia map_size x map_size (cresce col livello, da MAP_SIZE a
* MAX_MAP_SIZE) in cui le celle dispari (1,1 o 3,3) sono le stanze e le pari sono i muri
* divisori. Il DFS scava i muri saltando di 2 in 2 e abbattendo il muro in mezzo.
* 3. Fase post-DFS: riapre casualmente il 15% dei muri per creare loop (vitali per
* aggirare l'inseguitore).
* 4. La BOTOLA (traguardo) viene piazzata su una cella a sufficiente distanza (Chebyshev
* >= map_size/2) dalla partenza (1,1), in modo che sia sempre lontana ma su qualunque tile.
*/
void generate_maze(void) {
// 1. Inizializziamo l'intera mappa a 0 (che rappresenta un Muro / Spazio Vuoto)
uint8_t sz = map_size;
// 1. Inizializziamo l'intera mappa a 0 (Muro / Spazio Vuoto)
memset(maze, 0, sizeof(maze));
// Stack array per il backtracking. La griglia 7x7 ha al massimo 49 celle dispari (stanze).
uint8_t stack_x[49];
uint8_t stack_y[49];
uint8_t stack_ptr = 0;
// Partiamo sempre dalle coordinate (1, 1) in alto a sinistra
uint8_t cx = 1;
uint8_t cy = 1;
maze[cy][cx] = 1; // 1 = Pavimento calpestabile
while (1) {
// Cerchiamo i vicini non visitati a distanza 2 (saltando il muro divisorio)
uint8_t nx[4];
uint8_t ny[4];
uint8_t count = 0;
// Su
if (cy >= 3 && maze[cy - 2][cx] == 0) {
nx[count] = cx; ny[count] = cy - 2; count++;
}
// Giù
if (cy <= MAP_SIZE - 4 && maze[cy + 2][cx] == 0) {
if (cy <= sz - 4 && maze[cy + 2][cx] == 0) {
nx[count] = cx; ny[count] = cy + 2; count++;
}
// Sinistra
@@ -53,49 +60,48 @@ void generate_maze(void) {
nx[count] = cx - 2; ny[count] = cy; count++;
}
// Destra
if (cx <= MAP_SIZE - 4 && maze[cy][cx + 2] == 0) {
if (cx <= sz - 4 && maze[cy][cx + 2] == 0) {
nx[count] = cx + 2; ny[count] = cy; count++;
}
// Se ci sono vicini validi
if (count > 0) {
// Scegli una direzione a caso tra quelle disponibili
uint8_t dir = rand() % count;
// Salviamo la cella corrente nello stack per poterci tornare
stack_x[stack_ptr] = cx;
stack_y[stack_ptr] = cy;
stack_ptr++;
// Abbattiamo il muro nel mezzo (calcolato facendo la media aritmetica delle coordinate)
// Abbattiamo il muro nel mezzo (media aritmetica delle coordinate)
maze[(cy + ny[dir]) / 2][(cx + nx[dir]) / 2] = 1;
// Spostiamo la posizione corrente sul vicino scelto
cx = nx[dir];
cy = ny[dir];
maze[cy][cx] = 1;
}
// Se non ci sono vicini, torna indietro prendendo l'ultima cella salvata dallo stack
}
// Se non ci sono vicini, torna indietro prendendo l'ultima cella dallo stack
else if (stack_ptr > 0) {
stack_ptr--;
cx = stack_x[stack_ptr];
cy = stack_y[stack_ptr];
}
}
// Se lo stack è vuoto, abbiamo visitato tutte le celle: il labirinto è completo!
else {
break;
}
}
// FASE 2: Riapriamo casualmente dei muri per creare percorsi alternativi e loop (15% chance).
// Questo è cruciale per la giocabilità: permette al giocatore di aggirare l'inseguitore!
for (uint8_t y = 1; y < MAP_SIZE - 1; y++) {
for (uint8_t x = 1; x < MAP_SIZE - 1; x++) {
// FASE 2: Riapriamo casualmente dei muri per creare percorsi alternativi e loop (15%).
// Cruciale per la giocabilità: permette al giocatore di aggirare l'inseguitore!
for (uint8_t y = 1; y < sz - 1; y++) {
for (uint8_t x = 1; x < sz - 1; x++) {
if (maze[y][x] == 0) {
// Se la casella è un muro che connette orizzontalmente o verticalmente due corridoi
// Muro che connette orizzontalmente o verticalmente due corridoi
if ((maze[y][x - 1] == 1 && maze[y][x + 1] == 1) ||
(maze[y - 1][x] == 1 && maze[y + 1][x] == 1)) {
// Genera numero casuale tra 0 e 99. Se < 15, trasforma in pavimento.
if ((rand() % 100) < 15) {
maze[y][x] = 1;
}
@@ -103,27 +109,24 @@ void generate_maze(void) {
}
}
}
// FASE 3: Posizionamento della Botola (Traguardo).
// La botola viene piazzata su una cella calpestabile a "sufficiente distanza"
// dalla casella di partenza del giocatore (1,1), cosi' che il traguardo sia
// sempre lontano ma possa trovarsi su una qualunque tile del labirinto,
// non piu' vincolato al bordo sud. Usiamo la distanza di Chebyshev
// (consistente con il resto dell'engine: fog of war, attivazione nemico).
#define MIN_GOAL_DIST 3
uint8_t valid_x[MAP_SIZE * MAP_SIZE];
uint8_t valid_y[MAP_SIZE * MAP_SIZE];
// La botola viene piazzata su una cella calpestabile a "sufficiente distanza" (Chebyshev
// >= map_size/2) dalla partenza (1,1): sempre lontana, ma su una qualunque tile.
// La soglia scala con la dimensione del labirinto (3 per 7x7, 8 per 17x17).
uint8_t min_goal = sz / 2;
if (min_goal < 3) min_goal = 3;
uint8_t num_valid = 0;
for (uint8_t y = 1; y < MAP_SIZE - 1; y++) {
for (uint8_t x = 1; x < MAP_SIZE - 1; x++) {
for (uint8_t y = 1; y < sz - 1; y++) {
for (uint8_t x = 1; x < sz - 1; x++) {
if (maze[y][x] == 1) {
int8_t dx = (int8_t)x - 1;
int8_t dy = (int8_t)y - 1;
if (dx < 0) dx = -dx;
if (dy < 0) dy = -dy;
int8_t dist = (dx > dy) ? dx : dy;
if (dist >= MIN_GOAL_DIST) {
if (dist >= (int8_t)min_goal) {
valid_x[num_valid] = x;
valid_y[num_valid] = y;
num_valid++;
@@ -140,14 +143,13 @@ void generate_maze(void) {
stairs_lx = target_x;
stairs_ly = target_y;
} else {
// Fallback estremo: nessuna cella a sufficienza distante (teoricamente
// impossibile in un perfect maze 7x7 con partenza 1,1, ma difensivo).
// Sceglie la cella calpestabile piu' lontana in assoluto da (1,1).
uint8_t best_x = MAP_SIZE - 2;
uint8_t best_y = MAP_SIZE - 2;
// Fallback estremo: nessuna cella a sufficienza distante. Sceglie la cella
// calpestabile piu' lontana in assoluto da (1,1).
uint8_t best_x = sz - 2;
uint8_t best_y = sz - 2;
int8_t best_dist = -1;
for (uint8_t y = 1; y < MAP_SIZE - 1; y++) {
for (uint8_t x = 1; x < MAP_SIZE - 1; x++) {
for (uint8_t y = 1; y < sz - 1; y++) {
for (uint8_t x = 1; x < sz - 1; x++) {
if (maze[y][x] == 1) {
int8_t dx = (int8_t)x - 1;
int8_t dy = (int8_t)y - 1;
@@ -166,4 +168,4 @@ void generate_maze(void) {
stairs_lx = best_x;
stairs_ly = best_y;
}
}
}
+2 -2
View File
@@ -144,7 +144,7 @@ void update_player_movement(uint8_t keys, uint8_t prev_keys) {
int8_t land_ly = player_ly + move_ly * 2;
// Verifica che il salto cada all'interno del labirinto
if (land_lx >= 0 && land_lx < MAP_SIZE && land_ly >= 0 && land_ly < MAP_SIZE) {
if (land_lx >= 0 && (uint8_t)land_lx < map_size && land_ly >= 0 && (uint8_t)land_ly < map_size) {
// Condizioni del salto:
// a) La cella intermedia DEVE essere un muro (maze == 0)
// b) La cella di arrivo DEVE essere un pavimento o vittoria (maze == 1 o 2)
@@ -185,7 +185,7 @@ void update_player_movement(uint8_t keys, uint8_t prev_keys) {
int8_t new_lx = player_lx + move_lx;
int8_t new_ly = player_ly + move_ly;
if (new_lx >= 0 && new_lx < MAP_SIZE && new_ly >= 0 && new_ly < MAP_SIZE) {
if (new_lx >= 0 && (uint8_t)new_lx < map_size && new_ly >= 0 && (uint8_t)new_ly < map_size) {
// Se la destinazione è pavimento normale o casella di vittoria
if (maze[new_ly][new_lx] == 1 || maze[new_ly][new_lx] == 2) {
is_moving = 1;
+10 -7
View File
@@ -11,7 +11,7 @@
#include "maze.h"
uint8_t get_tile_state(int8_t cx, int8_t cy, int8_t lx, int8_t ly) {
if (lx < 0 || lx >= MAP_SIZE || ly < 0 || ly >= MAP_SIZE) return 0;
if (lx < 0 || (uint8_t)lx >= map_size || ly < 0 || (uint8_t)ly >= map_size) return 0;
if (maze[ly][lx] == 0) return 0;
int8_t dx = lx - cx;
int8_t dy = ly - cy;
@@ -150,12 +150,12 @@ void draw_map(uint8_t center_x, uint8_t center_y) {
int8_t start_x = center_x - 2;
if (start_x < 0) start_x = 0;
int8_t end_x = center_x + 2;
if (end_x >= MAP_SIZE) end_x = MAP_SIZE - 1;
if ((uint8_t)end_x >= map_size) end_x = map_size - 1;
int8_t start_y = center_y - 2;
if (start_y < 0) start_y = 0;
int8_t end_y = center_y + 2;
if (end_y >= MAP_SIZE) end_y = MAP_SIZE - 1;
if ((uint8_t)end_y >= map_size) end_y = map_size - 1;
// Processiamo la mappa in DUE passate per risolvere il problema dell'overlapping isometrico.
// L'engine disegna da Nord a Sud. I tile a Sud sovrascrivono la metà inferiore dei tile a Nord.
@@ -248,10 +248,13 @@ void draw_map(uint8_t center_x, uint8_t center_y) {
update_stamina_display();
// Ottimizzazione hardware critica: trasferire tutta la mappa (1024 bytes) via set_bkg_tiles
// causa lag e sfarfallii (VRAM access). Trasferiamo solo le righe da 2 a 17 (16 righe totali),
// che è la porzione visibile del Game Boy (160x144 px) in cui si svolge l'azione.
set_bkg_tiles(0, 2, 32, 16, &map_buffer[2 * 32]);
// Trasferiamo l'intera mappa 32x32 (1024 byte) al Background hardware.
// Con labirinti grandi (map_size > 7) la finestra fog-of-war 5x5, proiettata in
// coordinate isometriche assolute, puo' cadere in righe OLTRE il vecchio range
// 2-17 (a causa del wrapping & 31), quindi non basta piu' flussare solo 16 righe.
// draw_map e' chiamato solo ai passi del movimento (non ogni frame), quindi il
// costo del flush completo e' sostenibile.
set_bkg_tiles(0, 0, 32, 32, map_buffer);
}
/**