Mercurial > hg > early-roguelike
comparison srogue/rip.c @ 279:d3968e9cb98d
Use C stdio functions for score files and save files.
Switching from Unix file descriptor operations to C standard FILE*
functions will reduce portability problems.
author | John "Elwin" Edwards |
---|---|
date | Fri, 15 Sep 2017 19:57:54 -0400 |
parents | 94a0d9dd5ce1 |
children | 70aa5808c782 |
comparison
equal
deleted
inserted
replaced
278:c222f9d56776 | 279:d3968e9cb98d |
---|---|
45 " ________)\\\\//\\//\\)/\\//\\)/\\//\\)/\\//\\)/\\//\\//(________", | 45 " ________)\\\\//\\//\\)/\\//\\)/\\//\\)/\\//\\)/\\//\\//(________", |
46 }; | 46 }; |
47 | 47 |
48 #define RIP_LINES (sizeof rip / (sizeof (char *))) | 48 #define RIP_LINES (sizeof rip / (sizeof (char *))) |
49 | 49 |
50 extern int scorefd; | 50 extern FILE *scoreboard; |
51 extern FILE *logfile; | 51 extern FILE *logfile; |
52 | 52 |
53 char *killname(unsigned char monst); | 53 char *killname(unsigned char monst); |
54 void showpack(bool winner, char *howso); | 54 void showpack(bool winner, char *howso); |
55 | 55 |
121 */ | 121 */ |
122 void | 122 void |
123 score(int amount, int aflag, char monst) | 123 score(int amount, int aflag, char monst) |
124 { | 124 { |
125 reg struct sc_ent *scp, *sc2; | 125 reg struct sc_ent *scp, *sc2; |
126 reg int i, fd, prflags = 0; | 126 reg int i, prflags = 0; |
127 reg FILE *outf; | |
128 char *packend; | 127 char *packend; |
129 | 128 |
130 md_onsignal_exit(); | 129 md_onsignal_exit(); |
131 if (aflag != WINNER) { | 130 if (aflag != WINNER) { |
132 if (aflag == CHICKEN) | 131 if (aflag == CHICKEN) |
140 showpack(FALSE, packend); | 139 showpack(FALSE, packend); |
141 } | 140 } |
142 /* | 141 /* |
143 * Open file and read list | 142 * Open file and read list |
144 */ | 143 */ |
145 if ((fd = scorefd) < 0) | 144 if (scoreboard == NULL) |
146 return; | 145 return; |
147 outf = (FILE *) md_fdopen(fd, "w"); | |
148 for (scp = top_ten; scp <= &top_ten[9]; scp++) { | 146 for (scp = top_ten; scp <= &top_ten[9]; scp++) { |
149 scp->sc_score = 0; | 147 scp->sc_score = 0; |
150 for (i = 0; i < 80; i++) | 148 for (i = 0; i < 80; i++) |
151 scp->sc_name[i] = rnd(255); | 149 scp->sc_name[i] = rnd(255); |
152 scp->sc_flags = rnd(255); | 150 scp->sc_flags = rnd(255); |
163 prflags = 1; | 161 prflags = 1; |
164 for(i = 0; i < 10; i++) | 162 for(i = 0; i < 10; i++) |
165 { | 163 { |
166 unsigned int mon; | 164 unsigned int mon; |
167 | 165 |
168 encread((char *) &top_ten[i].sc_name, LINLEN, fd); | 166 encread((char *) &top_ten[i].sc_name, LINLEN, scoreboard); |
169 encread((char *) scoreline, 100, fd); | 167 encread((char *) scoreline, 100, scoreboard); |
170 sscanf(scoreline, " %d %d %d %d %u %d %ld %lx \n", | 168 sscanf(scoreline, " %d %d %d %d %u %d %ld %lx \n", |
171 &top_ten[i].sc_score, &top_ten[i].sc_flags, | 169 &top_ten[i].sc_score, &top_ten[i].sc_flags, |
172 &top_ten[i].sc_level, &top_ten[i].sc_uid, | 170 &top_ten[i].sc_level, &top_ten[i].sc_uid, |
173 &mon, &top_ten[i].sc_explvl, | 171 &mon, &top_ten[i].sc_explvl, |
174 &top_ten[i].sc_exppts, &top_ten[i].sc_date); | 172 &top_ten[i].sc_exppts, &top_ten[i].sc_date); |
197 scp->sc_exppts = him->s_exp; | 195 scp->sc_exppts = him->s_exp; |
198 time(&scp->sc_date); | 196 time(&scp->sc_date); |
199 } | 197 } |
200 } | 198 } |
201 ignore(); | 199 ignore(); |
202 fseek(outf, 0L, 0); | 200 fseek(scoreboard, 0L, 0); |
203 for(i = 0; i < 10; i++) | 201 for(i = 0; i < 10; i++) |
204 { | 202 { |
205 memset(scoreline,0,100); | 203 memset(scoreline,0,100); |
206 encwrite((char *) top_ten[i].sc_name, LINLEN, outf); | 204 encwrite((char *) top_ten[i].sc_name, LINLEN, scoreboard); |
207 sprintf(scoreline, " %d %d %d %d %u %d %ld %lx \n", | 205 sprintf(scoreline, " %d %d %d %d %u %d %ld %lx \n", |
208 top_ten[i].sc_score, top_ten[i].sc_flags, | 206 top_ten[i].sc_score, top_ten[i].sc_flags, |
209 top_ten[i].sc_level, top_ten[i].sc_uid, | 207 top_ten[i].sc_level, top_ten[i].sc_uid, |
210 top_ten[i].sc_monster, top_ten[i].sc_explvl, | 208 top_ten[i].sc_monster, top_ten[i].sc_explvl, |
211 top_ten[i].sc_exppts, top_ten[i].sc_date); | 209 top_ten[i].sc_exppts, top_ten[i].sc_date); |
212 encwrite((char *) scoreline, 100, outf); | 210 encwrite((char *) scoreline, 100, scoreboard); |
213 } | 211 } |
214 fclose(outf); | 212 fclose(scoreboard); |
215 md_onsignal_exit(); | 213 md_onsignal_exit(); |
216 clear(); | 214 clear(); |
217 refresh(); | 215 refresh(); |
218 endwin(); | 216 endwin(); |
219 showtop(prflags); /* print top ten list */ | 217 showtop(prflags); /* print top ten list */ |
261 * Display the top ten on the screen | 259 * Display the top ten on the screen |
262 */ | 260 */ |
263 bool | 261 bool |
264 showtop(int showname) | 262 showtop(int showname) |
265 { | 263 { |
266 reg int fd, i; | 264 reg int i; |
267 char *killer; | 265 char *killer; |
268 struct sc_ent *scp; | 266 struct sc_ent *scp; |
269 | 267 FILE *score_rdonly; |
270 if ((fd = open(scorefile, O_RDONLY)) < 0) | 268 |
269 if ((score_rdonly = fopen(scorefile, "r")) == NULL) | |
271 return FALSE; | 270 return FALSE; |
272 | 271 |
273 for(i = 0; i < 10; i++) | 272 for(i = 0; i < 10; i++) |
274 { | 273 { |
275 unsigned int mon; | 274 unsigned int mon; |
276 encread((char *) &top_ten[i].sc_name, LINLEN, fd); | 275 encread((char *) &top_ten[i].sc_name, LINLEN, score_rdonly); |
277 encread((char *) scoreline, 100, fd); | 276 encread((char *) scoreline, 100, score_rdonly); |
278 sscanf(scoreline, " %d %d %d %d %u %d %ld %lx \n", | 277 sscanf(scoreline, " %d %d %d %d %u %d %ld %lx \n", |
279 &top_ten[i].sc_score, &top_ten[i].sc_flags, | 278 &top_ten[i].sc_score, &top_ten[i].sc_flags, |
280 &top_ten[i].sc_level, &top_ten[i].sc_uid, | 279 &top_ten[i].sc_level, &top_ten[i].sc_uid, |
281 &mon, &top_ten[i].sc_explvl, | 280 &mon, &top_ten[i].sc_explvl, |
282 &top_ten[i].sc_exppts, &top_ten[i].sc_date); | 281 &top_ten[i].sc_exppts, &top_ten[i].sc_date); |
283 top_ten[i].sc_monster = mon; | 282 top_ten[i].sc_monster = mon; |
284 } | 283 } |
285 close(fd); | 284 fclose(score_rdonly); |
286 printf("Top Ten Adventurers:\nRank\tScore\tName\n"); | 285 printf("Top Ten Adventurers:\nRank\tScore\tName\n"); |
287 for (scp = top_ten; scp <= &top_ten[9]; scp++) { | 286 for (scp = top_ten; scp <= &top_ten[9]; scp++) { |
288 if (scp->sc_score > 0) { | 287 if (scp->sc_score > 0) { |
289 printf("%d\t%d\t%s: %s\t\t--> %s on level %d", | 288 printf("%d\t%d\t%s: %s\t\t--> %s on level %d", |
290 scp - top_ten + 1, scp->sc_score, scp->sc_name, | 289 scp - top_ten + 1, scp->sc_score, scp->sc_name, |