~ubuntu-branches/ubuntu/dapper/xbill/dapper

« back to all changes in this revision

Viewing changes to Scorelist.c

  • Committer: Bazaar Package Importer
  • Author(s): Adrian Bridgett
  • Date: 2004-09-07 09:52:14 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20040907095214-d0m46ojc8f7upuqm
Tags: 2.1-4
maintainer upload of NMU - many thanks Tollef (closes: #268885) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <stdlib.h>
 
3
#include <string.h>
 
4
 
 
5
#include "Scorelist.h"
 
6
#include "UI.h"
 
7
 
 
8
#define NAMELEN 20
 
9
#define SCORES 10
 
10
 
 
11
typedef struct Score {
 
12
        char name[NAMELEN + 1];
 
13
        int level;
 
14
        int score;
 
15
} Score;
 
16
 
 
17
static Score scores[SCORES];
 
18
 
 
19
void
 
20
Scorelist_read() {
 
21
        FILE *scorefile = fopen(SCOREFILE, "r");
 
22
        int i;
 
23
 
 
24
        if (scorefile != NULL) {
 
25
                for (i = 0; i < SCORES; i++)
 
26
                        fscanf(scorefile, "%20s%d%d\n", scores[i].name,
 
27
                               &scores[i].level, &scores[i].score);
 
28
                fclose(scorefile);
 
29
        }
 
30
        else {
 
31
                for (i = 0; i < SCORES; i++) {
 
32
                        strcpy(scores[i].name, "Anonymous");
 
33
                        scores[i].level = 0;
 
34
                        scores[i].score = 0;
 
35
                }
 
36
        }
 
37
}
 
38
 
 
39
void
 
40
Scorelist_write() {
 
41
        FILE *scorefile = fopen(SCOREFILE, "w");
 
42
        int i;
 
43
        if (scorefile == NULL)
 
44
                return;
 
45
        for (i = 0; i < SCORES; i++)
 
46
                fprintf(scorefile, "%-*s %d %d\n", NAMELEN,
 
47
                        scores[i].name, scores[i].level, scores[i].score);
 
48
        fclose(scorefile);
 
49
}
 
50
 
 
51
/*  Add new high score to list   */
 
52
void
 
53
Scorelist_recalc(const char *str, int level, int score) {
 
54
        int i;
 
55
        char tname[NAMELEN + 1];
 
56
        char *nl;
 
57
 
 
58
        if (scores[SCORES - 1].score >= score)
 
59
                return;
 
60
        for (i = SCORES - 1; i > 0; i--) {
 
61
                if (scores[i - 1].score < score) {
 
62
                        strcpy (scores[i].name, scores[i - 1].name);
 
63
                        scores[i].level = scores[i - 1].level;
 
64
                        scores[i].score = scores[i - 1].score;
 
65
                }
 
66
                else
 
67
                        break;
 
68
        }
 
69
 
 
70
        memset(tname, 0, sizeof(tname));
 
71
        if (str == NULL || str[0] == 0)
 
72
                strcpy(tname, "Anonymous");
 
73
        strncpy(tname, str, sizeof(tname) - 1);
 
74
        nl = strchr(tname,'\n');
 
75
        if (nl != NULL)
 
76
                *nl = 0;
 
77
        
 
78
        strcpy(scores[i].name, tname);
 
79
        scores[i].level = level;
 
80
        scores[i].score = score;
 
81
}
 
82
 
 
83
void
 
84
Scorelist_update() {
 
85
        char str[500];
 
86
        int i;
 
87
        sprintf(str, "%s\n\n", "High Scores:");
 
88
        sprintf(str, "%s%-*s %6s %7s\n", str, NAMELEN,
 
89
                "Name", "Level", "Score");
 
90
        for (i = 0; i < SCORES; i++) {
 
91
                sprintf(str, "%s%-*s %6d %7d\n", str, NAMELEN,
 
92
                        scores[i].name, scores[i].level, scores[i].score);
 
93
        }
 
94
        UI_update_dialog(DIALOG_HIGHSCORE, str);
 
95
}
 
96
 
 
97
int
 
98
Scorelist_ishighscore(int val) {
 
99
        return (val > scores[SCORES - 1].score);
 
100
}