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