1 /*        $NetBSD: mille.h,v 1.21 2021/05/02 12:50:45 rillig 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  *        @(#)mille.h         8.1 (Berkeley) 5/31/93
32  */
33 
34 #include <sys/types.h>
35 #include <sys/uio.h>
36 #include <sys/stat.h>
37 #include <ctype.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <curses.h>
41 #include <fcntl.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <termios.h>
45 #include <unistd.h>
46 
47 /*
48  * @(#)mille.h      1.1 (Berkeley) 4/1/82
49  */
50 
51 /*
52  * Miscellaneous constants
53  */
54 
55 #define CARD                  short
56 
57 #define HAND_SZ               7         /* number of cards in a hand  */
58 #define DECK_SZ               101       /* number of cards in decks   */
59 #define NUM_SAFE    4         /* number of safety cards     */
60 #define NUM_MILES   5         /* number of milestones types */
61 #define NUM_CARDS   20        /* number of types of cards   */
62 #define BOARD_Y               17        /* size of board screen                 */
63 #define BOARD_X               40
64 #define MILES_Y               7         /* size of mileage screen     */
65 #define MILES_X               80
66 #define SCORE_Y               17        /* size of score screen                 */
67 #define SCORE_X               40
68 #define MOVE_Y                10        /* Where to print move prompt */
69 #define MOVE_X                20
70 #define ERR_Y                 15        /* Where to print errors      */
71 #define ERR_X                 5
72 #define EXT_Y                 4         /* Where to put Extension     */
73 #define EXT_X                 9
74 
75 #define PLAYER                0
76 #define COMP                  1
77 
78 #define W_SMALL               0         /* Small (initial) window     */
79 #define W_FULL                1         /* Full (final) window                  */
80 
81 /*
82  * Move types
83  */
84 
85 #define M_DISCARD   0
86 #define M_DRAW                1
87 #define M_PLAY                2
88 #define M_ORDER               3
89 
90 /*
91  * Scores
92  */
93 
94 #define SC_SAFETY   100
95 #define SC_ALL_SAFE 300
96 #define SC_COUP               300
97 #define SC_TRIP               400
98 #define SC_SAFE               300
99 #define SC_DELAY    300
100 #define SC_EXTENSION          200
101 #define SC_SHUT_OUT 500
102 
103 /*
104  * safety descriptions
105  */
106 
107 #undef S_UNKNOWN
108 #undef S_IN_HAND
109 #undef S_PLAYED
110 #undef S_GAS_SAFE
111 #undef S_SPARE_SAFE
112 #undef S_DRIVE_SAFE
113 #undef S_RIGHT_WAY
114 #undef S_CONV
115 #define S_UNKNOWN   0         /* location of safety unknown */
116 #define S_IN_HAND   1         /* safety in player's hand    */
117 #define S_PLAYED    2         /* safety has been played     */
118 #define S_GAS_SAFE  0         /* Gas safety card index      */
119 #define S_SPARE_SAFE          1         /* Tire safety card index     */
120 #define S_DRIVE_SAFE          2         /* Driveing safety card index */
121 #define S_RIGHT_WAY 3         /* Right-of-Way card index    */
122 #define S_CONV                15        /* conversion from C_ to S_   */
123 
124 /*
125  * card numbers
126  */
127 
128 #define C_INIT                -1
129 #define C_25                  0
130 #define C_50                  1
131 #define C_75                  2
132 #define C_100                 3
133 #define C_200                 4
134 #define C_EMPTY               5
135 #define C_FLAT                6
136 #define C_CRASH               7
137 #define C_STOP                8
138 #define C_LIMIT               9
139 #define C_GAS                 10
140 #define C_SPARE               11
141 #define C_REPAIRS   12
142 #define C_GO                  13
143 #define C_END_LIMIT 14
144 #define C_GAS_SAFE  15
145 #define C_SPARE_SAFE          16
146 #define C_DRIVE_SAFE          17
147 #define C_RIGHT_WAY 18
148 
149 /*
150  * prompt types
151  */
152 
153 #define MOVEPROMPT            0
154 #define REALLYPROMPT                    1
155 #define ANOTHERHANDPROMPT     2
156 #define ANOTHERGAMEPROMPT     3
157 #define SAVEGAMEPROMPT                  4
158 #define SAMEFILEPROMPT                  5
159 #define FILEPROMPT            6
160 #define EXTENSIONPROMPT                 7
161 #define OVERWRITEFILEPROMPT   8
162 
163 #ifdef    SYSV
164 #define srandom(x)  srand(x)
165 #define random()    rand()
166 #endif    /* SYSV */
167 
168 typedef struct {
169           bool      coups[NUM_SAFE];
170           bool      can_go;
171           bool      new_battle;
172           bool      new_speed;
173           short     safety[NUM_SAFE];
174           short     sh_safety[NUM_SAFE];
175           short     nummiles[NUM_MILES];
176           short     sh_nummiles[NUM_MILES];
177           CARD      hand[HAND_SZ];
178           CARD      sh_hand[HAND_SZ];
179           CARD      battle;
180           CARD      sh_battle;
181           CARD      speed;
182           CARD      sh_speed;
183           int       mileage;
184           int       sh_mileage;
185           int       hand_tot;
186           int       sh_hand_tot;
187           int       safescore;
188           int       sh_safescore;
189           int       coupscore;
190           int       total;
191           int       sh_total;
192           int       games;
193           int       sh_games;
194           int       was_finished;
195 } PLAY;
196 
197 /*
198  * macros
199  */
200 
201 #define other(x)    (1 - x)
202 #define nextplay()  (Play = other(Play))
203 #define nextwin(x)  (1 - x)
204 #define opposite(x) (Opposite[x])
205 #define is_safety(x)          (x >= C_GAS_SAFE)
206 
207 /*
208  * externals
209  */
210 
211 extern bool         Debug, Finished, Next, On_exit, Order, Saved;
212 
213 #define INITSTR_SIZE 100
214 extern char         Initstr[INITSTR_SIZE];
215 extern const char   *const *C_name, *Fromfile;
216 #define C_fmt "%-18.18s"
217 
218 extern int          Card_no, End, Handstart, Movetype, Numgos,
219                     Numneed[], Numseen[NUM_CARDS], Play, Window;
220 extern const int    Numcards[], Value[];
221 
222 extern CARD         Deck[DECK_SZ], Discard, Sh_discard, *Topcard;
223 extern const CARD   Opposite[NUM_CARDS];
224 
225 extern FILE         *outf;
226 
227 extern PLAY         Player[2];
228 
229 extern WINDOW       *Board, *Miles, *Score;
230 
231 /*
232  * functions
233  */
234 
235 void      account(CARD);
236 void      calcmove(void);
237 int       canplay(const PLAY *, const PLAY *, CARD);
238 int       check_ext(bool);
239 void      check_more(void);
240 void      die(int) __dead;
241 void      domove(void);
242 bool      error(const char *, ...) __printflike(1,2);
243 void      extrapolate(PLAY *);
244 void      finalscore(PLAY *);
245 CARD      getcard(void);
246 int       getyn(int);
247 void      init(void);
248 int       is_repair(CARD);
249 int       main(int, char **);
250 void      newboard(void);
251 void      newscore(void);
252 int       onecard(const PLAY *);
253 void      prboard(void);
254 void      prompt(int);
255 void      prscore(bool);
256 int       readch(void);
257 bool      rest_f(const char *);
258 int       roll(int, int);
259 void      rub(int);
260 int       safety(CARD);
261 bool      save(void);
262 void      shuffle(void);
263 void      sort(CARD *);
264 void      undoex(int);
265 bool      varpush(int, ssize_t (int, const struct iovec *, int));
266