1 #include "globals.h"
2 #include "score.h"
3 #include "menu.h"
4 #include "object.h"
5 #include "ship.h"
6 
7 
8 HighScore HighScores[NUM_HIGHSCORES];
9 char HighScoreName[MAX_PLAYERNAME_LENGTH];
10 int Score;
11 char ScoreFileName[MAX_FILENAME_SIZE];
12 
13 
14 void InitHighScores();
15 int CheckForHighScore();
16 void AddHighScore(char *name);
17 void ReadHighScores();
18 void SaveHighScores();
19 
20 
ClearHighScores()21 void ClearHighScores()
22 {
23   InitHighScores();
24   SaveHighScores();
25   ScoreMenu();
26 }
27 
28 
InitHighScores()29 void InitHighScores()
30 {
31   int i;
32 
33   for (i=0; i<NUM_HIGHSCORES; i++) {
34     HighScores[i].Score=0;
35     HighScores[i].Name[0]=0;
36   }
37 }
38 
39 
CheckForHighScore()40 int CheckForHighScore()
41 {
42   int i;
43 
44   for (i=0; i<NUM_HIGHSCORES; i++) {
45     if (Ships[myShip]->Score > HighScores[i].Score) {
46       return(1);
47     }
48   }
49   return(0);
50 }
51 
52 
AddHighScore(char * name)53 void AddHighScore(char *name)
54 {
55   int i, j;
56 
57   for (i=0; i<NUM_HIGHSCORES; i++) {
58     if (Ships[myShip]->Score > HighScores[i].Score) {
59       for (j=NUM_HIGHSCORES-1; j>i; j--) {
60         HighScores[j].Score=HighScores[j-1].Score;
61         strcpy(HighScores[j].Name, HighScores[j-1].Name);
62       }
63       HighScores[i].Score=Ships[myShip]->Score;
64       strcpy(HighScores[i].Name, name);
65       break;
66     }
67   }
68   SaveHighScores();
69 }
70 
71 
ReadHighScores()72 void ReadHighScores()
73 {
74   int i;
75   char score[10];
76   char name[MAX_PLAYERNAME_LENGTH];
77   char *c;
78   FILE *HighScoreFile;
79 
80   if (!(HighScoreFile=fopen(ScoreFileName, "r"))) {
81     return;
82   }
83 
84   for (i=0; i<NUM_HIGHSCORES; i++) {
85     c=score;
86     while((*c=fgetc(HighScoreFile))!=' ') {
87       if (*c==EOF) {
88 	fclose(HighScoreFile);
89 	return;
90       }
91       c++;
92     }
93     *c=0;
94 
95     c=name;
96     while((*c=fgetc(HighScoreFile))!='\n') {
97       if (*c==EOF) {
98 	fclose(HighScoreFile);
99 	return;
100       }
101       c++;
102     }
103     *c=0;
104     HighScores[i].Score=atoi(score);
105     strcpy(HighScores[i].Name, name);
106   }
107   fclose(HighScoreFile);
108 }
109 
110 
SaveHighScores()111 void SaveHighScores()
112 {
113   int i;
114   FILE *HighScoreFile;
115 
116   HighScoreFile=fopen(ScoreFileName, "w");
117 
118   for (i=0; i<NUM_HIGHSCORES; i++) {
119     fprintf(HighScoreFile, "%d %s\n", HighScores[i].Score, HighScores[i].Name);
120   }
121   fclose(HighScoreFile);
122 }
123