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