1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "io.h"
#include "general.h"
int create_world(SDL_Surface* screen)
{
FILE *general_file = NULL, *other_file = NULL;
robot *my_robot = NULL;
char name_of_map[256] = "", nbr_of_robots_c[256] = "", path_of_file[256] = "", commands[256] = "";
signed int rslt_of_pause = -1;
int *temp = NULL;
int world = 1, i = 0, j = 0, nbr_of_robots = 0, end = 0;
strcpy(path_of_file, "Data/fichier_principal.myr");
general_file = fopen(path_of_file, "r");
if(general_file == NULL)
{
return 1;
}
while(end != EOF)
{
if(i == 0) /* Si c'est la premiere ligne */
{
fgets(nbr_of_robots_c, sizeof nbr_of_robots_c, general_file);
fclean(nbr_of_robots_c, stdin);
nbr_of_robots = strtol(nbr_of_robots_c, NULL, 10);
if(nbr_of_robots <= 0 || nbr_of_robots > 5)
{
nbr_of_robots = 2;
fprintf(stdout, "Le nombre de robots est incorrect, il sera de %d\n", nbr_of_robots);
}
else
{
fprintf(stdout, "Le nombre de robots sera de %d\n", nbr_of_robots);
}
i++;
}
else if(i == 1)
{
fgets(name_of_map, sizeof name_of_map, general_file);
fclean(name_of_map, stdin);
fprintf(stdout, "La map sera %s\n", name_of_map);
i = 0;
end = EOF;
}
fflush(stdout);
}
my_robot = realloc(my_robot, sizeof(robot)*nbr_of_robots);
if(my_robot == NULL)
{
return 2;
}
fprintf(stdout, "Initialision des robots\n");
for(i = 0 ; i < nbr_of_robots ; i++)
{
(my_robot+i)->surface = NULL;
strcpy((my_robot+i)->name, "");
(my_robot+i)->commands = NULL;
(my_robot+i)->position.x = -1;
(my_robot+i)->position.y = -1;
(my_robot+i)->type = 0;
(my_robot+i)->n_commands = 0;
}
fprintf(stdout, "Fin de l'initialision des robots\n");
fprintf(stdout, "Stockage des actions des robots\n");
for(i = 0 ; i < nbr_of_robots ; i++)
{
strcpy(path_of_file, "");
strcpy((my_robot+i)->name, "robot");
strcat(path_of_file, "Data/");
sprintf(nbr_of_robots_c, "%d", i);
strcat((my_robot+i)->name, nbr_of_robots_c);
strcat(path_of_file, (my_robot+i)->name);
other_file = fopen(path_of_file, "r");
if(other_file == NULL)
{
fprintf(stdout, "Le fichier \"%s\" est introuvable\n", path_of_file);
exit(EXIT_FAILURE);
}
else
{
(my_robot+i)->n_commands = nbr_of_lines(other_file);
temp = realloc((my_robot+i)->commands, ((my_robot+i)->n_commands)*sizeof(int));
if(temp == NULL)
{
fprintf(stderr, "temp == NULL\n");
exit(EXIT_FAILURE);
}
else
{
(my_robot+i)->commands = temp;
}
for(j = 0 ; j < (my_robot+i)->n_commands ; j++)
{
fgets(commands, sizeof commands, other_file);
fclean(commands, stdin);
if(strcmp(commands, "") == 0)
{
(my_robot+i)->commands[j] = NOTHING;
}
else if(strcmp(commands, "rien") == 0)
{
(my_robot+i)->commands[j] = NOTHING;
}
else if(strcmp(commands, "start") == 0)
{
(my_robot+i)->commands[j] = START;
}
else if(strcmp(commands, "stop") == 0)
{
(my_robot+i)->commands[j] = STOP;
}
fprintf(stdout, "Commands %d - %d\n", j, (my_robot+i)->commands[j]);
}
fclose(other_file);
}
fflush(stdout);
}
fprintf(stdout, "Fin de stockage des actions des robots\n");
while(world == 1) /* tant que le monde tourne rond */
{
rslt_of_pause = my_pause();
if(rslt_of_pause == 0)
{
world = 0;
}
}
free(my_robot);
return EXIT_SUCCESS;
} |