-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLOADSAVE.CPP
143 lines (125 loc) · 3.35 KB
/
LOADSAVE.CPP
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
#include "r136.h"
void ReportSaveFailure(FILE *fp, const char *fpath)
{
textcolor(YELLOW);
cputs("Fout bij wegschrijven status.");
textcolor(LIGHTGRAY);
cputs("\r\n\r\nStatus niet opgeslagen!\r\n");
fclose(fp);
remove(fpath);
}
void SaveStatus(Progdata &progdata)
{
FILE *fp;
char fpath[255];
int i;
memset(fpath, 0, 255);
strcat(strncpy(fpath, _argv[0], int(strrchr(_argv[0], '\\') - _argv[0]) + 1), "data.rip");
textcolor(YELLOW);
cputs("\r\n\r\nWil je je huidige status opslaan? ");
if (tolower(agetchar("jJnN")) != 'j')
{
cprintf("\r\n");
return;
}
while ((fp = fopen(fpath, "wb")) == NULL)
{
textcolor(YELLOW);
cputs("\r\n\r\nKon het save-bestand niet openen voor schrijven. Nogmaals proberen? ");
if (tolower(agetchar("jJnN")) != 'j')
{
cputs("\r\n\r\nStatus niet opgeslagen!\r\n");
remove(fpath);
return;
}
}
cprintf("\r\n");
for (i = 0; i < 25; i++)
if (fwrite(&progdata.items[i].room, sizeof(char), 1, fp) < 1)
{
ReportSaveFailure(fp, fpath);
return;
}
for (i = 0; i < 80; i++)
if (fwrite(progdata.rooms[i].connect, sizeof(char) * 6, 1, fp) < 1)
{
ReportSaveFailure(fp, fpath);
return;
}
for (i = 0; i < 21; i++)
if (fwrite(&progdata.living[i], sizeof(Living), 1, fp) < 1)
{
ReportSaveFailure(fp, fpath);
return;
}
if (fwrite(progdata.owneditems, sizeof(char), 10, fp) < 10)
{
ReportSaveFailure(fp, fpath);
return;
}
if (fwrite(&progdata.status, sizeof(Status), 1, fp) < 1)
{
ReportSaveFailure(fp, fpath);
return;
}
fclose(fp);
}
bool ReportLoadFailure(Progdata &progdata, FILE *fp, const char *fpath)
{
textcolor(YELLOW);
cputs("Fout bij lezen status.");
textcolor(LIGHTGRAY);
cputs("\r\n\r\nJe start een nieuw spel. ");
fclose(fp);
remove(fpath);
textcolor(YELLOW);
cputs("Druk op een toets om te beginnen...");
textcolor(LIGHTGRAY);
getch();
cputs("\r\n\r\n");
Initialize(progdata);
return false;
}
bool LoadStatus(Progdata &progdata)
{
FILE *fp;
char fpath[255];
int i;
memset(fpath, 0, 255);
strcat(strncpy(fpath, _argv[0], int(strrchr(_argv[0], '\\') - _argv[0]) + 1), "data.rip");
if ((fp = fopen(fpath, "rb")) == NULL)
{
textcolor(YELLOW);
PrintCentered("Druk op een toets om te beginnen...");
textcolor(LIGHTGRAY);
getch();
return false;
}
textcolor(YELLOW);
PrintCentered("Toets 1 voor een nieuw spel, 2 voor een gesaved spel: ", 1);
if (agetchar("12") != '2')
{
fclose(fp);
remove(fpath);
return false;
}
cputs("\r\n");
for (i = 0; i < 25; i++)
if (fread(&progdata.items[i].room, sizeof(char), 1, fp) < 1)
ReportLoadFailure(progdata, fp, fpath);
for (i = 0; i < 80; i++)
if (fread(progdata.rooms[i].connect, sizeof(char) * 6, 1, fp) < 1)
ReportLoadFailure(progdata, fp, fpath);
for (i = 0; i < 21; i++)
if (fread(&progdata.living[i], sizeof(Living), 1, fp) < 1)
ReportLoadFailure(progdata, fp, fpath);
if (fread(progdata.owneditems, sizeof(char), 10, fp) < 10)
ReportLoadFailure(progdata, fp, fpath);
if (fread(&progdata.status, sizeof(Status), 1, fp) < 1)
ReportLoadFailure(progdata, fp, fpath);
cputs("\r\n");
fclose(fp);
if (progdata.living[BOOM].status == 2)
ApplySimmeringForest(progdata);
return true;
}