don't play with the magic constants...
[supertux.git] / src / sector.cpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2004 Matthias Braun <matze@braunis.de
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 #include <config.h>
20
21 #include <memory>
22 #include <algorithm>
23 #include <stdexcept>
24 #include <iostream>
25 #include <fstream>
26 #include <sstream>
27 #include <stdexcept>
28
29 #include "sector.h"
30 #include "player_status.h"
31 #include "object/gameobjs.h"
32 #include "object/camera.h"
33 #include "object/background.h"
34 #include "object/particlesystem.h"
35 #include "object/particlesystem_interactive.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 "game_session.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 "collision.h"
50 #include "spawn_point.h"
51 #include "math/rect.h"
52 #include "math/aatriangle.h"
53 #include "object/coin.h"
54 #include "object/block.h"
55 #include "object/invisible_block.h"
56 #include "object/bullet.h"
57 #include "object/text_object.h"
58 #include "badguy/jumpy.h"
59 #include "badguy/spike.h"
60 #include "trigger/sequence_trigger.h"
61 #include "player_status.h"
62 #include "scripting/script_interpreter.h"
63 #include "scripting/sound.h"
64 #include "scripting/scripted_object.h"
65 #include "scripting/text.h"
66
67 //#define USE_GRID
68
69 Sector* Sector::_current = 0;
70
71 Sector::Sector()
72   : gravity(10), player(0), solids(0), camera(0),
73     currentmusic(LEVEL_MUSIC)
74 {
75   song_title = "Mortimers_chipdisko.mod";
76   player = new Player(&player_status);
77   add_object(player);
78
79 #ifdef USE_GRID
80   grid = new CollisionGrid(32000, 32000);
81 #else
82   grid = 0;
83 #endif
84 }
85
86 Sector::~Sector()
87 {
88   update_game_objects();
89   assert(gameobjects_new.size() == 0);
90
91   delete grid;
92
93   for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
94       ++i) {
95     delete *i;
96   }
97
98   for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
99       ++i)
100     delete *i;
101     
102   if(_current == this)
103     _current = 0;
104 }
105
106 GameObject*
107 Sector::parse_object(const std::string& name, const lisp::Lisp& reader)
108 {
109   if(name == "camera") {
110     Camera* camera = new Camera(this);
111     camera->parse(reader);
112     return camera;
113   } else if(name == "particles-snow") {
114     SnowParticleSystem* partsys = new SnowParticleSystem();
115     partsys->parse(reader);
116     return partsys;
117   } else if(name == "particles-rain") {
118     RainParticleSystem* partsys = new RainParticleSystem();
119     partsys->parse(reader);
120     return partsys;
121   } else if(name == "particles-comets") {
122     CometParticleSystem* partsys = new CometParticleSystem();
123     partsys->parse(reader);
124     return partsys;
125   } else if(name == "particles-ghosts") {
126     GhostParticleSystem* partsys = new GhostParticleSystem();
127     partsys->parse(reader);
128     return partsys;
129   } else if(name == "particles-clouds") {
130     CloudParticleSystem* partsys = new CloudParticleSystem();
131     partsys->parse(reader);
132     return partsys;
133   } else if(name == "money") { // for compatibility with old maps
134     return new Jumpy(reader);
135   } 
136
137   try {
138     return create_object(name, reader);
139   } catch(std::exception& e) {
140     std::cerr << e.what() << "\n";
141   }
142   
143   return 0;
144 }
145
146 void
147 Sector::parse(const lisp::Lisp& sector)
148 {
149   _current = this;
150   
151   lisp::ListIterator iter(&sector);
152   while(iter.next()) {
153     const std::string& token = iter.item();
154     if(token == "name") {
155       iter.value()->get(name);
156     } else if(token == "gravity") {
157       iter.value()->get(gravity);
158     } else if(token == "music") {
159       iter.value()->get(song_title);
160       load_music();
161     } else if(token == "spawnpoint") {
162       SpawnPoint* sp = new SpawnPoint(iter.lisp());
163       spawnpoints.push_back(sp);
164     } else if(token == "init-script") {
165       iter.value()->get(init_script);
166     } else {
167       GameObject* object = parse_object(token, *(iter.lisp()));
168       if(object) {
169         add_object(object);
170       }
171     }
172   }
173
174   update_game_objects();
175   fix_old_tiles();
176   if(!camera) {
177     std::cerr << "sector '" << name << "' does not contain a camera.\n";
178     update_game_objects();
179     add_object(new Camera(this));
180   }
181   if(!solids)
182     throw std::runtime_error("sector does not contain a solid tile layer.");
183
184   update_game_objects();
185 }
186
187 void
188 Sector::parse_old_format(const lisp::Lisp& reader)
189 {
190   _current = this;
191   
192   name = "main";
193   reader.get("gravity", gravity);
194
195   std::string backgroundimage;
196   reader.get("background", backgroundimage);
197   float bgspeed = .5;
198   reader.get("bkgd_speed", bgspeed);
199   bgspeed /= 100;
200
201   Color bkgd_top, bkgd_bottom;
202   int r = 0, g = 0, b = 128;
203   reader.get("bkgd_red_top", r);
204   reader.get("bkgd_green_top",  g);
205   reader.get("bkgd_blue_top",  b);
206   bkgd_top.red = r;
207   bkgd_top.green = g;
208   bkgd_top.blue = b;
209   
210   reader.get("bkgd_red_bottom",  r);
211   reader.get("bkgd_green_bottom", g);
212   reader.get("bkgd_blue_bottom", b);
213   bkgd_bottom.red = r;
214   bkgd_bottom.green = g;
215   bkgd_bottom.blue = b;
216   
217   if(backgroundimage != "") {
218     Background* background = new Background;
219     background->set_image(backgroundimage, bgspeed);
220     add_object(background);
221   } else {
222     Background* background = new Background;
223     background->set_gradient(bkgd_top, bkgd_bottom);
224     add_object(background);
225   }
226
227   std::string particlesystem;
228   reader.get("particle_system", particlesystem);
229   if(particlesystem == "clouds")
230     add_object(new CloudParticleSystem());
231   else if(particlesystem == "snow")
232     add_object(new SnowParticleSystem());
233   else if(particlesystem == "rain")
234     add_object(new RainParticleSystem());
235
236   Vector startpos(100, 170);
237   reader.get("start_pos_x", startpos.x);
238   reader.get("start_pos_y", startpos.y);
239
240   SpawnPoint* spawn = new SpawnPoint;
241   spawn->pos = startpos;
242   spawn->name = "main";
243   spawnpoints.push_back(spawn);
244
245   song_title = "Mortimers_chipdisko.mod";
246   reader.get("music", song_title);
247   load_music();
248
249   int width, height = 15;
250   reader.get("width", width);
251   reader.get("height", height);
252   
253   std::vector<unsigned int> tiles;
254   if(reader.get_vector("interactive-tm", tiles)
255       || reader.get_vector("tilemap", tiles)) {
256     TileMap* tilemap = new TileMap();
257     tilemap->set(width, height, tiles, LAYER_TILES, true);
258     add_object(tilemap);
259   }
260
261   if(reader.get_vector("background-tm", tiles)) {
262     TileMap* tilemap = new TileMap();
263     tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
264     add_object(tilemap);
265   }
266
267   if(reader.get_vector("foreground-tm", tiles)) {
268     TileMap* tilemap = new TileMap();
269     tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
270     add_object(tilemap);
271   }
272
273   // read reset-points (now spawn-points)
274   const lisp::Lisp* resetpoints = reader.get_lisp("reset-points");
275   if(resetpoints) {
276     lisp::ListIterator iter(resetpoints);
277     while(iter.next()) {
278       if(iter.item() == "point") {
279         Vector sp_pos;
280         if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
281           {
282           SpawnPoint* sp = new SpawnPoint;
283           sp->name = "main";
284           sp->pos = sp_pos;
285           spawnpoints.push_back(sp);
286           }
287       } else {
288         std::cerr << "Unknown token '" << iter.item() << "' in reset-points.\n";
289       }
290     }
291   }
292
293   // read objects
294   const lisp::Lisp* objects = reader.get_lisp("objects");
295   if(objects) {
296     lisp::ListIterator iter(objects);
297     while(iter.next()) {
298       GameObject* object = parse_object(iter.item(), *(iter.lisp()));
299       if(object) {
300         add_object(object);
301       } else {
302         std::cerr << "Unknown object '" << iter.item() << "' in level.\n";
303       }
304     }
305   }
306
307   // add a camera
308   Camera* camera = new Camera(this);
309   add_object(camera);
310
311   update_game_objects();
312   fix_old_tiles();
313   update_game_objects();
314   if(solids == 0)
315     throw std::runtime_error("sector does not contain a solid tile layer.");  
316 }
317
318 void
319 Sector::fix_old_tiles()
320 {
321   // hack for now...
322   for(size_t x=0; x < solids->get_width(); ++x) {
323     for(size_t y=0; y < solids->get_height(); ++y) {
324       const Tile* tile = solids->get_tile(x, y);
325       Vector pos(x*32, y*32);
326       
327       if(tile->getID() == 112) {
328         add_object(new InvisibleBlock(pos));
329         solids->change(x, y, 0);
330       } else if(tile->getID() == 295) {
331         add_object(new Spike(pos, Spike::NORTH));
332         solids->change(x, y, 0);
333       } else if(tile->getID() == 296) {
334         add_object(new Spike(pos, Spike::EAST));
335         solids->change(x, y, 0);
336       } else if(tile->getID() == 297) {
337         add_object(new Spike(pos, Spike::SOUTH));
338         solids->change(x, y, 0);
339       } else if(tile->getID() == 298) {
340         add_object(new Spike(pos, Spike::WEST));
341         solids->change(x, y, 0);
342       } else if(tile->getAttributes() & Tile::COIN) {
343         add_object(new Coin(pos));
344         solids->change(x, y, 0);
345       } else if(tile->getAttributes() & Tile::FULLBOX) {
346         add_object(new BonusBlock(pos, tile->getData()));
347         solids->change(x, y, 0);
348       } else if(tile->getAttributes() & Tile::BRICK) {
349         add_object(new Brick(pos, tile->getData()));
350         solids->change(x, y, 0);
351       } else if(tile->getAttributes() & Tile::GOAL) {
352         std::string sequence = tile->getData() == 0 ? "endsequence" : "stoptux";
353         add_object(new SequenceTrigger(pos, sequence));
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   SpawnPoint* sp = 0;
413   for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
414       ++i) {
415     if((*i)->name == spawnpoint) {
416       sp = *i;
417       break;
418     }
419   }                                                                           
420   if(!sp) {
421     std::cerr << "Spawnpoint '" << spawnpoint << "' not found.\n";
422     if(spawnpoint != "main") {
423       activate("main");
424     } else {
425       activate(Vector(0, 0));
426     }
427   } else {
428     activate(sp->pos);
429   }
430
431   // Run init script
432   if(init_script != "") {
433     ScriptInterpreter::add_script_object(this,
434         std::string("Sector(") + name + ") - init", init_script);
435   }
436 }
437
438 void
439 Sector::activate(const Vector& player_pos)
440 {
441   _current = this;
442
443   player->move(player_pos);
444   camera->reset(player->get_pos());
445 }
446
447 Rect
448 Sector::get_active_region()
449 {
450   return Rect(
451     camera->get_translation() - Vector(1600, 1200),
452     camera->get_translation() + Vector(1600, 1200));
453 }
454
455 void
456 Sector::update(float elapsed_time)
457 {
458   player->check_bounds(camera);
459
460 #if 0
461   CollisionGridIterator iter(*grid, get_active_region());
462   while(MovingObject* object = iter.next()) {
463     if(!object->is_valid())
464       continue;
465
466     object->update(elapsed_time);
467   }
468 #else
469   /* update objects */
470   for(GameObjects::iterator i = gameobjects.begin();
471           i != gameobjects.end(); ++i) {
472     GameObject* object = *i;
473     if(!object->is_valid())
474       continue;
475     
476     object->update(elapsed_time);
477   }
478 #endif
479   
480   /* Handle all possible collisions. */
481   collision_handler();                                                                              
482   update_game_objects();
483 }
484
485 void
486 Sector::update_game_objects()
487 {
488   /** cleanup marked objects */
489   for(std::vector<GameObject*>::iterator i = gameobjects.begin();
490       i != gameobjects.end(); /* nothing */) {
491     GameObject* object = *i;
492     
493     if(object->is_valid()) {
494       ++i;
495       continue;
496     }
497     
498     Bullet* bullet = dynamic_cast<Bullet*> (object);
499     if(bullet) {
500       bullets.erase(
501           std::remove(bullets.begin(), bullets.end(), bullet),
502           bullets.end());
503     }
504 #ifdef USE_GRID
505     MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
506     if(movingobject) {
507       grid->remove_object(movingobject);
508     }
509 #endif
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 #ifdef USE_GRID
525     MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
526     if(movingobject)
527       grid->add_object(movingobject);
528 #endif
529     
530     TileMap* tilemap = dynamic_cast<TileMap*> (object);
531     if(tilemap && tilemap->is_solid()) {
532       if(solids == 0) {
533         solids = tilemap;
534       } else {
535         std::cerr << "Another solid tilemaps added. Ignoring.";
536       }
537     }
538
539     Camera* camera = dynamic_cast<Camera*> (object);
540     if(camera) {
541       if(this->camera != 0) {
542         std::cerr << "Warning: Multiple cameras added. Ignoring.";
543         continue;
544       }
545       this->camera = camera;
546     }
547
548     gameobjects.push_back(object);
549   }
550   gameobjects_new.clear();
551 }
552
553 void
554 Sector::draw(DrawingContext& context)
555 {
556   context.push_transform();
557   context.set_translation(camera->get_translation());
558
559 #if 0
560   CollisionGridIterator iter(*grid, get_active_region());
561   while(MovingObject* object = iter.next()) {
562     if(!object->is_valid())
563       continue;
564
565     object->draw(context);
566   }
567 #else
568   for(GameObjects::iterator i = gameobjects.begin();
569       i != gameobjects.end(); ++i) {
570     GameObject* object = *i; 
571     if(!object->is_valid())
572       continue;
573     
574     object->draw(context);
575   }
576 #endif
577
578   context.pop_transform();
579 }
580
581 static const float DELTA = .001;
582
583 void
584 Sector::collision_tilemap(MovingObject* object, int depth)
585 {
586   if(depth >= 4) {
587 #ifdef DEBUG
588     std::cout << "Max collision depth reached.\n";
589 #endif
590     object->movement = Vector(0, 0);
591     return;
592   }
593
594   // calculate rectangle where the object will move
595   float x1, x2;
596   if(object->get_movement().x >= 0) {
597     x1 = object->get_pos().x;
598     x2 = object->get_bbox().p2.x + object->get_movement().x;
599   } else {
600     x1 = object->get_pos().x + object->get_movement().x;
601     x2 = object->get_bbox().p2.x;
602   }
603   float y1, y2;
604   if(object->get_movement().y >= 0) {
605     y1 = object->get_pos().y;
606     y2 = object->get_bbox().p2.y + object->get_movement().y;
607   } else {
608     y1 = object->get_pos().y + object->get_movement().y;
609     y2 = object->get_bbox().p2.y;
610   }
611
612   // test with all tiles in this rectangle
613   int starttilex = int(x1-1) / 32;
614   int starttiley = int(y1-1) / 32;
615   int max_x = int(x2+1);
616   int max_y = int(y2+1);
617
618   CollisionHit temphit, hit;
619   Rect dest = object->get_bbox();
620   dest.move(object->movement);
621   hit.time = -1; // represents an invalid value
622   for(int x = starttilex; x*32 < max_x; ++x) {
623     for(int y = starttiley; y*32 < max_y; ++y) {
624       const Tile* tile = solids->get_tile(x, y);
625       if(!tile)
626         continue;
627       // skip non-solid tiles
628       if(!(tile->getAttributes() & Tile::SOLID))
629         continue;
630       // only handle unisolid when the player is falling down and when he was
631       // above the tile before
632       if(tile->getAttributes() & Tile::UNISOLID) {
633         if(object->movement.y < 0 || object->get_bbox().p2.y > y*32)
634           continue;
635       }
636
637       if(tile->getAttributes() & Tile::SLOPE) { // slope tile
638         AATriangle triangle;
639         Vector p1(x*32, y*32);
640         Vector p2((x+1)*32, (y+1)*32);
641         triangle = AATriangle(p1, p2, tile->getData());
642
643         if(Collision::rectangle_aatriangle(temphit, dest, object->movement,
644               triangle)) {
645           if(temphit.time > hit.time)
646             hit = temphit;
647         }
648       } else { // normal rectangular tile
649         Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
650         if(Collision::rectangle_rectangle(temphit, dest,
651               object->movement, rect)) {
652           if(temphit.time > hit.time)
653             hit = temphit;
654         }
655       }
656     }
657   }
658
659   // did we collide at all?
660   if(hit.time < 0)
661     return;
662  
663   // call collision function
664   HitResponse response = object->collision(*solids, hit);
665   if(response == ABORT_MOVE) {
666     object->movement = Vector(0, 0);
667     return;
668   }
669   if(response == FORCE_MOVE) {
670       return;
671   }
672   // move out of collision and try again
673   object->movement += hit.normal * (hit.depth + DELTA);
674   collision_tilemap(object, depth+1);
675 }
676
677 void
678 Sector::collision_object(MovingObject* object1, MovingObject* object2)
679 {
680   CollisionHit hit;
681   Rect dest1 = object1->get_bbox();
682   dest1.move(object1->get_movement());
683   Rect dest2 = object2->get_bbox();
684   dest2.move(object2->get_movement());
685
686   Vector movement = object1->get_movement() - object2->get_movement();
687   if(Collision::rectangle_rectangle(hit, dest1, movement, dest2)) {
688     HitResponse response1 = object1->collision(*object2, hit);
689     hit.normal *= -1;
690     HitResponse response2 = object2->collision(*object1, hit);
691
692     if(response1 != CONTINUE) {
693       if(response1 == ABORT_MOVE)
694         object1->movement = Vector(0, 0);
695       if(response2 == CONTINUE)
696         object2->movement += hit.normal * (hit.depth + DELTA);
697     } else if(response2 != CONTINUE) {
698       if(response2 == ABORT_MOVE)
699         object2->movement = Vector(0, 0);
700       if(response1 == CONTINUE)
701         object1->movement += -hit.normal * (hit.depth + DELTA);
702     } else {
703       object1->movement += -hit.normal * (hit.depth/2 + DELTA);
704       object2->movement += hit.normal * (hit.depth/2 + DELTA);
705     }
706   }
707 }
708
709 void
710 Sector::collision_handler()
711 {
712 #ifdef USE_GRID
713   grid->check_collisions();
714 #else
715   for(std::vector<GameObject*>::iterator i = gameobjects.begin();
716       i != gameobjects.end(); ++i) {
717     GameObject* gameobject = *i;
718     if(!gameobject->is_valid())
719       continue;
720     MovingObject* movingobject = dynamic_cast<MovingObject*> (gameobject);
721     if(!movingobject)
722       continue;
723     if(movingobject->get_flags() & GameObject::FLAG_NO_COLLDET) {
724       movingobject->bbox.move(movingobject->movement);
725       movingobject->movement = Vector(0, 0);
726       continue;
727     }
728
729     // collision with tilemap
730     if(! (movingobject->movement == Vector(0, 0)))
731       collision_tilemap(movingobject, 0);
732
733     // collision with other objects
734     for(std::vector<GameObject*>::iterator i2 = i+1;
735         i2 != gameobjects.end(); ++i2) {
736       GameObject* other_object = *i2;
737       if(!other_object->is_valid() 
738           || other_object->get_flags() & GameObject::FLAG_NO_COLLDET)
739         continue;
740       MovingObject* movingobject2 = dynamic_cast<MovingObject*> (other_object);
741       if(!movingobject2)
742         continue;
743
744       collision_object(movingobject, movingobject2);
745     }
746
747     movingobject->bbox.move(movingobject->get_movement());
748     movingobject->movement = Vector(0, 0);
749   }
750 #endif
751 }
752
753 bool
754 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
755 {
756   // TODO remove this function and move these checks elsewhere...
757   static const size_t MAX_FIRE_BULLETS = 2;
758   static const size_t MAX_ICE_BULLETS = 1;
759
760   Bullet* new_bullet = 0;
761   if(player_status.bonus == FIRE_BONUS) {
762     if(bullets.size() > MAX_FIRE_BULLETS-1)
763       return false;
764     new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET);
765   } else if(player_status.bonus == ICE_BONUS) {
766     if(bullets.size() > MAX_ICE_BULLETS-1)
767       return false;
768     new_bullet = new Bullet(pos, xm, dir, ICE_BULLET);
769   } else {
770     return false;
771   }
772   add_object(new_bullet);
773
774   sound_manager->play_sound("shoot");
775
776   return true;
777 }
778
779 bool
780 Sector::add_smoke_cloud(const Vector& pos)
781 {
782   add_object(new SmokeCloud(pos));
783   return true;
784 }
785
786 void
787 Sector::add_floating_text(const Vector& pos, const std::string& text)
788 {
789   add_object(new FloatingText(pos, text));
790 }
791
792 void
793 Sector::load_music()
794 {
795   level_song = sound_manager->load_music("/music/" + song_title);
796 }
797
798 void
799 Sector::play_music(MusicType type)
800 {
801   currentmusic = type;
802   switch(currentmusic) {
803     case LEVEL_MUSIC:
804       sound_manager->play_music(level_song);
805       break;
806     case HERRING_MUSIC:
807       sound_manager->play_music(herring_song);
808       break;
809     default:
810       sound_manager->halt_music();
811       break;
812   }
813 }
814
815 MusicType
816 Sector::get_music_type()
817 {
818   return currentmusic;
819 }
820
821 int
822 Sector::get_total_badguys()
823 {
824   int total_badguys = 0;
825   for(GameObjects::iterator i = gameobjects.begin();
826       i != gameobjects.end(); ++i) {
827     BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
828     if (badguy && badguy->countMe)
829       total_badguys++;
830   }
831
832   return total_badguys;
833 }
834
835 bool
836 Sector::inside(const Rect& rect) const
837 {
838   if(rect.p1.x > solids->get_width() * 32 
839       || rect.p1.y > solids->get_height() * 32
840       || rect.p2.x < 0 || rect.p2.y < 0)
841     return false;
842
843   return true;
844 }