Skip to content

Commit 5071ecb

Browse files
committed
Add statistics.
1 parent 3802097 commit 5071ecb

File tree

11 files changed

+497
-282
lines changed

11 files changed

+497
-282
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ file(GLOB SOURCES src/thrust.c src/fast_gr.c src/hiscore.c src/conf.c src/thin
2424
datasrc/font.c
2525
datasrc/blip.c datasrc/boom.c datasrc/boom2.c datasrc/harp.c
2626
datasrc/engine.c datasrc/zero.c
27-
src/sdl.c src/sdlsound.c src/sdlkey.c
27+
src/sdl.c src/sdlsound.c src/sdlkey.c src/statistics.c
2828
)
2929

3030
Add_Executable (sdlthrust ${SOURCES})

Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ SRC = \
1717
src/init.c src/level.c src/font5x5.c src/graphics.c src/compat.h \
1818
src/conf.h src/fast_gr.h src/font5x5.h src/gr_drv.h src/graphics.h \
1919
src/hiscore.h src/init.h src/keyboard.h src/level.h src/options.h \
20-
src/sound.h src/soundIt.h src/things.h src/thrust.h src/thrust_t.h
20+
src/sound.h src/soundIt.h src/things.h src/thrust.h src/thrust_t.h \
21+
src/statistics.c src/statistics.h
2122

2223
GENDATASRC = \
2324
datasrc/blks.c datasrc/ship.c datasrc/shld.c datasrc/colors.c \

src/conf.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ get_user_path()
196196
}
197197

198198
char *
199-
getthrustrc(void)
199+
getuserfile(char *name)
200200
{
201201
char *home;
202202
char *thrustrc;
@@ -205,7 +205,7 @@ getthrustrc(void)
205205
if(home==NULL)
206206
home = "";
207207

208-
thrustrc = malloc(strlen(home) + 11);
208+
thrustrc = malloc(strlen(home) + strlen(name) + 2);
209209
if(thrustrc == NULL) {
210210
printf("Out of memory when trying to read .thrustrc.\n");
211211
return NULL;
@@ -221,17 +221,23 @@ getthrustrc(void)
221221
if(thrustrc[strlen(thrustrc)-1]!='\\')
222222
strcat(thrustrc, "\\");
223223
}
224-
strcat(thrustrc, "thrustrc");
224+
strcat(thrustrc, name);
225225
#else
226226
if(thrustrc[0])
227227
if(thrustrc[strlen(thrustrc)-1]!='/')
228228
strcat(thrustrc, "/");
229-
strcat(thrustrc, "thrustrc");
229+
strcat(thrustrc, name);
230230
#endif
231231

232232
return thrustrc;
233233
}
234234

