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