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