235+
char *
236+
getthrustrc(void)
237+
{
238+
return getuserfile("thrustrc");
239+
}
240+
235241
char *underscore(char *keystring)
236242
{
237243
static char keybuffer[100];

src/conf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ extern char *keynames[];
1111
void initkeys(void);
1212
void writekeys(void);
1313
char *underscore(char *keystring);
14+
char *getuserfile(char *name);
1415
char *getthrustrc(void);
1516
void conf(void);
1617
int getscancode(int old, int x, int y);

src/sdlkey.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include "gr_drv.h"
1717

1818
#include "keyboard.h"
19+
#include "init.h"
20+
#include "statistics.h"
1921

2022
int scancode[5] = {
2123
SDLK_a,
@@ -102,7 +104,9 @@ getkeys(void)
102104
{
103105
case SDL_QUIT:
104106
{
105-
SDL_Quit();
107+
writestatistics();
108+
restoremem();
109+
restorehardware();
106110
exit(0);
107111
}
108112
case SDL_KEYDOWN:

src/sdlsound.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ int sdlLoop[SOUND_SAMPLES];
1515
int
1616
sound_depends_on_graphics()
1717
{
18-
return 0;
18+
return 1;
1919
}
2020

2121
int

src/statistics.c

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include "conf.h"
5+
#include "statistics.h"
6+
7+
statistics stats;
8+
9+
static char *fields[6] = {
10+
"PlanetsDestroyed",
11+
"BunkersDestroyed",
12+
"Level",
13+
"PodsAcquired",
14+
"FuelAcquired",
15+
"FuelDestroyed"
16+
};
17+
18+
void
19+
clearstatgroup(statgroup *s)
20+
{
21+
s->planetsdestroyed = 0;
22+
s->bunkersdestroyed = 0;
23+
s->level = 0;
24+
s->podsacquired = 0;
25+
s->fuelacquired = 0;
26+
s->fueldestroyed = 0;
27+
}
28+
29+
void
30+
parsestatline(char *line, statgroup *s)
31+
{
32+
char *separator = strstr(line, " = ");
33+
if (separator) {
34+
long val = atol(separator + 3);
35+
if ((strncmp(line, fields[0], strlen(fields[0])) == 0) && (strlen(fields[0]) == separator - line))
36+
s->planetsdestroyed = val;
37+
else if ((strncmp(line, fields[1], strlen(fields[1])) == 0) && (strlen(fields[1]) == separator - line))
38+
s->bunkersdestroyed = val;
39+
else if ((strncmp(line, fields[2], strlen(fields[2])) == 0) && (strlen(fields[2]) == separator - line))
40+
s->level = val;
41+
else if ((strncmp(line, fields[3], strlen(fields[3])) == 0) && (strlen(fields[3]) == separator - line))
42+
s->podsacquired = val;
43+
else if ((strncmp(line, fields[4], strlen(fields[4])) == 0) && (strlen(fields[4]) == separator - line))
44+
s->fuelacquired = val;
45+
else if ((strncmp(line, fields[5], strlen(fields[5])) == 0) && (strlen(fields[5]) == separator - line))
46+
s->fueldestroyed = val;
47+
}
48+
}
49+
50+
void
51+
writestatgroup(FILE *f, statgroup *s)
52+
{
53+
fprintf(f, "%s = %ld\n", fields[0], s->planetsdestroyed);
54+
fprintf(f, "%s = %ld\n", fields[1], s->bunkersdestroyed);
55+
fprintf(f, "%s = %ld\n", fields[2], s->level);
56+
fprintf(f, "%s = %ld\n", fields[3], s->podsacquired);
57+
fprintf(f, "%s = %ld\n", fields[4], s->fuelacquired);
58+
fprintf(f, "%s = %ld\n", fields[5], s->fueldestroyed);
59+
}
60+
61+
void
62+
initstatistics()
63+
{
64+
clearstatgroup(&stats.overall);
65+
clearstatgroup(&stats.best);
66+
clearstatgroup(&stats.current);
67+
char *filename = getuserfile("statistics");
68+
FILE *f = fopen(filename, "rt");
69+
if (f) {
70+
char line[256];
71+
statgroup *s = NULL;
72+
while (!feof(f))
73+
{
74+
fgets(line, 256, f);
75+
if (line[0] == '[') {
76+
if (strncmp("Overall]", line + 1, 8) == 0)
77+
s = &stats.overall;
78+
else if (strncmp("Best]", line + 1, 5) == 0)
79+
s = &stats.best;
80+
else
81+
s = NULL;
82+
}
83+
else if (s != NULL)
84+
parsestatline(line, s);
85+
}
86+
fclose(f);
87+
}
88+
free(filename);
89+
}
90+
91+
void
92+
writestatistics()
93+
{
94+
char *filename = getuserfile("statistics");
95+
FILE *f = fopen(filename, "wt");
96+
if (f) {
97+
fputs("[Overall]\n", f);
98+
writestatgroup(f, &stats.overall);
99+
fputs("\n[Best]\n", f);
100+
writestatgroup(f, &stats.best);
101+
fclose(f);
102+
}
103+
free(filename);
104+
}
105+
106+
void
107+
clearcurrentstats()
108+
{
109+
clearstatgroup(&stats.current);
110+
}
111+
112+
void
113+
updatestatistics(long planets, long bunkers, long level, long pods, long fuelacquired, long fueldestroyed)
114+
{
115+
stats.current.planetsdestroyed += planets;
116+
stats.current.bunkersdestroyed += bunkers;
117+
stats.current.level += level;
118+
stats.current.podsacquired += pods;
119+
stats.current.fuelacquired += fuelacquired;
120+
stats.current.fueldestroyed += fueldestroyed;
121+
stats.overall.planetsdestroyed += planets;
122+
stats.overall.bunkersdestroyed += bunkers;
123+
stats.overall.level += level;
124+
stats.overall.podsacquired += pods;
125+
stats.overall.fuelacquired += fuelacquired;
126+
stats.overall.fueldestroyed += fueldestroyed;
127+
if (stats.current.planetsdestroyed > stats.best.planetsdestroyed)
128+
stats.best.planetsdestroyed = stats.current.planetsdestroyed;
129+
if (stats.current.bunkersdestroyed > stats.best.bunkersdestroyed)
130+
stats.best.bunkersdestroyed = stats.current.bunkersdestroyed;
131+
if (stats.current.level > stats.best.level)
132+
stats.best.level = stats.current.level;
133+
if (stats.current.podsacquired > stats.best.podsacquired)
134+
stats.best.podsacquired = stats.current.podsacquired;
135+
if (stats.current.fuelacquired > stats.best.fuelacquired)
136+
stats.best.fuelacquired = stats.current.fuelacquired;
137+
if (stats.current.fueldestroyed > stats.best.fueldestroyed)
138+
stats.best.fueldestroyed = stats.current.fueldestroyed;
139+
}

src/statistics.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
/* Written by Dennis Payne, dulsi@identicalsoftware.com */
3+
4+
#ifndef STATISTICS_H
5+
#define STATISTICS_H
6+
7+
typedef struct {
8+
long planetsdestroyed;
9+
long bunkersdestroyed;
10+
long level;
11+
long podsacquired;
12+
long fuelacquired;
13+
long fueldestroyed;
14+
} statgroup;
15+
16+
typedef struct {
17+
statgroup overall;
18+
statgroup best;
19+
statgroup current;
20+
} statistics;
21+
22+
void initstatistics();
23+
void writestatistics();
24+
void clearcurrentstats();
25+
void updatestatistics(long planets, long bunkers, long level, long pods, long fuelacquired, long fueldestroyed);
26+
27+
#endif

0 commit comments

Comments
 (0)