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