From f55731906a4604ea75e1237a9089fcf18d8c88e8 Mon Sep 17 00:00:00 2001 From: quenousimporte Date: Fri, 28 Mar 2025 10:58:57 +0100 Subject: [PATCH] load external map slight refactor --- w/w.c | 131 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 66 insertions(+), 65 deletions(-) diff --git a/w/w.c b/w/w.c index b002bc7..a06327f 100644 --- a/w/w.c +++ b/w/w.c @@ -1,22 +1,6 @@ #include #include -const int MAP_SIZE = 9; -const char* statepath = "w.state"; -const char* map[] = { - "wwwwwwwww", - "w w", - "w wwwww w", - "w w w w w", - "w w wdw w", - "w w w w", - "wdw wdw", - "w d w", - "wwwwwwwww" -}; -char pov[3][3]; -unsigned char pov3d[10][10]; - typedef struct { int x; int y; @@ -28,7 +12,27 @@ typedef struct { Location location; } Gamestate; -Gamestate gamestate; +const int MAP_SIZE = 9; +const char* statepath = "w.state"; +char pov[3][3]; +unsigned char pov3d[10][10]; +Gamestate state; +char map[9][9]; + +void loadmap() +{ + FILE* file = fopen("f1.map", "r"); + for (int i = 0; i < MAP_SIZE; i++) + { + for (int j = 0; j < MAP_SIZE; j++) + { + map[i][j] = fgetc(file); + } + // skip lf + fgetc(file); + } + fclose(file); +} void drawmap() { @@ -37,38 +41,35 @@ void drawmap() printf("%d", y); for (int j = 0; j < MAP_SIZE; j++) { char toprint = map[i][j]; - if (y == gamestate.location.y && j == gamestate.location.x) + if (y == state.location.y && j == state.location.x) { - if (gamestate.location.orientation == 'n') toprint = '^'; - if (gamestate.location.orientation == 's') toprint = 'v'; - if (gamestate.location.orientation == 'e') toprint = '>'; - if (gamestate.location.orientation == 'w') toprint = '<'; + if (state.location.orientation == 'n') toprint = '^'; + if (state.location.orientation == 's') toprint = 'v'; + if (state.location.orientation == 'e') toprint = '>'; + if (state.location.orientation == 'w') toprint = '<'; } printf("%c", toprint); } printf("\n"); } - printf("0123456789\n"); + printf(" 012345678\n"); } void loadstate() { - gamestate.name[0] = '\0'; - FILE* file = fopen(statepath, "r+"); + state.name[0] = '\0'; + FILE* file = fopen(statepath, "r"); if (file) { - fread(&gamestate, sizeof(Gamestate), 1, file); - fclose(file); + fread(&state, sizeof(Gamestate), 1, file); + fclose(file); } } void savestate() { FILE* file = fopen(statepath, "w"); - - if (file) { - fwrite(&gamestate, sizeof(Gamestate), 1, file); - } + fwrite(&state, sizeof(Gamestate), 1, file); fclose(file); } @@ -202,30 +203,31 @@ void update3dpov() } } - // doors (front only) + const char doorchar = 177; if (pov[1][1] == 'd') { + // front door for (int i = 4; i <= 7; i++) { for (int j = 3; j < 7; j++) - pov3d[i][j] = 176; + pov3d[i][j] = doorchar; } } if (pov[2][0] == 'd') { - for (int i = 5; i < 9; i++) pov3d[i][0] = 176; + for (int i = 5; i < 9; i++) pov3d[i][0] = doorchar; } if (pov[2][2] == 'd') { - for (int i = 5; i < 9; i++) pov3d[i][9] = 176; + for (int i = 5; i < 9; i++) pov3d[i][9] = doorchar; } } void updatepov() { - int x = gamestate.location.x; - int y = gamestate.location.y; - char orientation = gamestate.location.orientation; + int x = state.location.x; + int y = state.location.y; + char orientation = state.location.orientation; for (int frontoffset = 2; frontoffset >= 0; frontoffset--) { @@ -262,19 +264,19 @@ void forward() { if (pov[1][1] != 'w') { - switch (gamestate.location.orientation) + switch (state.location.orientation) { case 'n': - gamestate.location.y += 2; + state.location.y += 2; break; case 's': - gamestate.location.y -= 2; + state.location.y -= 2; break; case 'e': - gamestate.location.x += 2; + state.location.x += 2; break; case 'w': - gamestate.location.x -= 2; + state.location.x -= 2; break; } } @@ -282,38 +284,38 @@ void forward() void right() { - switch (gamestate.location.orientation) + switch (state.location.orientation) { case 'n': - gamestate.location.orientation = 'e'; + state.location.orientation = 'e'; break; case 's': - gamestate.location.orientation = 'w'; + state.location.orientation = 'w'; break; case 'e': - gamestate.location.orientation = 's'; + state.location.orientation = 's'; break; case 'w': - gamestate.location.orientation = 'n'; + state.location.orientation = 'n'; break; } } void left() { - switch (gamestate.location.orientation) + switch (state.location.orientation) { case 'n': - gamestate.location.orientation = 'w'; + state.location.orientation = 'w'; break; case 's': - gamestate.location.orientation = 'e'; + state.location.orientation = 'e'; break; case 'e': - gamestate.location.orientation = 'n'; + state.location.orientation = 'n'; break; case 'w': - gamestate.location.orientation = 's'; + state.location.orientation = 's'; break; } } @@ -321,26 +323,25 @@ void left() void dumpstate() { printf("Name:%s\nx:%d\ny:%d\norientation:%c\n", - gamestate.name, - gamestate.location.x, - gamestate.location.y, - gamestate.location.orientation); + state.name, + state.location.x, + state.location.y, + state.location.orientation); } void init() { + loadmap(); loadstate(); - printf("test\n"); - if (strlen(gamestate.name) == 0) + if (strlen(state.name) == 0) { printf("Welcome.\nEnter your name: "); - fgets(gamestate.name, sizeof(gamestate.name), stdin); - gamestate.name[strcspn(gamestate.name, "\n")] = 0; - gamestate.location.x = 1; - gamestate.location.y = 1; - gamestate.location.orientation = 'n'; + fgets(state.name, sizeof(state.name), stdin); + state.name[strcspn(state.name, "\n")] = 0; + state.location.x = 1; + state.location.y = 1; + state.location.orientation = 'n'; } - updatepov(); }