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_a; /* Ideal view rotation about Y axis */
58 static float view_fov; /* Field of view */
60 static float view_c[3]; /* Current view center */
61 static float view_p[3]; /* Current view position */
62 static float view_e[3][3]; /* Current view reference frame */
64 static int coins = 0; /* Collected coins */
65 static int goal_e = 0; /* Goal enabled flag */
66 static float goal_k = 0; /* Goal animation */
68 static int jump_e = 1; /* Jumping enabled flag */
69 static int jump_b = 0; /* Jump-in-progress flag */
70 static float jump_dt; /* Jump duration */
72 static float fade_k = 0.0; /* Fade in/out level */
73 static float fade_d = 0.0; /* Fade in/out direction */
75 static int ups; /* Updates per second */
76 static int first_update; /* First update flag */
77 static int curr_ball; /* Current ball index */
82 } version; /* Current map version */
84 /*---------------------------------------------------------------------------*/
86 static void game_run_cmd(const union cmd *cmd)
88 static const float gup[] = { 0.0f, +9.8f, 0.0f };
89 static const float gdn[] = { 0.0f, -9.8f, 0.0f };
92 * Neverball <= 1.5.1 does not send explicit tilt axes, rotation
93 * happens directly around view vectors. So for compatibility if
94 * at the time of receiving tilt angles we have not yet received
95 * the tilt axes, we use the view vectors.
97 static int got_tilt_axes;
111 case CMD_END_OF_UPDATE:
121 /* Compute gravity for particle effects. */
123 if (status == GAME_GOAL)
124 game_tilt_grav(f, gup, &tilt);
126 game_tilt_grav(f, gdn, &tilt);
128 /* Step particle, goal and jump effects. */
132 dt = 1.0f / (float) ups;
134 if (goal_e && goal_k < 1.0f)
151 /* Allocate a new ball and mark it as the current ball. */
153 if ((up = realloc(file.uv, sizeof (*up) * (file.uc + 1))))
162 /* Allocate and initialise a new item. */
164 if ((hp = realloc(file.hv, sizeof (*hp) * (file.hc + 1))))
168 v_cpy(h.p, cmd->mkitem.p);
174 file.hv[file.hc] = h;
181 /* Set up particle effects and discard the item. */
183 assert(cmd->pkitem.hi < file.hc);
185 hp = &file.hv[cmd->pkitem.hi];
188 part_burst(hp->p, f);
194 case CMD_TILT_ANGLES:
196 game_tilt_axes(&tilt, view_e);
198 tilt.rx = cmd->tiltangles.x;
199 tilt.rz = cmd->tiltangles.z;
203 /* Play the sound, then free its file name. */
207 audio_play(cmd->sound.n, cmd->sound.a);
210 * FIXME Command memory management should be done
211 * elsewhere and done properly.
219 timer = cmd->timer.t;
223 status = cmd->status.t;
227 coins = cmd->coins.n;
241 file.bv[cmd->bodypath.bi].pi = cmd->bodypath.pi;
245 file.bv[cmd->bodytime.bi].t = cmd->bodytime.t;
250 * Enable the goal and make sure it's fully visible if
251 * this is the first update.
257 goal_k = first_update ? 1.0f : 0.0f;
262 file.xv[cmd->swchenter.xi].e = 1;
265 case CMD_SWCH_TOGGLE:
266 file.xv[cmd->swchtoggle.xi].f = !file.xv[cmd->swchtoggle.xi].f;
270 file.xv[cmd->swchexit.xi].e = 0;
273 case CMD_UPDATES_PER_SECOND:
277 case CMD_BALL_RADIUS:
278 file.uv[curr_ball].r = cmd->ballradius.r;
281 case CMD_CLEAR_ITEMS:
290 case CMD_CLEAR_BALLS:
299 case CMD_BALL_POSITION:
300 v_cpy(file.uv[curr_ball].p, cmd->ballpos.p);
304 v_cpy(file.uv[curr_ball].e[0], cmd->ballbasis.e[0]);
305 v_cpy(file.uv[curr_ball].e[1], cmd->ballbasis.e[1]);
307 v_crs(file.uv[curr_ball].e[2],
308 file.uv[curr_ball].e[0],
309 file.uv[curr_ball].e[1]);
312 case CMD_BALL_PEND_BASIS:
313 v_cpy(file.uv[curr_ball].E[0], cmd->ballpendbasis.E[0]);
314 v_cpy(file.uv[curr_ball].E[1], cmd->ballpendbasis.E[1]);
316 v_crs(file.uv[curr_ball].E[2],
317 file.uv[curr_ball].E[0],
318 file.uv[curr_ball].E[1]);
321 case CMD_VIEW_POSITION:
322 v_cpy(view_p, cmd->viewpos.p);
325 case CMD_VIEW_CENTER:
326 v_cpy(view_c, cmd->viewcenter.c);
330 v_cpy(view_e[0], cmd->viewbasis.e[0]);
331 v_cpy(view_e[1], cmd->viewbasis.e[1]);
333 v_crs(view_e[2], view_e[0], view_e[1]);
335 view_a = V_DEG(fatan2f(view_e[2][0], view_e[2][2]));
339 case CMD_CURRENT_BALL:
340 curr_ball = cmd->currball.ui;
344 file.pv[cmd->pathflag.pi].f = cmd->pathflag.f;
347 case CMD_STEP_SIMULATION:
349 * Simulate body motion.
351 * This is done on the client side due to replay file size
352 * concerns and isn't done as part of CMD_END_OF_UPDATE to
353 * match the server state as closely as possible. Body
354 * time is still synchronised with the server on a
355 * semi-regular basis and path indices are handled through
356 * CMD_BODY_PATH, thus this code doesn't need to be as
357 * sophisticated as sol_body_step.
360 dt = cmd->stepsim.dt;
362 for (i = 0; i < file.bc; i++)
364 struct s_body *bp = file.bv + i;
365 struct s_path *pp = file.pv + bp->pi;
367 if (bp->pi >= 0 && pp->f)
375 * Note if the loaded map matches the server's
376 * expectations. (No, this doesn't actually load a map,
377 * yet. Something else somewhere else does.)
381 game_compat_map = version.x == cmd->map.version.x;
386 v_cpy(tilt.x, cmd->tiltaxes.x);
387 v_cpy(tilt.z, cmd->tiltaxes.z);
397 void game_client_step(fs_file demo_fp)
401 while ((cmdp = game_proxy_deq()))
404 * Note: cmd_put is called first here because game_run_cmd
405 * frees some command struct members.
409 cmd_put(demo_fp, cmdp);
417 /*---------------------------------------------------------------------------*/
419 int game_client_init(const char *file_name)
421 char *back_name = NULL, *grad_name = NULL;
430 if (!sol_load_gl(&file, file_name,
431 config_get_d(CONFIG_TEXTURES),
432 config_get_d(CONFIG_SHADOW)))
433 return (client_state = 0);
435 reflective = sol_reflective(&file);
439 game_tilt_init(&tilt);
441 /* Initialize jump and goal states. */
449 /* Initialise the level, background, particles, fade, and view. */
458 for (i = 0; i < file.dc; i++)
460 char *k = file.av + file.dv[i].ai;
461 char *v = file.av + file.dv[i].aj;
463 if (strcmp(k, "back") == 0) back_name = v;
464 if (strcmp(k, "grad") == 0) grad_name = v;
466 if (strcmp(k, "version") == 0)
467 sscanf(v, "%d.%d", &version.x, &version.y);
471 * If the client map's version is 1, assume the map is compatible
472 * with the server. This ensures that 1.5.0 replays don't trigger
473 * bogus map compatibility warnings. (Post-1.5.0 replays will
474 * have CMD_MAP override this.)
477 game_compat_map = version.x == 1;
479 part_reset(GOAL_HEIGHT, JUMP_HEIGHT);
481 view_fov = (float) config_get_d(CONFIG_VIEW_FOV);
486 back_init(grad_name, config_get_d(CONFIG_GEOMETRY));
487 sol_load_gl(&back, back_name,
488 config_get_d(CONFIG_TEXTURES), 0);
493 void game_client_free(void)
505 /*---------------------------------------------------------------------------*/
509 return (int) (timer * 100.f);
517 int curr_status(void)
522 /*---------------------------------------------------------------------------*/
524 static void game_draw_balls(const struct s_file *fp,
525 const float *bill_M, float t)
527 float c[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
532 m_basis(ball_M, fp->uv[0].e[0], fp->uv[0].e[1], fp->uv[0].e[2]);
533 m_basis(pend_M, fp->uv[0].E[0], fp->uv[0].E[1], fp->uv[0].E[2]);
535 glPushAttrib(GL_LIGHTING_BIT);
538 glTranslatef(fp->uv[0].p[0],
539 fp->uv[0].p[1] + BALL_FUDGE,
541 glScalef(fp->uv[0].r,
546 ball_draw(ball_M, pend_M, bill_M, t);
552 static void game_draw_items(const struct s_file *fp, float t)
557 glPushAttrib(GL_LIGHTING_BIT);
559 item_push(ITEM_COIN);
561 for (hi = 0; hi < fp->hc; hi++)
563 if (fp->hv[hi].t == ITEM_COIN && fp->hv[hi].n > 0)
567 glTranslatef(fp->hv[hi].p[0],
570 glRotatef(r, 0.0f, 1.0f, 0.0f);
571 item_draw(&fp->hv[hi], r);
578 item_push(ITEM_SHRINK);
580 for (hi = 0; hi < fp->hc; hi++)
582 if (fp->hv[hi].t == ITEM_SHRINK)
586 glTranslatef(fp->hv[hi].p[0],
589 glRotatef(r, 0.0f, 1.0f, 0.0f);
590 item_draw(&fp->hv[hi], r);
597 item_push(ITEM_GROW);
599 for (hi = 0; hi < fp->hc; hi++)
601 if (fp->hv[hi].t == ITEM_GROW)
605 glTranslatef(fp->hv[hi].p[0],
608 glRotatef(r, 0.0f, 1.0f, 0.0f);
609 item_draw(&fp->hv[hi], r);
619 static void game_draw_goals(const struct s_file *fp, const float *M, float t)
625 /* Draw the goal particles. */
627 glEnable(GL_TEXTURE_2D);
629 for (zi = 0; zi < fp->zc; zi++)
633 glTranslatef(fp->zv[zi].p[0],
637 part_draw_goal(M, fp->zv[zi].r, goal_k, t);
642 glDisable(GL_TEXTURE_2D);
644 /* Draw the goal column. */
646 for (zi = 0; zi < fp->zc; zi++)
650 glTranslatef(fp->zv[zi].p[0],
654 glScalef(fp->zv[zi].r,
665 static void game_draw_jumps(const struct s_file *fp, const float *M, float t)
669 glEnable(GL_TEXTURE_2D);
671 for (ji = 0; ji < fp->jc; ji++)
675 glTranslatef(fp->jv[ji].p[0],
679 part_draw_jump(M, fp->jv[ji].r, 1.0f, t);
684 glDisable(GL_TEXTURE_2D);
686 for (ji = 0; ji < fp->jc; ji++)
690 glTranslatef(fp->jv[ji].p[0],
693 glScalef(fp->jv[ji].r,
703 static void game_draw_swchs(const struct s_file *fp)
707 for (xi = 0; xi < fp->xc; xi++)
714 glTranslatef(fp->xv[xi].p[0],
717 glScalef(fp->xv[xi].r,
721 swch_draw(fp->xv[xi].f, fp->xv[xi].e);
727 /*---------------------------------------------------------------------------*/
729 static void game_draw_tilt(int d)
731 const float *ball_p = file.uv->p;
733 /* Rotate the environment about the position of the ball. */
735 glTranslatef(+ball_p[0], +ball_p[1] * d, +ball_p[2]);
736 glRotatef(-tilt.rz * d, tilt.z[0], tilt.z[1], tilt.z[2]);
737 glRotatef(-tilt.rx * d, tilt.x[0], tilt.x[1], tilt.x[2]);
738 glTranslatef(-ball_p[0], -ball_p[1] * d, -ball_p[2]);
741 static void game_refl_all(void)
747 /* Draw the floor. */
754 /*---------------------------------------------------------------------------*/
756 static void game_draw_light(void)
758 const float light_p[2][4] = {
759 { -8.0f, +32.0f, -8.0f, 0.0f },
760 { +8.0f, +32.0f, +8.0f, 0.0f },
762 const float light_c[2][4] = {
763 { 1.0f, 0.8f, 0.8f, 1.0f },
764 { 0.8f, 1.0f, 0.8f, 1.0f },
767 /* Configure the lighting. */
770 glLightfv(GL_LIGHT0, GL_POSITION, light_p[0]);
771 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_c[0]);
772 glLightfv(GL_LIGHT0, GL_SPECULAR, light_c[0]);
775 glLightfv(GL_LIGHT1, GL_POSITION, light_p[1]);
776 glLightfv(GL_LIGHT1, GL_DIFFUSE, light_c[1]);
777 glLightfv(GL_LIGHT1, GL_SPECULAR, light_c[1]);
780 static void game_draw_back(int pose, int d, float t)
789 glRotatef(tilt.rz * 2, tilt.z[0], tilt.z[1], tilt.z[2]);
790 glRotatef(tilt.rx * 2, tilt.x[0], tilt.x[1], tilt.x[2]);
793 glTranslatef(view_p[0], view_p[1] * d, view_p[2]);
795 if (config_get_d(CONFIG_BACKGROUND))
797 /* Draw all background layers back to front. */
799 sol_back(&back, BACK_DIST, FAR_DIST, t);
801 sol_back(&back, 0, BACK_DIST, t);
808 static void game_clip_refl(int d)
810 /* Fudge to eliminate the floor from reflection. */
812 GLdouble e[4], k = -0.00001;
819 glClipPlane(GL_CLIP_PLANE0, e);
822 static void game_clip_ball(int d, const float *p)
824 GLdouble r, c[3], pz[4], nz[4];
826 /* Compute the plane giving the front of the ball, as seen from view_p. */
832 pz[0] = view_p[0] - c[0];
833 pz[1] = view_p[1] - c[1];
834 pz[2] = view_p[2] - c[2];
836 r = sqrt(pz[0] * pz[0] + pz[1] * pz[1] + pz[2] * pz[2]);
841 pz[3] = -(pz[0] * c[0] +
845 /* Find the plane giving the back of the ball, as seen from view_p. */
852 /* Reflect these planes as necessary, and store them in the GL state. */
857 glClipPlane(GL_CLIP_PLANE1, nz);
858 glClipPlane(GL_CLIP_PLANE2, pz);
861 static void game_draw_fore(int pose, const float *M, int d, float t)
863 const float *ball_p = file.uv->p;
864 const float ball_r = file.uv->r;
868 /* Rotate the environment about the position of the ball. */
872 /* Compute clipping planes for reflection and ball facing. */
875 game_clip_ball(d, ball_p);
878 glEnable(GL_CLIP_PLANE0);
883 sol_draw(&file, 0, 1);
887 /* Draw the coins. */
889 game_draw_items(&file, t);
891 /* Draw the floor. */
893 sol_draw(&file, 0, 1);
899 /* Draw the ball shadow. */
901 if (d > 0 && config_get_d(CONFIG_SHADOW))
903 shad_draw_set(ball_p, ball_r);
910 game_draw_balls(&file, M, t);
915 /* Draw the particles and light columns. */
917 glEnable(GL_COLOR_MATERIAL);
918 glDisable(GL_LIGHTING);
919 glDepthMask(GL_FALSE);
921 glColor3f(1.0f, 1.0f, 1.0f);
923 sol_bill(&file, M, t);
924 part_draw_coin(M, t);
926 glDisable(GL_TEXTURE_2D);
928 game_draw_goals(&file, M, t);
929 game_draw_jumps(&file, M, t);
930 game_draw_swchs(&file);
932 glEnable(GL_TEXTURE_2D);
934 glColor3f(1.0f, 1.0f, 1.0f);
936 glDepthMask(GL_TRUE);
937 glEnable(GL_LIGHTING);
938 glDisable(GL_COLOR_MATERIAL);
941 glDisable(GL_CLIP_PLANE0);
946 void game_draw(int pose, float t)
948 float fov = view_fov;
950 if (jump_b) fov *= 2.f * fabsf(jump_dt - 0.5);
954 video_push_persp(fov, 0.1f, FAR_DIST);
957 float T[16], U[16], M[16], v[3];
959 /* Compute direct and reflected view bases. */
965 m_view(T, view_c, view_p, view_e[1]);
966 m_view(U, view_c, v, view_e[1]);
970 /* Apply current the view. */
972 v_sub(v, view_c, view_p);
974 glTranslatef(0.f, 0.f, -v_len(v));
976 glTranslatef(-view_c[0], -view_c[1], -view_c[2]);
978 if (reflective && config_get_d(CONFIG_REFLECTION))
980 glEnable(GL_STENCIL_TEST);
982 /* Draw the mirrors only into the stencil buffer. */
984 glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
985 glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
986 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
987 glDepthMask(GL_FALSE);
991 glDepthMask(GL_TRUE);
992 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
993 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
994 glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
996 /* Draw the scene reflected into color and depth buffers. */
1001 glScalef(+1.0f, -1.0f, +1.0f);
1004 game_draw_back(pose, -1, t);
1005 game_draw_fore(pose, U, -1, t);
1008 glFrontFace(GL_CCW);
1010 glDisable(GL_STENCIL_TEST);
1013 /* Draw the scene normally. */
1017 game_draw_back(pose, +1, t);
1018 game_draw_fore(pose, T, +1, t);
1023 /* Draw the fade overlay. */
1029 /*---------------------------------------------------------------------------*/
1031 void game_look(float phi, float theta)
1033 view_c[0] = view_p[0] + fsinf(V_RAD(theta)) * fcosf(V_RAD(phi));
1034 view_c[1] = view_p[1] + fsinf(V_RAD(phi));
1035 view_c[2] = view_p[2] - fcosf(V_RAD(theta)) * fcosf(V_RAD(phi));
1038 /*---------------------------------------------------------------------------*/
1040 void game_kill_fade(void)
1046 void game_step_fade(float dt)
1048 if ((fade_k < 1.0f && fade_d > 0.0f) ||
1049 (fade_k > 0.0f && fade_d < 0.0f))
1050 fade_k += fade_d * dt;
1064 void game_fade(float d)
1069 /*---------------------------------------------------------------------------*/
1071 const struct s_file *game_client_file(void)
1076 /*---------------------------------------------------------------------------*/