1/*	$NetBSD: back.h,v 1.23 2021/12/12 22:20:52 andvar Exp $	*/
2
3/*
4 * Copyright (c) 1980, 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 *	@(#)back.h	8.1 (Berkeley) 5/31/93
32 */
33
34#include <sys/types.h>
35#include <sys/uio.h>
36
37#include <fcntl.h>
38#include <signal.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <termios.h>
43#include <termcap.h>
44#include <unistd.h>
45
46#define rnum(r)	(random()%r)
47#define D0	dice[0]
48#define D1	dice[1]
49#define mswap(m) { \
50	(m)->D0 ^= (m)->D1;  \
51	(m)->D1 ^= (m)->D0;  \
52	(m)->D0 ^= (m)->D1;  \
53	(m)->d0 = 1-(m)->d0; \
54}
55
56struct move {
57	int	dice[2];	/* value of dice */
58	int	mvlim;		/* 'move limit':  max. number of moves */
59	int	p[5];		/* starting position of moves */
60	int	g[5];		/* ending position of moves (goals) */
61	int	h[4];		/* flag for each move if a man was hit */
62	int	d0;		/* flag if dice have been reversed from
63				   original position */
64};
65
66/*
67 *
68 * Some numerical conventions:
69 *
70 *	Arrays have white's value in [0], red in [1].
71 *	Numeric values which are one color or the other use
72 *	-1 for white, 1 for red.
73 *	Hence, white will be negative values, red positive one.
74 *	This makes a lot of sense since white is going in descending
75 *	order around the board, and red is ascending.
76 *
77 */
78
79extern	const char	EXEC[];		/* object for main program */
80extern	const char	TEACH[];	/* object for tutorial program */
81
82extern	int	pnum;		/* color of player:
83					-1 = white
84					 1 = red
85					 0 = both
86					 2 = not yet init'ed */
87extern	char	args[100];	/* args passed to teachgammon and back */
88extern	int	acnt;		/* length of args */
89extern	int	aflag;		/* flag to ask for rules or instructions */
90extern	int	bflag;		/* flag for automatic board printing */
91extern	int	cflag;		/* case conversion flag */
92extern	int	hflag;		/* flag for cleaning screen */
93extern	int	mflag;		/* backgammon flag */
94extern	int	raflag;		/* 'roll again' flag for recovered game */
95extern	int	rflag;		/* recovered game flag */
96extern	int	tflag;		/* cursor addressing flag */
97extern	int	rfl;		/* saved value of rflag */
98extern	int	iroll;		/* special flag for inputting rolls */
99extern	int	board[26];	/* board:  negative values are white,
100				   positive are red */
101extern	int	cturn;		/* whose turn it currently is:
102					-1 = white
103					 1 = red
104					 0 = just quitted
105					-2 = white just lost
106					 2 = red just lost */
107extern	int	table[6][6];	/* odds table for possible rolls */
108extern	int	rscore;		/* red's score */
109extern	int	wscore;		/* white's score */
110extern	int	gvalue;		/* value of game (64 max.) */
111extern	int	dlast;		/* who doubled last (0 = neither) */
112extern	int	bar;		/* position of bar for current player */
113extern	int	home;		/* position of home for current player */
114extern	int	off[2];		/* number of men off board */
115extern	int	*offptr;	/* pointer to off for current player */
116extern	int	*offopp;	/* pointer to off for opponent */
117extern	int	in[2];		/* number of men in inner table */
118extern	int	*inptr;		/* pointer to in for current player */
119extern	int	*inopp;		/* pointer to in for opponent */
120
121extern	int	ncin;		/* number of characters in cin */
122extern	char	cin[100];	/* input line of current move
123				   (used for reconstructing input after
124				   a backspace) */
125
126extern	const char	*const color[];
127				/* colors as strings */
128extern	const char	*const *colorptr;	/* color of current player */
129extern	const char	*const *Colorptr;	/* ditto, capitalized */
130extern	int	colen;		/* length of color of current player */
131
132extern int buffnum;
133
134extern	struct termios	old, noech, raw;/* original tty status */
135
136extern	int	curr;		/* row position of cursor */
137extern	int	curc;		/* column position of cursor */
138extern	int	begscr;		/* 'beginning' of screen
139				   (not including board) */
140
141int	addbuf(int);
142void	backone(struct move *, int);
143void	buflush(void);
144int	canhit(int, int);
145int	checkmove(struct move *, int);
146void	clear(void);
147void	clend(void);
148void	cline(void);
149int	count(void);
150void	curmove(int, int);
151void	errexit(const char *) __dead;
152void	fancyc(int);
153void	fboard(void);
154void	fixtty(struct termios *);
155void	getarg(struct move *, char ***);
156int	getcaps(const char *);
157void	getmove(struct move *);
158void	getout(int) __dead;
159void	gwrite(void);
160void	init(void);
161int	main(int, char *[]);
162int	makmove(struct move *, int);
163int	movallow(struct move *);
164void	movback(struct move *, int);
165void	moverr(struct move *, int);
166int	movokay(struct move *, int);
167void	newpos(void);
168void	nexturn(void);
169void	odds(int, int, int);
170void	proll(struct move *);
171int	quit(struct move *);
172int	readc(void);
173void	recover(struct move *, const char *);
174void	refresh(void);
175void	roll(struct move *);
176void	save(struct move *, int);
177int	wrtext(const char *const *);
178void	wrboard(void);
179void	wrhit(int);
180void	wrint(int);
181void	writec(int);
182void	writel(const char *);
183void	wrscore(void);
184int	yorn(int);
185
186void move_init(struct move *);
187