- moved tilemanager into its own class
[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   /** cleanup marked objects */
336   for(std::vector<GameObject*>::iterator i = gameobjects.begin();
337       i != gameobjects.end(); /* nothing */) {
338     if((*i)->is_valid() == false) {
339       BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
340       if(badguy) {
341         badguys.erase(std::remove(badguys.begin(), badguys.end(), badguy),
342             badguys.end());
343       }
344       Bullet* bullet = dynamic_cast<Bullet*> (*i);
345       if(bullet) {
346         bullets.erase(
347             std::remove(bullets.begin(), bullets.end(), bullet),
348             bullets.end());
349       }
350       InteractiveObject* interactive_object =
351           dynamic_cast<InteractiveObject*> (*i);
352       if(interactive_object) {
353         interactive_objects.erase(
354             std::remove(interactive_objects.begin(), interactive_objects.end(),
355                 interactive_object), interactive_objects.end());
356       }
357       Upgrade* upgrade = dynamic_cast<Upgrade*> (*i);
358       if(upgrade) {
359         upgrades.erase(
360             std::remove(upgrades.begin(), upgrades.end(), upgrade),
361             upgrades.end());
362       }
363       Trampoline* trampoline = dynamic_cast<Trampoline*> (*i);
364       if(trampoline) {
365         trampolines.erase(
366             std::remove(trampolines.begin(), trampolines.end(), trampoline),
367             trampolines.end());
368       }
369       FlyingPlatform* flying_platform= dynamic_cast<FlyingPlatform*> (*i);
370       if(flying_platform) {
371         flying_platforms.erase(
372             std::remove(flying_platforms.begin(), flying_platforms.end(), flying_platform),
373             flying_platforms.end());
374       }
375                                                                                 
376       delete *i;
377       i = gameobjects.erase(i);
378     } else {
379       ++i;
380     }
381   }
382
383   /* add newly created objects */
384   for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
385       i != gameobjects_new.end(); ++i)
386   {
387           BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
388           if(badguy)
389             badguys.push_back(badguy);
390           Bullet* bullet = dynamic_cast<Bullet*> (*i);
391           if(bullet)
392             bullets.push_back(bullet);
393           Upgrade* upgrade = dynamic_cast<Upgrade*> (*i);
394           if(upgrade)
395             upgrades.push_back(upgrade);
396           Trampoline* trampoline = dynamic_cast<Trampoline*> (*i);
397           if(trampoline)
398             trampolines.push_back(trampoline);
399           FlyingPlatform* flying_platform = dynamic_cast<FlyingPlatform*> (*i);
400           if(flying_platform)
401             flying_platforms.push_back(flying_platform);
402           InteractiveObject* interactive_object 
403               = dynamic_cast<InteractiveObject*> (*i);
404           if(interactive_object)
405             interactive_objects.push_back(interactive_object);
406
407           gameobjects.push_back(*i);
408   }
409   gameobjects_new.clear();
410
411 }
412
413 void
414 Sector::draw(DrawingContext& context)
415 {
416   context.push_transform();
417   context.set_translation(camera->get_translation());
418   
419   for(GameObjects::iterator i = gameobjects.begin();
420       i != gameobjects.end(); ++i) {
421     if( (*i)->is_valid() )
422       (*i)->draw(context);
423   }
424
425   context.pop_transform();
426 }
427
428 void
429 Sector::collision_handler()
430 {
431   // CO_BULLET & CO_BADGUY check
432   for(unsigned int i = 0; i < bullets.size(); ++i)
433     {
434       for (BadGuys::iterator j = badguys.begin(); j != badguys.end(); ++j)
435         {
436           if((*j)->dying != DYING_NOT)
437             continue;
438                                                                                 
439           if(rectcollision(bullets[i]->base, (*j)->base))
440             {
441               // We have detected a collision and now call the
442               // collision functions of the collided objects.
443               (*j)->collision(bullets[i], CO_BULLET, COLLISION_NORMAL);
444               bullets[i]->collision(CO_BADGUY);
445               break; // bullet is invalid now, so break
446             }
447         }
448     }
449                                                                                 
450   /* CO_BADGUY & CO_BADGUY check */
451   for (BadGuys::iterator i = badguys.begin(); i != badguys.end(); ++i)
452     {
453       if((*i)->dying != DYING_NOT)
454         continue;
455                                                                                 
456       BadGuys::iterator j = i;
457       ++j;
458       for (; j != badguys.end(); ++j)
459         {
460           if(j == i || (*j)->dying != DYING_NOT)
461             continue;
462                                                                                 
463           if(rectcollision((*i)->base, (*j)->base))
464             {
465               // We have detected a collision and now call the
466               // collision functions of the collided objects.
467               (*j)->collision(*i, CO_BADGUY);
468               (*i)->collision(*j, CO_BADGUY);
469             }
470         }
471     }
472   if(player->dying != DYING_NOT) return;
473                                                                                 
474   // CO_BADGUY & CO_PLAYER check
475   for (BadGuys::iterator i = badguys.begin(); i != badguys.end(); ++i)
476     {
477       if((*i)->dying != DYING_NOT)
478         continue;
479                                                                                 
480       if(rectcollision_offset((*i)->base, player->base, 0, 0))
481         {
482           // We have detected a collision and now call the collision
483           // functions of the collided objects.
484           if (player->previous_base.y < player->base.y &&
485               player->previous_base.y + player->previous_base.height
486               < (*i)->base.y + (*i)->base.height/2
487               && !player->invincible_timer.started())
488             {
489               (*i)->collision(player, CO_PLAYER, COLLISION_SQUISH);
490             }
491           else
492             {
493               player->collision(*i, CO_BADGUY);
494               (*i)->collision(player, CO_PLAYER, COLLISION_NORMAL);
495             }
496         }
497     }
498                                                                                 
499   // CO_UPGRADE & CO_PLAYER check
500   for(unsigned int i = 0; i < upgrades.size(); ++i)
501     {
502       if(rectcollision(upgrades[i]->base, player->base))
503         {
504           // We have detected a collision and now call the collision
505           // functions of the collided objects.
506           upgrades[i]->collision(player, CO_PLAYER, COLLISION_NORMAL);
507         }
508     }
509                                                                                 
510   // CO_TRAMPOLINE & (CO_PLAYER or CO_BADGUY)
511   for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
512   {
513     if (rectcollision((*i)->base, player->base))
514     {
515       if (player->previous_base.y < player->base.y &&
516           player->previous_base.y + player->previous_base.height
517           < (*i)->base.y + (*i)->base.height/2)
518       {
519         (*i)->collision(player, CO_PLAYER, COLLISION_SQUISH);
520       }
521       else if (player->previous_base.y <= player->base.y)
522       {
523         player->collision(*i, CO_TRAMPOLINE);
524         (*i)->collision(player, CO_PLAYER, COLLISION_NORMAL);
525       }
526     }
527   }
528                                                                                 
529   // CO_FLYING_PLATFORM & (CO_PLAYER or CO_BADGUY)
530   for (FlyingPlatforms::iterator i = flying_platforms.begin(); i != flying_platforms.end(); ++i)
531   {
532     if (rectcollision((*i)->base, player->base))
533     {
534       if (player->previous_base.y < player->base.y &&
535           player->previous_base.y + player->previous_base.height
536           < (*i)->base.y + (*i)->base.height/2)
537       {
538         (*i)->collision(player, CO_PLAYER, COLLISION_SQUISH);
539         player->collision(*i, CO_FLYING_PLATFORM);
540       }
541 /*      else if (player->previous_base.y <= player->base.y)
542       {
543       }*/
544     }
545   }
546 }
547
548 void
549 Sector::add_score(const Vector& pos, int s)
550 {
551   player_status.score += s;
552                                                                                 
553   add_object(new FloatingScore(pos, s));
554 }
555                                                                                 
556 void
557 Sector::add_bouncy_distro(const Vector& pos)
558 {
559   add_object(new BouncyDistro(pos));
560 }
561                                                                                 
562 void
563 Sector::add_broken_brick(const Vector& pos, Tile* tile)
564 {
565   add_broken_brick_piece(pos, Vector(-1, -4), tile);
566   add_broken_brick_piece(pos + Vector(0, 16), Vector(-1.5, -3), tile);
567                                                                                 
568   add_broken_brick_piece(pos + Vector(16, 0), Vector(1, -4), tile);
569   add_broken_brick_piece(pos + Vector(16, 16), Vector(1.5, -3), tile);
570 }
571                                                                                 
572 void
573 Sector::add_broken_brick_piece(const Vector& pos, const Vector& movement,
574     Tile* tile)
575 {
576   add_object(new BrokenBrick(tile, pos, movement));
577 }
578                                                                                 
579 void
580 Sector::add_bouncy_brick(const Vector& pos)
581 {
582   add_object(new BouncyBrick(pos));
583 }
584
585 BadGuy*
586 Sector::add_bad_guy(float x, float y, BadGuyKind kind)
587 {
588   BadGuy* badguy = new BadGuy(kind, x, y);
589   add_object(badguy);
590   return badguy;
591 }
592                                                                                 
593 void
594 Sector::add_upgrade(const Vector& pos, Direction dir, UpgradeKind kind)
595 {
596   add_object(new Upgrade(pos, dir, kind));
597 }
598                                                                                 
599 bool
600 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
601 {
602   if(player->got_power == Player::FIRE_POWER)
603     {
604     if(bullets.size() > MAX_FIRE_BULLETS-1)
605       return false;
606     }
607   else if(player->got_power == Player::ICE_POWER)
608     {
609     if(bullets.size() > MAX_ICE_BULLETS-1)
610       return false;
611     }
612                                                                                 
613   Bullet* new_bullet = 0;
614   if(player->got_power == Player::FIRE_POWER)
615     new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET);
616   else if(player->got_power == Player::ICE_POWER)
617     new_bullet = new Bullet(pos, xm, dir, ICE_BULLET);
618   else
619     throw std::runtime_error("wrong bullet type.");
620   add_object(new_bullet);
621                                                                                 
622   sound_manager->play_sound(sounds[SND_SHOOT]);
623                                                                                 
624   return true;
625 }
626
627 /* Break a brick: */
628 bool
629 Sector::trybreakbrick(const Vector& pos, bool small)
630 {
631   Tile* tile = solids->get_tile_at(pos);
632   if (!tile)
633   {
634     char errmsg[64];
635     sprintf(errmsg, "Invalid tile at %i,%i", (int)((pos.x+1)/32*32), (int)((pos.y+1)/32*32));
636     throw SuperTuxException(errmsg, __FILE__, __LINE__);
637   }
638
639   if (tile->attributes & Tile::BRICK)
640     {
641       if (tile->data > 0)
642         {
643           /* Get a distro from it: */
644           add_bouncy_distro(
645               Vector(((int)(pos.x + 1) / 32) * 32, (int)(pos.y / 32) * 32));
646                                                                                 
647           // TODO: don't handle this in a global way but per-tile...
648           if (!counting_distros)
649             {
650               counting_distros = true;
651               distro_counter = 5;
652             }
653           else
654             {
655               distro_counter--;
656             }
657                                                                                 
658           if (distro_counter <= 0)
659             {
660               counting_distros = false;
661               solids->change_at(pos, tile->next_tile);
662             }
663                                                                                 
664           sound_manager->play_sound(sounds[SND_DISTRO]);
665           player_status.score = player_status.score + SCORE_DISTRO;
666           player_status.distros++;
667           return true;
668         }
669       else if (!small)
670         {
671           /* Get rid of it: */
672           solids->change_at(pos, tile->next_tile);
673                                                                                 
674           /* Replace it with broken bits: */
675           add_broken_brick(Vector(
676                                  ((int)(pos.x + 1) / 32) * 32,
677                                  (int)(pos.y / 32) * 32), tile);
678                                                                                 
679           /* Get some score: */
680           sound_manager->play_sound(sounds[SND_BRICK]);
681           player_status.score = player_status.score + SCORE_BRICK;
682                                                                                 
683           return true;
684         }
685     }
686                                                                                 
687   return false;
688 }
689                                                                                 
690 /* Empty a box: */
691 void
692 Sector::tryemptybox(const Vector& pos, Direction col_side)
693 {
694   Tile* tile = solids->get_tile_at(pos);
695   if (!tile)
696   {
697     char errmsg[64];
698     sprintf(errmsg, "Invalid tile at %i,%i", (int)((pos.x+1)/32*32), (int)((pos.y+1)/32*32));
699     throw SuperTuxException(errmsg, __FILE__, __LINE__);
700   }
701
702
703   if (!(tile->attributes & Tile::FULLBOX))
704     return;
705                                                                                 
706   // according to the collision side, set the upgrade direction
707   if(col_side == LEFT)
708     col_side = RIGHT;
709   else
710     col_side = LEFT;
711                                                                                 
712   int posx = ((int)(pos.x+1) / 32) * 32;
713   int posy = (int)(pos.y/32) * 32 - 32;
714   switch(tile->data)
715     {
716     case 1: // Box with a distro!
717       add_bouncy_distro(Vector(posx, posy));
718       sound_manager->play_sound(sounds[SND_DISTRO]);
719       player_status.score = player_status.score + SCORE_DISTRO;
720       player_status.distros++;
721       break;
722                                                                                 
723     case 2: // Add a fire flower upgrade!
724       if (player->size == SMALL)     /* Tux is small, add mints! */
725         add_upgrade(Vector(posx, posy), col_side, UPGRADE_GROWUP);
726       else     /* Tux is big, add a fireflower: */
727         add_upgrade(Vector(posx, posy), col_side, UPGRADE_FIREFLOWER);
728       sound_manager->play_sound(sounds[SND_UPGRADE]);
729       break;
730                                                                                 
731     case 5: // Add an ice flower upgrade!
732       if (player->size == SMALL)     /* Tux is small, add mints! */
733         add_upgrade(Vector(posx, posy), col_side, UPGRADE_GROWUP);
734       else     /* Tux is big, add an iceflower: */
735         add_upgrade(Vector(posx, posy), col_side, UPGRADE_ICEFLOWER);
736       sound_manager->play_sound(sounds[SND_UPGRADE]);
737       break;
738                                                                                 
739     case 3: // Add a golden herring
740       add_upgrade(Vector(posx, posy), col_side, UPGRADE_HERRING);
741       break;
742                                                                                 
743     case 4: // Add a 1up extra
744       add_upgrade(Vector(posx, posy), col_side, UPGRADE_1UP);
745       break;
746     default:
747       break;
748     }
749                                                                                 
750   /* Empty the box: */
751   solids->change_at(pos, tile->next_tile);
752 }
753                                                                                 
754 /* Try to grab a distro: */
755 void
756 Sector::trygrabdistro(const Vector& pos, int bounciness)
757 {
758   Tile* tile = solids->get_tile_at(pos);
759   if (!tile)
760   {
761     /*char errmsg[64];
762     sprintf(errmsg, "Invalid tile at %i,%i", (int)((pos.x+1)/32*32), (int)((pos.y+1)/32*32));
763     throw SuperTuxException(errmsg, __FILE__, __LINE__); */
764     
765     //Bad tiles (i.e. tiles that are not defined in supertux.stgt but appear in the map) are changed to ID 0 (blank tile)
766     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;
767     solids->change_at(pos,0);
768     tile = solids->get_tile_at(pos);
769   }
770
771
772   if (!(tile->attributes & Tile::COIN))
773     return;
774
775   solids->change_at(pos, tile->next_tile);
776   sound_manager->play_sound(sounds[SND_DISTRO]);
777                                                                             
778   if (bounciness == BOUNCE)
779     {
780       add_bouncy_distro(Vector(((int)(pos.x + 1) / 32) * 32,
781                               (int)(pos.y / 32) * 32));
782     }
783                                                                             
784   player_status.score = player_status.score + SCORE_DISTRO;
785   player_status.distros++;
786
787 }
788                                                                                 
789 /* Try to bump a bad guy from below: */
790 void
791 Sector::trybumpbadguy(const Vector& pos)
792 {
793   // Bad guys:
794   for (BadGuys::iterator i = badguys.begin(); i != badguys.end(); ++i)
795     {
796       if ((*i)->base.x >= pos.x - 32 && (*i)->base.x <= pos.x + 32 &&
797           (*i)->base.y >= pos.y - 16 && (*i)->base.y <= pos.y + 16)
798         {
799           (*i)->collision(player, CO_PLAYER, COLLISION_BUMP);
800         }
801     }
802                                                                                 
803   // Upgrades:
804   for (unsigned int i = 0; i < upgrades.size(); i++)
805     {
806       if (upgrades[i]->base.height == 32 &&
807           upgrades[i]->base.x >= pos.x - 32 && upgrades[i]->base.x <= pos.x + 32 &&
808           upgrades[i]->base.y >= pos.y - 16 && upgrades[i]->base.y <= pos.y + 16)
809         {
810           upgrades[i]->collision(player, CO_PLAYER, COLLISION_BUMP);
811         }
812     }
813 }
814
815 void
816 Sector::load_music()
817 {
818   char* song_path;
819   char* song_subtitle;
820                                                                                 
821   level_song = sound_manager->load_music(datadir + "/music/" + song_title);
822                                                                                 
823   song_path = (char *) malloc(sizeof(char) * datadir.length() +
824                               strlen(song_title.c_str()) + 8 + 5);
825   song_subtitle = strdup(song_title.c_str());
826   strcpy(strstr(song_subtitle, "."), "\0");
827   sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(),
828           song_subtitle, strstr(song_title.c_str(), "."));
829   if(!sound_manager->exists_music(song_path)) {
830     level_song_fast = level_song;
831   } else {
832     level_song_fast = sound_manager->load_music(song_path);
833   }
834   free(song_subtitle);
835   free(song_path);
836 }
837
838 void
839 Sector::play_music(int type)
840 {
841   currentmusic = type;
842   switch(currentmusic) {
843     case HURRYUP_MUSIC:
844       sound_manager->play_music(level_song_fast);
845       break;
846     case LEVEL_MUSIC:
847       sound_manager->play_music(level_song);
848       break;
849     case HERRING_MUSIC:
850       sound_manager->play_music(herring_song);
851       break;
852     default:
853       sound_manager->halt_music();
854       break;
855   }
856 }
857
858 int
859 Sector::get_music_type()
860 {
861   return currentmusic;
862 }