improve 3d view

This commit is contained in:
quenousimporte 2025-03-27 18:03:12 +01:00
parent ea58c5883d
commit a5f6410159
1 changed files with 129 additions and 126 deletions

255
w/w.c
View File

@ -42,7 +42,7 @@ void drawmap()
void loadstate() void loadstate()
{ {
gamestate.name[0] = '\0'; gamestate.name[0] = '\0';
FILE* file = fopen(statepath, "r+"); FILE* file = fopen(statepath, "r+");
if (file) if (file)
{ {
fread(&gamestate, sizeof(Gamestate), 1, file); fread(&gamestate, sizeof(Gamestate), 1, file);
@ -50,7 +50,7 @@ void loadstate()
} }
} }
void savestate() void savestate()
{ {
FILE* file = fopen(statepath, "w"); FILE* file = fopen(statepath, "w");
@ -101,7 +101,7 @@ void updatepov()
} }
pov[2 - frontoffset][1 + rtoloffset] = charatpos(newX, newY); pov[2 - frontoffset][1 + rtoloffset] = charatpos(newX, newY);
} }
} }
} }
@ -184,7 +184,7 @@ void move(char movement)
void dumpstate() void dumpstate()
{ {
printf("Name:%s\nx:%d\ny:%d\norientation:%c\n", printf("Name:%s\nx:%d\ny:%d\norientation:%c\n",
gamestate.name, gamestate.name,
gamestate.location.x, gamestate.location.x,
gamestate.location.y, gamestate.location.y,
@ -194,17 +194,17 @@ void dumpstate()
void init() void init()
{ {
loadstate(); loadstate();
printf("test\n"); printf("test\n");
if (strlen(gamestate.name) == 0) if (strlen(gamestate.name) == 0)
{ {
printf("Welcome.\nEnter your name: "); printf("Welcome.\nEnter your name: ");
fgets(gamestate.name, sizeof(gamestate.name), stdin); fgets(gamestate.name, sizeof(gamestate.name), stdin);
gamestate.name[strcspn(gamestate.name, "\n")] = 0; gamestate.name[strcspn(gamestate.name, "\n")] = 0;
gamestate.location.x = 1; gamestate.location.x = 1;
gamestate.location.y = 1; gamestate.location.y = 1;
gamestate.location.orientation = 'n'; gamestate.location.orientation = 'n';
} }
updatepov(); updatepov();
} }
@ -227,155 +227,158 @@ void update(char command)
} }
} }
char replacechar(char in, int y)
{
if (in == 'w')
{
return '#';
}
else if (in == 'd')
{
return '*';
}
return in;
}
void draw2() void draw2()
{ {
char screen[10][10]; unsigned char screen[10][10];
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
{ {
memset(screen[i], ' ', 10); memset(screen[i], ' ', 10);
} }
if (pov[2][0] != ' ') // front constants
for (int i = 2; i < 8; i++)
{ {
// wall on the left screen[1][i] = '_';
screen[0][0] = '\\'; screen[7][i] = '_';
screen[1][1] = '\\'; }
screen[9][0] = '/';
screen[8][1] = '/'; if (pov[2][0] == ' ')
{
// front left free
screen[1][0] = '_';
screen[1][1] = '_';
screen[7][0] = '_';
screen[7][1] = '_';
} }
else else
{ {
screen[1][0] = '_'; // front left wall
screen[1][1] = '_'; screen[0][0] = '\\';
screen[1][1] = '\\';
screen[7][0] = '_'; screen[8][1] = '/';
screen[7][1] = '_'; screen[9][0] = '/';
for (int i = 2; i < 8; i++) screen[i][1] = '|';
screen[2][1] = '|';
screen[3][1] = '|';
screen[4][1] = '|';
screen[5][1] = '|';
screen[6][1] = '|';
screen[7][1] = '|';
} }
if (pov[2][2] != ' ') if (pov[2][2] == ' ')
{ {
// wall on the right // front right free
screen[1][8] = '_';
screen[1][9] = '_';
screen[7][8] = '_';
screen[7][9] = '_';
}
else
{
// front right wall
screen[8][8] = '\\';
screen[9][9] = '\\';
screen[0][9] = '/'; screen[0][9] = '/';
screen[1][8] = '/'; screen[1][8] = '/';
screen[9][9] = '\\'; for (int i = 2; i < 8; i++) screen[i][7+1] = '|';
screen[8][8] = '\\';
} }
else
{
screen[1][9] = '_';
screen[1][8] = '_';
screen[7][9] = '_';
screen[7][8] = '_';
screen[2][8] = '|';
screen[3][8] = '|';
screen[4][8] = '|';
screen[5][8] = '|';
screen[6][8] = '|';
screen[7][8] = '|';
}
if (pov[1][1] == ' ') if (pov[1][1] == ' ')
{ {
// front is free // far constants
screen[3][4] = '_'; screen[3][4] = '_';
screen[3][5] = '_'; screen[3][5] = '_';
screen[5][4] = '_'; screen[5][4] = '_';
screen[5][5] = '_'; screen[5][5] = '_';
if (pov[0][0] != ' ')
{
screen[2][2] = '\\';
screen[3][3] = '\\';
screen[4][3] = '|';
screen[5][3] = '|';
screen[7][2] = '/';
screen[6][3] = '/';
}
if (pov[0][2] != ' ') if (pov[0][0] == ' ')
{ {
screen[3][6] = '/'; // far left free
screen[2][7] = '/'; screen[3][2] = '_';
screen[4][6] = '|'; screen[3][3] = '_';
screen[5][6] = '|'; screen[5][2] = '_';
screen[6][6] = '\\'; screen[5][3] = '_';
screen[7][7] = '\\';
}
else
{
screen[2][8] = '|';
screen[3][8] = '|';
screen[4][8] = '|';
screen[5][8] = '|';
screen[6][8] = '|';
screen[7][8] = '|';
screen[3][6] = '_';
screen[3][7] = '_';
screen[5][6] = '_';
screen[5][7] = '_';
}
}
else
{
// front is blocked
for (int i = 2; i < 8; i++)
{
screen[1][i] = '_';
screen[7][i] = '_';
}
if (pov[2][0] != ' ') // if front is free as well
{ if (pov[2][0] == ' ')
// wall on the left
for (int i = 2; i < 8; i++) screen[i][1] = '|';
}
if (pov[2][2] != ' ')
{
// wall on the right
for (int i = 2; i < 8; i++) screen[i][8] = '|';
}
else
{
for (int i = 8; i < 10; i++)
{ {
screen[1][i] = '_'; screen[3][0] = '_';
screen[7][i] = '_'; screen[3][1] = '_';
screen[5][0] = '_';
screen[5][1] = '_';
} }
} }
else
{
// far left wall
screen[2][2] = '\\';
screen[3][3] = '\\';
screen[7][2] = '/';
screen[6][3] = '/';
screen[4][3] = '|';
screen[5][3] = '|';
// ensure front view is complete
for (int i = 2; i < 8; i++) screen[i][1] = '|';
}
if (pov[0][2] == ' ')
{
// far right free
screen[3][6] = '_';
screen[3][7] = '_';
screen[5][6] = '_';
screen[5][7] = '_';
// if front is free as well
if (pov[2][2] == ' ')
{
screen[3][8] = '_';
screen[3][9] = '_';
screen[5][8] = '_';
screen[5][9] = '_';
}
}
else
{
// far right wall
screen[6][6] = '\\';
screen[7][7] = '\\';
screen[2][7] = '/';
screen[3][6] = '/';
screen[4][5+1] = '|';
screen[5][5+1] = '|';
// ensure front view is complete
for (int i = 2; i < 8; i++) screen[i][7+1] = '|';
}
} }
// doors (front only)
if (pov[1][1] == 'd')
{
for (int i = 4; i <= 7; i++)
{
for (int j = 3; j < 7; j++)
screen[i][j] = 176;
}
}
if (pov[2][0] == 'd')
{
for (int i = 5; i < 9; i++) screen[i][0] = 176;
}
if (pov[2][2] == 'd')
{
for (int i = 5; i < 9; i++) screen[i][9] = 176;
}
// print result
printf("o----------o\n");
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
{ {
printf("|");
for (int j = 0; j < 10; j++) for (int j = 0; j < 10; j++)
{ {
printf("%c", screen[i][j]); printf("%c", screen[i][j]);
} }
printf("\n"); printf("|\n");
} }
printf("o----------o\n");
} }
void draw() void draw()
@ -386,13 +389,13 @@ void draw()
#else #else
system("clear"); system("clear");
#endif #endif
draw2();
draw2();
return;
printf("\n\n\n"); printf("\n\n\n");
printf(" o=======o\n"); printf(" o=======o\n");
for (int y = 0; y < 3; y++) for (int y = 0; y < 3; y++)
{ {
printf(" | "); printf(" | ");
@ -408,10 +411,10 @@ void draw()
} }
else else
{ {
printf("%c", replacechar(pov[y][x], y)); printf("%c", pov[y][x]);
} }
} }
printf(" |\n"); printf(" |\n");
} }
printf(" o=======o\n"); printf(" o=======o\n");
printf(" | %s |\n", gamestate.name); printf(" | %s |\n", gamestate.name);