credits music plays on credits screen now
[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 <config.h>
21
22 #include <memory>
23 #include <algorithm>
24 #include <stdexcept>
25 #include <iostream>
26 #include <fstream>
27 #include <stdexcept>
28
29 #include "app/globals.h"
30 #include "sector.h"
31 #include "object/gameobjs.h"
32 #include "object/camera.h"
33 #include "object/background.h"
34 #include "object/particlesystem.h"
35 #include "object/tilemap.h"
36 #include "lisp/parser.h"
37 #include "lisp/lisp.h"
38 #include "lisp/writer.h"
39 #include "lisp/list_iterator.h"
40 #include "tile.h"
41 #include "audio/sound_manager.h"
42 #include "gameloop.h"
43 #include "resources.h"
44 #include "statistics.h"
45 #include "collision_grid.h"
46 #include "collision_grid_iterator.h"
47 #include "object_factory.h"
48 #include "special/collision.h"
49 #include "math/rectangle.h"
50 #include "math/aatriangle.h"
51 #include "object/coin.h"
52 #include "object/block.h"
53 #include "object/invisible_block.h"
54 #include "object/bullet.h"
55 #include "badguy/jumpy.h"
56 #include "badguy/spike.h"
57 #include "trigger/sequence_trigger.h"
58
59 Sector* Sector::_current = 0;
60
61 Sector::Sector()
62   : gravity(10), player(0), solids(0), camera(0),
63     currentmusic(LEVEL_MUSIC)
64 {
65   song_title = "Mortimers_chipdisko.mod";
66   player = new Player();
67   add_object(player);
68
69   grid = new CollisionGrid(32000, 32000);
70 }
71
72 Sector::~Sector()
73 {
74   update_game_objects();
75   assert(gameobjects_new.size() == 0);
76
77   delete grid;
78
79   for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
80       ++i) {
81     delete *i;
82   }
83
84   for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
85       ++i)
86     delete *i;
87     
88   if(_current == this)
89     _current = 0;
90 }
91
92 GameObject*
93 Sector::parse_object(const std::string& name, const lisp::Lisp& reader)
94 {
95   if(name == "camera") {
96     Camera* camera = new Camera(this);
97     camera->parse(reader);
98     return camera;
99   } else if(name == "particles-snow") {
100     SnowParticleSystem* partsys = new SnowParticleSystem();
101     partsys->parse(reader);
102     return partsys;
103   } else if(name == "particles-clouds") {
104     CloudParticleSystem* partsys = new CloudParticleSystem();
105     partsys->parse(reader);
106     return partsys;
107   } else if(name == "money") { // for compatibility with old maps
108     return new Jumpy(reader);
109   } 
110
111   try {
112     return create_object(name, reader);
113   } catch(std::exception& e) {
114     std::cerr << e.what() << "\n";
115   }
116   
117   return 0;
118 }
119
120 void
121 Sector::parse(const lisp::Lisp& sector)
122 {
123   _current = this;
124   
125   lisp::ListIterator iter(&sector);
126   while(iter.next()) {
127     const std::string& token = iter.item();
128     if(token == "name") {
129       iter.value()->get(name);
130     } else if(token == "gravity") {
131       iter.value()->get(gravity);
132     } else if(token == "music") {
133       iter.value()->get(song_title);
134       load_music();
135     } else if(token == "spawnpoint") {
136       const lisp::Lisp* spawnpoint_lisp = iter.lisp();
137       
138       SpawnPoint* sp = new SpawnPoint;
139       spawnpoint_lisp->get("name", sp->name);
140       spawnpoint_lisp->get("x", sp->pos.x);
141       spawnpoint_lisp->get("y", sp->pos.y);
142       spawnpoints.push_back(sp);
143     } else {
144       GameObject* object = parse_object(token, *(iter.lisp()));
145       if(object) {
146         add_object(object);
147       }
148     }
149   }
150
151   update_game_objects();
152   fix_old_tiles();
153   update_game_objects();
154   if(!camera) {
155     std::cerr << "sector '" << name << "' does not contain a camera.\n";
156     camera = new Camera(this);
157     add_object(camera);
158   }
159   if(!solids)
160     throw std::runtime_error("sector does not contain a solid tile layer.");
161 }
162
163 void
164 Sector::parse_old_format(const lisp::Lisp& reader)
165 {
166   _current = this;
167   
168   name = "main";
169   reader.get("gravity", gravity);
170
171   std::string backgroundimage;
172   reader.get("background", backgroundimage);
173   float bgspeed = .5;
174   reader.get("bkgd_speed", bgspeed);
175   bgspeed /= 100;
176
177   Color bkgd_top, bkgd_bottom;
178   int r = 0, g = 0, b = 128;
179   reader.get("bkgd_red_top", r);
180   reader.get("bkgd_green_top",  g);
181   reader.get("bkgd_blue_top",  b);
182   bkgd_top.red = r;
183   bkgd_top.green = g;
184   bkgd_top.blue = b;
185   
186   reader.get("bkgd_red_bottom",  r);
187   reader.get("bkgd_green_bottom", g);
188   reader.get("bkgd_blue_bottom", b);
189   bkgd_bottom.red = r;
190   bkgd_bottom.green = g;
191   bkgd_bottom.blue = b;
192   
193   if(backgroundimage != "") {
194     Background* background = new Background;
195     background->set_image(backgroundimage, bgspeed);
196     add_object(background);
197   } else {
198     Background* background = new Background;
199     background->set_gradient(bkgd_top, bkgd_bottom);
200     add_object(background);
201   }
202
203   std::string particlesystem;
204   reader.get("particle_system", particlesystem);
205   if(particlesystem == "clouds")
206     add_object(new CloudParticleSystem());
207   else if(particlesystem == "snow")
208     add_object(new SnowParticleSystem());
209
210   Vector startpos(100, 170);
211   reader.get("start_pos_x", startpos.x);
212   reader.get("start_pos_y", startpos.y);
213
214   SpawnPoint* spawn = new SpawnPoint;
215   spawn->pos = startpos;
216   spawn->name = "main";
217   spawnpoints.push_back(spawn);
218
219   song_title = "Mortimers_chipdisko.mod";
220   reader.get("music", song_title);
221   load_music();
222
223   int width, height = 15;
224   reader.get("width", width);
225   reader.get("height", height);
226   
227   std::vector<unsigned int> tiles;
228   if(reader.get_vector("interactive-tm", tiles)
229       || reader.get_vector("tilemap", tiles)) {
230     TileMap* tilemap = new TileMap();
231     tilemap->set(width, height, tiles, LAYER_TILES, true);
232     add_object(tilemap);
233   }
234
235   if(reader.get_vector("background-tm", tiles)) {
236     TileMap* tilemap = new TileMap();
237     tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
238     add_object(tilemap);
239   }
240
241   if(reader.get_vector("foreground-tm", tiles)) {
242     TileMap* tilemap = new TileMap();
243     tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
244     add_object(tilemap);
245   }
246
247   // read reset-points (now spawn-points)
248   const lisp::Lisp* resetpoints = reader.get_lisp("reset-points");
249   if(resetpoints) {
250     lisp::ListIterator iter(resetpoints);
251     while(iter.next()) {
252       if(iter.item() == "point") {
253         Vector sp_pos;
254         if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
255           {
256           SpawnPoint* sp = new SpawnPoint;
257           sp->name = "main";
258           sp->pos = sp_pos;
259           spawnpoints.push_back(sp);
260           }
261       } else {
262         std::cerr << "Unknown token '" << iter.item() << "' in reset-points.\n";
263       }
264     }
265   }
266
267   // read objects
268   const lisp::Lisp* objects = reader.get_lisp("objects");
269   if(objects) {
270     lisp::ListIterator iter(objects);
271     while(iter.next()) {
272       GameObject* object = parse_object(iter.item(), *(iter.lisp()));
273       if(object) {
274         add_object(object);
275       } else {
276         std::cerr << "Unknown object '" << iter.item() << "' in level.\n";
277       }
278     }
279   }
280
281   // add a camera
282   Camera* camera = new Camera(this);
283   add_object(camera);
284
285   update_game_objects();
286   fix_old_tiles();
287   update_game_objects();
288   if(solids == 0)
289     throw std::runtime_error("sector does not contain a solid tile layer.");  
290 }
291
292 void
293 Sector::fix_old_tiles()
294 {
295   // hack for now...
296   for(size_t x=0; x < solids->get_width(); ++x) {
297     for(size_t y=0; y < solids->get_height(); ++y) {
298       const Tile* tile = solids->get_tile(x, y);
299       Vector pos(x*32, y*32);
300       
301       if(tile->getID() == 112) {
302         add_object(new InvisibleBlock(pos));
303         solids->change(x, y, 0);
304       } else if(tile->getID() == 295) {
305         add_object(new Spike(pos, Spike::NORTH));
306         solids->change(x, y, 0);
307       } else if(tile->getID() == 296) {
308         add_object(new Spike(pos, Spike::EAST));
309         solids->change(x, y, 0);
310       } else if(tile->getID() == 297) {
311         add_object(new Spike(pos, Spike::SOUTH));
312         solids->change(x, y, 0);
313       } else if(tile->getID() == 298) {
314         add_object(new Spike(pos, Spike::WEST));
315         solids->change(x, y, 0);
316       } else if(tile->getAttributes() & Tile::COIN) {
317         add_object(new Coin(pos));
318         solids->change(x, y, 0);
319       } else if(tile->getAttributes() & Tile::FULLBOX) {
320         add_object(new BonusBlock(pos, tile->getData()));
321         solids->change(x, y, 0);
322       } else if(tile->getAttributes() & Tile::BRICK) {
323         add_object(new Brick(pos, tile->getData()));
324         solids->change(x, y, 0);
325       } else if(tile->getAttributes() & Tile::GOAL) {
326         add_object(new SequenceTrigger(pos, "endsequence"));
327         solids->change(x, y, 0);
328       }
329     }                                                   
330   }
331 }
332
333 void
334 Sector::write(lisp::Writer& writer)
335 {
336   writer.write_string("name", name);
337   writer.write_float("gravity", gravity);
338   writer.write_string("music", song_title);
339
340   // write spawnpoints
341   for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
342       ++i) {
343     SpawnPoint* spawn = *i;
344     writer.start_list("spawn-points");
345     writer.write_string("name", spawn->name);
346     writer.write_float("x", spawn->pos.x);
347     writer.write_float("y", spawn->pos.y);
348     writer.end_list("spawn-points");
349   }
350
351   // write objects
352   for(GameObjects::iterator i = gameobjects.begin();
353       i != gameobjects.end(); ++i) {
354     Serializable* serializable = dynamic_cast<Serializable*> (*i);
355     if(serializable)
356       serializable->write(writer);
357   }
358 }
359
360 void
361 Sector::add_object(GameObject* object)
362 {
363   // make sure the object isn't already in the list
364 #ifdef DEBUG
365   for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
366       ++i) {
367     if(*i == object) {
368       assert("object already added to sector" == 0);
369     }
370   }
371   for(GameObjects::iterator i = gameobjects_new.begin();
372       i != gameobjects_new.end(); ++i) {
373     if(*i == object) {
374       assert("object already added to sector" == 0);
375     }
376   }
377 #endif
378
379   gameobjects_new.push_back(object);
380 }
381
382 void
383 Sector::activate(const std::string& spawnpoint)
384 {
385   _current = this;
386
387   // Apply bonuses from former levels
388   switch (player_status.bonus)
389     {
390     case PlayerStatus::NO_BONUS:
391       break;
392                                                                                 
393     case PlayerStatus::FLOWER_BONUS:
394       player->got_power = Player::FIRE_POWER;  // FIXME: add ice power to here
395       // fall through
396                                                                                 
397     case PlayerStatus::GROWUP_BONUS:
398       player->grow(false);
399       break;
400     }
401
402   SpawnPoint* sp = 0;
403   for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
404       ++i) {
405     if((*i)->name == spawnpoint) {
406       sp = *i;
407       break;
408     }
409   }
410   if(!sp) {
411     std::cerr << "Spawnpoint '" << spawnpoint << "' not found.\n";
412   } else {
413     player->move(sp->pos);
414   }
415
416   camera->reset(player->get_pos());
417 }
418
419 Vector
420 Sector::get_best_spawn_point(Vector pos)
421 {
422   Vector best_reset_point = Vector(-1,-1);
423
424   for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
425       ++i) {
426     if((*i)->name != "main")
427       continue;
428     if((*i)->pos.x > best_reset_point.x && (*i)->pos.x < pos.x)
429       best_reset_point = (*i)->pos;
430   }
431
432   return best_reset_point;
433 }
434
435 Rectangle
436 Sector::get_active_region()
437 {
438   return Rectangle(
439     camera->get_translation() - Vector(1600, 1200),
440     camera->get_translation() + Vector(1600, 1200));
441 }
442
443 void
444 Sector::action(float elapsed_time)
445 {
446   player->check_bounds(camera);
447
448 #if 0
449   CollisionGridIterator iter(*grid, get_active_region());
450   while(MovingObject* object = iter.next()) {
451     if(!object->is_valid())
452       continue;
453
454     object->action(elapsed_time);
455   }
456 #else
457   /* update objects */
458   for(GameObjects::iterator i = gameobjects.begin();
459           i != gameobjects.end(); ++i) {
460     GameObject* object = *i;
461     if(!object->is_valid())
462       continue;
463     
464     object->action(elapsed_time);
465   }
466 #endif
467   
468   /* Handle all possible collisions. */
469   collision_handler();                                                                              
470   update_game_objects();
471 }
472
473 void
474 Sector::update_game_objects()
475 {
476   /** cleanup marked objects */
477   for(std::vector<GameObject*>::iterator i = gameobjects.begin();
478       i != gameobjects.end(); /* nothing */) {
479     GameObject* object = *i;
480     
481     if(object->is_valid()) {
482       ++i;
483       continue;
484     }
485     
486     Bullet* bullet = dynamic_cast<Bullet*> (object);
487     if(bullet) {
488       bullets.erase(
489           std::remove(bullets.begin(), bullets.end(), bullet),
490           bullets.end());
491     }
492     MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
493     if(movingobject) {
494       grid->remove_object(movingobject);
495     }
496     delete *i;
497     i = gameobjects.erase(i);
498   }
499
500   /* add newly created objects */
501   for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
502       i != gameobjects_new.end(); ++i)
503   {
504     GameObject* object = *i;
505     
506     Bullet* bullet = dynamic_cast<Bullet*> (object);
507     if(bullet)
508       bullets.push_back(bullet);
509
510     MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
511     if(movingobject)
512       grid->add_object(movingobject);
513     
514     TileMap* tilemap = dynamic_cast<TileMap*> (object);
515     if(tilemap && tilemap->is_solid()) {
516       if(solids == 0) {
517         solids = tilemap;
518       } else {
519         std::cerr << "Another solid tilemaps added. Ignoring.";
520       }
521     }
522
523     Camera* camera = dynamic_cast<Camera*> (object);
524     if(camera) {
525       if(this->camera != 0) {
526         std::cerr << "Warning: Multiple cameras added. Ignoring.";
527         continue;
528       }
529       this->camera = camera;
530     }
531
532     gameobjects.push_back(object);
533   }
534   gameobjects_new.clear();
535 }
536
537 void
538 Sector::draw(DrawingContext& context)
539 {
540   context.push_transform();
541   context.set_translation(camera->get_translation());
542
543 #if 0
544   CollisionGridIterator iter(*grid, get_active_region());
545   while(MovingObject* object = iter.next()) {
546     if(!object->is_valid())
547       continue;
548
549     object->draw(context);
550   }
551 #else
552   for(GameObjects::iterator i = gameobjects.begin();
553       i != gameobjects.end(); ++i) {
554     GameObject* object = *i; 
555     if(!object->is_valid())
556       continue;
557     
558     object->draw(context);
559   }
560 #endif
561
562   context.pop_transform();
563 }
564
565 static const float DELTA = .001;
566
567 void
568 Sector::collision_tilemap(MovingObject* object, int depth)
569 {
570   if(depth >= 4) {
571 #ifdef DEBUG
572     std::cout << "Max collision depth reached.\n";
573 #endif
574     object->movement = Vector(0, 0);
575     return;
576   }
577
578   // calculate rectangle where the object will move
579   float x1, x2;
580   if(object->get_movement().x >= 0) {
581     x1 = object->get_pos().x;
582     x2 = object->get_bbox().p2.x + object->get_movement().x;
583   } else {
584     x1 = object->get_pos().x + object->get_movement().x;
585     x2 = object->get_bbox().p2.x;
586   }
587   float y1, y2;
588   if(object->get_movement().y >= 0) {
589     y1 = object->get_pos().y;
590     y2 = object->get_bbox().p2.y + object->get_movement().y;
591   } else {
592     y1 = object->get_pos().y + object->get_movement().y;
593     y2 = object->get_bbox().p2.y;
594   }
595
596   // test with all tiles in this rectangle
597   int starttilex = int(x1-1) / 32;
598   int starttiley = int(y1-1) / 32;
599   int max_x = int(x2+1);
600   int max_y = int(y2+1);
601
602   CollisionHit temphit, hit;
603   Rectangle dest = object->get_bbox();
604   dest.move(object->movement);
605   hit.time = -1; // represents an invalid value
606   for(int x = starttilex; x*32 < max_x; ++x) {
607     for(int y = starttiley; y*32 < max_y; ++y) {
608       const Tile* tile = solids->get_tile(x, y);
609       if(!tile)
610         continue;
611       if(!(tile->getAttributes() & Tile::SOLID))
612         continue;
613       if((tile->getAttributes() & Tile::UNISOLID) && object->movement.y < 0)
614         continue;
615
616       if(tile->getAttributes() & Tile::SLOPE) { // slope tile
617         AATriangle triangle;
618         Vector p1(x*32, y*32);
619         Vector p2((x+1)*32, (y+1)*32);
620         triangle = AATriangle(p1, p2, tile->getData());
621
622         if(Collision::rectangle_aatriangle(temphit, dest, object->movement,
623               triangle)) {
624           if(temphit.time > hit.time)
625             hit = temphit;
626         }
627       } else { // normal rectangular tile
628         Rectangle rect(x*32, y*32, (x+1)*32, (y+1)*32);
629         if(Collision::rectangle_rectangle(temphit, dest,
630               object->movement, rect)) {
631           if(temphit.time > hit.time)
632             hit = temphit;
633         }
634       }
635     }
636   }
637
638   // did we collide at all?
639   if(hit.time < 0)
640     return;
641  
642   // call collision function
643   HitResponse response = object->collision(*solids, hit);
644   if(response == ABORT_MOVE) {
645     object->movement = Vector(0, 0);
646     return;
647   }
648   if(response == FORCE_MOVE) {
649       return;
650   }
651   // move out of collision and try again
652   object->movement += hit.normal * (hit.depth + DELTA);
653   collision_tilemap(object, depth+1);
654 }
655
656 void
657 Sector::collision_object(MovingObject* object1, MovingObject* object2)
658 {
659   CollisionHit hit;
660   Rectangle dest1 = object1->get_bbox();
661   dest1.move(object1->get_movement());
662   Rectangle dest2 = object2->get_bbox();
663   dest2.move(object2->get_movement());
664
665   Vector movement = object1->get_movement() - object2->get_movement();
666   if(Collision::rectangle_rectangle(hit, dest1, movement, dest2)) {
667     HitResponse response1 = object1->collision(*object2, hit);
668     hit.normal *= -1;
669     HitResponse response2 = object2->collision(*object1, hit);
670
671     if(response1 != CONTINUE) {
672       if(response1 == ABORT_MOVE)
673         object1->movement = Vector(0, 0);
674       if(response2 == CONTINUE)
675         object2->movement += hit.normal * (hit.depth + DELTA);
676     } else if(response2 != CONTINUE) {
677       if(response2 == ABORT_MOVE)
678         object2->movement = Vector(0, 0);
679       if(response1 == CONTINUE)
680         object1->movement += -hit.normal * (hit.depth + DELTA);
681     } else {
682       object1->movement += -hit.normal * (hit.depth/2 + DELTA);
683       object2->movement += hit.normal * (hit.depth/2 + DELTA);
684     }
685   }
686 }
687
688 void
689 Sector::collision_handler()
690 {
691 #if 0
692   grid->check_collisions();
693 #else
694   for(std::vector<GameObject*>::iterator i = gameobjects.begin();
695       i != gameobjects.end(); ++i) {
696     GameObject* gameobject = *i;
697     if(!gameobject->is_valid())
698       continue;
699     MovingObject* movingobject = dynamic_cast<MovingObject*> (gameobject);
700     if(!movingobject)
701       continue;
702     if(movingobject->get_flags() & GameObject::FLAG_NO_COLLDET) {
703       movingobject->bbox.move(movingobject->movement);
704       movingobject->movement = Vector(0, 0);
705       continue;
706     }
707
708     // collision with tilemap
709     if(! (movingobject->movement == Vector(0, 0)))
710       collision_tilemap(movingobject, 0);
711
712     // collision with other objects
713     for(std::vector<GameObject*>::iterator i2 = i+1;
714         i2 != gameobjects.end(); ++i2) {
715       GameObject* other_object = *i2;
716       if(!other_object->is_valid() 
717           || other_object->get_flags() & GameObject::FLAG_NO_COLLDET)
718         continue;
719       MovingObject* movingobject2 = dynamic_cast<MovingObject*> (other_object);
720       if(!movingobject2)
721         continue;
722
723       collision_object(movingobject, movingobject2);
724     }
725
726     movingobject->bbox.move(movingobject->get_movement());
727     movingobject->movement = Vector(0, 0);
728   }
729 #endif
730 }
731
732 bool
733 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
734 {
735   if(player->got_power == Player::FIRE_POWER) {
736     if(bullets.size() > MAX_FIRE_BULLETS-1)
737       return false;
738   } else if(player->got_power == Player::ICE_POWER) {
739     if(bullets.size() > MAX_ICE_BULLETS-1)
740       return false;
741   }
742                                                                                 
743   Bullet* new_bullet = 0;
744   if(player->got_power == Player::FIRE_POWER)
745     new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET);
746   else if(player->got_power == Player::ICE_POWER)
747     new_bullet = new Bullet(pos, xm, dir, ICE_BULLET);
748   else
749     throw std::runtime_error("wrong bullet type.");
750   add_object(new_bullet);
751
752   SoundManager::get()->play_sound(IDToSound(SND_SHOOT));
753
754   return true;
755 }
756
757 bool
758 Sector::add_smoke_cloud(const Vector& pos)
759 {
760   add_object(new SmokeCloud(pos));
761   return true;
762 }
763
764 void
765 Sector::add_floating_text(const Vector& pos, const std::string& text)
766 {
767   add_object(new FloatingText(pos, text));
768 }
769
770 void
771 Sector::load_music()
772 {
773   char* song_path;
774   char* song_subtitle;
775                                                                                 
776   level_song = SoundManager::get()->load_music(datadir + "/music/" + song_title);
777                                                                                 
778   song_path = (char *) malloc(sizeof(char) * datadir.length() +
779                               strlen(song_title.c_str()) + 8 + 5);
780   song_subtitle = strdup(song_title.c_str());
781   strcpy(strstr(song_subtitle, "."), "\0");
782   sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(),
783           song_subtitle, strstr(song_title.c_str(), "."));
784   if(!SoundManager::get()->exists_music(song_path)) {
785     level_song_fast = level_song;
786   } else {
787     level_song_fast = SoundManager::get()->load_music(song_path);
788   }
789   free(song_subtitle);
790   free(song_path);
791 }
792
793 void
794 Sector::play_music(int type)
795 {
796   currentmusic = type;
797   switch(currentmusic) {
798     case HURRYUP_MUSIC:
799       SoundManager::get()->play_music(level_song_fast);
800       break;
801     case LEVEL_MUSIC:
802       SoundManager::get()->play_music(level_song);
803       break;
804     case HERRING_MUSIC:
805       SoundManager::get()->play_music(herring_song);
806       break;
807     case CREDITS_MUSIC:
808       SoundManager::get()->play_music(credits_song);
809       break;
810     default:
811       SoundManager::get()->halt_music();
812       break;
813   }
814 }
815
816 int
817 Sector::get_music_type()
818 {
819   return currentmusic;
820 }
821
822 int
823 Sector::get_total_badguys()
824 {
825   int total_badguys = 0;
826 #if 0
827   for(GameObjects::iterator i = gameobjects_new.begin(); i != gameobjects_new.end(); ++i)
828     {
829     BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
830     if(badguy)
831       total_badguys++;
832     }
833 #endif
834   return total_badguys;
835 }
836
837 bool
838 Sector::inside(const Rectangle& rect) const
839 {
840   if(rect.p1.x > solids->get_width() * 32 
841       || rect.p1.y > solids->get_height() * 32
842       || rect.p2.x < 0 || rect.p2.y < 0)
843     return false;
844
845   return true;
846 }