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 */
61 * Neverball <= 1.5.1 does not send explicit tilt axes, rotation
62 * happens directly around view vectors. So for compatibility if at
63 * the time of receiving tilt angles we have not yet received the tilt
64 * axes, we use the view vectors.
67 static int got_tilt_axes;
69 /*---------------------------------------------------------------------------*/
71 static void game_run_cmd(const union cmd *cmd)
75 struct s_file *fp = &dr.file;
85 case CMD_END_OF_UPDATE:
95 /* Compute gravity for particle effects. */
97 if (status == GAME_GOAL)
98 game_tilt_grav(v, GRAVITY_UP, &dr.tilt);
100 game_tilt_grav(v, GRAVITY_DN, &dr.tilt);
102 /* Step particle, goal and jump effects. */
106 dt = 1.0f / (float) ups;
108 if (dr.goal_e && dr.goal_k < 1.0f)
115 if (1.0f < dr.jump_dt)
125 /* Allocate a new ball and mark it as the current ball. */
127 if ((up = realloc(fp->uv, sizeof (*up) * (fp->uc + 1))))
136 /* Allocate and initialise a new item. */
138 if ((hp = realloc(fp->hv, sizeof (*hp) * (fp->hc + 1))))
142 v_cpy(h.p, cmd->mkitem.p);
155 /* Set up particle effects and discard the item. */
157 assert(cmd->pkitem.hi < fp->hc);
159 hp = fp->hv + cmd->pkitem.hi;
162 part_burst(hp->p, v);
168 case CMD_TILT_ANGLES:
170 game_tilt_axes(&dr.tilt, dr.view.e);
172 dr.tilt.rx = cmd->tiltangles.x;
173 dr.tilt.rz = cmd->tiltangles.z;
177 /* Play the sound. */
180 audio_play(cmd->sound.n, cmd->sound.a);
185 timer = cmd->timer.t;
189 status = cmd->status.t;
193 coins = cmd->coins.n;
207 fp->bv[cmd->bodypath.bi].pi = cmd->bodypath.pi;
211 fp->bv[cmd->bodytime.bi].t = cmd->bodytime.t;
216 * Enable the goal and make sure it's fully visible if
217 * this is the first update.
223 dr.goal_k = first_update ? 1.0f : 0.0f;
228 fp->xv[cmd->swchenter.xi].e = 1;
231 case CMD_SWCH_TOGGLE:
232 fp->xv[cmd->swchtoggle.xi].f = !fp->xv[cmd->swchtoggle.xi].f;
236 fp->xv[cmd->swchexit.xi].e = 0;
239 case CMD_UPDATES_PER_SECOND:
243 case CMD_BALL_RADIUS:
244 fp->uv[curr_ball].r = cmd->ballradius.r;
247 case CMD_CLEAR_ITEMS:
256 case CMD_CLEAR_BALLS:
265 case CMD_BALL_POSITION:
266 up = fp->uv + curr_ball;
268 v_cpy(up->p, cmd->ballpos.p);
272 up = fp->uv + curr_ball;
274 v_cpy(up->e[0], cmd->ballbasis.e[0]);
275 v_cpy(up->e[1], cmd->ballbasis.e[1]);
276 v_crs(up->e[2], up->e[0], up->e[1]);
279 case CMD_BALL_PEND_BASIS:
280 up = fp->uv + curr_ball;
282 v_cpy(up->E[0], cmd->ballpendbasis.E[0]);
283 v_cpy(up->E[1], cmd->ballpendbasis.E[1]);
284 v_crs(up->E[2], up->E[0], up->E[1]);
287 case CMD_VIEW_POSITION:
288 v_cpy(dr.view.p, cmd->viewpos.p);
291 case CMD_VIEW_CENTER:
292 v_cpy(dr.view.c, cmd->viewcenter.c);
296 v_cpy(dr.view.e[0], cmd->viewbasis.e[0]);
297 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]);
301 case CMD_CURRENT_BALL:
302 curr_ball = cmd->currball.ui;
306 fp->pv[cmd->pathflag.pi].f = cmd->pathflag.f;
309 case CMD_STEP_SIMULATION:
311 * Simulate body motion.
313 * This is done on the client side due to replay file size
314 * concerns and isn't done as part of CMD_END_OF_UPDATE to
315 * match the server state as closely as possible. Body time
316 * is still synchronized with the server on a semi-regular
317 * basis and path indices are handled through CMD_BODY_PATH,
318 * thus this code doesn't need to be as sophisticated as
322 dt = cmd->stepsim.dt;
324 for (i = 0; i < fp->bc; i++)
326 struct s_body *bp = fp->bv + i;
327 struct s_path *pp = fp->pv + bp->pi;
329 if (bp->pi >= 0 && pp->f)
336 * Note a version (mis-)match between the loaded map and what
337 * the server has. (This doesn't actually load a map.)
340 game_compat_map = version.x == cmd->map.version.x;
345 v_cpy(dr.tilt.x, cmd->tiltaxes.x);
346 v_cpy(dr.tilt.z, cmd->tiltaxes.z);
355 void game_client_sync(fs_file demo_fp)
359 while ((cmdp = game_proxy_deq()))
362 cmd_put(demo_fp, cmdp);
370 /*---------------------------------------------------------------------------*/
372 int game_client_init(const char *file_name)
374 char *back_name = "", *grad_name = "";
383 if (!sol_load_gl(&dr.file, file_name, config_get_d(CONFIG_SHADOW)))
384 return (dr.state = 0);
386 dr.reflective = sol_reflective(&dr.file);
390 game_tilt_init(&dr.tilt);
392 /* Initialize jump and goal states. */
400 /* Initialise the level, background, particles, fade, and view. */
409 for (i = 0; i < dr.file.dc; i++)
411 char *k = dr.file.av + dr.file.dv[i].ai;
412 char *v = dr.file.av + dr.file.dv[i].aj;
414 if (strcmp(k, "back") == 0) back_name = v;
415 if (strcmp(k, "grad") == 0) grad_name = v;
417 if (strcmp(k, "version") == 0)
418 sscanf(v, "%d.%d", &version.x, &version.y);
422 * If the version of the loaded map is 1, assume we have a version
423 * match with the server. In this way 1.5.0 replays don't trigger
424 * bogus map compatibility warnings. Post-1.5.0 replays will have
425 * CMD_MAP override this.
428 game_compat_map = version.x == 1;
430 part_reset(GOAL_HEIGHT, JUMP_HEIGHT);
435 back_init(grad_name);
436 sol_load_gl(&dr.back, back_name, 0);
441 void game_client_free(void)
446 sol_free_gl(&dr.file);
447 sol_free_gl(&dr.back);
453 /*---------------------------------------------------------------------------*/
455 void game_client_draw(int pose, float t)
457 game_draw(&dr, pose, t);
460 /*---------------------------------------------------------------------------*/
464 return (int) (timer * 100.f);
472 int curr_status(void)
477 /*---------------------------------------------------------------------------*/
480 void game_look(float phi, float theta)
482 dr.view.c[0] = dr.view.p[0] + fsinf(V_RAD(theta)) * fcosf(V_RAD(phi));
483 dr.view.c[1] = dr.view.p[1] + fsinf(V_RAD(phi));
484 dr.view.c[2] = dr.view.p[2] - fcosf(V_RAD(theta)) * fcosf(V_RAD(phi));
487 /*---------------------------------------------------------------------------*/
489 void game_kill_fade(void)
495 void game_step_fade(float dt)
497 if ((dr.fade_k < 1.0f && dr.fade_d > 0.0f) ||
498 (dr.fade_k > 0.0f && dr.fade_d < 0.0f))
499 dr.fade_k += dr.fade_d * dt;
501 if (dr.fade_k < 0.0f)
506 if (dr.fade_k > 1.0f)
513 void game_fade(float d)
518 /*---------------------------------------------------------------------------*/
520 void game_client_fly(float k)
522 game_view_fly(&dr.view, &dr.file, k);
525 /*---------------------------------------------------------------------------*/