-
Notifications
You must be signed in to change notification settings - Fork 0
/
lunar.c
280 lines (242 loc) · 7.14 KB
/
lunar.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Translation of
// <http://www.cs.brandeis.edu/~storer/LunarLander/LunarLander/LunarLanderListing.jpg>
// by Jim Storer from FOCAL to C.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
// Global variables
//
// A - Altitude (miles)
// G - Gravity
// I - Intermediate altitude (miles)
// J - Intermediate velocity (miles/sec)
// K - Fuel rate (lbs/sec)
// L - Elapsed time (sec)
// M - Total weight (lbs)
// N - Empty weight (lbs, Note: M - N is remaining fuel weight)
// S - Time elapsed in current 10-second turn (sec)
// T - Time remaining in current 10-second turn (sec)
// V - Downward speed (miles/sec)
// W - Temporary working variable
// Z - Thrust per pound of fuel burned
static double A, G, I, J, K, L, M, N, S, T, V, W, Z;
static int echo_input = 0;
static void play_game();
static void update_lander_state();
static void apply_thrust();
// Input routines (substitutes for FOCAL ACCEPT command)
static int accept_double(double *value);
static int accept_yes_or_no();
static void accept_line(char **buffer, size_t *buffer_length);
int main(int argc, const char **argv)
{
if (argc > 1)
{
// If --echo is present, then write all input back to standard output.
// (This is useful for testing with files as redirected input.)
if (strcmp(argv[1], "--echo") == 0)
echo_input = 1;
}
puts("CONTROL CALLING LUNAR MODULE. MANUAL CONTROL IS NECESSARY");
puts("YOU MAY RESET FUEL RATE K EACH 10 SECS TO 0 OR ANY VALUE");
puts("BETWEEN 8 & 200 LBS/SEC. YOU'VE 16000 LBS FUEL. ESTIMATED");
puts("FREE FALL IMPACT TIME-120 SECS. CAPSULE WEIGHT-32500 LBS\n\n");
do
{
play_game();
puts("\n\n\nTRY AGAIN?");
} while (accept_yes_or_no());
puts("CONTROL OUT\n\n");
return 0;
}
void play_game()
{
// 01.20 in original FOCAL code
puts("FIRST RADAR CHECK COMING UP\n\n");
puts("COMMENCE LANDING PROCEDURE");
puts("TIME,SECS ALTITUDE,MILES+FEET VELOCITY,MPH FUEL,LBS FUEL RATE");
A = 120;
V = 1;
M = 32500;
N = 16500;
G = .001;
Z = 1.8;
L = 0;
start_turn: // 02.10 in original FOCAL code
printf("%7.0f%16.0f%7.0f%15.2f%12.1f ",
L,
trunc(A),
5280 * (A - trunc(A)),
3600 * V,
M - N);
prompt_for_k:
fputs("K=:", stdout);
int is_valid_input = accept_double(&K);
if (!is_valid_input || K < 0 || ((0 < K) && (K < 8)) || K > 200)
{
fputs("NOT POSSIBLE", stdout);
for (int x = 1; x <= 51; ++x)
putchar('.');
goto prompt_for_k;
}
T = 10;
turn_loop:
for (;;) // 03.10 in original FOCAL code
{
if (M - N < .001)
goto fuel_out;
if (T < .001)
goto start_turn;
S = T;
if (N + S * K - M > 0)
S = (M - N) / K;
apply_thrust();
if (I <= 0)
goto loop_until_on_the_moon;
if ((V > 0) && (J < 0))
{
for (;;) // 08.10 in original FOCAL code
{
// FOCAL-to-C gotcha: In FOCAL, multiplication has a higher
// precedence than division. In C, they have the same
// precedence and are evaluated left-to-right. So the
// original FOCAL subexpression `M * G / Z * K` can't be
// copied as-is into C: `Z * K` has to be parenthesized to
// get the same result.
W = (1 - M * G / (Z * K)) / 2;
S = M * V / (Z * K * (W + sqrt(W * W + V / Z))) + 0.05;
apply_thrust();
if (I <= 0)
goto loop_until_on_the_moon;
update_lander_state();
if (-J < 0)
goto turn_loop;
if (V <= 0)
goto turn_loop;
}
}
update_lander_state();
}
loop_until_on_the_moon: // 07.10 in original FOCAL code
while (S >= .005)
{
S = 2 * A / (V + sqrt(V * V + 2 * A * (G - Z * K / M)));
apply_thrust();
update_lander_state();
}
goto on_the_moon;
fuel_out: // 04.10 in original FOCAL code
printf("FUEL OUT AT %8.2f SECS\n", L);
S = (sqrt(V * V + 2 * A * G) - V) / G;
V += G * S;
L += S;
on_the_moon: // 05.10 in original FOCAL code
printf("ON THE MOON AT %8.2f SECS\n", L);
W = 3600 * V;
printf("IMPACT VELOCITY OF %8.2f M.P.H.\n", W);
printf("FUEL LEFT: %8.2f LBS\n", M - N);
if (W <= 1)
puts("PERFECT LANDING !-(LUCKY)");
else if (W <= 10)
puts("GOOD LANDING-(COULD BE BETTER)");
else if (W <= 22)
puts("CONGRATULATIONS ON A POOR LANDING");
else if (W <= 40)
puts("CRAFT DAMAGE. GOOD LUCK");
else if (W <= 60)
puts("CRASH LANDING-YOU'VE 5 HRS OXYGEN");
else
{
puts("SORRY,BUT THERE WERE NO SURVIVORS-YOU BLEW IT!");
printf("IN FACT YOU BLASTED A NEW LUNAR CRATER %8.2f FT. DEEP\n", W * .277777);
}
}
// Subroutine at line 06.10 in original FOCAL code
void update_lander_state()
{
L += S;
T -= S;
M -= S * K;
A = I;
V = J;
}
// Subroutine at line 09.10 in original FOCAL code
void apply_thrust()
{
double Q = S * K / M;
double Q_2 = pow(Q, 2);
double Q_3 = pow(Q, 3);
double Q_4 = pow(Q, 4);
double Q_5 = pow(Q, 5);
J = V + G * S + Z * (-Q - Q_2 / 2 - Q_3 / 3 - Q_4 / 4 - Q_5 / 5);
I = A - G * S * S / 2 - V * S + Z * S * (Q / 2 + Q_2 / 6 + Q_3 / 12 + Q_4 / 20 + Q_5 / 30);
}
// Read a floating-point value from stdin.
//
// Returns 1 on success, or 0 if input did not contain a number.
//
// Calls exit(-1) on EOF or other failure to read input.
int accept_double(double *value)
{
char *buffer = NULL;
size_t buffer_length = 0;
accept_line(&buffer, &buffer_length);
int is_valid_input = sscanf(buffer, "%lf", value);
free(buffer);
if (is_valid_input != 1)
is_valid_input = 0;
return is_valid_input;
}
// Reads input and returns 1 if it starts with 'Y' or 'y', or returns 0 if it
// starts with 'N' or 'n'.
//
// If input starts with none of those characters, prompt again.
//
// If unable to read input, calls exit(-1);
int accept_yes_or_no()
{
int result = -1;
do
{
fputs("(ANS. YES OR NO):", stdout);
char *buffer = NULL;
size_t buffer_length = 0;
accept_line(&buffer, &buffer_length);
if (buffer_length > 0)
{
switch (buffer[0])
{
case 'y':
case 'Y':
result = 1;
break;
case 'n':
case 'N':
result = 0;
break;
default:
break;
}
}
free(buffer);
} while (result < 0);
return result;
}
// Reads a line of input. Caller is responsible for calling free() on the
// returned buffer.
//
// If unable to read input, calls exit(-1).
void accept_line(char **buffer, size_t *buffer_length)
{
fflush(stdout);
if (getline(buffer, buffer_length, stdin) == -1)
{
fputs("\nEND OF INPUT\n", stderr);
exit(-1);
}
if (echo_input)
{
fputs(*buffer, stdout);
}
}