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