Removed all remntants of an old implementation of special/warp goals.
[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 /* Currently playing level. */
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 /* Start to play the current level */
41
42 int level_play_go(void)
43 {
44     struct level_game *lg = &current_level_game;
45
46     assert(lg->level != NULL);
47
48     lg->goal = (lg->mode == MODE_PRACTICE) ? 0 : lg->level->goal;
49     lg->time = (lg->mode == MODE_PRACTICE) ? 0 : lg->level->time;
50
51     /* Clear other fields. */
52
53     lg->state = GAME_NONE;
54     lg->coins = 0;
55     lg->timer = lg->time;
56     lg->coin_rank = lg->goal_rank = lg->time_rank =
57         lg->score_rank = lg->times_rank = 3;
58
59     lg->win = lg->dead = lg->unlock = 0;
60     lg->next_level = NULL;
61
62     return demo_play_init(USER_REPLAY_FILE, lg->level, lg);
63 }
64
65 /* Prepare to play a level sequence from the `i'th level */
66
67 void level_play(const struct level *l, int m)
68 {
69     struct level_game *lg = &current_level_game;
70
71     memset(lg, 0, sizeof (struct level_game));
72
73     lg->mode  = m;
74     lg->level = l;
75     lg->balls = 3;
76 }
77
78 /*---------------------------------------------------------------------------*/
79
80 const struct level_game *curr_lg(void)
81 {
82     return &current_level_game;
83 }
84
85 int count_extra_balls(int old_score, int coins)
86 {
87     return ((old_score % 100) + coins) / 100;
88 }
89
90 /* Stop the current playing level */
91
92 void level_stop(int state, int clock, int coins)
93 {
94     struct level_game *lg = &current_level_game;
95     int mode = lg->mode;
96     int timer = (mode == MODE_PRACTICE) ? clock : lg->time - clock;
97
98     lg->state = state;
99     lg->coins = coins;
100     lg->timer = timer;
101
102     /* Performs challenge mode operations */
103     if (mode == MODE_CHALLENGE)
104     {
105         /* sum time */
106         lg->times += timer;
107
108         /* sum coins an earn extra balls */
109         if (state == GAME_GOAL || lg->level->is_bonus)
110         {
111             lg->balls += count_extra_balls(lg->score, coins);
112             lg->score += coins;
113         }
114
115         /* lose ball and game */
116         else
117         {
118             lg->dead = (lg->balls <= 0);
119             lg->balls--;
120         }
121     }
122
123     /* Update high-scores and next level */
124     set_finish_level(lg, config_simple_get_s(CONFIG_PLAYER));
125
126     /* stop demo recording */
127     demo_play_stop(lg);
128 }
129
130 void level_next(void)
131 {
132     struct level_game *lg = &current_level_game;
133     lg->level = lg->next_level;
134 }
135
136 void level_update_player_name(void)
137 {
138     score_change_name(&current_level_game, config_simple_get_s(CONFIG_PLAYER));
139 }
140
141 /*---------------------------------------------------------------------------*/
142