draw.c revision 1.5 1 1.5 dholland /* $NetBSD: draw.c,v 1.5 2009/06/28 21:12:35 dholland 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.5 dholland __RCSID("$NetBSD: draw.c,v 1.5 2009/06/28 21:12:35 dholland Exp $");
36 1.2 lukem #endif /* not lint */
37 1.2 lukem
38 1.1 mrg # include "hunt.h"
39 1.1 mrg
40 1.2 lukem void
41 1.1 mrg drawmaze(pp)
42 1.2 lukem PLAYER *pp;
43 1.1 mrg {
44 1.2 lukem int x;
45 1.2 lukem char *sp;
46 1.2 lukem int y;
47 1.2 lukem char *endp;
48 1.1 mrg
49 1.1 mrg clrscr(pp);
50 1.1 mrg outstr(pp, pp->p_maze[0], WIDTH);
51 1.1 mrg for (y = 1; y < HEIGHT - 1; y++) {
52 1.1 mrg endp = &pp->p_maze[y][WIDTH];
53 1.1 mrg for (x = 0, sp = pp->p_maze[y]; sp < endp; x++, sp++)
54 1.1 mrg if (*sp != SPACE) {
55 1.1 mrg cgoto(pp, y, x);
56 1.1 mrg if (pp->p_x == x && pp->p_y == y)
57 1.1 mrg outch(pp, translate(*sp));
58 1.1 mrg else if (isplayer(*sp))
59 1.1 mrg outch(pp, player_sym(pp, y, x));
60 1.1 mrg else
61 1.1 mrg outch(pp, *sp);
62 1.1 mrg }
63 1.1 mrg }
64 1.1 mrg cgoto(pp, HEIGHT - 1, 0);
65 1.1 mrg outstr(pp, pp->p_maze[HEIGHT - 1], WIDTH);
66 1.1 mrg drawstatus(pp);
67 1.1 mrg }
68 1.1 mrg
69 1.1 mrg /*
70 1.1 mrg * drawstatus - put up the status lines (this assumes the screen
71 1.1 mrg * size is 80x24 with the maze being 64x24)
72 1.1 mrg */
73 1.2 lukem void
74 1.1 mrg drawstatus(pp)
75 1.2 lukem PLAYER *pp;
76 1.1 mrg {
77 1.2 lukem int i;
78 1.2 lukem PLAYER *np;
79 1.1 mrg
80 1.1 mrg cgoto(pp, STAT_AMMO_ROW, STAT_LABEL_COL);
81 1.1 mrg outstr(pp, "Ammo:", 5);
82 1.5 dholland (void) snprintf(Buf, sizeof(Buf), "%3d", pp->p_ammo);
83 1.1 mrg cgoto(pp, STAT_AMMO_ROW, STAT_VALUE_COL);
84 1.1 mrg outstr(pp, Buf, 3);
85 1.1 mrg
86 1.1 mrg cgoto(pp, STAT_GUN_ROW, STAT_LABEL_COL);
87 1.1 mrg outstr(pp, "Gun:", 4);
88 1.1 mrg cgoto(pp, STAT_GUN_ROW, STAT_VALUE_COL);
89 1.1 mrg outstr(pp, (pp->p_ncshot < MAXNCSHOT) ? " ok" : " ", 3);
90 1.1 mrg
91 1.1 mrg cgoto(pp, STAT_DAM_ROW, STAT_LABEL_COL);
92 1.1 mrg outstr(pp, "Damage:", 7);
93 1.5 dholland (void) snprintf(Buf, sizeof(Buf), "%2d/%2d", pp->p_damage,
94 1.5 dholland pp->p_damcap);
95 1.1 mrg cgoto(pp, STAT_DAM_ROW, STAT_VALUE_COL);
96 1.1 mrg outstr(pp, Buf, 5);
97 1.1 mrg
98 1.1 mrg cgoto(pp, STAT_KILL_ROW, STAT_LABEL_COL);
99 1.1 mrg outstr(pp, "Kills:", 6);
100 1.5 dholland (void) snprintf(Buf, sizeof(Buf), "%3d", (pp->p_damcap - MAXDAM) / 2);
101 1.1 mrg cgoto(pp, STAT_KILL_ROW, STAT_VALUE_COL);
102 1.1 mrg outstr(pp, Buf, 3);
103 1.1 mrg
104 1.1 mrg cgoto(pp, STAT_PLAY_ROW, STAT_LABEL_COL);
105 1.1 mrg outstr(pp, "Player:", 7);
106 1.1 mrg for (i = STAT_PLAY_ROW + 1, np = Player; np < End_player; np++) {
107 1.5 dholland (void) snprintf(Buf, sizeof(Buf), "%5.2f%c%-10.10s %c",
108 1.5 dholland np->p_ident->i_score,
109 1.1 mrg stat_char(np), np->p_ident->i_name,
110 1.1 mrg np->p_ident->i_team);
111 1.1 mrg cgoto(pp, i++, STAT_NAME_COL);
112 1.1 mrg outstr(pp, Buf, STAT_NAME_LEN);
113 1.1 mrg }
114 1.1 mrg
115 1.1 mrg # ifdef MONITOR
116 1.1 mrg cgoto(pp, STAT_MON_ROW, STAT_LABEL_COL);
117 1.1 mrg outstr(pp, "Monitor:", 8);
118 1.1 mrg for (i = STAT_MON_ROW + 1, np = Monitor; np < End_monitor; np++) {
119 1.5 dholland (void) snprintf(Buf, sizeof(Buf), "%5.5s %-10.10s %c", " ",
120 1.1 mrg np->p_ident->i_name, np->p_ident->i_team);
121 1.1 mrg cgoto(pp, i++, STAT_NAME_COL);
122 1.1 mrg outstr(pp, Buf, STAT_NAME_LEN);
123 1.1 mrg }
124 1.1 mrg # endif
125 1.1 mrg }
126 1.1 mrg
127 1.2 lukem void
128 1.1 mrg look(pp)
129 1.2 lukem PLAYER *pp;
130 1.1 mrg {
131 1.2 lukem int x, y;
132 1.1 mrg
133 1.1 mrg x = pp->p_x;
134 1.1 mrg y = pp->p_y;
135 1.1 mrg
136 1.1 mrg check(pp, y - 1, x - 1);
137 1.1 mrg check(pp, y - 1, x );
138 1.1 mrg check(pp, y - 1, x + 1);
139 1.1 mrg check(pp, y , x - 1);
140 1.1 mrg check(pp, y , x );
141 1.1 mrg check(pp, y , x + 1);
142 1.1 mrg check(pp, y + 1, x - 1);
143 1.1 mrg check(pp, y + 1, x );
144 1.1 mrg check(pp, y + 1, x + 1);
145 1.1 mrg
146 1.1 mrg switch (pp->p_face) {
147 1.1 mrg case LEFTS:
148 1.1 mrg see(pp, LEFTS);
149 1.1 mrg see(pp, ABOVE);
150 1.1 mrg see(pp, BELOW);
151 1.1 mrg break;
152 1.1 mrg case RIGHT:
153 1.1 mrg see(pp, RIGHT);
154 1.1 mrg see(pp, ABOVE);
155 1.1 mrg see(pp, BELOW);
156 1.1 mrg break;
157 1.1 mrg case ABOVE:
158 1.1 mrg see(pp, ABOVE);
159 1.1 mrg see(pp, LEFTS);
160 1.1 mrg see(pp, RIGHT);
161 1.1 mrg break;
162 1.1 mrg case BELOW:
163 1.1 mrg see(pp, BELOW);
164 1.1 mrg see(pp, LEFTS);
165 1.1 mrg see(pp, RIGHT);
166 1.1 mrg break;
167 1.1 mrg # ifdef FLY
168 1.1 mrg case FLYER:
169 1.1 mrg break;
170 1.1 mrg # endif
171 1.1 mrg }
172 1.1 mrg cgoto(pp, y, x);
173 1.1 mrg }
174 1.1 mrg
175 1.2 lukem void
176 1.1 mrg see(pp, face)
177 1.2 lukem PLAYER *pp;
178 1.2 lukem int face;
179 1.1 mrg {
180 1.2 lukem char *sp;
181 1.2 lukem 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.1 mrg check(pp, y, x)
264 1.2 lukem PLAYER *pp;
265 1.2 lukem int y, x;
266 1.1 mrg {
267 1.4 dholland int indx;
268 1.2 lukem int ch;
269 1.2 lukem PLAYER *rpp;
270 1.1 mrg
271 1.4 dholland indx = y * sizeof Maze[0] + x;
272 1.4 dholland ch = ((char *) Maze)[indx];
273 1.4 dholland if (ch != ((char *) pp->p_maze)[indx]) {
274 1.1 mrg rpp = pp;
275 1.1 mrg cgoto(rpp, y, x);
276 1.1 mrg if (x == rpp->p_x && y == rpp->p_y)
277 1.1 mrg outch(rpp, translate(ch));
278 1.1 mrg else if (isplayer(ch))
279 1.1 mrg outch(rpp, player_sym(rpp, y, x));
280 1.1 mrg else
281 1.1 mrg outch(rpp, ch);
282 1.4 dholland ((char *) rpp->p_maze)[indx] = ch;
283 1.1 mrg }
284 1.1 mrg }
285 1.1 mrg
286 1.1 mrg /*
287 1.1 mrg * showstat
288 1.1 mrg * Update the status of players
289 1.1 mrg */
290 1.2 lukem void
291 1.1 mrg showstat(pp)
292 1.2 lukem PLAYER *pp;
293 1.1 mrg {
294 1.2 lukem PLAYER *np;
295 1.2 lukem int y;
296 1.2 lukem char c;
297 1.1 mrg
298 1.1 mrg y = STAT_PLAY_ROW + 1 + (pp - Player);
299 1.1 mrg c = stat_char(pp);
300 1.1 mrg # ifdef MONITOR
301 1.1 mrg for (np = Monitor; np < End_monitor; np++) {
302 1.1 mrg cgoto(np, y, STAT_SCAN_COL);
303 1.1 mrg outch(np, c);
304 1.1 mrg }
305 1.1 mrg # endif
306 1.1 mrg for (np = Player; np < End_player; np++) {
307 1.1 mrg cgoto(np, y, STAT_SCAN_COL);
308 1.1 mrg outch(np, c);
309 1.1 mrg }
310 1.1 mrg }
311 1.1 mrg
312 1.1 mrg /*
313 1.1 mrg * drawplayer:
314 1.1 mrg * Draw the player on the screen and show him to everyone who's scanning
315 1.1 mrg * unless he is cloaked.
316 1.1 mrg */
317 1.2 lukem void
318 1.1 mrg drawplayer(pp, draw)
319 1.2 lukem PLAYER *pp;
320 1.2 lukem FLAG draw;
321 1.1 mrg {
322 1.2 lukem PLAYER *newp;
323 1.2 lukem int x, y;
324 1.1 mrg
325 1.1 mrg x = pp->p_x;
326 1.1 mrg y = pp->p_y;
327 1.1 mrg Maze[y][x] = draw ? pp->p_face : pp->p_over;
328 1.1 mrg
329 1.1 mrg # ifdef MONITOR
330 1.1 mrg for (newp = Monitor; newp < End_monitor; newp++)
331 1.1 mrg check(newp, y, x);
332 1.1 mrg # endif
333 1.1 mrg
334 1.1 mrg for (newp = Player; newp < End_player; newp++) {
335 1.1 mrg if (!draw || newp == pp) {
336 1.1 mrg check(newp, y, x);
337 1.1 mrg continue;
338 1.1 mrg }
339 1.1 mrg if (newp->p_scan == 0) {
340 1.1 mrg newp->p_scan--;
341 1.1 mrg showstat(newp);
342 1.1 mrg }
343 1.1 mrg else if (newp->p_scan > 0) {
344 1.1 mrg if (pp->p_cloak < 0)
345 1.1 mrg check(newp, y, x);
346 1.1 mrg newp->p_scan--;
347 1.1 mrg }
348 1.1 mrg }
349 1.1 mrg if (!draw || pp->p_cloak < 0)
350 1.1 mrg return;
351 1.1 mrg if (pp->p_cloak-- == 0)
352 1.1 mrg showstat(pp);
353 1.1 mrg }
354 1.1 mrg
355 1.2 lukem void
356 1.1 mrg message(pp, s)
357 1.2 lukem PLAYER *pp;
358 1.4 dholland const char *s;
359 1.1 mrg {
360 1.1 mrg cgoto(pp, HEIGHT, 0);
361 1.1 mrg outstr(pp, s, strlen(s));
362 1.1 mrg ce(pp);
363 1.1 mrg }
364 1.1 mrg
365 1.1 mrg /*
366 1.1 mrg * translate:
367 1.1 mrg * Turn a character into the right direction character if we are
368 1.1 mrg * looking at the current player.
369 1.1 mrg */
370 1.2 lukem char
371 1.1 mrg translate(ch)
372 1.2 lukem char ch;
373 1.1 mrg {
374 1.1 mrg switch (ch) {
375 1.1 mrg case LEFTS:
376 1.1 mrg return '<';
377 1.1 mrg case RIGHT:
378 1.1 mrg return '>';
379 1.1 mrg case ABOVE:
380 1.1 mrg return '^';
381 1.1 mrg case BELOW:
382 1.1 mrg return 'v';
383 1.1 mrg }
384 1.1 mrg return ch;
385 1.1 mrg }
386 1.1 mrg
387 1.1 mrg /*
388 1.1 mrg * player_sym:
389 1.1 mrg * Return the player symbol
390 1.1 mrg */
391 1.2 lukem int
392 1.1 mrg player_sym(pp, y, x)
393 1.2 lukem PLAYER *pp;
394 1.2 lukem int y, x;
395 1.1 mrg {
396 1.2 lukem PLAYER *npp;
397 1.1 mrg
398 1.1 mrg npp = play_at(y, x);
399 1.1 mrg if (npp->p_ident->i_team == ' ')
400 1.1 mrg return Maze[y][x];
401 1.1 mrg #ifdef MONITOR
402 1.1 mrg if (pp->p_ident->i_team == '*')
403 1.1 mrg return npp->p_ident->i_team;
404 1.1 mrg #endif
405 1.1 mrg if (pp->p_ident->i_team != npp->p_ident->i_team)
406 1.1 mrg return Maze[y][x];
407 1.1 mrg return pp->p_ident->i_team;
408 1.1 mrg }
409