xref: /netbsd-src/games/trek/initquad.c (revision 50b862e21c853cbd36b7a06588389f4c21b9a079)
1*50b862e2Sdholland /*	$NetBSD: initquad.c,v 1.7 2009/05/24 21:44:56 dholland Exp $	*/
2887dd216Scgd 
361f28255Scgd /*
4887dd216Scgd  * Copyright (c) 1980, 1993
5887dd216Scgd  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15e5aeb4eaSagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
32ef383c95Schristos #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
34887dd216Scgd #if 0
35887dd216Scgd static char sccsid[] = "@(#)initquad.c	8.1 (Berkeley) 5/31/93";
36887dd216Scgd #else
37*50b862e2Sdholland __RCSID("$NetBSD: initquad.c,v 1.7 2009/05/24 21:44:56 dholland Exp $");
38887dd216Scgd #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd 
41ef383c95Schristos #include <stdio.h>
4261f28255Scgd #include "trek.h"
4361f28255Scgd 
4461f28255Scgd /*
4561f28255Scgd **  Paramize Quadrant Upon Entering
4661f28255Scgd **
4761f28255Scgd **	A quadrant is initialized from the information held in the
4861f28255Scgd **	Quad matrix.  Basically, everything is just initialized
4961f28255Scgd **	randomly, except for the starship, which goes into a fixed
5061f28255Scgd **	sector.
5161f28255Scgd **
5261f28255Scgd **	If there are Klingons in the quadrant, the captain is informed
5361f28255Scgd **	that the condition is RED, and he is given a chance to put
5461f28255Scgd **	his shields up if the computer is working.
5561f28255Scgd **
5661f28255Scgd **	The flag `f' is set to disable the check for condition red.
5761f28255Scgd **	This mode is used in situations where you know you are going
5861f28255Scgd **	to be docked, i.e., abandon() and help().
5961f28255Scgd */
6061f28255Scgd 
61ef383c95Schristos void
initquad(int f)62bf0917b6Sdholland initquad(int f)
6361f28255Scgd {
64ef383c95Schristos 	int		i, j;
6561f28255Scgd 	int		rx, ry;
6661f28255Scgd 	int		nbases, nstars;
67ef383c95Schristos 	struct quad	*q;
6861f28255Scgd 	int		nholes;
6961f28255Scgd 
7061f28255Scgd 	q = &Quad[Ship.quadx][Ship.quady];
7161f28255Scgd 
7261f28255Scgd 	/* ignored supernova'ed quadrants (this is checked again later anyway */
7361f28255Scgd 	if (q->stars < 0)
7461f28255Scgd 		return;
7561f28255Scgd 	Etc.nkling = q->klings;
7661f28255Scgd 	nbases = q->bases;
7761f28255Scgd 	nstars = q->stars;
7861f28255Scgd 	nholes = q->holes;
7961f28255Scgd 
8061f28255Scgd 	/* have we blundered into a battle zone w/ shields down? */
81*50b862e2Sdholland 	if (Etc.nkling > 0 && !f) {
8261f28255Scgd 		printf("Condition RED\n");
8361f28255Scgd 		Ship.cond = RED;
8461f28255Scgd 		if (!damaged(COMPUTER))
8561f28255Scgd 			shield(1);
8661f28255Scgd 	}
8761f28255Scgd 
8861f28255Scgd 	/* clear out the quadrant */
8961f28255Scgd 	for (i = 0; i < NSECTS; i++)
9061f28255Scgd 		for (j = 0; j < NSECTS; j++)
9161f28255Scgd 			Sect[i][j] = EMPTY;
9261f28255Scgd 
9361f28255Scgd 	/* initialize Enterprise */
9461f28255Scgd 	Sect[Ship.sectx][Ship.secty] = Ship.ship;
9561f28255Scgd 
9661f28255Scgd 	/* initialize Klingons */
97*50b862e2Sdholland 	for (i = 0; i < Etc.nkling; i++) {
9861f28255Scgd 		sector(&rx, &ry);
9961f28255Scgd 		Sect[rx][ry] = KLINGON;
10061f28255Scgd 		Etc.klingon[i].x = rx;
10161f28255Scgd 		Etc.klingon[i].y = ry;
10261f28255Scgd 		Etc.klingon[i].power = Param.klingpwr;
10361f28255Scgd 		Etc.klingon[i].srndreq = 0;
10461f28255Scgd 	}
10561f28255Scgd 	compkldist(1);
10661f28255Scgd 
10761f28255Scgd 	/* initialize star base */
108*50b862e2Sdholland 	if (nbases > 0) {
10961f28255Scgd 		sector(&rx, &ry);
11061f28255Scgd 		Sect[rx][ry] = BASE;
11161f28255Scgd 		Etc.starbase.x = rx;
11261f28255Scgd 		Etc.starbase.y = ry;
11361f28255Scgd 	}
11461f28255Scgd 
11561f28255Scgd 	/* initialize inhabited starsystem */
116*50b862e2Sdholland 	if (q->qsystemname != 0) {
11761f28255Scgd 		sector(&rx, &ry);
11861f28255Scgd 		Sect[rx][ry] = INHABIT;
11961f28255Scgd 		nstars -= 1;
12061f28255Scgd 	}
12161f28255Scgd 
12261f28255Scgd 	/* initialize black holes */
123*50b862e2Sdholland 	for (i = 0; i < nholes; i++) {
12461f28255Scgd 		sector(&rx, &ry);
12561f28255Scgd 		Sect[rx][ry] = HOLE;
12661f28255Scgd 	}
12761f28255Scgd 
12861f28255Scgd 	/* initialize stars */
129*50b862e2Sdholland 	for (i = 0; i < nstars; i++) {
13061f28255Scgd 		sector(&rx, &ry);
13161f28255Scgd 		Sect[rx][ry] = STAR;
13261f28255Scgd 	}
13361f28255Scgd 	Move.newquad = 1;
13461f28255Scgd }
13561f28255Scgd 
13661f28255Scgd 
137ef383c95Schristos void
sector(int * x,int * y)138bf0917b6Sdholland sector(int *x, int *y)
13961f28255Scgd {
140ef383c95Schristos 	int	i, j;
14161f28255Scgd 
142*50b862e2Sdholland 	do {
14361f28255Scgd 		i = ranf(NSECTS);
14461f28255Scgd 		j = ranf(NSECTS);
14561f28255Scgd 	} while (Sect[i][j] != EMPTY);
14661f28255Scgd 	*x = i;
14761f28255Scgd 	*y = j;
14861f28255Scgd 	return;
14961f28255Scgd }
150