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