]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - backgammon/common_source/fancy.c
9df17be3a315ad3f6b67ee9ce5e694d97dae3699
1 /* $NetBSD: fancy.c,v 1.16 2021/04/13 02:11:39 mrg 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. 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.
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
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)fancy.c 8.1 (Berkeley) 5/31/93";
37 __RCSID("$NetBSD: fancy.c,v 1.16 2021/04/13 02:11:39 mrg Exp $");
43 static void bsect(int, int, int, int);
44 static void fixpos(int, int, int, int, int);
45 static void fixcol(int, int, int, int, int);
46 static void newline(void);
49 * These need to be declared so they come out as commons, because
50 * termcap might or might not define some of them. Our termcap defines
51 * PC, BC, and UP only. This is gross.
53 * XXX: rewrite this crap using curses.
56 char PC
; /* padding character */
57 char *BC
; /* backspace sequence */
59 char *CD
; /* clear to end of screen sequence */
60 char *CE
; /* clear to end of line sequence */
61 char *CL
; /* clear screen sequence */
62 char *CM
; /* cursor movement instructions */
63 char *HO
; /* home cursor sequence */
64 char *MC
; /* column cursor movement map */
65 char *ML
; /* row cursor movement map */
66 char *ND
; /* forward cursor sequence */
68 char *UP
; /* up cursor sequence */
71 static int lHO
; /* length of HO */
72 static int lBC
; /* length of BC */
73 static int lND
; /* length of ND */
74 static int lUP
; /* length of UP */
75 static int CO
; /* number of columns */
76 static int LI
; /* number of lines */
77 static int *linect
; /* array of lengths of lines on screen (the
78 * actual screen is not stored) */
80 /* two letter codes */
81 static char tcap
[] = "bccdceclcmhomcmlndup";
82 /* corresponding strings */
83 static char **tstr
[] = {&BC
, &CD
, &CE
, &CL
, &CM
, &HO
, &MC
, &ML
, &ND
, &UP
};
85 static char tbuf
[1024]; /* buffer for decoded termcap entries */
87 static int oldb
[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
92 /* "real" cursor positions, so it knows when to reposition. These are -1 if
93 * curr and curc are accurate */
102 curmove(0, 0); /* do top line */
103 for (i
= 0; i
< 53; i
++)
106 curmove(15, 0); /* do botttom line */
107 for (i
= 0; i
< 53; i
++)
110 l
= 1; /* do vertical lines */
111 for (i
= 52; i
> -1; i
-= 28) {
112 curmove((l
== 1 ? 1 : 15), i
);
114 for (j
= 0; j
< 14; j
++) {
115 curmove(curr
+ l
, curc
- 1);
120 l
= -l
; /* alternate directions */
123 curmove(2, 1); /* label positions 13-18 */
124 for (i
= 13; i
< 18; i
++) {
126 fancyc((i
% 10) + '0');
127 curmove(curr
, curc
+ 2);
132 curmove(2, 29); /* label positions 19-24 */
135 for (i
= 20; i
< 25; i
++) {
136 curmove(curr
, curc
+ 2);
138 fancyc((i
% 10) + '0');
141 curmove(14, 1); /* label positions 12-7 */
144 for (i
= 11; i
> 6; i
--) {
145 curmove(curr
, curc
+ 2);
146 fancyc(i
> 9 ? '1' : ' ');
147 fancyc((i
% 10) + '0');
150 curmove(14, 30); /* label positions 6-1 */
152 for (i
= 5; i
> 0; i
--) {
153 curmove(curr
, curc
+ 3);
157 for (i
= 12; i
> 6; i
--)/* print positions 12-7 */
159 bsect(board
[i
], 13, 1 + 4 * (12 - i
), -1);
161 if (board
[0]) /* print red men on bar */
162 bsect(board
[0], 13, 25, -1);
164 for (i
= 6; i
> 0; i
--) /* print positions 6-1 */
166 bsect(board
[i
], 13, 29 + 4 * (6 - i
), -1);
168 l
= (off
[1] < 0 ? off
[1] + 15 : off
[1]); /* print white's home */
171 curmove(8, 25); /* print the word BAR */
176 for (i
= 13; i
< 19; i
++) /* print positions 13-18 */
178 bsect(board
[i
], 3, 1 + 4 * (i
- 13), 1);
180 if (board
[25]) /* print white's men on bar */
181 bsect(board
[25], 3, 25, 1);
183 for (i
= 19; i
< 25; i
++) /* print positions 19-24 */
185 bsect(board
[i
], 3, 29 + 4 * (i
- 19), 1);
187 l
= (off
[0] < 0 ? off
[0] + 15 : off
[0]); /* print red's home */
188 bsect(-l
, 13, 54, -1);
190 for (i
= 0; i
< 26; i
++)/* save board position for refresh later */
192 oldr
= (off
[1] < 0 ? off
[1] + 15 : off
[1]);
193 oldw
= -(off
[0] < 0 ? off
[0] + 15 : off
[0]);
196 * bsect (b,rpos,cpos,cnext)
197 * Print the contents of a board position. "b" has the value of the
198 * position, "rpos" is the row to start printing, "cpos" is the column to
199 * start printing, and "cnext" is positive if the position starts at the top
200 * and negative if it starts at the bottom. The value of "cpos" is checked
201 * to see if the position is a player's home, since those are printed
205 bsect(int b
, int rpos
, int cpos
, int cnext
)
208 int n
; /* number of men on position */
209 int bct
; /* counter */
211 char pc
; /* color of men on position */
214 n
= abs(b
); /* initialize n and pc */
215 pc
= (b
> 0 ? 'r' : 'w');
217 if (n
< 6 && cpos
< 54) /* position cursor at start */
218 curmove(rpos
, cpos
+ 1);
222 for (j
= 0; j
< 5; j
++) { /* print position row by row */
224 for (k
= 0; k
< 15; k
+= 5) /* print men */
228 if (j
< 4) { /* figure how far to back up for next row */
229 if (n
< 6) { /* stop if none left */
232 bct
= 1; /* single column */
234 if (n
< 11) { /* two columns */
235 if (cpos
== 54) { /* home pos */
241 if (cpos
< 54) { /* not home */
247 } else { /* three columns */
254 /* reposition cursor */
255 curmove(curr
+ cnext
, curc
- bct
);
265 r
= curr
; /* save current position */
268 for (i
= 12; i
> 6; i
--)/* fix positions 12-7 */
269 if (board
[i
] != oldb
[i
]) {
270 fixpos(oldb
[i
], board
[i
], 13, 1 + (12 - i
) * 4, -1);
273 if (board
[0] != oldb
[0]) { /* fix red men on bar */
274 fixpos(oldb
[0], board
[0], 13, 25, -1);
277 for (i
= 6; i
> 0; i
--) /* fix positions 6-1 */
278 if (board
[i
] != oldb
[i
]) {
279 fixpos(oldb
[i
], board
[i
], 13, 29 + (6 - i
) * 4, -1);
282 i
= -(off
[0] < 0 ? off
[0] + 15 : off
[0]); /* fix white's home */
284 fixpos(oldw
, i
, 13, 54, -1);
287 for (i
= 13; i
< 19; i
++) /* fix positions 13-18 */
288 if (board
[i
] != oldb
[i
]) {
289 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];
296 for (i
= 19; i
< 25; i
++) /* fix positions 19-24 */
297 if (board
[i
] != oldb
[i
]) {
298 fixpos(oldb
[i
], board
[i
], 3, 29 + (i
- 19) * 4, 1);
301 i
= (off
[1] < 0 ? off
[1] + 15 : off
[1]); /* fix red's home */
303 fixpos(oldr
, i
, 3, 54, 1);
306 curmove(r
, c
); /* return to saved position */
312 fixpos(int cur
, int new, int r
, int c
, int inc
)
319 if (cur
* new >= 0) {
322 col
= (cur
+ new > 0 ? 'r' : 'w');
329 nc
= c
< 54 ? c
: c
+ 1;
331 nc
= c
< 54 ? c
+ 1 : c
;
333 fixcol(r
+ inc
* (nv
- n
* 5), nc
,
334 abs(ov
- nv
), ' ', inc
);
336 fixcol(r
+ inc
* (ov
- o
* 5), nc
,
337 abs(ov
- nv
), col
, inc
);
343 fixcol(r
, c
, abs(nv
- 5), col
,
350 fixcol(r
, c
, abs(ov
- 5), ' ',
361 fixcol(r
+ inc
* (ov
- 5), c
,
362 abs(ov
- 10), col
, inc
);
363 fixcol(r
, c
+ 2, abs(nv
- 10), col
,
367 fixcol(r
+ inc
* (nv
- 5), c
,
368 abs(nv
- 10), ' ', inc
);
369 fixcol(r
, c
+ 2, abs(ov
- 10), ' ',
375 fixcol(r
+ inc
* (ov
% 5), c
+ o
,
376 abs(5 * n
- ov
), col
, inc
);
378 fixcol(r
, c
+ n
, abs(5 * n
- nv
),
381 fixcol(r
+ inc
* (nv
% 5), c
+ n
,
382 abs(5 * n
- nv
), ' ', inc
);
384 fixcol(r
, c
+ o
, abs(5 * o
- ov
),
391 fixcol(r
, c
+ 1, nv
, new > 0 ? 'r' : 'w', inc
);
392 if (abs(cur
) <= abs(new))
394 fixcol(r
+ inc
* new, c
+ 1, abs(cur
+ new), ' ', inc
);
398 fixcol(int r
, int c
, int l
, int ch
, int inc
)
404 for (i
= 1; i
< l
; i
++) {
405 curmove(curr
+ inc
, curc
- 1);
411 curmove(int r
, int c
)
413 if (curr
== r
&& curc
== c
)
426 int r
; /* destination row */
427 int c
; /* destination column */
428 int mode
= -1; /* mode of movement */
430 int ccount
= 1000; /* character count */
432 int n
; /* temporary variable */
433 char *m
; /* string containing CM movement */
437 if (realr
== -1) /* see if already there */
440 r
= curr
; /* set current and dest. positions */
445 /* double check position */
446 if (curr
== r
&& curc
== c
) {
450 if (CM
) { /* try CM to get there */
452 m
= (char *) tgoto(CM
, c
, r
);
455 /* try HO and local movement */
456 if (HO
&& (n
= r
+ c
* lND
+ lHO
) < ccount
) {
460 /* try various LF combinations */
463 if ((n
= (r
- curr
) + c
* lND
+ 1) < ccount
) {
468 if (c
>= curc
&& (n
= (r
- curr
) + (c
- curc
) * lND
) < ccount
) {
473 if (c
< curc
&& (n
= (r
- curr
) + (curc
- c
) * lBC
) < ccount
) {
478 /* try corresponding UP combinations */
481 if ((n
= (curr
- r
) * lUP
+ c
* lND
+ 1) < ccount
) {
487 (n
= (curr
- r
) * lUP
+ (c
- curc
) * lND
) < ccount
) {
493 (n
= (curr
- r
) * lUP
+ (curc
- c
) * lBC
) < ccount
) {
499 if (curr
== r
&& c
> curc
&& linect
[r
] < curc
&& c
- curc
< ccount
)
504 case -1: /* error! */
505 write(2, "\r\nInternal cursor error.\r\n", 26);
508 /* direct cursor motion */
510 tputs(m
, abs(curr
- r
), addbuf
);
513 /* relative to "home" */
515 tputs(HO
, r
, addbuf
);
516 for (i
= 0; i
< r
; i
++)
518 for (i
= 0; i
< c
; i
++)
519 tputs(ND
, 1, addbuf
);
522 /* CR and down and over */
525 for (i
= 0; i
< r
- curr
; i
++)
527 for (i
= 0; i
< c
; i
++)
528 tputs(ND
, 1, addbuf
);
533 for (i
= 0; i
< r
- curr
; i
++)
535 for (i
= 0; i
< c
- curc
; i
++)
536 tputs(ND
, 1, addbuf
);
541 for (i
= 0; i
< r
- curr
; i
++)
543 for (i
= 0; i
< curc
- c
; i
++)
547 /* CR and up and over */
550 for (i
= 0; i
< curr
- r
; i
++)
551 tputs(UP
, 1, addbuf
);
552 for (i
= 0; i
< c
; i
++)
553 tputs(ND
, 1, addbuf
);
558 for (i
= 0; i
< curr
- r
; i
++)
559 tputs(UP
, 1, addbuf
);
560 for (i
= 0; i
< c
- curc
; i
++)
561 tputs(ND
, 1, addbuf
);
566 for (i
= 0; i
< curr
- r
; i
++)
567 tputs(UP
, 1, addbuf
);
568 for (i
= 0; i
< curc
- c
; i
++) {
570 tputs(BC
, 1, addbuf
);
578 for (i
= 0; i
< c
- curc
; i
++)
594 /* double space if can't clear */
599 curr
= curc
= 0; /* fix position markers */
601 for (i
= 0; i
< 24; i
++)/* clear line counts */
603 buffnum
= -1; /* ignore leftover buffer contents */
604 tputs(CL
, CO
, addbuf
); /* put CL in buffer */
610 int sp
; /* counts spaces in a tab */
612 if (c
== '\007') { /* bells go in blindly */
616 /* process tabs, use spaces if the tab should be erasing things,
617 * otherwise use cursor movement routines. Note this does not use
618 * hardware tabs at all. */
620 sp
= (curc
+ 8) & (~7); /* compute spaces */
621 /* check line length */
622 if (linect
[curr
] >= curc
|| sp
< 4) {
623 for (; sp
> curc
; sp
--)
625 curc
= sp
; /* fix curc */
630 /* do newline be calling newline */
635 /* ignore any other control chars */
639 /* if an erasing space or non-space, just add it to buffer. Otherwise
640 * use cursor movement routine, so that multiple spaces will be
641 * grouped together */
642 if (c
> ' ' || linect
[curr
] >= curc
) {
643 newpos(); /* make sure position correct */
644 addbuf(c
); /* add character to buffer */
645 /* fix line length */
646 if (c
== ' ' && linect
[curr
] == curc
)
649 if (linect
[curr
] < curc
)
651 curc
++; /* fix curc */
653 /* use cursor movement routine */
654 curmove(curr
, curc
+ 1);
663 tputs(CD
, CO
- curr
, addbuf
);
664 for (i
= curr
; i
< LI
; i
++)
668 curmove(i
= curr
, 0);
670 while (curr
< LI
- 1) {
671 curmove(curr
+ 1, 0);
672 if (linect
[curr
] > -1)
683 if (curc
> linect
[curr
])
687 tputs(CE
, 1, addbuf
);
688 linect
[curr
] = curc
- 1;
691 while (linect
[curr
] > c
) {
696 curmove(curr
, c
+ 1);
707 curmove(curr
+ 1, 0);
711 getcaps(const char *s
)
713 char *code
; /* two letter code */
714 char ***cap
; /* pointer to cap string */
715 char *bufp
; /* pointer to cap buffer */
716 char tentry
[1024]; /* temporary uncoded caps buffer */
718 tgetent(tentry
, s
); /* get uncoded termcap entry */
720 LI
= tgetnum("li"); /* get number of lines */
723 CO
= tgetnum("co"); /* get number of columns */
727 bufp
= tbuf
; /* get padding character */
728 tgetstr("pc", &bufp
);
734 bufp
= tbuf
; /* get string entries */
736 for (code
= tcap
; *code
; code
+= 2)
737 **cap
++ = (char *) tgetstr(code
, &bufp
);
739 /* get pertinent lengths */
750 if (LI
< 24 || CO
< 72 || !(CL
&& UP
&& ND
))
752 linect
= (int *) calloc(LI
+ 1, sizeof(int));
753 if (linect
== NULL
) {
754 write(2, "\r\nOut of memory!\r\n", 18);