A few changes to make the code ready for the new level editor.
[supertux.git] / src / sector.cpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2004 Matthias Braun <matze@braunis.de
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <memory>
21 #include <algorithm>
22 #include <stdexcept>
23 #include <iostream>
24 #include <fstream>
25 #include <stdexcept>
26
27 #include "globals.h"
28 #include "sector.h"
29 #include "lispreader.h"
30 #include "badguy.h"
31 #include "special.h"
32 #include "gameobjs.h"
33 #include "camera.h"
34 #include "background.h"
35 #include "particlesystem.h"
36 #include "tile.h"
37 #include "tilemap.h"
38 #include "sound_manager.h"
39 #include "gameloop.h"
40 #include "resources.h"
41 #include "interactive_object.h"
42 #include "door.h"
43
44 Sector* Sector::_current = 0;
45
46 Sector::Sector()
47   : gravity(10), player(0), solids(0), background(0), camera(0),
48     currentmusic(LEVEL_MUSIC)
49 {
50   song_title = "Mortimers_chipdisko.mod";
51   player = new Player();
52   add_object(player);
53 }
54
55 Sector::~Sector()
56 {
57   for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
58       ++i)
59     delete *i;
60
61   for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
62       ++i)
63     delete *i;
64     
65   if(_current == this)
66     _current = 0;
67 }
68
69 void
70 Sector::parse(LispReader& lispreader)
71 {
72   _current = this;
73   
74   for(lisp_object_t* cur = lispreader.get_lisp(); !lisp_nil_p(cur);
75       cur = lisp_cdr(cur)) {
76     std::string token = lisp_symbol(lisp_car(lisp_car(cur)));
77     // FIXME: doesn't handle empty data
78     lisp_object_t* data = lisp_car(lisp_cdr(lisp_car(cur)));
79     LispReader reader(lisp_cdr(lisp_car(cur)));
80
81     if(token == "name") {
82       name = lisp_string(data);
83     } else if(token == "gravity") {
84       gravity = lisp_real(data);
85     } else if(token == "music") {
86       song_title = lisp_string(data);
87       load_music();
88     } else if(token == "camera") {
89       if(camera) {
90         std::cerr << "Warning: More than 1 camera defined in sector.\n";
91         continue;
92       }
93       camera = new Camera(this);
94       camera->read(reader);
95       add_object(camera);
96     } else if(token == "background") {
97       background = new Background(reader);
98       add_object(background);
99     } else if(token == "playerspawn") {
100       SpawnPoint* sp = new SpawnPoint;
101       reader.read_string("name", sp->name);
102       reader.read_float("x", sp->pos.x);
103       reader.read_float("y", sp->pos.y);
104       spawnpoints.push_back(sp);
105     } else if(token == "tilemap") {
106       TileMap* tilemap = new TileMap(reader);
107       add_object(tilemap);
108
109       if(tilemap->is_solid()) {
110         if(solids) {
111           std::cerr << "Warning multiple solid tilemaps in sector.\n";
112           continue;
113         }
114         solids = tilemap;
115       }
116     } else if(badguykind_from_string(token) != BAD_INVALID) {
117       add_object(new BadGuy(badguykind_from_string(token), reader));
118     } else if(token == "trampoline") {
119       add_object(new Trampoline(reader));
120     } else if(token == "flying-platform") {
121       add_object(new FlyingPlatform(reader));
122     } else if(token == "particles-snow") {
123       SnowParticleSystem* partsys = new SnowParticleSystem();
124       partsys->parse(reader);
125       add_object(partsys);
126     } else if(token == "particles-clouds") {
127       CloudParticleSystem* partsys = new CloudParticleSystem();
128       partsys->parse(reader);
129       add_object(partsys);
130     } else if(token == "door") {
131       add_object(new Door(reader));
132     } else {
133       std::cerr << "Unknown object type '" << token << "'.\n";
134     }
135   }
136
137   if(!camera) {
138     std::cerr << "sector '" << name << "' does not contain a camera.\n";
139     camera = new Camera(this);
140     add_object(camera);
141   }
142   if(!solids)
143     throw std::runtime_error("sector does not contain a solid tile layer.");
144 }
145
146 void
147 Sector::parse_old_format(LispReader& reader)
148 {
149   _current = this;
150   
151   name = "main";
152   reader.read_float("gravity", gravity);
153
154   std::string backgroundimage;
155   reader.read_string("background", backgroundimage);
156   float bgspeed = .5;
157   reader.read_float("bkgd_speed", bgspeed);
158
159   Color bkgd_top, bkgd_bottom;
160   int r = 0, g = 0, b = 128;
161   reader.read_int("bkgd_red_top", r);
162   reader.read_int("bkgd_green_top",  g);
163   reader.read_int("bkgd_blue_top",  b);
164   bkgd_top.red = r;
165   bkgd_top.green = g;
166   bkgd_top.blue = b;
167   
168   reader.read_int("bkgd_red_bottom",  r);
169   reader.read_int("bkgd_green_bottom", g);
170   reader.read_int("bkgd_blue_bottom", b);
171   bkgd_bottom.red = r;
172   bkgd_bottom.green = g;
173   bkgd_bottom.blue = b;
174   
175   if(backgroundimage != "") {
176     background = new Background;
177     background->set_image(backgroundimage, bgspeed);
178     add_object(background);
179   } else {
180     background = new Background;
181     background->set_gradient(bkgd_top, bkgd_bottom);
182     add_object(background);
183   }
184
185   std::string particlesystem;
186   reader.read_string("particle_system", particlesystem);
187   if(particlesystem == "clouds")
188     add_object(new CloudParticleSystem());
189   else if(particlesystem == "snow")
190     add_object(new SnowParticleSystem());
191
192   Vector startpos(100, 170);
193   reader.read_float("start_pos_x", startpos.x);
194   reader.read_float("start_pos_y", startpos.y);
195
196   SpawnPoint* spawn = new SpawnPoint;
197   spawn->pos = startpos;
198   spawn->name = "main";
199   spawnpoints.push_back(spawn);
200
201   song_title = "Mortimers_chipdisko.mod";
202   reader.read_string("music", song_title);
203   load_music();
204
205   int width, height = 15;
206   reader.read_int("width", width);
207   reader.read_int("height", height);
208   
209   std::vector<unsigned int> tiles;
210   if(reader.read_int_vector("interactive-tm", tiles)
211       || reader.read_int_vector("tilemap", tiles)) {
212     TileMap* tilemap = new TileMap();
213     tilemap->set(width, height, tiles, LAYER_TILES, true);
214     solids = tilemap;
215     add_object(tilemap);
216   }
217
218   if(reader.read_int_vector("background-tm", tiles)) {
219     TileMap* tilemap = new TileMap();
220     tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
221     add_object(tilemap);
222   }
223
224   if(reader.read_int_vector("foreground-tm", tiles)) {
225     TileMap* tilemap = new TileMap();
226     tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
227     add_object(tilemap);
228   }
229
230   // TODO read resetpoints
231
232   // read objects
233   {
234     lisp_object_t* cur = 0;
235     if(reader.read_lisp("objects", cur)) {
236       while(!lisp_nil_p(cur)) {
237         lisp_object_t* data = lisp_car(cur);
238         std::string object_type = lisp_symbol(lisp_car(data));
239                                                                                 
240         LispReader reader(lisp_cdr(data));
241                                                                                 
242         if(object_type == "trampoline") {
243           add_object(new Trampoline(reader));
244         }
245         else if(object_type == "flying-platform") {
246           add_object(new FlyingPlatform(reader));
247         }
248         else {
249           BadGuyKind kind = badguykind_from_string(object_type);
250           add_object(new BadGuy(kind, reader));
251         }
252                                                                                 
253         cur = lisp_cdr(cur);
254       }
255     }
256   }
257
258   // add a camera
259   camera = new Camera(this);
260   add_object(camera);
261 }
262
263 void
264 Sector::write(LispWriter& writer)
265 {
266   writer.write_string("name", name);
267   writer.write_float("gravity", gravity);
268
269   for(GameObjects::iterator i = gameobjects.begin();
270       i != gameobjects.end(); ++i) {
271     Serializable* serializable = dynamic_cast<Serializable*> (*i);
272     if(serializable)
273       serializable->write(writer);
274   }
275 }
276
277 void
278 Sector::add_object(GameObject* object)
279 {
280   gameobjects_new.push_back(object);
281 }
282
283 void
284 Sector::activate(const std::string& spawnpoint)
285 {
286   _current = this;
287
288   // Apply bonuses from former levels
289   switch (player_status.bonus)
290     {
291     case PlayerStatus::NO_BONUS:
292       break;
293                                                                                 
294     case PlayerStatus::FLOWER_BONUS:
295       player->got_power = Player::FIRE_POWER;  // FIXME: add ice power to here
296       // fall through
297                                                                                 
298     case PlayerStatus::GROWUP_BONUS:
299       player->grow(false);
300       break;
301     }
302
303   SpawnPoint* sp = 0;
304   for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
305       ++i) {
306     if((*i)->name == spawnpoint) {
307       sp = *i;
308       break;
309     }
310   }
311   if(!sp) {
312     std::cerr << "Spawnpoint '" << spawnpoint << "' not found.\n";
313   } else {
314     player->move(sp->pos);
315   }
316
317   camera->reset(Vector(player->base.x, player->base.y));
318 }
319
320 void
321 Sector::action(float elapsed_time)
322 {
323   player->check_bounds(camera);
324                                                                                 
325   /* update objects (don't use iterators here, because the list might change
326    * during the iteration)
327    */
328   for(size_t i = 0; i < gameobjects.size(); ++i)
329     if(gameobjects[i]->is_valid())
330       gameobjects[i]->action(elapsed_time);
331                                                                                 
332   /* Handle all possible collisions. */
333   collision_handler();
334                                                                                 
335   update_game_objects();
336 }
337
338 void
339 Sector::update_game_objects()
340 {
341   /** cleanup marked objects */
342   for(std::vector<GameObject*>::iterator i = gameobjects.begin();
343       i != gameobjects.end(); /* nothing */) {
344     if((*i)->is_valid() == false) {
345       BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
346       if(badguy) {
347         badguys.erase(std::remove(badguys.begin(), badguys.end(), badguy),
348             badguys.end());
349       }
350       Bullet* bullet = dynamic_cast<Bullet*> (*i);
351       if(bullet) {
352         bullets.erase(
353             std::remove(bullets.begin(), bullets.end(), bullet),
354             bullets.end());
355       }
356       InteractiveObject* interactive_object =
357           dynamic_cast<InteractiveObject*> (*i);
358       if(interactive_object) {
359         interactive_objects.erase(
360             std::remove(interactive_objects.begin(), interactive_objects.end(),
361                 interactive_object), interactive_objects.end());
362       }
363       Upgrade* upgrade = dynamic_cast<Upgrade*> (*i);
364       if(upgrade) {
365         upgrades.erase(
366             std::remove(upgrades.begin(), upgrades.end(), upgrade),
367             upgrades.end());
368       }
369       Trampoline* trampoline = dynamic_cast<Trampoline*> (*i);
370       if(trampoline) {
371         trampolines.erase(
372             std::remove(trampolines.begin(), trampolines.end(), trampoline),
373             trampolines.end());
374       }
375       FlyingPlatform* flying_platform= dynamic_cast<FlyingPlatform*> (*i);
376       if(flying_platform) {
377         flying_platforms.erase(
378             std::remove(flying_platforms.begin(), flying_platforms.end(), flying_platform),
379             flying_platforms.end());
380       }
381                                                                                 
382       delete *i;
383       i = gameobjects.erase(i);
384     } else {
385       ++i;
386     }
387   }
388
389   /* add newly created objects */
390   for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
391       i != gameobjects_new.end(); ++i)
392   {
393           BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
394           if(badguy)
395             badguys.push_back(badguy);
396           Bullet* bullet = dynamic_cast<Bullet*> (*i);
397           if(bullet)
398             bullets.push_back(bullet);
399           Upgrade* upgrade = dynamic_cast<Upgrade*> (*i);
400           if(upgrade)
401             upgrades.push_back(upgrade);
402           Trampoline* trampoline = dynamic_cast<Trampoline*> (*i);
403           if(trampoline)
404             trampolines.push_back(trampoline);
405           FlyingPlatform* flying_platform = dynamic_cast<FlyingPlatform*> (*i);
406           if(flying_platform)
407             flying_platforms.push_back(flying_platform);
408           InteractiveObject* interactive_object 
409               = dynamic_cast<InteractiveObject*> (*i);
410           if(interactive_object)
411             interactive_objects.push_back(interactive_object);
412
413           gameobjects.push_back(*i);
414   }
415   gameobjects_new.clear();
416 }
417
418 void
419 Sector::draw(DrawingContext& context)
420 {
421   context.push_transform();
422   context.set_translation(camera->get_translation());
423   
424   for(GameObjects::iterator i = gameobjects.begin();
425       i != gameobjects.end(); ++i) {
426     if( (*i)->is_valid() )
427       (*i)->draw(context);
428   }
429
430   context.pop_transform();
431 }
432
433 void
434 Sector::collision_handler()
435 {
436   // CO_BULLET & CO_BADGUY check
437   for(unsigned int i = 0; i < bullets.size(); ++i)
438     {
439       for (BadGuys::iterator j = badguys.begin(); j != badguys.end(); ++j)
440         {
441           if((*j)->dying != DYING_NOT)
442             continue;
443                                                                                 
444           if(rectcollision(bullets[i]->base, (*j)->base))
445             {
446               // We have detected a collision and now call the
447               // collision functions of the collided objects.
448               (*j)->collision(bullets[i], CO_BULLET, COLLISION_NORMAL);
449               bullets[i]->collision(CO_BADGUY);
450               break; // bullet is invalid now, so break
451             }
452         }
453     }
454                                                                                 
455   /* CO_BADGUY & CO_BADGUY check */
456   for (BadGuys::iterator i = badguys.begin(); i != badguys.end(); ++i)
457     {
458       if((*i)->dying != DYING_NOT)
459         continue;
460                                                                                 
461       BadGuys::iterator j = i;
462       ++j;
463       for (; j != badguys.end(); ++j)
464         {
465           if(j == i || (*j)->dying != DYING_NOT)
466             continue;
467                                                                                 
468           if(rectcollision((*i)->base, (*j)->base))
469             {
470               // We have detected a collision and now call the
471               // collision functions of the collided objects.
472               (*j)->collision(*i, CO_BADGUY);
473               (*i)->collision(*j, CO_BADGUY);
474             }
475         }
476     }
477   if(player->dying != DYING_NOT) return;
478                                                                                 
479   // CO_BADGUY & CO_PLAYER check
480   for (BadGuys::iterator i = badguys.begin(); i != badguys.end(); ++i)
481     {
482       if((*i)->dying != DYING_NOT)
483         continue;
484                                                                                 
485       if(rectcollision_offset((*i)->base, player->base, 0, 0))
486         {
487           // We have detected a collision and now call the collision
488           // functions of the collided objects.
489           if (player->previous_base.y < player->base.y &&
490               player->previous_base.y + player->previous_base.height
491               < (*i)->base.y + (*i)->base.height/2
492               && !player->invincible_timer.started())
493             {
494               (*i)->collision(player, CO_PLAYER, COLLISION_SQUISH);
495             }
496           else
497             {
498               player->collision(*i, CO_BADGUY);
499               (*i)->collision(player, CO_PLAYER, COLLISION_NORMAL);
500             }
501         }
502     }
503                                                                                 
504   // CO_UPGRADE & CO_PLAYER check
505   for(unsigned int i = 0; i < upgrades.size(); ++i)
506     {
507       if(rectcollision(upgrades[i]->base, player->base))
508         {
509           // We have detected a collision and now call the collision
510           // functions of the collided objects.
511           upgrades[i]->collision(player, CO_PLAYER, COLLISION_NORMAL);
512         }
513     }
514                                                                                 
515   // CO_TRAMPOLINE & (CO_PLAYER or CO_BADGUY)
516   for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
517   {
518     if (rectcollision((*i)->base, player->base))
519     {
520       if (player->previous_base.y < player->base.y &&
521           player->previous_base.y + player->previous_base.height
522           < (*i)->base.y + (*i)->base.height/2)
523       {
524         (*i)->collision(player, CO_PLAYER, COLLISION_SQUISH);
525       }
526       else if (player->previous_base.y <= player->base.y)
527       {
528         player->collision(*i, CO_TRAMPOLINE);
529         (*i)->collision(player, CO_PLAYER, COLLISION_NORMAL);
530       }
531     }
532   }
533                                                                                 
534   // CO_FLYING_PLATFORM & (CO_PLAYER or CO_BADGUY)
535   for (FlyingPlatforms::iterator i = flying_platforms.begin(); i != flying_platforms.end(); ++i)
536   {
537     if (rectcollision((*i)->base, player->base))
538     {
539       if (player->previous_base.y < player->base.y &&
540           player->previous_base.y + player->previous_base.height
541           < (*i)->base.y + (*i)->base.height/2)
542       {
543         (*i)->collision(player, CO_PLAYER, COLLISION_SQUISH);
544         player->collision(*i, CO_FLYING_PLATFORM);
545       }
546 /*      else if (player->previous_base.y <= player->base.y)
547       {
548       }*/
549     }
550   }
551 }
552
553 void
554 Sector::add_score(const Vector& pos, int s)
555 {
556   player_status.score += s;
557                                                                                 
558   add_object(new FloatingScore(pos, s));
559 }
560                                                                                 
561 void
562 Sector::add_bouncy_distro(const Vector& pos)
563 {
564   add_object(new BouncyDistro(pos));
565 }
566                                                                                 
567 void
568 Sector::add_broken_brick(const Vector& pos, Tile* tile)
569 {
570   add_broken_brick_piece(pos, Vector(-1, -4), tile);
571   add_broken_brick_piece(pos + Vector(0, 16), Vector(-1.5, -3), tile);
572                                                                                 
573   add_broken_brick_piece(pos + Vector(16, 0), Vector(1, -4), tile);
574   add_broken_brick_piece(pos + Vector(16, 16), Vector(1.5, -3), tile);
575 }
576                                                                                 
577 void
578 Sector::add_broken_brick_piece(const Vector& pos, const Vector& movement,
579     Tile* tile)
580 {
581   add_object(new BrokenBrick(tile, pos, movement));
582 }
583                                                                                 
584 void
585 Sector::add_bouncy_brick(const Vector& pos)
586 {
587   add_object(new BouncyBrick(pos));
588 }
589
590 BadGuy*
591 Sector::add_bad_guy(float x, float y, BadGuyKind kind)
592 {
593   BadGuy* badguy = new BadGuy(kind, x, y);
594   add_object(badguy);
595   return badguy;
596 }
597                                                                                 
598 void
599 Sector::add_upgrade(const Vector& pos, Direction dir, UpgradeKind kind)
600 {
601   add_object(new Upgrade(pos, dir, kind));
602 }
603                                                                                 
604 bool
605 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
606 {
607   if(player->got_power == Player::FIRE_POWER)
608     {
609     if(bullets.size() > MAX_FIRE_BULLETS-1)
610       return false;
611     }
612   else if(player->got_power == Player::ICE_POWER)
613     {
614     if(bullets.size() > MAX_ICE_BULLETS-1)
615       return false;
616     }
617                                                                                 
618   Bullet* new_bullet = 0;
619   if(player->got_power == Player::FIRE_POWER)
620     new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET);
621   else if(player->got_power == Player::ICE_POWER)
622     new_bullet = new Bullet(pos, xm, dir, ICE_BULLET);
623   else
624     throw std::runtime_error("wrong bullet type.");
625   add_object(new_bullet);
626                                                                                 
627   sound_manager->play_sound(sounds[SND_SHOOT]);
628                                                                                 
629   return true;
630 }
631
632 /* Break a brick: */
633 bool
634 Sector::trybreakbrick(const Vector& pos, bool small)
635 {
636   Tile* tile = solids->get_tile_at(pos);
637   if (!tile)
638   {
639     char errmsg[64];
640     sprintf(errmsg, "Invalid tile at %i,%i", (int)((pos.x+1)/32*32), (int)((pos.y+1)/32*32));
641     throw SuperTuxException(errmsg, __FILE__, __LINE__);
642   }
643
644   if (tile->attributes & Tile::BRICK)
645     {
646       if (tile->data > 0)
647         {
648           /* Get a distro from it: */
649           add_bouncy_distro(
650               Vector(((int)(pos.x + 1) / 32) * 32, (int)(pos.y / 32) * 32));
651                                                                                 
652           // TODO: don't handle this in a global way but per-tile...
653           if (!counting_distros)
654             {
655               counting_distros = true;
656               distro_counter = 5;
657             }
658           else
659             {
660               distro_counter--;
661             }
662                                                                                 
663           if (distro_counter <= 0)
664             {
665               counting_distros = false;
666               solids->change_at(pos, tile->next_tile);
667             }
668                                                                                 
669           sound_manager->play_sound(sounds[SND_DISTRO]);
670           player_status.score = player_status.score + SCORE_DISTRO;
671           player_status.distros++;
672           return true;
673         }
674       else if (!small)
675         {
676           /* Get rid of it: */
677           solids->change_at(pos, tile->next_tile);
678                                                                                 
679           /* Replace it with broken bits: */
680           add_broken_brick(Vector(
681                                  ((int)(pos.x + 1) / 32) * 32,
682                                  (int)(pos.y / 32) * 32), tile);
683                                                                                 
684           /* Get some score: */
685           sound_manager->play_sound(sounds[SND_BRICK]);
686           player_status.score = player_status.score + SCORE_BRICK;
687                                                                                 
688           return true;
689         }
690     }
691                                                                                 
692   return false;
693 }
694                                                                                 
695 /* Empty a box: */
696 void
697 Sector::tryemptybox(const Vector& pos, Direction col_side)
698 {
699   Tile* tile = solids->get_tile_at(pos);
700   if (!tile)
701   {
702     char errmsg[64];
703     sprintf(errmsg, "Invalid tile at %i,%i", (int)((pos.x+1)/32*32), (int)((pos.y+1)/32*32));
704     throw SuperTuxException(errmsg, __FILE__, __LINE__);
705   }
706
707
708   if (!(tile->attributes & Tile::FULLBOX))
709     return;
710                                                                                 
711   // according to the collision side, set the upgrade direction
712   if(col_side == LEFT)
713     col_side = RIGHT;
714   else
715     col_side = LEFT;
716                                                                                 
717   int posx = ((int)(pos.x+1) / 32) * 32;
718   int posy = (int)(pos.y/32) * 32 - 32;
719   switch(tile->data)
720     {
721     case 1: // Box with a distro!
722       add_bouncy_distro(Vector(posx, posy));
723       sound_manager->play_sound(sounds[SND_DISTRO]);
724       player_status.score = player_status.score + SCORE_DISTRO;
725       player_status.distros++;
726       break;
727                                                                                 
728     case 2: // Add a fire flower upgrade!
729       if (player->size == SMALL)     /* Tux is small, add mints! */
730         add_upgrade(Vector(posx, posy), col_side, UPGRADE_GROWUP);
731       else     /* Tux is big, add a fireflower: */
732         add_upgrade(Vector(posx, posy), col_side, UPGRADE_FIREFLOWER);
733       sound_manager->play_sound(sounds[SND_UPGRADE]);
734       break;
735                                                                                 
736     case 5: // Add an ice flower upgrade!
737       if (player->size == SMALL)     /* Tux is small, add mints! */
738         add_upgrade(Vector(posx, posy), col_side, UPGRADE_GROWUP);
739       else     /* Tux is big, add an iceflower: */
740         add_upgrade(Vector(posx, posy), col_side, UPGRADE_ICEFLOWER);
741       sound_manager->play_sound(sounds[SND_UPGRADE]);
742       break;
743                                                                                 
744     case 3: // Add a golden herring
745       add_upgrade(Vector(posx, posy), col_side, UPGRADE_HERRING);
746       break;
747                                                                                 
748     case 4: // Add a 1up extra
749       add_upgrade(Vector(posx, posy), col_side, UPGRADE_1UP);
750       break;
751     default:
752       break;
753     }
754                                                                                 
755   /* Empty the box: */
756   solids->change_at(pos, tile->next_tile);
757 }
758                                                                                 
759 /* Try to grab a distro: */
760 void
761 Sector::trygrabdistro(const Vector& pos, int bounciness)
762 {
763   Tile* tile = solids->get_tile_at(pos);
764   if (!tile)
765   {
766     /*char errmsg[64];
767     sprintf(errmsg, "Invalid tile at %i,%i", (int)((pos.x+1)/32*32), (int)((pos.y+1)/32*32));
768     throw SuperTuxException(errmsg, __FILE__, __LINE__); */
769     
770     //Bad tiles (i.e. tiles that are not defined in supertux.stgt but appear in the map) are changed to ID 0 (blank tile)
771     std::cout << "Warning: Undefined tile at " <<(int)pos.x/32 << "/" << (int)pos.y/32 << " (ID: " << (int)solids->get_tile_id_at(pos).id << ")" << std::endl;
772     solids->change_at(pos,0);
773     tile = solids->get_tile_at(pos);
774   }
775
776
777   if (!(tile->attributes & Tile::COIN))
778     return;
779
780   solids->change_at(pos, tile->next_tile);
781   sound_manager->play_sound(sounds[SND_DISTRO]);
782                                                                             
783   if (bounciness == BOUNCE)
784     {
785       add_bouncy_distro(Vector(((int)(pos.x + 1) / 32) * 32,
786                               (int)(pos.y / 32) * 32));
787     }
788                                                                             
789   player_status.score = player_status.score + SCORE_DISTRO;
790   player_status.distros++;
791
792 }
793                                                                                 
794 /* Try to bump a bad guy from below: */
795 void
796 Sector::trybumpbadguy(const Vector& pos)
797 {
798   // Bad guys:
799   for (BadGuys::iterator i = badguys.begin(); i != badguys.end(); ++i)
800     {
801       if ((*i)->base.x >= pos.x - 32 && (*i)->base.x <= pos.x + 32 &&
802           (*i)->base.y >= pos.y - 16 && (*i)->base.y <= pos.y + 16)
803         {
804           (*i)->collision(player, CO_PLAYER, COLLISION_BUMP);
805         }
806     }
807                                                                                 
808   // Upgrades:
809   for (unsigned int i = 0; i < upgrades.size(); i++)
810     {
811       if (upgrades[i]->base.height == 32 &&
812           upgrades[i]->base.x >= pos.x - 32 && upgrades[i]->base.x <= pos.x + 32 &&
813           upgrades[i]->base.y >= pos.y - 16 && upgrades[i]->base.y <= pos.y + 16)
814         {
815           upgrades[i]->collision(player, CO_PLAYER, COLLISION_BUMP);
816         }
817     }
818 }
819
820 void
821 Sector::load_music()
822 {
823   char* song_path;
824   char* song_subtitle;
825                                                                                 
826   level_song = sound_manager->load_music(datadir + "/music/" + song_title);
827                                                                                 
828   song_path = (char *) malloc(sizeof(char) * datadir.length() +
829                               strlen(song_title.c_str()) + 8 + 5);
830   song_subtitle = strdup(song_title.c_str());
831   strcpy(strstr(song_subtitle, "."), "\0");
832   sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(),
833           song_subtitle, strstr(song_title.c_str(), "."));
834   if(!sound_manager->exists_music(song_path)) {
835     level_song_fast = level_song;
836   } else {
837     level_song_fast = sound_manager->load_music(song_path);
838   }
839   free(song_subtitle);
840   free(song_path);
841 }
842
843 void
844 Sector::play_music(int type)
845 {
846   currentmusic = type;
847   switch(currentmusic) {
848     case HURRYUP_MUSIC:
849       sound_manager->play_music(level_song_fast);
850       break;
851     case LEVEL_MUSIC:
852       sound_manager->play_music(level_song);
853       break;
854     case HERRING_MUSIC:
855       sound_manager->play_music(herring_song);
856       break;
857     default:
858       sound_manager->halt_music();
859       break;
860   }
861 }
862
863 int
864 Sector::get_music_type()
865 {
866   return currentmusic;
867 }