2 * Copyright (C) 2003 Robert Kooima
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.
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.
32 #include "game_client.h"
33 #include "game_common.h"
34 #include "game_proxy.h"
38 /*---------------------------------------------------------------------------*/
40 static int client_state = 0;
42 static struct s_file file;
43 static struct s_file back;
45 static int reflective; /* Reflective geometry used? */
47 static float timer = 0.f; /* Clock time */
49 static int status = GAME_NONE; /* Outcome of the game */
51 static float game_rx; /* Floor rotation about X axis */
52 static float game_rz; /* Floor rotation about Z axis */
54 static float view_a; /* Ideal view rotation about Y axis */
55 static float view_fov; /* Field of view */
57 static float view_c[3]; /* Current view center */
58 static float view_p[3]; /* Current view position */
59 static float view_e[3][3]; /* Current view reference frame */
61 static int coins = 0; /* Collected coins */
62 static int goal_e = 0; /* Goal enabled flag */
63 static float goal_k = 0; /* Goal animation */
65 static int jump_e = 1; /* Jumping enabled flag */
66 static int jump_b = 0; /* Jump-in-progress flag */
67 static float jump_dt; /* Jump duration */
69 static float fade_k = 0.0; /* Fade in/out level */
70 static float fade_d = 0.0; /* Fade in/out direction */
72 static int ups; /* Updates per second */
73 static int first_update; /* First update flag */
74 static int curr_ball; /* Current ball index */
76 /*---------------------------------------------------------------------------*/
78 static void game_run_cmd(const union cmd *cmd)
80 static const float gup[] = { 0.0f, +9.8f, 0.0f };
81 static const float gdn[] = { 0.0f, -9.8f, 0.0f };
95 case CMD_END_OF_UPDATE:
102 /* Compute gravity for particle effects. */
104 if (status == GAME_GOAL)
105 game_comp_grav(f, gup, view_a, game_rx, game_rz);
107 game_comp_grav(f, gdn, view_a, game_rx, game_rz);
109 /* Step particle, goal and jump effects. */
113 dt = 1.0f / (float) ups;
115 if (goal_e && goal_k < 1.0f)
132 /* Allocate a new ball and mark it as the current ball. */
134 if ((up = realloc(file.uv, sizeof (*up) * (file.uc + 1))))
143 /* Allocate and initialise a new item. */
145 if ((hp = realloc(file.hv, sizeof (*hp) * (file.hc + 1))))
149 v_cpy(h.p, cmd->mkitem.p);
155 file.hv[file.hc] = h;
162 /* Set up particle effects and discard the item. */
164 assert(cmd->pkitem.hi < file.hc);
166 hp = &file.hv[cmd->pkitem.hi];
169 part_burst(hp->p, f);
176 game_rx = cmd->rotate.x;
177 game_rz = cmd->rotate.z;
181 /* Play the sound, then free its file name. */
185 audio_play(cmd->sound.n, cmd->sound.a);
188 * FIXME Command memory management should be done
189 * elsewhere and done properly.
197 timer = cmd->timer.t;
201 status = cmd->status.t;
205 coins = cmd->coins.n;
219 file.bv[cmd->bodypath.bi].pi = cmd->bodypath.pi;
223 file.bv[cmd->bodytime.bi].t = cmd->bodytime.t;
228 * Enable the goal and make sure it's fully visible if
229 * this is the first update.
235 goal_k = first_update ? 1.0f : 0.0f;
240 file.xv[cmd->swchenter.xi].e = 1;
243 case CMD_SWCH_TOGGLE:
244 file.xv[cmd->swchtoggle.xi].f = !file.xv[cmd->swchtoggle.xi].f;
248 file.xv[cmd->swchexit.xi].e = 0;
251 case CMD_UPDATES_PER_SECOND:
255 case CMD_BALL_RADIUS:
256 file.uv[curr_ball].r = cmd->ballradius.r;
259 case CMD_CLEAR_ITEMS:
268 case CMD_CLEAR_BALLS:
277 case CMD_BALL_POSITION:
278 v_cpy(file.uv[curr_ball].p, cmd->ballpos.p);
282 v_cpy(file.uv[curr_ball].e[0], cmd->ballbasis.e[0]);
283 v_cpy(file.uv[curr_ball].e[1], cmd->ballbasis.e[1]);
285 v_crs(file.uv[curr_ball].e[2],
286 file.uv[curr_ball].e[0],
287 file.uv[curr_ball].e[1]);
290 case CMD_BALL_PEND_BASIS:
291 v_cpy(file.uv[curr_ball].E[0], cmd->ballpendbasis.E[0]);
292 v_cpy(file.uv[curr_ball].E[1], cmd->ballpendbasis.E[1]);
294 v_crs(file.uv[curr_ball].E[2],
295 file.uv[curr_ball].E[0],
296 file.uv[curr_ball].E[1]);
299 case CMD_VIEW_POSITION:
300 v_cpy(view_p, cmd->viewpos.p);
303 case CMD_VIEW_CENTER:
304 v_cpy(view_c, cmd->viewcenter.c);
308 v_cpy(view_e[0], cmd->viewbasis.e[0]);
309 v_cpy(view_e[1], cmd->viewbasis.e[1]);
311 v_crs(view_e[2], view_e[0], view_e[1]);
313 view_a = V_DEG(fatan2f(view_e[2][0], view_e[2][2]));
317 case CMD_CURRENT_BALL:
318 curr_ball = cmd->currball.ui;
322 file.pv[cmd->pathflag.pi].f = cmd->pathflag.f;
325 case CMD_STEP_SIMULATION:
327 * Simulate body motion.
329 * This is done on the client side due to replay file size
330 * concerns and isn't done as part of CMD_END_OF_UPDATE to
331 * match the server state as closely as possible. Body
332 * time is still synchronised with the server on a
333 * semi-regular basis and path indices are handled through
334 * CMD_BODY_PATH, thus this code doesn't need to be as
335 * sophisticated as sol_body_step.
338 dt = cmd->stepsim.dt;
340 for (i = 0; i < file.bc; i++)
342 struct s_body *bp = file.bv + i;
343 struct s_path *pp = file.pv + bp->pi;
345 if (bp->pi >= 0 && pp->f)
357 void game_client_step(FILE *demo_fp)
361 while ((cmdp = game_proxy_deq()))
364 * Note: cmd_put is called first here because game_run_cmd
365 * frees the filename of CMD_SOUND.
369 cmd_put(demo_fp, cmdp);
377 /*---------------------------------------------------------------------------*/
379 int game_client_init(const char *file_name)
381 char *back_name = NULL, *grad_name = NULL;
390 if (!sol_load_gl(&file, config_data(file_name),
391 config_get_d(CONFIG_TEXTURES),
392 config_get_d(CONFIG_SHADOW)))
393 return (client_state = 0);
395 reflective = sol_reflective(&file);
402 /* Initialize jump and goal states. */
410 /* Initialise the level, background, particles, fade, and view. */
415 for (i = 0; i < file.dc; i++)
417 char *k = file.av + file.dv[i].ai;
418 char *v = file.av + file.dv[i].aj;
420 if (strcmp(k, "back") == 0) back_name = v;
421 if (strcmp(k, "grad") == 0) grad_name = v;
424 part_reset(GOAL_HEIGHT, JUMP_HEIGHT);
426 view_fov = (float) config_get_d(CONFIG_VIEW_FOV);
431 back_init(grad_name, config_get_d(CONFIG_GEOMETRY));
432 sol_load_gl(&back, config_data(back_name),
433 config_get_d(CONFIG_TEXTURES), 0);
438 void game_client_free(void)
450 /*---------------------------------------------------------------------------*/
454 return (int) (timer * 100.f);
462 int curr_status(void)
467 /*---------------------------------------------------------------------------*/
469 static void game_draw_balls(const struct s_file *fp,
470 const float *bill_M, float t)
472 float c[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
477 m_basis(ball_M, fp->uv[0].e[0], fp->uv[0].e[1], fp->uv[0].e[2]);
478 m_basis(pend_M, fp->uv[0].E[0], fp->uv[0].E[1], fp->uv[0].E[2]);
480 glPushAttrib(GL_LIGHTING_BIT);
483 glTranslatef(fp->uv[0].p[0],
484 fp->uv[0].p[1] + BALL_FUDGE,
486 glScalef(fp->uv[0].r,
491 ball_draw(ball_M, pend_M, bill_M, t);
497 static void game_draw_items(const struct s_file *fp, float t)
502 glPushAttrib(GL_LIGHTING_BIT);
504 item_push(ITEM_COIN);
506 for (hi = 0; hi < fp->hc; hi++)
508 if (fp->hv[hi].t == ITEM_COIN && fp->hv[hi].n > 0)
512 glTranslatef(fp->hv[hi].p[0],
515 glRotatef(r, 0.0f, 1.0f, 0.0f);
516 item_draw(&fp->hv[hi], r);
523 item_push(ITEM_SHRINK);
525 for (hi = 0; hi < fp->hc; hi++)
527 if (fp->hv[hi].t == ITEM_SHRINK)
531 glTranslatef(fp->hv[hi].p[0],
534 glRotatef(r, 0.0f, 1.0f, 0.0f);
535 item_draw(&fp->hv[hi], r);
542 item_push(ITEM_GROW);
544 for (hi = 0; hi < fp->hc; hi++)
546 if (fp->hv[hi].t == ITEM_GROW)
550 glTranslatef(fp->hv[hi].p[0],
553 glRotatef(r, 0.0f, 1.0f, 0.0f);
554 item_draw(&fp->hv[hi], r);
564 static void game_draw_goals(const struct s_file *fp, const float *M, float t)
570 /* Draw the goal particles. */
572 glEnable(GL_TEXTURE_2D);
574 for (zi = 0; zi < fp->zc; zi++)
578 glTranslatef(fp->zv[zi].p[0],
582 part_draw_goal(M, fp->zv[zi].r, goal_k, t);
587 glDisable(GL_TEXTURE_2D);
589 /* Draw the goal column. */
591 for (zi = 0; zi < fp->zc; zi++)
595 glTranslatef(fp->zv[zi].p[0],
599 glScalef(fp->zv[zi].r,
610 static void game_draw_jumps(const struct s_file *fp, const float *M, float t)
614 glEnable(GL_TEXTURE_2D);
616 for (ji = 0; ji < fp->jc; ji++)
620 glTranslatef(fp->jv[ji].p[0],
624 part_draw_jump(M, fp->jv[ji].r, 1.0f, t);
629 glDisable(GL_TEXTURE_2D);
631 for (ji = 0; ji < fp->jc; ji++)
635 glTranslatef(fp->jv[ji].p[0],
638 glScalef(fp->jv[ji].r,
648 static void game_draw_swchs(const struct s_file *fp)
652 for (xi = 0; xi < fp->xc; xi++)
659 glTranslatef(fp->xv[xi].p[0],
662 glScalef(fp->xv[xi].r,
666 swch_draw(fp->xv[xi].f, fp->xv[xi].e);
672 /*---------------------------------------------------------------------------*/
674 static void game_draw_tilt(int d)
676 const float *ball_p = file.uv->p;
678 /* Rotate the environment about the position of the ball. */
680 glTranslatef(+ball_p[0], +ball_p[1] * d, +ball_p[2]);
681 glRotatef(-game_rz * d, view_e[2][0], view_e[2][1], view_e[2][2]);
682 glRotatef(-game_rx * d, view_e[0][0], view_e[0][1], view_e[0][2]);
683 glTranslatef(-ball_p[0], -ball_p[1] * d, -ball_p[2]);
686 static void game_refl_all(void)
692 /* Draw the floor. */
699 /*---------------------------------------------------------------------------*/
701 static void game_draw_light(void)
703 const float light_p[2][4] = {
704 { -8.0f, +32.0f, -8.0f, 0.0f },
705 { +8.0f, +32.0f, +8.0f, 0.0f },
707 const float light_c[2][4] = {
708 { 1.0f, 0.8f, 0.8f, 1.0f },
709 { 0.8f, 1.0f, 0.8f, 1.0f },
712 /* Configure the lighting. */
715 glLightfv(GL_LIGHT0, GL_POSITION, light_p[0]);
716 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_c[0]);
717 glLightfv(GL_LIGHT0, GL_SPECULAR, light_c[0]);
720 glLightfv(GL_LIGHT1, GL_POSITION, light_p[1]);
721 glLightfv(GL_LIGHT1, GL_DIFFUSE, light_c[1]);
722 glLightfv(GL_LIGHT1, GL_SPECULAR, light_c[1]);
725 static void game_draw_back(int pose, int d, float t)
731 glRotatef(game_rz * 2, view_e[2][0], view_e[2][1], view_e[2][2]);
732 glRotatef(game_rx * 2, view_e[0][0], view_e[0][1], view_e[0][2]);
735 glTranslatef(view_p[0], view_p[1] * d, view_p[2]);
737 if (config_get_d(CONFIG_BACKGROUND))
739 /* Draw all background layers back to front. */
741 sol_back(&back, BACK_DIST, FAR_DIST, t);
743 sol_back(&back, 0, BACK_DIST, t);
750 static void game_clip_refl(int d)
752 /* Fudge to eliminate the floor from reflection. */
754 GLdouble e[4], k = -0.00001;
761 glClipPlane(GL_CLIP_PLANE0, e);
764 static void game_clip_ball(int d, const float *p)
766 GLdouble r, c[3], pz[4], nz[4];
768 /* Compute the plane giving the front of the ball, as seen from view_p. */
774 pz[0] = view_p[0] - c[0];
775 pz[1] = view_p[1] - c[1];
776 pz[2] = view_p[2] - c[2];
778 r = sqrt(pz[0] * pz[0] + pz[1] * pz[1] + pz[2] * pz[2]);
783 pz[3] = -(pz[0] * c[0] +
787 /* Find the plane giving the back of the ball, as seen from view_p. */
794 /* Reflect these planes as necessary, and store them in the GL state. */
799 glClipPlane(GL_CLIP_PLANE1, nz);
800 glClipPlane(GL_CLIP_PLANE2, pz);
803 static void game_draw_fore(int pose, const float *M, int d, float t)
805 const float *ball_p = file.uv->p;
806 const float ball_r = file.uv->r;
810 /* Rotate the environment about the position of the ball. */
814 /* Compute clipping planes for reflection and ball facing. */
817 game_clip_ball(d, ball_p);
820 glEnable(GL_CLIP_PLANE0);
823 sol_draw(&file, 0, 1);
826 /* Draw the coins. */
828 game_draw_items(&file, t);
830 /* Draw the floor. */
832 sol_draw(&file, 0, 1);
834 /* Draw the ball shadow. */
836 if (d > 0 && config_get_d(CONFIG_SHADOW))
838 shad_draw_set(ball_p, ball_r);
845 game_draw_balls(&file, M, t);
848 /* Draw the particles and light columns. */
850 glEnable(GL_COLOR_MATERIAL);
851 glDisable(GL_LIGHTING);
852 glDepthMask(GL_FALSE);
854 glColor3f(1.0f, 1.0f, 1.0f);
856 sol_bill(&file, M, t);
857 part_draw_coin(M, t);
859 glDisable(GL_TEXTURE_2D);
861 game_draw_goals(&file, M, t);
862 game_draw_jumps(&file, M, t);
863 game_draw_swchs(&file);
865 glEnable(GL_TEXTURE_2D);
867 glColor3f(1.0f, 1.0f, 1.0f);
869 glDepthMask(GL_TRUE);
870 glEnable(GL_LIGHTING);
871 glDisable(GL_COLOR_MATERIAL);
874 glDisable(GL_CLIP_PLANE0);
879 void game_draw(int pose, float t)
881 float fov = view_fov;
883 if (jump_b) fov *= 2.f * fabsf(jump_dt - 0.5);
887 video_push_persp(fov, 0.1f, FAR_DIST);
890 float T[16], U[16], M[16], v[3];
892 /* Compute direct and reflected view bases. */
898 m_view(T, view_c, view_p, view_e[1]);
899 m_view(U, view_c, v, view_e[1]);
903 /* Apply current the view. */
905 v_sub(v, view_c, view_p);
907 glTranslatef(0.f, 0.f, -v_len(v));
909 glTranslatef(-view_c[0], -view_c[1], -view_c[2]);
911 if (reflective && config_get_d(CONFIG_REFLECTION))
913 glEnable(GL_STENCIL_TEST);
915 /* Draw the mirrors only into the stencil buffer. */
917 glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
918 glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
919 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
920 glDepthMask(GL_FALSE);
924 glDepthMask(GL_TRUE);
925 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
926 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
927 glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
929 /* Draw the scene reflected into color and depth buffers. */
934 glScalef(+1.0f, -1.0f, +1.0f);
937 game_draw_back(pose, -1, t);
938 game_draw_fore(pose, U, -1, t);
943 glDisable(GL_STENCIL_TEST);
946 /* Draw the scene normally. */
950 game_draw_back(pose, +1, t);
951 game_draw_fore(pose, T, +1, t);
956 /* Draw the fade overlay. */
962 /*---------------------------------------------------------------------------*/
964 void game_look(float phi, float theta)
966 view_c[0] = view_p[0] + fsinf(V_RAD(theta)) * fcosf(V_RAD(phi));
967 view_c[1] = view_p[1] + fsinf(V_RAD(phi));
968 view_c[2] = view_p[2] - fcosf(V_RAD(theta)) * fcosf(V_RAD(phi));
971 /*---------------------------------------------------------------------------*/
973 void game_kill_fade(void)
979 void game_step_fade(float dt)
981 if ((fade_k < 1.0f && fade_d > 0.0f) ||
982 (fade_k > 0.0f && fade_d < 0.0f))
983 fade_k += fade_d * dt;
997 void game_fade(float d)
1002 /*---------------------------------------------------------------------------*/
1004 const struct s_file *game_client_file(void)
1009 /*---------------------------------------------------------------------------*/