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