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