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"
35 #include "game_draw.h"
39 /*---------------------------------------------------------------------------*/
41 int game_compat_map; /* Client/server map compat flag */
43 /*---------------------------------------------------------------------------*/
45 static struct game_draw dr;
47 static float timer = 0.0f; /* Clock time */
48 static int status = GAME_NONE; /* Outcome of the game */
49 static int coins = 0; /* Collected coins */
51 static int ups; /* Updates per second */
52 static int first_update; /* First update flag */
53 static int curr_ball; /* Current ball index */
58 } version; /* Current map version */
60 /*---------------------------------------------------------------------------*/
62 static void game_run_cmd(const union cmd *cmd)
65 * Neverball <= 1.5.1 does not send explicit tilt axes, rotation
66 * happens directly around view vectors. So for compatibility if
67 * at the time of receiving tilt angles we have not yet received
68 * the tilt axes, we use the view vectors.
70 static int got_tilt_axes;
84 case CMD_END_OF_UPDATE:
94 /* Compute gravity for particle effects. */
96 if (status == GAME_GOAL)
97 game_tilt_grav(v, GRAVITY_UP, &dr.tilt);
99 game_tilt_grav(v, GRAVITY_DN, &dr.tilt);
101 /* Step particle, goal and jump effects. */
105 dt = 1.0f / (float) ups;
107 if (dr.goal_e && dr.goal_k < 1.0f)
114 if (1.0f < dr.jump_dt)
124 /* Allocate a new ball and mark it as the current ball. */
126 if ((up = realloc(dr.file.uv, sizeof (*up) * (dr.file.uc + 1))))
129 curr_ball = dr.file.uc;
135 /* Allocate and initialise a new item. */
137 if ((hp = realloc(dr.file.hv, sizeof (*hp) * (dr.file.hc + 1))))
141 v_cpy(h.p, cmd->mkitem.p);
147 dr.file.hv[dr.file.hc] = h;
154 /* Set up particle effects and discard the item. */
156 assert(cmd->pkitem.hi < dr.file.hc);
158 hp = &dr.file.hv[cmd->pkitem.hi];
161 part_burst(hp->p, v);
167 case CMD_TILT_ANGLES:
169 game_tilt_axes(&dr.tilt, dr.view.e);
171 dr.tilt.rx = cmd->tiltangles.x;
172 dr.tilt.rz = cmd->tiltangles.z;
176 /* Play the sound. */
179 audio_play(cmd->sound.n, cmd->sound.a);
184 timer = cmd->timer.t;
188 status = cmd->status.t;
192 coins = cmd->coins.n;
206 dr.file.bv[cmd->bodypath.bi].pi = cmd->bodypath.pi;
210 dr.file.bv[cmd->bodytime.bi].t = cmd->bodytime.t;
215 * Enable the goal and make sure it's fully visible if
216 * this is the first update.
222 dr.goal_k = first_update ? 1.0f : 0.0f;
227 dr.file.xv[cmd->swchenter.xi].e = 1;
230 case CMD_SWCH_TOGGLE:
231 dr.file.xv[cmd->swchtoggle.xi].f = !dr.file.xv[cmd->swchtoggle.xi].f;
235 dr.file.xv[cmd->swchexit.xi].e = 0;
238 case CMD_UPDATES_PER_SECOND:
242 case CMD_BALL_RADIUS:
243 dr.file.uv[curr_ball].r = cmd->ballradius.r;
246 case CMD_CLEAR_ITEMS:
255 case CMD_CLEAR_BALLS:
264 case CMD_BALL_POSITION:
265 v_cpy(dr.file.uv[curr_ball].p, cmd->ballpos.p);
269 v_cpy(dr.file.uv[curr_ball].e[0], cmd->ballbasis.e[0]);
270 v_cpy(dr.file.uv[curr_ball].e[1], cmd->ballbasis.e[1]);
272 v_crs(dr.file.uv[curr_ball].e[2],
273 dr.file.uv[curr_ball].e[0],
274 dr.file.uv[curr_ball].e[1]);
277 case CMD_BALL_PEND_BASIS:
278 v_cpy(dr.file.uv[curr_ball].E[0], cmd->ballpendbasis.E[0]);
279 v_cpy(dr.file.uv[curr_ball].E[1], cmd->ballpendbasis.E[1]);
281 v_crs(dr.file.uv[curr_ball].E[2],
282 dr.file.uv[curr_ball].E[0],
283 dr.file.uv[curr_ball].E[1]);
286 case CMD_VIEW_POSITION:
287 v_cpy(dr.view.p, cmd->viewpos.p);
290 case CMD_VIEW_CENTER:
291 v_cpy(dr.view.c, cmd->viewcenter.c);
295 v_cpy(dr.view.e[0], cmd->viewbasis.e[0]);
296 v_cpy(dr.view.e[1], cmd->viewbasis.e[1]);
298 v_crs(dr.view.e[2], dr.view.e[0], dr.view.e[1]);
302 case CMD_CURRENT_BALL:
303 curr_ball = cmd->currball.ui;
307 dr.file.pv[cmd->pathflag.pi].f = cmd->pathflag.f;
310 case CMD_STEP_SIMULATION:
312 * Simulate body motion.
314 * This is done on the client side due to replay file size
315 * concerns and isn't done as part of CMD_END_OF_UPDATE to
316 * match the server state as closely as possible. Body
317 * time is still synchronised with the server on a
318 * semi-regular basis and path indices are handled through
319 * CMD_BODY_PATH, thus this code doesn't need to be as
320 * sophisticated as sol_body_step.
323 dt = cmd->stepsim.dt;
325 for (i = 0; i < dr.file.bc; i++)
327 struct s_body *bp = dr.file.bv + i;
328 struct s_path *pp = dr.file.pv + bp->pi;
330 if (bp->pi >= 0 && pp->f)
337 * Note a version (mis-)match between the loaded map and
338 * what the server has. (This doesn't actually load a
342 game_compat_map = version.x == cmd->map.version.x;
347 v_cpy(dr.tilt.x, cmd->tiltaxes.x);
348 v_cpy(dr.tilt.z, cmd->tiltaxes.z);
358 void game_client_sync(fs_file demo_fp)
362 while ((cmdp = game_proxy_deq()))
365 cmd_put(demo_fp, cmdp);
373 /*---------------------------------------------------------------------------*/
375 int game_client_init(const char *file_name)
377 char *back_name = "", *grad_name = "";
386 if (!sol_load_gl(&dr.file, file_name, config_get_d(CONFIG_SHADOW)))
387 return (dr.state = 0);
389 dr.reflective = sol_reflective(&dr.file);
393 game_tilt_init(&dr.tilt);
395 /* Initialize jump and goal states. */
403 /* Initialise the level, background, particles, fade, and view. */
412 for (i = 0; i < dr.file.dc; i++)
414 char *k = dr.file.av + dr.file.dv[i].ai;
415 char *v = dr.file.av + dr.file.dv[i].aj;
417 if (strcmp(k, "back") == 0) back_name = v;
418 if (strcmp(k, "grad") == 0) grad_name = v;
420 if (strcmp(k, "version") == 0)
421 sscanf(v, "%d.%d", &version.x, &version.y);
425 * If the version of the loaded map is 1, assume we have a version
426 * match with the server. In this way 1.5.0 replays don't trigger
427 * bogus map compatibility warnings. Post-1.5.0 replays will have
428 * CMD_MAP override this.
431 game_compat_map = version.x == 1;
433 part_reset(GOAL_HEIGHT, JUMP_HEIGHT);
438 back_init(grad_name, config_get_d(CONFIG_GEOMETRY));
439 sol_load_gl(&dr.back, back_name, 0);
444 void game_client_free(void)
449 sol_free_gl(&dr.file);
450 sol_free_gl(&dr.back);
456 /*---------------------------------------------------------------------------*/
458 void game_client_draw(int pose, float t)
460 game_draw(&dr, pose, t);
463 /*---------------------------------------------------------------------------*/
467 return (int) (timer * 100.f);
475 int curr_status(void)
480 /*---------------------------------------------------------------------------*/
483 void game_look(float phi, float theta)
485 dr.view.c[0] = dr.view.p[0] + fsinf(V_RAD(theta)) * fcosf(V_RAD(phi));
486 dr.view.c[1] = dr.view.p[1] + fsinf(V_RAD(phi));
487 dr.view.c[2] = dr.view.p[2] - fcosf(V_RAD(theta)) * fcosf(V_RAD(phi));
490 /*---------------------------------------------------------------------------*/
492 void game_kill_fade(void)
498 void game_step_fade(float dt)
500 if ((dr.fade_k < 1.0f && dr.fade_d > 0.0f) ||
501 (dr.fade_k > 0.0f && dr.fade_d < 0.0f))
502 dr.fade_k += dr.fade_d * dt;
504 if (dr.fade_k < 0.0f)
509 if (dr.fade_k > 1.0f)
516 void game_fade(float d)
521 /*---------------------------------------------------------------------------*/
523 void game_client_fly(float k)
525 game_view_fly(&dr.view, &dr.file, k);
528 /*---------------------------------------------------------------------------*/