[7f9531]: score.c  Maximize  Restore  History

Download this file

236 lines (196 with data), 6.8 kB

  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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* Asteroids3D - a first person game of blowing up asteroids
* Copyright (C) 2000 Stuart Mark Pomerantz <smp [at] psc edu>
* Copyright Š Jan Engelhardt <jengelh [at] linux01 gwdg de>, 2003 - 2005
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Stuart Pomerantz
* 3935 Stonecliffe drive
* Monroeville, PA 15146
*/
#include <GL/glut.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "asteroids3D.h"
#include "vecops.h"
#define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH)
#define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH)
// This stuff is for the score file
#define MAX_FIELD_LEN 64
#define MAX_LINE_LEN 256
#define SCORE_FILE "/tmp/asteroids-scores"
#define NUM_SCORES 10
#define SCORE_FILE_SEPARATOR_CHAR ','
#define SCORE_FILE_COMMENT_CHAR '#'
#define SCORE_FILE_ESCAPE_CHAR '\\'
typedef struct score_file_entry {
char name[MAX_FIELD_LEN], date[MAX_FIELD_LEN];
unsigned int score;
int new_score_flag; // this value isn't saved to disk
} ScoreFileEntry;
static int compare_scores(const void *, const void *);
static void read_score_file(void);
static void score_display(void);
static void score_keyboard_handler(unsigned char, int, int);
static void write_score_file(void);
// last but not least, what would a game be without a score
unsigned int score = 0;
double difficulty_multiplier = 1;
// Data structure to hold the scores read from the score file
static ScoreFileEntry score_entry[NUM_SCORES] = {};
void add_rock_to_score(const Asteroid *a) {
/* Scoring works like this:
*
* - small rocks are worth more than big ones
* - fast rocks are worth more than slow ones
*
* so I am doing (100/size)*speed <=> (100 * speed / size)
*
* Adding one below so I do not inadvertantly
* score a zero for a stationary or slow asertoid.
*/
score += 100.0 * (vec_length(&a->velocity) + 1) / (a->type + 1.0);
return;
}
static void read_score_file(void) {
char buf[MAX_LINE_LEN];
int i = 0;
FILE *fp;
if((fp = fopen(SCORE_FILE, "r")) == NULL)
return;
while(i < NUM_SCORES && fgets(buf, sizeof(buf), fp) != NULL) {
char *bp = buf, *res;
ScoreFileEntry *se;
strip_nl(bp);
if(*bp == '#' || *bp == '\0')
continue;
se = &score_entry[i++];
if((res = strsep(&bp, ",")) != NULL) {
strncpy(se->name, res, sizeof(se->name) - 1);
se->name[sizeof(se->name) - 1] = '\0';
}
if((res = strsep(&bp, ",")) != NULL)
se->score = strtol(res, NULL, 0);
if(bp != NULL) {
strncpy(se->date, bp, sizeof(se->date) - 1);
se->date[sizeof(se->date) - 1] = '\0';
}
se->new_score_flag = 0;
}
fclose(fp);
return;
}
static void write_score_file(void) {
FILE *fp;
int i;
if((fp = fopen(SCORE_FILE, "w")) == NULL) {
fprintf(stderr, "Could not open %s for writing: %s\n",
SCORE_FILE, strerror(errno));
return;
}
fprintf(fp,
"#\n"
"# asteroids score file\n"
"#\n"
"# This file is automatically generated and will be recreated if deleted.\n"
"#\n"
"# name,score,date\n"
);
for(i = 0; i < NUM_SCORES; ++i) {
ScoreFileEntry *se = &score_entry[i];
if(*se->name != '\0' && *se->date != '\0')
fprintf(fp, "%s,%u,%s\n", se->name, se->score, se->date);
}
fclose(fp);
chmod(SCORE_FILE, S_IRUGO | S_IWUGO);
return;
}
static int compare_scores(const void *pa, const void *pb) {
const ScoreFileEntry *a = pa, *b = pb;
return b->score - a->score;
}
void init_score_display(void) {
ScoreFileEntry *se;
time_t now;
char *user;
/* The player's score is scaled to reflect the difficulty of the game
starting with more asteroids gets a higher score than starting with fewer.
NUM_ASTEROIDS asteroids is the "base" score. The difficulty multiplier is
already calculated at game start so that we can display the correct score
already in-game. */
score *= difficulty_multiplier;
read_score_file();
glutDisplayFunc(score_display);
glutKeyboardFunc(score_keyboard_handler);
if(score <= score_entry[NUM_SCORES-1].score)
return;
se = &score_entry[NUM_SCORES-1];
if((user = getenv("USER")) != NULL)
strncpy(se->name, user, sizeof(se->name) - 1);
else
strncpy(se->name, "(unknown)", sizeof(se->name) - 1);
se->name[sizeof(se->name) - 1] = '\0';
se->score = score;
se->new_score_flag = 1;
now = time(NULL);
strftime(se->date, sizeof(se->date), "%B %d %Y, %H:%M", localtime(&now));
qsort(score_entry, NUM_SCORES, sizeof(ScoreFileEntry), compare_scores);
write_score_file();
return;
}
static void score_keyboard_handler(unsigned char key, int x, int y) {
if( key == keymap.end_game )
exit(0) ;
return ;
}
static void score_display(void) {
char buf[MAX_LINE_LEN];
double r, g, b, a;
int i;
/* do some error checking for each frame. */
glutReportErrors();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw_text_in_color(-0.2, 0.9, "Top Ten Scores", 0, 0, 1, 1);
for(i = 0; i < NUM_SCORES; ++i) {
ScoreFileEntry *se = &score_entry[i];
if(*se->name == '\0' || *se->date == '\0')
continue;
if(!se->new_score_flag) {
r = g = b = a = 1;
} else {
r = a = 1;
g = b = 0;
}
draw_text_in_color(-0.7, 0.7 - (double)i / NUM_SCORES,
se->name, r, g, b, a);
snprintf(buf, sizeof(buf), "%u", se->score);
draw_text_in_color( -0.2, 0.7 - (double)i / NUM_SCORES,
buf, r, g, b, a);
draw_text_in_color(0.2, 0.7 - (double)i / NUM_SCORES,
se->date, r, g, b, a);
}
key_char2str(keymap.end_game, buf, sizeof(buf));
strcat(buf, " key exits");
draw_text_in_color(-0.2, -0.7, buf, 0.5, 0.5, 0.5, 1);
glutSwapBuffers();
return;
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks