diff --git a/CMakeLists.txt b/CMakeLists.txt index 56e7d81..d6490c0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,7 @@ set(SOURCES src/mutation_engine.c src/data.c src/epidemic_engine.c + src/save.c # ... ) # Shared assets, fx-9860G-only assets and fx-CG-50-only assets diff --git a/Plague.g1a b/Plague.g1a index bfc6a57..29520fe 100644 Binary files a/Plague.g1a and b/Plague.g1a differ diff --git a/src/core.c b/src/core.c index 3be605c..a476971 100644 --- a/src/core.c +++ b/src/core.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "core.h" #include "mutation_engine.h" @@ -53,12 +54,12 @@ void next_frame(struct game *current_game) current_game->time = 0; // Update the game - if (current_game->dna <= 100) current_game->dna += 1; - current_game->research += current_game->priority; + if (current_game->dna <= 100) current_game->dna += 1 + floor(current_game->severity / 25); + if (current_game->research < current_game->limit) current_game->research += current_game->priority; epidemic_simulation(current_game); // Check the end of the game - if (current_game->research > current_game->limit) + if (current_game->research >= current_game->limit) { const char *msg[5] = {"Vous avez", "perdu.", "", "", ""}; message(msg); diff --git a/src/core.h b/src/core.h index 734dc89..cd9427a 100644 --- a/src/core.h +++ b/src/core.h @@ -3,6 +3,8 @@ #include + + // Duration for internal clock (ms) #define ENGINE_TICK 50 #define CURSOR_TICK 150 diff --git a/src/data.c b/src/data.c index 0ad46a1..2aad3a3 100644 --- a/src/data.c +++ b/src/data.c @@ -4,17 +4,17 @@ // (contation, severity, lethality, DNA cost, change to cure requirement) const struct mutation symptoms_data[14] = { - {15, 10, 0, 2, 0, "NAUSEE"}, - {25, 10, 0, 4, 0, "VOMISSEMENT"}, + {10, 10, 0, 2, 0, "NAUSEE"}, + {25, 10, 0, 10, 0, "VOMISSEMENT"}, {20, 10, 0, 3, 0, "TOUX"}, - {15, 20, 0, 4, 0, "PNEUMONIE"}, + {15, 20, 0, 10, 0, "PNEUMONIE"}, {10, 30, 20, 15, 25, "TUMEUR"}, {25, 10, 0, 4, 0, "PLAIES"}, {10, 10, 0, 10, 0, "LESIONS"}, {30, 20, 20, 20, 30, "HEMORRAGIE"}, {25, 15, 10, 17, 0, "INFECTION"}, - {15, 10, 10, 5, 0, "INFLAMMATION"}, - {10, 15, 20, 12, 0, "IMMUNITE"}, + {25, 10, 20, 20, 0, "INFLAMMATION"}, + {10, 15, 20, 15, 0, "IMMUNITE"}, { 0, 20, 0, 15, 120, "PARANOIA"}, { 0, 15, 0, 20, 100, "FOLIE"}, { 0, 30, 30, 30, 250, "ARRET TOTAL"}, @@ -25,8 +25,8 @@ const struct mutation abilities_data[6] = { {15, 0, 0, 10, 0, "FROID"}, {15, 0, 0, 15, 0, "CHAUD"}, - { 0, 20, 4, 25, 20, "GENETIQUE"}, - {10, 40, 8, 30, 50, "MUTATION+"}, + { 0, 10, 4, 25, 20, "GENETIQUE"}, + {10, 40, 20, 30, 50, "MUTATION+"}, {30, 0, 0, 30, 0, "ENVIRON"}, { 0, 15, 30, 15, 30, "MEDICAMENT"}, }; diff --git a/src/main.c b/src/main.c index 8c3318d..18568f8 100644 --- a/src/main.c +++ b/src/main.c @@ -17,7 +17,7 @@ #include "core.h" #include "display_engine.h" #include "mutation_engine.h" - +#include "save.h" // title_screen : display the title screen static void title_screen(void); @@ -41,6 +41,7 @@ int main(void) struct plane plane_4 = {104, 20, 3, 104, 50, 104, 20}; struct plane plane_5 = {68, 44, 4, 34, 44, 68, 44}; + struct game current_game = { .contagion = 0, @@ -65,21 +66,19 @@ int main(void) .grid = {64, 128, NULL}, }; - /* allocate memory */ + // Allocate memory current_game.grid.data = calloc(current_game.grid.width * current_game.grid.height, sizeof(uint8_t)); - if (current_game.grid.data == NULL) - { - const char *msg[5] = {"CALLOC", "FAILED", "", "", ""}; - message(msg); - } - current_game.grid.data[95 + 20 * current_game.grid.width] = 1; current_game.humans[0] = (current_game.grid.width * current_game.grid.height) - 1; + read_save(¤t_game); + main_loop(¤t_game); - /* free memory */ + write_save(¤t_game); + + // Free memory free(current_game.grid.data); return 1; diff --git a/src/mutation_engine.c b/src/mutation_engine.c index 403257f..1fa3c1d 100644 --- a/src/mutation_engine.c +++ b/src/mutation_engine.c @@ -158,7 +158,7 @@ void update_disease(struct game *current_game) // research parameters current_game->limit = RESEARCH_LIMIT + symptom->changement + ability->changement + transmission->changement; - current_game->priority = floor((current_game->severity + current_game->lethality) / 200); + current_game->priority = floor((current_game->severity + current_game->lethality) / 40); } diff --git a/src/save.c b/src/save.c new file mode 100644 index 0000000..addae58 --- /dev/null +++ b/src/save.c @@ -0,0 +1,93 @@ +#include +#include +#include + +#include "save.h" + +/* BUG + Démarrer le jeu, quitter, revenir sur le jeu, quitter -> la calto plante + => Premier démarrage du jeu : ok (valeur par défaut bien initialisée) + => Quand on revient sur le jeu, pas de problème : tout est sauvé correctement (sauvé correctement et chargé correctement) + => On quitte : bug +*/ + +// Name of the savefile +static const uint16_t *filename = u"\\\\fls0\\Plague.sav"; + + +void read_save(struct game *current_game) +{ + struct BFile_FileInfo fileInfo; + int fd, handle; + uint16_t foundpath[30]; + + // Sizes of data and init + const int planes_size = sizeof(*current_game->planes) * (NB_PLANES + 1); + struct plane *new_planes[NB_PLANES + 1]; + for (int i = 0; i < NB_PLANES; i ++) + { + new_planes[i] = current_game->planes[i]; + } + + const int data_size = current_game->grid.width * current_game->grid.height; + uint8_t *data = current_game->grid.data; + + // Check if the savefile exists + char checkfile = BFile_FindFirst(filename, &handle, foundpath, &fileInfo); + BFile_FindClose(handle); + + // If file doesn't exists + if (checkfile == -1) + { + int size = sizeof(struct game) + planes_size + data_size; + BFile_Create(filename, BFile_File, &size); + } + + // Loading game data + else + { + fd = BFile_Open(filename, BFile_ReadOnly); + BFile_Read(fd, current_game, sizeof(struct game), 0); + + // Overwritten struct plane* planes + for (int i = 0; i < NB_PLANES; i ++) + { + current_game->planes[i] = new_planes[i]; + } + + // Loa + for (int i = 0; i < NB_PLANES; i ++) + { + BFile_Read(fd, current_game->planes[i], sizeof(struct plane), -1); + } + BFile_Read(fd, data, data_size, -1); + BFile_Close(fd); + + current_game->grid.data = data; + } +} + + +void write_save(const struct game *current_game) +{ + // Remove the old savefile + BFile_Remove(filename); + + // Create a new one + const int data_size = current_game->grid.width * current_game->grid.height; + int size = sizeof(struct game) + NB_PLANES * sizeof(struct plane) + data_size; + BFile_Create(filename, BFile_File, &size); + + int fd = BFile_Open(filename, BFile_WriteOnly); + + // Write data + BFile_Write(fd, current_game, sizeof(struct game)); + for (int i = 0; i < NB_PLANES ; i ++) + { + BFile_Write(fd, current_game->planes[i], sizeof(struct plane)); + } + BFile_Write(fd, current_game->grid.data, data_size); + + // Close file + BFile_Close(fd); +} \ No newline at end of file diff --git a/src/save.h b/src/save.h new file mode 100644 index 0000000..f4dd4bc --- /dev/null +++ b/src/save.h @@ -0,0 +1,12 @@ +#ifndef _SAVE_H +#define _SAVE_H + +#include "core.h" + +// read_save : read or create a new save +void read_save(struct game *current_game); + +// write_save : write the savefile +void write_save(const struct game *current_game); + +#endif /* _SAVE_H */ \ No newline at end of file