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 int game_compat_map; /* Client/server map compat flag */
42 /*---------------------------------------------------------------------------*/
44 static int client_state = 0;
46 static struct s_file file;
47 static struct s_file back;
49 static int reflective; /* Reflective geometry used? */
51 static float timer = 0.f; /* Clock time */
53 static int status = GAME_NONE; /* Outcome of the game */
55 static struct game_tilt tilt; /* Floor rotation */
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 */
79 } version; /* Current map version */
81 /*---------------------------------------------------------------------------*/
83 static void game_run_cmd(const union cmd *cmd)
85 static const float gup[] = { 0.0f, +9.8f, 0.0f };
86 static const float gdn[] = { 0.0f, -9.8f, 0.0f };
89 * Neverball <= 1.5.1 does not send explicit tilt axes, rotation
90 * happens directly around view vectors. So for compatibility if
91 * at the time of receiving tilt angles we have not yet received
92 * the tilt axes, we use the view vectors.
94 static int got_tilt_axes;
108 case CMD_END_OF_UPDATE:
118 /* Compute gravity for particle effects. */
120 if (status == GAME_GOAL)
121 game_tilt_grav(f, gup, &tilt);
123 game_tilt_grav(f, gdn, &tilt);
125 /* Step particle, goal and jump effects. */
129 dt = 1.0f / (float) ups;
131 if (goal_e && goal_k < 1.0f)
148 /* Allocate a new ball and mark it as the current ball. */
150 if ((up = realloc(file.uv, sizeof (*up) * (file.uc + 1))))
159 /* Allocate and initialise a new item. */
161 if ((hp = realloc(file.hv, sizeof (*hp) * (file.hc + 1))))
165 v_cpy(h.p, cmd->mkitem.p);
171 file.hv[file.hc] = h;
178 /* Set up particle effects and discard the item. */
180 assert(cmd->pkitem.hi < file.hc);
182 hp = &file.hv[cmd->pkitem.hi];
185 part_burst(hp->p, f);
191 case CMD_TILT_ANGLES:
193 game_tilt_axes(&tilt, view_e);
195 tilt.rx = cmd->tiltangles.x;
196 tilt.rz = cmd->tiltangles.z;
200 /* Play the sound, then free its file name. */
204 audio_play(cmd->sound.n, cmd->sound.a);
207 * FIXME Command memory management should be done
208 * elsewhere and done properly.
216 timer = cmd->timer.t;
220 status = cmd->status.t;
224 coins = cmd->coins.n;
238 file.bv[cmd->bodypath.bi].pi = cmd->bodypath.pi;
242 file.bv[cmd->bodytime.bi].t = cmd->bodytime.t;
247 * Enable the goal and make sure it's fully visible if
248 * this is the first update.
254 goal_k = first_update ? 1.0f : 0.0f;
259 file.xv[cmd->swchenter.xi].e = 1;
262 case CMD_SWCH_TOGGLE:
263 file.xv[cmd->swchtoggle.xi].f = !file.xv[cmd->swchtoggle.xi].f;
267 file.xv[cmd->swchexit.xi].e = 0;
270 case CMD_UPDATES_PER_SECOND:
274 case CMD_BALL_RADIUS:
275 file.uv[curr_ball].r = cmd->ballradius.r;
278 case CMD_CLEAR_ITEMS:
287 case CMD_CLEAR_BALLS:
296 case CMD_BALL_POSITION:
297 v_cpy(file.uv[curr_ball].p, cmd->ballpos.p);
301 v_cpy(file.uv[curr_ball].e[0], cmd->ballbasis.e[0]);
302 v_cpy(file.uv[curr_ball].e[1], cmd->ballbasis.e[1]);
304 v_crs(file.uv[curr_ball].e[2],
305 file.uv[curr_ball].e[0],
306 file.uv[curr_ball].e[1]);
309 case CMD_BALL_PEND_BASIS:
310 v_cpy(file.uv[curr_ball].E[0], cmd->ballpendbasis.E[0]);
311 v_cpy(file.uv[curr_ball].E[1], cmd->ballpendbasis.E[1]);
313 v_crs(file.uv[curr_ball].E[2],
314 file.uv[curr_ball].E[0],
315 file.uv[curr_ball].E[1]);
318 case CMD_VIEW_POSITION:
319 v_cpy(view_p, cmd->viewpos.p);
322 case CMD_VIEW_CENTER:
323 v_cpy(view_c, cmd->viewcenter.c);
327 v_cpy(view_e[0], cmd->viewbasis.e[0]);
328 v_cpy(view_e[1], cmd->viewbasis.e[1]);
330 v_crs(view_e[2], view_e[0], view_e[1]);
334 case CMD_CURRENT_BALL:
335 curr_ball = cmd->currball.ui;
339 file.pv[cmd->pathflag.pi].f = cmd->pathflag.f;
342 case CMD_STEP_SIMULATION:
344 * Simulate body motion.
346 * This is done on the client side due to replay file size
347 * concerns and isn't done as part of CMD_END_OF_UPDATE to
348 * match the server state as closely as possible. Body
349 * time is still synchronised with the server on a
350 * semi-regular basis and path indices are handled through
351 * CMD_BODY_PATH, thus this code doesn't need to be as
352 * sophisticated as sol_body_step.
355 dt = cmd->stepsim.dt;
357 for (i = 0; i < file.bc; i++)
359 struct s_body *bp = file.bv + i;
360 struct s_path *pp = file.pv + bp->pi;
362 if (bp->pi >= 0 && pp->f)
370 * Note if the loaded map matches the server's
371 * expectations. (No, this doesn't actually load a map,
372 * yet. Something else somewhere else does.)
376 game_compat_map = version.x == cmd->map.version.x;
381 v_cpy(tilt.x, cmd->tiltaxes.x);
382 v_cpy(tilt.z, cmd->tiltaxes.z);
392 void game_client_step(fs_file demo_fp)
396 while ((cmdp = game_proxy_deq()))
399 * Note: cmd_put is called first here because game_run_cmd
400 * frees some command struct members.
404 cmd_put(demo_fp, cmdp);
412 /*---------------------------------------------------------------------------*/
414 int game_client_init(const char *file_name)
416 char *back_name = "", *grad_name = "";
425 if (!sol_load_gl(&file, file_name, config_get_d(CONFIG_SHADOW)))
426 return (client_state = 0);
428 reflective = sol_reflective(&file);
432 game_tilt_init(&tilt);
434 /* Initialize jump and goal states. */
442 /* Initialise the level, background, particles, fade, and view. */
451 for (i = 0; i < file.dc; i++)
453 char *k = file.av + file.dv[i].ai;
454 char *v = file.av + file.dv[i].aj;
456 if (strcmp(k, "back") == 0) back_name = v;
457 if (strcmp(k, "grad") == 0) grad_name = v;
459 if (strcmp(k, "version") == 0)
460 sscanf(v, "%d.%d", &version.x, &version.y);
464 * If the client map's version is 1, assume the map is compatible
465 * with the server. This ensures that 1.5.0 replays don't trigger
466 * bogus map compatibility warnings. (Post-1.5.0 replays will
467 * have CMD_MAP override this.)
470 game_compat_map = version.x == 1;
472 part_reset(GOAL_HEIGHT, JUMP_HEIGHT);
477 back_init(grad_name, config_get_d(CONFIG_GEOMETRY));
478 sol_load_gl(&back, back_name, 0);
483 void game_client_free(void)
495 /*---------------------------------------------------------------------------*/
499 return (int) (timer * 100.f);
507 int curr_status(void)
512 /*---------------------------------------------------------------------------*/
514 static void game_draw_balls(const struct s_file *fp,
515 const float *bill_M, float t)
517 float c[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
522 m_basis(ball_M, fp->uv[0].e[0], fp->uv[0].e[1], fp->uv[0].e[2]);
523 m_basis(pend_M, fp->uv[0].E[0], fp->uv[0].E[1], fp->uv[0].E[2]);
525 glPushAttrib(GL_LIGHTING_BIT);
528 glTranslatef(fp->uv[0].p[0],
529 fp->uv[0].p[1] + BALL_FUDGE,
531 glScalef(fp->uv[0].r,
536 ball_draw(ball_M, pend_M, bill_M, t);
542 static void game_draw_items(const struct s_file *fp, float t)
547 glPushAttrib(GL_LIGHTING_BIT);
549 item_push(ITEM_COIN);
551 for (hi = 0; hi < fp->hc; hi++)
553 if (fp->hv[hi].t == ITEM_COIN && fp->hv[hi].n > 0)
557 glTranslatef(fp->hv[hi].p[0],
560 glRotatef(r, 0.0f, 1.0f, 0.0f);
561 item_draw(&fp->hv[hi], r);
568 item_push(ITEM_SHRINK);
570 for (hi = 0; hi < fp->hc; hi++)
572 if (fp->hv[hi].t == ITEM_SHRINK)
576 glTranslatef(fp->hv[hi].p[0],
579 glRotatef(r, 0.0f, 1.0f, 0.0f);
580 item_draw(&fp->hv[hi], r);
587 item_push(ITEM_GROW);
589 for (hi = 0; hi < fp->hc; hi++)
591 if (fp->hv[hi].t == ITEM_GROW)
595 glTranslatef(fp->hv[hi].p[0],
598 glRotatef(r, 0.0f, 1.0f, 0.0f);
599 item_draw(&fp->hv[hi], r);
609 static void game_draw_goals(const struct s_file *fp, const float *M, float t)
615 /* Draw the goal particles. */
617 glEnable(GL_TEXTURE_2D);
619 for (zi = 0; zi < fp->zc; zi++)
623 glTranslatef(fp->zv[zi].p[0],
627 part_draw_goal(M, fp->zv[zi].r, goal_k, t);
632 glDisable(GL_TEXTURE_2D);
634 /* Draw the goal column. */
636 for (zi = 0; zi < fp->zc; zi++)
640 glTranslatef(fp->zv[zi].p[0],
644 glScalef(fp->zv[zi].r,
655 static void game_draw_jumps(const struct s_file *fp, const float *M, float t)
659 glEnable(GL_TEXTURE_2D);
661 for (ji = 0; ji < fp->jc; ji++)
665 glTranslatef(fp->jv[ji].p[0],
669 part_draw_jump(M, fp->jv[ji].r, 1.0f, t);
674 glDisable(GL_TEXTURE_2D);
676 for (ji = 0; ji < fp->jc; ji++)
680 glTranslatef(fp->jv[ji].p[0],
683 glScalef(fp->jv[ji].r,
693 static void game_draw_swchs(const struct s_file *fp)
697 for (xi = 0; xi < fp->xc; xi++)
704 glTranslatef(fp->xv[xi].p[0],
707 glScalef(fp->xv[xi].r,
711 swch_draw(fp->xv[xi].f, fp->xv[xi].e);
717 /*---------------------------------------------------------------------------*/
719 static void game_draw_tilt(int d)
721 const float *ball_p = file.uv->p;
723 /* Rotate the environment about the position of the ball. */
725 glTranslatef(+ball_p[0], +ball_p[1] * d, +ball_p[2]);
726 glRotatef(-tilt.rz * d, tilt.z[0], tilt.z[1], tilt.z[2]);
727 glRotatef(-tilt.rx * d, tilt.x[0], tilt.x[1], tilt.x[2]);
728 glTranslatef(-ball_p[0], -ball_p[1] * d, -ball_p[2]);
731 static void game_refl_all(void)
737 /* Draw the floor. */
744 /*---------------------------------------------------------------------------*/
746 static void game_draw_light(void)
748 const float light_p[2][4] = {
749 { -8.0f, +32.0f, -8.0f, 0.0f },
750 { +8.0f, +32.0f, +8.0f, 0.0f },
752 const float light_c[2][4] = {
753 { 1.0f, 0.8f, 0.8f, 1.0f },
754 { 0.8f, 1.0f, 0.8f, 1.0f },
757 /* Configure the lighting. */
760 glLightfv(GL_LIGHT0, GL_POSITION, light_p[0]);
761 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_c[0]);
762 glLightfv(GL_LIGHT0, GL_SPECULAR, light_c[0]);
765 glLightfv(GL_LIGHT1, GL_POSITION, light_p[1]);
766 glLightfv(GL_LIGHT1, GL_DIFFUSE, light_c[1]);
767 glLightfv(GL_LIGHT1, GL_SPECULAR, light_c[1]);
770 static void game_draw_back(int pose, int d, float t)
779 glRotatef(tilt.rz * 2, tilt.z[0], tilt.z[1], tilt.z[2]);
780 glRotatef(tilt.rx * 2, tilt.x[0], tilt.x[1], tilt.x[2]);
783 glTranslatef(view_p[0], view_p[1] * d, view_p[2]);
785 if (config_get_d(CONFIG_BACKGROUND))
787 /* Draw all background layers back to front. */
789 sol_back(&back, BACK_DIST, FAR_DIST, t);
791 sol_back(&back, 0, BACK_DIST, t);
798 static void game_clip_refl(int d)
800 /* Fudge to eliminate the floor from reflection. */
802 GLdouble e[4], k = -0.00001;
809 glClipPlane(GL_CLIP_PLANE0, e);
812 static void game_clip_ball(int d, const float *p)
814 GLdouble r, c[3], pz[4], nz[4];
816 /* Compute the plane giving the front of the ball, as seen from view_p. */
822 pz[0] = view_p[0] - c[0];
823 pz[1] = view_p[1] - c[1];
824 pz[2] = view_p[2] - c[2];
826 r = sqrt(pz[0] * pz[0] + pz[1] * pz[1] + pz[2] * pz[2]);
831 pz[3] = -(pz[0] * c[0] +
835 /* Find the plane giving the back of the ball, as seen from view_p. */
842 /* Reflect these planes as necessary, and store them in the GL state. */
847 glClipPlane(GL_CLIP_PLANE1, nz);
848 glClipPlane(GL_CLIP_PLANE2, pz);
851 static void game_draw_fore(int pose, const float *M, int d, float t)
853 const float *ball_p = file.uv->p;
854 const float ball_r = file.uv->r;
858 /* Rotate the environment about the position of the ball. */
862 /* Compute clipping planes for reflection and ball facing. */
865 game_clip_ball(d, ball_p);
868 glEnable(GL_CLIP_PLANE0);
873 sol_draw(&file, 0, 1);
877 /* Draw the coins. */
879 game_draw_items(&file, t);
881 /* Draw the floor. */
883 sol_draw(&file, 0, 1);
889 /* Draw the ball shadow. */
891 if (d > 0 && config_get_d(CONFIG_SHADOW))
893 shad_draw_set(ball_p, ball_r);
900 game_draw_balls(&file, M, t);
905 /* Draw the particles and light columns. */
907 glEnable(GL_COLOR_MATERIAL);
908 glDisable(GL_LIGHTING);
909 glDepthMask(GL_FALSE);
911 glColor3f(1.0f, 1.0f, 1.0f);
913 sol_bill(&file, M, t);
914 part_draw_coin(M, t);
916 glDisable(GL_TEXTURE_2D);
918 game_draw_goals(&file, M, t);
919 game_draw_jumps(&file, M, t);
920 game_draw_swchs(&file);
922 glEnable(GL_TEXTURE_2D);
924 glColor3f(1.0f, 1.0f, 1.0f);
926 glDepthMask(GL_TRUE);
927 glEnable(GL_LIGHTING);
928 glDisable(GL_COLOR_MATERIAL);
931 glDisable(GL_CLIP_PLANE0);
936 void game_draw(int pose, float t)
938 float fov = (float) config_get_d(CONFIG_VIEW_FOV);
940 if (jump_b) fov *= 2.f * fabsf(jump_dt - 0.5);
944 video_push_persp(fov, 0.1f, FAR_DIST);
947 float T[16], U[16], M[16], v[3];
949 /* Compute direct and reflected view bases. */
955 m_view(T, view_c, view_p, view_e[1]);
956 m_view(U, view_c, v, view_e[1]);
960 /* Apply the current view. */
962 v_sub(v, view_c, view_p);
964 glTranslatef(0.f, 0.f, -v_len(v));
966 glTranslatef(-view_c[0], -view_c[1], -view_c[2]);
968 if (reflective && config_get_d(CONFIG_REFLECTION))
970 glEnable(GL_STENCIL_TEST);
972 /* Draw the mirrors only into the stencil buffer. */
974 glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
975 glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
976 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
977 glDepthMask(GL_FALSE);
981 glDepthMask(GL_TRUE);
982 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
983 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
984 glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
986 /* Draw the scene reflected into color and depth buffers. */
991 glScalef(+1.0f, -1.0f, +1.0f);
994 game_draw_back(pose, -1, t);
995 game_draw_fore(pose, U, -1, t);
1000 glDisable(GL_STENCIL_TEST);
1003 /* Draw the scene normally. */
1009 if (config_get_d(CONFIG_REFLECTION))
1011 /* Draw background while preserving reflections. */
1013 glEnable(GL_STENCIL_TEST);
1015 glStencilFunc(GL_NOTEQUAL, 1, 0xFFFFFFFF);
1016 game_draw_back(pose, +1, t);
1018 glDisable(GL_STENCIL_TEST);
1026 /* Draw background. */
1028 game_draw_back(pose, +1, t);
1031 * Draw mirrors, first fully opaque with a custom
1032 * material color, then blending normally with the
1033 * opaque surfaces using their original material
1034 * properties. (Keeps background from showing
1038 glEnable(GL_COLOR_MATERIAL);
1040 glColor4f(0.0, 0.0, 0.05, 1.0);
1042 glColor4f(1.0, 1.0, 1.0, 1.0);
1044 glDisable(GL_COLOR_MATERIAL);
1051 game_draw_back(pose, +1, t);
1055 game_draw_fore(pose, T, +1, t);
1060 /* Draw the fade overlay. */
1066 /*---------------------------------------------------------------------------*/
1068 void game_look(float phi, float theta)
1070 view_c[0] = view_p[0] + fsinf(V_RAD(theta)) * fcosf(V_RAD(phi));
1071 view_c[1] = view_p[1] + fsinf(V_RAD(phi));
1072 view_c[2] = view_p[2] - fcosf(V_RAD(theta)) * fcosf(V_RAD(phi));
1075 /*---------------------------------------------------------------------------*/
1077 void game_kill_fade(void)
1083 void game_step_fade(float dt)
1085 if ((fade_k < 1.0f && fade_d > 0.0f) ||
1086 (fade_k > 0.0f && fade_d < 0.0f))
1087 fade_k += fade_d * dt;
1101 void game_fade(float d)
1106 /*---------------------------------------------------------------------------*/
1108 const struct s_file *game_client_file(void)
1113 /*---------------------------------------------------------------------------*/