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