Since the --replay command line option, extension and basename stuff is not exclusive...
[neverball] / ball / levels.c
1 /*
2  * Copyright (C) 2003 Robert Kooima
3  *
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.
8  *
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.
13  */
14
15 #include <stdio.h>
16 #include <string.h>
17 #include <math.h>
18 #include <assert.h>
19
20 #include "level.h"
21 #include "levels.h"
22 #include "game.h"
23 #include "demo.h"
24 #include "audio.h"
25 #include "config.h"
26 #include "set.h"
27
28 /*---------------------------------------------------------------------------*/
29
30 /* The level currently playing */
31 static struct level_game current_level_game;
32
33 /*---------------------------------------------------------------------------*/
34
35 int level_replay(const char *filename)
36 {
37     return demo_replay_init(filename, &current_level_game);
38 }
39
40 static struct level single_level; /* a level without set */
41
42 int level_play_go(void)
43 /* Start to play the current level */
44 {
45     struct level_game *lg = &current_level_game;
46     const struct level *l = lg->level;
47     int mode = lg->mode;
48
49     assert(l != NULL);
50
51     lg->goal = (mode == MODE_PRACTICE) ? 0 : l->goal;
52     lg->time = (mode == MODE_PRACTICE) ? 0 : l->time;
53
54     /* clear other fields */
55     lg->state = GAME_NONE;
56     lg->coins = 0;
57     lg->timer = lg->time;
58     lg->coin_rank = lg->goal_rank = lg->time_rank =
59         lg->score_rank = lg->times_rank = 3;
60     lg->win = lg->dead = lg->unlock = 0;
61     lg->next_level = NULL;
62
63     return demo_play_init(USER_REPLAY_FILE, l, lg);
64 }
65
66 /* Prepare to play a single level */
67
68 void level_play_single(const char *filename)
69 {
70     struct level *l = &single_level;
71
72     level_load(filename, l);
73     level_play(l, MODE_SINGLE);
74 }
75
76 /* Prepare to play a level sequence from the `i'th level */
77
78 void level_play(const struct level *l, int m)
79 {
80     struct level_game *lg = &current_level_game;
81
82     memset(lg, 0, sizeof (struct level_game));
83
84     lg->mode  = m;
85     lg->level = l;
86     lg->balls = 3;
87 }
88
89 /*---------------------------------------------------------------------------*/
90
91 const struct level_game *curr_lg(void)
92 {
93     return &current_level_game;
94 }
95
96 int count_extra_balls(int old_score, int coins)
97 {
98     int modulo = old_score % 100;
99     int sum    = modulo + coins;
100     return sum / 100;
101 }
102
103 /* Stop the current playing level */
104
105 void level_stop(int state, int state_value, int clock, int coins)
106 {
107     struct level_game *lg = &current_level_game;
108     int mode = lg->mode;
109     int timer = (mode == MODE_PRACTICE
110                  || mode == MODE_SINGLE) ? clock : lg->time - clock;
111
112     lg->state = state;
113     lg->coins = coins;
114     lg->timer = timer;
115     lg->state_value = state_value;
116
117     /* Performs challenge mode opperations */
118     if (mode == MODE_CHALLENGE)
119     {
120         /* sum time */
121         lg->times += timer;
122
123         /* sum coins an earn extra balls */
124         if (state == GAME_GOAL || state == GAME_SPEC || lg->level->is_bonus)
125         {
126             lg->balls += count_extra_balls(lg->score, coins);
127             lg->score += coins;
128         }
129
130         /* lose ball and game */
131         else                    /* if ((state == GAME_TIME || state == GAME_FALL) && !lg->level->is_bonus) */
132         {
133             lg->dead = (lg->balls <= 0);
134             lg->balls--;
135         }
136     }
137
138     /* Update high-scores and next level */
139     set_finish_level(lg, config_simple_get_s(CONFIG_PLAYER));
140
141     /* stop demo recording */
142     demo_play_stop(lg);
143 }
144
145 void level_next(void)
146 {
147     struct level_game *lg = &current_level_game;
148     lg->level = lg->next_level;
149 }
150
151 void level_update_player_name(void)
152 {
153     score_change_name(&current_level_game, config_simple_get_s(CONFIG_PLAYER));
154 }
155
156 /*---------------------------------------------------------------------------*/
157