move_robs.c revision 1.9
1/*	$OpenBSD: move_robs.c,v 1.9 2016/01/04 17:33:24 mestre 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#include "robots.h"
34
35/*
36 * move_robots:
37 *	Move the robots around
38 */
39void
40move_robots(void)
41{
42	COORD	*rp;
43
44#ifdef DEBUG
45	move(Min.y, Min.x);
46	addch(inch());
47	move(Max.y, Max.x);
48	addch(inch());
49#endif /* DEBUG */
50	for (rp = Robots; rp < &Robots[MAXROBOTS]; rp++) {
51		if (rp->y < 0)
52			continue;
53		mvaddch(rp->y, rp->x, ' ');
54		Field[rp->y][rp->x]--;
55		rp->y += sign(My_pos.y - rp->y);
56		rp->x += sign(My_pos.x - rp->x);
57		if (rp->y <= 0)
58			rp->y = 0;
59		else if (rp->y >= Y_FIELDSIZE)
60			rp->y = Y_FIELDSIZE - 1;
61		if (rp->x <= 0)
62			rp->x = 0;
63		else if (rp->x >= X_FIELDSIZE)
64			rp->x = X_FIELDSIZE - 1;
65		Field[rp->y][rp->x]++;
66	}
67
68	Min.y = Y_FIELDSIZE;
69	Min.x = X_FIELDSIZE;
70	Max.y = 0;
71	Max.x = 0;
72	for (rp = Robots; rp < &Robots[MAXROBOTS]; rp++)
73		if (rp->y < 0)
74			continue;
75		else if (rp->y == My_pos.y && rp->x == My_pos.x)
76			Dead = TRUE;
77		else if (Field[rp->y][rp->x] > 1) {
78			mvaddch(rp->y, rp->x, HEAP);
79			rp->y = -1;
80			Num_robots--;
81			if (Waiting)
82				Wait_bonus++;
83			add_score(ROB_SCORE);
84		}
85		else {
86			mvaddch(rp->y, rp->x, ROBOT);
87			if (rp->y < Min.y)
88				Min.y = rp->y;
89			if (rp->x < Min.x)
90				Min.x = rp->x;
91			if (rp->y > Max.y)
92				Max.y = rp->y;
93			if (rp->x > Max.x)
94				Max.x = rp->x;
95		}
96
97#ifdef DEBUG
98	standout();
99	move(Min.y, Min.x);
100	addch(inch());
101	move(Max.y, Max.x);
102	addch(inch());
103	standend();
104#endif /* DEBUG */
105}
106
107/*
108 * add_score:
109 *	Add a score to the overall point total
110 */
111void
112add_score(int add)
113{
114	Score += add;
115	move(Y_SCORE, X_SCORE);
116	printw("%d", Score);
117}
118
119/*
120 * sign:
121 *	Return the sign of the number
122 */
123int
124sign(int n)
125{
126	if (n < 0)
127		return -1;
128	else if (n > 0)
129		return 1;
130	else
131		return 0;
132}
133