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