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