move_robs.c revision 1.7
1/*	$OpenBSD: move_robs.c,v 1.7 2004/11/29 08:52:28 jsg Exp $	*/
2/*	$NetBSD: move_robs.c,v 1.3 1995/04/22 10:08:59 cgd Exp $	*/
3
4/*
5 * Copyright (c) 1980, 1993
6 *	The Regents of the University of California.  All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#ifndef lint
34#if 0
35static char sccsid[] = "@(#)move_robs.c	8.1 (Berkeley) 5/31/93";
36#else
37static char rcsid[] = "$OpenBSD: move_robs.c,v 1.7 2004/11/29 08:52:28 jsg Exp $";
38#endif
39#endif /* not lint */
40
41#include	"robots.h"
42
43/*
44 * move_robots:
45 *	Move the robots around
46 */
47void
48move_robots(void)
49{
50	COORD	*rp;
51
52#ifdef DEBUG
53	move(Min.y, Min.x);
54	addch(inch());
55	move(Max.y, Max.x);
56	addch(inch());
57#endif /* DEBUG */
58	for (rp = Robots; rp < &Robots[MAXROBOTS]; rp++) {
59		if (rp->y < 0)
60			continue;
61		mvaddch(rp->y, rp->x, ' ');
62		Field[rp->y][rp->x]--;
63		rp->y += sign(My_pos.y - rp->y);
64		rp->x += sign(My_pos.x - rp->x);
65		if (rp->y <= 0)
66			rp->y = 0;
67		else if (rp->y >= Y_FIELDSIZE)
68			rp->y = Y_FIELDSIZE - 1;
69		if (rp->x <= 0)
70			rp->x = 0;
71		else if (rp->x >= X_FIELDSIZE)
72			rp->x = X_FIELDSIZE - 1;
73		Field[rp->y][rp->x]++;
74	}
75
76	Min.y = Y_FIELDSIZE;
77	Min.x = X_FIELDSIZE;
78	Max.y = 0;
79	Max.x = 0;
80	for (rp = Robots; rp < &Robots[MAXROBOTS]; rp++)
81		if (rp->y < 0)
82			continue;
83		else if (rp->y == My_pos.y && rp->x == My_pos.x)
84			Dead = TRUE;
85		else if (Field[rp->y][rp->x] > 1) {
86			mvaddch(rp->y, rp->x, HEAP);
87			rp->y = -1;
88			Num_robots--;
89			if (Waiting)
90				Wait_bonus++;
91			add_score(ROB_SCORE);
92		}
93		else {
94			mvaddch(rp->y, rp->x, ROBOT);
95			if (rp->y < Min.y)
96				Min.y = rp->y;
97			if (rp->x < Min.x)
98				Min.x = rp->x;
99			if (rp->y > Max.y)
100				Max.y = rp->y;
101			if (rp->x > Max.x)
102				Max.x = rp->x;
103		}
104
105#ifdef DEBUG
106	standout();
107	move(Min.y, Min.x);
108	addch(inch());
109	move(Max.y, Max.x);
110	addch(inch());
111	standend();
112#endif /* DEBUG */
113}
114
115/*
116 * add_score:
117 *	Add a score to the overall point total
118 */
119void
120add_score(int add)
121{
122	Score += add;
123	move(Y_SCORE, X_SCORE);
124	printw("%d", Score);
125}
126
127/*
128 * sign:
129 *	Return the sign of the number
130 */
131int
132sign(int n)
133{
134	if (n < 0)
135		return -1;
136	else if (n > 0)
137		return 1;
138	else
139		return 0;
140}
141