]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - backgammon/common_source/fancy.c
f9f99258803f5094f850974072d7b2f9ac66b35a
[bsdgames-darwin.git] / backgammon / common_source / fancy.c
1 /* $NetBSD: fancy.c,v 1.11 2003/08/07 09:36:57 agc 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.11 2003/08/07 09:36:57 agc Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include "back.h"
42
43 char PC; /* padding character */
44 char *BC; /* backspace sequence */
45 char *CD; /* clear to end of screen sequence */
46 char *CE; /* clear to end of line sequence */
47 char *CL; /* clear screen sequence */
48 char *CM; /* cursor movement instructions */
49 char *HO; /* home cursor sequence */
50 char *MC; /* column cursor movement map */
51 char *ML; /* row cursor movement map */
52 char *ND; /* forward cursor sequence */
53 char *UP; /* up cursor sequence */
54
55 int lHO; /* length of HO */
56 int lBC; /* length of BC */
57 int lND; /* length of ND */
58 int lUP; /* length of UP */
59 int CO; /* number of columns */
60 int LI; /* number of lines */
61 int *linect; /* array of lengths of lines on screen (the
62 * actual screen is not stored) */
63
64 /* two letter codes */
65 char tcap[] = "bccdceclcmhomcmlndup";
66 /* corresponding strings */
67 char **tstr[] = {&BC, &CD, &CE, &CL, &CM, &HO, &MC, &ML, &ND, &UP};
68
69 int buffnum; /* pointer to output buffer */
70
71 char tbuf[1024]; /* buffer for decoded termcap entries */
72
73 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};
74
75 int oldr;
76 int oldw;
77 /* "real" cursor positions, so it knows when to reposition. These are -1 if
78 * curr and curc are accurate */
79 int realr;
80 int realc;
81
82 void
83 fboard()
84 {
85 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 for refresh later */
176 oldb[i] = board[i];
177 oldr = (off[1] < 0 ? off[1] + 15 : off[1]);
178 oldw = -(off[0] < 0 ? off[0] + 15 : off[0]);
179 }
180 /*
181 * bsect (b,rpos,cpos,cnext)
182 * Print the contents of a board position. "b" has the value of the
183 * position, "rpos" is the row to start printing, "cpos" is the column to
184 * start printing, and "cnext" is positive if the position starts at the top
185 * and negative if it starts at the bottom. The value of "cpos" is checked
186 * to see if the position is a player's home, since those are printed
187 * differently.
188 */
189 void
190 bsect(b, rpos, cpos, cnext)
191 int b; /* contents of position */
192 int rpos; /* row of position */
193 int cpos; /* column of position */
194 int cnext; /* direction of position */
195 {
196 int j; /* index */
197 int n; /* number of men on position */
198 int bct; /* counter */
199 int k; /* index */
200 char pc; /* color of men on position */
201
202 bct = 0;
203 n = abs(b); /* initialize n and pc */
204 pc = (b > 0 ? 'r' : 'w');
205
206 if (n < 6 && cpos < 54) /* position cursor at start */
207 curmove(rpos, cpos + 1);
208 else
209 curmove(rpos, cpos);
210
211 for (j = 0; j < 5; j++) { /* print position row by row */
212
213 for (k = 0; k < 15; k += 5) /* print men */
214 if (n > j + k)
215 fancyc(pc);
216
217 if (j < 4) { /* figure how far to back up for next row */
218 if (n < 6) { /* stop if none left */
219 if (j + 1 == n)
220 break;
221 bct = 1; /* single column */
222 } else {
223 if (n < 11) { /* two columns */
224 if (cpos == 54) { /* home pos */
225 if (j + 5 >= n)
226 bct = 1;
227 else
228 bct = 2;
229 }
230 if (cpos < 54) { /* not home */
231 if (j + 6 >= n)
232 bct = 1;
233 else
234 bct = 2;
235 }
236 } else { /* three columns */
237 if (j + 10 >= n)
238 bct = 2;
239 else
240 bct = 3;
241 }
242 }
243 curmove(curr + cnext, curc - bct); /* reposition cursor */
244 }
245 }
246 }
247
248 void
249 refresh()
250 {
251 int i, r, c;
252
253 r = curr; /* save current position */
254 c = curc;
255
256 for (i = 12; i > 6; i--)/* fix positions 12-7 */
257 if (board[i] != oldb[i]) {
258 fixpos(oldb[i], board[i], 13, 1 + (12 - i) * 4, -1);
259 oldb[i] = board[i];
260 }
261 if (board[0] != oldb[0]) { /* fix red men on bar */
262 fixpos(oldb[0], board[0], 13, 25, -1);
263 oldb[0] = board[0];
264 }
265 for (i = 6; i > 0; i--) /* fix positions 6-1 */
266 if (board[i] != oldb[i]) {
267 fixpos(oldb[i], board[i], 13, 29 + (6 - i) * 4, -1);
268 oldb[i] = board[i];
269 }
270 i = -(off[0] < 0 ? off[0] + 15 : off[0]); /* fix white's home */
271 if (oldw != i) {
272 fixpos(oldw, i, 13, 54, -1);
273 oldw = i;
274 }
275 for (i = 13; i < 19; i++) /* fix positions 13-18 */
276 if (board[i] != oldb[i]) {
277 fixpos(oldb[i], board[i], 3, 1 + (i - 13) * 4, 1);
278 oldb[i] = board[i];
279 }
280 if (board[25] != oldb[25]) { /* fix white men on bar */
281 fixpos(oldb[25], board[25], 3, 25, 1);
282 oldb[25] = board[25];
283 }
284 for (i = 19; i < 25; i++) /* fix positions 19-24 */
285 if (board[i] != oldb[i]) {
286 fixpos(oldb[i], board[i], 3, 29 + (i - 19) * 4, 1);
287 oldb[i] = board[i];
288 }
289 i = (off[1] < 0 ? off[1] + 15 : off[1]); /* fix red's home */
290 if (oldr != i) {
291 fixpos(oldr, i, 3, 54, 1);
292 oldr = i;
293 }
294 curmove(r, c); /* return to saved position */
295 newpos();
296 buflush();
297 }
298
299 void
300 fixpos(old, new, r, c, inc)
301 int old, new, r, c, inc;
302 {
303 int o, n, nv;
304 int ov, nc;
305 char col;
306
307 nc = 0;
308 if (old * new >= 0) {
309 ov = abs(old);
310 nv = abs(new);
311 col = (old + new > 0 ? 'r' : 'w');
312 o = (ov - 1) / 5;
313 n = (nv - 1) / 5;
314 if (o == n) {
315 if (o == 2)
316 nc = c + 2;
317 if (o == 1)
318 nc = c < 54 ? c : c + 1;
319 if (o == 0)
320 nc = c < 54 ? c + 1 : c;
321 if (ov > nv)
322 fixcol(r + inc * (nv - n * 5), nc, abs(ov - nv), ' ', inc);
323 else
324 fixcol(r + inc * (ov - o * 5), nc, abs(ov - nv), col, inc);
325 return;
326 } else {
327 if (c < 54) {
328 if (o + n == 1) {
329 if (n) {
330 fixcol(r, c, abs(nv - 5), col, inc);
331 if (ov != 5)
332 fixcol(r + inc * ov, c + 1,
333 abs(ov - 5), col, inc);
334 } else {
335 fixcol(r, c, abs(ov - 5), ' ', inc);
336 if (nv != 5)
337 fixcol(r + inc * nv, c + 1,
338 abs(nv - 5), ' ', inc);
339 }
340 return;
341 }
342 if (n == 2) {
343 if (ov != 10)
344 fixcol(r + inc * (ov - 5), c,
345 abs(ov - 10), col, inc);
346 fixcol(r, c + 2, abs(nv - 10), col, inc);
347 } else {
348 if (nv != 10)
349 fixcol(r + inc * (nv - 5), c,
350 abs(nv - 10), ' ', inc);
351 fixcol(r, c + 2, abs(ov - 10), ' ', inc);
352 }
353 return;
354 }
355 if (n > o) {
356 fixcol(r + inc * (ov % 5), c + o, abs(5 * n - ov), col, inc);
357 if (nv != 5 * n)
358 fixcol(r, c + n, abs(5 * n - nv), col, inc);
359 } else {
360 fixcol(r + inc * (nv % 5), c + n, abs(5 * n - nv), ' ', inc);
361 if (ov != 5 * o)
362 fixcol(r, c + o, abs(5 * o - ov), ' ', inc);
363 }
364 return;
365 }
366 }
367 nv = abs(new);
368 fixcol(r, c + 1, nv, new > 0 ? 'r' : 'w', inc);
369 if (abs(old) <= abs(new))
370 return;
371 fixcol(r + inc * new, c + 1, abs(old + new), ' ', inc);
372 }
373
374 void
375 fixcol(r, c, l, ch, inc)
376 int l, ch, r, c, inc;
377 {
378 int i;
379
380 curmove(r, c);
381 fancyc(ch);
382 for (i = 1; i < l; i++) {
383 curmove(curr + inc, curc - 1);
384 fancyc(ch);
385 }
386 }
387
388 void
389 curmove(r, c)
390 int r, c;
391 {
392 if (curr == r && curc == c)
393 return;
394 if (realr == -1) {
395 realr = curr;
396 realc = curc;
397 }
398 curr = r;
399 curc = c;
400 }
401
402 void
403 newpos()
404 {
405 int r; /* destination row */
406 int c; /* destination column */
407 int mode = -1; /* mode of movement */
408
409 int count = 1000; /* character count */
410 int i; /* index */
411 int n; /* temporary variable */
412 char *m; /* string containing CM movement */
413
414
415 m = NULL;
416 if (realr == -1) /* see if already there */
417 return;
418
419 r = curr; /* set current and dest. positions */
420 c = curc;
421 curr = realr;
422 curc = realc;
423
424 /* double check position */
425 if (curr == r && curc == c) {
426 realr = realc = -1;
427 return;
428 }
429 if (CM) { /* try CM to get there */
430 mode = 0;
431 m = (char *) tgoto(CM, c, r);
432 count = strlen(m);
433 }
434 /* try HO and local movement */
435 if (HO && (n = r + c * lND + lHO) < count) {
436 mode = 1;
437 count = n;
438 }
439 /* try various LF combinations */
440 if (r >= curr) {
441 /* CR, LF, and ND */
442 if ((n = (r - curr) + c * lND + 1) < count) {
443 mode = 2;
444 count = n;
445 }
446 /* LF, ND */
447 if (c >= curc && (n = (r - curr) + (c - curc) * lND) < count) {
448 mode = 3;
449 count = n;
450 }
451 /* LF, BS */
452 if (c < curc && (n = (r - curr) + (curc - c) * lBC) < count) {
453 mode = 4;
454 count = n;
455 }
456 }
457 /* try corresponding UP combinations */
458 if (r < curr) {
459 /* CR, UP, and ND */
460 if ((n = (curr - r) * lUP + c * lND + 1) < count) {
461 mode = 5;
462 count = n;
463 }
464 /* UP and ND */
465 if (c >= curc && (n = (curr - r) * lUP + (c - curc) * lND) < count) {
466 mode = 6;
467 count = n;
468 }
469 /* UP and BS */
470 if (c < curc && (n = (curr - r) * lUP + (curc - c) * lBC) < count) {
471 mode = 7;
472 count = n;
473 }
474 }
475 /* space over */
476 if (curr == r && c > curc && linect[r] < curc && c - curc < count)
477 mode = 8;
478
479 switch (mode) {
480
481 case -1: /* error! */
482 write(2, "\r\nInternal cursor error.\r\n", 26);
483 getout(0);
484
485 /* direct cursor motion */
486 case 0:
487 tputs(m, abs(curr - r), addbuf);
488 break;
489
490 /* relative to "home" */
491 case 1:
492 tputs(HO, r, addbuf);
493 for (i = 0; i < r; i++)
494 addbuf('\012');
495 for (i = 0; i < c; i++)
496 tputs(ND, 1, addbuf);
497 break;
498
499 /* CR and down and over */
500 case 2:
501 addbuf('\015');
502 for (i = 0; i < r - curr; i++)
503 addbuf('\012');
504 for (i = 0; i < c; i++)
505 tputs(ND, 1, addbuf);
506 break;
507
508 /* down and over */
509 case 3:
510 for (i = 0; i < r - curr; i++)
511 addbuf('\012');
512 for (i = 0; i < c - curc; i++)
513 tputs(ND, 1, addbuf);
514 break;
515
516 /* down and back */
517 case 4:
518 for (i = 0; i < r - curr; i++)
519 addbuf('\012');
520 for (i = 0; i < curc - c; i++)
521 addbuf('\010');
522 break;
523
524 /* CR and up and over */
525 case 5:
526 addbuf('\015');
527 for (i = 0; i < curr - r; i++)
528 tputs(UP, 1, addbuf);
529 for (i = 0; i < c; i++)
530 tputs(ND, 1, addbuf);
531 break;
532
533 /* up and over */
534 case 6:
535 for (i = 0; i < curr - r; i++)
536 tputs(UP, 1, addbuf);
537 for (i = 0; i < c - curc; i++)
538 tputs(ND, 1, addbuf);
539 break;
540
541 /* up and back */
542 case 7:
543 for (i = 0; i < curr - r; i++)
544 tputs(UP, 1, addbuf);
545 for (i = 0; i < curc - c; i++) {
546 if (BC)
547 tputs(BC, 1, addbuf);
548 else
549 addbuf('\010');
550 }
551 break;
552
553 /* safe space */
554 case 8:
555 for (i = 0; i < c - curc; i++)
556 addbuf(' ');
557 }
558
559 /* fix positions */
560 curr = r;
561 curc = c;
562 realr = -1;
563 realc = -1;
564 }
565
566 void
567 clear()
568 {
569 int i;
570
571 /* double space if can't clear */
572 if (CL == 0) {
573 writel("\n\n");
574 return;
575 }
576 curr = curc = 0; /* fix position markers */
577 realr = realc = -1;
578 for (i = 0; i < 24; i++)/* clear line counts */
579 linect[i] = -1;
580 buffnum = -1; /* ignore leftover buffer contents */
581 tputs(CL, CO, addbuf); /* put CL in buffer */
582 }
583
584 void
585 fancyc(c)
586 char c; /* character to output */
587 {
588 int sp; /* counts spaces in a tab */
589
590 if (c == '\007') { /* bells go in blindly */
591 addbuf(c);
592 return;
593 }
594 /* process tabs, use spaces if the the tab should be erasing things,
595 * otherwise use cursor movement routines. Note this does not use
596 * hardware tabs at all. */
597 if (c == '\t') {
598 sp = (curc + 8) & (~7); /* compute spaces */
599 /* check line length */
600 if (linect[curr] >= curc || sp < 4) {
601 for (; sp > curc; sp--)
602 addbuf(' ');
603 curc = sp; /* fix curc */
604 } else
605 curmove(curr, sp);
606 return;
607 }
608 /* do newline be calling newline */
609 if (c == '\n') {
610 newline();
611 return;
612 }
613 /* ignore any other control chars */
614 if (c < ' ')
615 return;
616
617 /* if an erasing space or non-space, just add it to buffer. Otherwise
618 * use cursor movement routine, so that multiple spaces will be
619 * grouped together */
620 if (c > ' ' || linect[curr] >= curc) {
621 newpos(); /* make sure position correct */
622 addbuf(c); /* add character to buffer */
623 /* fix line length */
624 if (c == ' ' && linect[curr] == curc)
625 linect[curr]--;
626 else
627 if (linect[curr] < curc)
628 linect[curr] = curc;
629 curc++; /* fix curc */
630 } else
631 /* use cursor movement routine */
632 curmove(curr, curc + 1);
633 }
634
635 void
636 clend()
637 {
638 int i;
639
640 if (CD) {
641 tputs(CD, CO - curr, addbuf);
642 for (i = curr; i < LI; i++)
643 linect[i] = -1;
644 return;
645 }
646 curmove(i = curr, 0);
647 cline();
648 while (curr < LI - 1) {
649 curmove(curr + 1, 0);
650 if (linect[curr] > -1)
651 cline();
652 }
653 curmove(i, 0);
654 }
655
656 void
657 cline()
658 {
659 int c;
660
661 if (curc > linect[curr])
662 return;
663 newpos();
664 if (CE) {
665 tputs(CE, 1, addbuf);
666 linect[curr] = curc - 1;
667 } else {
668 c = curc - 1;
669 while (linect[curr] > c) {
670 addbuf(' ');
671 curc++;
672 linect[curr]--;
673 }
674 curmove(curr, c + 1);
675 }
676 }
677
678 void
679 newline()
680 {
681 cline();
682 if (curr == LI - 1)
683 curmove(begscr, 0);
684 else
685 curmove(curr + 1, 0);
686 }
687
688 int
689 getcaps(s)
690 const char *s;
691 {
692 char *code; /* two letter code */
693 char ***cap; /* pointer to cap string */
694 char *bufp; /* pointer to cap buffer */
695 char tentry[1024]; /* temporary uncoded caps buffer */
696
697 tgetent(tentry, s); /* get uncoded termcap entry */
698
699 LI = tgetnum("li"); /* get number of lines */
700 if (LI == -1)
701 LI = 12;
702 CO = tgetnum("co"); /* get number of columns */
703 if (CO == -1)
704 CO = 65;
705
706 bufp = tbuf; /* get padding character */
707 tgetstr("pc", &bufp);
708 if (bufp != tbuf)
709 PC = *tbuf;
710 else
711 PC = 0;
712
713 bufp = tbuf; /* get string entries */
714 cap = tstr;
715 for (code = tcap; *code; code += 2)
716 **cap++ = (char *) tgetstr(code, &bufp);
717
718 /* get pertinent lengths */
719 if (HO)
720 lHO = strlen(HO);
721 if (BC)
722 lBC = strlen(BC);
723 else
724 lBC = 1;
725 if (UP)
726 lUP = strlen(UP);
727 if (ND)
728 lND = strlen(ND);
729 if (LI < 24 || CO < 72 || !(CL && UP && ND))
730 return (0);
731 linect = (int *) calloc(LI + 1, sizeof(int));
732 if (linect == NULL) {
733 write(2, "\r\nOut of memory!\r\n", 18);
734 getout(0);
735 }
736 return (1);
737 }