xref: /netbsd-src/games/mille/init.c (revision ae9172d6cd9432a6a1a56760d86b32c57a66c39c)
1 /*
2  * Copyright (c) 1982, 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 /*static char sccsid[] = "from: @(#)init.c	8.1 (Berkeley) 5/31/93";*/
36 static char rcsid[] = "$Id: init.c,v 1.4 1994/05/12 17:39:31 jtc Exp $";
37 #endif /* not lint */
38 
39 # include	"mille.h"
40 
41 /*
42  * @(#)init.c	1.1 (Berkeley) 4/1/82
43  */
44 
45 init() {
46 
47 	reg PLAY	*pp;
48 	reg int		i, j;
49 	reg CARD	card;
50 
51 	bzero(Numseen, sizeof Numseen);
52 	Numgos = 0;
53 
54 	for (i = 0; i < 2; i++) {
55 		pp = &Player[i];
56 		pp->hand[0] = C_INIT;
57 		for (j = 0; j < NUM_SAFE; j++) {
58 			pp->safety[j] = S_UNKNOWN;
59 			pp->coups[j] = FALSE;
60 		}
61 		for (j = 1; j < HAND_SZ; j++) {
62 			pp->hand[j] = *--Topcard;
63 			if (i == COMP) {
64 				account(card = *Topcard);
65 				if (issafety(card))
66 					pp->safety[card - S_CONV] = S_IN_HAND;
67 			}
68 		}
69 		pp->mileage = 0;
70 		pp->hand_tot = 0;
71 		pp->safescore = 0;
72 		pp->coupscore = 0;
73 		pp->can_go = FALSE;
74 		pp->speed = C_INIT;
75 		pp->battle = C_INIT;
76 		pp->new_speed = FALSE;
77 		pp->new_battle = FALSE;
78 		for (j = 0; j < NUM_MILES; j++)
79 			pp->nummiles[j] = 0;
80 	}
81 	if (Order)
82 		sort(Player[PLAYER].hand);
83 	Discard = C_INIT;
84 	Finished = FALSE;
85 	End = 700;
86 }
87 
88 shuffle() {
89 
90 	reg int		i, r;
91 	reg CARD	temp;
92 
93 	for (i = 0; i < DECK_SZ; i++) {
94 		r = roll(1, DECK_SZ) - 1;
95 		if (r < 0 || r > DECK_SZ - 1) {
96 			fprintf(stderr, "shuffle: card no. error: %d\n", r);
97 			die(1);
98 		}
99 		temp = Deck[r];
100 		Deck[r] = Deck[i];
101 		Deck[i] = temp;
102 	}
103 	Topcard = &Deck[DECK_SZ];
104 }
105 
106 newboard() {
107 
108 	register int	i;
109 	register PLAY	*pp;
110 	static int	first = TRUE;
111 
112 	if (first) {
113 		werase(Board);
114 		werase(Score);
115 		mvaddstr(5, 0, "--HAND--");
116 		mvaddch(6, 0, 'P');
117 		mvaddch(7, 0, '1');
118 		mvaddch(8, 0, '2');
119 		mvaddch(9, 0, '3');
120 		mvaddch(10, 0, '4');
121 		mvaddch(11, 0, '5');
122 		mvaddch(12, 0, '6');
123 		mvaddstr(13, 0, "--BATTLE--");
124 		mvaddstr(15, 0, "--SPEED--");
125 		mvaddstr(5, 20, "--DECK--");
126 		mvaddstr(7, 20, "--DISCARD--");
127 		mvaddstr(13, 20, "--BATTLE--");
128 		mvaddstr(15, 20, "--SPEED--");
129 		mvwaddstr(Miles, 0, 0, "--MILEAGE--");
130 		mvwaddstr(Miles, 0, 41, "--MILEAGE--");
131 		Sh_discard = -1;
132 		for (pp = Player; pp <= &Player[COMP]; pp++) {
133 			for (i = 0; i < HAND_SZ; i++)
134 				pp->sh_hand[i] = -1;
135 			pp->sh_battle = -1;
136 			pp->sh_speed = -1;
137 			pp->sh_mileage = -1;
138 		}
139 		first = FALSE;
140 	}
141 	else {
142 		for (i = 0; i < 5; i++) {
143 			move(i, 0);
144 			clrtoeol();
145 		}
146 		wmove(Miles, 1, 0);
147 		wclrtobot(Miles);
148 		wmove(Board, MOVE_Y + 1, MOVE_X);
149 		wclrtoeol(Board);
150 		wmove(Board, MOVE_Y + 2, MOVE_X);
151 		wclrtoeol(Board);
152 	}
153 	Sh_discard = -1;
154 	for (pp = Player; pp <= &Player[COMP]; pp++) {
155 		for (i = 0; i < NUM_SAFE; i++)
156 			pp->sh_safety[i] = FALSE;
157 		for (i = 0; i < NUM_MILES; i++)
158 			pp->sh_nummiles[i] = 0;
159 		pp->sh_safescore = -1;
160 	}
161 	newscore();
162 }
163 
164 newscore() {
165 
166 	reg int		i, new;
167 	register PLAY	*pp;
168 	static int	was_full = -1;
169 	static int	last_win = -1;
170 
171 	if (was_full < 0)
172 		was_full = (Window != W_FULL);
173 	stdscr = Score;
174 	move(0, 22);
175 	new = FALSE;
176 	if (inch() != 'Y') {
177 		erase();
178 		mvaddstr(0, 22,  "You   Comp   Value");
179 		mvaddstr(1, 2, "Milestones Played");
180 		mvaddstr(2, 8, "Each Safety");
181 		mvaddstr(3, 5, "All 4 Safeties");
182 		mvaddstr(4, 3, "Each Coup Fourre");
183 		mvaddstr(2, 37, "100");
184 		mvaddstr(3, 37, "300");
185 		mvaddstr(4, 37, "300");
186 		new = TRUE;
187 	}
188 	else if ((Window == W_FULL || Finished) ^ was_full) {
189 		move(5, 1);
190 		clrtobot();
191 		new = TRUE;
192 	}
193 	else if (Window != last_win)
194 		new = TRUE;
195 	if (new) {
196 		for (i = 0; i < SCORE_Y; i++)
197 			mvaddch(i, 0, '|');
198 		move(SCORE_Y - 1, 1);
199 		for (i = 0; i < SCORE_X; i++)
200 			addch('_');
201 		for (pp = Player; pp <= &Player[COMP]; pp++) {
202 			pp->sh_hand_tot = -1;
203 			pp->sh_total = -1;
204 			pp->sh_games = -1;
205 			pp->sh_safescore = -1;
206 		}
207 	}
208 	Player[PLAYER].was_finished = !Finished;
209 	Player[COMP].was_finished = !Finished;
210 	if (Window == W_FULL || Finished) {
211 		if (!was_full || new) {
212 			mvaddstr(5, 5, "Trip Completed");
213 			mvaddstr(6, 10, "Safe Trip");
214 			mvaddstr(7, 5, "Delayed Action");
215 			mvaddstr(8, 10, "Extension");
216 			mvaddstr(9, 11, "Shut-Out");
217 			mvaddstr(10, 21, "----   ----   -----");
218 			mvaddstr(11, 9, "Hand Total");
219 			mvaddstr(12, 20, "-----  -----");
220 			mvaddstr(13, 6, "Overall Total");
221 			mvaddstr(14, 15, "Games");
222 			mvaddstr(5, 37, "400");
223 			mvaddstr(6, 37, "300");
224 			mvaddstr(7, 37, "300");
225 			mvaddstr(8, 37, "200");
226 			mvaddstr(9, 37, "500");
227 		}
228 	}
229 	else
230 		if (was_full || new) {
231 			mvaddstr(5, 21, "----   ----   -----");
232 			mvaddstr(6, 9, "Hand Total");
233 			mvaddstr(7, 20, "-----  -----");
234 			mvaddstr(8, 6, "Overall Total");
235 			mvaddstr(9, 15, "Games");
236 			mvaddstr(11, 2, "p: pick");
237 			mvaddstr(12, 2, "u: use #");
238 			mvaddstr(13, 2, "d: discard #");
239 			mvaddstr(14, 2, "w: toggle window");
240 			mvaddstr(11, 21, "q: quit");
241 			if (!Order)
242 				mvaddstr(12, 21, "o: order hand");
243 			else
244 				mvaddstr(12, 21, "o: stop ordering");
245 			mvaddstr(13, 21, "s: save");
246 			mvaddstr(14, 21, "r: reprint");
247 		}
248 	stdscr = Board;
249 	was_full = (Window == W_FULL || Finished);
250 	last_win = Window;
251 }
252