1/*	$NetBSD: init.c,v 1.10 2009/05/25 23:24:54 dholland Exp $	*/
2
3/*
4 * Copyright (c) 1982, 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. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33#ifndef lint
34#if 0
35static char sccsid[] = "@(#)init.c	8.1 (Berkeley) 5/31/93";
36#else
37__RCSID("$NetBSD: init.c,v 1.10 2009/05/25 23:24:54 dholland Exp $");
38#endif
39#endif /* not lint */
40
41#include "mille.h"
42
43/*
44 * @(#)init.c	1.1 (Berkeley) 4/1/82
45 */
46
47void
48init(void)
49{
50	PLAY	*pp;
51	int	i, j;
52	CARD	card;
53
54	memset(Numseen, 0, sizeof Numseen);
55	Numgos = 0;
56
57	for (i = 0; i < 2; i++) {
58		pp = &Player[i];
59		pp->hand[0] = C_INIT;
60		for (j = 0; j < NUM_SAFE; j++) {
61			pp->safety[j] = S_UNKNOWN;
62			pp->coups[j] = FALSE;
63		}
64		for (j = 1; j < HAND_SZ; j++) {
65			pp->hand[j] = *--Topcard;
66			if (i == COMP) {
67				account(card = *Topcard);
68				if (is_safety(card))
69					pp->safety[card - S_CONV] = S_IN_HAND;
70			}
71		}
72		pp->mileage = 0;
73		pp->hand_tot = 0;
74		pp->safescore = 0;
75		pp->coupscore = 0;
76		pp->can_go = FALSE;
77		pp->speed = C_INIT;
78		pp->battle = C_INIT;
79		pp->new_speed = FALSE;
80		pp->new_battle = FALSE;
81		for (j = 0; j < NUM_MILES; j++)
82			pp->nummiles[j] = 0;
83	}
84	if (Order)
85		sort(Player[PLAYER].hand);
86	Discard = C_INIT;
87	Finished = FALSE;
88	End = 700;
89}
90
91void
92shuffle(void)
93{
94	int	i, r;
95	CARD	temp;
96
97	for (i = 0; i < DECK_SZ; i++) {
98		r = roll(1, DECK_SZ) - 1;
99		if (r < 0 || r > DECK_SZ - 1) {
100			warnx("shuffle: card no. error: %d", r);
101			die(1);
102		}
103		temp = Deck[r];
104		Deck[r] = Deck[i];
105		Deck[i] = temp;
106	}
107	Topcard = &Deck[DECK_SZ];
108}
109
110void
111newboard(void)
112{
113	int		i;
114	PLAY		*pp;
115	static int	first = TRUE;
116
117	if (first) {
118		werase(Board);
119		werase(Score);
120		mvaddstr(5, 0, "--HAND--");
121		mvaddch(6, 0, 'P');
122		mvaddch(7, 0, '1');
123		mvaddch(8, 0, '2');
124		mvaddch(9, 0, '3');
125		mvaddch(10, 0, '4');
126		mvaddch(11, 0, '5');
127		mvaddch(12, 0, '6');
128		mvaddstr(13, 0, "--BATTLE--");
129		mvaddstr(15, 0, "--SPEED--");
130		mvaddstr(5, 20, "--DECK--");
131		mvaddstr(7, 20, "--DISCARD--");
132		mvaddstr(13, 20, "--BATTLE--");
133		mvaddstr(15, 20, "--SPEED--");
134		mvwaddstr(Miles, 0, 0, "--MILEAGE--");
135		mvwaddstr(Miles, 0, 41, "--MILEAGE--");
136		Sh_discard = -1;
137		for (pp = Player; pp <= &Player[COMP]; pp++) {
138			for (i = 0; i < HAND_SZ; i++)
139				pp->sh_hand[i] = -1;
140			pp->sh_battle = -1;
141			pp->sh_speed = -1;
142			pp->sh_mileage = -1;
143		}
144		first = FALSE;
145	}
146	else {
147		for (i = 0; i < 5; i++) {
148			move(i, 0);
149			clrtoeol();
150		}
151		wmove(Miles, 1, 0);
152		wclrtobot(Miles);
153		wmove(Board, MOVE_Y + 1, MOVE_X);
154		wclrtoeol(Board);
155		wmove(Board, MOVE_Y + 2, MOVE_X);
156		wclrtoeol(Board);
157	}
158	Sh_discard = -1;
159	for (pp = Player; pp <= &Player[COMP]; pp++) {
160		for (i = 0; i < NUM_SAFE; i++)
161			pp->sh_safety[i] = FALSE;
162		for (i = 0; i < NUM_MILES; i++)
163			pp->sh_nummiles[i] = 0;
164		pp->sh_safescore = -1;
165	}
166	newscore();
167}
168
169void
170newscore(void)
171{
172	int		i, new;
173	PLAY		*pp;
174	static int	was_full = -1;
175	static int	last_win = -1;
176
177	if (was_full < 0)
178		was_full = (Window != W_FULL);
179	stdscr = Score;
180	move(0, 22);
181	new = FALSE;
182	if (inch() != 'Y') {
183		erase();
184		mvaddstr(0, 22,  "You   Comp   Value");
185		mvaddstr(1, 2, "Milestones Played");
186		mvaddstr(2, 8, "Each Safety");
187		mvaddstr(3, 5, "All 4 Safeties");
188		mvaddstr(4, 3, "Each Coup Fourre");
189		mvaddstr(2, 37, "100");
190		mvaddstr(3, 37, "300");
191		mvaddstr(4, 37, "300");
192		new = TRUE;
193	}
194	else if ((Window == W_FULL || Finished) ^ was_full) {
195		move(5, 1);
196		clrtobot();
197		new = TRUE;
198	}
199	else if (Window != last_win)
200		new = TRUE;
201	if (new) {
202		for (i = 0; i < SCORE_Y; i++)
203			mvaddch(i, 0, '|');
204		move(SCORE_Y - 1, 1);
205		for (i = 0; i < SCORE_X; i++)
206			addch('_');
207		for (pp = Player; pp <= &Player[COMP]; pp++) {
208			pp->sh_hand_tot = -1;
209			pp->sh_total = -1;
210			pp->sh_games = -1;
211			pp->sh_safescore = -1;
212		}
213	}
214	Player[PLAYER].was_finished = !Finished;
215	Player[COMP].was_finished = !Finished;
216	if (Window == W_FULL || Finished) {
217		if (!was_full || new) {
218			mvaddstr(5, 5, "Trip Completed");
219			mvaddstr(6, 10, "Safe Trip");
220			mvaddstr(7, 5, "Delayed Action");
221			mvaddstr(8, 10, "Extension");
222			mvaddstr(9, 11, "Shut-Out");
223			mvaddstr(10, 21, "----   ----   -----");
224			mvaddstr(11, 9, "Hand Total");
225			mvaddstr(12, 20, "-----  -----");
226			mvaddstr(13, 6, "Overall Total");
227			mvaddstr(14, 15, "Games");
228			mvaddstr(5, 37, "400");
229			mvaddstr(6, 37, "300");
230			mvaddstr(7, 37, "300");
231			mvaddstr(8, 37, "200");
232			mvaddstr(9, 37, "500");
233		}
234	}
235	else
236		if (was_full || new) {
237			mvaddstr(5, 21, "----   ----   -----");
238			mvaddstr(6, 9, "Hand Total");
239			mvaddstr(7, 20, "-----  -----");
240			mvaddstr(8, 6, "Overall Total");
241			mvaddstr(9, 15, "Games");
242			mvaddstr(11, 2, "p: pick");
243			mvaddstr(12, 2, "u: use #");
244			mvaddstr(13, 2, "d: discard #");
245			mvaddstr(14, 2, "w: toggle window");
246			mvaddstr(11, 21, "q: quit");
247			if (!Order)
248				mvaddstr(12, 21, "o: order hand");
249			else
250				mvaddstr(12, 21, "o: stop ordering");
251			mvaddstr(13, 21, "s: save");
252			mvaddstr(14, 21, "r: reprint");
253		}
254	stdscr = Board;
255	was_full = (Window == W_FULL || Finished);
256	last_win = Window;
257}
258