Home | History | Annotate | Line # | Download | only in huntd
draw.c revision 1.8.12.1
      1  1.8.12.1       tls /*	$NetBSD: draw.c,v 1.8.12.1 2014/08/20 00:00:23 tls Exp $	*/
      2       1.1       mrg /*
      3       1.3       wiz  * Copyright (c) 1983-2003, Regents of the University of California.
      4       1.3       wiz  * All rights reserved.
      5       1.3       wiz  *
      6       1.3       wiz  * Redistribution and use in source and binary forms, with or without
      7       1.3       wiz  * modification, are permitted provided that the following conditions are
      8       1.3       wiz  * met:
      9       1.3       wiz  *
     10       1.3       wiz  * + Redistributions of source code must retain the above copyright
     11       1.3       wiz  *   notice, this list of conditions and the following disclaimer.
     12       1.3       wiz  * + Redistributions in binary form must reproduce the above copyright
     13       1.3       wiz  *   notice, this list of conditions and the following disclaimer in the
     14       1.3       wiz  *   documentation and/or other materials provided with the distribution.
     15       1.3       wiz  * + Neither the name of the University of California, San Francisco nor
     16       1.3       wiz  *   the names of its contributors may be used to endorse or promote
     17       1.3       wiz  *   products derived from this software without specific prior written
     18       1.3       wiz  *   permission.
     19       1.3       wiz  *
     20       1.3       wiz  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
     21       1.3       wiz  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22       1.3       wiz  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     23       1.3       wiz  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     24       1.3       wiz  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     25       1.3       wiz  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     26       1.3       wiz  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27       1.3       wiz  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28       1.3       wiz  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29       1.3       wiz  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     30       1.3       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.8.12.1       tls __RCSID("$NetBSD: draw.c,v 1.8.12.1 2014/08/20 00:00:23 tls Exp $");
     36       1.2     lukem #endif /* not lint */
     37       1.2     lukem 
     38       1.7  dholland #include "hunt.h"
     39       1.1       mrg 
     40       1.8  dholland static void drawstatus(PLAYER *);
     41       1.8  dholland static void see(PLAYER *, int);
     42       1.8  dholland static char translate(char);
     43       1.8  dholland static int player_sym(PLAYER *, int, int);
     44       1.8  dholland 
     45       1.2     lukem void
     46       1.6  dholland drawmaze(PLAYER *pp)
     47       1.1       mrg {
     48       1.7  dholland 	int x;
     49       1.7  dholland 	char *sp;
     50       1.7  dholland 	int y;
     51       1.7  dholland 	char *endp;
     52       1.1       mrg 
     53       1.1       mrg 	clrscr(pp);
     54       1.1       mrg 	outstr(pp, pp->p_maze[0], WIDTH);
     55       1.1       mrg 	for (y = 1; y < HEIGHT - 1; y++) {
     56       1.1       mrg 		endp = &pp->p_maze[y][WIDTH];
     57       1.1       mrg 		for (x = 0, sp = pp->p_maze[y]; sp < endp; x++, sp++)
     58       1.1       mrg 			if (*sp != SPACE) {
     59       1.1       mrg 				cgoto(pp, y, x);
     60       1.1       mrg 				if (pp->p_x == x && pp->p_y == y)
     61       1.1       mrg 					outch(pp, translate(*sp));
     62       1.1       mrg 				else if (isplayer(*sp))
     63       1.1       mrg 					outch(pp, player_sym(pp, y, x));
     64       1.1       mrg 				else
     65       1.1       mrg 					outch(pp, *sp);
     66       1.1       mrg 			}
     67       1.1       mrg 	}
     68       1.1       mrg 	cgoto(pp, HEIGHT - 1, 0);
     69       1.1       mrg 	outstr(pp, pp->p_maze[HEIGHT - 1], WIDTH);
     70       1.1       mrg 	drawstatus(pp);
     71       1.1       mrg }
     72       1.1       mrg 
     73       1.1       mrg /*
     74       1.1       mrg  * drawstatus - put up the status lines (this assumes the screen
     75       1.1       mrg  *		size is 80x24 with the maze being 64x24)
     76       1.1       mrg  */
     77       1.8  dholland static void
     78       1.6  dholland drawstatus(PLAYER *pp)
     79       1.1       mrg {
     80       1.7  dholland 	int i;
     81       1.7  dholland 	PLAYER *np;
     82       1.1       mrg 
     83       1.1       mrg 	cgoto(pp, STAT_AMMO_ROW, STAT_LABEL_COL);
     84       1.1       mrg 	outstr(pp, "Ammo:", 5);
     85       1.5  dholland 	(void) snprintf(Buf, sizeof(Buf), "%3d", pp->p_ammo);
     86       1.1       mrg 	cgoto(pp, STAT_AMMO_ROW, STAT_VALUE_COL);
     87       1.1       mrg 	outstr(pp, Buf, 3);
     88       1.1       mrg 
     89       1.1       mrg 	cgoto(pp, STAT_GUN_ROW, STAT_LABEL_COL);
     90       1.1       mrg 	outstr(pp, "Gun:", 4);
     91       1.1       mrg 	cgoto(pp, STAT_GUN_ROW, STAT_VALUE_COL);
     92       1.1       mrg 	outstr(pp, (pp->p_ncshot < MAXNCSHOT) ? " ok" : "   ", 3);
     93       1.1       mrg 
     94       1.1       mrg 	cgoto(pp, STAT_DAM_ROW, STAT_LABEL_COL);
     95       1.1       mrg 	outstr(pp, "Damage:", 7);
     96       1.5  dholland 	(void) snprintf(Buf, sizeof(Buf), "%2d/%2d", pp->p_damage,
     97       1.5  dholland 		pp->p_damcap);
     98       1.1       mrg 	cgoto(pp, STAT_DAM_ROW, STAT_VALUE_COL);
     99       1.1       mrg 	outstr(pp, Buf, 5);
    100       1.1       mrg 
    101       1.1       mrg 	cgoto(pp, STAT_KILL_ROW, STAT_LABEL_COL);
    102       1.1       mrg 	outstr(pp, "Kills:", 6);
    103       1.5  dholland 	(void) snprintf(Buf, sizeof(Buf), "%3d", (pp->p_damcap - MAXDAM) / 2);
    104       1.1       mrg 	cgoto(pp, STAT_KILL_ROW, STAT_VALUE_COL);
    105       1.1       mrg 	outstr(pp, Buf, 3);
    106       1.1       mrg 
    107       1.1       mrg 	cgoto(pp, STAT_PLAY_ROW, STAT_LABEL_COL);
    108       1.1       mrg 	outstr(pp, "Player:", 7);
    109       1.1       mrg 	for (i = STAT_PLAY_ROW + 1, np = Player; np < End_player; np++) {
    110       1.5  dholland 		(void) snprintf(Buf, sizeof(Buf), "%5.2f%c%-10.10s %c",
    111       1.5  dholland 			np->p_ident->i_score,
    112       1.1       mrg 			stat_char(np), np->p_ident->i_name,
    113       1.1       mrg 			np->p_ident->i_team);
    114       1.1       mrg 		cgoto(pp, i++, STAT_NAME_COL);
    115       1.1       mrg 		outstr(pp, Buf, STAT_NAME_LEN);
    116       1.1       mrg 	}
    117       1.1       mrg 
    118       1.7  dholland #ifdef MONITOR
    119       1.1       mrg 	cgoto(pp, STAT_MON_ROW, STAT_LABEL_COL);
    120       1.1       mrg 	outstr(pp, "Monitor:", 8);
    121       1.1       mrg 	for (i = STAT_MON_ROW + 1, np = Monitor; np < End_monitor; np++) {
    122       1.5  dholland 		(void) snprintf(Buf, sizeof(Buf), "%5.5s %-10.10s %c", " ",
    123       1.1       mrg 			np->p_ident->i_name, np->p_ident->i_team);
    124       1.1       mrg 		cgoto(pp, i++, STAT_NAME_COL);
    125       1.1       mrg 		outstr(pp, Buf, STAT_NAME_LEN);
    126       1.1       mrg 	}
    127       1.7  dholland #endif
    128       1.1       mrg }
    129       1.1       mrg 
    130       1.2     lukem void
    131       1.6  dholland look(PLAYER *pp)
    132       1.1       mrg {
    133       1.7  dholland 	int x, y;
    134       1.1       mrg 
    135       1.1       mrg 	x = pp->p_x;
    136       1.1       mrg 	y = pp->p_y;
    137       1.1       mrg 
    138       1.1       mrg 	check(pp, y - 1, x - 1);
    139       1.1       mrg 	check(pp, y - 1, x    );
    140       1.1       mrg 	check(pp, y - 1, x + 1);
    141       1.1       mrg 	check(pp, y    , x - 1);
    142       1.1       mrg 	check(pp, y    , x    );
    143       1.1       mrg 	check(pp, y    , x + 1);
    144       1.1       mrg 	check(pp, y + 1, x - 1);
    145       1.1       mrg 	check(pp, y + 1, x    );
    146       1.1       mrg 	check(pp, y + 1, x + 1);
    147       1.1       mrg 
    148       1.1       mrg 	switch (pp->p_face) {
    149       1.1       mrg 	  case LEFTS:
    150       1.1       mrg 		see(pp, LEFTS);
    151       1.1       mrg 		see(pp, ABOVE);
    152       1.1       mrg 		see(pp, BELOW);
    153       1.1       mrg 		break;
    154       1.1       mrg 	  case RIGHT:
    155       1.1       mrg 		see(pp, RIGHT);
    156       1.1       mrg 		see(pp, ABOVE);
    157       1.1       mrg 		see(pp, BELOW);
    158       1.1       mrg 		break;
    159       1.1       mrg 	  case ABOVE:
    160       1.1       mrg 		see(pp, ABOVE);
    161       1.1       mrg 		see(pp, LEFTS);
    162       1.1       mrg 		see(pp, RIGHT);
    163       1.1       mrg 		break;
    164       1.1       mrg 	  case BELOW:
    165       1.1       mrg 		see(pp, BELOW);
    166       1.1       mrg 		see(pp, LEFTS);
    167       1.1       mrg 		see(pp, RIGHT);
    168       1.1       mrg 		break;
    169       1.7  dholland #ifdef FLY
    170       1.1       mrg 	  case FLYER:
    171       1.1       mrg 		break;
    172       1.7  dholland #endif
    173       1.1       mrg 	}
    174       1.1       mrg 	cgoto(pp, y, x);
    175       1.1       mrg }
    176       1.1       mrg 
    177       1.8  dholland static void
    178       1.6  dholland see(PLAYER *pp, int face)
    179       1.1       mrg {
    180       1.7  dholland 	char *sp;
    181       1.7  dholland 	int y, x, i, cnt;
    182       1.1       mrg 
    183       1.1       mrg 	x = pp->p_x;
    184       1.1       mrg 	y = pp->p_y;
    185       1.1       mrg 
    186       1.1       mrg 	switch (face) {
    187       1.1       mrg 	  case LEFTS:
    188       1.1       mrg 		sp = &Maze[y][x];
    189       1.2     lukem 		for (i = 0; See_over[(int)*--sp]; i++)
    190       1.1       mrg 			continue;
    191       1.1       mrg 
    192       1.1       mrg 		if (i == 0)
    193       1.1       mrg 			break;
    194       1.1       mrg 
    195       1.1       mrg 		cnt = i;
    196       1.1       mrg 		x = pp->p_x - 1;
    197       1.1       mrg 		--y;
    198       1.1       mrg 		while (i--)
    199       1.1       mrg 			check(pp, y, --x);
    200       1.1       mrg 		i = cnt;
    201       1.1       mrg 		x = pp->p_x - 1;
    202       1.1       mrg 		++y;
    203       1.1       mrg 		while (i--)
    204       1.1       mrg 			check(pp, y, --x);
    205       1.1       mrg 		i = cnt;
    206       1.1       mrg 		x = pp->p_x - 1;
    207       1.1       mrg 		++y;
    208       1.1       mrg 		while (i--)
    209       1.1       mrg 			check(pp, y, --x);
    210       1.1       mrg 		break;
    211       1.1       mrg 	  case RIGHT:
    212       1.1       mrg 		sp = &Maze[y][++x];
    213       1.2     lukem 		for (i = 0; See_over[(int)*sp++]; i++)
    214       1.1       mrg 			continue;
    215       1.1       mrg 
    216       1.1       mrg 		if (i == 0)
    217       1.1       mrg 			break;
    218       1.1       mrg 
    219       1.1       mrg 		cnt = i;
    220       1.1       mrg 		x = pp->p_x + 1;
    221       1.1       mrg 		--y;
    222       1.1       mrg 		while (i--)
    223       1.1       mrg 			check(pp, y, ++x);
    224       1.1       mrg 		i = cnt;
    225       1.1       mrg 		x = pp->p_x + 1;
    226       1.1       mrg 		++y;
    227       1.1       mrg 		while (i--)
    228       1.1       mrg 			check(pp, y, ++x);
    229       1.1       mrg 		i = cnt;
    230       1.1       mrg 		x = pp->p_x + 1;
    231       1.1       mrg 		++y;
    232       1.1       mrg 		while (i--)
    233       1.1       mrg 			check(pp, y, ++x);
    234       1.1       mrg 		break;
    235       1.1       mrg 	  case ABOVE:
    236       1.1       mrg 		sp = &Maze[--y][x];
    237       1.2     lukem 		if (!See_over[(int)*sp])
    238       1.1       mrg 			break;
    239       1.1       mrg 		do {
    240       1.1       mrg 			--y;
    241       1.1       mrg 			sp -= sizeof Maze[0];
    242       1.1       mrg 			check(pp, y, x - 1);
    243       1.1       mrg 			check(pp, y, x    );
    244       1.1       mrg 			check(pp, y, x + 1);
    245       1.2     lukem 		} while (See_over[(int)*sp]);
    246       1.1       mrg 		break;
    247       1.1       mrg 	  case BELOW:
    248       1.1       mrg 		sp = &Maze[++y][x];
    249       1.2     lukem 		if (!See_over[(int)*sp])
    250       1.1       mrg 			break;
    251       1.1       mrg 		do {
    252       1.1       mrg 			y++;
    253       1.1       mrg 			sp += sizeof Maze[0];
    254       1.1       mrg 			check(pp, y, x - 1);
    255       1.1       mrg 			check(pp, y, x    );
    256       1.1       mrg 			check(pp, y, x + 1);
    257       1.2     lukem 		} while (See_over[(int)*sp]);
    258       1.1       mrg 		break;
    259       1.1       mrg 	}
    260       1.1       mrg }
    261       1.1       mrg 
    262       1.2     lukem void
    263       1.6  dholland check(PLAYER *pp, int y, int x)
    264       1.1       mrg {
    265       1.7  dholland 	int indx;
    266       1.7  dholland 	int ch;
    267       1.7  dholland 	PLAYER *rpp;
    268       1.1       mrg 
    269       1.4  dholland 	indx = y * sizeof Maze[0] + x;
    270       1.4  dholland 	ch = ((char *) Maze)[indx];
    271       1.4  dholland 	if (ch != ((char *) pp->p_maze)[indx]) {
    272       1.1       mrg 		rpp = pp;
    273       1.1       mrg 		cgoto(rpp, y, x);
    274       1.1       mrg 		if (x == rpp->p_x && y == rpp->p_y)
    275       1.1       mrg 			outch(rpp, translate(ch));
    276       1.1       mrg 		else if (isplayer(ch))
    277       1.1       mrg 			outch(rpp, player_sym(rpp, y, x));
    278       1.1       mrg 		else
    279       1.1       mrg 			outch(rpp, ch);
    280       1.4  dholland 		((char *) rpp->p_maze)[indx] = ch;
    281       1.1       mrg 	}
    282       1.1       mrg }
    283       1.1       mrg 
    284       1.1       mrg /*
    285       1.1       mrg  * showstat
    286       1.1       mrg  *	Update the status of players
    287       1.1       mrg  */
    288       1.2     lukem void
    289       1.6  dholland showstat(PLAYER *pp)
    290       1.1       mrg {
    291       1.7  dholland 	PLAYER *np;
    292       1.7  dholland 	int y;
    293       1.7  dholland 	char c;
    294       1.1       mrg 
    295       1.1       mrg 	y = STAT_PLAY_ROW + 1 + (pp - Player);
    296       1.1       mrg 	c = stat_char(pp);
    297       1.7  dholland #ifdef MONITOR
    298       1.1       mrg 	for (np = Monitor; np < End_monitor; np++) {
    299       1.1       mrg 		cgoto(np, y, STAT_SCAN_COL);
    300       1.1       mrg 		outch(np, c);
    301       1.1       mrg 	}
    302       1.7  dholland #endif
    303       1.1       mrg 	for (np = Player; np < End_player; np++) {
    304       1.1       mrg 		cgoto(np, y, STAT_SCAN_COL);
    305       1.1       mrg 		outch(np, c);
    306       1.1       mrg 	}
    307       1.1       mrg }
    308       1.1       mrg 
    309       1.1       mrg /*
    310       1.1       mrg  * drawplayer:
    311       1.1       mrg  *	Draw the player on the screen and show him to everyone who's scanning
    312       1.1       mrg  *	unless he is cloaked.
    313       1.1       mrg  */
    314       1.2     lukem void
    315  1.8.12.1       tls drawplayer(PLAYER *pp, bool draw)
    316       1.1       mrg {
    317       1.7  dholland 	PLAYER *newp;
    318       1.7  dholland 	int x, y;
    319       1.1       mrg 
    320       1.1       mrg 	x = pp->p_x;
    321       1.1       mrg 	y = pp->p_y;
    322       1.1       mrg 	Maze[y][x] = draw ? pp->p_face : pp->p_over;
    323       1.1       mrg 
    324       1.7  dholland #ifdef MONITOR
    325       1.1       mrg 	for (newp = Monitor; newp < End_monitor; newp++)
    326       1.1       mrg 		check(newp, y, x);
    327       1.7  dholland #endif
    328       1.1       mrg 
    329       1.1       mrg 	for (newp = Player; newp < End_player; newp++) {
    330       1.1       mrg 		if (!draw || newp == pp) {
    331       1.1       mrg 			check(newp, y, x);
    332       1.1       mrg 			continue;
    333       1.1       mrg 		}
    334       1.1       mrg 		if (newp->p_scan == 0) {
    335       1.1       mrg 			newp->p_scan--;
    336       1.1       mrg 			showstat(newp);
    337       1.1       mrg 		}
    338       1.1       mrg 		else if (newp->p_scan > 0) {
    339       1.1       mrg 			if (pp->p_cloak < 0)
    340       1.1       mrg 				check(newp, y, x);
    341       1.1       mrg 			newp->p_scan--;
    342       1.1       mrg 		}
    343       1.1       mrg 	}
    344       1.1       mrg 	if (!draw || pp->p_cloak < 0)
    345       1.1       mrg 		return;
    346       1.1       mrg 	if (pp->p_cloak-- == 0)
    347       1.1       mrg 		showstat(pp);
    348       1.1       mrg }
    349       1.1       mrg 
    350       1.2     lukem void
    351       1.6  dholland message(PLAYER *pp, const char *s)
    352       1.1       mrg {
    353       1.1       mrg 	cgoto(pp, HEIGHT, 0);
    354       1.1       mrg 	outstr(pp, s, strlen(s));
    355       1.1       mrg 	ce(pp);
    356       1.1       mrg }
    357       1.1       mrg 
    358       1.1       mrg /*
    359       1.1       mrg  * translate:
    360       1.1       mrg  *	Turn a character into the right direction character if we are
    361       1.1       mrg  *	looking at the current player.
    362       1.1       mrg  */
    363       1.8  dholland static char
    364       1.6  dholland translate(char ch)
    365       1.1       mrg {
    366       1.1       mrg 	switch (ch) {
    367       1.1       mrg 	  case LEFTS:
    368       1.1       mrg 		return '<';
    369       1.1       mrg 	  case RIGHT:
    370       1.1       mrg 		return '>';
    371       1.1       mrg 	  case ABOVE:
    372       1.1       mrg 		return '^';
    373       1.1       mrg 	  case BELOW:
    374       1.1       mrg 		return 'v';
    375       1.1       mrg 	}
    376       1.1       mrg 	return ch;
    377       1.1       mrg }
    378       1.1       mrg 
    379       1.1       mrg /*
    380       1.1       mrg  * player_sym:
    381       1.1       mrg  *	Return the player symbol
    382       1.1       mrg  */
    383       1.8  dholland static int
    384       1.6  dholland player_sym(PLAYER *pp, int y, int x)
    385       1.1       mrg {
    386       1.7  dholland 	PLAYER *npp;
    387       1.1       mrg 
    388       1.1       mrg 	npp = play_at(y, x);
    389       1.1       mrg 	if (npp->p_ident->i_team == ' ')
    390       1.1       mrg 		return Maze[y][x];
    391       1.1       mrg #ifdef MONITOR
    392       1.1       mrg 	if (pp->p_ident->i_team == '*')
    393       1.1       mrg 		return npp->p_ident->i_team;
    394       1.1       mrg #endif
    395       1.1       mrg 	if (pp->p_ident->i_team != npp->p_ident->i_team)
    396       1.1       mrg 		return Maze[y][x];
    397       1.1       mrg 	return pp->p_ident->i_team;
    398       1.1       mrg }
    399