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