aboutsummaryrefslogtreecommitdiff
path: root/scores.c
blob: aa3975c3fbf6c4f2769eb287009dc3dfecd92d34 (plain)
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
#include <stdio.h>

#ifdef __GNUC__

#include <gem.h>

#define HIWORD(x)  ((int16_t)((uint32_t)x >> 16))
#define LOWORD(x)  ((int16_t)((uint32_t)x & 0xFFFF)) 

#else

#include <portab.h>
#include <aes.h>
#include <vdi.h>

#endif /* __GNUC__ */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef PCGEM
#include "wormpc.h"
#else
#include "wormst.h"
#endif

#define SCOREFILE   "worm.hi"

struct score {
    char initials[4];
    int score;
};

char *defaults[10] = {
    "JBA",
    "GEM",
    "TOS",
    "DOS",
    "68K",
    "X86",
    "AES",
    "VDI",
    "WRM",
    "MGX" };

static struct score scores[10];

char *get_hi_score_filepath(const char *fullpath)
{
char *scorepath;
char *tmp;
size_t fpl;

    if(fullpath != NULL)
        fpl = strlen(fullpath) + strlen(SCOREFILE); /* fine... */
    else
        fpl = strlen(SCOREFILE);
    scorepath = (char *)malloc(fpl);
    
    if(fullpath != NULL) {
        strcpy(scorepath, fullpath);
        tmp = strrchr(scorepath, '\\');
        if(tmp == NULL)
            tmp = strrchr(scorepath, '/');
        if(tmp == NULL)
            tmp = scorepath;
        else
            tmp++;    
        tmp[0] = '\0';
        
    } else
        scorepath[0] = '\0';
        
    strcat(scorepath, SCOREFILE);
    
    return scorepath;
}

void load_scores(const char *fullpath)
{
char *filename;
FILE *fp;
int i;
char linebuf[16];

    filename = get_hi_score_filepath(fullpath);
    fp = fopen(filename, "r");
    if(fp != NULL) {
        for(i=0;i<10;i++) {
            fgets(linebuf, 16, fp);
            memcpy(scores[i].initials, linebuf, 3);
            scores[i].initials[3] = '\0';
            scores[i].score = atoi(&linebuf[3]);
        }
        scores[i].initials[3] = '\0';
        fclose(fp);
    } else {
        for(i=0;i<10;i++) {
            memcpy(scores[i].initials, defaults[i], 4);
            scores[9-i].score = i == 0 ? 10 : (i * 500 + 100);
        }
    }
    free(filename);
}

void save_scores(const char *fullpath)
{
char *filename;
FILE *fp;
int i;

    filename = get_hi_score_filepath(fullpath);
    fp = fopen(filename, "w");
    if(fp == NULL) return;
    for(i=0;i<10;i++) {
        fwrite(scores[i].initials, 1, 3, fp);
        fprintf(fp, " %6d\n", scores[i].score);
    }
    fclose(fp);
    free(filename);
}

char *get_score_at(int pos, int *score)
{
    *score = scores[pos].score;
    return scores[pos].initials;
}

int is_high_score(int score)
{
    return (score > scores[9].score);
}

void add_high_score(const char *initials, int score)
{
int i,j;

    for(i=0;i<10;i++) {
        if(score > scores[i].score) {
            for(j=8;j>=i;j--) {
                scores[j+1].score = scores[j].score;
                memcpy(scores[j+1].initials, scores[j].initials, 3);
            }
            scores[i].score = score;
            memcpy(scores[i].initials, initials, 3);
            break;
        }
    }
}