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