|
|
1.1 root 1: /*
2: * Copyright (c) 1985 Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms are permitted
6: * provided that the above copyright notice and this paragraph are
7: * duplicated in all such forms and that any documentation,
8: * advertising materials, and other materials related to such
9: * distribution and use acknowledge that the software was developed
10: * by the University of California, Berkeley. The name of the
11: * University may not be used to endorse or promote products derived
12: * from this software without specific prior written permission.
13: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15: * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16: */
17:
18: #ifndef lint
19: static char sccsid[] = "@(#)playit.c 5.3 (Berkeley) 6/27/88";
20: #endif /* not lint */
21:
22: /*
23: * Hunt
24: * Copyright (c) 1985 Conrad C. Huang, Gregory S. Couch, Kenneth C.R.C. Arnold
25: * San Francisco, California
26: */
27:
28: # include <curses.h>
29: # include <ctype.h>
30: # include <signal.h>
31: # include <errno.h>
32: # include "hunt.h"
33: # include <sys/file.h>
34:
35: # undef CTRL
36: # define CTRL(x) ('x' & 037)
37:
38: int input();
39: static int nchar_send;
40: static int in = FREAD;
41: char screen[24][80], blanks[80];
42: int cur_row, cur_col;
43: # ifdef OTTO
44: int Otto_count;
45: int Otto_mode;
46: static int otto_y, otto_x;
47: static char otto_face;
48: # endif OTTO
49:
50: # define MAX_SEND 5
51:
52: /*
53: * ibuf is the input buffer used for the stream from the driver.
54: * It is small because we do not check for user input when there
55: * are characters in the input buffer.
56: */
57: static char ibuf[20];
58:
59: #define GETCHR(fd) (--(fd)->_cnt >= 0 ? *(fd)->_ptr++&0377 : getchr(fd))
60:
61: extern int _putchar();
62:
63: /*
64: * playit:
65: * Play a given game, handling all the curses commands from
66: * the driver.
67: */
68: playit()
69: {
70: register FILE *inf;
71: register int ch;
72: register unsigned int y, x;
73: extern int Master_pid;
74: extern int errno;
75:
76: errno = 0;
77: while ((inf = fdopen(Socket, "r")) == NULL)
78: if (errno == EINTR)
79: errno = 0;
80: else {
81: perror("fdopen of socket");
82: exit(1);
83: }
84: setbuffer(inf, ibuf, sizeof ibuf);
85: Master_pid = getw(inf);
86: if (Master_pid == 0 || Master_pid == EOF) {
87: bad_con();
88: /* NOTREACHED */
89: }
90: # ifdef OTTO
91: Otto_count = 0;
92: # endif OTTO
93: nchar_send = MAX_SEND;
94: while ((ch = GETCHR(inf)) != EOF) {
95: # ifdef DEBUG
96: fputc(ch, stderr);
97: # endif DEBUG
98: switch (ch & 0377) {
99: case MOVE:
100: y = GETCHR(inf);
101: x = GETCHR(inf);
102: mvcur(cur_row, cur_col, y, x);
103: cur_row = y;
104: cur_col = x;
105: break;
106: case ADDCH:
107: ch = GETCHR(inf);
108: # ifdef OTTO
109: switch (ch) {
110:
111: case '<':
112: case '>':
113: case '^':
114: case 'v':
115: otto_face = ch;
116: getyx(stdscr, otto_y, otto_x);
117: break;
118: }
119: # endif OTTO
120: put_ch(ch);
121: break;
122: case CLRTOEOL:
123: clear_eol();
124: break;
125: case CLEAR:
126: clear_screen();
127: break;
128: case REFRESH:
129: fflush(stdout);
130: break;
131: case REDRAW:
132: redraw_screen();
133: fflush(stdout);
134: break;
135: case ENDWIN:
136: fflush(stdout);
137: if ((ch = GETCHR(inf)) == LAST_PLAYER)
138: Last_player = TRUE;
139: ch = EOF;
140: goto out;
141: case BELL:
142: putchar(CTRL(G));
143: break;
144: case READY:
145: (void) fflush(stdout);
146: if (nchar_send < 0)
147: (void) ioctl(fileno(stdin), TIOCFLUSH, &in);
148: nchar_send = MAX_SEND;
149: # ifndef OTTO
150: (void) GETCHR(inf);
151: # else OTTO
152: Otto_count -= (GETCHR(inf) & 255);
153: if (!Am_monitor) {
154: # ifdef DEBUG
155: fputc('0' + Otto_count, stderr);
156: # endif DEBUG
157: if (Otto_count == 0 && Otto_mode)
158: otto(otto_y, otto_x, otto_face);
159: }
160: # endif OTTO
161: break;
162: default:
163: # ifdef OTTO
164: switch (ch) {
165:
166: case '<':
167: case '>':
168: case '^':
169: case 'v':
170: otto_face = ch;
171: getyx(stdscr, otto_y, otto_x);
172: break;
173: }
174: # endif OTTO
175: put_ch(ch);
176: break;
177: }
178: }
179: out:
180: (void) fclose(inf);
181: }
182:
183: /*
184: * getchr:
185: * Grab input and pass it along to the driver
186: * Return any characters from the driver
187: * When this routine is called by GETCHR, we already know there are
188: * no characters in the input buffer.
189: */
190: getchr(fd)
191: register FILE *fd;
192: {
193: long nchar;
194: long readfds, s_readfds;
195: int driver_mask, stdin_mask;
196: int nfds, s_nfds;
197: extern int errno;
198:
199: driver_mask = 1L << fileno(fd);
200: stdin_mask = 1L << fileno(stdin);
201: s_readfds = driver_mask | stdin_mask;
202: s_nfds = (driver_mask > stdin_mask) ? driver_mask : stdin_mask;
203: s_nfds++;
204:
205: one_more_time:
206: do {
207: errno = 0;
208: readfds = s_readfds;
209: nfds = s_nfds;
210: # ifndef OLDIPC
211: nfds = select(nfds, &readfds, NULL, NULL, NULL);
212: # else OLDIPC
213: nfds = select(nfds, &readfds, (int *) NULL, 32767);
214: # endif OLDIPC
215: } while (nfds <= 0 && errno == EINTR);
216:
217: if (readfds & stdin_mask)
218: send_stuff();
219: if ((readfds & driver_mask) == 0)
220: goto one_more_time;
221: return _filbuf(fd);
222: }
223:
224: /*
225: * send_stuff:
226: * Send standard input characters to the driver
227: */
228: send_stuff()
229: {
230: register int count;
231: register char *sp, *nsp;
232: static char inp[sizeof Buf];
233: extern char map_key[256];
234:
235: count = read(fileno(stdin), Buf, sizeof Buf);
236: if (count <= 0)
237: return;
238: if (nchar_send <= 0) {
239: (void) write(1, "\7", 1);
240: return;
241: }
242:
243: /*
244: * look for 'q'uit commands; if we find one,
245: * confirm it. If it is not confirmed, strip
246: * it out of the input
247: */
248: Buf[count] = '\0';
249: nsp = inp;
250: for (sp = Buf; *sp != '\0'; sp++)
251: if ((*nsp = map_key[*sp]) == 'q')
252: intr();
253: # ifdef OTTO
254: else if (*nsp == CTRL(O))
255: Otto_mode = !Otto_mode;
256: # endif OTTO
257: else
258: nsp++;
259: count = nsp - inp;
260: if (count) {
261: # ifdef OTTO
262: Otto_count += count;
263: # endif OTTO
264: nchar_send -= count;
265: if (nchar_send < 0)
266: count += nchar_send;
267: (void) write(Socket, inp, count);
268: }
269: }
270:
271: /*
272: * quit:
273: * Handle the end of the game when the player dies
274: */
275: quit()
276: {
277: register int explain, ch;
278:
279: if (Last_player)
280: return TRUE;
281: # ifdef OTTO
282: if (Otto_mode)
283: return FALSE;
284: # endif OTTO
285: mvcur(cur_row, cur_col, HEIGHT, 0);
286: cur_row = HEIGHT;
287: cur_col = 0;
288: put_str("Re-enter game? ");
289: clear_eol();
290: fflush(stdout);
291: explain = FALSE;
292: for (;;) {
293: if (isupper(ch = getchar()))
294: ch = tolower(ch);
295: if (ch == 'y') {
296: sleep(2);
297: return FALSE;
298: }
299: else if (ch == 'n')
300: return TRUE;
301: (void) putchar(CTRL(G));
302: if (!explain) {
303: put_str("(Y or N) ");
304: explain = TRUE;
305: }
306: fflush(stdout);
307: }
308: }
309:
310: put_ch(ch)
311: char ch;
312: {
313: if (!isprint(ch)) {
314: fprintf(stderr, "r,c,ch: %d,%d,%d", cur_row, cur_col, ch);
315: return;
316: }
317: screen[cur_row][cur_col] = ch;
318: putchar(ch);
319: if (++cur_col >= COLS) {
320: if (!AM || XN)
321: putchar('\n');
322: cur_col = 0;
323: if (++cur_row >= LINES)
324: cur_row = LINES;
325: }
326: }
327:
328: put_str(s)
329: char *s;
330: {
331: while (*s)
332: put_ch(*s++);
333: }
334:
335: clear_screen()
336: {
337: register int i;
338:
339: if (blanks[0] == '\0')
340: for (i = 0; i < 80; i++)
341: blanks[i] = ' ';
342:
343: if (CL != NULL) {
344: tputs(CL, LINES, _putchar);
345: for (i = 0; i < 24; i++)
346: bcopy(blanks, screen[i], 80);
347: } else {
348: for (i = 0; i < 24; i++) {
349: mvcur(cur_row, cur_col, i, 0);
350: cur_row = i;
351: cur_col = 0;
352: clear_eol();
353: }
354: mvcur(cur_row, cur_col, 0, 0);
355: }
356: cur_row = cur_col = 0;
357: }
358:
359: clear_eol()
360: {
361: if (CE != NULL)
362: tputs(CE, 1, _putchar);
363: else {
364: fwrite(blanks, sizeof (char), 80 - cur_col, stdout);
365: if (COLS != 80)
366: mvcur(cur_row, 80, cur_row, cur_col);
367: else if (AM)
368: mvcur(cur_row + 1, 0, cur_row, cur_col);
369: else
370: mvcur(cur_row, 79, cur_row, cur_col);
371: }
372: bcopy(blanks, &screen[cur_row][cur_col], 80 - cur_col);
373: }
374:
375: redraw_screen()
376: {
377: register int i;
378: static int first = 1;
379:
380: if (first) {
381: if ((curscr = newwin(24, 80, 0, 0)) == NULL) {
382: fprintf(stderr, "Can't create curscr\n");
383: exit(1);
384: }
385: for (i = 0; i < 24; i++)
386: curscr->_y[i] = screen[i];
387: first = 0;
388: }
389: curscr->_cury = cur_row;
390: curscr->_curx = cur_col;
391: wrefresh(curscr);
392: #ifdef NOCURSES
393: mvcur(cur_row, cur_col, 0, 0);
394: for (i = 0; i < 23; i++) {
395: fwrite(screen[i], sizeof (char), 80, stdout);
396: if (COLS > 80 || (COLS == 80 && !AM))
397: putchar('\n');
398: }
399: fwrite(screen[23], sizeof (char), 79, stdout);
400: mvcur(23, 79, cur_row, cur_col);
401: #endif
402: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.