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.
31 #include "solid_draw.h"
33 #include "game_client.h"
34 #include "game_common.h"
35 #include "game_proxy.h"
36 #include "game_draw.h"
40 /*---------------------------------------------------------------------------*/
42 int game_compat_map; /* Client/server map compat flag */
44 /*---------------------------------------------------------------------------*/
49 static struct game_draw gd;
50 static struct game_lerp gl;
52 static float timer = 0.0f; /* Clock time */
53 static int status = GAME_NONE; /* Outcome of the game */
54 static int coins = 0; /* Collected coins */
56 static int ups; /* Updates per second */
57 static int first_update; /* First update flag */
58 static int curr_ball; /* Current ball index */
63 } version; /* Current map version */
66 * Neverball <= 1.5.1 does not send explicit tilt axes, rotation
67 * happens directly around view vectors. So for compatibility if at
68 * the time of receiving tilt angles we have not yet received the tilt
69 * axes, we use the view vectors.
72 static int got_tilt_axes;
74 static int next_update;
76 /*---------------------------------------------------------------------------*/
78 static void game_run_cmd(const union cmd *cmd)
82 struct game_view *view = &gl.view[CURR];
83 struct game_tilt *tilt = &gl.tilt[CURR];
85 struct s_vary *vary = &gd.file.vary;
99 case CMD_END_OF_UPDATE:
107 /* Hack to sync state before the next update. */
108 game_lerp_apply(&gl, &gd, 1.0f);
113 /* Compute gravity for particle effects. */
115 if (status == GAME_GOAL)
116 game_tilt_grav(v, GRAVITY_UP, tilt);
118 game_tilt_grav(v, GRAVITY_DN, tilt);
120 /* Step particle, goal and jump effects. */
124 dt = 1.0f / (float) ups;
126 if (gd.goal_e && gl.goal_k[CURR] < 1.0f)
127 gl.goal_k[CURR] += dt;
131 gl.jump_dt[CURR] += dt;
133 if (1.0f < gl.jump_dt[CURR])
143 /* Allocate a new ball and mark it as the current ball. */
145 if (sol_lerp_cmd(&gl.lerp, cmd))
146 curr_ball = gl.lerp.uc - 1;
151 /* Allocate and initialise a new item. */
153 if ((hp = realloc(vary->hv, sizeof (*hp) * (vary->hc + 1))))
157 v_cpy(h.p, cmd->mkitem.p);
163 vary->hv[vary->hc] = h;
170 /* Set up particle effects and discard the item. */
172 assert(cmd->pkitem.hi < vary->hc);
174 hp = vary->hv + cmd->pkitem.hi;
177 part_burst(hp->p, v);
183 case CMD_TILT_ANGLES:
185 game_tilt_axes(tilt, view->e);
187 tilt->rx = cmd->tiltangles.x;
188 tilt->rz = cmd->tiltangles.z;
192 /* Play the sound. */
195 audio_play(cmd->sound.n, cmd->sound.a);
200 timer = cmd->timer.t;
204 status = cmd->status.t;
208 coins = cmd->coins.n;
214 gl.jump_dt[CURR] = 0.0f;
222 sol_lerp_cmd(&gl.lerp, cmd);
226 sol_lerp_cmd(&gl.lerp, cmd);
231 * Enable the goal and make sure it's fully visible if
232 * this is the first update.
238 gl.goal_k[CURR] = first_update ? 1.0f : 0.0f;
243 vary->xv[cmd->swchenter.xi].e = 1;
246 case CMD_SWCH_TOGGLE:
247 vary->xv[cmd->swchtoggle.xi].f = !vary->xv[cmd->swchtoggle.xi].f;
251 vary->xv[cmd->swchexit.xi].e = 0;
254 case CMD_UPDATES_PER_SECOND:
258 case CMD_BALL_RADIUS:
259 sol_lerp_cmd(&gl.lerp, cmd);
262 case CMD_CLEAR_ITEMS:
271 case CMD_CLEAR_BALLS:
272 sol_lerp_cmd(&gl.lerp, cmd);
275 case CMD_BALL_POSITION:
276 sol_lerp_cmd(&gl.lerp, cmd);
280 sol_lerp_cmd(&gl.lerp, cmd);
283 case CMD_BALL_PEND_BASIS:
284 sol_lerp_cmd(&gl.lerp, cmd);
287 case CMD_VIEW_POSITION:
288 v_cpy(view->p, cmd->viewpos.p);
291 case CMD_VIEW_CENTER:
292 v_cpy(view->c, cmd->viewcenter.c);
296 v_cpy(view->e[0], cmd->viewbasis.e[0]);
297 v_cpy(view->e[1], cmd->viewbasis.e[1]);
298 v_crs(view->e[2], view->e[0], view->e[1]);
301 case CMD_CURRENT_BALL:
302 sol_lerp_cmd(&gl.lerp, cmd);
303 curr_ball = cmd->currball.ui;
307 vary->pv[cmd->pathflag.pi].f = cmd->pathflag.f;
310 case CMD_STEP_SIMULATION:
311 sol_lerp_cmd(&gl.lerp, cmd);
316 * Note a version (mis-)match between the loaded map and what
317 * the server has. (This doesn't actually load a map.)
319 game_compat_map = version.x == cmd->map.version.x;
324 v_cpy(tilt->x, cmd->tiltaxes.x);
325 v_cpy(tilt->z, cmd->tiltaxes.z);
335 void game_client_sync(fs_file demo_fp)
339 while ((cmdp = game_proxy_deq()))
342 cmd_put(demo_fp, cmdp);
350 /*---------------------------------------------------------------------------*/
352 int game_client_init(const char *file_name)
354 char *back_name = "", *grad_name = "";
363 if (!sol_load_full(&gd.file, file_name, config_get_d(CONFIG_SHADOW)))
364 return (gd.state = 0);
368 /* Initialize game state. */
370 game_tilt_init(&gd.tilt);
371 game_view_init(&gd.view);
380 /* Initialize interpolation. */
382 game_lerp_init(&gl, &gd);
384 /* Initialize fade. */
389 /* Load level info. */
394 for (i = 0; i < gd.file.base.dc; i++)
396 char *k = gd.file.base.av + gd.file.base.dv[i].ai;
397 char *v = gd.file.base.av + gd.file.base.dv[i].aj;
399 if (strcmp(k, "back") == 0) back_name = v;
400 if (strcmp(k, "grad") == 0) grad_name = v;
402 if (strcmp(k, "version") == 0)
403 sscanf(v, "%d.%d", &version.x, &version.y);
407 * If the version of the loaded map is 1, assume we have a version
408 * match with the server. In this way 1.5.0 replays don't trigger
409 * bogus map compatibility warnings. Post-1.5.0 replays will have
410 * CMD_MAP override this.
413 game_compat_map = version.x == 1;
415 /* Initialize particles. */
417 part_reset(GOAL_HEIGHT, JUMP_HEIGHT);
419 /* Initialize command state. */
424 /* Initialize background. */
426 back_init(grad_name);
427 sol_load_full(&gd.back, back_name, 0);
432 void game_client_free(void)
438 sol_free_full(&gd.file);
439 sol_free_full(&gd.back);
445 /*---------------------------------------------------------------------------*/
447 void game_client_draw(int pose, float t, float a)
449 game_lerp_apply(&gl, &gd, a);
450 game_draw(&gd, pose, t);
453 /*---------------------------------------------------------------------------*/
457 return (int) (timer * 100.f);
465 int curr_status(void)
470 /*---------------------------------------------------------------------------*/
472 void game_look(float phi, float theta)
474 struct game_view *view = &gl.view[CURR];
476 view->c[0] = view->p[0] + fsinf(V_RAD(theta)) * fcosf(V_RAD(phi));
477 view->c[1] = view->p[1] + fsinf(V_RAD(phi));
478 view->c[2] = view->p[2] - fcosf(V_RAD(theta)) * fcosf(V_RAD(phi));
481 /*---------------------------------------------------------------------------*/
483 void game_kill_fade(void)
489 void game_step_fade(float dt)
491 if ((gd.fade_k < 1.0f && gd.fade_d > 0.0f) ||
492 (gd.fade_k > 0.0f && gd.fade_d < 0.0f))
493 gd.fade_k += gd.fade_d * dt;
495 if (gd.fade_k < 0.0f)
500 if (gd.fade_k > 1.0f)
507 void game_fade(float d)
512 /*---------------------------------------------------------------------------*/
514 void game_client_fly(float k)
516 game_view_fly(&gl.view[CURR], &gd.file.vary, k);
519 /*---------------------------------------------------------------------------*/