//
// Created by Alban on 03/12/2024.
//
#include "simulation1.h"
// Fonction pour créer un tableau d'espèces à partir du JSON
int *recuperer_info_json(const char *fichier_json) {
FILE *file = fopen(fichier_json, "r");
if (!file) {
fprintf(stderr, "Impossible d'ouvrir le fichier JSON.\n");
return NULL;
}
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
fseek(file, 0, SEEK_SET);
char *json_data = (char *) malloc(file_size + 1);
if (!json_data) {
fprintf(stderr, "Erreur d'allocation mémoire.\n");
fclose(file);
return NULL;
}
fread(json_data, 1, file_size, file);
json_data[file_size] = '\0';
fclose(file);
cJSON *root = cJSON_Parse(json_data);
free(json_data);
if (!root) {
fprintf(stderr, "Erreur de parsing du JSON : %s\n", cJSON_GetErrorPtr());
return NULL;
}
cJSON *nodes = cJSON_GetObjectItemCaseSensitive(root, "nodes");
if (!cJSON_IsArray(nodes)) {
fprintf(stderr, "Erreur : le champ 'nodes' est manquant ou n'est pas un tableau.\n");
cJSON_Delete(root);
return NULL;
}
size_t nb_especes = cJSON_GetArraySize(nodes);
if (nb_especes == 0) {
fprintf(stderr, "Aucune espèce trouvée dans le JSON.\n");
cJSON_Delete(root);
return NULL;
}
int *tableau = (int *) malloc((nb_especes + 1) * sizeof(int));
if (!tableau) {
fprintf(stderr, "Erreur d'allocation mémoire.\n");
cJSON_Delete(root);
return NULL;
}
tableau[0] = nb_especes;
for (size_t i = 1; i <= tableau[0]; i++) {
tableau[i] = -1; // Initialisation à une valeur par défaut
}
printf("Valeurs initiales du tableau :\n");
for (size_t i = 0; i <= tableau[0]; i++) {
printf("tableau[%zu] = %d\n", i, tableau[i]);
}
cJSON_Delete(root);
return tableau;
}
int SaisieSimulation(toutAllegro *toutAllegro1, nom_fichier_extension *nomFichierExtension) {
bool running = true;
int saisie = 0;
while (running) {
char input[32] = {0}; // Tampon pour la saisie
int cursor_pos = 0;
bool input_complete = false;
while (!input_complete) {
ALLEGRO_EVENT event;
al_wait_for_event(toutAllegro1->eventQueue, &event);
if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
running = false;
input_complete = true;
} else if (event.type == ALLEGRO_EVENT_KEY_CHAR) {
char c = event.keyboard.unichar;
if (c >= '0' && c <= '9' && cursor_pos < 9) { // Limiter à 9 chiffres
input[cursor_pos++] = c;
input[cursor_pos] = '\0';
} else if (c == '\b' && cursor_pos > 0) { // Gestion du backspace
input[--cursor_pos] = '\0';
} else if (c == '\r') { // Validation de la saisie avec Entrée
if (strlen(input) > 0) {
input_complete = true;
}
}
}
// Affichage du texte
al_draw_filled_rectangle(1225, 245, 1792, 827, al_map_rgb(255, 255, 255));
al_draw_text(toutAllegro1->font_large, COULEUR_PRIMAIRE, 1500, 340, ALLEGRO_ALIGN_CENTRE, "Population :");
al_draw_text(toutAllegro1->font_mid, COULEUR_VERT, 1500, 475, ALLEGRO_ALIGN_CENTRE, input);
if (cursor_pos == 0) {
al_draw_text(toutAllegro1->font_small, al_map_rgb(159, 51, 67), 1500, 620, ALLEGRO_ALIGN_CENTRE,
"Aucune valeur saisie!");
}
al_flip_display();
}
// Conversion de l'entrée en entier
saisie = atoi(input);
// Retourner la valeur saisie
return saisie;
}
// Retour par défaut en cas de fermeture de la fenêtre
return saisie;
}
bool tableau_valide(int *tableau, int taille) {
// Parcours du tableau
for (int i = 0; i < taille + 1; i++) {
if (tableau[i] == -1) {
return false; // Retourner false si une valeur égale à -1 est trouvée
}
}
return true; // Retourner true si toutes les valeurs sont différentes de -1
}
void
saisirPopulation(toutAllegro *toutAllegro1, nom_fichier_extension *nomFichierExtension, cJSON *json, Graphe *graphe) {
for (int i = 0; i < graphe->nbSommets; i++) {
graphe->tabEspece[i].population = (double *) calloc(1 + NB_VALEUR_SIMU, sizeof(double));
}
toutAllegro1->tabImage[choix_population_gris] = al_load_bitmap("DATA/PNG/Menu_Simulation0.png");
toutAllegro1->tabImage[graphe_total] = al_load_bitmap(nomFichierExtension->nom_fichier_png);
if (!toutAllegro1->tabImage[choix_population_gris] || !toutAllegro1->tabImage[graphe_total])
printf("erreur json \n");
ALLEGRO_COLOR texte_couleur = COULEUR_PRIMAIRE;
cJSON *nodes = cJSON_GetObjectItem(json, "nodes");
cJSON *ecosystem = cJSON_GetObjectItem(json, "ecosystem");
cJSON *dernier_node = NULL;
float graph_x = 600 - al_get_bitmap_width(toutAllegro1->tabImage[graphe_total]) / 2;
int graph_y = 500 - al_get_bitmap_height(toutAllegro1->tabImage[graphe_total]) / 2; int* table = recuperer_info_json(nomFichierExtension->nom_fichier_json);
char buffer[50]; // Un tampon pour stocker le texte converti
al_draw_bitmap(toutAllegro1->tabImage[choix_population_gris], 0, 0, 0);
al_draw_bitmap(toutAllegro1->tabImage[graphe_total], graph_x, graph_y, 0);
al_draw_textf(toutAllegro1->font_large, texte_couleur, 1250, 50, 0, "%s", ecosystem->valuestring);
al_flip_display();
bool running = true;
ALLEGRO_BITMAP *capture = al_create_bitmap(SCREEN_WIDTH, SCREEN_HEIGHT);
// Copier le contenu du backbuffer dans un bitmap lisible
ALLEGRO_BITMAP *backbuffer = al_get_backbuffer(toutAllegro1->display);
al_set_target_bitmap(capture);
al_draw_bitmap(backbuffer, 0, 0, 0);
al_set_target_backbuffer(toutAllegro1->display);
while (running) {
ALLEGRO_EVENT event;
al_wait_for_event(toutAllegro1->eventQueue, &event);
if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) break;
if (event.type == ALLEGRO_EVENT_KEY_DOWN && event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
backbuffer = al_get_backbuffer(toutAllegro1->display);
al_set_target_bitmap(capture);
al_draw_bitmap(backbuffer, 0, 0, 0);
al_set_target_backbuffer(toutAllegro1->display);
if (pause(toutAllegro1, capture) == 1)// Appel de la fonction pause
{
running = 0;
}
}
if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
float sourisX = event.mouse.x, sourisY = event.mouse.y;
ALLEGRO_COLOR couleur_sous_souris = al_get_pixel(toutAllegro1->tabImage[graphe_total], sourisX - graph_x,
sourisY - graph_y);
cJSON *node_selectionne = NULL;
cJSON_ArrayForEach(node_selectionne, nodes) {
const char *couleur_hex = cJSON_GetObjectItem(node_selectionne, "couleur")->valuestring;
if ((couleur_sous_souris.r == 0 && couleur_sous_souris.g == 0 && couleur_sous_souris.b == 0) ||
couleurs_egales(couleur_sous_souris, al_map_rgb(255, 255, 255))) {
al_draw_bitmap(toutAllegro1->tabImage[choix_population_gris], 0, 0, 0);
al_draw_bitmap(toutAllegro1->tabImage[graphe_total], graph_x, graph_y, 0);
al_draw_filled_rectangle(1225, 0, 1792, 205, BLANC);
al_draw_text(toutAllegro1->font_mid, COULEUR_PRIMAIRE, 1500, 250, ALLEGRO_ALIGN_CENTRE,
"Valeur enregistrée :");
al_draw_textf(toutAllegro1->font_large, COULEUR_PRIMAIRE, 1250, 50, 0, "%s",
ecosystem->valuestring);
float top = 550 - (16 * table[0]);
for (int i = 0; i < graphe->nbSommets; i++) {
al_draw_text(toutAllegro1->font_small, COULEUR_VERT, 1250, top + (i * 35), 0,
graphe->tabEspece[i].name);
if (table[i + 1] != -1) {
size_t taille = (table[i + 1] * sizeof(char)) + 1;
char *tablechar = malloc(taille); /// probleme d affichage savane
snprintf(tablechar, taille, "%d", table[i + 1]);
al_draw_text(toutAllegro1->font_small, COULEUR_VERT, (int) (1250 + al_get_text_width(
toutAllegro1->font_small, graphe->tabEspece[i].name) + 5), top + (i * 35), 0, ":");
al_draw_text(toutAllegro1->font_small, COULEUR_VERT, (int) (1250 + al_get_text_width(
toutAllegro1->font_small, graphe->tabEspece[i].name) + 15), top + (i * 35), 0,
tablechar);
free(tablechar);
}
}
}
if (couleurs_egales(couleur_sous_souris, hex_to_color(couleur_hex))) {
dernier_node = node_selectionne;
int node_id = cJSON_GetObjectItem(dernier_node, "id")->valueint;
// Gestion de la population
if (table[node_id] != -1) {
sprintf(buffer, "%d", table[node_id]);
} else {
al_draw_filled_rectangle(1225, 0, 1792, 205, BLANC);
al_draw_textf(toutAllegro1->font_mid, texte_couleur, 1500, 65, ALLEGRO_ALIGN_CENTRE, "%s",
cJSON_GetObjectItem(dernier_node, "name")->valuestring);
table[node_id] = SaisieSimulation(toutAllegro1, nomFichierExtension);
sprintf(buffer, "%d", table[node_id]);
}
// Mise à jour de l'affichage
al_draw_bitmap(toutAllegro1->tabImage[choix_population_gris], 0, 0, 0);
al_draw_bitmap(toutAllegro1->tabImage[graphe_total], graph_x, graph_y, 0);
al_draw_filled_rectangle(1225, 0, 1792, 205, BLANC);
al_draw_textf(toutAllegro1->font_mid, texte_couleur, 1500, 65, ALLEGRO_ALIGN_CENTRE, "%s",
cJSON_GetObjectItem(dernier_node, "name")->valuestring);
al_draw_filled_rectangle(1225, 245, 1792, 827, BLANC);
al_draw_text(toutAllegro1->font_large, COULEUR_PRIMAIRE, 1500, 340, ALLEGRO_ALIGN_CENTRE,
"Population :");
al_draw_text(toutAllegro1->font_mid, COULEUR_VERT, 1500, 475, ALLEGRO_ALIGN_CENTRE, buffer);
al_draw_text(toutAllegro1->font_small, COULEUR_VERT, 1500, 620, ALLEGRO_ALIGN_CENTRE,
"Valeur enregistrée !");
break;
}
}
// Vérifier le tableau valide
if (tableau_valide(table, table[0])) {
toutAllegro1->tabImage[choix_population_couleur] = al_load_bitmap("DATA/PNG/Menu_Simulation1.png");
if (!toutAllegro1->tabImage[choix_population_couleur]) {
fprintf(stderr, "Erreur de chargement du fond.\n");
break;
}
al_draw_bitmap(toutAllegro1->tabImage[choix_population_couleur], 0, 0, 0);
al_draw_bitmap(toutAllegro1->tabImage[graphe_total], graph_x, graph_y, 0);
if ((couleur_sous_souris.r == 0 && couleur_sous_souris.g == 0 && couleur_sous_souris.b == 0) ||
couleurs_egales(couleur_sous_souris, al_map_rgb(255, 255, 255))) {
al_draw_bitmap(toutAllegro1->tabImage[choix_population_couleur], 0, 0, 0);
al_draw_bitmap(toutAllegro1->tabImage[graphe_total], graph_x, graph_y, 0);
al_draw_filled_rectangle(1225, 0, 1792, 205, BLANC);
al_draw_text(toutAllegro1->font_mid, COULEUR_PRIMAIRE, 1500, 250, ALLEGRO_ALIGN_CENTRE,
"Valeur enregistrée :");
al_draw_textf(toutAllegro1->font_large, COULEUR_PRIMAIRE, 1250, 50, 0, "%s",
ecosystem->valuestring);
float top = 550 - (16 * table[0]);
for (int i = 0; i < graphe->nbSommets; i++) {
al_draw_text(toutAllegro1->font_small, COULEUR_VERT, 1250, top + (i * 35), 0,
graphe->tabEspece[i].name);
if (table[i + 1] != -1) {
size_t taille = (table[i + 1] * sizeof(char)) + 1;
char *tablechar = malloc(taille); /// probleme d affichage savane
snprintf(tablechar, taille, "%d", table[i + 1]);
al_draw_text(toutAllegro1->font_small, COULEUR_VERT, (int) (1250 + al_get_text_width(
toutAllegro1->font_small, graphe->tabEspece[i].name) + 5), top + (i * 35), 0, ":");
al_draw_text(toutAllegro1->font_small, COULEUR_VERT, (int) (1250 + al_get_text_width(
toutAllegro1->font_small, graphe->tabEspece[i].name) + 15), top + (i * 35), 0,
tablechar);
free(tablechar);
}
}
} else {
al_draw_filled_rectangle(1225, 0, 1792, 205, BLANC);
al_draw_textf(toutAllegro1->font_mid, texte_couleur, 1500, 65, ALLEGRO_ALIGN_CENTRE, "%s",
cJSON_GetObjectItem(dernier_node, "name")->valuestring);
al_draw_text(toutAllegro1->font_large, COULEUR_PRIMAIRE, 1500, 340, ALLEGRO_ALIGN_CENTRE,
"Population :");
al_draw_text(toutAllegro1->font_mid, COULEUR_VERT, 1500, 475, ALLEGRO_ALIGN_CENTRE, buffer);
al_draw_text(toutAllegro1->font_small, COULEUR_VERT, 1500, 620, ALLEGRO_ALIGN_CENTRE,
"Valeur enregistrée !");
}
if (sourisX > 1357 && sourisX < 1675 && sourisY > 832 && sourisY < 937) {
running = 0; // Quitter si on clique sur le bouton
}
}
al_flip_display();
}
}
for (int i = 0; i < graphe->nbSommets; i++) {
graphe->tabEspece[i].population[0] = (double) table[i + 1];
}
}
void simulationPopulation(toutAllegro *toutAllegro1, nom_fichier_extension *nomFichierExtension, Graphe *graphe,
cJSON *json) {
int mouse_x = 0;
int mouse_y = 0;
ALLEGRO_COLOR couleur_sous_souris;
toutAllegro1->tabImage[fond_simulation] = al_load_bitmap("DATA/PNG/Simulation.png");
if (!toutAllegro1->tabImage[fond_simulation])
printf("erreur allocation bitmap simulationBFSHIIBDINFSBINBINBNIBNIFSBFBNIBFNIFB \n");
al_draw_bitmap(toutAllegro1->tabImage[fond_simulation], 0, 0, 0);
al_draw_bitmap(toutAllegro1->tabImage[graphe_total],
500 - al_get_bitmap_width(toutAllegro1->tabImage[graphe_total]) / 2,
600 - al_get_bitmap_height(toutAllegro1->tabImage[graphe_total]) / 2, 0);
al_flip_display();
if (!json) {
fprintf(stderr, "Erreur : Impossible de parser le fichier JSON.\n");
}
cJSON *nodes = cJSON_GetObjectItem(json, "nodes");
cJSON *ecosystem = cJSON_GetObjectItem(json, "ecosystem");
cJSON *dernier_node = NULL;
if (nodes == NULL || ecosystem == NULL) {
fprintf(stderr, "Erreur : Données JSON invalides.\n");
}
int graph_x = 450 - al_get_bitmap_width(toutAllegro1->tabImage[graphe_total]) / 2;
int graph_y = 500 - al_get_bitmap_height(toutAllegro1->tabImage[graphe_total]) / 2;
al_draw_bitmap(toutAllegro1->tabImage[fond_simulation], 0, 0, 0);
al_draw_bitmap(toutAllegro1->tabImage[graphe_total], graph_x, graph_y, 0);
al_draw_textf(toutAllegro1->font_large, COULEUR_PRIMAIRE, 1150, 75, 0, "%s", ecosystem->valuestring);
afficher_infos_noeud(toutAllegro1, dernier_node, COULEUR_PRIMAIRE);
al_flip_display();
ALLEGRO_BITMAP *capture = al_create_bitmap(SCREEN_WIDTH, SCREEN_HEIGHT);
if (!capture) {
fprintf(stderr, "Erreur : impossible de creer le bitmap de capture.\n");
}
// Copier le contenu du backbuffer dans un bitmap lisible
ALLEGRO_BITMAP *backbuffer = al_get_backbuffer(toutAllegro1->display);
al_set_target_bitmap(capture);
al_draw_bitmap(backbuffer, 0, 0, 0);
al_set_target_backbuffer(toutAllegro1->display);
al_start_timer(toutAllegro1->timer);
bool running = true;
int compte = 0;
cJSON *node_selectionne = NULL;
while (running) {
ALLEGRO_EVENT event;
al_wait_for_event(toutAllegro1->eventQueue, &event);
switch (event.type) {
case ALLEGRO_EVENT_TIMER:
compte++;
calculPop(graphe, &compte);
int j = 0;
bool regenerer = true;
for (int i = 0; i < graphe->nbSommets; i++) {
if (graphe->tabEspece[i].population[compte] <= 0 && graphe->tabEspece[i].vivante == 1) {
printf("Suppression %s\n", graphe->tabEspece[i].name);
supprimerEspece(graphe, i);
regenerer = false;
j = i;
}
}
if (regenerer == false) {
ecrire_json(graphe, compte);
al_draw_bitmap(toutAllegro1->tabImage[chargement], 0, 0, 0);
char especeSup[100];
snprintf(especeSup, 100 * sizeof(char), "Supression de l'espece : %s", graphe->tabEspece[j].name);
al_draw_text(toutAllegro1->font_mid, al_map_rgb(0, 0, 0),
al_get_display_width(toutAllegro1->display) / 2,
al_get_display_height(toutAllegro1->display) / 2, 0, especeSup);
convertionSimulation();
al_destroy_bitmap(toutAllegro1->tabImage[graphe_total]);
toutAllegro1->tabImage[graphe_total] = NULL;
toutAllegro1->tabImage[graphe_total] = al_load_bitmap("IMG/simulation.png");
}
break;
case ALLEGRO_EVENT_MOUSE_AXES:
mouse_x = event.mouse.x;
mouse_y = event.mouse.y;
if (mouse_x >= 0 && mouse_x < al_get_bitmap_width(capture) &&
mouse_y >= 0 && mouse_y < al_get_bitmap_height(capture)) {
couleur_sous_souris = al_get_pixel(toutAllegro1->tabImage[graphe_total], (int) mouse_x - graph_x,
(int) mouse_y - graph_y);
// Traitez la couleur...
} else {
printf("Coordonnees en dehors des limites du bitmap.\n");
}
// Trouver le nœud correspondant
node_selectionne = NULL;
cJSON_ArrayForEach(node_selectionne, nodes) {
const char *couleur_hex = cJSON_GetObjectItem(node_selectionne, "couleur")->valuestring;
if (couleurs_egales(couleur_sous_souris, hex_to_color(couleur_hex))) {
dernier_node = node_selectionne;
break;
}
}
break;
case ALLEGRO_EVENT_DISPLAY_CLOSE:
running = false;
break;
case ALLEGRO_EVENT_DISPLAY_RESIZE:
al_acknowledge_resize(toutAllegro1->display); // Important pour finaliser le redimensionnement
printf("Nouvelle taille : %d x %d\n", event.display.width, event.display.height);
break;
case ALLEGRO_EVENT_KEY_DOWN:
switch (event.keyboard.keycode)
{
case ALLEGRO_KEY_P:
al_stop_timer(toutAllegro1->timer);
break;
case ALLEGRO_KEY_R:
al_start_timer(toutAllegro1->timer);
break;
case ALLEGRO_KEY_ESCAPE:
backbuffer = al_get_backbuffer(toutAllegro1->display);
al_set_target_bitmap(capture);
al_draw_bitmap(backbuffer, 0, 0, 0);
al_set_target_backbuffer(toutAllegro1->display);
int sel = -1;
al_stop_timer(toutAllegro1->timer);
sel = pause(toutAllegro1, capture);
if (sel == 0)// Appel de la fonction pause
{
al_start_timer(toutAllegro1->timer);
}
if (sel == 1)// Appel de la fonction pause
{
afficherGraphe(graphe);
}
else if (sel == 2)// Appel de la fonction pause
{
afficherAnalyseConnexite(graphe);
}
else if (sel == 3)// Appel de la fonction pause
{
RechercheProducteurPrimaire(graphe);
}
else if (sel == 4)// Appel de la fonction pause
{
afficherComplexiteReseau(graphe);
}
else if (sel == 5)// Appel de la fonction pause
{
AffichageSommetSpecifique(graphe);
AnalyseHauteurTrophique(graphe);
}
else if (sel == 6)// Appel de la fonction pause
{
RechercheSansPredateur(graphe);
}
else if (sel == 7)// Appel de la fonction pause
{
running=0;
}
break;
}
}
// Redessiner l'écran
al_draw_bitmap(toutAllegro1->tabImage[fond_simulation], 0, 0, 0);
al_draw_bitmap(toutAllegro1->tabImage[graphe_total], graph_x, graph_y, 0);
al_draw_text(toutAllegro1->font_large, COULEUR_PRIMAIRE, 1325, 300,ALLEGRO_ALIGN_CENTRE, "Population :");
float top = 650-(16*graphe->nbSommets);
for (int i = 0; i < graphe->nbSommets; i++) {
al_draw_text(toutAllegro1->font_small, COULEUR_PRIMAIRE, 1150, top + (i * 35), 0, graphe->tabEspece[i].name);
al_draw_text(toutAllegro1->font_small, COULEUR_PRIMAIRE,(int) (1150 + al_get_text_width(toutAllegro1->font_small, graphe->tabEspece[i].name) + 5), top + (i * 35), 0, ":");
char pop[100] = {0};
snprintf(pop, 100 * sizeof(char), "%d", (int) graphe->tabEspece[i].population[compte]);
al_draw_text(toutAllegro1->font_small, COULEUR_PRIMAIRE,(int) (1160 + al_get_text_width(toutAllegro1->font_small, graphe->tabEspece[i].name) + 15), top + (i * 35), 0, pop);
}
if(node_selectionne!=NULL)
{
int especeSel = cJSON_GetObjectItem(node_selectionne, "id")->valueint - 1;
draw_smooth_curve(toutAllegro1, graphe->tabEspece[especeSel].population, maxPop(graphe->tabEspece[especeSel], compte), compte, COULEUR_PRIMAIRE);
al_draw_textf(toutAllegro1->font_small, COULEUR_PRIMAIRE, 1265, 900, 0, "%s", cJSON_GetObjectItem(node_selectionne, "name")->valuestring);
}
al_flip_display();
}
// Nettoyage
al_destroy_bitmap(capture);
}
int maxPop(espece espece1, int compte) {
int max = 0;
for (int i = 0; i < compte; ++i) {
if (max < espece1.population[i]) {
max = espece1.population[i] + 100;
}
}
return (max == 0) ? 100 : max;
}
void calculPop(Graphe *graphe, int *compte) {
int mois = *compte - 1;
int max = *compte;
for (int i = 0; i < graphe->nbSommets; i++) {
if (max > NB_VALEUR_SIMU) {
for (int j = 1; j < NB_VALEUR_SIMU; j++) {
graphe->tabEspece[i].population[j - 1] = graphe->tabEspece[i].population[j];
}
*compte = NB_VALEUR_SIMU;
}
if (graphe->tabEspece[i].type_espece != 1) {
double popActuelle = graphe->tabEspece[i].population[mois];
double k = 0;
double tauxMort = 0.05;
double predation = 0;
for (int j = 0; j < graphe->nbSommets; j++) {
if (graphe->capacites[j][i] != 0) {
if (graphe->tabEspece[j].population[mois] != 0) {
k += (double) (graphe->tabEspece[j].population[mois] / graphe->capacites[j][i]);
}
}
if (graphe->capacites[i][j] != 0) {
predation += graphe->tabEspece[j].population[mois] * graphe->capacites[i][j];
}
}
if (k == 0) {
graphe->tabEspece[i].population[*compte] = (double) (popActuelle - popActuelle * (0.4)) - predation;
} else {
if (k < popActuelle) {
k = popActuelle;
}
graphe->tabEspece[i].population[*compte] = (double) (popActuelle +
(double) (graphe->tabEspece[i].tCroissance *
popActuelle *
(double) (1 - (popActuelle / k))) -
(tauxMort * popActuelle)) - predation;
/*printf("Espece: %s, popActuelle: %.2f, croissance: %.2f, (1 - popActuelle / k): %.2f, tauxMort: %.2f, predation: %.2f, nouvelle pop: %.2f\n",
graphe->tabEspece[i].name,
popActuelle,
graphe->tabEspece[i].tCroissance * popActuelle,(1 - (popActuelle / k)),
tauxMort * popActuelle,
predation,
graphe->tabEspece[i].population[*compte]);*/
}
} else {
graphe->tabEspece[i].population[*compte] = graphe->tabEspece[i].population[mois];
}
if (graphe->tabEspece[i].population[*compte] < 0) {
graphe->tabEspece[i].population[*compte] = 0;
}
}
printf("\n\n\n");
}
void supprimerEspece(Graphe *graphe, int i) {
for (int j = 0; j < graphe->nbSommets; j++) {
graphe->capacites[i][j] = 0;
graphe->capacites[j][i] = 0;
}
graphe->tabEspece[i].vivante = 0;
}
void draw_smooth_curve(toutAllegro *toutAllegro1, double *serie, int max_y, int data_size, ALLEGRO_COLOR colors) {
int display_width = al_get_display_width(toutAllegro1->display);
int display_height = al_get_display_height(toutAllegro1->display);
// Définir les limites pour dessiner dans la moitié droite
int margin = 50;
int graph_left = display_width / 2 + margin + 75;
int graph_right = display_width - margin;
int graph_top = margin + 350;
int graph_bottom = display_height - margin - 150;
// Échelles des axes
int min_x = 1, max_x = 12;
int min_y = 0;
for (int i = 0; i < NB_VALEUR_SIMU; ++i) {
if (serie[i] < min_y) serie[i] = min_y;
if (serie[i] > max_y) serie[i] = max_y;
}
// Nettoyer la partie droite
al_draw_filled_rectangle((float)(graph_left - margin), (float) graph_top-25, (float)(graph_right + margin), (float)display_height, al_map_rgb(255, 255, 255));
// Dessiner les axes
al_draw_line((float) graph_left, (float) graph_bottom, (float) graph_right, (float) graph_bottom, COULEUR_PRIMAIRE,
2); // Axe X
al_draw_line((float) graph_left, (float) graph_top, (float) graph_left, (float) graph_bottom, COULEUR_PRIMAIRE,
2); // Axe Y
// Étiquettes des axes X
for (int i = min_x; i <= max_x; i++) {
int x = graph_left + (i - min_x) * (graph_right - graph_left) / (max_x - min_x);
al_draw_line((float) x, (float) graph_bottom - 5, (float) x, (float) graph_bottom + 5, al_map_rgb(0, 0, 0), 1);
char label[10];
snprintf(label, sizeof(label), "%d", i);
al_draw_text(toutAllegro1->font_small, COULEUR_PRIMAIRE, (float) x, (float) graph_bottom + 10,
ALLEGRO_ALIGN_CENTER, label);
}
// Étiquettes des axes Y
for (int i = 0; i <= 10; i++) {
int y = graph_bottom - i * (graph_bottom - graph_top) / 10;
int value = min_y + i * (max_y - min_y) / 10;
al_draw_line((float) graph_left - 5, (float) y, (float) graph_left + 5, y, COULEUR_PRIMAIRE, 1);
char label[10];
snprintf(label, sizeof(label), "%d", value);
al_draw_text(toutAllegro1->font_small, COULEUR_PRIMAIRE, (float) graph_left - 10, y - 5, ALLEGRO_ALIGN_RIGHT,
label);
}
// Tracer les courbes
double *x = malloc(data_size * sizeof(double));
double *y = malloc(data_size * sizeof(double));
if (!x || !y) {
fprintf(stderr, "Erreur d'allocation mémoire\n");
exit(1);
}
for (int i = 0; i < data_size; i++) {
x[i] = graph_left + (double) i * (graph_right - graph_left) / (data_size - 1);
y[i] = graph_bottom - (serie[i] - min_y) * (graph_bottom - graph_top) / (max_y - min_y);
}
for (int i = 0; i < data_size - 1; i++) {
al_draw_line(x[i], y[i], x[i + 1], y[i + 1], colors, 2.0f);
}
free(x);
free(y);
}