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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "config.h"
#include "rockdodger_globals.h"
#include "highscore_io.h"
#include "SFont.h"
// High score table
struct highscore high[EDITION_MAXIMUM_EDITION][MAXIMUM_HIGH_SCORE_ENTRIES] = {
{
{13000,"Pad",2,0},
{12500,"Pad",2,0},
{6500,"Pad",1,0},
{5000,"Pad",1,0},
{3000,"Pad",1,0},
{2500,"Pad",1,0},
{2000,"Pad",1,0},
{1500,"Pad",1,0}
}, {
{4000, "Pad", 5, 0},
{3200, "Pad", 4, 0},
{2600, "Pad", 3, 0},
{2200, "Pad", 3, 0},
{1800, "RPK", 2, 0},
{1500, "Pad", 2, 0},
{1200, "RPK", 2, 0},
{900, "RPK", 1, 0}
}
};
long score = 0;
int scorerank = -1;
char name_input_buf[512];
static const char *HIGHSCORE_HEADER_FORMAT = "rockdodger highscore %x\n";
static const int HIGHSCORE_VERSION = 5;
static const char *HIGHSCORE_EDITION_FORMAT = "edition %d\n";
FILE *hs_fopen(const char *mode) {
mode_t mask;
char s[1024];
FILE *f = NULL;
s[sizeof(s) - 1] = '\0';
snprintf(s, sizeof(s) - 1, "%s/rockdodger.scores", GAMESDIR);
mask = umask(0111);
if((f = fopen(s, mode)) == NULL) {
umask(0117);
snprintf(s, sizeof(s) - 1, "%s/.rockdodger_high", getenv("HOME"));
if((f = fopen(s, mode)) == NULL) {
fprintf(stderr, "Could not open highscore file '%s', mode '%s'!\n", s,
mode);
}
}
umask(mask);
return f;
}
void read_high_score_table() {
FILE *f;
int i, giveup = 0;
int edition = 0;
int got_edition = 1;
if((f = hs_fopen("r")) != NULL) {
// If the file exists, read from it
if(fscanf(f, HIGHSCORE_HEADER_FORMAT, &i) == 1) {
#ifdef DEBUG
printf(HIGHSCORE_HEADER_FORMAT, i);
#endif
if(i == HIGHSCORE_VERSION) {
do {
#ifdef DEBUG
char debugbuf[889];
long pos = ftell(f);
if(fgets(debugbuf, sizeof(debugbuf), f) == NULL) {
perror("fgets in read_high_score_table()");
break; //the loop
}
printf("next line pos=%04lX '%s'\n", pos, debugbuf);
fseek(f, pos, SEEK_SET);
#endif
i = fscanf(f, HIGHSCORE_EDITION_FORMAT, &edition);
#ifdef DEBUG
printf("i=%d: ", i);
printf(HIGHSCORE_EDITION_FORMAT, edition);
#endif
if(i == 1) /* read OK */ {
giveup = 0;
if(edition < 0 || edition >= EDITION_MAXIMUM_EDITION) {
fprintf(stderr, "Illegal edition $%X at pos $%lX encountered!\n", edition, ftell(f));
/* No gurus yet, what a
shame... guru_meditation(GM_FLAGS_RECOVERY |
GM_FLAGS_CHOICE | GM_FLAGS_ABORTIFY, GM_SS_Misc |
GM_GE_BadParm | GURU_SEC_highscore_io,
(void*)(0x01000000 | (edition & 0xffffff)));
*/
continue;
}
} else /* none read, or error */ {
got_edition = 0;
giveup = 1;
}
for(i = 0; i < MAXIMUM_HIGH_SCORE_ENTRIES && !giveup; ++i) {
char s[1024];
long int highscore;
int scanret;
if(high[edition][i].allocated) {
free(high[edition][i].name);
high[edition][i].allocated = 0;
}
scanret = fscanf(f, "%ld %1023[^\n]", &highscore, s);
#ifdef DEBUG
printf("i=%d scanret=%d pos=%04lX highscore=%5ld '%s'\n", i, scanret, ftell(f), highscore, s);
#endif
switch (scanret) {
case 2:
high[edition][i].name = strdup(s);
high[edition][i].score = highscore;
high[edition][i].allocated = 1;
break;
case 1:
high[edition][i].name = "";
high[edition][i].score = highscore;
high[edition][i].allocated = 0;
break;
default:
giveup = 1;
break;
}
}
/* This is needed as the 'scanret = fscanf(f, "%ld
%1023[^\n]", &highscore, s)' reads until a LF is
encountered. *But* this LF is *not* consumed so, in order
of the rest of the loop ('i = fscanf(f,
HIGHSCORE_EDITION_FORMAT, &edition);') to work correctly,
we have to skip the LF. Stupid of me to forget this and
to do a lot of debugging and debug print output. Phew! */
i = fgetc(f);
} while(got_edition && i != EOF);
}
}
fclose(f);
}
}
void write_high_score_table() {
FILE *f;
int edition, i;
if((f = hs_fopen("w")) != NULL) {
// If the file exists, write to it
fprintf(f, HIGHSCORE_HEADER_FORMAT, HIGHSCORE_VERSION);
for(edition = 0; edition < EDITION_MAXIMUM_EDITION; ++edition) {
fprintf(f, HIGHSCORE_EDITION_FORMAT, edition);
for(i = 0; i < MAXIMUM_HIGH_SCORE_ENTRIES; ++i) {
fprintf(f, "%ld %s\n", high[edition][i].score, high[edition][i].name);
}
}
fclose(f);
}
/* #ifdef DEBUG */
/* FILE *debugfile = fopen("$HIGHSCORE:MEMORY", "w"); */
/* if(debugfile) { */
/* fwrite(high, sizeof(name_input_buf), 1, debugfile); */
/* fseek(debugfile, 0x1000, SEEK_SET); */
/* fwrite(high, sizeof(struct highscore), MAXIMUM_HIGH_SCORE_ENTRIES, debugfile); */
/* for(i = 0; i < MAXIMUM_HIGH_SCORE_ENTRIES; ++i) { */
/* fseek(debugfile, 0x2000 + 0x0100 * i, SEEK_SET); */
/* fwrite(high[i].name, sizeof(char), 0x0100, debugfile); */
/* } */
/* fclose(debugfile); */
/* } */
/* #endif */
}
void inc_score(int x, int y, long dscore) {
score += dscore;
}
long clear_score(void) {
long old = score;
score = 0.0;
return old;
}
void game_over() {
int i;
clearBuffer();
state_timeout = 5.0e6;
if(score >= high[current_edition][MAXIMUM_HIGH_SCORE_ENTRIES - 1].score) {
// Read the high score table from the storage file
read_high_score_table();
// Find ranking of this score, store as scorerank
for(i = 0; i < MAXIMUM_HIGH_SCORE_ENTRIES; ++i) {
if(high[current_edition][i].score <= score) {
scorerank = i;
break;
}
}
// Lose the lowest name forever (loser!)
if(high[current_edition][MAXIMUM_HIGH_SCORE_ENTRIES - 1].allocated) {
high[current_edition][MAXIMUM_HIGH_SCORE_ENTRIES - 1].allocated = 0;
free(high[current_edition][MAXIMUM_HIGH_SCORE_ENTRIES - 1].name);
}
// Move all lower scores down a notch
for(i = MAXIMUM_HIGH_SCORE_ENTRIES - 1; i > scorerank; --i) {
high[current_edition][i] = high[current_edition][i - 1];
}
// Insert blank high score
high[current_edition][scorerank].score = score;
high[current_edition][scorerank].name = "";
high[current_edition][scorerank].allocated = 0;
}
}
|