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