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.
25 #include "solid_sim.h"
26 #include "solid_all.h"
27 #include "solid_cmd.h"
29 #include "game_common.h"
30 #include "game_server.h"
31 #include "game_proxy.h"
35 /*---------------------------------------------------------------------------*/
37 static int server_state = 0;
39 static struct s_base base;
40 static struct s_vary file;
42 static float timer = 0.f; /* Clock time */
43 static int timer_down = 1; /* Timer go up or down? */
45 static int status = GAME_NONE; /* Outcome of the game */
47 static struct game_tilt tilt; /* Floor rotation */
48 static struct game_view view; /* Current view */
52 static int coins = 0; /* Collected coins */
53 static int goal_e = 0; /* Goal enabled flag */
54 static float goal_k = 0; /* Goal animation */
55 static int jump_e = 1; /* Jumping enabled flag */
56 static int jump_b = 0; /* Jump-in-progress flag */
57 static float jump_dt; /* Jump duration */
58 static float jump_p[3]; /* Jump destination */
59 static float jump_w[3]; /* View destination */
61 /*---------------------------------------------------------------------------*/
64 * This is an abstraction of the game's input state. All input is
65 * encapsulated here, and all references to the input by the game are
77 static struct input input_current;
79 static void input_init(void)
87 static void input_set_x(float x)
89 if (x < -ANGLE_BOUND) x = -ANGLE_BOUND;
90 if (x > ANGLE_BOUND) x = ANGLE_BOUND;
95 static void input_set_z(float z)
97 if (z < -ANGLE_BOUND) z = -ANGLE_BOUND;
98 if (z > ANGLE_BOUND) z = ANGLE_BOUND;
103 static void input_set_r(float r)
105 if (r < -VIEWR_BOUND) r = -VIEWR_BOUND;
106 if (r > VIEWR_BOUND) r = VIEWR_BOUND;
111 static void input_set_c(int c)
116 static float input_get_x(void)
118 return input_current.x;
121 static float input_get_z(void)
123 return input_current.z;
126 static float input_get_r(void)
128 return input_current.r;
131 static int input_get_c(void)
133 return input_current.c;
136 /*---------------------------------------------------------------------------*/
139 * Utility functions for preparing the "server" state and events for
140 * consumption by the "client".
143 static union cmd cmd;
145 static void game_cmd_map(const char *name, int ver_x, int ver_y)
148 cmd.map.name = strdup(name);
149 cmd.map.version.x = ver_x;
150 cmd.map.version.y = ver_y;
151 game_proxy_enq(&cmd);
154 static void game_cmd_eou(void)
156 cmd.type = CMD_END_OF_UPDATE;
157 game_proxy_enq(&cmd);
160 static void game_cmd_ups(void)
162 cmd.type = CMD_UPDATES_PER_SECOND;
164 game_proxy_enq(&cmd);
167 static void game_cmd_sound(const char *filename, float a)
169 cmd.type = CMD_SOUND;
171 cmd.sound.n = strdup(filename);
174 game_proxy_enq(&cmd);
177 #define audio_play(s, f) game_cmd_sound((s), (f))
179 static void game_cmd_goalopen(void)
181 cmd.type = CMD_GOAL_OPEN;
182 game_proxy_enq(&cmd);
185 static void game_cmd_updball(void)
187 cmd.type = CMD_BALL_POSITION;
188 v_cpy(cmd.ballpos.p, file.uv[0].p);
189 game_proxy_enq(&cmd);
191 cmd.type = CMD_BALL_BASIS;
192 v_cpy(cmd.ballbasis.e[0], file.uv[0].e[0]);
193 v_cpy(cmd.ballbasis.e[1], file.uv[0].e[1]);
194 game_proxy_enq(&cmd);
196 cmd.type = CMD_BALL_PEND_BASIS;
197 v_cpy(cmd.ballpendbasis.E[0], file.uv[0].E[0]);
198 v_cpy(cmd.ballpendbasis.E[1], file.uv[0].E[1]);
199 game_proxy_enq(&cmd);
202 static void game_cmd_updview(void)
204 cmd.type = CMD_VIEW_POSITION;
205 v_cpy(cmd.viewpos.p, view.p);
206 game_proxy_enq(&cmd);
208 cmd.type = CMD_VIEW_CENTER;
209 v_cpy(cmd.viewcenter.c, view.c);
210 game_proxy_enq(&cmd);
212 cmd.type = CMD_VIEW_BASIS;
213 v_cpy(cmd.viewbasis.e[0], view.e[0]);
214 v_cpy(cmd.viewbasis.e[1], view.e[1]);
215 game_proxy_enq(&cmd);
218 static void game_cmd_ballradius(void)
220 cmd.type = CMD_BALL_RADIUS;
221 cmd.ballradius.r = file.uv[0].r;
222 game_proxy_enq(&cmd);
225 static void game_cmd_init_balls(void)
227 cmd.type = CMD_CLEAR_BALLS;
228 game_proxy_enq(&cmd);
230 cmd.type = CMD_MAKE_BALL;
231 game_proxy_enq(&cmd);
234 game_cmd_ballradius();
237 static void game_cmd_init_items(void)
241 cmd.type = CMD_CLEAR_ITEMS;
242 game_proxy_enq(&cmd);
244 for (i = 0; i < file.hc; i++)
246 cmd.type = CMD_MAKE_ITEM;
248 v_cpy(cmd.mkitem.p, file.hv[i].p);
250 cmd.mkitem.t = file.hv[i].t;
251 cmd.mkitem.n = file.hv[i].n;
253 game_proxy_enq(&cmd);
257 static void game_cmd_pkitem(int hi)
259 cmd.type = CMD_PICK_ITEM;
261 game_proxy_enq(&cmd);
264 static void game_cmd_jump(int e)
266 cmd.type = e ? CMD_JUMP_ENTER : CMD_JUMP_EXIT;
267 game_proxy_enq(&cmd);
270 static void game_cmd_tiltangles(void)
272 cmd.type = CMD_TILT_ANGLES;
274 cmd.tiltangles.x = tilt.rx;
275 cmd.tiltangles.z = tilt.rz;
277 game_proxy_enq(&cmd);
280 static void game_cmd_tiltaxes(void)
282 cmd.type = CMD_TILT_AXES;
284 v_cpy(cmd.tiltaxes.x, tilt.x);
285 v_cpy(cmd.tiltaxes.z, tilt.z);
287 game_proxy_enq(&cmd);
290 static void game_cmd_timer(void)
292 cmd.type = CMD_TIMER;
294 game_proxy_enq(&cmd);
297 static void game_cmd_coins(void)
299 cmd.type = CMD_COINS;
301 game_proxy_enq(&cmd);
304 static void game_cmd_status(void)
306 cmd.type = CMD_STATUS;
307 cmd.status.t = status;
308 game_proxy_enq(&cmd);
311 /*---------------------------------------------------------------------------*/
313 static int grow = 0; /* Should the ball be changing size? */
314 static float grow_orig = 0; /* the original ball size */
315 static float grow_goal = 0; /* how big or small to get! */
316 static float grow_t = 0.0; /* timer for the ball to grow... */
317 static float grow_strt = 0; /* starting value for growth */
318 static int got_orig = 0; /* Do we know original ball size? */
320 #define GROW_TIME 0.5f /* sec for the ball to get to size. */
321 #define GROW_BIG 1.5f /* large factor */
322 #define GROW_SMALL 0.5f /* small factor */
324 static int grow_state = 0; /* Current state (values -1, 0, +1) */
326 static void grow_init(const struct s_vary *vary, int type)
330 grow_orig = vary->uv->r;
331 grow_goal = grow_orig;
332 grow_strt = grow_orig;
339 if (type == ITEM_SHRINK)
347 audio_play(AUD_SHRINK, 1.f);
348 grow_goal = grow_orig * GROW_SMALL;
354 audio_play(AUD_SHRINK, 1.f);
355 grow_goal = grow_orig;
361 else if (type == ITEM_GROW)
366 audio_play(AUD_GROW, 1.f);
367 grow_goal = grow_orig;
373 audio_play(AUD_GROW, 1.f);
374 grow_goal = grow_orig * GROW_BIG;
387 grow_strt = vary->uv->r;
391 static void grow_step(const struct s_vary *vary, float dt)
398 /* Calculate new size based on how long since you touched the coin... */
402 if (grow_t >= GROW_TIME)
408 dr = grow_strt + ((grow_goal-grow_strt) * (1.0f / (GROW_TIME / grow_t)));
410 /* No sinking through the floor! Keeps ball's bottom constant. */
412 vary->uv->p[1] += (dr - vary->uv->r);
415 game_cmd_ballradius();
418 /*---------------------------------------------------------------------------*/
420 static struct lockstep server_step;
422 int game_server_init(const char *file_name, int t, int e)
431 timer = (float) t / 100.f;
432 timer_down = (t > 0);
439 if (!sol_load_base(&base, file_name))
440 return (server_state = 0);
442 if (!sol_load_vary(&file, &base))
443 return (server_state = 0);
450 for (i = 0; i < file.base->dc; i++)
452 char *k = file.base->av + file.base->dv[i].ai;
453 char *v = file.base->av + file.base->dv[i].aj;
455 if (strcmp(k, "version") == 0)
456 sscanf(v, "%d.%d", &version.x, &version.y);
461 game_tilt_init(&tilt);
463 /* Initialize jump and goal states. */
469 goal_k = e ? 1.0f : 0.0f;
471 /* Initialize the view. */
473 game_view_init(&view);
477 /* Initialize ball size tracking... */
482 /* Initialize simulation. */
486 sol_cmd_enq_func(game_proxy_enq);
488 /* Queue client commands. */
490 game_cmd_map(file_name, version.x, version.y);
494 if (goal_e) game_cmd_goalopen();
496 game_cmd_init_balls();
497 game_cmd_init_items();
499 /* Store the initial view. */
501 game_server_fly(0.0f);
503 /* End first update. */
507 /* Reset lockstep state. */
509 lockstep_clr(&server_step);
514 void game_server_free(void)
519 sol_free_vary(&file);
524 /*---------------------------------------------------------------------------*/
526 static void game_update_view(float dt)
528 float dc = view.dc * (jump_b ? 2.0f * fabsf(jump_dt - 0.5f) : 1.0f);
529 float da = input_get_r() * dt * 90.0f;
532 float M[16], v[3], Y[3] = { 0.0f, 1.0f, 0.0f };
535 /* Center the view about the ball. */
537 v_cpy(view.c, file.uv->p);
539 view_v[0] = -file.uv->v[0];
541 view_v[2] = -file.uv->v[2];
543 switch (input_get_c())
545 case VIEW_LAZY: /* Viewpoint chases the ball position. */
547 v_sub(view.e[2], view.p, view.c);
551 case VIEW_MANUAL: /* View vector is given by view angle. */
553 view.e[2][0] = fsinf(V_RAD(view.a));
555 view.e[2][2] = fcosf(V_RAD(view.a));
559 case VIEW_CHASE: /* View vector approaches the ball velocity vector. */
561 v_sub(view.e[2], view.p, view.c);
562 v_nrm(view.e[2], view.e[2]);
563 v_mad(view.e[2], view.e[2], view_v, v_dot(view_v, view_v) * dt / 4);
568 /* Apply manual rotation. */
570 m_rot(M, Y, V_RAD(da));
571 m_vxfm(v, M, view.e[2]);
574 /* Orthonormalize the new view reference frame. */
576 v_crs(view.e[0], view.e[1], view.e[2]);
577 v_crs(view.e[2], view.e[0], view.e[1]);
578 v_nrm(view.e[0], view.e[0]);
579 v_nrm(view.e[2], view.e[2]);
581 /* Compute the new view position. */
583 k = 1.0f + v_dot(view.e[2], view_v) / 10.0f;
585 view_k = view_k + (k - view_k) * dt;
587 if (view_k < 0.5) view_k = 0.5;
589 v_scl(v, view.e[1], view.dp * view_k);
590 v_mad(v, v, view.e[2], view.dz * view_k);
591 v_add(view.p, v, file.uv->p);
593 /* Compute the new view center. */
595 v_cpy(view.c, file.uv->p);
596 v_mad(view.c, view.c, view.e[1], dc);
598 /* Note the current view angle. */
600 view.a = V_DEG(fatan2f(view.e[2][0], view.e[2][2]));
605 static void game_update_time(float dt, int b)
607 if (goal_e && goal_k < 1.0f)
610 /* The ticking clock. */
624 if (b) game_cmd_timer();
627 static int game_update_state(int bt)
629 struct s_vary *vary = &file;
635 /* Test for an item. */
637 if (bt && (hi = sol_item_test(vary, p, ITEM_RADIUS)) != -1)
639 struct v_item *hp = file.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)
717 struct s_vary *vary = &file;
721 /* Smooth jittery or discontinuous input. */
723 tilt.rx += (input_get_x() - tilt.rx) * dt / RESPONSE;
724 tilt.rz += (input_get_z() - tilt.rz) * dt / RESPONSE;
726 game_tilt_axes(&tilt, view.e);
729 game_cmd_tiltangles();
733 game_tilt_grav(h, g, &tilt);
743 v_cpy(vary->uv->p, jump_p);
744 v_cpy(view.p, jump_w);
753 float b = sol_step(vary, h, dt, 0, NULL);
755 /* Mix the sound of a ball bounce. */
759 float k = (b - 0.5f) * 2.0f;
763 if (vary->uv->r > grow_orig) audio_play(AUD_BUMPL, k);
764 else if (vary->uv->r < grow_orig) audio_play(AUD_BUMPS, k);
765 else audio_play(AUD_BUMPM, k);
767 else audio_play(AUD_BUMPM, k);
773 game_update_view(dt);
774 game_update_time(dt, bt);
776 return game_update_state(bt);
781 static void game_server_iter(float dt)
785 case GAME_GOAL: game_step(GRAVITY_UP, dt, 0); break;
786 case GAME_FALL: game_step(GRAVITY_DN, dt, 0); break;
789 if ((status = game_step(GRAVITY_DN, dt, 1)) != GAME_NONE)
797 static struct lockstep server_step = { game_server_iter, DT };
799 void game_server_step(float dt)
801 lockstep_run(&server_step, dt);
804 /*---------------------------------------------------------------------------*/
806 void game_set_goal(void)
808 audio_play(AUD_SWITCH, 1.0f);
814 void game_clr_goal(void)
819 /*---------------------------------------------------------------------------*/
821 void game_set_x(float k)
823 input_set_x(-ANGLE_BOUND * k);
826 void game_set_z(float k)
828 input_set_z(+ANGLE_BOUND * k);
831 void game_set_ang(int x, int z)
837 void game_set_pos(int x, int y)
839 const float range = ANGLE_BOUND * 2;
841 input_set_x(input_get_x() + range * y / config_get_d(CONFIG_MOUSE_SENSE));
842 input_set_z(input_get_z() + range * x / config_get_d(CONFIG_MOUSE_SENSE));
845 void game_set_cam(int c)
850 void game_set_rot(float r)
855 void game_server_fly(float k)
857 game_view_fly(&view, &file, k);
861 /*---------------------------------------------------------------------------*/