Bug with backup gestion

This commit is contained in:
Shadow15510 2022-06-23 11:03:57 +02:00
parent e0a1b58ed9
commit d716218997
6 changed files with 101 additions and 37 deletions

View File

@ -15,6 +15,7 @@ set(SOURCES
src/display.c
src/menus.c
src/calccity.c
src/save.c
)
set(ASSETS)

View File

@ -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);
}

View File

@ -1,6 +1,7 @@
#ifndef _CORE_H
#define _CORE_H
#include <stdbool.h>
#include "calccity.h"
#include "display.h"
#include "menus.h"

View File

@ -1,5 +1,7 @@
#include <gint/display.h>
#include <gint/gint.h>
#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;
}

41
src/save.c Normal file
View File

@ -0,0 +1,41 @@
#include <stdio.h>
#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);
}
}

14
src/save.h Normal file
View File

@ -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 */