]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - backgammon/common_source/fancy.c
e4734ca1e33846edff73bebd517bd4b7eb651c7f
1 /* $NetBSD: fancy.c,v 1.5 1997/10/10 08:59:45 lukem Exp $ */
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
39 static char sccsid
[] = "@(#)fancy.c 8.1 (Berkeley) 5/31/93";
41 __RCSID("$NetBSD: fancy.c,v 1.5 1997/10/10 08:59:45 lukem Exp $");
47 int tgetent
__P((char *, char *));
48 int tgetnum
__P((char *));
49 char *tgoto
__P((char *, int, int));
50 char *tgetstr
__P((char *, char **));
51 void tputs
__P((char *, int, void (*outc
) __P((int))));
53 char PC
; /* padding character */
54 char *BC
; /* backspace sequence */
55 char *CD
; /* clear to end of screen sequence */
56 char *CE
; /* clear to end of line sequence */
57 char *CL
; /* clear screen sequence */
58 char *CM
; /* cursor movement instructions */
59 char *HO
; /* home cursor sequence */
60 char *MC
; /* column cursor movement map */
61 char *ML
; /* row cursor movement map */
62 char *ND
; /* forward cursor sequence */
63 char *UP
; /* up cursor sequence */
65 int lHO
; /* length of HO */
66 int lBC
; /* length of BC */
67 int lND
; /* length of ND */
68 int lUP
; /* length of UP */
69 int CO
; /* number of columns */
70 int LI
; /* number of lines */
71 int *linect
; /* array of lengths of lines on screen (the
72 * actual screen is not stored) */
74 /* two letter codes */
75 char tcap
[] = "bccdceclcmhomcmlndup";
76 /* corresponding strings */
77 char **tstr
[] = {&BC
, &CD
, &CE
, &CL
, &CM
, &HO
, &MC
, &ML
, &ND
, &UP
};
79 int buffnum
; /* pointer to output buffer */
81 char tbuf
[1024]; /* buffer for decoded termcap entries */
83 int oldb
[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
87 /* "real" cursor positions, so it knows when to reposition. These are -1 if
88 * curr and curc are accurate */
97 curmove(0, 0); /* do top line */
98 for (i
= 0; i
< 53; i
++)
101 curmove(15, 0); /* do botttom line */
102 for (i
= 0; i
< 53; i
++)
105 l
= 1; /* do vertical lines */
106 for (i
= 52; i
> -1; i
-= 28) {
107 curmove((l
== 1 ? 1 : 15), i
);
109 for (j
= 0; j
< 14; j
++) {
110 curmove(curr
+ l
, curc
- 1);
115 l
= -l
; /* alternate directions */
118 curmove(2, 1); /* label positions 13-18 */
119 for (i
= 13; i
< 18; i
++) {
121 fancyc((i
% 10) + '0');
122 curmove(curr
, curc
+ 2);
127 curmove(2, 29); /* label positions 19-24 */
130 for (i
= 20; i
< 25; i
++) {
131 curmove(curr
, curc
+ 2);
133 fancyc((i
% 10) + '0');
136 curmove(14, 1); /* label positions 12-7 */
139 for (i
= 11; i
> 6; i
--) {
140 curmove(curr
, curc
+ 2);
141 fancyc(i
> 9 ? '1' : ' ');
142 fancyc((i
% 10) + '0');
145 curmove(14, 30); /* label positions 6-1 */
147 for (i
= 5; i
> 0; i
--) {
148 curmove(curr
, curc
+ 3);
152 for (i
= 12; i
> 6; i
--)/* print positions 12-7 */
154 bsect(board
[i
], 13, 1 + 4 * (12 - i
), -1);
156 if (board
[0]) /* print red men on bar */
157 bsect(board
[0], 13, 25, -1);
159 for (i
= 6; i
> 0; i
--) /* print positions 6-1 */
161 bsect(board
[i
], 13, 29 + 4 * (6 - i
), -1);
163 l
= (off
[1] < 0 ? off
[1] + 15 : off
[1]); /* print white's home */
166 curmove(8, 25); /* print the word BAR */
171 for (i
= 13; i
< 19; i
++) /* print positions 13-18 */
173 bsect(board
[i
], 3, 1 + 4 * (i
- 13), 1);
175 if (board
[25]) /* print white's men on bar */
176 bsect(board
[25], 3, 25, 1);
178 for (i
= 19; i
< 25; i
++) /* print positions 19-24 */
180 bsect(board
[i
], 3, 29 + 4 * (i
- 19), 1);
182 l
= (off
[0] < 0 ? off
[0] + 15 : off
[0]); /* print red's home */
183 bsect(-l
, 13, 54, -1);
185 for (i
= 0; i
< 26; i
++)/* save board position for refresh later */
187 oldr
= (off
[1] < 0 ? off
[1] + 15 : off
[1]);
188 oldw
= -(off
[0] < 0 ? off
[0] + 15 : off
[0]);
191 * bsect (b,rpos,cpos,cnext)
192 * Print the contents of a board position. "b" has the value of the
193 * position, "rpos" is the row to start printing, "cpos" is the column to
194 * start printing, and "cnext" is positive if the position starts at the top
195 * and negative if it starts at the bottom. The value of "cpos" is checked
196 * to see if the position is a player's home, since those are printed
200 bsect(b
, rpos
, cpos
, cnext
)
201 int b
; /* contents of position */
202 int rpos
; /* row of position */
203 int cpos
; /* column of position */
204 int cnext
; /* direction of position */
207 int n
; /* number of men on position */
208 int bct
; /* counter */
210 char pc
; /* color of men on position */
213 n
= abs(b
); /* initialize n and pc */
214 pc
= (b
> 0 ? 'r' : 'w');
216 if (n
< 6 && cpos
< 54) /* position cursor at start */
217 curmove(rpos
, cpos
+ 1);
221 for (j
= 0; j
< 5; j
++) { /* print position row by row */
223 for (k
= 0; k
< 15; k
+= 5) /* print men */
227 if (j
< 4) { /* figure how far to back up for next row */
228 if (n
< 6) { /* stop if none left */
231 bct
= 1; /* single column */
233 if (n
< 11) { /* two columns */
234 if (cpos
== 54) { /* home pos */
240 if (cpos
< 54) { /* not home */
246 } else { /* three columns */
253 curmove(curr
+ cnext
, curc
- bct
); /* reposition cursor */
263 r
= curr
; /* save current position */
266 for (i
= 12; i
> 6; i
--)/* fix positions 12-7 */
267 if (board
[i
] != oldb
[i
]) {
268 fixpos(oldb
[i
], board
[i
], 13, 1 + (12 - i
) * 4, -1);
271 if (board
[0] != oldb
[0]) { /* fix red men on bar */
272 fixpos(oldb
[0], board
[0], 13, 25, -1);
275 for (i
= 6; i
> 0; i
--) /* fix positions 6-1 */
276 if (board
[i
] != oldb
[i
]) {
277 fixpos(oldb
[i
], board
[i
], 13, 29 + (6 - i
) * 4, -1);
280 i
= -(off
[0] < 0 ? off
[0] + 15 : off
[0]); /* fix white's home */
282 fixpos(oldw
, i
, 13, 54, -1);
285 for (i
= 13; i
< 19; i
++) /* fix positions 13-18 */
286 if (board
[i
] != oldb
[i
]) {
287 fixpos(oldb
[i
], board
[i
], 3, 1 + (i
- 13) * 4, 1);
290 if (board
[25] != oldb
[25]) { /* fix white men on bar */
291 fixpos(oldb
[25], board
[25], 3, 25, 1);
292 oldb
[25] = board
[25];
294 for (i
= 19; i
< 25; i
++) /* fix positions 19-24 */
295 if (board
[i
] != oldb
[i
]) {
296 fixpos(oldb
[i
], board
[i
], 3, 29 + (i
- 19) * 4, 1);
299 i
= (off
[1] < 0 ? off
[1] + 15 : off
[1]); /* fix red's home */
301 fixpos(oldr
, i
, 3, 54, 1);
304 curmove(r
, c
); /* return to saved position */
310 fixpos(old
, new, r
, c
, inc
)
311 int old
, new, r
, c
, inc
;
318 if (old
* new >= 0) {
321 col
= (old
+ new > 0 ? 'r' : 'w');
328 nc
= c
< 54 ? c
: c
+ 1;
330 nc
= c
< 54 ? c
+ 1 : c
;
332 fixcol(r
+ inc
* (nv
- n
* 5), nc
, abs(ov
- nv
), ' ', inc
);
334 fixcol(r
+ inc
* (ov
- o
* 5), nc
, abs(ov
- nv
), col
, inc
);
340 fixcol(r
, c
, abs(nv
- 5), col
, inc
);
342 fixcol(r
+ inc
* ov
, c
+ 1,
343 abs(ov
- 5), col
, inc
);
345 fixcol(r
, c
, abs(ov
- 5), ' ', inc
);
347 fixcol(r
+ inc
* nv
, c
+ 1,
348 abs(nv
- 5), ' ', inc
);
354 fixcol(r
+ inc
* (ov
- 5), c
,
355 abs(ov
- 10), col
, inc
);
356 fixcol(r
, c
+ 2, abs(nv
- 10), col
, inc
);
359 fixcol(r
+ inc
* (nv
- 5), c
,
360 abs(nv
- 10), ' ', inc
);
361 fixcol(r
, c
+ 2, abs(ov
- 10), ' ', inc
);
366 fixcol(r
+ inc
* (ov
% 5), c
+ o
, abs(5 * n
- ov
), col
, inc
);
368 fixcol(r
, c
+ n
, abs(5 * n
- nv
), col
, inc
);
370 fixcol(r
+ inc
* (nv
% 5), c
+ n
, abs(5 * n
- nv
), ' ', inc
);
372 fixcol(r
, c
+ o
, abs(5 * o
- ov
), ' ', inc
);
378 fixcol(r
, c
+ 1, nv
, new > 0 ? 'r' : 'w', inc
);
379 if (abs(old
) <= abs(new))
381 fixcol(r
+ inc
* new, c
+ 1, abs(old
+ new), ' ', inc
);
385 fixcol(r
, c
, l
, ch
, inc
)
386 int l
, ch
, r
, c
, inc
;
392 for (i
= 1; i
< l
; i
++) {
393 curmove(curr
+ inc
, curc
- 1);
402 if (curr
== r
&& curc
== c
)
415 int r
; /* destination row */
416 int c
; /* destination column */
417 int mode
= -1; /* mode of movement */
419 int count
= 1000; /* character count */
421 int n
; /* temporary variable */
422 char *m
; /* string containing CM movement */
426 if (realr
== -1) /* see if already there */
429 r
= curr
; /* set current and dest. positions */
434 /* double check position */
435 if (curr
== r
&& curc
== c
) {
439 if (CM
) { /* try CM to get there */
441 m
= (char *) tgoto(CM
, c
, r
);
444 /* try HO and local movement */
445 if (HO
&& (n
= r
+ c
* lND
+ lHO
) < count
) {
449 /* try various LF combinations */
452 if ((n
= (r
- curr
) + c
* lND
+ 1) < count
) {
457 if (c
>= curc
&& (n
= (r
- curr
) + (c
- curc
) * lND
) < count
) {
462 if (c
< curc
&& (n
= (r
- curr
) + (curc
- c
) * lBC
) < count
) {
467 /* try corresponding UP combinations */
470 if ((n
= (curr
- r
) * lUP
+ c
* lND
+ 1) < count
) {
475 if (c
>= curc
&& (n
= (curr
- r
) * lUP
+ (c
- curc
) * lND
) < count
) {
480 if (c
< curc
&& (n
= (curr
- r
) * lUP
+ (curc
- c
) * lBC
) < count
) {
486 if (curr
== r
&& c
> curc
&& linect
[r
] < curc
&& c
- curc
< count
)
491 case -1: /* error! */
492 write(2, "\r\nInternal cursor error.\r\n", 26);
495 /* direct cursor motion */
497 tputs(m
, abs(curr
- r
), addbuf
);
500 /* relative to "home" */
502 tputs(HO
, r
, addbuf
);
503 for (i
= 0; i
< r
; i
++)
505 for (i
= 0; i
< c
; i
++)
506 tputs(ND
, 1, addbuf
);
509 /* CR and down and over */
512 for (i
= 0; i
< r
- curr
; i
++)
514 for (i
= 0; i
< c
; i
++)
515 tputs(ND
, 1, addbuf
);
520 for (i
= 0; i
< r
- curr
; i
++)
522 for (i
= 0; i
< c
- curc
; i
++)
523 tputs(ND
, 1, addbuf
);
528 for (i
= 0; i
< r
- curr
; i
++)
530 for (i
= 0; i
< curc
- c
; i
++)
534 /* CR and up and over */
537 for (i
= 0; i
< curr
- r
; i
++)
538 tputs(UP
, 1, addbuf
);
539 for (i
= 0; i
< c
; i
++)
540 tputs(ND
, 1, addbuf
);
545 for (i
= 0; i
< curr
- r
; i
++)
546 tputs(UP
, 1, addbuf
);
547 for (i
= 0; i
< c
- curc
; i
++)
548 tputs(ND
, 1, addbuf
);
553 for (i
= 0; i
< curr
- r
; i
++)
554 tputs(UP
, 1, addbuf
);
555 for (i
= 0; i
< curc
- c
; i
++) {
557 tputs(BC
, 1, addbuf
);
565 for (i
= 0; i
< c
- curc
; i
++)
581 /* double space if can't clear */
586 curr
= curc
= 0; /* fix position markers */
588 for (i
= 0; i
< 24; i
++)/* clear line counts */
590 buffnum
= -1; /* ignore leftover buffer contents */
591 tputs(CL
, CO
, addbuf
); /* put CL in buffer */
603 char c
; /* character to output */
605 int sp
; /* counts spaces in a tab */
607 if (c
== '\007') { /* bells go in blindly */
611 /* process tabs, use spaces if the the tab should be erasing things,
612 * otherwise use cursor movement routines. Note this does not use
613 * hardware tabs at all. */
615 sp
= (curc
+ 8) & (~7); /* compute spaces */
616 /* check line length */
617 if (linect
[curr
] >= curc
|| sp
< 4) {
618 for (; sp
> curc
; sp
--)
620 curc
= sp
; /* fix curc */
625 /* do newline be calling newline */
630 /* ignore any other control chars */
634 /* if an erasing space or non-space, just add it to buffer. Otherwise
635 * use cursor movement routine, so that multiple spaces will be
636 * grouped together */
637 if (c
> ' ' || linect
[curr
] >= curc
) {
638 newpos(); /* make sure position correct */
639 addbuf(c
); /* add character to buffer */
640 /* fix line length */
641 if (c
== ' ' && linect
[curr
] == curc
)
644 if (linect
[curr
] < curc
)
646 curc
++; /* fix curc */
648 /* use cursor movement routine */
649 curmove(curr
, curc
+ 1);
658 tputs(CD
, CO
- curr
, addbuf
);
659 for (i
= curr
; i
< LI
; i
++)
663 curmove(i
= curr
, 0);
665 while (curr
< LI
- 1) {
666 curmove(curr
+ 1, 0);
667 if (linect
[curr
] > -1)
678 if (curc
> linect
[curr
])
682 tputs(CE
, 1, addbuf
);
683 linect
[curr
] = curc
- 1;
686 while (linect
[curr
] > c
) {
691 curmove(curr
, c
+ 1);
702 curmove(curr
+ 1, 0);
709 char *code
; /* two letter code */
710 char ***cap
; /* pointer to cap string */
711 char *bufp
; /* pointer to cap buffer */
712 char tentry
[1024]; /* temporary uncoded caps buffer */
714 tgetent(tentry
, s
); /* get uncoded termcap entry */
716 LI
= tgetnum("li"); /* get number of lines */
719 CO
= tgetnum("co"); /* get number of columns */
723 bufp
= tbuf
; /* get padding character */
724 tgetstr("pc", &bufp
);
730 bufp
= tbuf
; /* get string entries */
732 for (code
= tcap
; *code
; code
+= 2)
733 **cap
++ = (char *) tgetstr(code
, &bufp
);
735 /* get pertinent lengths */
746 if (LI
< 24 || CO
< 72 || !(CL
&& UP
&& ND
))
748 linect
= (int *) calloc(LI
+ 1, sizeof(int));