]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - backgammon/common_source/fancy.c
2 * Copyright (c) 1980 Regents of the University of California.
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
35 /*static char sccsid[] = "from: @(#)fancy.c 5.5 (Berkeley) 6/1/90";*/
36 static char rcsid
[] = "$Id: fancy.c,v 1.2 1993/08/01 18:56:43 mycroft Exp $";
41 char PC
; /* padding character */
42 char *BC
; /* backspace sequence */
43 char *CD
; /* clear to end of screen sequence */
44 char *CE
; /* clear to end of line sequence */
45 char *CL
; /* clear screen sequence */
46 char *CM
; /* cursor movement instructions */
47 char *HO
; /* home cursor sequence */
48 char *MC
; /* column cursor movement map */
49 char *ML
; /* row cursor movement map */
50 char *ND
; /* forward cursor sequence */
51 char *UP
; /* up cursor sequence */
53 int lHO
; /* length of HO */
54 int lBC
; /* length of BC */
55 int lND
; /* length of ND */
56 int lUP
; /* length of UP */
57 int CO
; /* number of columns */
58 int LI
; /* number of lines */
59 int *linect
; /* array of lengths of lines on screen
60 (the actual screen is not stored) */
62 /* two letter codes */
63 char tcap
[] = "bccdceclcmhomcmlndup";
64 /* corresponding strings */
65 char **tstr
[] = { &BC
, &CD
, &CE
, &CL
, &CM
, &HO
, &MC
, &ML
, &ND
, &UP
};
67 int buffnum
; /* pointer to output buffer */
69 char tbuf
[1024]; /* buffer for decoded termcap entries */
71 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};
75 /* "real" cursor positions, so
76 * it knows when to reposition.
77 * These are -1 if curr and curc
87 curmove (0,0); /* do top line */
88 for (i
= 0; i
< 53; i
++)
91 curmove (15,0); /* do botttom line */
92 for (i
= 0; i
< 53; i
++)
95 l
= 1; /* do vertical lines */
96 for (i
= 52; i
> -1; i
-= 28) {
97 curmove ( (l
== 1? 1: 15) ,i
);
99 for (j
= 0; j
< 14; j
++) {
100 curmove (curr
+l
,curc
-1);
105 l
= -l
; /* alternate directions */
108 curmove (2,1); /* label positions 13-18 */
109 for (i
= 13; i
< 18; i
++) {
111 fancyc ((i
% 10)+'0');
112 curmove (curr
,curc
+2);
117 curmove (2,29); /* label positions 19-24 */
120 for (i
= 20; i
< 25; i
++) {
121 curmove (curr
,curc
+2);
123 fancyc ((i
% 10)+'0');
126 curmove (14,1); /* label positions 12-7 */
129 for (i
= 11; i
> 6; i
--) {
130 curmove (curr
,curc
+2);
131 fancyc (i
> 9? '1': ' ');
132 fancyc ((i
% 10)+'0');
135 curmove (14,30); /* label positions 6-1 */
137 for (i
= 5; i
> 0; i
--) {
138 curmove (curr
,curc
+3);
142 for (i
= 12; i
> 6; i
--) /* print positions 12-7 */
144 bsect (board
[i
],13,1+4*(12-i
),-1);
146 if (board
[0]) /* print red men on bar */
147 bsect (board
[0],13,25,-1);
149 for (i
= 6; i
> 0; i
--) /* print positions 6-1 */
151 bsect (board
[i
],13,29+4*(6-i
),-1);
153 l
= (off
[1] < 0? off
[1]+15: off
[1]); /* print white's home */
156 curmove (8,25); /* print the word BAR */
161 for (i
= 13; i
< 19; i
++) /* print positions 13-18 */
163 bsect (board
[i
],3,1+4*(i
-13),1);
165 if (board
[25]) /* print white's men on bar */
166 bsect (board
[25],3,25,1);
168 for (i
= 19; i
< 25; i
++) /* print positions 19-24 */
170 bsect (board
[i
],3,29+4*(i
-19),1);
172 l
= (off
[0] < 0? off
[0]+15: off
[0]); /* print red's home */
175 for (i
= 0; i
< 26; i
++) /* save board position
176 * for refresh later */
178 oldr
= (off
[1] < 0? off
[1]+15: off
[1]);
179 oldw
= -(off
[0] < 0? off
[0]+15: off
[0]);
183 * bsect (b,rpos,cpos,cnext)
184 * Print the contents of a board position. "b" has the value of the
185 * position, "rpos" is the row to start printing, "cpos" is the column to
186 * start printing, and "cnext" is positive if the position starts at the top
187 * and negative if it starts at the bottom. The value of "cpos" is checked
188 * to see if the position is a player's home, since those are printed
192 bsect (b
,rpos
,cpos
,cnext
)
193 int b
; /* contents of position */
194 int rpos
; /* row of position */
195 int cpos
; /* column of position */
196 int cnext
; /* direction of position */
199 register int j
; /* index */
200 register int n
; /* number of men on position */
201 register int bct
; /* counter */
203 char pc
; /* color of men on position */
205 n
= abs(b
); /* initialize n and pc */
206 pc
= (b
> 0? 'r': 'w');
208 if (n
< 6 && cpos
< 54) /* position cursor at start */
209 curmove (rpos
,cpos
+1);
213 for (j
= 0; j
< 5; j
++) { /* print position row by row */
215 for (k
= 0; k
< 15; k
+= 5) /* print men */
219 if (j
< 4) { /* figure how far to
222 if (n
< 6) { /* stop if none left */
225 bct
= 1; /* single column */
227 if (n
< 11) { /* two columns */
228 if (cpos
== 54) { /* home pos */
234 if (cpos
< 54) { /* not home */
240 } else { /* three columns */
247 curmove (curr
+cnext
,curc
-bct
); /* reposition cursor */
253 register int i
, r
, c
;
255 r
= curr
; /* save current position */
258 for (i
= 12; i
> 6; i
--) /* fix positions 12-7 */
259 if (board
[i
] != oldb
[i
]) {
260 fixpos (oldb
[i
],board
[i
],13,1+(12-i
)*4,-1);
264 if (board
[0] != oldb
[0]) { /* fix red men on bar */
265 fixpos (oldb
[0],board
[0],13,25,-1);
269 for (i
= 6; i
> 0; i
--) /* fix positions 6-1 */
270 if (board
[i
] != oldb
[i
]) {
271 fixpos (oldb
[i
],board
[i
],13,29+(6-i
)*4,-1);
275 i
= -(off
[0] < 0? off
[0]+15: off
[0]); /* fix white's home */
277 fixpos (oldw
,i
,13,54,-1);
281 for (i
= 13; i
< 19; i
++) /* fix positions 13-18 */
282 if (board
[i
] != oldb
[i
]) {
283 fixpos (oldb
[i
],board
[i
],3,1+(i
-13)*4,1);
287 if (board
[25] != oldb
[25]) { /* fix white men on bar */
288 fixpos (oldb
[25],board
[25],3,25,1);
289 oldb
[25] = board
[25];
292 for (i
= 19; i
< 25; i
++) /* fix positions 19-24 */
293 if (board
[i
] != oldb
[i
]) {
294 fixpos (oldb
[i
],board
[i
],3,29+(i
-19)*4,1);
298 i
= (off
[1] < 0? off
[1]+15: off
[1]); /* fix red's home */
300 fixpos (oldr
,i
,3,54,1);
304 curmove (r
,c
); /* return to saved position */
309 fixpos (old
,new,r
,c
,inc
)
310 int old
, new, r
, c
, inc
;
313 register int o
, n
, nv
;
320 col
= (old
+new > 0? 'r': 'w');
331 fixcol (r
+inc
*(nv
-n
*5),nc
,abs(ov
-nv
),' ',inc
);
333 fixcol (r
+inc
*(ov
-o
*5),nc
,abs(ov
-nv
),col
,inc
);
339 fixcol (r
,c
,abs(nv
-5),col
,inc
);
341 fixcol (r
+inc
*ov
,c
+1,abs(ov
-5),col
,inc
);
343 fixcol (r
,c
,abs(ov
-5),' ',inc
);
345 fixcol (r
+inc
*nv
,c
+1,abs(nv
-5),' ',inc
);
351 fixcol (r
+inc
*(ov
-5),c
,abs(ov
-10),col
,inc
);
352 fixcol (r
,c
+2,abs(nv
-10),col
,inc
);
355 fixcol (r
+inc
*(nv
-5),c
,abs(nv
-10),' ',inc
);
356 fixcol (r
,c
+2,abs(ov
-10),' ',inc
);
361 fixcol (r
+inc
*(ov%5
),c
+o
,abs(5*n
-ov
),col
,inc
);
363 fixcol (r
,c
+n
,abs(5*n
-nv
),col
,inc
);
365 fixcol (r
+inc
*(nv%5
),c
+n
,abs(5*n
-nv
),' ',inc
);
367 fixcol (r
,c
+o
,abs(5*o
-ov
),' ',inc
);
373 fixcol (r
,c
+1,nv
,new > 0? 'r': 'w',inc
);
374 if (abs(old
) <= abs(new))
376 fixcol (r
+inc
*new,c
+1,abs(old
+new),' ',inc
);
379 fixcol (r
,c
,l
,ch
,inc
)
388 for (i
= 1; i
< l
; i
++) {
389 curmove (curr
+inc
,curc
-1);
398 if (curr
== r
&& curc
== c
)
409 register int r
; /* destination row */
410 register int c
; /* destination column */
411 register int mode
= -1; /* mode of movement */
413 int count
= 1000; /* character count */
416 int n
; /* temporary variable */
417 char *m
; /* string containing CM movement */
420 if (realr
== -1) /* see if already there */
423 r
= curr
; /* set current and dest. positions */
428 /* double check position */
429 if (curr
== r
&& curc
== c
) {
434 if (CM
) { /* try CM to get there */
436 m
= (char *)tgoto (CM
,c
,r
);
440 /* try HO and local movement */
441 if (HO
&& (n
= r
+c
*lND
+lHO
) < count
) {
446 /* try various LF combinations */
449 if ((n
= (r
-curr
)+c
*lND
+1) < count
) {
454 if (c
>= curc
&& (n
= (r
-curr
)+(c
-curc
)*lND
) < count
) {
459 if (c
< curc
&& (n
= (r
-curr
)+(curc
-c
)*lBC
) < count
) {
465 /* try corresponding UP combinations */
468 if ((n
= (curr
-r
)*lUP
+c
*lND
+1) < count
) {
473 if (c
>= curc
&& (n
= (curr
-r
)*lUP
+(c
-curc
)*lND
) < count
) {
478 if (c
< curc
&& (n
= (curr
-r
)*lUP
+(curc
-c
)*lBC
) < count
) {
485 if (curr
== r
&& c
> curc
&& linect
[r
] < curc
&& c
-curc
< count
)
490 case -1: /* error! */
491 write (2,"\r\nInternal cursor error.\r\n",26);
494 /* direct cursor motion */
496 tputs (m
,abs(curr
-r
),addbuf
);
499 /* relative to "home" */
502 for (i
= 0; i
< r
; i
++)
504 for (i
= 0; i
< c
; i
++)
508 /* CR and down and over */
511 for (i
= 0; i
< r
-curr
; i
++)
513 for (i
= 0; i
< c
; i
++)
519 for (i
= 0; i
< r
-curr
; i
++)
521 for (i
= 0; i
< c
-curc
; i
++)
527 for (i
= 0; i
< r
-curr
; i
++)
529 for (i
= 0; i
< curc
-c
; i
++)
533 /* CR and up and over */
536 for (i
= 0; i
< curr
-r
; i
++)
538 for (i
= 0; i
< c
; i
++)
544 for (i
= 0; i
< curr
-r
; i
++)
546 for (i
= 0; i
< c
-curc
; i
++)
552 for (i
= 0; i
< curr
-r
; i
++)
554 for (i
= 0; i
< curc
-c
; i
++) {
564 for (i
= 0; i
< c
-curc
; i
++)
578 /* double space if can't clear */
584 curr
= curc
= 0; /* fix position markers */
586 for (i
= 0; i
< 24; i
++) /* clear line counts */
588 buffnum
= -1; /* ignore leftover buffer contents */
589 tputs (CL
,CO
,addbuf
); /* put CL in buffer */
592 tos () { /* home cursor */
597 register char c
; /* character to output */
599 register int sp
; /* counts spaces in a tab */
601 if (c
== '\007') { /* bells go in blindly */
606 /* process tabs, use spaces if the
607 * the tab should be erasing things,
608 * otherwise use cursor movement
609 * routines. Note this does not use
610 * hardware tabs at all. */
612 sp
= (curc
+8) & (~ 7); /* compute spaces */
613 /* check line length */
614 if (linect
[curr
] >= curc
|| sp
< 4) {
615 for (; sp
> curc
; sp
--)
617 curc
= sp
; /* fix curc */
623 /* do newline be calling newline */
629 /* ignore any other control chars */
633 /* if an erasing space or non-space,
634 * just add it to buffer. Otherwise
635 * use cursor movement routine, so that
636 * multiple spaces will be grouped
638 if (c
> ' ' || linect
[curr
] >= curc
) {
639 newpos (); /* make sure position correct */
640 addbuf (c
); /* add character to buffer */
641 /* fix line length */
642 if (c
== ' ' && linect
[curr
] == curc
)
644 else 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
++)
664 curmove (i
= curr
,0);
666 while (curr
< LI
-1) {
668 if (linect
[curr
] > -1)
679 if (curc
> linect
[curr
])
684 linect
[curr
] = curc
-1;
687 while (linect
[curr
] > c
) {
708 register char *code
; /* two letter code */
709 register char ***cap
; /* pointer to cap string */
710 char *bufp
; /* pointer to cap buffer */
711 char tentry
[1024]; /* temporary uncoded caps buffer */
713 tgetent (tentry
,s
); /* get uncoded termcap entry */
715 LI
= tgetnum ("li"); /* get number of lines */
718 CO
= tgetnum ("co"); /* get number of columns */
722 bufp
= tbuf
; /* get padding character */
723 tgetstr ("pc",&bufp
);
729 bufp
= tbuf
; /* get string entries */
731 for (code
= tcap
; *code
; code
+= 2)
732 **cap
++ = (char *)tgetstr (code
,&bufp
);
734 /* get pertinent lengths */
745 if (LI
< 24 || CO
< 72 || !(CL
&& UP
&& ND
))
747 linect
= (int *)calloc (LI
+1,sizeof(int));