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