Un-internationalised the title strings and updated POs once again.
[neverball] / ball / game.c
1 /*   
2  * Copyright (C) 2003 Robert Kooima
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #include <SDL.h>
16 #include <math.h>
17
18 #include "glext.h"
19 #include "game.h"
20 #include "vec3.h"
21 #include "geom.h"
22 #include "back.h"
23 #include "part.h"
24 #include "image.h"
25 #include "audio.h"
26 #include "solid.h"
27 #include "config.h"
28 #include "binary.h"
29 #include "level.h"
30
31 /*---------------------------------------------------------------------------*/
32
33 static int game_state = 0;
34
35 static struct s_file file;
36 static struct s_file back;
37
38 static float clock      = 0.f;          /* Clock time                        */
39 static int   clock_down = 1;            /* Clock go up or down?              */
40
41 static float game_ix;                   /* Input rotation about X axis       */
42 static float game_iz;                   /* Input rotation about Z axis       */
43 static float game_rx;                   /* Floor rotation about X axis       */
44 static float game_rz;                   /* Floor rotation about Z axis       */
45
46 static float view_a;                    /* Ideal view rotation about Y axis  */
47 static float view_ry;                   /* Angular velocity about Y axis     */
48 static float view_dc;                   /* Ideal view distance above ball    */
49 static float view_dp;                   /* Ideal view distance above ball    */
50 static float view_dz;                   /* Ideal view distance behind ball   */
51 static float view_fov;                  /* Field of view                     */
52
53 static float view_c[3];                 /* Current view center               */
54 static float view_v[3];                 /* Current view vector               */
55 static float view_p[3];                 /* Current view position             */
56 static float view_e[3][3];              /* Current view orientation          */
57 static float view_k;
58
59 static int   coins  = 0;                /* Collected coins                   */
60 static int   goal_c = 0;                /* Goal coins remaining (0 = open)   */
61 static float goal_k = 0;                /* Goal animation                    */
62 static int   swch_e = 1;                /* Switching enabled flag            */
63 static int   jump_e = 1;                /* Jumping enabled flag              */
64 static int   jump_b = 0;                /* Jump-in-progress flag             */
65 static float jump_dt;                   /* Jump duration                     */
66 static float jump_p[3];                 /* Jump destination                  */
67 static float fade_k = 0.0;              /* Fade in/out level                 */
68 static float fade_d = 0.0;              /* Fade in/out direction             */
69
70 /*---------------------------------------------------------------------------*/
71
72 static void view_init(void)
73 {
74     view_a  = 0.f;
75     view_ry = 0.f;
76
77     view_fov = (float) config_get_d(CONFIG_VIEW_FOV);
78     view_dp  = (float) config_get_d(CONFIG_VIEW_DP) / 100.0f;
79     view_dc  = (float) config_get_d(CONFIG_VIEW_DC) / 100.0f;
80     view_dz  = (float) config_get_d(CONFIG_VIEW_DZ) / 100.0f;
81     view_k   = 1.0f;
82
83     view_c[0] = 0.f;
84     view_c[1] = view_dc;
85     view_c[2] = 0.f;
86
87     view_p[0] =     0.f;
88     view_p[1] = view_dp;
89     view_p[2] = view_dz;
90
91     view_e[0][0] = 1.f;
92     view_e[0][1] = 0.f;
93     view_e[0][2] = 0.f;
94     view_e[1][0] = 0.f;
95     view_e[1][1] = 1.f;
96     view_e[1][2] = 0.f;
97     view_e[2][0] = 0.f;
98     view_e[2][1] = 0.f;
99     view_e[2][2] = 1.f;
100 }
101
102 int game_init(const struct level * level, int t, int g)
103 {
104     clock      = (float) t / 100.f;
105     clock_down = (t > 0);
106     coins      = 0;
107
108     if (game_state)
109         game_free();
110
111     game_ix = 0.f;
112     game_iz = 0.f;
113     game_rx = 0.f;
114     game_rz = 0.f;
115
116     /* Initialize jump and goal states. */
117
118     jump_e = 1;
119     jump_b = 0;
120
121     goal_c = g;
122     goal_k = (g == 0) ? 1.0f : 0.0f;
123
124     /* Initialise the level, background, particles, fade, and view. */
125
126     fade_k =  1.0f;
127     fade_d = -2.0f;
128
129     part_reset(GOAL_HEIGHT);
130     view_init();
131     back_init(level->grad, config_get_d(CONFIG_GEOMETRY));
132
133     if (sol_load(&back, config_data(level->back),
134                  config_get_d(CONFIG_TEXTURES), 0) &&
135         sol_load(&file, level->file,
136                  config_get_d(CONFIG_TEXTURES), config_get_d(CONFIG_SHADOW)))
137         return (game_state = 1);
138     else
139         return (game_state = 0);
140 }
141
142 void game_free(void)
143 {
144     if (game_state)
145     {
146         sol_free(&file);
147         sol_free(&back);
148         back_free();
149     }
150     game_state = 0;
151 }
152
153 /*---------------------------------------------------------------------------*/
154
155 int curr_clock(void)
156 {
157     return (int) (clock * 100.f);
158 }
159
160 int curr_coins(void)
161 {
162     return coins;
163 }
164
165 int curr_goal(void)
166 {
167     return goal_c;
168 }
169
170 char *curr_intro(void)
171 {
172     return (file.ac > 0) ? file.av : NULL;
173 }
174
175 /*---------------------------------------------------------------------------*/
176
177 static void game_draw_balls(const struct s_file *fp)
178 {
179     float c[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
180     float M[16];
181
182     m_basis(M, fp->uv[0].e[0], fp->uv[0].e[1], fp->uv[0].e[2]);
183
184     glPushMatrix();
185     {
186         glTranslatef(fp->uv[0].p[0],
187                      fp->uv[0].p[1] + BALL_FUDGE,
188                      fp->uv[0].p[2]);
189         glMultMatrixf(M);
190         glScalef(fp->uv[0].r,
191                  fp->uv[0].r,
192                  fp->uv[0].r);
193
194         glColor4fv(c);
195
196         ball_draw();
197     }
198     glPopMatrix();
199 }
200
201 static void game_draw_coins(const struct s_file *fp)
202 {
203     float r = 360.f * SDL_GetTicks() / 1000.f;
204     int ci;
205
206     coin_push();
207     {
208         for (ci = 0; ci < fp->cc; ci++)
209             if (fp->cv[ci].n > 0)
210             {
211                 glPushMatrix();
212                 {
213                     glTranslatef(fp->cv[ci].p[0],
214                                  fp->cv[ci].p[1],
215                                  fp->cv[ci].p[2]);
216                     glRotatef(r, 0.0f, 1.0f, 0.0f);
217                     coin_draw(fp->cv[ci].n, r);
218                 }
219                 glPopMatrix();
220             }
221     }
222     coin_pull();
223 }
224
225 static void game_draw_goals(const struct s_file *fp, float rx, float ry)
226 {
227     int zi;
228
229     if (goal_c == 0)
230         for (zi = 0; zi < fp->zc; zi++)
231         {
232             glPushMatrix();
233             {
234                 glTranslatef(fp->zv[zi].p[0],
235                              fp->zv[zi].p[1],
236                              fp->zv[zi].p[2]);
237
238                 part_draw_goal(rx, ry, fp->zv[zi].r, goal_k);
239
240                 glScalef(fp->zv[zi].r, goal_k, fp->zv[zi].r);
241                 goal_draw();
242             }
243             glPopMatrix();
244         }
245 }
246
247 static void game_draw_jumps(const struct s_file *fp)
248 {
249     int ji;
250
251     for (ji = 0; ji < fp->jc; ji++)
252     {
253         glPushMatrix();
254         {
255             glTranslatef(fp->jv[ji].p[0],
256                          fp->jv[ji].p[1],
257                          fp->jv[ji].p[2]);
258
259             glScalef(fp->jv[ji].r, 1.f, fp->jv[ji].r);
260             jump_draw();
261         }
262         glPopMatrix();
263     }
264 }
265
266 static void game_draw_swchs(const struct s_file *fp)
267 {
268     int xi;
269
270     for (xi = 0; xi < fp->xc; xi++)
271     {
272         glPushMatrix();
273         {
274             glTranslatef(fp->xv[xi].p[0],
275                          fp->xv[xi].p[1],
276                          fp->xv[xi].p[2]);
277
278             glScalef(fp->xv[xi].r, 1.f, fp->xv[xi].r);
279             swch_draw(fp->xv[xi].f);
280         }
281         glPopMatrix();
282     }
283 }
284
285 /*---------------------------------------------------------------------------*/
286
287 static void game_refl_all(int s)
288 {
289     const float *ball_p = file.uv->p;
290
291     glPushMatrix();
292     {
293         /* Rotate the environment about the position of the ball. */
294
295         glTranslatef(+ball_p[0], +ball_p[1], +ball_p[2]);
296         glRotatef(-game_rz, view_e[2][0], view_e[2][1], view_e[2][2]);
297         glRotatef(-game_rx, view_e[0][0], view_e[0][1], view_e[0][2]);
298         glTranslatef(-ball_p[0], -ball_p[1], -ball_p[2]);
299
300         /* Draw the floor. */
301
302         sol_refl(&file);
303     }
304     glPopMatrix();
305 }
306
307 /*---------------------------------------------------------------------------*/
308
309 static void game_draw_light(void)
310 {
311     const float light_p[2][4] = {
312         { -8.0f, +32.0f, -8.0f, 1.0f },
313         { +8.0f, +32.0f, +8.0f, 1.0f },
314     };
315     const float light_c[2][4] = {
316         { 1.0f, 0.8f, 0.8f, 1.0f },
317         { 0.8f, 1.0f, 0.8f, 1.0f },
318     };
319
320     /* Configure the lighting. */
321
322     glEnable(GL_LIGHT0);
323     glLightfv(GL_LIGHT0, GL_POSITION, light_p[0]);
324     glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_c[0]);
325     glLightfv(GL_LIGHT0, GL_SPECULAR, light_c[0]);
326
327     glEnable(GL_LIGHT1);
328     glLightfv(GL_LIGHT1, GL_POSITION, light_p[1]);
329     glLightfv(GL_LIGHT1, GL_DIFFUSE,  light_c[1]);
330     glLightfv(GL_LIGHT1, GL_SPECULAR, light_c[1]);
331 }
332
333 static void game_draw_back(int pose, int d, const float p[3])
334 {
335     float c[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
336     float t = SDL_GetTicks() / 1000.f + 120.0f;
337
338     glPushMatrix();
339     {
340         if (d < 0)
341         {
342             glRotatef(game_rz * 2, view_e[2][0], view_e[2][1], view_e[2][2]);
343             glRotatef(game_rx * 2, view_e[0][0], view_e[0][1], view_e[0][2]);
344         }
345
346         glTranslatef(p[0], p[1], p[2]);
347         glColor4fv(c);
348
349         if (config_get_d(CONFIG_BACKGROUND))
350         {
351             /* Draw all background layers back to front. */
352
353             sol_back(&back, BACK_DIST, FAR_DIST, t);
354             back_draw(0);
355             sol_back(&back, 0, BACK_DIST, t);
356
357             /* Draw all foreground geometry in the background file. */
358
359             sol_draw(&back);
360         }
361         else back_draw(0);
362     }
363     glPopMatrix();
364 }
365
366 static void game_draw_fore(int pose, float rx, float ry, int d, const float p[3])
367 {
368     const float *ball_p = file.uv->p;
369     const float  ball_r = file.uv->r;
370     
371     glPushAttrib(GL_LIGHTING_BIT | GL_COLOR_BUFFER_BIT);
372     {
373         glPushMatrix();
374         {
375             /* Rotate the environment about the position of the ball. */
376
377             glTranslatef(+ball_p[0], +ball_p[1] * d, +ball_p[2]);
378             glRotatef(-game_rz * d, view_e[2][0], view_e[2][1], view_e[2][2]);
379             glRotatef(-game_rx * d, view_e[0][0], view_e[0][1], view_e[0][2]);
380             glTranslatef(-ball_p[0], -ball_p[1] * d, -ball_p[2]);
381
382             if (d < 0)
383             {
384                 GLdouble e[4];
385
386                 e[0] = +0;
387                 e[1] = +1;
388                 e[2] = +0;
389                 e[3] = -0.00001;
390
391                 glEnable(GL_CLIP_PLANE0);
392                 glClipPlane(GL_CLIP_PLANE0, e);
393             }
394
395             /* Draw the floor. */
396
397             sol_draw(&file);
398
399             if (config_get_d(CONFIG_SHADOW))
400             {
401                 shad_draw_set(ball_p, ball_r);
402                 sol_shad(&file);
403                 shad_draw_clr();
404             }
405
406             /* Draw the game elements. */
407
408             glEnable(GL_BLEND);
409             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
410
411             if (pose == 0)
412             {
413                 part_draw_coin(-rx * d, -ry);
414                 game_draw_coins(&file);
415                 game_draw_balls(&file);
416             }
417             game_draw_goals(&file, -rx * d, -ry);
418             game_draw_jumps(&file);
419             game_draw_swchs(&file);
420
421             glDisable(GL_CLIP_PLANE0);
422         }
423         glPopMatrix();
424     }
425     glPopAttrib();
426 }
427
428 void game_draw(int pose, float st)
429 {
430     float fov = view_fov;
431
432     if (jump_b) fov *= 2.f * fabsf(jump_dt - 0.5);
433
434     if (game_state)
435     {
436         config_push_persp(fov, 0.1f, FAR_DIST);
437         glPushMatrix();
438         {
439             float v[3], rx, ry;
440             float pup[3];
441             float pdn[3];
442
443             v_cpy(pup, view_p);
444             v_cpy(pdn, view_p);
445             pdn[1] = -pdn[1];
446
447             /* Compute and apply the view. */
448
449             v_sub(v, view_c, view_p);
450
451             rx = V_DEG(fatan2f(-v[1], fsqrtf(v[0] * v[0] + v[2] * v[2])));
452             ry = V_DEG(fatan2f(+v[0], -v[2])) + st;
453
454             glTranslatef(0.f, 0.f, -v_len(v));
455             glRotatef(rx, 1.f, 0.f, 0.f);
456             glRotatef(ry, 0.f, 1.f, 0.f);
457             glTranslatef(-view_c[0], -view_c[1], -view_c[2]);
458
459             if (config_get_d(CONFIG_REFLECTION))
460             {
461                 /* Draw the mirror only into the stencil buffer. */
462
463                 glDisable(GL_DEPTH_TEST);
464                 glEnable(GL_STENCIL_TEST);
465                 glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
466                 glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
467                 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
468
469                 game_refl_all(0);
470
471                 /* Draw the scene reflected into color and depth buffers. */
472
473                 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
474                 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
475                 glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
476                 glEnable(GL_DEPTH_TEST);
477
478                 glFrontFace(GL_CW);
479                 glPushMatrix();
480                 {
481                     glScalef(+1.f, -1.f, +1.f);
482
483                     game_draw_light();
484                     game_draw_back(pose,         -1, pdn);
485                     game_draw_fore(pose, rx, ry, -1, pdn);
486                 }
487                 glPopMatrix();
488                 glFrontFace(GL_CCW);
489
490                 glDisable(GL_STENCIL_TEST);
491             }
492
493             /* Draw the scene normally. */
494
495             game_draw_light();
496             game_refl_all(pose ? 0 : config_get_d(CONFIG_SHADOW));
497             game_draw_back(pose,         +1, pup);
498             game_draw_fore(pose, rx, ry, +1, pup);
499         }
500         glPopMatrix();
501         config_pop_matrix();
502
503         /* Draw the fade overlay. */
504
505         fade_draw(fade_k);
506     }
507 }
508
509 /*---------------------------------------------------------------------------*/
510
511 static void game_update_grav(float h[3], const float g[3])
512 {
513     struct s_file *fp = &file;
514
515     float x[3];
516     float y[3] = { 0.f, 1.f, 0.f };
517     float z[3];
518     float X[16];
519     float Z[16];
520     float M[16];
521
522     /* Compute the gravity vector from the given world rotations. */
523
524     v_sub(z, view_p, fp->uv->p);
525     v_crs(x, y, z);
526     v_crs(z, x, y);
527     v_nrm(x, x);
528     v_nrm(z, z);
529
530     m_rot (Z, z, V_RAD(game_rz));
531     m_rot (X, x, V_RAD(game_rx));
532     m_mult(M, Z, X);
533     m_vxfm(h, M, g);
534 }
535
536 static void game_update_view(float dt)
537 {
538     float dc = view_dc * (jump_b ? 2.0f * fabsf(jump_dt - 0.5f) : 1.0f);
539     float dx = view_ry * dt * 5.0f;
540     float k;
541
542     view_a += view_ry * dt * 90.f;
543
544     /* Center the view about the ball. */
545
546     v_cpy(view_c, file.uv->p);
547     v_inv(view_v, file.uv->v);
548
549     switch (config_get_d(CONFIG_CAMERA))
550     {
551     case 1: /* Camera 1:  Viewpoint chases the ball position. */
552
553         v_sub(view_e[2], view_p, view_c);
554         break;
555
556     case 2: /* Camera 2: View vector is given by view angle. */
557
558         view_e[2][0] = fsinf(V_RAD(view_a));
559         view_e[2][1] = 0.f;
560         view_e[2][2] = fcosf(V_RAD(view_a));
561
562         dx = 0.0f;
563
564         break;
565
566     default: /* Default: View vector approaches the ball velocity vector. */
567
568         k = v_dot(view_v, view_v);
569
570         v_sub(view_e[2], view_p, view_c);
571         v_mad(view_e[2], view_e[2], view_v, k * dt / 4);
572
573         break;
574     }
575
576     /* Orthonormalize the basis of the view in its new position. */
577
578     v_crs(view_e[0], view_e[1], view_e[2]);
579     v_crs(view_e[2], view_e[0], view_e[1]);
580     v_nrm(view_e[0], view_e[0]);
581     v_nrm(view_e[2], view_e[2]);
582
583     /* Compute the new view position. */
584
585     k = 1.0f + v_dot(view_e[2], view_v) / 10.0f;
586
587     view_k = view_k + (k - view_k) * dt;
588
589     if (view_k < 0.5) view_k = 0.5;
590
591     v_cpy(view_p, file.uv->p);
592     v_mad(view_p, view_p, view_e[0], dx      * view_k);
593     v_mad(view_p, view_p, view_e[1], view_dp * view_k);
594     v_mad(view_p, view_p, view_e[2], view_dz * view_k);
595
596     /* Compute the new view center. */
597
598     v_cpy(view_c, file.uv->p);
599     v_mad(view_c, view_c, view_e[1], dc);
600
601     /* Note the current view angle. */
602
603     view_a = V_DEG(fatan2f(view_e[2][0], view_e[2][2]));
604 }
605
606 static void game_update_time(float dt, int b)
607 {
608     if (goal_c == 0 && goal_k < 1.0f)
609         goal_k += dt;
610
611    /* The ticking clock. */
612
613     if (b && clock_down)
614     {
615         if (clock < 600.f)
616             clock -= dt;
617         if (clock < 0.f)
618             clock = 0.f;
619     }
620     else if (b)
621     {
622         clock += dt;
623     }
624 }
625
626 static int game_update_state(int bt)
627 {
628     struct s_file *fp = &file;
629     float p[3];
630     float c[3];
631     int n, e = swch_e;
632
633     /* Test for a coin grab. */
634     
635     if (bt && (n = sol_coin_test(fp, p, COIN_RADIUS)) > 0)
636     {
637         coin_color(c, n);
638         part_burst(p, c);
639
640         coins += n;
641         /* Check for goal open. */
642         if (goal_c > 0)
643         {
644             goal_c = goal_c - n;
645             if (goal_c <= 0)
646             {
647                 audio_play(AUD_SWITCH, 1.f);
648                 goal_c = 0;
649             }
650             else
651                 audio_play(AUD_COIN, 1.f);
652         } 
653         else
654             audio_play(AUD_COIN, 1.f);
655     }
656
657     /* Test for a switch. */
658
659     if ((swch_e = sol_swch_test(fp, swch_e, 0)) != e && e)
660         audio_play(AUD_SWITCH, 1.f);
661
662     /* Test for a jump. */
663
664     if (jump_e == 1 && jump_b == 0 && sol_jump_test(fp, jump_p, 0) == 1)
665     {
666         jump_b  = 1;
667         jump_e  = 0;
668         jump_dt = 0.f;
669         
670         audio_play(AUD_JUMP, 1.f);
671     }
672     if (jump_e == 0 && jump_b == 0 && sol_jump_test(fp, jump_p, 0) == 0)
673         jump_e = 1;
674
675     /* Test for a goal. */
676
677     if (bt && goal_c == 0 && sol_goal_test(fp, p, 0))
678     {
679         audio_play(AUD_GOAL, 1.0f);
680         return GAME_GOAL;
681     }
682
683     /* Test for time-out. */
684
685     if (bt && clock_down && clock <= 0.f)
686         return GAME_TIME;
687
688     /* Test for fall-out. */
689
690     if (bt && fp->uv[0].p[1] < fp->vv[0].p[1])
691         return GAME_FALL;
692
693     return GAME_NONE;
694 }
695
696 /*
697  * On  most  hardware, rendering  requires  much  more  computing power  than
698  * physics.  Since  physics takes less time  than graphics, it  make sense to
699  * detach  the physics update  time step  from the  graphics frame  rate.  By
700  * performing multiple physics updates for  each graphics update, we get away
701  * with higher quality physics with little impact on overall performance.
702  *
703  * Toward this  end, we establish a  baseline maximum physics  time step.  If
704  * the measured  frame time  exceeds this  maximum, we cut  the time  step in
705  * half, and  do two updates.  If THIS  time step exceeds the  maximum, we do
706  * four updates.  And  so on.  In this way, the physics  system is allowed to
707  * seek an optimal update rate independant of, yet in integral sync with, the
708  * graphics frame rate.
709  */
710
711 int game_step(const float g[3], float dt, int bt)
712 {
713     struct s_file *fp = &file;
714
715     float h[3];
716     float d = 0.f;
717     float b = 0.f;
718     float t;
719     int i, n = 1;
720
721     if (game_state)
722     {
723         t = dt;
724
725         /* Smooth jittery or discontinuous input. */
726
727         if (t < RESPONSE)
728         {
729             game_rx += (game_ix - game_rx) * t / RESPONSE;
730             game_rz += (game_iz - game_rz) * t / RESPONSE;
731         }
732         else
733         {
734             game_rx = game_ix;
735             game_rz = game_iz;
736         }
737
738         game_update_grav(h, g);
739         part_step(h, t);
740
741         if (jump_b)
742         {
743             jump_dt += t;
744
745             /* Handle a jump. */
746
747             if (0.5 < jump_dt)
748             {
749                 fp->uv[0].p[0] = jump_p[0];
750                 fp->uv[0].p[1] = jump_p[1];
751                 fp->uv[0].p[2] = jump_p[2];
752             }
753             if (1.f < jump_dt)
754                 jump_b = 0;
755         }
756         else
757         {
758             /* Run the sim. */
759
760             while (t > MAX_DT && n < MAX_DN)
761             {
762                 t /= 2;
763                 n *= 2;
764             }
765
766             for (i = 0; i < n; i++)
767                 if (b < (d = sol_step(fp, h, t, 0, NULL)))
768                     b = d;
769
770             /* Mix the sound of a ball bounce. */
771
772             if (b > 0.5)
773                 audio_play(AUD_BUMP, (b - 0.5f) * 2.0f);
774         }
775
776         game_step_fade(dt);
777         game_update_view(dt);
778         game_update_time(dt, bt);
779
780         return game_update_state(bt);
781     }
782     return GAME_NONE;
783 }
784
785 /*---------------------------------------------------------------------------*/
786
787 void game_no_aa(void)
788 {
789     float max = game_ix * game_ix + game_iz * game_iz;
790     if (max > ANGLE_BOUND * ANGLE_BOUND)
791     {
792         max = ANGLE_BOUND / sqrt(max);
793         game_ix *= max;
794         game_iz *= max;
795     }
796 }
797
798 void game_set_x(int k)
799 {
800     game_ix = -(ANGLE_BOUND) * k / JOY_MAX;
801 #if NO_AA
802     game_no_aa();
803 #endif
804 }
805
806 void game_set_z(int k)
807 {
808     game_iz = +ANGLE_BOUND * k / JOY_MAX;
809 #if NO_AA
810     game_no_aa();
811 #endif
812 }
813
814 void game_set_pos(int x, int y)
815 {
816     game_ix += 40.f * y / config_get_d(CONFIG_MOUSE_SENSE);
817     game_iz += 40.f * x / config_get_d(CONFIG_MOUSE_SENSE);
818     
819 #if NO_AA
820     game_no_aa();
821 #else
822     if (game_ix > +ANGLE_BOUND) game_ix = +ANGLE_BOUND;
823     if (game_ix < -ANGLE_BOUND) game_ix = -ANGLE_BOUND;
824     if (game_iz > +ANGLE_BOUND) game_iz = +ANGLE_BOUND;
825     if (game_iz < -ANGLE_BOUND) game_iz = -ANGLE_BOUND;
826 #endif
827 }
828
829 void game_set_rot(float r)
830 {
831     view_ry = r;
832 }
833
834 /*---------------------------------------------------------------------------*/
835
836 void game_set_fly(float k)
837 {
838     struct s_file *fp = &file;
839
840     float  x[3] = { 1.f, 0.f, 0.f };
841     float  y[3] = { 0.f, 1.f, 0.f };
842     float  z[3] = { 0.f, 0.f, 1.f };
843     float c0[3] = { 0.f, 0.f, 0.f };
844     float p0[3] = { 0.f, 0.f, 0.f };
845     float c1[3] = { 0.f, 0.f, 0.f };
846     float p1[3] = { 0.f, 0.f, 0.f };
847     float  v[3];
848
849     v_cpy(view_e[0], x);
850     v_cpy(view_e[1], y);
851     v_cpy(view_e[2], z);
852
853     /* k = 0.0 view is at the ball. */
854
855     if (fp->uc > 0)
856     {
857         v_cpy(c0, fp->uv[0].p);
858         v_cpy(p0, fp->uv[0].p);
859     }
860
861     v_mad(p0, p0, y, view_dp);
862     v_mad(p0, p0, z, view_dz);
863     v_mad(c0, c0, y, view_dc);
864
865     /* k = +1.0 view is s_view 0 */
866
867     if (k >= 0 && fp->wc > 0)
868     {
869         v_cpy(p1, fp->wv[0].p);
870         v_cpy(c1, fp->wv[0].q);
871     }
872
873     /* k = -1.0 view is s_view 1 */
874
875     if (k <= 0 && fp->wc > 1)
876     {
877         v_cpy(p1, fp->wv[1].p);
878         v_cpy(c1, fp->wv[1].q);
879     }
880
881     /* Interpolate the views. */
882
883     v_sub(v, p1, p0);
884     v_mad(view_p, p0, v, k * k);
885
886     v_sub(v, c1, c0);
887     v_mad(view_c, c0, v, k * k);
888
889     /* Orthonormalize the view basis. */
890
891     v_sub(view_e[2], view_p, view_c);
892     v_crs(view_e[0], view_e[1], view_e[2]);
893     v_crs(view_e[2], view_e[0], view_e[1]);
894     v_nrm(view_e[0], view_e[0]);
895     v_nrm(view_e[2], view_e[2]);
896 }
897
898 void game_look(float phi, float theta)
899 {
900     view_c[0] = view_p[0] + fsinf(V_RAD(theta)) * fcosf(V_RAD(phi));
901     view_c[1] = view_p[1] +                       fsinf(V_RAD(phi));
902     view_c[2] = view_p[2] - fcosf(V_RAD(theta)) * fcosf(V_RAD(phi));
903 }
904
905 /*---------------------------------------------------------------------------*/
906
907 void game_kill_fade(void)
908 {
909     fade_k = 0.0f;
910     fade_d = 0.0f;
911 }
912
913 void game_step_fade(float dt)
914 {
915     if ((fade_k < 1.0f && fade_d > 0.0f) ||
916         (fade_k > 0.0f && fade_d < 0.0f))
917         fade_k += fade_d * dt;
918
919     if (fade_k < 0.0f)
920     {
921         fade_k = 0.0f;
922         fade_d = 0.0f;
923     }
924     if (fade_k > 1.0f)
925     {
926         fade_k = 1.0f;
927         fade_d = 0.0f;
928     }
929 }
930
931 void game_fade(float d)
932 {
933     fade_d = d;
934 }
935
936 /*---------------------------------------------------------------------------*/
937
938 int put_game_state(FILE *fout)
939 {
940     if (game_state)
941     {
942         /* Write the view and tilt state. */
943
944         put_float(fout, &game_rx);
945         put_float(fout, &game_rz);
946         put_array(fout,  view_c, 3);
947         put_array(fout,  view_p, 3);
948
949         /* Write the game simulation state. */
950
951         put_file_state(fout, &file);
952
953         return 1;
954     }
955     return 0;
956 }
957
958 int get_game_state(FILE *fin)
959 {
960     if (game_state)
961     {
962         /* Read the view and tilt state. */
963
964         get_float(fin, &game_rx);
965         get_float(fin, &game_rz);
966         get_array(fin,  view_c, 3);
967         get_array(fin,  view_p, 3);
968
969         /* Read the game simulation state. */
970
971         get_file_state(fin, &file);
972
973         return (feof(fin) ? 0 : 1);
974     }
975     return 0;
976 }
977
978 /*---------------------------------------------------------------------------*/