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