forked from rogueforge/rogomatic14
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrgmplot.c
More file actions
218 lines (169 loc) · 6.04 KB
/
rgmplot.c
File metadata and controls
218 lines (169 loc) · 6.04 KB
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
/*
* Rog-O-Matic
* Automatically exploring the dungeons of doom.
*
* Copyright (C) 2008 by Anthony Molinaro
* Copyright (C) 1985 by Appel, Jacobson, Hamey, and Mauldin.
*
* This file is part of Rog-O-Matic.
*
* Rog-O-Matic is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rog-O-Matic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Rog-O-Matic. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* rgmplot.c:
*
* This program takes a Rog-O-Matic score file sorted by date and score,
* and produces a scatter plot of the scores.
*/
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
# include "have_strlcat.h"
# include "have_strlcpy.h"
# include "strl.h"
# include "types.h"
# define WIDTH 50
# define AVLEN 30
# define SCALE(n) (((n)+100)/200)
# define isdigit(c) ((c) >= '0' && (c) <= '9')
int cheat = false;
/* static declarations */
static char *month[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
static int doavg = 0;
static int minscore = -1;
static int getlin (char *s);
static int getscore (int *mm, int *dd, int *yy, char *player, int *score, char *cheated);
int
main (int argc, char *argv[])
{
int mm, dd, yy, score = 0, lastday = -1, lastmon = -1, lastyy = -1, h;
int sumscores = 0, numscores = 0, i;
int sum[AVLEN + 1]; /* daily score accumulated sum, +1 for paranoia */
int num[AVLEN + 1]; /* daily score, +1 for paranoia */
int rsum, rnum, davg, ravg;
char player[MU_BUF + 1]; /* rogue player, +1 for paranoia */
char plot[MU_BUF + 1]; /* plot string, +1 for paranoia */
char cheated;
char buf[MU_BUF + 1]; /* message buffer, +1 for paranoia */
/* zeroize arrays */
memset (player, 0, sizeof(player)); /* paranoia */
memset (plot, 0, sizeof(plot)); /* paranoia */
memset (buf, 0, sizeof(buf)); /* paranoia */
/* Clear out the rolling average statistics */
memset (sum, 0, sizeof(sum));
memset (num, 0, sizeof(num));
/* Get the options */
while (--argc > 0 && (*++argv)[0] == '-')
while (*++(*argv)) {
switch (**argv) {
case 'c': cheat++; break; /* List cheat games */
case 'a': doavg++; break; /* Print average */
default: printf ("Usage: rgmplot [-ac] [mininum]\n");
exit (1);
}
}
if (argc > 0) minscore = atoi (argv[0]);
/* Print out the header */
printf ("\t\t Scatter Plot of Rog-O-Matic Scores versus time\n\n");
if (minscore > 0)
printf ("\t\t Scores greater than %d\n\n", minscore);
printf ("\t\t0 2000 4000 6000 8000 10000\n");
printf ("\t\t|----+----|----+----|----+----|----+----|----+----|\n");
/* Build an empty plot line */
strlcpy (plot, "| |", sizeof(plot));
/* While more scores do action for each score */
while (getscore (&mm, &dd, &yy, player, &score, &cheated) != EOF) {
/* Change days, overprint the average for day, rolling avg */
if ((dd != lastday || mm != lastmon || yy != lastyy) && lastday > 0) {
if (doavg) {
rsum = *sum; rnum = *num;
for (i = 1; i < AVLEN; i++)
{ rsum += sum[i]; rnum += num[i]; }
davg = SCALE ((*num > 0) ? (*sum / *num) : 0);
ravg = SCALE ((rnum > 0) ? (rsum / rnum) : 0);
/* Roll the daily average statistics */
for (i = AVLEN-1; i > 0; i--)
{ sum[i] = sum[i-1]; num[i] = num[i-1]; }
*sum = *num = 0;
/* Print a '*' for the daily average */
if (davg > 0 && davg < WIDTH)
plot[davg] = '*';
/* Print a '###' for the rolling average */
if (ravg > 0 && ravg < WIDTH-1)
plot[ravg-1] = plot[ravg] = plot[ravg+1] = '#';
}
printf ("%3s %2d %4d\t%s\n", month[lastmon-1], lastday, lastyy, plot);
strlcpy (plot, "| |", sizeof(plot));
}
if (score > EOF) {
if ((h = SCALE(score)) >= WIDTH) { snprintf (buf, MU_BUF, " %d", score); strlcat(plot, buf, sizeof(plot)); }
else if (plot[h] == '9') ;
else if (isdigit(plot[h])) plot[h]++;
else plot[h] = '1';
*sum += score;
++*num;
sumscores += score;
++numscores;
lastday = dd; lastmon = mm; lastyy = yy;
}
}
printf ("\t\t|----+----|----+----|----+----|----+----|----+----|\n");
printf ("\t\t0 2000 4000 6000 8000 10000\n");
if (numscores > 0)
printf ("\nAverage score %d, total games %d.\n\n",
sumscores/numscores, numscores);
printf ("1-9 Number of games in range.\n");
if (doavg) {
printf (" * Average of day's scores.\n");
printf ("### Rolling %d day average.\n", AVLEN);
}
}
static int
getlin (char *s)
{
int ch, i;
static int endfile = 0;
if (endfile) return (EOF);
for (i=0; (ch = getchar()) != EOF && ch != '\n'; i++)
s[i] = ch;
s[i] = '\0';
if (ch == EOF) {
endfile = 1;
strlcpy (s, "-1 -1, -1 string -1 ", TY_BUF); /* sizeof(line) is TY_BUF + 1 chars */
return (20);
}
return (i);
}
static int
getscore (int *mm, int *dd, int *yy, char *player, int *score, char *cheated)
{
char line[TY_BUF + 1];
char reason[TY_BUF + 1];
/* zeroize arrays */
memset(line, 0, sizeof(line));
memset(reason, 0, sizeof(reason));
while (getlin (line) != EOF) {
sscanf (line, "%d %d, %d %10s%d%c%17s",
mm, dd, yy, player, score, cheated, reason);
if ((*score >= minscore || *score < 0) &&
(*cheated != '*' || cheat) &&
!stlmatch (reason, "saved") &&
(*score > 2000 || !stlmatch (reason, "user")))
return (1);
}
return (EOF);
}