rationalizing things

This commit is contained in:
quenousimporte 2025-03-29 11:38:06 +01:00
parent 35d9e91598
commit aae413fe1a
1 changed files with 71 additions and 82 deletions

153
w/w.c
View File

@ -3,53 +3,54 @@
#include <stdlib.h>
#include <stdbool.h>
#define CMD_LEN 10
#define NAME_LEN 10
#define POV_3D_SIZE 10
#define POV_SIZE 3
#define PLACE_TITLE 0
#define PLACE_STATION 1
#define PLACE_MAP 2
typedef struct {
int x;
int y;
char orientation;
char place[2];
} Location;
typedef struct {
char name[10];
char name[NAME_LEN];
Location location;
char place;
} Gamestate;
const char* statepath = "w.state";
char pov[3][3];
unsigned char pov3d[10][10];
char pov[POV_SIZE][POV_SIZE];
unsigned char pov3d[POV_3D_SIZE][POV_3D_SIZE];
Gamestate state;
int MAP_SIZE = 0;
int mapsize = 0;
char* map = NULL;
bool inmap()
{
return state.location.place[0] == 'f';
}
char charatpos(int x, int y)
{
if (x < 0 || y < 0 || x >= MAP_SIZE || y >= MAP_SIZE)
if (x < 0 || y < 0 || x >= mapsize || y >= mapsize)
{
return ' ';
}
return map[ (MAP_SIZE - y - 1) * MAP_SIZE + x];
return map[ (mapsize - y - 1) * mapsize + x];
}
void loadmap()
{
char filename[6];
strcpy(filename, state.location.place);
strcat(filename, ".map");
FILE* file = fopen(filename, "r");
FILE* file = fopen("w.map", "r");
while (fgetc(file) != '\n')
{
MAP_SIZE++;
mapsize++;
}
rewind(file);
map = (char*)malloc(MAP_SIZE * MAP_SIZE * sizeof(char));
map = (char*)malloc(mapsize * mapsize * sizeof(char));
char c = 0;
int i = 0;
@ -66,11 +67,10 @@ void loadmap()
void drawmap()
{
for (int i = 0; i < MAP_SIZE; i++) {
int y = MAP_SIZE - i - 1;
//printf("%d", y);
for (int j = 0; j < MAP_SIZE; j++) {
char toprint = map[i * MAP_SIZE + j];
for (int i = 0; i < mapsize; i++) {
int y = mapsize - i - 1;
for (int j = 0; j < mapsize; j++) {
char toprint = map[i * mapsize + j];
if (y == state.location.y && j == state.location.x)
{
if (state.location.orientation == 'n') toprint = '^';
@ -82,18 +82,6 @@ void drawmap()
}
printf("\n");
}
//printf(" 012345678\n");
}
void loadstate()
{
state.name[0] = '\0';
FILE* file = fopen(statepath, "r");
if (file)
{
fread(&state, sizeof(Gamestate), 1, file);
fclose(file);
}
}
void savestate()
@ -105,9 +93,9 @@ void savestate()
void update3dpov()
{
for (int i = 0; i < 10; i++)
for (int i = 0; i < POV_3D_SIZE; i++)
{
memset(pov3d[i], ' ', 10);
memset(pov3d[i], ' ', POV_3D_SIZE);
}
// front constants
@ -361,26 +349,25 @@ void clearscreen()
void init()
{
loadstate();
if (strlen(state.name) == 0)
FILE* file = fopen(statepath, "r");
if (file)
{
// start at station
strcpy(state.location.place, "st");
state.location.x = 1;
state.location.y = 1;
state.location.orientation = 'n';
fread(&state, sizeof(Gamestate), 1, file);
fclose(file);
}
else
{
state.place = PLACE_TITLE;
}
loadmap();
}
void update(char command)
void update(char command[CMD_LEN])
{
if (inmap())
char c = command[0];
if (state.place == PLACE_MAP)
{
if (map == NULL)
{
loadmap();
}
switch (command)
switch (c)
{
case 's':
forward();
@ -396,22 +383,32 @@ void update(char command)
}
updatepov();
}
else
else if (state.place == PLACE_TITLE)
{
if (command == 'n')
if (strlen(command) > 0)
{
strcpy(state.location.place, "f1");
strcpy(state.name, command);
state.place = PLACE_STATION;
}
}
else if (state.place == PLACE_STATION)
{
if (c == 'n')
{
state.place = PLACE_MAP;
state.location.x = 1;
state.location.y = 7;
state.location.orientation = 'n';
updatepov();
}
}
}
void draw()
{
clearscreen();
if (strlen(state.name) == 0)
if (state.place == PLACE_TITLE)
{
printf("\n\n La légende des dongeons de l'espace\n\n\n");
printf("Bonjour.\n");
@ -420,9 +417,9 @@ void draw()
printf("> ");
}
else if (inmap())
else if (state.place == PLACE_MAP)
{
int shift = 10;
int shift = 5;
for (int i = 0; i < shift; i++) printf("\n");
for (int i = 0; i < shift; i++) printf(" ");
@ -439,27 +436,22 @@ void draw()
printf("|\n");
}
for (int i = 0; i < shift; i++) printf(" ");
printf("o----------o\n");
printf("o----------o\n\n");
drawmap();
//drawmap();
}
else
else if (state.place == PLACE_STATION)
{
printf("%s est à la station intergalactique.\n\n", state.name);
printf("n: prendre la navette pour le dongeon de l'espace\n");
printf("r: aller dans une capsule de repos\n");
printf("m: aller au grand marché hyperspatial\n");
printf("i: consulter l'intercom\n\n");
}
}
void drawtopdownpov()
{
for (int y = 0; y < 3; y++)
for (int y = 0; y < POV_SIZE; y++)
{
for (int x = 0; x < 3; x++)
for (int x = 0; x < POV_SIZE; x++)
{
if (y == 0 && pov[1][1] != ' ')
{
@ -478,26 +470,23 @@ void drawtopdownpov()
}
}
void getcommand(char* command)
{
fgets(command, sizeof(char) * CMD_LEN, stdin);
command[strcspn(command, "\n")] = 0;
}
int main()
{
init();
char command = ' ';
while (command != 'q')
char command[CMD_LEN] = "";
while (command[0] != 'q')
{
update(command);
draw();
// to refactor in a "getcommand" function or sumfin
if (strlen(state.name) == 0)
{
fgets(state.name, sizeof(state.name), stdin);
state.name[strcspn(state.name, "\n")] = 0;
}
else
{
command = getchar();
}
getcommand(command);
}
savestate();
return 0;