moved savelevel() code to level.h/c where it belongs to. :)
[supertux.git] / src / scene.c
1 //
2 // C Implementation: scene
3 //
4 // Description: 
5 //
6 //
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2004
8 //
9 // Copyright: See COPYING file that comes with this distribution
10 //
11 //
12
13 #include <stdlib.h>
14 #include "scene.h"
15
16 /* Initialize all 'dynamic' arrays */
17 void arrays_init(void)
18 {
19 num_bad_guys = 0;
20 num_bouncy_distros = 0;
21 num_broken_bricks = 0;
22 num_bouncy_bricks = 0;
23 num_floating_scores = 0;
24 num_upgrades = 0;
25 num_bullets = 0;
26 bad_guys = NULL;
27 bouncy_distros = NULL;
28 broken_bricks = NULL;
29 bouncy_bricks = NULL;
30 floating_scores = NULL;
31 upgrades = NULL;
32 bullets = NULL;
33 }
34
35 /* Free memory of 'dynamic' arrays */
36 void arrays_free(void)
37 {
38 free(bad_guys);
39 free(bouncy_distros);
40 free(broken_bricks);
41 free(bouncy_bricks);
42 free(floating_scores);
43 free(upgrades);
44 free(bullets);
45 }
46
47 void set_defaults(void)
48 {
49   /* Set defaults: */
50   
51   scroll_x = 0;
52
53   score_multiplier = 1;
54   timer_init(&super_bkgd_timer);
55
56   counting_distros = NO;
57   distro_counter = 0;
58
59   endpos = 0;
60
61   /* set current song/music */
62   current_music = LEVEL_MUSIC;
63 }
64
65 /* Add score: */
66
67 void add_score(int x, int y, int s)
68 {
69   int i, found;
70
71
72   /* Add the score: */
73
74   score = score + s;
75
76
77   /* Add a floating score thing to the game: */
78
79   found = -1;
80
81   for (i = 0; i < num_floating_scores && found == -1; i++)
82     {
83       if (!floating_scores[i].base.alive)
84         found = i;
85     }
86     
87   if (found == -1)
88   {
89   ++num_floating_scores;
90   floating_scores = realloc(floating_scores,num_floating_scores*sizeof(floating_score_type));
91   floating_score_init(&floating_scores[num_floating_scores-1],x,y,s);
92   found = -1;
93   }
94
95   if (found != -1)
96     {
97         floating_score_init(&floating_scores[found],x,y,s);
98     }
99 }
100
101 /* Add a bouncy distro: */
102
103 void add_bouncy_distro(float x, float y)
104 {
105   int i, found;
106
107   found = -1;
108
109   for (i = 0; i < num_bouncy_distros && found == -1; i++)
110     {
111       if (!bouncy_distros[i].base.alive)
112         found = i;
113     }
114     
115   if (found == -1)
116   {
117   ++num_bouncy_distros;
118   bouncy_distros = realloc(bouncy_distros,num_bouncy_distros*sizeof(bouncy_distro_type));
119   found = num_bouncy_distros - 1;
120   }
121     
122   if (found != -1)
123     {
124         bouncy_distro_init(&bouncy_distros[found],x,y);
125     }
126 }
127
128
129 /* Add broken brick pieces: */
130
131 void add_broken_brick(float x, float y)
132 {
133   add_broken_brick_piece(x, y, -1, -4);
134   add_broken_brick_piece(x, y + 16, -1.5, -3);
135
136   add_broken_brick_piece(x + 16, y, 1, -4);
137   add_broken_brick_piece(x + 16, y + 16, 1.5, -3);
138 }
139
140
141 /* Add a broken brick piece: */
142
143 void add_broken_brick_piece(float x, float y, float xm, float ym)
144 {
145   int i, found;
146
147   found = -1;
148
149   for (i = 0; i < num_broken_bricks && found == -1; i++)
150     {
151       if (!broken_bricks[i].base.alive)
152         found = i;
153     }
154
155   if (found == -1)
156   {
157   ++num_broken_bricks;
158   broken_bricks = realloc(broken_bricks,num_broken_bricks*sizeof(broken_brick_type));
159   found = num_broken_bricks - 1;
160   }
161
162   if (found != -1)
163     {
164        broken_brick_init(&broken_bricks[found], x, y, xm, ym);
165     }
166 }
167
168
169 /* Add a bouncy brick piece: */
170
171 void add_bouncy_brick(float x, float y)
172 {
173   int i, found;
174
175   found = -1;
176
177   for (i = 0; i < num_bouncy_bricks && found == -1; i++)
178     {
179       if (!bouncy_bricks[i].base.alive)
180         found = i;
181     }
182     
183   if (found == -1)
184   {
185   ++num_bouncy_bricks;
186   bouncy_bricks = realloc(bouncy_bricks,num_bouncy_bricks*sizeof(bouncy_brick_type));
187   found = num_bouncy_bricks - 1;
188   }
189
190   if (found != -1)
191     {
192       bouncy_brick_init(&bouncy_bricks[found],x,y);
193     }
194 }
195
196
197 /* Add a bad guy: */
198
199 void add_bad_guy(float x, float y, int kind)
200 {
201   int i, found;
202
203   found = -1;
204
205   for (i = 0; i < num_bad_guys && found == -1; i++)
206     {
207       if (!bad_guys[i].base.alive)
208         found = i;
209     }
210  
211   if (found == -1)
212   {
213   ++num_bad_guys;
214   bad_guys = realloc(bad_guys,num_bad_guys*sizeof(bad_guy_type));
215   found = num_bad_guys - 1;
216   }
217
218   if (found != -1)
219     {
220        badguy_init(&bad_guys[found], x, y, kind);
221     }
222 }
223
224 /* Add an upgrade: */
225
226 void add_upgrade(float x, float y, int kind)
227 {
228   int i, found;
229
230   found = -1;
231
232   for (i = 0; i < num_upgrades && found == -1; i++)
233     {
234       if (!upgrades[i].base.alive)
235         found = i;
236     }
237
238   if (found == -1)
239   {
240   ++num_upgrades;
241   upgrades = realloc(upgrades,num_upgrades*sizeof(upgrade_type));
242   found = num_upgrades - 1;
243   }
244
245   if (found != -1)
246     {
247       upgrade_init(&upgrades[found], x, y, kind);
248     }
249 }
250
251 /* Add a bullet: */
252
253 void add_bullet(float x, float y, float xm, int dir)
254 {
255   int i, found;
256   
257   found = -1;
258
259   for (i = 0; i < num_bullets && found == -1; i++)
260     {
261       if (!bullets[i].base.alive)
262         found = i;
263     }
264
265   if (found == -1)
266   {
267   ++num_bullets;
268   bullets = realloc(bullets,num_bullets*sizeof(bullet_type));
269   found = num_bullets - 1;
270   }
271
272   if (found != -1)
273     {
274       bullet_init(&bullets[found], x, y, xm, dir);
275
276       play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
277     }
278 }
279