]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - games/pom/pom.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.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
36 #if 0
37 #ifndef lint
38 static const char copyright[] =
39 "@(#) Copyright (c) 1989, 1993\n\
40         The Regents of the University of California.  All rights reserved.\n";
41 #endif /* not lint */
42
43 #ifndef lint
44 static const char sccsid[] = "@(#)pom.c       8.1 (Berkeley) 5/31/93";
45 #endif /* not lint */
46 #endif
47 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD$");
49
50 /*
51  * Phase of the Moon.  Calculates the current phase of the moon.
52  * Based on routines from `Practical Astronomy with Your Calculator',
53  * by Duffett-Smith.  Comments give the section from the book that
54  * particular piece of code was adapted from.
55  *
56  * -- Keith E. Brandt  VIII 1984
57  *
58  */
59
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <math.h>
63 #include <string.h>
64 #include <sysexits.h>
65 #include <time.h>
66 #include <unistd.h> 
67
68 #ifndef PI
69 #define PI        3.14159265358979323846
70 #endif
71 #define EPOCH     85
72 #define EPSILONg  279.611371    /* solar ecliptic long at EPOCH */
73 #define RHOg      282.680403    /* solar ecliptic long of perigee at EPOCH */
74 #define ECCEN     0.01671542    /* solar orbit eccentricity */
75 #define lzero     18.251907     /* lunar mean long at EPOCH */
76 #define Pzero     192.917585    /* lunar mean long of perigee at EPOCH */
77 #define Nzero     55.204723     /* lunar mean long of node at EPOCH */
78 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
79
80 static void     adj360(double *);
81 static double   dtor(double);
82 static double   potm(double);
83 static void     usage(char *progname);
84
85 int
86 main(int argc, char **argv)
87 {
88         time_t tt;
89         struct tm GMT, tmd;
90         double days, today, tomorrow;
91         int ch, cnt;
92         char *odate = NULL, *otime = NULL;
93
94         while ((ch = getopt(argc, argv, "d:t:")) != -1)
95                 switch (ch) {
96                 case 'd':
97                         odate = optarg;
98                         break;
99                 case 't':
100                         otime = optarg;
101                         break;
102                 default:
103                         usage(argv[0]);
104                 }
105
106         argc -= optind;
107         argv += optind;
108
109         if (argc)
110                 usage(argv[0]);
111
112         /* Adjust based on users preferences */
113         time(&tt);
114         if (otime != NULL || odate != NULL) {
115                 /* Save today in case -d isn't specified */
116                 localtime_r(&tt, &tmd);
117
118                 if (odate != NULL) {
119                         tmd.tm_year = strtol(odate, NULL, 10) - 1900;
120                         tmd.tm_mon = strtol(odate + 5, NULL, 10) - 1;
121                         tmd.tm_mday = strtol(odate + 8, NULL, 10);
122                         /* Use midnight as the middle of the night */
123                         tmd.tm_hour = 0;
124                         tmd.tm_min = 0;
125                         tmd.tm_sec = 0;
126                 }
127                 if (otime != NULL) {
128                         tmd.tm_hour = strtol(otime, NULL, 10);
129                         tmd.tm_min = strtol(otime + 3, NULL, 10);
130                         tmd.tm_sec = strtol(otime + 6, NULL, 10);
131                 }
132                 tt = mktime(&tmd);
133         }
134
135         gmtime_r(&tt, &GMT);
136         days = (GMT.tm_yday + 1) + ((GMT.tm_hour +
137             (GMT.tm_min / 60.0) + (GMT.tm_sec / 3600.0)) / 24.0);
138         for (cnt = EPOCH; cnt < GMT.tm_year; ++cnt)
139                 days += isleap(1900 + cnt) ? 366 : 365;
140         today = potm(days) + .5;
141         (void)printf("The Moon is ");
142         if ((int)today == 100)
143                 (void)printf("Full\n");
144         else if (!(int)today)
145                 (void)printf("New\n");
146         else {
147                 tomorrow = potm(days + 1);
148                 if ((int)today == 50)
149                         (void)printf("%s\n", tomorrow > today ?
150                             "at the First Quarter" : "at the Last Quarter");
151                 else {
152                         (void)printf("%s ", tomorrow > today ?
153                             "Waxing" : "Waning");
154                         if (today > 50)
155                                 (void)printf("Gibbous (%1.0f%% of Full)\n",
156                                     today);
157                         else if (today < 50)
158                                 (void)printf("Crescent (%1.0f%% of Full)\n",
159                                     today);
160                 }
161         }
162
163         return 0;
164 }
165
166 /*
167  * potm --
168  *      return phase of the moon
169  */
170 static double
171 potm(double days)
172 {
173         double N, Msol, Ec, LambdaSol, l, Mm, Ev, Ac, A3, Mmprime;
174         double A4, lprime, V, ldprime, D, Nm;
175
176         N = 360 * days / 365.2422;                              /* sec 42 #3 */
177         adj360(&N);
178         Msol = N + EPSILONg - RHOg;                             /* sec 42 #4 */
179         adj360(&Msol);
180         Ec = 360 / PI * ECCEN * sin(dtor(Msol));                /* sec 42 #5 */
181         LambdaSol = N + Ec + EPSILONg;                          /* sec 42 #6 */
182         adj360(&LambdaSol);
183         l = 13.1763966 * days + lzero;                          /* sec 61 #4 */
184         adj360(&l);
185         Mm = l - (0.1114041 * days) - Pzero;                    /* sec 61 #5 */
186         adj360(&Mm);
187         Nm = Nzero - (0.0529539 * days);                        /* sec 61 #6 */
188         adj360(&Nm);
189         Ev = 1.2739 * sin(dtor(2*(l - LambdaSol) - Mm));        /* sec 61 #7 */
190         Ac = 0.1858 * sin(dtor(Msol));                          /* sec 61 #8 */
191         A3 = 0.37 * sin(dtor(Msol));
192         Mmprime = Mm + Ev - Ac - A3;                            /* sec 61 #9 */
193         Ec = 6.2886 * sin(dtor(Mmprime));                       /* sec 61 #10 */
194         A4 = 0.214 * sin(dtor(2 * Mmprime));                    /* sec 61 #11 */
195         lprime = l + Ev + Ec - Ac + A4;                         /* sec 61 #12 */
196         V = 0.6583 * sin(dtor(2 * (lprime - LambdaSol)));       /* sec 61 #13 */
197         ldprime = lprime + V;                                   /* sec 61 #14 */
198         D = ldprime - LambdaSol;                                /* sec 63 #2 */
199         return(50 * (1 - cos(dtor(D))));                        /* sec 63 #3 */
200 }
201
202 /*
203  * dtor --
204  *      convert degrees to radians
205  */
206 static double
207 dtor(double deg)
208 {
209
210         return(deg * PI / 180);
211 }
212
213 /*
214  * adj360 --
215  *      adjust value so 0 <= deg <= 360
216  */
217 static void
218 adj360(double *deg)
219 {
220
221         for (;;)
222                 if (*deg < 0)
223                         *deg += 360;
224                 else if (*deg > 360)
225                         *deg -= 360;
226                 else
227                         break;
228 }
229
230 static void
231 usage(char *progname)
232 {
233
234         fprintf(stderr, "Usage: %s [-d yyyy.mm.dd] [-t hh:mm:ss]\n", progname);
235         exit(EX_USAGE);
236 }