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