1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "glk/alan2/types.h"
24 #include "glk/alan2/alan_version.h"
25 #include "glk/alan2/debug.h"
26 #include "glk/alan2/exe.h"
27 #include "glk/alan2/glkio.h"
28 #include "glk/alan2/inter.h"
29 #include "glk/alan2/main.h"
30 #include "glk/alan2/parse.h"
31 
32 namespace Glk {
33 namespace Alan2 {
34 
showatrs(Aword atradr)35 static void showatrs(Aword atradr) {
36 	AtrElem *at;
37 	int i;
38 	char str[80];
39 
40 	if (atradr == 0) return;
41 
42 	i = 1;
43 	for (at = (AtrElem *) addrTo(atradr); !endOfTable(at); at++) {
44 		sprintf(str, "$i%3ld: %ld (%s)", (long) i, (unsigned long) at->val, (char *) addrTo(at->stradr));
45 		output(str);
46 		i++;
47 	}
48 }
49 
showobjs()50 static void showobjs() {
51 	char str[80];
52 	uint obj;
53 
54 	output("OBJECTS:");
55 	for (obj = OBJMIN; obj <= OBJMAX; obj++) {
56 		sprintf(str, "$i%3ld: ", (long) obj);
57 		output(str);
58 		say(obj);
59 	}
60 }
61 
showobj(int obj)62 static void showobj(int obj) {
63 	char str[80];
64 #define OBJ (obj-OBJMIN)
65 
66 
67 	if (!isObj(obj)) {
68 		sprintf(str, "Object number out of range. Between %ld and %ld, please.", (unsigned long) OBJMIN, (unsigned long) OBJMAX);
69 		output(str);
70 		return;
71 	}
72 
73 	sprintf(str, "OBJECT %d :", obj);
74 	output(str);
75 	say(obj);
76 
77 	sprintf(str, "$iLocation = %ld", (unsigned long) where(obj));
78 	output(str);
79 	if (isLoc(objs[OBJ].loc))
80 		say(objs[OBJ].loc);
81 	else if (isCnt(objs[OBJ].loc)) {
82 		if (isObj(objs[OBJ].loc)) {
83 			output("in");
84 			say(objs[OBJ].loc);
85 		} else if (isAct(objs[OBJ].loc)) {
86 			output("carried by");
87 			say(objs[OBJ].loc);
88 		} else
89 			interpret(cnts[objs[OBJ].loc - CNTMIN].nam);
90 	} else if (objs[OBJ].loc == 0)
91 		output("nowhere");
92 	else
93 		output("Illegal location!");
94 
95 
96 	output("$iAttributes =");
97 	showatrs(objs[OBJ].atrs);
98 
99 #undef OBJ
100 }
101 
showcnts()102 static void showcnts() {
103 	char str[80];
104 	uint cnt;
105 #define  CNT (cnt-CNTMIN)
106 
107 	output("CONTAINERS:");
108 	for (cnt = CNTMIN; cnt <= CNTMAX; cnt++) {
109 		sprintf(str, "$i%3ld: ", (long) cnt);
110 		output(str);
111 		if (cnts[CNT].nam != 0)
112 			interpret(cnts[CNT].nam);
113 		if (cnts[CNT].parent != 0)
114 			say(cnts[CNT].parent);
115 	}
116 
117 #undef CNT
118 }
119 
showcnt(int cnt)120 static void showcnt(int cnt) {
121 	char str[80];
122 	uint i;
123 	Abool found = FALSE;
124 #define  CNT (int)(cnt - CNTMIN)
125 
126 	if (cnt < (int)CNTMIN || cnt >(int)CNTMAX) {
127 		sprintf(str, "Container number out of range. Between %ld and %ld, please.", (unsigned long) CNTMIN, (unsigned long) CNTMAX);
128 		output(str);
129 		return;
130 	}
131 
132 	sprintf(str, "CONTAINER %d :", cnt);
133 	output(str);
134 	if (cnts[CNT].nam != 0)
135 		interpret(cnts[CNT].nam);
136 	if (cnts[CNT].parent != 0) {
137 		cnt = cnts[CNT].parent;
138 		say(cnt);
139 		sprintf(str, "$iLocation = %ld", (unsigned long) where(cnt));
140 		output(str);
141 	}
142 	output("$iContains ");
143 	for (i = OBJMIN; i <= OBJMAX; i++) {
144 		if (in(i, cnt)) { /* Yes, it's in this container */
145 			if (!found) {
146 				output("$n");
147 				found = TRUE;
148 			}
149 			sprintf(str, "$t$t%d: ", i);
150 			output(str);
151 			say(i);
152 		}
153 	}
154 	if (!found)
155 		output("nothing");
156 
157 #undef CNT
158 }
159 
showlocs()160 static void showlocs() {
161 	char str[80];
162 	uint loc;
163 
164 	output("LOCATIONS:");
165 	for (loc = LOCMIN; loc <= LOCMAX; loc++) {
166 		sprintf(str, "$i%3ld: ", (long) loc);
167 		output(str);
168 		say(loc);
169 	}
170 }
171 
showloc(int loc)172 static void showloc(int loc) {
173 	char str[80];
174 
175 
176 	if (!isLoc(loc)) {
177 		sprintf(str, "Location number out of range. Between %ld and %ld, please.", (unsigned long) LOCMIN, (unsigned long) LOCMAX);
178 		output(str);
179 		return;
180 	}
181 
182 	sprintf(str, "LOCATION %d :", loc);
183 	output(str);
184 	say(loc);
185 
186 	output("$iAttributes =");
187 	showatrs(locs[loc - LOCMIN].atrs);
188 }
189 
showacts()190 static void showacts() {
191 	char str[80];
192 	uint act;
193 
194 	output("ACTORS:");
195 	for (act = ACTMIN; act <= ACTMAX; act++) {
196 		sprintf(str, "$i%3ld:", (long) act);
197 		output(str);
198 		say(act);
199 	}
200 }
201 
showact(int act)202 static void showact(int act) {
203 	char str[80];
204 	Boolean oldstp;
205 
206 	if (!isAct(act)) {
207 		sprintf(str, "Actor number out of range. Between %ld and %ld, please.", (unsigned long) ACTMIN, (unsigned long) ACTMAX);
208 		output(str);
209 		return;
210 	}
211 
212 	sprintf(str, "ACTOR %d :", act);
213 	output(str);
214 	oldstp = stpflg;
215 	stpflg = FALSE; /* Make sure not to trace this! */
216 	say(act);
217 	stpflg = oldstp;
218 
219 	sprintf(str, "$iLocation = %ld", (unsigned long) acts[act - ACTMIN].loc);
220 	output(str);
221 	if (isLoc(acts[act - ACTMIN].loc))
222 		say(acts[act - ACTMIN].loc);
223 	else if (acts[act - ACTMIN].loc == 0)
224 		output("nowhere");
225 	else
226 		output("Illegal location!");
227 
228 	sprintf(str, "$iScript = %ld", (unsigned long) acts[act - ACTMIN].script);
229 	output(str);
230 
231 	sprintf(str, "$iStep = %ld", (unsigned long) acts[act - ACTMIN].step);
232 	output(str);
233 
234 	output("$iAttributes =");
235 	showatrs(acts[act - ACTMIN].atrs);
236 }
237 
showevts()238 static void showevts() {
239 	int i;
240 	char str[80];
241 	Boolean scheduled;
242 
243 	output("EVENTS:");
244 	for (uint evt = EVTMIN; evt <= EVTMAX; evt++) {
245 		sprintf(str, "$i%d (%s):", evt, (char *)addrTo(evts[evt - EVTMIN].stradr));
246 		output(str);
247 		scheduled = FALSE;
248 		for (i = 0; i < etop; i++)
249 			if ((scheduled = (eventq[i].event == (int)evt)))
250 				break;
251 		if (scheduled) {
252 			sprintf(str, "Scheduled for +%d, at ", eventq[i].time - cur.tick);
253 			output(str);
254 			say(eventq[i].where);
255 		} else
256 			output("Not scheduled.");
257 	}
258 }
259 
260 
261 static Boolean trc, stp;
262 static int loc;
263 
saveInfo()264 void saveInfo() {
265 	/* Save some important things */
266 	trc = trcflg;
267 	trcflg = FALSE;
268 	stp = stpflg;
269 	stpflg = FALSE;
270 	loc = cur.loc;
271 	cur.loc = where(HERO);
272 }
273 
restoreInfo()274 void restoreInfo() {
275 	/* Restore! */
276 	trcflg = trc;
277 	stpflg = stp;
278 	cur.loc = loc;
279 }
280 
debug()281 void debug() {
282 	char buf[256];
283 	char c;
284 	int i;
285 
286 	saveInfo();
287 	while (TRUE) {
288 		if (anyOutput)
289 			para();
290 		do {
291 			output("ABUG> ");
292 			(void)readline(buf);
293 
294 			lin = 1;
295 			c = buf[0];
296 			i = 0;
297 			(void)sscanf(&buf[1], "%d", &i);
298 		} while (/*buf &&*/ c == '\0');
299 
300 		switch (toUpper(c)) {
301 		case 'H':
302 		case '?':
303 			output(alan.longHeader);
304 			output("$nABUG Commands:\
305 	  $iO [n] -- show object[s]\
306 	  $iA [n] -- show actor[s]\
307 	  $iL [n] -- show location[s]\
308 	  $iC [n] -- show container[s]\
309 	  $iE -- show events\
310 	  $iG -- go on\
311 	  $iT -- toggle trace mode\
312 	  $iS -- toggle step mode\
313 	  $iX -- exit debug mode\
314 	  $iQ -- quit game");
315 			break;
316 		case 'Q': {
317 			Context ctx;
318 			terminate(ctx, 0);
319 			break;
320 		}
321 		case 'X':
322 			dbgflg = FALSE;
323 			restoreInfo();
324 			return;
325 		case 'G':
326 			restoreInfo();
327 			return;
328 		case 'O':
329 			if (i == 0)
330 				showobjs();
331 			else
332 				showobj(i);
333 			break;
334 		case 'C':
335 			if (i == 0)
336 				showcnts();
337 			else
338 				showcnt(i);
339 			break;
340 		case 'A':
341 			if (i == 0)
342 				showacts();
343 			else
344 				showact(i);
345 			break;
346 		case 'L':
347 			if (i == 0)
348 				showlocs();
349 			else
350 				showloc(i);
351 			break;
352 		case 'E':
353 			showevts();
354 			break;
355 		case 'S':
356 			if ((stp = !stp))
357 				printf("Step on.");
358 			else
359 				printf("Step off.");
360 			break;
361 		case 'T':
362 			if ((trc = !trc))
363 				printf("Trace on.");
364 			else
365 				printf("Trace off.");
366 			break;
367 		default:
368 			output("Unknown ABUG command. ? for help.");
369 			break;
370 		}
371 	}
372 }
373 
374 
375 /*======================================================================
376 
377   debugsay()
378 
379   Say somethin, but make sure we don't disturb anything and that it is
380   shown to the player.
381 
382 */
debugsay(int item)383 void debugsay(int item) {
384 	saveInfo();
385 	needsp = FALSE;
386 	col = 1;
387 	if (item == 0)
388 		printf("$null$");
389 	else
390 		say(item);
391 	needsp = FALSE;
392 	col = 1;
393 	restoreInfo();
394 }
395 
396 } // End of namespace Alan2
397 } // End of namespace Glk
398