Home | History | Annotate | Line # | Download | only in PSD.doc
      1 /*	$NetBSD: twinkle2.c,v 1.6 2005/05/23 04:04:49 christos Exp $	*/
      2 
      3 /*
      4  *
      5  *  Copyright (c) 1980, 1993
      6  * 	 The Regents of the University of California.  All rights reserved.
      7  *
      8  *  Redistribution and use in source and binary forms, with or without
      9  *  modification, are permitted provided that the following conditions
     10  *  are met:
     11  *  1. Redistributions of source code must retain the above copyright
     12  *     notice, this list of conditions and the following disclaimer.
     13  *  2. Redistributions in binary form must reproduce the above copyright
     14  *     notice, this list of conditions and the following disclaimer in the
     15  *     documentation and/or other materials provided with the distribution.
     16  *  3. Neither the name of the University nor the names of its contributors
     17  *     may be used to endorse or promote products derived from this software
     18  *     without specific prior written permission.
     19  *
     20  *  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     21  *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  *  ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     24  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  *  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  *  SUCH DAMAGE.
     31  *
     32  * 	@(#)twinkle2.c	8.1 (Berkeley) 6/8/93
     33  */
     34 
     35 #include <stdlib.h>
     36 #include <unistd.h>
     37 #include <curses.h>
     38 #include <signal.h>
     39 
     40 #define	NCOLS	80
     41 #define	NLINES	24
     42 #define	MAXPATTERNS	4
     43 
     44 typedef struct {
     45 	int	y, x;
     46 } LOCS;
     47 
     48 static LOCS	Layout[NCOLS * NLINES];	/* current board layout */
     49 
     50 static int	Pattern,		/* current pattern number */
     51 		Numstars;		/* number of stars in pattern */
     52 
     53 static void puton(char);
     54 static void die(int);
     55 static void makeboard(void);
     56 static int ison(int, int);
     57 
     58 static int AM;
     59 static char *VS;
     60 static char *TI;
     61 static char *CL;
     62 
     63 int
     64 main(void)
     65 {
     66 	char	*sp;
     67 	char	buf[1024];
     68 	char	*ptr = buf;
     69 
     70 	srand(getpid());		/* initialize random sequence */
     71 
     72 	if (isatty(0)) {
     73 		initscr();
     74 		gettmode();
     75 		if ((sp = getenv("TERM")) != NULL)
     76 			setterm(sp);
     77 		signal(SIGINT, die);
     78 	}
     79 	else {
     80 		printf("Need a terminal on fd=%d\n", 0);
     81 		exit(1);
     82 	}
     83 
     84 	tgetent(buf, sp);
     85 
     86 	AM = tgetflag("am");
     87 	TI = tgetstr("ti", &ptr);
     88 	if (TI == NULL) {
     89 		printf("terminal does not have the ti capability\n");
     90 		exit(1);
     91 	}
     92 	VS = tgetstr("vs", &ptr);
     93 	if (VS == NULL) {
     94 		printf("terminal does not have the vs capability\n");
     95 		exit(1);
     96 	}
     97 	CL = tgetstr("cl", &ptr);
     98 	if (CL == NULL) {
     99 		printf("terminal does not have the cl capability\n");
    100 		exit(1);
    101 	}
    102 	puts(TI);
    103 	puts(VS);
    104 
    105 	noecho();
    106 	nonl();
    107 	tputs(CL, NLINES, putchar);
    108 	for (;;) {
    109 		makeboard();		/* make the board setup */
    110 		puton('*');		/* put on '*'s */
    111 		puton(' ');		/* cover up with ' 's */
    112 	}
    113 }
    114 
    115 /*
    116  * On program exit, move the cursor to the lower left corner by
    117  * direct addressing, since current location is not guaranteed.
    118  * We lie and say we used to be at the upper right corner to guarantee
    119  * absolute addressing.
    120  */
    121 static void
    122 die(int n)
    123 {
    124 	signal(SIGINT, SIG_IGN);
    125 	mvcur(0, COLS - 1, LINES - 1, 0);
    126 	endwin();
    127 	exit(n);
    128 }
    129 
    130 static void
    131 puton(char ch)
    132 {
    133 	LOCS	*lp;
    134 	int		r;
    135 	LOCS	*end;
    136 	LOCS		temp;
    137 	static int	lasty, lastx;
    138 
    139 	end = &Layout[Numstars];
    140 	for (lp = Layout; lp < end; lp++) {
    141 		r = rand() % Numstars;
    142 		temp = *lp;
    143 		*lp = Layout[r];
    144 		Layout[r] = temp;
    145 	}
    146 
    147 	for (lp = Layout; lp < end; lp++)
    148 			/* prevent scrolling */
    149 		if (!AM || (lp->y < NLINES - 1 || lp->x < NCOLS - 1)) {
    150 			mvcur(lasty, lastx, lp->y, lp->x);
    151 			putchar(ch);
    152 			lasty = lp->y;
    153 			if ((lastx = lp->x + 1) >= NCOLS)
    154 				if (AM) {
    155 					lastx = 0;
    156 					lasty++;
    157 				}
    158 				else
    159 					lastx = NCOLS - 1;
    160 		}
    161 }
    162 
    163 /*
    164  * Make the current board setup.  It picks a random pattern and
    165  * calls ison() to determine if the character is on that pattern
    166  * or not.
    167  */
    168 static void
    169 makeboard(void)
    170 {
    171 	int		y, x;
    172 	LOCS	*lp;
    173 
    174 	Pattern = rand() % MAXPATTERNS;
    175 	lp = Layout;
    176 	for (y = 0; y < NLINES; y++)
    177 		for (x = 0; x < NCOLS; x++)
    178 			if (ison(y, x)) {
    179 				lp->y = y;
    180 				lp->x = x;
    181 				lp++;
    182 			}
    183 	Numstars = lp - Layout;
    184 }
    185 
    186 /*
    187  * Return TRUE if (y, x) is on the current pattern.
    188  */
    189 static int
    190 ison(int y, int x)
    191 {
    192 	switch (Pattern) {
    193 	  case 0:	/* alternating lines */
    194 		return !(y & 01);
    195 	  case 1:	/* box */
    196 		if (x >= LINES && y >= NCOLS)
    197 			return FALSE;
    198 		if (y < 3 || y >= NLINES - 3)
    199 			return TRUE;
    200 		return (x < 3 || x >= NCOLS - 3);
    201 	  case 2:	/* holy pattern! */
    202 		return ((x + y) & 01);
    203 	  case 3:	/* bar across center */
    204 		return (y >= 9 && y <= 15);
    205 	}
    206 	/* NOTREACHED */
    207 }
    208