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.
28 #include "solid_sim.h"
29 #include "solid_all.h"
30 #include "solid_cmd.h"
32 #include "game_common.h"
33 #include "game_server.h"
34 #include "game_proxy.h"
38 /*---------------------------------------------------------------------------*/
40 static int server_state = 0;
42 static struct s_vary vary;
44 static float timer = 0.f; /* Clock time */
45 static int timer_down = 1; /* Timer go up or down? */
47 static int status = GAME_NONE; /* Outcome of the game */
49 static struct game_tilt tilt; /* Floor rotation */
50 static struct game_view view; /* Current view */
54 static int coins = 0; /* Collected coins */
55 static int goal_e = 0; /* Goal enabled flag */
56 static float goal_k = 0; /* Goal animation */
57 static int jump_e = 1; /* Jumping enabled flag */
58 static int jump_b = 0; /* Jump-in-progress flag */
59 static float jump_dt; /* Jump duration */
60 static float jump_p[3]; /* Jump destination */
61 static float jump_w[3]; /* View destination */
63 /*---------------------------------------------------------------------------*/
66 * This is an abstraction of the game's input state. All input is
67 * encapsulated here, and all references to the input by the game are
79 static struct input input_current;
81 static void input_init(void)
89 static void input_set_x(float x)
91 if (x < -ANGLE_BOUND) x = -ANGLE_BOUND;
92 if (x > ANGLE_BOUND) x = ANGLE_BOUND;
97 static void input_set_z(float z)
99 if (z < -ANGLE_BOUND) z = -ANGLE_BOUND;
100 if (z > ANGLE_BOUND) z = ANGLE_BOUND;
105 static void input_set_r(float r)
107 if (r < -VIEWR_BOUND) r = -VIEWR_BOUND;
108 if (r > VIEWR_BOUND) r = VIEWR_BOUND;
113 static void input_set_c(int c)
118 static float input_get_x(void)
120 return input_current.x;
123 static float input_get_z(void)
125 return input_current.z;
128 static float input_get_r(void)
130 return input_current.r;
133 static int input_get_c(void)
135 return input_current.c;
138 /*---------------------------------------------------------------------------*/
141 * Utility functions for preparing the "server" state and events for
142 * consumption by the "client".
145 static union cmd cmd;
147 static void game_cmd_map(const char *name, int ver_x, int ver_y)
150 cmd.map.name = strdup(name);
151 cmd.map.version.x = ver_x;
152 cmd.map.version.y = ver_y;
153 game_proxy_enq(&cmd);
156 static void game_cmd_eou(void)
158 cmd.type = CMD_END_OF_UPDATE;
159 game_proxy_enq(&cmd);
162 static void game_cmd_ups(void)
164 cmd.type = CMD_UPDATES_PER_SECOND;
166 game_proxy_enq(&cmd);
169 static void game_cmd_sound(const char *filename, float a)
171 cmd.type = CMD_SOUND;
173 cmd.sound.n = strdup(filename);
176 game_proxy_enq(&cmd);
179 #define audio_play(s, f) game_cmd_sound((s), (f))
181 static void game_cmd_goalopen(void)
183 cmd.type = CMD_GOAL_OPEN;
184 game_proxy_enq(&cmd);
187 static void game_cmd_updball(void)
189 cmd.type = CMD_BALL_POSITION;
190 v_cpy(cmd.ballpos.p, vary.uv[0].p);
191 game_proxy_enq(&cmd);
193 cmd.type = CMD_BALL_BASIS;
194 v_cpy(cmd.ballbasis.e[0], vary.uv[0].e[0]);
195 v_cpy(cmd.ballbasis.e[1], vary.uv[0].e[1]);
196 game_proxy_enq(&cmd);
198 cmd.type = CMD_BALL_PEND_BASIS;
199 v_cpy(cmd.ballpendbasis.E[0], vary.uv[0].E[0]);
200 v_cpy(cmd.ballpendbasis.E[1], vary.uv[0].E[1]);
201 game_proxy_enq(&cmd);
204 static void game_cmd_updview(void)
206 cmd.type = CMD_VIEW_POSITION;
207 v_cpy(cmd.viewpos.p, view.p);
208 game_proxy_enq(&cmd);
210 cmd.type = CMD_VIEW_CENTER;
211 v_cpy(cmd.viewcenter.c, view.c);
212 game_proxy_enq(&cmd);
214 cmd.type = CMD_VIEW_BASIS;
215 v_cpy(cmd.viewbasis.e[0], view.e[0]);
216 v_cpy(cmd.viewbasis.e[1], view.e[1]);
217 game_proxy_enq(&cmd);
220 static void game_cmd_ballradius(void)
222 cmd.type = CMD_BALL_RADIUS;
223 cmd.ballradius.r = vary.uv[0].r;
224 game_proxy_enq(&cmd);
227 static void game_cmd_init_balls(void)
229 cmd.type = CMD_CLEAR_BALLS;
230 game_proxy_enq(&cmd);
232 cmd.type = CMD_MAKE_BALL;
233 game_proxy_enq(&cmd);
236 game_cmd_ballradius();
239 static void game_cmd_init_items(void)
243 cmd.type = CMD_CLEAR_ITEMS;
244 game_proxy_enq(&cmd);
246 for (i = 0; i < vary.hc; i++)
248 cmd.type = CMD_MAKE_ITEM;
250 v_cpy(cmd.mkitem.p, vary.hv[i].p);
252 cmd.mkitem.t = vary.hv[i].t;
253 cmd.mkitem.n = vary.hv[i].n;
255 game_proxy_enq(&cmd);
259 static void game_cmd_pkitem(int hi)
261 cmd.type = CMD_PICK_ITEM;
263 game_proxy_enq(&cmd);
266 static void game_cmd_jump(int e)
268 cmd.type = e ? CMD_JUMP_ENTER : CMD_JUMP_EXIT;
269 game_proxy_enq(&cmd);
272 static void game_cmd_tiltangles(void)
274 cmd.type = CMD_TILT_ANGLES;
276 cmd.tiltangles.x = tilt.rx;
277 cmd.tiltangles.z = tilt.rz;
279 game_proxy_enq(&cmd);
282 static void game_cmd_tiltaxes(void)
284 cmd.type = CMD_TILT_AXES;
286 v_cpy(cmd.tiltaxes.x, tilt.x);
287 v_cpy(cmd.tiltaxes.z, tilt.z);
289 game_proxy_enq(&cmd);
292 static void game_cmd_timer(void)
294 cmd.type = CMD_TIMER;
296 game_proxy_enq(&cmd);
299 static void game_cmd_coins(void)
301 cmd.type = CMD_COINS;
303 game_proxy_enq(&cmd);
306 static void game_cmd_status(void)
308 cmd.type = CMD_STATUS;
309 cmd.status.t = status;
310 game_proxy_enq(&cmd);
313 /*---------------------------------------------------------------------------*/
315 static int grow = 0; /* Should the ball be changing size? */
316 static float grow_orig = 0; /* the original ball size */
317 static float grow_goal = 0; /* how big or small to get! */
318 static float grow_t = 0.0; /* timer for the ball to grow... */
319 static float grow_strt = 0; /* starting value for growth */
320 static int got_orig = 0; /* Do we know original ball size? */
322 #define GROW_TIME 0.5f /* sec for the ball to get to size. */
323 #define GROW_BIG 1.5f /* large factor */
324 #define GROW_SMALL 0.5f /* small factor */
326 static int grow_state = 0; /* Current state (values -1, 0, +1) */
328 static void grow_init(const struct s_vary *vary, int type)
332 grow_orig = vary->uv->r;
333 grow_goal = grow_orig;
334 grow_strt = grow_orig;
341 if (type == ITEM_SHRINK)
349 audio_play(AUD_SHRINK, 1.f);
350 grow_goal = grow_orig * GROW_SMALL;
356 audio_play(AUD_SHRINK, 1.f);
357 grow_goal = grow_orig;
363 else if (type == ITEM_GROW)
368 audio_play(AUD_GROW, 1.f);
369 grow_goal = grow_orig;
375 audio_play(AUD_GROW, 1.f);
376 grow_goal = grow_orig * GROW_BIG;
389 grow_strt = vary->uv->r;
393 static void grow_step(const struct s_vary *vary, float dt)
400 /* Calculate new size based on how long since you touched the coin... */
404 if (grow_t >= GROW_TIME)
410 dr = grow_strt + ((grow_goal-grow_strt) * (1.0f / (GROW_TIME / grow_t)));
412 /* No sinking through the floor! Keeps ball's bottom constant. */
414 vary->uv->p[1] += (dr - vary->uv->r);
417 game_cmd_ballradius();
420 /*---------------------------------------------------------------------------*/
422 static struct lockstep server_step;
424 int game_server_init(const char *file_name, int t, int e)
426 struct { int x, y; } version;
429 timer = (float) t / 100.f;
430 timer_down = (t > 0);
434 game_server_free(file_name);
438 if (!game_base_load(file_name))
439 return (server_state = 0);
441 if (!sol_load_vary(&vary, &game_base))
443 game_base_free(NULL);
444 return (server_state = 0);
449 /* Get SOL version. */
454 for (i = 0; i < vary.base->dc; i++)
456 char *k = vary.base->av + vary.base->dv[i].ai;
457 char *v = vary.base->av + vary.base->dv[i].aj;
459 if (strcmp(k, "version") == 0)
460 sscanf(v, "%d.%d", &version.x, &version.y);
465 game_tilt_init(&tilt);
467 /* Initialize jump and goal states. */
473 goal_k = e ? 1.0f : 0.0f;
475 /* Initialize the view (and put it at the ball). */
477 game_view_fly(&view, &vary, 0.0f);
480 /* Initialize ball size tracking. */
485 /* Initialize simulation. */
488 sol_cmd_enq_func(game_proxy_enq);
490 /* Send initial update. */
492 game_cmd_map(file_name, version.x, version.y);
499 game_cmd_init_balls();
500 game_cmd_init_items();
505 /* Reset lockstep state. */
507 lockstep_clr(&server_step);
512 void game_server_free(const char *next)
517 sol_free_vary(&vary);
519 game_base_free(next);
525 /*---------------------------------------------------------------------------*/
527 static void game_update_view(float dt)
529 float dc = view.dc * (jump_b ? 2.0f * fabsf(jump_dt - 0.5f) : 1.0f);
530 float da = input_get_r() * dt * 90.0f;
533 float M[16], v[3], Y[3] = { 0.0f, 1.0f, 0.0f };
536 /* Center the view about the ball. */
538 v_cpy(view.c, vary.uv->p);
540 view_v[0] = -vary.uv->v[0];
542 view_v[2] = -vary.uv->v[2];
544 switch (input_get_c())
546 case VIEW_LAZY: /* Viewpoint chases the ball position. */
548 v_sub(view.e[2], view.p, view.c);
552 case VIEW_MANUAL: /* View vector is given by view angle. */
554 view.e[2][0] = fsinf(V_RAD(view.a));
556 view.e[2][2] = fcosf(V_RAD(view.a));
560 case VIEW_CHASE: /* View vector approaches the ball velocity vector. */
562 v_sub(view.e[2], view.p, view.c);
563 v_nrm(view.e[2], view.e[2]);
564 v_mad(view.e[2], view.e[2], view_v, v_dot(view_v, view_v) * dt / 4);
569 /* Apply manual rotation. */
571 m_rot(M, Y, V_RAD(da));
572 m_vxfm(v, M, view.e[2]);
575 /* Orthonormalize the new view reference frame. */
577 v_crs(view.e[0], view.e[1], view.e[2]);
578 v_crs(view.e[2], view.e[0], view.e[1]);
579 v_nrm(view.e[0], view.e[0]);
580 v_nrm(view.e[2], view.e[2]);
582 /* Compute the new view position. */
584 k = 1.0f + v_dot(view.e[2], view_v) / 10.0f;
586 view_k = view_k + (k - view_k) * dt;
588 if (view_k < 0.5) view_k = 0.5;
590 v_scl(v, view.e[1], view.dp * view_k);
591 v_mad(v, v, view.e[2], view.dz * view_k);
592 v_add(view.p, v, vary.uv->p);
594 /* Compute the new view center. */
596 v_cpy(view.c, vary.uv->p);
597 v_mad(view.c, view.c, view.e[1], dc);
599 /* Note the current view angle. */
601 view.a = V_DEG(fatan2f(view.e[2][0], view.e[2][2]));
606 static void game_update_time(float dt, int b)
608 if (goal_e && goal_k < 1.0f)
611 /* The ticking clock. */
625 if (b) game_cmd_timer();
628 static int game_update_state(int bt)
635 /* Test for an item. */
637 if (bt && (hi = sol_item_test(&vary, p, ITEM_RADIUS)) != -1)
639 struct v_item *hp = vary.hv + hi;
643 grow_init(&vary, hp->t);
645 if (hp->t == ITEM_COIN)
651 audio_play(AUD_COIN, 1.f);
658 /* Test for a switch. */
660 if (sol_swch_test(&vary, 0) == SWCH_TRIGGER)
661 audio_play(AUD_SWITCH, 1.f);
663 /* Test for a jump. */
665 if (jump_e == 1 && jump_b == 0 && (sol_jump_test(&vary, jump_p, 0) ==
672 v_sub(jump_w, jump_p, vary.uv->p);
673 v_add(jump_w, view.p, jump_w);
675 audio_play(AUD_JUMP, 1.f);
679 if (jump_e == 0 && jump_b == 0 && (sol_jump_test(&vary, jump_p, 0) ==
686 /* Test for a goal. */
688 if (bt && goal_e && (zp = sol_goal_test(&vary, p, 0)))
690 audio_play(AUD_GOAL, 1.0f);
694 /* Test for time-out. */
696 if (bt && timer_down && timer <= 0.f)
698 audio_play(AUD_TIME, 1.0f);
702 /* Test for fall-out. */
704 if (bt && vary.uv[0].p[1] < vary.base->vv[0].p[1])
706 audio_play(AUD_FALL, 1.0f);
713 static int game_step(const float g[3], float dt, int bt)
719 /* Smooth jittery or discontinuous input. */
721 tilt.rx += (input_get_x() - tilt.rx) * dt / RESPONSE;
722 tilt.rz += (input_get_z() - tilt.rz) * dt / RESPONSE;
724 game_tilt_axes(&tilt, view.e);
727 game_cmd_tiltangles();
729 grow_step(&vary, dt);
731 game_tilt_grav(h, g, &tilt);
741 v_cpy(vary.uv->p, jump_p);
742 v_cpy(view.p, jump_w);
751 float b = sol_step(&vary, h, dt, 0, NULL);
753 /* Mix the sound of a ball bounce. */
757 float k = (b - 0.5f) * 2.0f;
761 if (vary.uv->r > grow_orig) audio_play(AUD_BUMPL, k);
762 else if (vary.uv->r < grow_orig) audio_play(AUD_BUMPS, k);
763 else audio_play(AUD_BUMPM, k);
765 else audio_play(AUD_BUMPM, k);
774 game_update_view(dt);
775 game_update_time(dt, bt);
777 return game_update_state(bt);
782 static void game_server_iter(float dt)
786 case GAME_GOAL: game_step(GRAVITY_UP, dt, 0); break;
787 case GAME_FALL: game_step(GRAVITY_DN, dt, 0); break;
790 if ((status = game_step(GRAVITY_DN, dt, 1)) != GAME_NONE)
798 static struct lockstep server_step = { game_server_iter, DT };
800 void game_server_step(float dt)
802 lockstep_run(&server_step, dt);
805 float game_server_blend(void)
807 return lockstep_blend(&server_step);
810 /*---------------------------------------------------------------------------*/
812 void game_set_goal(void)
814 audio_play(AUD_SWITCH, 1.0f);
820 /*---------------------------------------------------------------------------*/
822 void game_set_x(float k)
824 input_set_x(-ANGLE_BOUND * k);
827 void game_set_z(float k)
829 input_set_z(+ANGLE_BOUND * k);
832 void game_set_ang(float x, float z)
838 void game_set_pos(int x, int y)
840 const float range = ANGLE_BOUND * 2;
842 input_set_x(input_get_x() + range * y / config_get_d(CONFIG_MOUSE_SENSE));
843 input_set_z(input_get_z() + range * x / config_get_d(CONFIG_MOUSE_SENSE));
846 void game_set_cam(int c)
851 void game_set_rot(float r)
856 /*---------------------------------------------------------------------------*/