aboutsummaryrefslogtreecommitdiff
path: root/scores.c
blob: f8406920974dd93357de656bc5fd35e522f7f182 (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
#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 scorebuf[8];

    filename = get_hi_score_filepath(fullpath);
    fp = fopen(filename, "r");
    if(fp != NULL) {
        for(i=0;i<10;i++) {
            memset(scorebuf, 0, 8);
            fread(scores[i].initials, 1, 3, fp);
            fgets(scorebuf, 8, fp);
            scores[i].score = atoi(scorebuf);
        }
        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;
}