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