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