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 = NULL, *grad_name = NULL;
425 if (!sol_load_gl(&file, file_name,
426 config_get_d(CONFIG_TEXTURES),
427 config_get_d(CONFIG_SHADOW)))
428 return (client_state = 0);
430 reflective = sol_reflective(&file);
434 game_tilt_init(&tilt);
436 /* Initialize jump and goal states. */
444 /* Initialise the level, background, particles, fade, and view. */
453 for (i = 0; i < file.dc; i++)
455 char *k = file.av + file.dv[i].ai;
456 char *v = file.av + file.dv[i].aj;
458 if (strcmp(k, "back") == 0) back_name = v;
459 if (strcmp(k, "grad") == 0) grad_name = v;
461 if (strcmp(k, "version") == 0)
462 sscanf(v, "%d.%d", &version.x, &version.y);
466 * If the client map's version is 1, assume the map is compatible
467 * with the server. This ensures that 1.5.0 replays don't trigger
468 * bogus map compatibility warnings. (Post-1.5.0 replays will
469 * have CMD_MAP override this.)
472 game_compat_map = version.x == 1;
474 part_reset(GOAL_HEIGHT, JUMP_HEIGHT);
479 back_init(grad_name, config_get_d(CONFIG_GEOMETRY));
480 sol_load_gl(&back, back_name,
481 config_get_d(CONFIG_TEXTURES), 0);
486 void game_client_free(void)
498 /*---------------------------------------------------------------------------*/
502 return (int) (timer * 100.f);
510 int curr_status(void)
515 /*---------------------------------------------------------------------------*/
517 static void game_draw_balls(const struct s_file *fp,
518 const float *bill_M, float t)
520 float c[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
525 m_basis(ball_M, fp->uv[0].e[0], fp->uv[0].e[1], fp->uv[0].e[2]);
526 m_basis(pend_M, fp->uv[0].E[0], fp->uv[0].E[1], fp->uv[0].E[2]);
528 glPushAttrib(GL_LIGHTING_BIT);
531 glTranslatef(fp->uv[0].p[0],
532 fp->uv[0].p[1] + BALL_FUDGE,
534 glScalef(fp->uv[0].r,
539 ball_draw(ball_M, pend_M, bill_M, t);
545 static void game_draw_items(const struct s_file *fp, float t)
550 glPushAttrib(GL_LIGHTING_BIT);
552 item_push(ITEM_COIN);
554 for (hi = 0; hi < fp->hc; hi++)
556 if (fp->hv[hi].t == ITEM_COIN && fp->hv[hi].n > 0)
560 glTranslatef(fp->hv[hi].p[0],
563 glRotatef(r, 0.0f, 1.0f, 0.0f);
564 item_draw(&fp->hv[hi], r);
571 item_push(ITEM_SHRINK);
573 for (hi = 0; hi < fp->hc; hi++)
575 if (fp->hv[hi].t == ITEM_SHRINK)
579 glTranslatef(fp->hv[hi].p[0],
582 glRotatef(r, 0.0f, 1.0f, 0.0f);
583 item_draw(&fp->hv[hi], r);
590 item_push(ITEM_GROW);
592 for (hi = 0; hi < fp->hc; hi++)
594 if (fp->hv[hi].t == ITEM_GROW)
598 glTranslatef(fp->hv[hi].p[0],
601 glRotatef(r, 0.0f, 1.0f, 0.0f);
602 item_draw(&fp->hv[hi], r);
612 static void game_draw_goals(const struct s_file *fp, const float *M, float t)
618 /* Draw the goal particles. */
620 glEnable(GL_TEXTURE_2D);
622 for (zi = 0; zi < fp->zc; zi++)
626 glTranslatef(fp->zv[zi].p[0],
630 part_draw_goal(M, fp->zv[zi].r, goal_k, t);
635 glDisable(GL_TEXTURE_2D);
637 /* Draw the goal column. */
639 for (zi = 0; zi < fp->zc; zi++)
643 glTranslatef(fp->zv[zi].p[0],
647 glScalef(fp->zv[zi].r,
658 static void game_draw_jumps(const struct s_file *fp, const float *M, float t)
662 glEnable(GL_TEXTURE_2D);
664 for (ji = 0; ji < fp->jc; ji++)
668 glTranslatef(fp->jv[ji].p[0],
672 part_draw_jump(M, fp->jv[ji].r, 1.0f, t);
677 glDisable(GL_TEXTURE_2D);
679 for (ji = 0; ji < fp->jc; ji++)
683 glTranslatef(fp->jv[ji].p[0],
686 glScalef(fp->jv[ji].r,
696 static void game_draw_swchs(const struct s_file *fp)
700 for (xi = 0; xi < fp->xc; xi++)
707 glTranslatef(fp->xv[xi].p[0],
710 glScalef(fp->xv[xi].r,
714 swch_draw(fp->xv[xi].f, fp->xv[xi].e);
720 /*---------------------------------------------------------------------------*/
722 static void game_draw_tilt(int d)
724 const float *ball_p = file.uv->p;
726 /* Rotate the environment about the position of the ball. */
728 glTranslatef(+ball_p[0], +ball_p[1] * d, +ball_p[2]);
729 glRotatef(-tilt.rz * d, tilt.z[0], tilt.z[1], tilt.z[2]);
730 glRotatef(-tilt.rx * d, tilt.x[0], tilt.x[1], tilt.x[2]);
731 glTranslatef(-ball_p[0], -ball_p[1] * d, -ball_p[2]);
734 static void game_refl_all(void)
740 /* Draw the floor. */
747 /*---------------------------------------------------------------------------*/
749 static void game_draw_light(void)
751 const float light_p[2][4] = {
752 { -8.0f, +32.0f, -8.0f, 0.0f },
753 { +8.0f, +32.0f, +8.0f, 0.0f },
755 const float light_c[2][4] = {
756 { 1.0f, 0.8f, 0.8f, 1.0f },
757 { 0.8f, 1.0f, 0.8f, 1.0f },
760 /* Configure the lighting. */
763 glLightfv(GL_LIGHT0, GL_POSITION, light_p[0]);
764 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_c[0]);
765 glLightfv(GL_LIGHT0, GL_SPECULAR, light_c[0]);
768 glLightfv(GL_LIGHT1, GL_POSITION, light_p[1]);
769 glLightfv(GL_LIGHT1, GL_DIFFUSE, light_c[1]);
770 glLightfv(GL_LIGHT1, GL_SPECULAR, light_c[1]);
773 static void game_draw_back(int pose, int d, float t)
782 glRotatef(tilt.rz * 2, tilt.z[0], tilt.z[1], tilt.z[2]);
783 glRotatef(tilt.rx * 2, tilt.x[0], tilt.x[1], tilt.x[2]);
786 glTranslatef(view_p[0], view_p[1] * d, view_p[2]);
788 if (config_get_d(CONFIG_BACKGROUND))
790 /* Draw all background layers back to front. */
792 sol_back(&back, BACK_DIST, FAR_DIST, t);
794 sol_back(&back, 0, BACK_DIST, t);
801 static void game_clip_refl(int d)
803 /* Fudge to eliminate the floor from reflection. */
805 GLdouble e[4], k = -0.00001;
812 glClipPlane(GL_CLIP_PLANE0, e);
815 static void game_clip_ball(int d, const float *p)
817 GLdouble r, c[3], pz[4], nz[4], ny[4];
819 /* Compute the plane giving the front of the ball, as seen from view_p. */
825 pz[0] = view_p[0] - c[0];
826 pz[1] = view_p[1] - c[1];
827 pz[2] = view_p[2] - c[2];
829 r = sqrt(pz[0] * pz[0] + pz[1] * pz[1] + pz[2] * pz[2]);
834 pz[3] = -(pz[0] * c[0] +
838 /* Find the plane giving the back of the ball, as seen from view_p. */
845 /* Compute the plane giving the bottom of the ball. */
850 ny[3] = -(ny[0] * c[0] +
854 /* Reflect these planes as necessary, and store them in the GL state. */
860 glClipPlane(GL_CLIP_PLANE1, nz);
861 glClipPlane(GL_CLIP_PLANE2, pz);
862 glClipPlane(GL_CLIP_PLANE3, ny);
865 static void game_draw_fore(int pose, const float *M, int d, float t)
867 const float *ball_p = file.uv->p;
868 const float ball_r = file.uv->r;
872 /* Rotate the environment about the position of the ball. */
876 /* Compute clipping planes for reflection and ball facing. */
879 game_clip_ball(d, ball_p);
882 glEnable(GL_CLIP_PLANE0);
887 sol_draw(&file, 0, 1);
891 /* Draw the coins. */
893 game_draw_items(&file, t);
895 /* Draw the floor. */
897 sol_draw(&file, 0, 1);
903 /* Draw the ball shadow. */
905 if (d > 0 && config_get_d(CONFIG_SHADOW))
907 shad_draw_set(ball_p, ball_r);
914 game_draw_balls(&file, M, t);
919 /* Draw the particles and light columns. */
921 glEnable(GL_COLOR_MATERIAL);
922 glDisable(GL_LIGHTING);
923 glDepthMask(GL_FALSE);
925 glColor3f(1.0f, 1.0f, 1.0f);
927 sol_bill(&file, M, t);
928 part_draw_coin(M, t);
930 glDisable(GL_TEXTURE_2D);
932 game_draw_goals(&file, M, t);
933 game_draw_jumps(&file, M, t);
934 game_draw_swchs(&file);
936 glEnable(GL_TEXTURE_2D);
938 glColor3f(1.0f, 1.0f, 1.0f);
940 glDepthMask(GL_TRUE);
941 glEnable(GL_LIGHTING);
942 glDisable(GL_COLOR_MATERIAL);
945 glDisable(GL_CLIP_PLANE0);
950 void game_draw(int pose, float t)
952 float fov = (float) config_get_d(CONFIG_VIEW_FOV);
954 if (jump_b) fov *= 2.f * fabsf(jump_dt - 0.5);
958 video_push_persp(fov, 0.1f, FAR_DIST);
961 float T[16], U[16], M[16], v[3];
963 /* Compute direct and reflected view bases. */
969 m_view(T, view_c, view_p, view_e[1]);
970 m_view(U, view_c, v, view_e[1]);
974 /* Apply current the view. */
976 v_sub(v, view_c, view_p);
978 glTranslatef(0.f, 0.f, -v_len(v));
980 glTranslatef(-view_c[0], -view_c[1], -view_c[2]);
982 if (reflective && config_get_d(CONFIG_REFLECTION))
984 glEnable(GL_STENCIL_TEST);
986 /* Draw the mirrors only into the stencil buffer. */
988 glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
989 glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
990 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
991 glDepthMask(GL_FALSE);
995 glDepthMask(GL_TRUE);
996 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
997 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
998 glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
1000 /* Draw the scene reflected into color and depth buffers. */
1005 glScalef(+1.0f, -1.0f, +1.0f);
1008 game_draw_back(pose, -1, t);
1009 game_draw_fore(pose, U, -1, t);
1012 glFrontFace(GL_CCW);
1014 glDisable(GL_STENCIL_TEST);
1017 /* Draw the scene normally. */
1023 if (config_get_d(CONFIG_REFLECTION))
1025 /* Draw background while preserving reflections. */
1027 glEnable(GL_STENCIL_TEST);
1029 glStencilFunc(GL_NOTEQUAL, 1, 0xFFFFFFFF);
1030 game_draw_back(pose, +1, t);
1032 glDisable(GL_STENCIL_TEST);
1040 /* Draw background. */
1042 game_draw_back(pose, +1, t);
1045 * Draw mirrors, first fully opaque with a custom
1046 * material color, then blending normally with the
1047 * opaque surfaces using their original material
1048 * properties. (Keeps background from showing
1052 glEnable(GL_COLOR_MATERIAL);
1054 glColor4f(0.0, 0.0, 0.05, 1.0);
1056 glColor4f(1.0, 1.0, 1.0, 1.0);
1058 glDisable(GL_COLOR_MATERIAL);
1065 game_draw_back(pose, +1, t);
1069 game_draw_fore(pose, T, +1, t);
1074 /* Draw the fade overlay. */
1080 /*---------------------------------------------------------------------------*/
1082 void game_look(float phi, float theta)
1084 view_c[0] = view_p[0] + fsinf(V_RAD(theta)) * fcosf(V_RAD(phi));
1085 view_c[1] = view_p[1] + fsinf(V_RAD(phi));
1086 view_c[2] = view_p[2] - fcosf(V_RAD(theta)) * fcosf(V_RAD(phi));
1089 /*---------------------------------------------------------------------------*/
1091 void game_kill_fade(void)
1097 void game_step_fade(float dt)
1099 if ((fade_k < 1.0f && fade_d > 0.0f) ||
1100 (fade_k > 0.0f && fade_d < 0.0f))
1101 fade_k += fade_d * dt;
1115 void game_fade(float d)
1120 /*---------------------------------------------------------------------------*/
1122 const struct s_file *game_client_file(void)
1127 /*---------------------------------------------------------------------------*/