diff --git a/CMakeLists.txt b/CMakeLists.txt index ecefb06..fd1837b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ set(SOURCES src/display.c src/menus.c src/calccity.c + src/save.c ) set(ASSETS) diff --git a/src/core.c b/src/core.c index a9cb754..dbae000 100644 --- a/src/core.c +++ b/src/core.c @@ -105,6 +105,7 @@ void next_step(struct calccity *calccity, struct map *map) if (!(calccity->tick % calccity->time_speed)) { calccity->month ++; + if (calccity->month > 12) { update_stat(calccity, map); @@ -112,46 +113,46 @@ void next_step(struct calccity *calccity, struct map *map) calccity->month = 1; calccity->year ++; } - } - if (calccity->disaster) - { - // Firehazard - if (calccity->stat[7] > 500 && rand() % 1000 == 0) + if (calccity->disaster) { - display_message("UN INCENDIE A RAVAGE VOTRE VILLE."); - disaster(calccity, map, calccity->stat[7]); - update_stat(calccity, map); - } - - // Nuclear disaster - if (calccity->stat[8] >= 1 && rand() % 10000 == 0) - { - display_message("UN ACCIDENT NUCLEAIRE A RAVAGE VOTRE VILLE."); - disaster(calccity, map, calccity->stat[8] * 100); - update_stat(calccity, map); - } - - // Earthquake - if (rand() % 10000 == 0) - { - display_message("UN TREMBLEMENT DE TERRE A RAVAGE VOTRE VILLE."); - disaster(calccity, map, rand() % 10); - update_stat(calccity, map); - } - - // Treasure - if (calccity->misc[0] > 1000 && rand() % 1000 == 0) - { - if (rand() % 2) + // Firehazard + if (calccity->stat[7] > 500 && rand() % 1000 == 0) { - display_message("DES INVESTISSEMENT AUDACIEUX VOUS ONT RAPPORTES 1000$ !"); - calccity->misc[0] += 1000; + display_message("UN INCENDIE A RAVAGE VOTRE VILLE."); + disaster(calccity, map, calccity->stat[7]); + update_stat(calccity, map); } - else + + // Nuclear disaster + if (calccity->stat[8] >= 1 && rand() % 10000 == 0) { - display_message("DES INVESTISSEMENT FOIREUX VOUS ONT FAIT PERDRE 1000$."); - calccity->misc[0] -= 1000; + display_message("UN ACCIDENT NUCLEAIRE A RAVAGE VOTRE VILLE."); + disaster(calccity, map, calccity->stat[8] * 100); + update_stat(calccity, map); + } + + // Earthquake + if (rand() % 10000 == 0) + { + display_message("UN TREMBLEMENT DE TERRE A RAVAGE VOTRE VILLE."); + disaster(calccity, map, rand() % 10); + update_stat(calccity, map); + } + + // Treasure + if (calccity->misc[0] > 1000 && rand() % 1000 == 0) + { + if (rand() % 2) + { + display_message("DES INVESTISSEMENT AUDACIEUX VOUS ONT RAPPORTES 1000$ !"); + calccity->misc[0] += 1000; + } + else + { + display_message("DES INVESTISSEMENT FOIREUX VOUS ONT FAIT PERDRE 1000$."); + calccity->misc[0] -= 1000; + } } } } @@ -274,7 +275,6 @@ void main_loop(struct calccity *calccity, struct camera *camera, struct map *map } } - // Free timer if (t >= 0) timer_stop(t); } diff --git a/src/core.h b/src/core.h index 391cc98..7e64eb5 100644 --- a/src/core.h +++ b/src/core.h @@ -1,6 +1,7 @@ #ifndef _CORE_H #define _CORE_H +#include #include "calccity.h" #include "display.h" #include "menus.h" diff --git a/src/main.c b/src/main.c index d2d7a95..efc85ea 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,7 @@ #include +#include #include "core.h" +#include "save.h" int main(void) @@ -13,8 +15,13 @@ int main(void) struct calccity calccity; struct camera camera; struct map map; - default_values(&calccity, &camera, &map); + gint_world_switch(GINT_CALL(read_save, (void *)&calccity, (void *)&camera, (void *)&map)); + // Game main_loop(&calccity, &camera, &map); + + // Backup data + gint_world_switch(GINT_CALL(write_save, (void *)&calccity, (void *)&camera, (void *)&map)); + return 1; } \ No newline at end of file diff --git a/src/save.c b/src/save.c new file mode 100644 index 0000000..e14dfd9 --- /dev/null +++ b/src/save.c @@ -0,0 +1,41 @@ +#include + +#include "save.h" + + +const char *filename = "/CalcCity.sav"; + + +void read_save(struct calccity *calccity, struct camera *camera, struct map *map) +{ + FILE *fp; + fp = fopen(filename, "rb"); + + // If the backup doesn't exist + if (fp == NULL) + default_values(calccity, camera, map); + else + { + fread(calccity, 1, sizeof *calccity, fp); + fread(camera, 1, sizeof *camera, fp); + fread(map, 1, sizeof *map, fp); + + fclose(fp); + } +} + + +void write_save(const struct calccity *calccity, const struct camera *camera, const struct map *map) +{ + FILE *fp; + fp = fopen(filename, "wb"); + + if (fp != NULL) + { + fwrite(calccity, sizeof *calccity, 1, fp); + fwrite(camera, sizeof *camera, 1, fp); + fwrite(map, sizeof *map, 1, fp); + + fclose(fp); + } +} diff --git a/src/save.h b/src/save.h new file mode 100644 index 0000000..c412fea --- /dev/null +++ b/src/save.h @@ -0,0 +1,14 @@ +#ifndef _SAVE_H +#define _SAVE_H + +#include "core.h" +#include "calccity.h" + + +// read_save : get the save if exists; else returns default values +void read_save(struct calccity *calccity, struct camera *camera, struct map *map); + +// write_save : save the game into savefile +void write_save(const struct calccity *calccity, const struct camera *camera, const struct map *map); + +#endif /* _SAVE_H */ \ No newline at end of file