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