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