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