Merge from vendor branch GDB:
[dragonfly.git] / games / pom / pom.c
1 /*
2  * Copyright (c) 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software posted to USENET.
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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by the University of
18  *      California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * @(#) Copyright (c) 1989, 1993 The Regents of the University of California.  All rights reserved.
36  * @(#)pom.c       8.1 (Berkeley) 5/31/93
37  * $FreeBSD: src/games/pom/pom.c,v 1.9 1999/11/30 03:49:09 billf Exp $
38  * $DragonFly: src/games/pom/pom.c,v 1.3 2003/11/12 14:53:54 eirikn Exp $
39  */
40
41 /*
42  * Phase of the Moon.  Calculates the current phase of the moon.
43  * Based on routines from `Practical Astronomy with Your Calculator',
44  * by Duffett-Smith.  Comments give the section from the book that
45  * particular piece of code was adapted from.
46  *
47  * -- Keith E. Brandt  VIII 1984
48  *
49  */
50
51 #include <time.h>
52 #include <stdio.h>
53 #include <math.h>
54
55 #ifndef PI
56 #define PI        3.14159265358979323846
57 #endif
58 #define EPOCH     85
59 #define EPSILONg  279.611371    /* solar ecliptic long at EPOCH */
60 #define RHOg      282.680403    /* solar ecliptic long of perigee at EPOCH */
61 #define ECCEN     0.01671542    /* solar orbit eccentricity */
62 #define lzero     18.251907     /* lunar mean long at EPOCH */
63 #define Pzero     192.917585    /* lunar mean long of perigee at EPOCH */
64 #define Nzero     55.204723     /* lunar mean long of node at EPOCH */
65 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
66
67 static void     adj360 (double *);
68 static double   dtor (double);
69 static double   potm (double);
70
71 int
72 main()
73 {
74         time_t tt;
75         struct tm *GMT;
76         double days, today, tomorrow;
77         int cnt;
78
79         (void) time(&tt);
80         GMT = gmtime(&tt);
81         days = (GMT->tm_yday + 1) + ((GMT->tm_hour +
82             (GMT->tm_min / 60.0) + (GMT->tm_sec / 3600.0)) / 24.0);
83         for (cnt = EPOCH; cnt < GMT->tm_year; ++cnt)
84                 days += isleap(1900 + cnt) ? 366 : 365;
85         today = potm(days) + .5;
86         (void)printf("The Moon is ");
87         if ((int)today == 100)
88                 (void)printf("Full\n");
89         else if (!(int)today)
90                 (void)printf("New\n");
91         else {
92                 tomorrow = potm(days + 1);
93                 if ((int)today == 50)
94                         (void)printf("%s\n", tomorrow > today ?
95                             "at the First Quarter" : "at the Last Quarter");
96                 else {
97                         (void)printf("%s ", tomorrow > today ?
98                             "Waxing" : "Waning");
99                         if (today > 50)
100                                 (void)printf("Gibbous (%1.0f%% of Full)\n",
101                                     today);
102                         else if (today < 50)
103                                 (void)printf("Crescent (%1.0f%% of Full)\n",
104                                     today);
105                 }
106         }
107
108         return 0;
109 }
110
111 /*
112  * potm --
113  *      return phase of the moon
114  */
115 static double
116 potm(days)
117         double days;
118 {
119         double N, Msol, Ec, LambdaSol, l, Mm, Ev, Ac, A3, Mmprime;
120         double A4, lprime, V, ldprime, D, Nm;
121
122         N = 360 * days / 365.2422;                              /* sec 42 #3 */
123         adj360(&N);
124         Msol = N + EPSILONg - RHOg;                             /* sec 42 #4 */
125         adj360(&Msol);
126         Ec = 360 / PI * ECCEN * sin(dtor(Msol));                /* sec 42 #5 */
127         LambdaSol = N + Ec + EPSILONg;                          /* sec 42 #6 */
128         adj360(&LambdaSol);
129         l = 13.1763966 * days + lzero;                          /* sec 61 #4 */
130         adj360(&l);
131         Mm = l - (0.1114041 * days) - Pzero;                    /* sec 61 #5 */
132         adj360(&Mm);
133         Nm = Nzero - (0.0529539 * days);                        /* sec 61 #6 */
134         adj360(&Nm);
135         Ev = 1.2739 * sin(dtor(2*(l - LambdaSol) - Mm));        /* sec 61 #7 */
136         Ac = 0.1858 * sin(dtor(Msol));                          /* sec 61 #8 */
137         A3 = 0.37 * sin(dtor(Msol));
138         Mmprime = Mm + Ev - Ac - A3;                            /* sec 61 #9 */
139         Ec = 6.2886 * sin(dtor(Mmprime));                       /* sec 61 #10 */
140         A4 = 0.214 * sin(dtor(2 * Mmprime));                    /* sec 61 #11 */
141         lprime = l + Ev + Ec - Ac + A4;                         /* sec 61 #12 */
142         V = 0.6583 * sin(dtor(2 * (lprime - LambdaSol)));       /* sec 61 #13 */
143         ldprime = lprime + V;                                   /* sec 61 #14 */
144         D = ldprime - LambdaSol;                                /* sec 63 #2 */
145         return(50 * (1 - cos(dtor(D))));                        /* sec 63 #3 */
146 }
147
148 /*
149  * dtor --
150  *      convert degrees to radians
151  */
152 static double
153 dtor(deg)
154         double deg;
155 {
156         return(deg * PI / 180);
157 }
158
159 /*
160  * adj360 --
161  *      adjust value so 0 <= deg <= 360
162  */
163 static void
164 adj360(deg)
165         double *deg;
166 {
167         for (;;)
168                 if (*deg < 0)
169                         *deg += 360;
170                 else if (*deg > 360)
171                         *deg -= 360;
172                 else
173                         break;
174 }