MatzeB: that shoudl fix the cases where you die when picking up an iceflower
[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   apply_bonuses();
57 }
58
59 World::World(const std::string& subset, int level_nr)
60 {
61   // FIXME: Move this to action and draw and everywhere else where the
62   // world calls child functions
63   current_ = this;
64
65   level = new Level(subset, level_nr);
66   tux.init();
67
68   set_defaults();
69
70   get_level()->load_gfx();
71   activate_bad_guys();
72   activate_particle_systems();
73   get_level()->load_song();
74
75   apply_bonuses();
76 }
77
78 void
79 World::apply_bonuses()
80 {
81   // Apply bonuses from former levels
82   switch (player_status.bonus)
83     {
84     case PlayerStatus::NO_BONUS:
85       break;
86
87     case PlayerStatus::FLOWER_BONUS:
88       tux.got_coffee = true;
89       // fall through
90
91     case PlayerStatus::GROWUP_BONUS:
92       // FIXME: Move this to Player class
93       tux.size = BIG;
94       tux.base.height = 64;
95       tux.base.y -= 32;
96       break;
97     }
98 }
99
100 World::~World()
101 {
102   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
103     delete *i;
104
105   for (ParticleSystems::iterator i = particle_systems.begin();
106           i != particle_systems.end(); ++i)
107     delete *i;
108   
109   delete level;
110 }
111
112 void
113 World::set_defaults()
114 {
115   // Set defaults: 
116   scroll_x = 0;
117
118   player_status.score_multiplier = 1;
119
120   counting_distros = false;
121   distro_counter = 0;
122
123   /* set current song/music */
124   currentmusic = LEVEL_MUSIC;
125 }
126
127 void
128 World::activate_bad_guys()
129 {
130   for (std::vector<BadGuyData>::iterator i = level->badguy_data.begin();
131        i != level->badguy_data.end();
132        ++i)
133     {
134       add_bad_guy(i->x, i->y, i->kind, i->stay_on_platform);
135     }
136 }
137
138 void
139 World::activate_particle_systems()
140 {
141   if (level->particle_system == "clouds")
142     {
143       particle_systems.push_back(new CloudParticleSystem);
144     }
145   else if (level->particle_system == "snow")
146     {
147       particle_systems.push_back(new SnowParticleSystem);
148     }
149   else if (level->particle_system != "")
150     {
151       st_abort("unknown particle system specified in level", "");
152     }
153 }
154
155 void
156 World::draw()
157 {
158   int y,x;
159
160   /* Draw the real background */
161   if(get_level()->bkgd_image[0] != '\0')
162     {
163       int s = ((int)scroll_x / 2)%640;
164       level->img_bkgd->draw_part(s, 0,0,0,level->img_bkgd->w - s, level->img_bkgd->h);
165       level->img_bkgd->draw_part(0, 0,screen->w - s ,0,s,level->img_bkgd->h);
166     }
167   else
168     {
169       drawgradient(level->bkgd_top, level->bkgd_bottom);
170     }
171     
172   /* Draw particle systems (background) */
173   std::vector<ParticleSystem*>::iterator p;
174   for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
175     {
176       (*p)->draw(scroll_x, 0, 0);
177     }
178
179   /* Draw background: */
180   for (y = 0; y < 15; ++y)
181     {
182       for (x = 0; x < 21; ++x)
183         {
184           Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
185                      level->bg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
186         }
187     }
188
189   /* Draw interactive tiles: */
190   for (y = 0; y < 15; ++y)
191     {
192       for (x = 0; x < 21; ++x)
193         {
194           Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
195                      level->ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
196         }
197     }
198
199   /* (Bouncy bricks): */
200   for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
201     bouncy_bricks[i]->draw();
202
203   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
204     (*i)->draw();
205
206   tux.draw();
207
208   for (unsigned int i = 0; i < bullets.size(); ++i)
209     bullets[i].draw();
210
211   for (unsigned int i = 0; i < floating_scores.size(); ++i)
212     floating_scores[i]->draw();
213
214   for (unsigned int i = 0; i < upgrades.size(); ++i)
215     upgrades[i].draw();
216
217   for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
218     bouncy_distros[i]->draw();
219
220   for (unsigned int i = 0; i < broken_bricks.size(); ++i)
221     broken_bricks[i]->draw();
222
223   /* Draw foreground: */
224   for (y = 0; y < 15; ++y)
225     {
226       for (x = 0; x < 21; ++x)
227         {
228           Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
229                      level->fg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
230         }
231     }
232
233   /* Draw particle systems (foreground) */
234   for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
235     {
236       (*p)->draw(scroll_x, 0, 1);
237     }
238 }
239
240 void
241 World::action(double frame_ratio)
242 {
243   tux.action(frame_ratio);
244
245   /* Handle bouncy distros: */
246   for (unsigned int i = 0; i < bouncy_distros.size(); i++)
247     bouncy_distros[i]->action(frame_ratio);
248
249   /* Handle broken bricks: */
250   for (unsigned int i = 0; i < broken_bricks.size(); i++)
251     broken_bricks[i]->action(frame_ratio);
252
253   /* Handle distro counting: */
254   if (counting_distros)
255     {
256       distro_counter--;
257
258       if (distro_counter <= 0)
259         counting_distros = -1;
260     }
261
262   // Handle all kinds of game objects
263   for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
264     bouncy_bricks[i]->action(frame_ratio);
265   
266   for (unsigned int i = 0; i < floating_scores.size(); i++)
267     floating_scores[i]->action(frame_ratio);
268
269   for (unsigned int i = 0; i < bullets.size(); ++i)
270     bullets[i].action(frame_ratio);
271   
272   for (unsigned int i = 0; i < upgrades.size(); i++)
273     upgrades[i].action(frame_ratio);
274
275   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
276     (*i)->action(frame_ratio);
277
278   /* update particle systems */
279   std::vector<ParticleSystem*>::iterator p;
280   for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
281     {
282       (*p)->simulate(frame_ratio);
283     }
284
285   /* Handle all possible collisions. */
286   collision_handler();
287   
288   // Cleanup marked badguys
289   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end();
290       /* ++i handled at end of the loop */) {
291     if ((*i)->is_removable()) {
292       delete *i;
293       i = bad_guys.erase(i);
294     } else {
295       ++i;
296     }
297   }
298 }
299
300 void
301 World::collision_handler()
302 {
303   // CO_BULLET & CO_BADGUY check
304   for(unsigned int i = 0; i < bullets.size(); ++i)
305     {
306       for (BadGuys::iterator j = bad_guys.begin(); j != bad_guys.end(); ++j)
307         {
308           if((*j)->dying != DYING_NOT)
309             continue;
310           
311           if(rectcollision(bullets[i].base, (*j)->base))
312             {
313               // We have detected a collision and now call the
314               // collision functions of the collided objects.
315               // collide with bad_guy first, since bullet_collision will
316               // delete the bullet
317               (*j)->collision(0, CO_BULLET);
318               bullets[i].collision(CO_BADGUY);
319               break; // bullet is invalid now, so break
320             }
321         }
322     }
323
324   /* CO_BADGUY & CO_BADGUY check */
325   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
326     {
327       if((*i)->dying != DYING_NOT)
328         continue;
329       
330       BadGuys::iterator j = i;
331       ++j;
332       for (; j != bad_guys.end(); ++j)
333         {
334           if(j == i || (*j)->dying != DYING_NOT)
335             continue;
336
337           if(rectcollision((*i)->base, (*j)->base))
338             {
339               // We have detected a collision and now call the
340               // collision functions of the collided objects.
341               (*j)->collision(*i, CO_BADGUY);
342               (*i)->collision(*j, CO_BADGUY);
343             }
344         }
345     }
346
347   if(tux.dying != DYING_NOT) return;
348     
349   // CO_BADGUY & CO_PLAYER check 
350   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
351     {
352       if((*i)->dying != DYING_NOT)
353         continue;
354       
355       if(rectcollision_offset((*i)->base, tux.base, 0, 0))
356         {
357           // We have detected a collision and now call the collision
358           // functions of the collided objects.
359           if (tux.previous_base.y < tux.base.y &&
360               tux.previous_base.y + tux.previous_base.height 
361               < (*i)->base.y + (*i)->base.height/2)
362             {
363               (*i)->collision(&tux, CO_PLAYER, COLLISION_SQUISH);
364             }
365           else
366             {
367               tux.collision(*i, CO_BADGUY);
368               (*i)->collision(&tux, CO_PLAYER, COLLISION_NORMAL);
369             }
370         }
371     }
372
373   // CO_UPGRADE & CO_PLAYER check
374   for(unsigned int i = 0; i < upgrades.size(); ++i)
375     {
376       if(rectcollision(upgrades[i].base, tux.base))
377         {
378           // We have detected a collision and now call the collision
379           // functions of the collided objects.
380           upgrades[i].collision(&tux, CO_PLAYER, COLLISION_NORMAL);
381         }
382     }
383 }
384
385 void
386 World::add_score(float x, float y, int s)
387 {
388   player_status.score += s;
389
390   FloatingScore* new_floating_score = new FloatingScore();
391   new_floating_score->init(x,y,s);
392   floating_scores.push_back(new_floating_score);
393 }
394
395 void
396 World::add_bouncy_distro(float x, float y)
397 {
398   BouncyDistro* new_bouncy_distro = new BouncyDistro();
399   new_bouncy_distro->init(x,y);
400   bouncy_distros.push_back(new_bouncy_distro);
401 }
402
403 void
404 World::add_broken_brick(Tile* tile, float x, float y)
405 {
406   add_broken_brick_piece(tile, x, y, -1, -4);
407   add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
408
409   add_broken_brick_piece(tile, x + 16, y, 1, -4);
410   add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
411 }
412
413 void
414 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
415 {
416   BrokenBrick* new_broken_brick = new BrokenBrick();
417   new_broken_brick->init(tile, x, y, xm, ym);
418   broken_bricks.push_back(new_broken_brick);
419 }
420
421 void
422 World::add_bouncy_brick(float x, float y)
423 {
424   BouncyBrick* new_bouncy_brick = new BouncyBrick();
425   new_bouncy_brick->init(x,y);
426   bouncy_bricks.push_back(new_bouncy_brick);
427 }
428
429 BadGuy*
430 World::add_bad_guy(float x, float y, BadGuyKind kind, bool stay_on_platform)
431 {
432   BadGuy* badguy = new BadGuy(x,y,kind, stay_on_platform);
433   bad_guys.push_back(badguy);
434   return badguy;
435 }
436
437 void
438 World::add_upgrade(float x, float y, Direction dir, UpgradeKind kind)
439 {
440   Upgrade new_upgrade;
441   new_upgrade.init(x,y,dir,kind);
442   upgrades.push_back(new_upgrade);
443 }
444
445 void 
446 World::add_bullet(float x, float y, float xm, Direction dir)
447 {
448   if(bullets.size() > MAX_BULLETS-1)
449     return;
450
451   Bullet new_bullet;
452   new_bullet.init(x,y,xm,dir);
453   bullets.push_back(new_bullet);
454   
455   play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
456 }
457
458 void
459 World::play_music(int musictype)
460 {
461   currentmusic = musictype;
462   switch(currentmusic) {
463     case HURRYUP_MUSIC:
464       music_manager->play_music(get_level()->get_level_music_fast());
465       break;
466     case LEVEL_MUSIC:
467       music_manager->play_music(get_level()->get_level_music());
468       break;
469     case HERRING_MUSIC:
470       music_manager->play_music(herring_song);
471       break;
472     default:
473       music_manager->halt_music();
474       break;
475   }
476 }
477
478 int
479 World::get_music_type()
480 {
481   return currentmusic;
482 }
483
484 /* Break a brick: */
485 void
486 World::trybreakbrick(float x, float y, bool small)
487 {
488   Level* plevel = get_level();
489   
490   Tile* tile = gettile(x, y);
491   if (tile->brick)
492     {
493       if (tile->data > 0)
494         {
495           /* Get a distro from it: */
496           add_bouncy_distro(((int)(x + 1) / 32) * 32,
497                                   (int)(y / 32) * 32);
498
499           if (!counting_distros)
500             {
501               counting_distros = true;
502               distro_counter = 50;
503             }
504
505           if (distro_counter <= 0)
506             plevel->change(x, y, TM_IA, tile->next_tile);
507
508           play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
509           player_status.score = player_status.score + SCORE_DISTRO;
510           player_status.distros++;
511         }
512       else if (!small)
513         {
514           /* Get rid of it: */
515           plevel->change(x, y, TM_IA, tile->next_tile);
516           
517           /* Replace it with broken bits: */
518           add_broken_brick(tile, 
519                                  ((int)(x + 1) / 32) * 32,
520                                  (int)(y / 32) * 32);
521           
522           /* Get some score: */
523           play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
524           player_status.score = player_status.score + SCORE_BRICK;
525         }
526     }
527 }
528
529 /* Empty a box: */
530 void
531 World::tryemptybox(float x, float y, Direction col_side)
532 {
533   Tile* tile = gettile(x,y);
534   if (!tile->fullbox)
535     return;
536
537   // according to the collision side, set the upgrade direction
538   if(col_side == LEFT)
539     col_side = RIGHT;
540   else
541     col_side = LEFT;
542
543   int posx = ((int)(x+1) / 32) * 32;
544   int posy = (int)(y/32) * 32 - 32;
545   switch(tile->data)
546     {
547     case 1: // Box with a distro!
548       add_bouncy_distro(posx, posy);
549       play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
550       player_status.score = player_status.score + SCORE_DISTRO;
551       player_status.distros++;
552       break;
553
554     case 2: // Add an upgrade!
555       if (tux.size == SMALL)     /* Tux is small, add mints! */
556         add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
557       else     /* Tux is big, add an iceflower: */
558         add_upgrade(posx, posy, col_side, UPGRADE_ICEFLOWER);
559       play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
560       break;
561
562     case 3: // Add a golden herring
563       add_upgrade(posx, posy, col_side, UPGRADE_HERRING);
564       break;
565
566     case 4: // Add a 1up extra
567       add_upgrade(posx, posy, col_side, UPGRADE_1UP);
568       break;
569     default:
570       break;
571     }
572
573   /* Empty the box: */
574   level->change(x, y, TM_IA, tile->next_tile);
575 }
576
577 /* Try to grab a distro: */
578 void
579 World::trygrabdistro(float x, float y, int bounciness)
580 {
581   Tile* tile = gettile(x, y);
582   if (tile && tile->distro)
583     {
584       level->change(x, y, TM_IA, tile->next_tile);
585       play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
586
587       if (bounciness == BOUNCE)
588         {
589           add_bouncy_distro(((int)(x + 1) / 32) * 32,
590                                   (int)(y / 32) * 32);
591         }
592
593       player_status.score = player_status.score + SCORE_DISTRO;
594       player_status.distros++;
595     }
596 }
597
598 /* Try to bump a bad guy from below: */
599 void
600 World::trybumpbadguy(float x, float y)
601 {
602   // Bad guys: 
603   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
604     {
605       if ((*i)->base.x >= x - 32 && (*i)->base.x <= x + 32 &&
606           (*i)->base.y >= y - 16 && (*i)->base.y <= y + 16)
607         {
608           (*i)->collision(&tux, CO_PLAYER, COLLISION_BUMP);
609         }
610     }
611
612   // Upgrades:
613   for (unsigned int i = 0; i < upgrades.size(); i++)
614     {
615       if (upgrades[i].base.height == 32 &&
616           upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
617           upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
618         {
619           upgrades[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);
620         }
621     }
622 }
623
624 /* EOF */
625