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