From f129882f5024d4b41af73ef3029049da5fd3b3e3 Mon Sep 17 00:00:00 2001 From: quenousimporte Date: Mon, 21 Apr 2025 22:26:58 +0200 Subject: [PATCH] some map rationalization --- w/w.c | 226 ++++++++++++++++++---------------------------------------- 1 file changed, 69 insertions(+), 157 deletions(-) diff --git a/w/w.c b/w/w.c index 2a3e753..67f544e 100644 --- a/w/w.c +++ b/w/w.c @@ -8,7 +8,7 @@ #define NAME_LEN 64 #define POV_3D_SIZE 10 #define POV_SIZE 3 -#define INVENTORY_SIZE 2 +#define INVENTORY_SIZE 1 #define MAP_SIZE 15 #define CMD_NULL "-" @@ -37,16 +37,16 @@ #define WEST 2 #define SOUTH 3 -#define TILE_WALL 'w' -#define TILE_FREE ' ' -#define TILE_DOOR 'd' -#define TILE_START 's' -#define TILE_UNKNOWN '?' -#define TILE_UNUSED '.' -#define TILE_UNVISITED 'x' +#define TILE_WALL 0 +#define TILE_FREE 1 +#define TILE_DOOR 2 +#define TILE_START 3 +#define TILE_UNUSED 4 +#define TILE_UNVISITED 5 +#define TILE_VISITED 6 +#define TILE_INIT 7 #define ITEM_POTION '0' -#define ITEM_TRUC '1' typedef struct { char id; @@ -114,12 +114,23 @@ int dices(int count) return result; } +char tileonmap(char tile) +{ + switch (tile) + { + case TILE_FREE: return ' '; // free passageway + case TILE_DOOR: return '+'; // door + case TILE_START: return 's'; // start point + case TILE_UNVISITED: return ' '; // not yet visited + case TILE_WALL: return '#'; // wall + case TILE_UNUSED: return '#'; // "poles" + case TILE_VISITED: return ' '; // already visited + case TILE_INIT: return '.'; // not yet initialized by dfs + } +} + char charatpos(int x, int y) { - if (x < 0 || y < 0 || x >= MAP_SIZE || y >= MAP_SIZE) - { - return ' '; - } return map[y * MAP_SIZE + x]; } @@ -137,7 +148,7 @@ 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); + char toprint = tileonmap(charatpos(x,y)); if (y == state.position.y && x == state.position.x) { if (state.position.orientation == NORTH) toprint = '^'; @@ -309,7 +320,7 @@ void update3dpov() } } - const char doorchar = '#'; + const char doorchar = '+'; if (pov[1][1] == TILE_DOOR) { // front door @@ -365,7 +376,10 @@ void updatepov() newY -= rtoloffset; break; } - pov[2 - frontoffset][1 + rtoloffset] = charatpos(newX, newY); + if (newX >= 0 && newY >= 0 && newX < MAP_SIZE && newY < MAP_SIZE) + { + pov[2 - frontoffset][1 + rtoloffset] = charatpos(newX, newY); + } } } update3dpov(); @@ -390,6 +404,7 @@ void forward() state.position.x -= 2; break; } + setatpos(state.position.x, state.position.y, TILE_VISITED); } } @@ -494,9 +509,7 @@ void createhero(char* name) state.hero.def = 1; Item potion = { ITEM_POTION, "potion", 20, 0 }; - Item truc = { ITEM_TRUC, "truc", 2000, 0 }; state.inventory[0] = potion; - state.inventory[1] = truc; } void createround(char action, int herodmg, int monsterdmg) @@ -579,112 +592,11 @@ void drawrounds() } } -char gentile(int x, int y) -{ - char c = charatpos(x, y); - if (c == TILE_UNKNOWN) - { - int d = dice(); - if (d == 1) - { - c = TILE_DOOR; - } - else if (d < 5) - { - c = TILE_WALL; - } - else - { - c = TILE_FREE; - } - setatpos(x, y, c); - return c; - } - return TILE_UNUSED; -} - -int ddl = 0; -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) { if (charatpos(x,y) != TILE_START) { - setatpos(x, y, TILE_FREE); + setatpos(x, y, TILE_UNVISITED); } bool nok = false; @@ -699,7 +611,7 @@ void dfs(int x, int y) { case NORTH: nok = true; - if (charatpos(x, y - 2) == TILE_UNVISITED) + if (charatpos(x, y - 2) == TILE_INIT) { setatpos(x, y - 1, dice() == 6 ? TILE_DOOR : TILE_FREE); dfs(x, y - 2); @@ -707,7 +619,7 @@ void dfs(int x, int y) break; case SOUTH: sok = true; - if (charatpos(x, y + 2) == TILE_UNVISITED) + if (charatpos(x, y + 2) == TILE_INIT) { setatpos(x, y + 1, dice() == 6 ? TILE_DOOR : TILE_FREE); dfs(x, y + 2); @@ -715,7 +627,7 @@ void dfs(int x, int y) break; case EAST: eok = true; - if (charatpos(x - 2, y) == TILE_UNVISITED) + if (charatpos(x - 2, y) == TILE_INIT) { setatpos(x - 1, y, dice() == 6 ? TILE_DOOR : TILE_FREE); dfs(x - 2, y); @@ -723,7 +635,7 @@ void dfs(int x, int y) break; case WEST: wok = true; - if (charatpos(x + 2, y) == TILE_UNVISITED) + if (charatpos(x + 2, y) == TILE_INIT) { setatpos(x + 1, y, dice() == 6 ? TILE_DOOR : TILE_FREE); dfs(x + 2, y); @@ -735,8 +647,7 @@ void dfs(int x, int y) void initmap() { - //memset(&map, TILE_UNKNOWN, MAP_SIZE * MAP_SIZE); - memset(&map, TILE_UNKNOWN, MAP_SIZE * MAP_SIZE); // dfs: put walls everywhere + memset(&map, TILE_INIT, MAP_SIZE * MAP_SIZE); for (int x = 0; x < MAP_SIZE; x ++) { @@ -749,16 +660,13 @@ void initmap() } else if (x % 2 && y % 2) { - // free tiles (or unvisited for dfs) - setatpos(x, y, TILE_UNVISITED); + // free tiles not yet initialized by dfs + setatpos(x, y, TILE_INIT); } else if (!(x % 2 ) && !(y % 2)) { // unused tiles aka "poles" - //setatpos(x, y, TILE_UNUSED); - - //... or walls for readibility on map - setatpos(x, y, TILE_WALL); + setatpos(x, y, TILE_UNUSED); } else { @@ -778,7 +686,7 @@ void initmap() setatpos(1, 13, TILE_START); // create map - dfs(1,13); + dfs(1, 13); } void update(char* command) @@ -795,7 +703,7 @@ void update(char* command) forward(); if ( (x != state.position.x || y != state.position.y) && dice() > 4) { - gotomonster(); + //gotomonster(); } break; case CMD_RIGHT: @@ -934,6 +842,29 @@ void update(char* command) } } +void drawtopdownpov() +{ + for (int y = 0; y < POV_SIZE; y++) + { + for (int x = 0; x < POV_SIZE; x++) + { + if (y == 0 && pov[1][1] != TILE_FREE) + { + printf(" "); + } + else if (y == 2 && x == 1) + { + printf("^"); + } + else + { + printf("%c", tileonmap(pov[y][x])); + } + } + printf("\n"); + } +} + void draw() { clearscreen(); @@ -971,7 +902,11 @@ void draw() } else if (state.screen == SC_MAP) { + drawpov3d(); + //printf("\n"); + //drawtopdownpov(); + //drawmap(); // todo: would need a compass? /*printf("\n\n"); @@ -1067,29 +1002,6 @@ void draw() } -void drawtopdownpov() -{ - for (int y = 0; y < POV_SIZE; y++) - { - for (int x = 0; x < POV_SIZE; x++) - { - if (y == 0 && pov[1][1] != ' ') - { - printf(" "); - } - else if (y == 2 && x == 1) - { - printf("^"); - } - else - { - printf("%c", pov[y][x]); - } - } - printf("\n"); - } -} - void getcommand(char* command) { fgets(command, sizeof(char) * CMD_LEN, stdin);