forked from rogueforge/rogomatic14
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreplay.c
More file actions
229 lines (176 loc) · 5.59 KB
/
replay.c
File metadata and controls
229 lines (176 loc) · 5.59 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
219
220
221
222
223
224
225
226
227
228
229
/*
* 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/>.
*/
/*
* replay.c:
*
* Make a table of offsets to the beginning of each level of a
* Rog-O-Matic log file.
*/
# include <ctype.h>
# include <string.h>
# include "modern_curses.h"
# include "types.h"
# include "globals.h"
# define MAXNUMLEV 50
# define FIRSTLEVSTR "\nR: "
# define NEWLEVSTR "\nR: {ff}"
# define POSITAT "{ff}"
struct levstruct {
long pos;
int level, gold, hp, hpmax, str, strmax, ac, explev, exp;
} levpos[MAXNUMLEV];
/* static declarations */
static int numlev = 0;
static int findlevel (FILE *f, struct levstruct *lvpos, int *nmlev, int maxnum);
static void fillstruct (FILE *f, struct levstruct *lev);
static int findmatch (FILE *f, char *s);
/*
* positionreplay: Called when user has typed the 'R' command, it fills
* the level table by calling findlevel if necessary, and then positions
* the log file to the level requested by the user.
*/
void
positionreplay (void)
{
int curlev;
long curpos;
char cmd;
/* Prompt user for a command character, read it, and lower case it */
saynow ("Which level (f=first, p=previous, c=current, n=next, l=last): ");
if (isupper ((cmd = getch ()))) cmd = tolower (cmd);
/* Clear the prompt */
saynow (NULL); /* NOTE: NULL ==> do not format / "say" anything, just refresh the screen */
/* If command is not in the list, clear the prompt and exit. */
switch (cmd) {
case 'f': case 'p': case 'c': case 'n': case 'l': break;
default: return;
}
/* Save the current position in the file */
curpos = ftell (logfile);
/* Read the log file, if we have not already done so */
if (!logdigested) {
saynow ("Reading whole log file to find levels...");
if (!findlevel (logfile, levpos, &numlev, MAXNUMLEV)) {
saynow ("Findlevel failed! Let's try to get back to where we were...");
fseek (logfile, curpos, 0);
return;
}
logdigested = true;
}
/* Now figure out the current level (so relative commands will work) */
for (curlev = 0; curlev < numlev-1; curlev++)
if (levpos[curlev+1].pos > curpos) break;
/* Now clear the screen, position the log file, and return */
switch (cmd) {
case 'f': fseek (logfile, levpos[0].pos, 0); break;
case 'p':
if (curlev > 0) fseek (logfile, levpos[curlev-1].pos, 0);
else fseek (logfile, levpos[0].pos, 0);
break;
case 'c': fseek (logfile, levpos[curlev].pos, 0); break;
case 'n':
if (curlev < numlev-1) fseek (logfile, levpos[curlev+1].pos, 0);
else fseek (logfile, levpos[curlev].pos, 0);
break;
case 'l': fseek (logfile, levpos[numlev-1].pos, 0);
break;
default: fseek (logfile, 0L, 0);
}
clearscreen (); /* Clear the screen */
Level = -1; /* Force a newlevel() call */
}
/*
* findlevel: Make a table of offsets to the various levels of a
* Rog-O-Matic log file.
*/
static int
findlevel (FILE *f, struct levstruct *lvpos, int *nmlev, int maxnum)
{
char ch;
int l=0;
*nmlev = 0;
/* Position file after first newline */
rewind (f);
while ((ch = getc (f)) != '\n' && (int) ch != EOF);
/* This is that start of level one */
lvpos[l].pos = ftell (f);
if (!findmatch (f, FIRSTLEVSTR)) {
rewind (f);
return (FAILURE);
}
fillstruct (f, &lvpos[l]);
while (++l <= maxnum && findmatch (f, NEWLEVSTR)) {
fseek (f, (long) -strlen (POSITAT), 1);
lvpos[l].pos = ftell (f);
fillstruct (f, &lvpos[l]);
}
*nmlev = l;
rewind (f);
return (SUCCESS);
}
/*
* fillstruct: scan the logfile from the current point, and fill in the
* fields of a levstruct.
*/
static void
fillstruct (FILE *f, struct levstruct *lev)
{
lev->level = 0;
lev->gold = 0;
lev->hp = 0;
lev->hpmax = 0;
lev->str = 0;
lev->strmax = 0;
lev->ac = 0;
lev->explev = 0;
lev->exp = 0;
if (!findmatch (f, "Level:")) return;
fscanf (f, "%d", &lev->level);
if (!findmatch (f, "Gold:")) return;
fscanf (f, "%d", &lev->gold);
if (!findmatch (f, "Hp:")) return;
fscanf (f, "%d(%d)", &lev->hp, &lev->hpmax);
if (!findmatch (f, "Str:")) return;
fscanf (f, "%d(%d)", &lev->str, &lev->strmax);
if (!findmatch (f, ":")) return; /* Armor class */
fscanf (f, "%d", &lev->ac);
if (!findmatch (f, "Exp:")) return;
fscanf (f, "%d/%d", &lev->explev, &lev->exp);
saynow ("Found level %d, has %d gold...", lev->level, lev->gold);
}
/*
* findmatch: read from a stream until string 's' has been read. Returns 0
* if EOF is read, and 1 if the match is found. The stream is left
* immediately after the matched string.
*
* Restriction: 's' must not contain prefix of itself as a substring.
*/
static int
findmatch (FILE *f, char *s)
{
char *m = s, ch;
while (*m && (int) (ch = fgetc (f)) != EOF)
if (ch != *(m++)) m = s;
if (*m) return (0);
else return (1);
}