Home | History | Annotate | Line # | Download | only in huntd
terminal.c revision 1.4
      1  1.4    wiz /*	$NetBSD: terminal.c,v 1.4 2003/06/11 12:00:23 wiz Exp $	*/
      2  1.1    mrg /*
      3  1.4    wiz  * Copyright (c) 1983-2003, Regents of the University of California.
      4  1.4    wiz  * All rights reserved.
      5  1.4    wiz  *
      6  1.4    wiz  * Redistribution and use in source and binary forms, with or without
      7  1.4    wiz  * modification, are permitted provided that the following conditions are
      8  1.4    wiz  * met:
      9  1.4    wiz  *
     10  1.4    wiz  * + Redistributions of source code must retain the above copyright
     11  1.4    wiz  *   notice, this list of conditions and the following disclaimer.
     12  1.4    wiz  * + Redistributions in binary form must reproduce the above copyright
     13  1.4    wiz  *   notice, this list of conditions and the following disclaimer in the
     14  1.4    wiz  *   documentation and/or other materials provided with the distribution.
     15  1.4    wiz  * + Neither the name of the University of California, San Francisco nor
     16  1.4    wiz  *   the names of its contributors may be used to endorse or promote
     17  1.4    wiz  *   products derived from this software without specific prior written
     18  1.4    wiz  *   permission.
     19  1.4    wiz  *
     20  1.4    wiz  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
     21  1.4    wiz  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  1.4    wiz  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     23  1.4    wiz  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     24  1.4    wiz  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     25  1.4    wiz  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     26  1.4    wiz  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.4    wiz  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.4    wiz  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.4    wiz  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     30  1.4    wiz  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.1    mrg  */
     32  1.1    mrg 
     33  1.2  lukem #include <sys/cdefs.h>
     34  1.2  lukem #ifndef lint
     35  1.4    wiz __RCSID("$NetBSD: terminal.c,v 1.4 2003/06/11 12:00:23 wiz Exp $");
     36  1.2  lukem #endif /* not lint */
     37  1.2  lukem 
     38  1.2  lukem #include <stdarg.h>
     39  1.3    wiz #include "hunt.h"
     40  1.3    wiz #define	TERM_WIDTH	80	/* Assume terminals are 80-char wide */
     41  1.1    mrg 
     42  1.1    mrg /*
     43  1.1    mrg  * cgoto:
     44  1.1    mrg  *	Move the cursor to the given position on the given player's
     45  1.1    mrg  *	terminal.
     46  1.1    mrg  */
     47  1.2  lukem void
     48  1.1    mrg cgoto(pp, y, x)
     49  1.2  lukem 	PLAYER	*pp;
     50  1.2  lukem 	int	y, x;
     51  1.1    mrg {
     52  1.1    mrg 	if (x == pp->p_curx && y == pp->p_cury)
     53  1.1    mrg 		return;
     54  1.1    mrg 	sendcom(pp, MOVE, y, x);
     55  1.1    mrg 	pp->p_cury = y;
     56  1.1    mrg 	pp->p_curx = x;
     57  1.1    mrg }
     58  1.1    mrg 
     59  1.1    mrg /*
     60  1.1    mrg  * outch:
     61  1.1    mrg  *	Put out a single character.
     62  1.1    mrg  */
     63  1.2  lukem void
     64  1.1    mrg outch(pp, ch)
     65  1.2  lukem 	PLAYER	*pp;
     66  1.2  lukem 	char	ch;
     67  1.1    mrg {
     68  1.1    mrg 	if (++pp->p_curx >= TERM_WIDTH) {
     69  1.1    mrg 		pp->p_curx = 0;
     70  1.1    mrg 		pp->p_cury++;
     71  1.1    mrg 	}
     72  1.1    mrg 	(void) putc(ch, pp->p_output);
     73  1.1    mrg }
     74  1.1    mrg 
     75  1.1    mrg /*
     76  1.1    mrg  * outstr:
     77  1.1    mrg  *	Put out a string of the given length.
     78  1.1    mrg  */
     79  1.2  lukem void
     80  1.1    mrg outstr(pp, str, len)
     81  1.2  lukem 	PLAYER	*pp;
     82  1.2  lukem 	char	*str;
     83  1.2  lukem 	int	len;
     84  1.1    mrg {
     85  1.1    mrg 	pp->p_curx += len;
     86  1.1    mrg 	pp->p_cury += (pp->p_curx / TERM_WIDTH);
     87  1.1    mrg 	pp->p_curx %= TERM_WIDTH;
     88  1.1    mrg 	while (len--)
     89  1.1    mrg 		(void) putc(*str++, pp->p_output);
     90  1.1    mrg }
     91  1.1    mrg 
     92  1.1    mrg /*
     93  1.1    mrg  * clrscr:
     94  1.1    mrg  *	Clear the screen, and reset the current position on the screen.
     95  1.1    mrg  */
     96  1.2  lukem void
     97  1.1    mrg clrscr(pp)
     98  1.2  lukem 	PLAYER	*pp;
     99  1.1    mrg {
    100  1.1    mrg 	sendcom(pp, CLEAR);
    101  1.1    mrg 	pp->p_cury = 0;
    102  1.1    mrg 	pp->p_curx = 0;
    103  1.1    mrg }
    104  1.1    mrg 
    105  1.1    mrg /*
    106  1.1    mrg  * ce:
    107  1.1    mrg  *	Clear to the end of the line
    108  1.1    mrg  */
    109  1.2  lukem void
    110  1.1    mrg ce(pp)
    111  1.2  lukem 	PLAYER	*pp;
    112  1.1    mrg {
    113  1.1    mrg 	sendcom(pp, CLRTOEOL);
    114  1.1    mrg }
    115  1.1    mrg 
    116  1.2  lukem #if 0		/* XXX lukem*/
    117  1.1    mrg /*
    118  1.1    mrg  * ref;
    119  1.1    mrg  *	Refresh the screen
    120  1.1    mrg  */
    121  1.2  lukem void
    122  1.1    mrg ref(pp)
    123  1.2  lukem 	PLAYER	*pp;
    124  1.1    mrg {
    125  1.1    mrg 	sendcom(pp, REFRESH);
    126  1.1    mrg }
    127  1.2  lukem #endif
    128  1.1    mrg 
    129  1.1    mrg /*
    130  1.1    mrg  * sendcom:
    131  1.1    mrg  *	Send a command to the given user
    132  1.1    mrg  */
    133  1.2  lukem void
    134  1.2  lukem sendcom(PLAYER *pp, int command, ...)
    135  1.2  lukem {
    136  1.2  lukem 	va_list	ap;
    137  1.2  lukem 	int	arg1, arg2;
    138  1.3    wiz 
    139  1.2  lukem 	va_start(ap, command);
    140  1.1    mrg 	(void) putc(command, pp->p_output);
    141  1.1    mrg 	switch (command & 0377) {
    142  1.2  lukem 	case MOVE:
    143  1.2  lukem 		arg1 = va_arg(ap, int);
    144  1.2  lukem 		arg2 = va_arg(ap, int);
    145  1.1    mrg 		(void) putc(arg1, pp->p_output);
    146  1.1    mrg 		(void) putc(arg2, pp->p_output);
    147  1.1    mrg 		break;
    148  1.2  lukem 	case ADDCH:
    149  1.2  lukem 	case READY:
    150  1.2  lukem 		arg1 = va_arg(ap, int);
    151  1.1    mrg 		(void) putc(arg1, pp->p_output);
    152  1.1    mrg 		break;
    153  1.1    mrg 	}
    154  1.2  lukem 
    155  1.2  lukem 	va_end(ap);		/* No return needed for void functions. */
    156  1.1    mrg }
    157