rationalizing things
This commit is contained in:
		
							parent
							
								
									35d9e91598
								
							
						
					
					
						commit
						aae413fe1a
					
				
							
								
								
									
										153
									
								
								w/w.c
								
								
								
								
							
							
						
						
									
										153
									
								
								w/w.c
								
								
								
								
							| 
						 | 
					@ -3,53 +3,54 @@
 | 
				
			||||||
#include <stdlib.h>
 | 
					#include <stdlib.h>
 | 
				
			||||||
#include <stdbool.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 {
 | 
					typedef struct {
 | 
				
			||||||
	int x;
 | 
						int x;
 | 
				
			||||||
	int y;
 | 
						int y;
 | 
				
			||||||
	char orientation;
 | 
						char orientation;
 | 
				
			||||||
	char place[2];
 | 
					 | 
				
			||||||
} Location;
 | 
					} Location;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef struct {
 | 
					typedef struct {
 | 
				
			||||||
	char name[10];
 | 
						char name[NAME_LEN];
 | 
				
			||||||
	Location location;
 | 
						Location location;
 | 
				
			||||||
 | 
						char place;
 | 
				
			||||||
} Gamestate;
 | 
					} Gamestate;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const char* statepath = "w.state";
 | 
					const char* statepath = "w.state";
 | 
				
			||||||
char pov[3][3];
 | 
					char pov[POV_SIZE][POV_SIZE];
 | 
				
			||||||
unsigned char pov3d[10][10];
 | 
					unsigned char pov3d[POV_3D_SIZE][POV_3D_SIZE];
 | 
				
			||||||
Gamestate state;
 | 
					Gamestate state;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int MAP_SIZE = 0;
 | 
					int mapsize = 0;
 | 
				
			||||||
char* map = NULL;
 | 
					char* map = NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bool inmap()
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
	return state.location.place[0] == 'f';
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
char charatpos(int x, int y)
 | 
					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 ' ';
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return map[ (MAP_SIZE - y - 1) * MAP_SIZE + x];
 | 
						return map[ (mapsize - y - 1) * mapsize + x];
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void loadmap()
 | 
					void loadmap()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	char filename[6];
 | 
						FILE* file = fopen("w.map", "r");
 | 
				
			||||||
	strcpy(filename, state.location.place);
 | 
					 | 
				
			||||||
	strcat(filename, ".map");
 | 
					 | 
				
			||||||
	FILE* file = fopen(filename, "r");
 | 
					 | 
				
			||||||
	while (fgetc(file) != '\n')
 | 
						while (fgetc(file) != '\n')
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		MAP_SIZE++;
 | 
							mapsize++;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	rewind(file);
 | 
						rewind(file);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	map = (char*)malloc(MAP_SIZE * MAP_SIZE * sizeof(char));
 | 
						map = (char*)malloc(mapsize * mapsize * sizeof(char));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	char c = 0;
 | 
						char c = 0;
 | 
				
			||||||
	int i = 0;
 | 
						int i = 0;
 | 
				
			||||||
| 
						 | 
					@ -66,11 +67,10 @@ void loadmap()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void drawmap()
 | 
					void drawmap()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	for (int i = 0; i < MAP_SIZE; i++) {
 | 
						for (int i = 0; i < mapsize; i++) {
 | 
				
			||||||
		int y = MAP_SIZE - i - 1;
 | 
							int y = mapsize - i - 1;
 | 
				
			||||||
		//printf("%d", y);
 | 
							for (int j = 0; j < mapsize; j++) {
 | 
				
			||||||
		for (int j = 0; j < MAP_SIZE; j++) {
 | 
								char toprint = map[i * mapsize + j];
 | 
				
			||||||
			char toprint = map[i * MAP_SIZE + j];
 | 
					 | 
				
			||||||
			if (y == state.location.y && j == state.location.x)
 | 
								if (y == state.location.y && j == state.location.x)
 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
				if (state.location.orientation == 'n') toprint = '^';
 | 
									if (state.location.orientation == 'n') toprint = '^';
 | 
				
			||||||
| 
						 | 
					@ -82,18 +82,6 @@ void drawmap()
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		printf("\n");
 | 
							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()
 | 
					void savestate()
 | 
				
			||||||
| 
						 | 
					@ -105,9 +93,9 @@ void savestate()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void update3dpov()
 | 
					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
 | 
						// front constants
 | 
				
			||||||
| 
						 | 
					@ -361,26 +349,25 @@ void clearscreen()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void init()
 | 
					void init()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	loadstate();
 | 
						FILE* file = fopen(statepath, "r");
 | 
				
			||||||
	if (strlen(state.name) == 0)
 | 
						if (file)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		// start at station
 | 
							fread(&state, sizeof(Gamestate), 1, file);
 | 
				
			||||||
		strcpy(state.location.place, "st");
 | 
							fclose(file);
 | 
				
			||||||
		state.location.x = 1;
 | 
					 | 
				
			||||||
		state.location.y = 1;
 | 
					 | 
				
			||||||
		state.location.orientation = 'n';
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						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)
 | 
							switch (c)
 | 
				
			||||||
		{
 | 
					 | 
				
			||||||
			loadmap();
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		switch (command)
 | 
					 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
		case 's':
 | 
							case 's':
 | 
				
			||||||
			forward();
 | 
								forward();
 | 
				
			||||||
| 
						 | 
					@ -396,22 +383,32 @@ void update(char command)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		updatepov();
 | 
							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()
 | 
					void draw()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	clearscreen();
 | 
						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("\n\n      La légende des dongeons de l'espace\n\n\n");
 | 
				
			||||||
		printf("Bonjour.\n");
 | 
							printf("Bonjour.\n");
 | 
				
			||||||
| 
						 | 
					@ -420,9 +417,9 @@ void draw()
 | 
				
			||||||
		printf("> ");
 | 
							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("\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		for (int i = 0; i < shift; i++) printf(" ");
 | 
							for (int i = 0; i < shift; i++) printf(" ");
 | 
				
			||||||
| 
						 | 
					@ -439,27 +436,22 @@ void draw()
 | 
				
			||||||
			printf("|\n");
 | 
								printf("|\n");
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		for (int i = 0; i < shift; i++) printf(" ");
 | 
							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("%s est à la station intergalactique.\n\n", state.name);
 | 
				
			||||||
		printf("n: prendre la navette pour le dongeon de l'espace\n");
 | 
							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()
 | 
					void drawtopdownpov()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
						for (int y = 0; y < POV_SIZE; y++)
 | 
				
			||||||
	for (int y = 0; y < 3; y++)
 | 
					 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		for (int x = 0; x < 3; x++)
 | 
							for (int x = 0; x < POV_SIZE; x++)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			if (y == 0 && pov[1][1] != ' ')
 | 
								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()
 | 
					int main()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	init();
 | 
						init();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	char command = ' ';
 | 
						char command[CMD_LEN] = "";
 | 
				
			||||||
	while (command != 'q')
 | 
						
 | 
				
			||||||
 | 
						while (command[0] != 'q')
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		update(command);
 | 
							update(command);
 | 
				
			||||||
		draw();
 | 
							draw();
 | 
				
			||||||
		
 | 
							getcommand(command);
 | 
				
			||||||
		// 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();
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	savestate();
 | 
						savestate();
 | 
				
			||||||
	return 0;
 | 
						return 0;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue