Added a new concept to the menu, the ID.
[supertux.git] / src / world.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
5 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
6 //  Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
7 //
8 //  This program is free software; you can redistribute it and/or
9 //  modify it under the terms of the GNU General Public License
10 //  as published by the Free Software Foundation; either version 2
11 //  of the License, or (at your option) any later version.
12 //
13 //  This program is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 //  GNU General Public License for more details.
17 // 
18 //  You should have received a copy of the GNU General Public License
19 //  along with this program; if not, write to the Free Software
20 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 //  02111-1307, USA.
22
23 #include <iostream>
24 #include <math.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "globals.h"
28 #include "scene.h"
29 #include "screen.h"
30 #include "defines.h"
31 #include "world.h"
32 #include "level.h"
33 #include "tile.h"
34 #include "resources.h"
35
36 Surface* img_distro[4];
37
38 World* World::current_ = 0;
39
40 World::World(const std::string& filename)
41 {
42   // FIXME: Move this to action and draw and everywhere else where the
43   // world calls child functions
44   current_ = this;
45
46   level = new Level(filename);
47   tux.init();
48
49   set_defaults();
50
51   get_level()->load_gfx();
52   activate_bad_guys();
53   activate_particle_systems();
54   get_level()->load_song();
55 }
56
57 World::World(const std::string& subset, int level_nr)
58 {
59   // FIXME: Move this to action and draw and everywhere else where the
60   // world calls child functions
61   current_ = this;
62
63   level = new Level(subset, level_nr);
64   tux.init();
65
66   set_defaults();
67
68   get_level()->load_gfx();
69   activate_bad_guys();
70   activate_particle_systems();
71   get_level()->load_song();
72 }
73
74 World::~World()
75 {
76   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
77     delete *i;
78   
79   halt_music(); // just to be sure (because levelmusic is freed now)
80   delete level;
81 }
82
83 void
84 World::set_defaults()
85 {
86   // Set defaults: 
87   scroll_x = 0;
88
89   player_status.score_multiplier = 1;
90
91   counting_distros = false;
92   distro_counter = 0;
93
94   /* set current song/music */
95   currentmusic = LEVEL_MUSIC;
96 }
97
98 void
99 World::activate_bad_guys()
100 {
101   for (std::vector<BadGuyData>::iterator i = level->badguy_data.begin();
102        i != level->badguy_data.end();
103        ++i)
104     {
105       add_bad_guy(i->x, i->y, i->kind, i->stay_on_platform);
106     }
107 }
108
109 void
110 World::activate_particle_systems()
111 {
112   if (level->particle_system == "clouds")
113     {
114       particle_systems.push_back(new CloudParticleSystem);
115     }
116   else if (level->particle_system == "snow")
117     {
118       particle_systems.push_back(new SnowParticleSystem);
119     }
120   else if (level->particle_system != "")
121     {
122       st_abort("unknown particle system specified in level", "");
123     }
124 }
125
126 void
127 World::draw()
128 {
129   int y,x;
130
131   /* Draw the real background */
132   if(get_level()->bkgd_image[0] != '\0')
133     {
134       int s = ((int)scroll_x / 2)%640;
135       level->img_bkgd->draw_part(s, 0,0,0,level->img_bkgd->w - s, level->img_bkgd->h);
136       level->img_bkgd->draw_part(0, 0,screen->w - s ,0,s,level->img_bkgd->h);
137     }
138   else
139     {
140       drawgradient(level->bkgd_top, level->bkgd_bottom);
141     }
142     
143   /* Draw particle systems (background) */
144   std::vector<ParticleSystem*>::iterator p;
145   for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
146     {
147       (*p)->draw(scroll_x, 0, 0);
148     }
149
150   /* Draw background: */
151   for (y = 0; y < 15; ++y)
152     {
153       for (x = 0; x < 21; ++x)
154         {
155           Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
156                      level->bg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
157         }
158     }
159
160   /* Draw interactive tiles: */
161   for (y = 0; y < 15; ++y)
162     {
163       for (x = 0; x < 21; ++x)
164         {
165           Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
166                      level->ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
167         }
168     }
169
170   /* (Bouncy bricks): */
171   for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
172     bouncy_bricks[i].draw();
173
174   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
175     (*i)->draw();
176
177   tux.draw();
178
179   for (unsigned int i = 0; i < bullets.size(); ++i)
180     bullets[i].draw();
181
182   for (unsigned int i = 0; i < floating_scores.size(); ++i)
183     floating_scores[i].draw();
184
185   for (unsigned int i = 0; i < upgrades.size(); ++i)
186     upgrades[i].draw();
187
188   for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
189     bouncy_distros[i].draw();
190
191   for (unsigned int i = 0; i < broken_bricks.size(); ++i)
192     broken_bricks[i].draw();
193
194   /* Draw foreground: */
195   for (y = 0; y < 15; ++y)
196     {
197       for (x = 0; x < 21; ++x)
198         {
199           Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
200                      level->fg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
201         }
202     }
203
204   /* Draw particle systems (foreground) */
205   for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
206     {
207       (*p)->draw(scroll_x, 0, 1);
208     }
209 }
210
211 void
212 World::action(double frame_ratio)
213 {
214   tux.action(frame_ratio);
215
216   /* Handle bouncy distros: */
217   for (unsigned int i = 0; i < bouncy_distros.size(); i++)
218     bouncy_distros[i].action(frame_ratio);
219
220   /* Handle broken bricks: */
221   for (unsigned int i = 0; i < broken_bricks.size(); i++)
222     broken_bricks[i].action(frame_ratio);
223
224   /* Handle distro counting: */
225   if (counting_distros)
226     {
227       distro_counter--;
228
229       if (distro_counter <= 0)
230         counting_distros = -1;
231     }
232
233   // Handle all kinds of game objects
234   for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
235     bouncy_bricks[i].action(frame_ratio);
236   
237   for (unsigned int i = 0; i < floating_scores.size(); i++)
238     floating_scores[i].action(frame_ratio);
239
240   for (unsigned int i = 0; i < bullets.size(); ++i)
241     bullets[i].action(frame_ratio);
242   
243   for (unsigned int i = 0; i < upgrades.size(); i++)
244     upgrades[i].action(frame_ratio);
245
246   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
247     (*i)->action(frame_ratio);
248
249   /* update particle systems */
250   std::vector<ParticleSystem*>::iterator p;
251   for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
252     {
253       (*p)->simulate(frame_ratio);
254     }
255
256   /* Handle all possible collisions. */
257   collision_handler();
258   
259   { // Cleanup marked badguys
260     for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
261       if ((*i)->is_removable())
262         delete *i;
263     bad_guys.remove_if(std::mem_fun(&BadGuy::is_removable));
264   }
265 }
266
267 void
268 World::collision_handler()
269 {
270   // CO_BULLET & CO_BADGUY check
271   for(unsigned int i = 0; i < bullets.size(); ++i)
272     {
273       for (BadGuys::iterator j = bad_guys.begin(); j != bad_guys.end(); ++j)
274         {
275           if((*j)->dying != DYING_NOT)
276             continue;
277           
278           if(rectcollision(bullets[i].base, (*j)->base))
279             {
280               // We have detected a collision and now call the
281               // collision functions of the collided objects.
282               // collide with bad_guy first, since bullet_collision will
283               // delete the bullet
284               (*j)->collision(0, CO_BULLET);
285               bullets[i].collision(CO_BADGUY);
286               break; // bullet is invalid now, so break
287             }
288         }
289     }
290
291   /* CO_BADGUY & CO_BADGUY check */
292   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
293     {
294       if((*i)->dying != DYING_NOT)
295         continue;
296       
297       BadGuys::iterator j = i;
298       ++j;
299       for (; j != bad_guys.end(); ++j)
300         {
301           if(j == i || (*j)->dying != DYING_NOT)
302             continue;
303
304           if(rectcollision((*i)->base, (*j)->base))
305             {
306               // We have detected a collision and now call the
307               // collision functions of the collided objects.
308               (*j)->collision(*i, CO_BADGUY);
309               (*i)->collision(*j, CO_BADGUY);
310             }
311         }
312     }
313
314   if(tux.dying != DYING_NOT) return;
315     
316   // CO_BADGUY & CO_PLAYER check 
317   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
318     {
319       if((*i)->dying != DYING_NOT)
320         continue;
321       
322       if(rectcollision_offset((*i)->base, tux.base, 0, 0))
323         {
324           // We have detected a collision and now call the collision
325           // functions of the collided objects.
326           if (tux.previous_base.y < tux.base.y &&
327               tux.previous_base.y + tux.previous_base.height 
328               < (*i)->base.y + (*i)->base.height/2)
329             {
330               (*i)->collision(&tux, CO_PLAYER, COLLISION_SQUISH);
331             }
332           else
333             {
334               tux.collision(*i, CO_BADGUY);
335               (*i)->collision(&tux, CO_PLAYER, COLLISION_NORMAL);
336             }
337         }
338     }
339
340   // CO_UPGRADE & CO_PLAYER check
341   for(unsigned int i = 0; i < upgrades.size(); ++i)
342     {
343       if(rectcollision(upgrades[i].base, tux.base))
344         {
345           // We have detected a collision and now call the collision
346           // functions of the collided objects.
347           upgrades[i].collision(&tux, CO_PLAYER);
348         }
349     }
350 }
351
352 void
353 World::add_score(float x, float y, int s)
354 {
355   player_status.score += s;
356
357   FloatingScore new_floating_score;
358   new_floating_score.init(x,y,s);
359   floating_scores.push_back(new_floating_score);
360 }
361
362 void
363 World::add_bouncy_distro(float x, float y)
364 {
365   BouncyDistro new_bouncy_distro;
366   new_bouncy_distro.init(x,y);
367   bouncy_distros.push_back(new_bouncy_distro);
368 }
369
370 void
371 World::add_broken_brick(Tile* tile, float x, float y)
372 {
373   add_broken_brick_piece(tile, x, y, -1, -4);
374   add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
375
376   add_broken_brick_piece(tile, x + 16, y, 1, -4);
377   add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
378 }
379
380 void
381 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
382 {
383   BrokenBrick new_broken_brick;
384   new_broken_brick.init(tile, x, y, xm, ym);
385   broken_bricks.push_back(new_broken_brick);
386 }
387
388 void
389 World::add_bouncy_brick(float x, float y)
390 {
391   BouncyBrick new_bouncy_brick;
392   new_bouncy_brick.init(x,y);
393   bouncy_bricks.push_back(new_bouncy_brick);
394 }
395
396 BadGuy*
397 World::add_bad_guy(float x, float y, BadGuyKind kind, bool stay_on_platform)
398 {
399   BadGuy* badguy = new BadGuy(x,y,kind, stay_on_platform);
400   bad_guys.push_back(badguy);
401   return badguy;
402 }
403
404 void
405 World::add_upgrade(float x, float y, Direction dir, UpgradeKind kind)
406 {
407   Upgrade new_upgrade;
408   new_upgrade.init(x,y,dir,kind);
409   upgrades.push_back(new_upgrade);
410 }
411
412 void 
413 World::add_bullet(float x, float y, float xm, Direction dir)
414 {
415   if(bullets.size() > MAX_BULLETS-1)
416     return;
417
418   Bullet new_bullet;
419   new_bullet.init(x,y,xm,dir);
420   bullets.push_back(new_bullet);
421   
422   play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
423 }
424
425 void
426 World::play_music(int musictype)
427 {
428   currentmusic = musictype;
429   switch(currentmusic) {
430     case HURRYUP_MUSIC:
431       ::play_music(get_level()->get_level_music_fast());
432       break;
433     case LEVEL_MUSIC:
434       ::play_music(get_level()->get_level_music());
435       break;
436     case HERRING_MUSIC:
437       ::play_music(herring_song);
438       break;
439     default:
440       ::halt_music();
441       break;
442   }
443 }
444
445 int
446 World::get_music_type()
447 {
448   return currentmusic;
449 }
450
451 /* Break a brick: */
452 void
453 World::trybreakbrick(float x, float y, bool small)
454 {
455   Level* plevel = get_level();
456   
457   Tile* tile = gettile(x, y);
458   if (tile->brick)
459     {
460       if (tile->data > 0)
461         {
462           /* Get a distro from it: */
463           add_bouncy_distro(((int)(x + 1) / 32) * 32,
464                                   (int)(y / 32) * 32);
465
466           if (!counting_distros)
467             {
468               counting_distros = true;
469               distro_counter = 50;
470             }
471
472           if (distro_counter <= 0)
473             plevel->change(x, y, TM_IA, tile->next_tile);
474
475           play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
476           player_status.score = player_status.score + SCORE_DISTRO;
477           player_status.distros++;
478         }
479       else if (!small)
480         {
481           /* Get rid of it: */
482           plevel->change(x, y, TM_IA, tile->next_tile);
483           
484           /* Replace it with broken bits: */
485           add_broken_brick(tile, 
486                                  ((int)(x + 1) / 32) * 32,
487                                  (int)(y / 32) * 32);
488           
489           /* Get some score: */
490           play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
491           player_status.score = player_status.score + SCORE_BRICK;
492         }
493     }
494 }
495
496 /* Empty a box: */
497 void
498 World::tryemptybox(float x, float y, Direction col_side)
499 {
500   Tile* tile = gettile(x,y);
501   if (!tile->fullbox)
502     return;
503
504   // according to the collision side, set the upgrade direction
505   if(col_side == LEFT)
506     col_side = RIGHT;
507   else
508     col_side = LEFT;
509
510   int posx = ((int)(x+1) / 32) * 32;
511   int posy = (int)(y/32) * 32 - 32;
512   switch(tile->data)
513     {
514     case 1: // Box with a distro!
515       add_bouncy_distro(posx, posy);
516       play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
517       player_status.score = player_status.score + SCORE_DISTRO;
518       player_status.distros++;
519       break;
520
521     case 2: // Add an upgrade!
522       if (tux.size == SMALL)     /* Tux is small, add mints! */
523         add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
524       else     /* Tux is big, add an iceflower: */
525         add_upgrade(posx, posy, col_side, UPGRADE_ICEFLOWER);
526       play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
527       break;
528
529     case 3: // Add a golden herring
530       add_upgrade(posx, posy, col_side, UPGRADE_HERRING);
531       break;
532
533     case 4: // Add a 1up extra
534       add_upgrade(posx, posy, col_side, UPGRADE_1UP);
535       break;
536     default:
537       break;
538     }
539
540   /* Empty the box: */
541   level->change(x, y, TM_IA, tile->next_tile);
542 }
543
544 /* Try to grab a distro: */
545 void
546 World::trygrabdistro(float x, float y, int bounciness)
547 {
548   Tile* tile = gettile(x, y);
549   if (tile && tile->distro)
550     {
551       level->change(x, y, TM_IA, tile->next_tile);
552       play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
553
554       if (bounciness == BOUNCE)
555         {
556           add_bouncy_distro(((int)(x + 1) / 32) * 32,
557                                   (int)(y / 32) * 32);
558         }
559
560       player_status.score = player_status.score + SCORE_DISTRO;
561       player_status.distros++;
562     }
563 }
564
565 /* Try to bump a bad guy from below: */
566 void
567 World::trybumpbadguy(float x, float y)
568 {
569   // Bad guys: 
570   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
571     {
572       if ((*i)->base.x >= x - 32 && (*i)->base.x <= x + 32 &&
573           (*i)->base.y >= y - 16 && (*i)->base.y <= y + 16)
574         {
575           (*i)->collision(&tux, CO_PLAYER, COLLISION_BUMP);
576         }
577     }
578
579   // Upgrades:
580   for (unsigned int i = 0; i < upgrades.size(); i++)
581     {
582       if (upgrades[i].base.height == 32 &&
583           upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
584           upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
585         {
586           upgrades[i].base.xm = -upgrades[i].base.xm;
587           upgrades[i].base.ym = -8;
588           play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);
589         }
590     }
591 }
592
593 /* EOF */
594