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