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