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