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.
30 #include "solid_draw.h"
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 /*---------------------------------------------------------------------------*/
48 static struct game_draw gd;
49 static struct game_lerp gl;
51 static float timer = 0.0f; /* Clock time */
52 static int status = GAME_NONE; /* Outcome of the game */
53 static int coins = 0; /* Collected coins */
55 static struct cmd_state cs; /* Command state */
60 } version; /* Current map version */
62 /*---------------------------------------------------------------------------*/
64 static void game_run_cmd(const union cmd *cmd)
68 struct game_view *view = &gl.view[CURR];
69 struct game_tilt *tilt = &gl.tilt[CURR];
71 struct s_vary *vary = &gd.vary;
85 case CMD_END_OF_UPDATE:
92 /* Hack to sync state before the next update. */
93 game_lerp_apply(&gl, &gd);
98 /* Compute gravity for particle effects. */
100 if (status == GAME_GOAL)
101 game_tilt_grav(v, GRAVITY_UP, tilt);
103 game_tilt_grav(v, GRAVITY_DN, tilt);
105 /* Step particle, goal and jump effects. */
111 if (gd.goal_e && gl.goal_k[CURR] < 1.0f)
112 gl.goal_k[CURR] += dt;
116 gl.jump_dt[CURR] += dt;
118 if (1.0f < gl.jump_dt[PREV])
128 /* Allocate a new ball and mark it as the current ball. */
130 if (sol_lerp_cmd(&gl.lerp, &cs, cmd))
131 cs.curr_ball = gl.lerp.uc - 1;
136 /* Allocate and initialise a new item. */
138 if ((hp = realloc(vary->hv, sizeof (*hp) * (vary->hc + 1))))
142 v_cpy(h.p, cmd->mkitem.p);
148 vary->hv[vary->hc] = h;
155 /* Set up particle effects and discard the item. */
157 assert(cmd->pkitem.hi < vary->hc);
159 hp = vary->hv + cmd->pkitem.hi;
162 part_burst(hp->p, v);
168 case CMD_TILT_ANGLES:
169 if (!cs.got_tilt_axes)
172 * Neverball <= 1.5.1 does not send explicit tilt
173 * axes, rotation happens directly around view
174 * vectors. So for compatibility if at the time of
175 * receiving tilt angles we have not yet received the
176 * tilt axes, we use the view vectors.
179 game_tilt_axes(tilt, view->e);
182 tilt->rx = cmd->tiltangles.x;
183 tilt->rz = cmd->tiltangles.z;
187 /* Play the sound. */
190 audio_play(cmd->sound.n, cmd->sound.a);
195 timer = cmd->timer.t;
199 status = cmd->status.t;
203 coins = cmd->coins.n;
209 gl.jump_dt[PREV] = 0.0f;
210 gl.jump_dt[CURR] = 0.0f;
218 sol_lerp_cmd(&gl.lerp, &cs, cmd);
222 sol_lerp_cmd(&gl.lerp, &cs, cmd);
227 * Enable the goal and make sure it's fully visible if
228 * this is the first update.
234 gl.goal_k[CURR] = cs.first_update ? 1.0f : 0.0f;
239 vary->xv[cmd->swchenter.xi].e = 1;
242 case CMD_SWCH_TOGGLE:
243 vary->xv[cmd->swchtoggle.xi].f = !vary->xv[cmd->swchtoggle.xi].f;
247 vary->xv[cmd->swchexit.xi].e = 0;
250 case CMD_UPDATES_PER_SECOND:
254 case CMD_BALL_RADIUS:
255 sol_lerp_cmd(&gl.lerp, &cs, cmd);
258 case CMD_CLEAR_ITEMS:
267 case CMD_CLEAR_BALLS:
268 sol_lerp_cmd(&gl.lerp, &cs, cmd);
271 case CMD_BALL_POSITION:
272 sol_lerp_cmd(&gl.lerp, &cs, cmd);
276 sol_lerp_cmd(&gl.lerp, &cs, cmd);
279 case CMD_BALL_PEND_BASIS:
280 sol_lerp_cmd(&gl.lerp, &cs, cmd);
283 case CMD_VIEW_POSITION:
284 v_cpy(view->p, cmd->viewpos.p);
287 case CMD_VIEW_CENTER:
288 v_cpy(view->c, cmd->viewcenter.c);
292 v_cpy(view->e[0], cmd->viewbasis.e[0]);
293 v_cpy(view->e[1], cmd->viewbasis.e[1]);
294 v_crs(view->e[2], view->e[0], view->e[1]);
297 case CMD_CURRENT_BALL:
298 cs.curr_ball = cmd->currball.ui;
302 vary->pv[cmd->pathflag.pi].f = cmd->pathflag.f;
305 case CMD_STEP_SIMULATION:
306 sol_lerp_cmd(&gl.lerp, &cs, cmd);
311 * Note a version (mis-)match between the loaded map and what
312 * the server has. (This doesn't actually load a map.)
314 game_compat_map = version.x == cmd->map.version.x;
318 cs.got_tilt_axes = 1;
319 v_cpy(tilt->x, cmd->tiltaxes.x);
320 v_cpy(tilt->z, cmd->tiltaxes.z);
330 void game_client_sync(fs_file demo_fp)
334 while ((cmdp = game_proxy_deq()))
337 cmd_put(demo_fp, cmdp);
345 /*---------------------------------------------------------------------------*/
347 int game_client_init(const char *file_name)
349 char *back_name = "", *grad_name = "";
355 game_client_free(file_name);
359 if (!game_base_load(file_name))
360 return (gd.state = 0);
362 if (!sol_load_vary(&gd.vary, &game_base))
364 game_base_free(NULL);
365 return (gd.state = 0);
368 if (!sol_load_draw(&gd.draw, &gd.vary, config_get_d(CONFIG_SHADOW)))
370 sol_free_vary(&gd.vary);
371 game_base_free(NULL);
372 return (gd.state = 0);
377 /* Initialize game state. */
379 game_tilt_init(&gd.tilt);
380 game_view_init(&gd.view);
389 /* Initialize interpolation. */
391 game_lerp_init(&gl, &gd);
393 /* Initialize fade. */
398 /* Load level info. */
403 for (i = 0; i < gd.vary.base->dc; i++)
405 char *k = gd.vary.base->av + gd.vary.base->dv[i].ai;
406 char *v = gd.vary.base->av + gd.vary.base->dv[i].aj;
408 if (strcmp(k, "back") == 0) back_name = v;
409 if (strcmp(k, "grad") == 0) grad_name = v;
411 if (strcmp(k, "version") == 0)
412 sscanf(v, "%d.%d", &version.x, &version.y);
416 * If the version of the loaded map is 1, assume we have a version
417 * match with the server. In this way 1.5.0 replays don't trigger
418 * bogus map compatibility warnings. Post-1.5.0 replays will have
419 * CMD_MAP override this.
422 game_compat_map = version.x == 1;
424 /* Initialize particles. */
428 /* Initialize command state. */
432 /* Initialize background. */
434 back_init(grad_name);
435 sol_load_full(&gd.back, back_name, 0);
440 void game_client_free(const char *next)
448 sol_free_draw(&gd.draw);
449 sol_free_vary(&gd.vary);
451 game_base_free(next);
453 sol_free_full(&gd.back);
459 /*---------------------------------------------------------------------------*/
461 void game_client_blend(float a)
466 void game_client_draw(int pose, float t)
468 game_lerp_apply(&gl, &gd);
469 game_draw(&gd, pose, t);
472 /*---------------------------------------------------------------------------*/
476 return (int) (timer * 100.f);
484 int curr_status(void)
489 /*---------------------------------------------------------------------------*/
491 void game_look(float phi, float theta)
493 struct game_view *view = &gl.view[CURR];
495 view->c[0] = view->p[0] + fsinf(V_RAD(theta)) * fcosf(V_RAD(phi));
496 view->c[1] = view->p[1] + fsinf(V_RAD(phi));
497 view->c[2] = view->p[2] - fcosf(V_RAD(theta)) * fcosf(V_RAD(phi));
499 gl.view[PREV] = gl.view[CURR];
502 /*---------------------------------------------------------------------------*/
504 void game_kill_fade(void)
510 void game_step_fade(float dt)
512 if ((gd.fade_k < 1.0f && gd.fade_d > 0.0f) ||
513 (gd.fade_k > 0.0f && gd.fade_d < 0.0f))
514 gd.fade_k += gd.fade_d * dt;
516 if (gd.fade_k < 0.0f)
521 if (gd.fade_k > 1.0f)
528 void game_fade(float d)
533 /*---------------------------------------------------------------------------*/
535 void game_client_fly(float k)
537 game_view_fly(&gl.view[CURR], &gd.vary, k);
539 gl.view[PREV] = gl.view[CURR];
542 /*---------------------------------------------------------------------------*/