From 0265c3a8d0786b6fbd8c6cf25baf79540b3bfbee Mon Sep 17 00:00:00 2001 From: quenousimporte Date: Wed, 16 Apr 2025 16:08:26 +0200 Subject: [PATCH] drop live generation and improve dfs --- w/w.c | 148 +++++++++++++++++++--------------------------------------- 1 file changed, 47 insertions(+), 101 deletions(-) diff --git a/w/w.c b/w/w.c index c2f6b04..381da34 100644 --- a/w/w.c +++ b/w/w.c @@ -91,7 +91,7 @@ Round* firstround = NULL; Round* currentround = NULL; char map[MAP_SIZE * MAP_SIZE]; -int ddl; + Character monsters[2] = { { "alien bleu", 2, 2, 1, 1 }, { "vilaine araignée", 5, 8, 2, 1 } @@ -133,6 +133,7 @@ void setatpos(int x, int y, char c) void drawmap() { for (int y = 0; y < MAP_SIZE; y++) { + printf("%02d ", y); for (int x = 0; x < MAP_SIZE; x++) { char toprint = charatpos(x,y); if (y == state.position.y && x == state.position.x) @@ -146,7 +147,11 @@ void drawmap() } printf("\n"); } - printf("ddl=%d\n", ddl); + printf(" "); + for (int x = 0; x < MAP_SIZE; x++) { + printf("%d", x % 10); + } + printf("\n\n"); } void drawpov3d() @@ -577,7 +582,7 @@ char gentile(int x, int y) { c = TILE_DOOR; } - else if (d < 5 && ddl > 1) + else if (d < 5) { c = TILE_WALL; } @@ -591,85 +596,12 @@ char gentile(int x, int y) return TILE_UNUSED; } -void updatemap() -{ - //ddl--; - int x = state.position.x; - int y = state.position.y; - - // north - char c = gentile(x, y - 1); - if (c == TILE_FREE) - { - ddl++; - gentile(x - 1, y - 2); - gentile(x + 1, y - 2); - } - else if (c == TILE_DOOR) - { - ddl++; - } - else if (c != TILE_UNUSED) - { - ddl--; - } - - // south - c = gentile(x, y + 1); - if (c == TILE_FREE) - { - ddl++; - gentile(x - 1, y + 2); - gentile(x + 1, y + 2); - } - else if (c == TILE_DOOR) - { - ddl++; - } - else if (c != TILE_UNUSED) - { - ddl--; - } - - // east - c = gentile(x + 1, y); - if (c == TILE_FREE) - { - ddl++; - gentile(x + 2, y - 1); - gentile(x + 2, y + 1); - } - else if (c == TILE_DOOR) - { - ddl++; - } - else if (c != TILE_UNUSED) - { - ddl--; - } - - // west - c = gentile(x - 1, y); - if (c == TILE_FREE) - { - ddl++; - gentile(x - 2, y - 1); - gentile(x - 2, y + 1); - } - else if (c == TILE_DOOR) - { - ddl++; - } - else if (c != TILE_UNUSED) - { - ddl--; - } - -} - void dfs(int x, int y) { - setatpos(x, y, TILE_FREE); + if (charatpos(x,y) != TILE_START) + { + setatpos(x, y, TILE_FREE); + } bool nok = false; bool eok = false; @@ -719,34 +651,50 @@ void dfs(int x, int y) void initmap() { - memset(&map, TILE_UNKNOWN, MAP_SIZE * MAP_SIZE); - //memset(&map, TILE_WALL, MAP_SIZE * MAP_SIZE); // dfs: put walls everywhere + //memset(&map, TILE_UNKNOWN, MAP_SIZE * MAP_SIZE); + memset(&map, TILE_UNKNOWN, MAP_SIZE * MAP_SIZE); // dfs: put walls everywhere - // dfs: add random room for (int x = 0; x < MAP_SIZE; x ++) - for (int y = 0; y < MAP_SIZE; y++) - if (dice() > 4) - // setatpos(x, y, TILE_FREE); - - for (int x = 0; x < MAP_SIZE; x += 2) { - for (int y = 14; y >=0; y -= 2) + for (int y = 0; y < MAP_SIZE; y++) { - setatpos(0, y - 1, TILE_WALL); - setatpos(14, y - 1, TILE_WALL); + if (x == 0 || y == 0 || x == MAP_SIZE - 1 || y == MAP_SIZE - 1) + { + // boundaries + setatpos(x, y, TILE_WALL); + } + else if (x % 2 && y % 2) + { + // free tiles (or unvisited for dfs) + setatpos(x, y, TILE_UNVISITED); + } + else if (!(x % 2 ) && !(y % 2)) + { + // unused tiles aka "poles" + //setatpos(x, y, TILE_UNUSED); - setatpos(x, y, TILE_WALL); // => instead of TILE_UNUSED, for readibility + //... or walls for readibility on map + setatpos(x, y, TILE_WALL); + } + else + { + // init all intersections with walls + setatpos(x, y, TILE_WALL); - setatpos(x + 1, y + 1, TILE_FREE); - //setatpos(x + 1, y + 1, TILE_UNVISITED); // dfs + //... and put a few free tiles here and there to create bigger rooms + if (dice() == 1) + { + setatpos(x, y, TILE_FREE); + } + } } - setatpos(x + 1, 0, TILE_WALL); - setatpos(x + 1, 14, TILE_WALL); } - // setatpos(1, 13, TILE_FREE); - //dfs(1,13); - ddl = 0; + // starting point + setatpos(1, 13, TILE_START); + + // create map + dfs(1,13); } void update(char* command) @@ -761,7 +709,6 @@ void update(char* command) int x = state.position.x; int y = state.position.y; forward(); - updatemap(); if ( (x != state.position.x || y != state.position.y) && dice() > 4) { // gotomonster(); @@ -809,7 +756,6 @@ void update(char* command) state.position.orientation = NORTH; initmap(); - updatemap(); updatepov(); } else if (c == CMD_GOTOMARKET)