xref: /dragonfly/games/trek/initquad.c (revision 984263bc)
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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)initquad.c	8.1 (Berkeley) 5/31/93";
37 #endif
38 static const char rcsid[] =
39  "$FreeBSD: src/games/trek/initquad.c,v 1.4 1999/11/30 03:49:49 billf Exp $";
40 #endif /* not lint */
41 
42 # include	"trek.h"
43 
44 /*
45 **  Paramize Quadrant Upon Entering
46 **
47 **	A quadrant is initialized from the information held in the
48 **	Quad matrix.  Basically, everything is just initialized
49 **	randomly, except for the starship, which goes into a fixed
50 **	sector.
51 **
52 **	If there are Klingons in the quadrant, the captain is informed
53 **	that the condition is RED, and he is given a chance to put
54 **	his shields up if the computer is working.
55 **
56 **	The flag `f' is set to disable the check for condition red.
57 **	This mode is used in situations where you know you are going
58 **	to be docked, i.e., abandon() and help().
59 */
60 
61 initquad(f)
62 int	f;
63 {
64 	int		i, j;
65 	int			rx, ry;
66 	int			nbases, nstars;
67 	struct quad	*q;
68 	int			nholes;
69 
70 	q = &Quad[Ship.quadx][Ship.quady];
71 
72 	/* ignored supernova'ed quadrants (this is checked again later anyway */
73 	if (q->stars < 0)
74 		return;
75 	Etc.nkling = q->klings;
76 	nbases = q->bases;
77 	nstars = q->stars;
78 	nholes = q->holes;
79 
80 	/* have we blundered into a battle zone w/ shields down? */
81 	if (Etc.nkling > 0 && !f)
82 	{
83 		printf("Condition RED\n");
84 		Ship.cond = RED;
85 		if (!damaged(COMPUTER))
86 			shield(1);
87 	}
88 
89 	/* clear out the quadrant */
90 	for (i = 0; i < NSECTS; i++)
91 		for (j = 0; j < NSECTS; j++)
92 			Sect[i][j] = EMPTY;
93 
94 	/* initialize Enterprise */
95 	Sect[Ship.sectx][Ship.secty] = Ship.ship;
96 
97 	/* initialize Klingons */
98 	for (i = 0; i < Etc.nkling; i++)
99 	{
100 		sector(&rx, &ry);
101 		Sect[rx][ry] = KLINGON;
102 		Etc.klingon[i].x = rx;
103 		Etc.klingon[i].y = ry;
104 		Etc.klingon[i].power = Param.klingpwr;
105 		Etc.klingon[i].srndreq = 0;
106 	}
107 	compkldist(1);
108 
109 	/* initialize star base */
110 	if (nbases > 0)
111 	{
112 		sector(&rx, &ry);
113 		Sect[rx][ry] = BASE;
114 		Etc.starbase.x = rx;
115 		Etc.starbase.y = ry;
116 	}
117 
118 	/* initialize inhabited starsystem */
119 	if (q->qsystemname != 0)
120 	{
121 		sector(&rx, &ry);
122 		Sect[rx][ry] = INHABIT;
123 		nstars -= 1;
124 	}
125 
126 	/* initialize black holes */
127 	for (i = 0; i < nholes; i++)
128 	{
129 		sector(&rx, &ry);
130 		Sect[rx][ry] = HOLE;
131 	}
132 
133 	/* initialize stars */
134 	for (i = 0; i < nstars; i++)
135 	{
136 		sector(&rx, &ry);
137 		Sect[rx][ry] = STAR;
138 	}
139 	Move.newquad = 1;
140 }
141 
142 
143 sector(x, y)
144 int	*x, *y;
145 {
146 	int		i, j;
147 
148 	do
149 	{
150 		i = ranf(NSECTS);
151 		j = ranf(NSECTS);
152 	} while (Sect[i][j] != EMPTY);
153 	*x = i;
154 	*y = j;
155 	return;
156 }
157