forked from rogueforge/rogomatic14
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebug.c
More file actions
296 lines (236 loc) · 8.05 KB
/
debug.c
File metadata and controls
296 lines (236 loc) · 8.05 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* 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/>.
*/
/*
* debug.c:
*
* This file contains the code for the debugger. Rogomatic has one of
* the tensest internal debuggers around, because in the early days it
* had an incredible number of bugs, with no way to repeat an error
* (because Rogue uses a different dungeon each time).
*/
# include <curses.h>
# include <setjmp.h>
# include <string.h>
# include <stdarg.h>
# include "types.h"
# include "globals.h"
# include "install.h"
/* static declarations */
static void dumpflags (int r, int c);
static int getscrpos (char *msg, int *r, int *c);
/*
* Debugging wait loop: Handle the usual Rogomatic command chars, and also
* allows dumping the flags '^' command. Exits when a non-command char is
* typed. To use, just put a "dwait (type, "message");" wherever you need
* debugging messages, and hit a space or a cr to continue
*/
int
dwait(int msgtype, char *f, ...)
{
char msg[MU_BUF + 1]; /* message buffer, +1 for paranoia */
int r, c;
va_list ap;
/* zeroize arrays */
memset (msg, 0, sizeof(msg)); /* paranoia */
/* Build the actual message */
va_start (ap, f);
vsnprintf (msg, MU_BUF, f, ap);
va_end(ap);
/* Log the message if the error is severe enough */
if (!replaying && (msgtype & (D_FATAL | D_ERROR | D_WARNING))) {
char errfn[TY_BUF + 1]; /* error filename, +1 for paranoia */
FILE *errfil;
/* zeroize arrays */
memset (errfn, 0, sizeof(errfn)); /* paranoia */
snprintf (errfn, TY_BUF, "%s/error%s", getRgmDir (), versionstr);
if ((errfil = wopen (errfn, "a")) != NULL) {
fprintf (errfil, "User %s, error type %d: %s\n\n",
getname(), msgtype, msg);
if (msgtype & (D_FATAL | D_ERROR)) {
printsnap (errfil);
summary (errfil, NEWLINE);
fprintf (errfil, "\f\n");
}
va_end (ap);
fclose (errfil);
}
}
if (msgtype & D_FATAL) {
extern jmp_buf commandtop; /* From play */
saynow (msg);
playing = 0;
quitrogue ("fatal error trap", Gold, SAVED);
longjmp (commandtop, 0);
}
if (! debug (msgtype | D_INFORM)) { /* If debugoff */
if (msgtype & D_SAY) /* Echo? */
{ saynow (msg); va_end (ap); return (1); } /* Yes => win */
va_end (ap);
return (0); /* No => lose */
}
if (*msg) { mvaddstr (0, 0, msg); clrtoeol (); } /* Write msg */
if (noterm) { va_end (ap); return (1); } /* Exit if no user */
/* Debugging loop, accept debugging commands from user */
while (1) {
refresh ();
switch (fgetc (stdin)) {
case '?':
say ("i=inv, d=debug !=stf, @=mon, #=wls, $=id, ^=flg, &=chr");
break;
case 'i': at (1,0); dumpinv ((FILE *) NULL); at (row, col); break;
case 'd': toggledebug (); break;
case 't': transparent = 1; break;
case '!': dumpstuff (); break;
case '@': dumpmonster (); break;
case '#': dumpwalls (); break;
case '^': promptforflags (); break;
case '&':
if (getscrpos ("char", &r, &c))
saynow ("Char at %d,%d '%c'", r, c, screen[r][c]);
break;
case '(': dumpdatabase (); at (row, col); break;
case ')': new_mark++; markcycles (DOPRINT); at (row, col); break;
case '~': saynow ("Version %d, quit at %d", version, quitat); break;
case '/': dosnapshot (); break;
default: at (row, col); va_end (ap); return (1);
}
}
}
/*
* promptforflags: Prompt the user for a location and dump its flags.
*/
void
promptforflags (void)
{
int r, c;
if (getscrpos ("flags", &r, &c)) {
mvprintw (0, 0, "Flags for %d,%d ", r, c);
dumpflags (r, c);
clrtoeol ();
at (row, col);
}
}
/*
* dumpflags: Create a message line for the scrmap flags of a particular
* square. Note that the fnames[] array must match the
* various flags defined in "types.h".
*/
static char *fnames[] = {
"been", "cango", "door", "hall", "psd", "room",
"safe", "seen", "deadend", "stuff", "trap", "arrow",
"trapdor", "teltrap", "gastrap", "beartrap", "dartrap", "waterap",
"monster", "wall", "useless", "scarem", "stairs", "runok",
"boundry", "sleeper", "everclr"
};
static void
dumpflags (int r, int c)
{
char **f; int b;
printw (":");
for (f=fnames, b=1; b<=EVERCLR; b = b * 2, f++)
if (scrmap[r][c] & b)
printw ("%s:", *f);
}
/*
* Timehistory: print a time analysis of the game.
*/
void
timehistory (FILE *f, char sep)
{
int i, j;
char s[BUFSIZ + 1]; /* time history message, +1 for paranoia */
char s2[MU_BUF + 1]; /* level message, +1 for paranoia */
/* zeroize arrays */
memset (s, 0, sizeof(s)); /* paranoia */
timespent[0].timestamp = 0;
snprintf (s, BUFSIZ, "Time Analysis: %s%c%c",
"othr hand fght rest move expl rung grop srch door total",
sep, sep);
for (i=1; i<=MaxLevel; i++) {
memset (s2, 0, sizeof(s2)); /* paranoia */
snprintf (s2, MU_BUF, "level %2d: ", i);
strcat (s, s2);
for (j = T_OTHER; j < T_LISTLEN; j++) {
snprintf (s2, MU_BUF, "%5d", timespent[i].activity[j]);
strcat (s, s2);
}
snprintf (s2, MU_BUF, "%6d%c",
timespent[i].timestamp - timespent[i-1].timestamp, sep);
strcat (s, s2);
}
if (f == NULL)
addstr (s);
else
fprintf (f, "%s", s);
}
/*
* toggledebug: Set the value of the debugging word.
*/
void
toggledebug (void)
{
char debugstr[100];
int type = debugging & ~(D_FATAL | D_ERROR | D_WARNING);
if (debugging == D_ALL) debugging = D_NORMAL;
else if (debugging == D_NORMAL) debugging = D_NORMAL | D_SEARCH;
else if (type == D_SEARCH) debugging = D_NORMAL | D_BATTLE;
else if (type == D_BATTLE) debugging = D_NORMAL | D_MESSAGE;
else if (type == D_MESSAGE) debugging = D_NORMAL | D_PACK;
else if (type == D_PACK) debugging = D_NORMAL | D_MONSTER;
else if (type == D_MONSTER) debugging = D_NORMAL | D_CONTROL;
else if (type == D_CONTROL) debugging = D_NORMAL | D_SCREEN;
else if (type == D_SCREEN) debugging = D_NORMAL | D_WARNING;
else if (!debug (D_INFORM)) debugging = D_NORMAL | D_WARNING | D_INFORM;
else debugging = D_ALL;
strncpy (debugstr, "Debugging :", 100);
if (debug(D_FATAL)) strcat (debugstr, "fatal:");
if (debug(D_ERROR)) strcat (debugstr, "error:");
if (debug(D_WARNING)) strcat (debugstr, "warn:");
if (debug(D_INFORM)) strcat (debugstr, "info:");
if (debug(D_SEARCH)) strcat (debugstr, "search:");
if (debug(D_BATTLE)) strcat (debugstr, "battle:");
if (debug(D_MESSAGE)) strcat (debugstr, "msg:");
if (debug(D_PACK)) strcat (debugstr, "pack:");
if (debug(D_CONTROL)) strcat (debugstr, "ctrl:");
if (debug(D_SCREEN)) strcat (debugstr, "screen:");
if (debug(D_MONSTER)) strcat (debugstr, "monster:");
saynow (debugstr);
}
/*
* getscrpos: Prompt the user for an x,y coordinate on the screen.
*/
static int
getscrpos (char *msg, int *r, int *c)
{
char buf[256];
saynow ("At %d,%d: enter 'row,col' for %s: ", atrow, atcol, msg);
if (fgets (buf, 256, stdin)) {
sscanf (buf, "%d,%d", r, c);
if (*r>=1 && *r<23 && *c>=0 && *c<=79)
return (1);
else
say ("%d,%d is not on the screen!", *r, *c);
}
at (row, col);
return (0);
}