]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - backgammon/common_source/fancy.c
Add RCS identifiers.
[bsdgames-darwin.git] / backgammon / common_source / fancy.c
1 /*
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
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 $";
37 #endif /* not lint */
38
39 #include "back.h"
40
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 */
52
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) */
61
62 /* two letter codes */
63 char tcap[] = "bccdceclcmhomcmlndup";
64 /* corresponding strings */
65 char **tstr[] = { &BC, &CD, &CE, &CL, &CM, &HO, &MC, &ML, &ND, &UP };
66
67 int buffnum; /* pointer to output buffer */
68
69 char tbuf[1024]; /* buffer for decoded termcap entries */
70
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};
72
73 int oldr;
74 int oldw;
75 /* "real" cursor positions, so
76 * it knows when to reposition.
77 * These are -1 if curr and curc
78 * are accurate */
79 int realr;
80 int realc;
81
82 void addbuf();
83
84 fboard () {
85 register int i, j, l;
86
87 curmove (0,0); /* do top line */
88 for (i = 0; i < 53; i++)
89 fancyc ('_');
90
91 curmove (15,0); /* do botttom line */
92 for (i = 0; i < 53; i++)
93 fancyc ('_');
94
95 l = 1; /* do vertical lines */
96 for (i = 52; i > -1; i -= 28) {
97 curmove ( (l == 1? 1: 15) ,i);
98 fancyc ('|');
99 for (j = 0; j < 14; j++) {
100 curmove (curr+l,curc-1);
101 fancyc ('|');
102 }
103 if (i == 24)
104 i += 32;
105 l = -l; /* alternate directions */
106 }
107
108 curmove (2,1); /* label positions 13-18 */
109 for (i = 13; i < 18; i++) {
110 fancyc ('1');
111 fancyc ((i % 10)+'0');
112 curmove (curr,curc+2);
113 }
114 fancyc ('1');
115 fancyc ('8');
116
117 curmove (2,29); /* label positions 19-24 */
118 fancyc ('1');
119 fancyc ('9');
120 for (i = 20; i < 25; i++) {
121 curmove (curr,curc+2);
122 fancyc ('2');
123 fancyc ((i % 10)+'0');
124 }
125
126 curmove (14,1); /* label positions 12-7 */
127 fancyc ('1');
128 fancyc ('2');
129 for (i = 11; i > 6; i--) {
130 curmove (curr,curc+2);
131 fancyc (i > 9? '1': ' ');
132 fancyc ((i % 10)+'0');
133 }
134
135 curmove (14,30); /* label positions 6-1 */
136 fancyc ('6');
137 for (i = 5; i > 0; i--) {
138 curmove (curr,curc+3);
139 fancyc (i+'0');
140 }
141
142 for (i = 12; i > 6; i--) /* print positions 12-7 */
143 if (board[i])
144 bsect (board[i],13,1+4*(12-i),-1);
145
146 if (board[0]) /* print red men on bar */
147 bsect (board[0],13,25,-1);
148
149 for (i = 6; i > 0; i--) /* print positions 6-1 */
150 if (board[i])
151 bsect (board[i],13,29+4*(6-i),-1);
152
153 l = (off[1] < 0? off[1]+15: off[1]); /* print white's home */
154 bsect (l,3,54,1);
155
156 curmove (8,25); /* print the word BAR */
157 fancyc ('B');
158 fancyc ('A');
159 fancyc ('R');
160
161 for (i = 13; i < 19; i++) /* print positions 13-18 */
162 if (board[i])
163 bsect (board[i],3,1+4*(i-13),1);
164
165 if (board[25]) /* print white's men on bar */
166 bsect (board[25],3,25,1);
167
168 for (i = 19; i < 25; i++) /* print positions 19-24 */
169 if (board[i])
170 bsect (board[i],3,29+4*(i-19),1);
171
172 l = (off[0] < 0? off[0]+15: off[0]); /* print red's home */
173 bsect (-l,13,54,-1);
174
175 for (i = 0; i < 26; i++) /* save board position
176 * for refresh later */
177 oldb[i] = board[i];
178 oldr = (off[1] < 0? off[1]+15: off[1]);
179 oldw = -(off[0] < 0? off[0]+15: off[0]);
180 }
181 \f
182 /*
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
189 * differently.
190 */
191
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 */
197
198 {
199 register int j; /* index */
200 register int n; /* number of men on position */
201 register int bct; /* counter */
202 int k; /* index */
203 char pc; /* color of men on position */
204
205 n = abs(b); /* initialize n and pc */
206 pc = (b > 0? 'r': 'w');
207
208 if (n < 6 && cpos < 54) /* position cursor at start */
209 curmove (rpos,cpos+1);
210 else
211 curmove (rpos,cpos);
212
213 for (j = 0; j < 5; j++) { /* print position row by row */
214
215 for (k = 0; k < 15; k += 5) /* print men */
216 if (n > j+k)
217 fancyc (pc);
218
219 if (j < 4) { /* figure how far to
220 * back up for next
221 * row */
222 if (n < 6) { /* stop if none left */
223 if (j+1 == n)
224 break;
225 bct = 1; /* single column */
226 } else {
227 if (n < 11) { /* two columns */
228 if (cpos == 54) { /* home pos */
229 if (j+5 >= n)
230 bct = 1;
231 else
232 bct = 2;
233 }
234 if (cpos < 54) { /* not home */
235 if (j+6 >= n)
236 bct = 1;
237 else
238 bct = 2;
239 }
240 } else { /* three columns */
241 if (j+10 >= n)
242 bct = 2;
243 else
244 bct = 3;
245 }
246 }
247 curmove (curr+cnext,curc-bct); /* reposition cursor */
248 }
249 }
250 }
251 \f
252 refresh() {
253 register int i, r, c;
254
255 r = curr; /* save current position */
256 c = curc;
257
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);
261 oldb[i] = board[i];
262 }
263
264 if (board[0] != oldb[0]) { /* fix red men on bar */
265 fixpos (oldb[0],board[0],13,25,-1);
266 oldb[0] = board[0];
267 }
268
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);
272 oldb[i] = board[i];
273 }
274
275 i = -(off[0] < 0? off[0]+15: off[0]); /* fix white's home */
276 if (oldw != i) {
277 fixpos (oldw,i,13,54,-1);
278 oldw = i;
279 }
280
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);
284 oldb[i] = board[i];
285 }
286
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];
290 }
291
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);
295 oldb[i] = board[i];
296 }
297
298 i = (off[1] < 0? off[1]+15: off[1]); /* fix red's home */
299 if (oldr != i) {
300 fixpos (oldr,i,3,54,1);
301 oldr = i;
302 }
303
304 curmove (r,c); /* return to saved position */
305 newpos();
306 buflush();
307 }
308 \f
309 fixpos (old,new,r,c,inc)
310 int old, new, r, c, inc;
311
312 {
313 register int o, n, nv;
314 int ov, nc;
315 char col;
316
317 if (old*new >= 0) {
318 ov = abs(old);
319 nv = abs(new);
320 col = (old+new > 0? 'r': 'w');
321 o = (ov-1)/5;
322 n = (nv-1)/5;
323 if (o == n) {
324 if (o == 2)
325 nc = c+2;
326 if (o == 1)
327 nc = c < 54? c: c+1;
328 if (o == 0)
329 nc = c < 54? c+1: c;
330 if (ov > nv)
331 fixcol (r+inc*(nv-n*5),nc,abs(ov-nv),' ',inc);
332 else
333 fixcol (r+inc*(ov-o*5),nc,abs(ov-nv),col,inc);
334 return;
335 } else {
336 if (c < 54) {
337 if (o+n == 1) {
338 if (n) {
339 fixcol (r,c,abs(nv-5),col,inc);
340 if (ov != 5)
341 fixcol (r+inc*ov,c+1,abs(ov-5),col,inc);
342 } else {
343 fixcol (r,c,abs(ov-5),' ',inc);
344 if (nv != 5)
345 fixcol (r+inc*nv,c+1,abs(nv-5),' ',inc);
346 }
347 return;
348 }
349 if (n == 2) {
350 if (ov != 10)
351 fixcol (r+inc*(ov-5),c,abs(ov-10),col,inc);
352 fixcol (r,c+2,abs(nv-10),col,inc);
353 } else {
354 if (nv != 10)
355 fixcol (r+inc*(nv-5),c,abs(nv-10),' ',inc);
356 fixcol (r,c+2,abs(ov-10),' ',inc);
357 }
358 return;
359 }
360 if (n > o) {
361 fixcol (r+inc*(ov%5),c+o,abs(5*n-ov),col,inc);
362 if (nv != 5*n)
363 fixcol (r,c+n,abs(5*n-nv),col,inc);
364 } else {
365 fixcol (r+inc*(nv%5),c+n,abs(5*n-nv),' ',inc);
366 if (ov != 5*o)
367 fixcol (r,c+o,abs(5*o-ov),' ',inc);
368 }
369 return;
370 }
371 }
372 nv = abs(new);
373 fixcol (r,c+1,nv,new > 0? 'r': 'w',inc);
374 if (abs(old) <= abs(new))
375 return;
376 fixcol (r+inc*new,c+1,abs(old+new),' ',inc);
377 }
378
379 fixcol (r,c,l,ch,inc)
380 register int l, ch;
381 int r, c, inc;
382
383 {
384 register int i;
385
386 curmove (r,c);
387 fancyc (ch);
388 for (i = 1; i < l; i++) {
389 curmove (curr+inc,curc-1);
390 fancyc (ch);
391 }
392 }
393 \f
394 curmove (r,c)
395 register int r, c;
396
397 {
398 if (curr == r && curc == c)
399 return;
400 if (realr == -1) {
401 realr = curr;
402 realc = curc;
403 }
404 curr = r;
405 curc = c;
406 }
407
408 newpos () {
409 register int r; /* destination row */
410 register int c; /* destination column */
411 register int mode = -1; /* mode of movement */
412
413 int count = 1000; /* character count */
414 int i; /* index */
415 int j; /* index */
416 int n; /* temporary variable */
417 char *m; /* string containing CM movement */
418
419
420 if (realr == -1) /* see if already there */
421 return;
422
423 r = curr; /* set current and dest. positions */
424 c = curc;
425 curr = realr;
426 curc = realc;
427
428 /* double check position */
429 if (curr == r && curc == c) {
430 realr = realc = -1;
431 return;
432 }
433
434 if (CM) { /* try CM to get there */
435 mode = 0;
436 m = (char *)tgoto (CM,c,r);
437 count = strlen (m);
438 }
439
440 /* try HO and local movement */
441 if (HO && (n = r+c*lND+lHO) < count) {
442 mode = 1;
443 count = n;
444 }
445
446 /* try various LF combinations */
447 if (r >= curr) {
448 /* CR, LF, and ND */
449 if ((n = (r-curr)+c*lND+1) < count) {
450 mode = 2;
451 count = n;
452 }
453 /* LF, ND */
454 if (c >= curc && (n = (r-curr)+(c-curc)*lND) < count) {
455 mode = 3;
456 count = n;
457 }
458 /* LF, BS */
459 if (c < curc && (n = (r-curr)+(curc-c)*lBC) < count) {
460 mode = 4;
461 count = n;
462 }
463 }
464
465 /* try corresponding UP combinations */
466 if (r < curr) {
467 /* CR, UP, and ND */
468 if ((n = (curr-r)*lUP+c*lND+1) < count) {
469 mode = 5;
470 count = n;
471 }
472 /* UP and ND */
473 if (c >= curc && (n = (curr-r)*lUP+(c-curc)*lND) < count) {
474 mode = 6;
475 count = n;
476 }
477 /* UP and BS */
478 if (c < curc && (n = (curr-r)*lUP+(curc-c)*lBC) < count) {
479 mode = 7;
480 count = n;
481 }
482 }
483
484 /* space over */
485 if (curr == r && c > curc && linect[r] < curc && c-curc < count)
486 mode = 8;
487
488 switch (mode) {
489
490 case -1: /* error! */
491 write (2,"\r\nInternal cursor error.\r\n",26);
492 getout();
493
494 /* direct cursor motion */
495 case 0:
496 tputs (m,abs(curr-r),addbuf);
497 break;
498
499 /* relative to "home" */
500 case 1:
501 tputs (HO,r,addbuf);
502 for (i = 0; i < r; i++)
503 addbuf ('\012');
504 for (i = 0; i < c; i++)
505 tputs (ND,1,addbuf);
506 break;
507
508 /* CR and down and over */
509 case 2:
510 addbuf ('\015');
511 for (i = 0; i < r-curr; i++)
512 addbuf ('\012');
513 for (i = 0; i < c; i++)
514 tputs (ND,1,addbuf);
515 break;
516
517 /* down and over */
518 case 3:
519 for (i = 0; i < r-curr; i++)
520 addbuf ('\012');
521 for (i = 0; i < c-curc; i++)
522 tputs (ND,1,addbuf);
523 break;
524
525 /* down and back */
526 case 4:
527 for (i = 0; i < r-curr; i++)
528 addbuf ('\012');
529 for (i = 0; i < curc-c; i++)
530 addbuf ('\010');
531 break;
532
533 /* CR and up and over */
534 case 5:
535 addbuf ('\015');
536 for (i = 0; i < curr-r; i++)
537 tputs (UP,1,addbuf);
538 for (i = 0; i < c; i++)
539 tputs (ND,1,addbuf);
540 break;
541
542 /* up and over */
543 case 6:
544 for (i = 0; i < curr-r; i++)
545 tputs (UP,1,addbuf);
546 for (i = 0; i < c-curc; i++)
547 tputs (ND,1,addbuf);
548 break;
549
550 /* up and back */
551 case 7:
552 for (i = 0; i < curr-r; i++)
553 tputs (UP,1,addbuf);
554 for (i = 0; i < curc-c; i++) {
555 if (BC)
556 tputs (BC,1,addbuf);
557 else
558 addbuf ('\010');
559 }
560 break;
561
562 /* safe space */
563 case 8:
564 for (i = 0; i < c-curc; i++)
565 addbuf (' ');
566 }
567
568 /* fix positions */
569 curr = r;
570 curc = c;
571 realr = -1;
572 realc = -1;
573 }
574 \f
575 clear () {
576 register int i;
577
578 /* double space if can't clear */
579 if (CL == 0) {
580 writel ("\n\n");
581 return;
582 }
583
584 curr = curc = 0; /* fix position markers */
585 realr = realc = -1;
586 for (i = 0; i < 24; i++) /* clear line counts */
587 linect[i] = -1;
588 buffnum = -1; /* ignore leftover buffer contents */
589 tputs (CL,CO,addbuf); /* put CL in buffer */
590 }
591
592 tos () { /* home cursor */
593 curmove (0,0);
594 }
595 \f
596 fancyc (c)
597 register char c; /* character to output */
598 {
599 register int sp; /* counts spaces in a tab */
600
601 if (c == '\007') { /* bells go in blindly */
602 addbuf (c);
603 return;
604 }
605
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. */
611 if (c == '\t') {
612 sp = (curc+8) & (~ 7); /* compute spaces */
613 /* check line length */
614 if (linect[curr] >= curc || sp < 4) {
615 for (; sp > curc; sp--)
616 addbuf (' ');
617 curc = sp; /* fix curc */
618 } else
619 curmove (curr,sp);
620 return;
621 }
622
623 /* do newline be calling newline */
624 if (c == '\n') {
625 newline();
626 return;
627 }
628
629 /* ignore any other control chars */
630 if (c < ' ')
631 return;
632
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
637 * together */
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)
643 linect[curr]--;
644 else if (linect[curr] < curc)
645 linect[curr] = curc;
646 curc++; /* fix curc */
647 } else
648 /* use cursor movement routine */
649 curmove (curr,curc+1);
650 }
651 \f
652 clend() {
653 register int i;
654 register char *s;
655
656
657 if (CD) {
658 tputs (CD,CO-curr,addbuf);
659 for (i = curr; i < LI; i++)
660 linect[i] = -1;
661 return;
662 }
663
664 curmove (i = curr,0);
665 cline();
666 while (curr < LI-1) {
667 curmove (curr+1,0);
668 if (linect[curr] > -1)
669 cline ();
670 }
671 curmove (i,0);
672 }
673
674 cline () {
675 register int i;
676 register int c;
677 register char *s;
678
679 if (curc > linect[curr])
680 return;
681 newpos ();
682 if (CE) {
683 tputs (CE,1,addbuf);
684 linect[curr] = curc-1;
685 } else {
686 c = curc-1;
687 while (linect[curr] > c) {
688 addbuf (' ');
689 curc++;
690 linect[curr]--;
691 }
692 curmove (curr,c+1);
693 }
694 }
695
696 newline () {
697 cline();
698 if (curr == LI-1)
699 curmove (begscr,0);
700 else
701 curmove (curr+1,0);
702 }
703 \f
704 getcaps (s)
705 register char *s;
706
707 {
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 */
712
713 tgetent (tentry,s); /* get uncoded termcap entry */
714
715 LI = tgetnum ("li"); /* get number of lines */
716 if (LI == -1)
717 LI = 12;
718 CO = tgetnum ("co"); /* get number of columns */
719 if (CO == -1)
720 CO = 65;
721
722 bufp = tbuf; /* get padding character */
723 tgetstr ("pc",&bufp);
724 if (bufp != tbuf)
725 PC = *tbuf;
726 else
727 PC = 0;
728
729 bufp = tbuf; /* get string entries */
730 cap = tstr;
731 for (code = tcap; *code; code += 2)
732 **cap++ = (char *)tgetstr (code,&bufp);
733
734 /* get pertinent lengths */
735 if (HO)
736 lHO = strlen (HO);
737 if (BC)
738 lBC = strlen (BC);
739 else
740 lBC = 1;
741 if (UP)
742 lUP = strlen (UP);
743 if (ND)
744 lND = strlen (ND);
745 if (LI < 24 || CO < 72 || !(CL && UP && ND))
746 return (0);
747 linect = (int *)calloc (LI+1,sizeof(int));
748 return (1);
749 }