]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - backgammon/common_source/fancy.c
1 /* $NetBSD: fancy.c,v 1.10 1999/07/26 18:39:06 hubertf 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.10 1999/07/26 18:39:06 hubertf Exp $");
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 (the
66 * 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 it knows when to reposition. These are -1 if
82 * curr and curc are accurate */
91 curmove(0, 0); /* do top line */
92 for (i
= 0; i
< 53; i
++)
95 curmove(15, 0); /* do botttom line */
96 for (i
= 0; i
< 53; i
++)
99 l
= 1; /* do vertical lines */
100 for (i
= 52; i
> -1; i
-= 28) {
101 curmove((l
== 1 ? 1 : 15), i
);
103 for (j
= 0; j
< 14; j
++) {
104 curmove(curr
+ l
, curc
- 1);
109 l
= -l
; /* alternate directions */
112 curmove(2, 1); /* label positions 13-18 */
113 for (i
= 13; i
< 18; i
++) {
115 fancyc((i
% 10) + '0');
116 curmove(curr
, curc
+ 2);
121 curmove(2, 29); /* label positions 19-24 */
124 for (i
= 20; i
< 25; i
++) {
125 curmove(curr
, curc
+ 2);
127 fancyc((i
% 10) + '0');
130 curmove(14, 1); /* label positions 12-7 */
133 for (i
= 11; i
> 6; i
--) {
134 curmove(curr
, curc
+ 2);
135 fancyc(i
> 9 ? '1' : ' ');
136 fancyc((i
% 10) + '0');
139 curmove(14, 30); /* label positions 6-1 */
141 for (i
= 5; i
> 0; i
--) {
142 curmove(curr
, curc
+ 3);
146 for (i
= 12; i
> 6; i
--)/* print positions 12-7 */
148 bsect(board
[i
], 13, 1 + 4 * (12 - i
), -1);
150 if (board
[0]) /* print red men on bar */
151 bsect(board
[0], 13, 25, -1);
153 for (i
= 6; i
> 0; i
--) /* print positions 6-1 */
155 bsect(board
[i
], 13, 29 + 4 * (6 - i
), -1);
157 l
= (off
[1] < 0 ? off
[1] + 15 : off
[1]); /* print white's home */
160 curmove(8, 25); /* print the word BAR */
165 for (i
= 13; i
< 19; i
++) /* print positions 13-18 */
167 bsect(board
[i
], 3, 1 + 4 * (i
- 13), 1);
169 if (board
[25]) /* print white's men on bar */
170 bsect(board
[25], 3, 25, 1);
172 for (i
= 19; i
< 25; i
++) /* print positions 19-24 */
174 bsect(board
[i
], 3, 29 + 4 * (i
- 19), 1);
176 l
= (off
[0] < 0 ? off
[0] + 15 : off
[0]); /* print red's home */
177 bsect(-l
, 13, 54, -1);
179 for (i
= 0; i
< 26; i
++)/* save board position for refresh later */
181 oldr
= (off
[1] < 0 ? off
[1] + 15 : off
[1]);
182 oldw
= -(off
[0] < 0 ? off
[0] + 15 : off
[0]);
185 * bsect (b,rpos,cpos,cnext)
186 * Print the contents of a board position. "b" has the value of the
187 * position, "rpos" is the row to start printing, "cpos" is the column to
188 * start printing, and "cnext" is positive if the position starts at the top
189 * and negative if it starts at the bottom. The value of "cpos" is checked
190 * to see if the position is a player's home, since those are printed
194 bsect(b
, rpos
, cpos
, cnext
)
195 int b
; /* contents of position */
196 int rpos
; /* row of position */
197 int cpos
; /* column of position */
198 int cnext
; /* direction of position */
201 int n
; /* number of men on position */
202 int bct
; /* counter */
204 char pc
; /* color of men on position */
207 n
= abs(b
); /* initialize n and pc */
208 pc
= (b
> 0 ? 'r' : 'w');
210 if (n
< 6 && cpos
< 54) /* position cursor at start */
211 curmove(rpos
, cpos
+ 1);
215 for (j
= 0; j
< 5; j
++) { /* print position row by row */
217 for (k
= 0; k
< 15; k
+= 5) /* print men */
221 if (j
< 4) { /* figure how far to back up for next row */
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 */
257 r
= curr
; /* save current position */
260 for (i
= 12; i
> 6; i
--)/* fix positions 12-7 */
261 if (board
[i
] != oldb
[i
]) {
262 fixpos(oldb
[i
], board
[i
], 13, 1 + (12 - i
) * 4, -1);
265 if (board
[0] != oldb
[0]) { /* fix red men on bar */
266 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);
274 i
= -(off
[0] < 0 ? off
[0] + 15 : off
[0]); /* fix white's home */
276 fixpos(oldw
, i
, 13, 54, -1);
279 for (i
= 13; i
< 19; i
++) /* fix positions 13-18 */
280 if (board
[i
] != oldb
[i
]) {
281 fixpos(oldb
[i
], board
[i
], 3, 1 + (i
- 13) * 4, 1);
284 if (board
[25] != oldb
[25]) { /* fix white men on bar */
285 fixpos(oldb
[25], board
[25], 3, 25, 1);
286 oldb
[25] = board
[25];
288 for (i
= 19; i
< 25; i
++) /* fix positions 19-24 */
289 if (board
[i
] != oldb
[i
]) {
290 fixpos(oldb
[i
], board
[i
], 3, 29 + (i
- 19) * 4, 1);
293 i
= (off
[1] < 0 ? off
[1] + 15 : off
[1]); /* fix red's home */
295 fixpos(oldr
, i
, 3, 54, 1);
298 curmove(r
, c
); /* return to saved position */
304 fixpos(old
, new, r
, c
, inc
)
305 int old
, new, r
, c
, inc
;
312 if (old
* new >= 0) {
315 col
= (old
+ new > 0 ? 'r' : 'w');
322 nc
= c
< 54 ? c
: c
+ 1;
324 nc
= c
< 54 ? c
+ 1 : c
;
326 fixcol(r
+ inc
* (nv
- n
* 5), nc
, abs(ov
- nv
), ' ', inc
);
328 fixcol(r
+ inc
* (ov
- o
* 5), nc
, abs(ov
- nv
), col
, inc
);
334 fixcol(r
, c
, abs(nv
- 5), col
, inc
);
336 fixcol(r
+ inc
* ov
, c
+ 1,
337 abs(ov
- 5), col
, inc
);
339 fixcol(r
, c
, abs(ov
- 5), ' ', inc
);
341 fixcol(r
+ inc
* nv
, c
+ 1,
342 abs(nv
- 5), ' ', inc
);
348 fixcol(r
+ inc
* (ov
- 5), c
,
349 abs(ov
- 10), col
, inc
);
350 fixcol(r
, c
+ 2, abs(nv
- 10), col
, inc
);
353 fixcol(r
+ inc
* (nv
- 5), c
,
354 abs(nv
- 10), ' ', inc
);
355 fixcol(r
, c
+ 2, abs(ov
- 10), ' ', inc
);
360 fixcol(r
+ inc
* (ov
% 5), c
+ o
, abs(5 * n
- ov
), col
, inc
);
362 fixcol(r
, c
+ n
, abs(5 * n
- nv
), col
, inc
);
364 fixcol(r
+ inc
* (nv
% 5), c
+ n
, abs(5 * n
- nv
), ' ', inc
);
366 fixcol(r
, c
+ o
, abs(5 * o
- ov
), ' ', inc
);
372 fixcol(r
, c
+ 1, nv
, new > 0 ? 'r' : 'w', inc
);
373 if (abs(old
) <= abs(new))
375 fixcol(r
+ inc
* new, c
+ 1, abs(old
+ new), ' ', inc
);
379 fixcol(r
, c
, l
, ch
, inc
)
380 int l
, ch
, r
, c
, inc
;
386 for (i
= 1; i
< l
; i
++) {
387 curmove(curr
+ inc
, curc
- 1);
396 if (curr
== r
&& curc
== c
)
409 int r
; /* destination row */
410 int c
; /* destination column */
411 int mode
= -1; /* mode of movement */
413 int count
= 1000; /* character count */
415 int n
; /* temporary variable */
416 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
) {
433 if (CM
) { /* try CM to get there */
435 m
= (char *) tgoto(CM
, c
, r
);
438 /* try HO and local movement */
439 if (HO
&& (n
= r
+ c
* lND
+ lHO
) < count
) {
443 /* try various LF combinations */
446 if ((n
= (r
- curr
) + c
* lND
+ 1) < count
) {
451 if (c
>= curc
&& (n
= (r
- curr
) + (c
- curc
) * lND
) < count
) {
456 if (c
< curc
&& (n
= (r
- curr
) + (curc
- c
) * lBC
) < count
) {
461 /* try corresponding UP combinations */
464 if ((n
= (curr
- r
) * lUP
+ c
* lND
+ 1) < count
) {
469 if (c
>= curc
&& (n
= (curr
- r
) * lUP
+ (c
- curc
) * lND
) < count
) {
474 if (c
< curc
&& (n
= (curr
- r
) * lUP
+ (curc
- c
) * lBC
) < count
) {
480 if (curr
== r
&& c
> curc
&& linect
[r
] < curc
&& c
- curc
< count
)
485 case -1: /* error! */
486 write(2, "\r\nInternal cursor error.\r\n", 26);
489 /* direct cursor motion */
491 tputs(m
, abs(curr
- r
), addbuf
);
494 /* relative to "home" */
496 tputs(HO
, r
, addbuf
);
497 for (i
= 0; i
< r
; i
++)
499 for (i
= 0; i
< c
; i
++)
500 tputs(ND
, 1, addbuf
);
503 /* CR and down and over */
506 for (i
= 0; i
< r
- curr
; i
++)
508 for (i
= 0; i
< c
; i
++)
509 tputs(ND
, 1, addbuf
);
514 for (i
= 0; i
< r
- curr
; i
++)
516 for (i
= 0; i
< c
- curc
; i
++)
517 tputs(ND
, 1, addbuf
);
522 for (i
= 0; i
< r
- curr
; i
++)
524 for (i
= 0; i
< curc
- c
; i
++)
528 /* CR and up and over */
531 for (i
= 0; i
< curr
- r
; i
++)
532 tputs(UP
, 1, addbuf
);
533 for (i
= 0; i
< c
; i
++)
534 tputs(ND
, 1, addbuf
);
539 for (i
= 0; i
< curr
- r
; i
++)
540 tputs(UP
, 1, addbuf
);
541 for (i
= 0; i
< c
- curc
; i
++)
542 tputs(ND
, 1, addbuf
);
547 for (i
= 0; i
< curr
- r
; i
++)
548 tputs(UP
, 1, addbuf
);
549 for (i
= 0; i
< curc
- c
; i
++) {
551 tputs(BC
, 1, addbuf
);
559 for (i
= 0; i
< c
- curc
; i
++)
575 /* double space if can't clear */
580 curr
= curc
= 0; /* fix position markers */
582 for (i
= 0; i
< 24; i
++)/* clear line counts */
584 buffnum
= -1; /* ignore leftover buffer contents */
585 tputs(CL
, CO
, addbuf
); /* put CL in buffer */
590 char c
; /* character to output */
592 int sp
; /* counts spaces in a tab */
594 if (c
== '\007') { /* bells go in blindly */
598 /* process tabs, use spaces if the the tab should be erasing things,
599 * otherwise use cursor movement routines. Note this does not use
600 * hardware tabs at all. */
602 sp
= (curc
+ 8) & (~7); /* compute spaces */
603 /* check line length */
604 if (linect
[curr
] >= curc
|| sp
< 4) {
605 for (; sp
> curc
; sp
--)
607 curc
= sp
; /* fix curc */
612 /* do newline be calling newline */
617 /* ignore any other control chars */
621 /* if an erasing space or non-space, just add it to buffer. Otherwise
622 * use cursor movement routine, so that multiple spaces will be
623 * grouped together */
624 if (c
> ' ' || linect
[curr
] >= curc
) {
625 newpos(); /* make sure position correct */
626 addbuf(c
); /* add character to buffer */
627 /* fix line length */
628 if (c
== ' ' && linect
[curr
] == curc
)
631 if (linect
[curr
] < curc
)
633 curc
++; /* fix curc */
635 /* use cursor movement routine */
636 curmove(curr
, curc
+ 1);
645 tputs(CD
, CO
- curr
, addbuf
);
646 for (i
= curr
; i
< LI
; i
++)
650 curmove(i
= curr
, 0);
652 while (curr
< LI
- 1) {
653 curmove(curr
+ 1, 0);
654 if (linect
[curr
] > -1)
665 if (curc
> linect
[curr
])
669 tputs(CE
, 1, addbuf
);
670 linect
[curr
] = curc
- 1;
673 while (linect
[curr
] > c
) {
678 curmove(curr
, c
+ 1);
689 curmove(curr
+ 1, 0);
696 char *code
; /* two letter code */
697 char ***cap
; /* pointer to cap string */
698 char *bufp
; /* pointer to cap buffer */
699 char tentry
[1024]; /* temporary uncoded caps buffer */
701 tgetent(tentry
, s
); /* get uncoded termcap entry */
703 LI
= tgetnum("li"); /* get number of lines */
706 CO
= tgetnum("co"); /* get number of columns */
710 bufp
= tbuf
; /* get padding character */
711 tgetstr("pc", &bufp
);
717 bufp
= tbuf
; /* get string entries */
719 for (code
= tcap
; *code
; code
+= 2)
720 **cap
++ = (char *) tgetstr(code
, &bufp
);
722 /* get pertinent lengths */
733 if (LI
< 24 || CO
< 72 || !(CL
&& UP
&& ND
))
735 linect
= (int *) calloc(LI
+ 1, sizeof(int));
736 if (linect
== NULL
) {
737 write(2, "\r\nOut of memory!\r\n", 18);