2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)fancy.c 8.1 (Berkeley) 5/31/93
34 * $FreeBSD: src/games/backgammon/common_source/fancy.c,v 1.7 1999/11/30 03:48:25 billf Exp $
35 * $DragonFly: src/games/backgammon/common_source/fancy.c,v 1.4 2006/08/08 16:36:11 pavalos Exp $
42 static void bsect(int, int, int, int);
43 static void fixpos(int, int, int, int, int);
44 static void fixcol(int, int, int, int, int);
45 static void newline(void);
47 char PC
; /* padding character */
48 char *BC
; /* backspace sequence */
49 char *CD
; /* clear to end of screen sequence */
50 char *CE
; /* clear to end of line sequence */
51 char *CL
; /* clear screen sequence */
52 char *CM
; /* cursor movement instructions */
53 char *HO
; /* home cursor sequence */
54 char *MC
; /* column cursor movement map */
55 char *ML
; /* row cursor movement map */
56 char *ND
; /* forward cursor sequence */
57 char *UP
; /* up cursor sequence */
59 int lHO
; /* length of HO */
60 int lBC
; /* length of BC */
61 int lND
; /* length of ND */
62 int lUP
; /* length of UP */
63 int CO
; /* number of columns */
64 int LI
; /* number of lines */
65 int *linect
; /* array of lengths of lines on screen
66 (the actual screen is not stored) */
68 /* two letter codes */
69 char tcap
[] = "bccdceclcmhomcmlndup";
70 /* corresponding strings */
71 char **tstr
[] = { &BC
, &CD
, &CE
, &CL
, &CM
, &HO
, &MC
, &ML
, &ND
, &UP
};
73 int buffnum
; /* pointer to output buffer */
75 char tbuf
[1024]; /* buffer for decoded termcap entries */
77 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};
81 /* "real" cursor positions, so
82 * it knows when to reposition.
83 * These are -1 if curr and curc
93 curmove (0,0); /* do top line */
94 for (i
= 0; i
< 53; i
++)
97 curmove (15,0); /* do botttom line */
98 for (i
= 0; i
< 53; i
++)
101 l
= 1; /* do vertical lines */
102 for (i
= 52; i
> -1; i
-= 28) {
103 curmove ( (l
== 1? 1: 15) ,i
);
105 for (j
= 0; j
< 14; j
++) {
106 curmove (curr
+l
,curc
-1);
111 l
= -l
; /* alternate directions */
114 curmove (2,1); /* label positions 13-18 */
115 for (i
= 13; i
< 18; i
++) {
117 fancyc ((i
% 10)+'0');
118 curmove (curr
,curc
+2);
123 curmove (2,29); /* label positions 19-24 */
126 for (i
= 20; i
< 25; i
++) {
127 curmove (curr
,curc
+2);
129 fancyc ((i
% 10)+'0');
132 curmove (14,1); /* label positions 12-7 */
135 for (i
= 11; i
> 6; i
--) {
136 curmove (curr
,curc
+2);
137 fancyc (i
> 9? '1': ' ');
138 fancyc ((i
% 10)+'0');
141 curmove (14,30); /* label positions 6-1 */
143 for (i
= 5; i
> 0; i
--) {
144 curmove (curr
,curc
+3);
148 for (i
= 12; i
> 6; i
--) /* print positions 12-7 */
150 bsect (board
[i
],13,1+4*(12-i
),-1);
152 if (board
[0]) /* print red men on bar */
153 bsect (board
[0],13,25,-1);
155 for (i
= 6; i
> 0; i
--) /* print positions 6-1 */
157 bsect (board
[i
],13,29+4*(6-i
),-1);
159 l
= (off
[1] < 0? off
[1]+15: off
[1]); /* print white's home */
162 curmove (8,25); /* print the word BAR */
167 for (i
= 13; i
< 19; i
++) /* print positions 13-18 */
169 bsect (board
[i
],3,1+4*(i
-13),1);
171 if (board
[25]) /* print white's men on bar */
172 bsect (board
[25],3,25,1);
174 for (i
= 19; i
< 25; i
++) /* print positions 19-24 */
176 bsect (board
[i
],3,29+4*(i
-19),1);
178 l
= (off
[0] < 0? off
[0]+15: off
[0]); /* print red's home */
181 for (i
= 0; i
< 26; i
++) /* save board position
182 * for refresh later */
184 oldr
= (off
[1] < 0? off
[1]+15: off
[1]);
185 oldw
= -(off
[0] < 0? off
[0]+15: off
[0]);
189 * bsect (b,rpos,cpos,cnext)
190 * Print the contents of a board position. "b" has the value of the
191 * position, "rpos" is the row to start printing, "cpos" is the column to
192 * start printing, and "cnext" is positive if the position starts at the top
193 * and negative if it starts at the bottom. The value of "cpos" is checked
194 * to see if the position is a player's home, since those are printed
199 bsect(int b
, int rpos
, int cpos
, int cnext
)
202 int n
; /* number of men on position */
203 int bct
; /* counter */
205 char pc
; /* color of men on position */
208 n
= abs(b
); /* initialize n and pc */
209 pc
= (b
> 0? 'r': 'w');
211 if (n
< 6 && cpos
< 54) /* position cursor at start */
212 curmove (rpos
,cpos
+1);
216 for (j
= 0; j
< 5; j
++) { /* print position row by row */
218 for (k
= 0; k
< 15; k
+= 5) /* print men */
222 if (j
< 4) { /* figure how far to
225 if (n
< 6) { /* stop if none left */
228 bct
= 1; /* single column */
230 if (n
< 11) { /* two columns */
231 if (cpos
== 54) { /* home pos */
237 if (cpos
< 54) { /* not home */
243 } else { /* three columns */
250 curmove (curr
+cnext
,curc
-bct
); /* reposition cursor */
260 r
= curr
; /* save current position */
263 for (i
= 12; i
> 6; i
--) /* fix positions 12-7 */
264 if (board
[i
] != oldb
[i
]) {
265 fixpos (oldb
[i
],board
[i
],13,1+(12-i
)*4,-1);
269 if (board
[0] != oldb
[0]) { /* fix red men on bar */
270 fixpos (oldb
[0],board
[0],13,25,-1);
274 for (i
= 6; i
> 0; i
--) /* fix positions 6-1 */
275 if (board
[i
] != oldb
[i
]) {
276 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);
286 for (i
= 13; i
< 19; i
++) /* fix positions 13-18 */
287 if (board
[i
] != oldb
[i
]) {
288 fixpos (oldb
[i
],board
[i
],3,1+(i
-13)*4,1);
292 if (board
[25] != oldb
[25]) { /* fix white men on bar */
293 fixpos (oldb
[25],board
[25],3,25,1);
294 oldb
[25] = board
[25];
297 for (i
= 19; i
< 25; i
++) /* fix positions 19-24 */
298 if (board
[i
] != oldb
[i
]) {
299 fixpos (oldb
[i
],board
[i
],3,29+(i
-19)*4,1);
303 i
= (off
[1] < 0? off
[1]+15: off
[1]); /* fix red's home */
305 fixpos (oldr
,i
,3,54,1);
309 curmove (r
,c
); /* return to saved position */
315 fixpos(int cur
, int new, int r
, int c
, int inc
)
325 col
= (cur
+new > 0? 'r': 'w');
336 fixcol (r
+inc
*(nv
-n
*5),nc
,abs(ov
-nv
),' ',inc
);
338 fixcol (r
+inc
*(ov
-o
*5),nc
,abs(ov
-nv
),col
,inc
);
344 fixcol (r
,c
,abs(nv
-5),col
,inc
);
346 fixcol (r
+inc
*ov
,c
+1,abs(ov
-5),col
,inc
);
348 fixcol (r
,c
,abs(ov
-5),' ',inc
);
350 fixcol (r
+inc
*nv
,c
+1,abs(nv
-5),' ',inc
);
356 fixcol (r
+inc
*(ov
-5),c
,abs(ov
-10),col
,inc
);
357 fixcol (r
,c
+2,abs(nv
-10),col
,inc
);
360 fixcol (r
+inc
*(nv
-5),c
,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(cur
) <= abs(new))
381 fixcol (r
+inc
*new,c
+1,abs(cur
+new),' ',inc
);
385 fixcol(int r
, int c
, int l
, int ch
, int inc
)
391 for (i
= 1; i
< l
; i
++) {
392 curmove (curr
+inc
,curc
-1);
398 curmove(int r
, int c
)
400 if (curr
== r
&& curc
== c
)
413 int r
; /* destination row */
414 int c
; /* destination column */
415 int mode
= -1; /* mode of movement */
417 int ccount
= 1000; /* character count */
419 int n
; /* temporary variable */
420 char *m
; /* string containing CM movement */
423 if (realr
== -1) /* see if already there */
426 r
= curr
; /* set current and dest. positions */
431 /* double check position */
432 if (curr
== r
&& curc
== c
) {
437 if (CM
) { /* try CM to get there */
439 m
= (char *)tgoto (CM
,c
,r
);
443 /* try HO and local movement */
444 if (HO
&& (n
= r
+c
*lND
+lHO
) < ccount
) {
449 /* try various LF combinations */
452 if ((n
= (r
-curr
)+c
*lND
+1) < ccount
) {
457 if (c
>= curc
&& (n
= (r
-curr
)+(c
-curc
)*lND
) < ccount
) {
462 if (c
< curc
&& (n
= (r
-curr
)+(curc
-c
)*lBC
) < ccount
) {
468 /* try corresponding UP combinations */
471 if ((n
= (curr
-r
)*lUP
+c
*lND
+1) < ccount
) {
476 if (c
>= curc
&& (n
= (curr
-r
)*lUP
+(c
-curc
)*lND
) < ccount
) {
481 if (c
< curc
&& (n
= (curr
-r
)*lUP
+(curc
-c
)*lBC
) < ccount
) {
488 if (curr
== r
&& c
> curc
&& linect
[r
] < curc
&& c
-curc
< ccount
)
493 case -1: /* error! */
494 write (2,"\r\nInternal cursor error.\r\n",26);
497 /* direct cursor motion */
499 tputs (m
,abs(curr
-r
),addbuf
);
502 /* relative to "home" */
505 for (i
= 0; i
< r
; i
++)
507 for (i
= 0; i
< c
; i
++)
511 /* CR and down and over */
514 for (i
= 0; i
< r
-curr
; i
++)
516 for (i
= 0; i
< c
; i
++)
522 for (i
= 0; i
< r
-curr
; i
++)
524 for (i
= 0; i
< c
-curc
; i
++)
530 for (i
= 0; i
< r
-curr
; i
++)
532 for (i
= 0; i
< curc
-c
; i
++)
536 /* CR and up and over */
539 for (i
= 0; i
< curr
-r
; i
++)
541 for (i
= 0; i
< c
; i
++)
547 for (i
= 0; i
< curr
-r
; i
++)
549 for (i
= 0; i
< c
-curc
; i
++)
555 for (i
= 0; i
< curr
-r
; i
++)
557 for (i
= 0; i
< curc
-c
; i
++) {
567 for (i
= 0; i
< c
-curc
; i
++)
583 /* double space if can't clear */
589 curr
= curc
= 0; /* fix position markers */
591 for (i
= 0; i
< 24; i
++) /* clear line counts */
593 buffnum
= -1; /* ignore leftover buffer contents */
594 tputs (CL
,CO
,addbuf
); /* put CL in buffer */
598 /* input is character to output */
602 int sp
; /* counts spaces in a tab */
604 if (c
== '\007') { /* bells go in blindly */
609 /* process tabs, use spaces if the
610 * the tab should be erasing things,
611 * otherwise use cursor movement
612 * 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 */
626 /* do newline be calling newline */
632 /* ignore any other control chars */
636 /* if an erasing space or non-space,
637 * just add it to buffer. Otherwise
638 * use cursor movement routine, so that
639 * multiple spaces will be grouped
641 if (c
> ' ' || linect
[curr
] >= curc
) {
642 newpos (); /* make sure position correct */
643 addbuf (c
); /* add character to buffer */
644 /* fix line length */
645 if (c
== ' ' && linect
[curr
] == curc
)
647 else if (linect
[curr
] < curc
)
649 curc
++; /* fix curc */
651 /* use cursor movement routine */
652 curmove (curr
,curc
+1);
661 tputs (CD
,CO
-curr
,addbuf
);
662 for (i
= curr
; i
< LI
; i
++)
667 curmove (i
= curr
,0);
669 while (curr
< LI
-1) {
671 if (linect
[curr
] > -1)
682 if (curc
> linect
[curr
])
687 linect
[curr
] = curc
-1;
690 while (linect
[curr
] > c
) {
710 getcaps(const char *s
)
712 char *code
; /* two letter code */
713 char ***cap
; /* pointer to cap string */
714 char *bufp
; /* pointer to cap buffer */
715 char tentry
[1024]; /* temporary uncoded caps buffer */
717 tgetent (tentry
, s
); /* get uncoded termcap entry */
719 LI
= tgetnum ("li"); /* get number of lines */
722 CO
= tgetnum ("co"); /* get number of columns */
726 bufp
= tbuf
; /* get padding character */
727 tgetstr ("pc",&bufp
);
733 bufp
= tbuf
; /* get string entries */
735 for (code
= tcap
; *code
; code
+= 2)
736 **cap
++ = (char *)tgetstr (code
,&bufp
);
738 /* get pertinent lengths */
749 if (LI
< 24 || CO
< 72 || !(CL
&& UP
&& ND
))
751 linect
= (int *)calloc (LI
+1,sizeof(int));