310d64f30d63fb849449c5cd39b4db711cb4ee33
[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 "special/collision.h"
48 #include "math/rectangle.h"
49 #include "math/aatriangle.h"
50 #include "object/coin.h"
51 #include "object/block.h"
52 #include "object/invisible_block.h"
53 #include "object/platform.h"
54 #include "object/bullet.h"
55 #include "object/rock.h"
56 #include "badguy/jumpy.h"
57 #include "badguy/snowball.h"
58 #include "badguy/bouncing_snowball.h"
59 #include "badguy/flame.h"
60 #include "badguy/flyingsnowball.h"
61 #include "badguy/mriceblock.h"
62 #include "badguy/mrbomb.h"
63 #include "badguy/dispenser.h"
64 #include "badguy/spike.h"
65 #include "badguy/spiky.h"
66 #include "badguy/nolok_01.h"
67 #include "trigger/door.h"
68 #include "trigger/sequence_trigger.h"
69 #include "trigger/secretarea_trigger.h"
70 #include "gameobjs_bridge.h"
71
72 Sector* Sector::_current = 0;
73
74 Sector::Sector()
75   : gravity(10), player(0), solids(0), camera(0),
76     currentmusic(LEVEL_MUSIC)
77 {
78   song_title = "Mortimers_chipdisko.mod";
79   player = new Player();
80   add_object(player);
81
82   grid = new CollisionGrid(32000, 32000);
83 }
84
85 Sector::~Sector()
86 {
87   update_game_objects();
88   assert(gameobjects_new.size() == 0);
89
90   delete grid;
91
92   for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
93       ++i) {
94     delete *i;
95   }
96
97   for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
98       ++i)
99     delete *i;
100     
101   if(_current == this)
102     _current = 0;
103 }
104
105 GameObject*
106 Sector::parse_object(const std::string& name, const lisp::Lisp& reader)
107 {
108   if(name == "background") {
109     return new Background(reader);
110   } else if(name == "camera") {
111     Camera* camera = new Camera(this);
112     camera->parse(reader);
113     return camera;
114   } else if(name == "tilemap") {
115     return  new TileMap(reader);
116   } else if(name == "particles-snow") {
117     SnowParticleSystem* partsys = new SnowParticleSystem();
118     partsys->parse(reader);
119     return partsys;
120   } else if(name == "particles-clouds") {
121     CloudParticleSystem* partsys = new CloudParticleSystem();
122     partsys->parse(reader);
123     return partsys;
124   } else if(name == "secretarea") {
125     return new SecretAreaTrigger(reader);
126   } else if(name == "sequencetrigger") {
127     return new SequenceTrigger(reader);
128   } else if(is_object(name)) {
129     return create_object(object_name_to_type(name), reader);
130   } else
131     std::cerr << "Unknown object type '" << name << "'.\n";
132   return 0;
133 }
134
135 void
136 Sector::parse(const lisp::Lisp& sector)
137 {
138   _current = this;
139   
140   lisp::ListIterator iter(&sector);
141   while(iter.next()) {
142     const std::string& token = iter.item();
143     if(token == "name") {
144       iter.value()->get(name);
145     } else if(token == "gravity") {
146       iter.value()->get(gravity);
147     } else if(token == "music") {
148       iter.value()->get(song_title);
149       load_music();
150     } else if(token == "spawnpoint") {
151       const lisp::Lisp* spawnpoint_lisp = iter.lisp();
152       
153       SpawnPoint* sp = new SpawnPoint;
154       spawnpoint_lisp->get("name", sp->name);
155       spawnpoint_lisp->get("x", sp->pos.x);
156       spawnpoint_lisp->get("y", sp->pos.y);
157       spawnpoints.push_back(sp);
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   update_game_objects();
169   if(!camera) {
170     std::cerr << "sector '" << name << "' does not contain a camera.\n";
171     camera = new Camera(this);
172     add_object(camera);
173   }
174   if(!solids)
175     throw std::runtime_error("sector does not contain a solid tile layer.");
176 }
177
178 void
179 Sector::parse_old_format(const lisp::Lisp& reader)
180 {
181   _current = this;
182   
183   name = "main";
184   reader.get("gravity", gravity);
185
186   std::string backgroundimage;
187   reader.get("background", backgroundimage);
188   float bgspeed = .5;
189   reader.get("bkgd_speed", bgspeed);
190   bgspeed /= 100;
191
192   Color bkgd_top, bkgd_bottom;
193   int r = 0, g = 0, b = 128;
194   reader.get("bkgd_red_top", r);
195   reader.get("bkgd_green_top",  g);
196   reader.get("bkgd_blue_top",  b);
197   bkgd_top.red = r;
198   bkgd_top.green = g;
199   bkgd_top.blue = b;
200   
201   reader.get("bkgd_red_bottom",  r);
202   reader.get("bkgd_green_bottom", g);
203   reader.get("bkgd_blue_bottom", b);
204   bkgd_bottom.red = r;
205   bkgd_bottom.green = g;
206   bkgd_bottom.blue = b;
207   
208   if(backgroundimage != "") {
209     Background* background = new Background;
210     background->set_image(backgroundimage, bgspeed);
211     add_object(background);
212   } else {
213     Background* background = new Background;
214     background->set_gradient(bkgd_top, bkgd_bottom);
215     add_object(background);
216   }
217
218   std::string particlesystem;
219   reader.get("particle_system", particlesystem);
220   if(particlesystem == "clouds")
221     add_object(new CloudParticleSystem());
222   else if(particlesystem == "snow")
223     add_object(new SnowParticleSystem());
224
225   Vector startpos(100, 170);
226   reader.get("start_pos_x", startpos.x);
227   reader.get("start_pos_y", startpos.y);
228
229   SpawnPoint* spawn = new SpawnPoint;
230   spawn->pos = startpos;
231   spawn->name = "main";
232   spawnpoints.push_back(spawn);
233
234   song_title = "Mortimers_chipdisko.mod";
235   reader.get("music", song_title);
236   load_music();
237
238   int width, height = 15;
239   reader.get("width", width);
240   reader.get("height", height);
241   
242   std::vector<unsigned int> tiles;
243   if(reader.get_vector("interactive-tm", tiles)
244       || reader.get_vector("tilemap", tiles)) {
245     TileMap* tilemap = new TileMap();
246     tilemap->set(width, height, tiles, LAYER_TILES, true);
247     add_object(tilemap);
248   }
249
250   if(reader.get_vector("background-tm", tiles)) {
251     TileMap* tilemap = new TileMap();
252     tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
253     add_object(tilemap);
254   }
255
256   if(reader.get_vector("foreground-tm", tiles)) {
257     TileMap* tilemap = new TileMap();
258     tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
259     add_object(tilemap);
260   }
261
262   // read reset-points (now spawn-points)
263   const lisp::Lisp* resetpoints = reader.get_lisp("reset-points");
264   if(resetpoints) {
265     lisp::ListIterator iter(resetpoints);
266     while(iter.next()) {
267       if(iter.item() == "point") {
268         Vector sp_pos;
269         if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
270           {
271           SpawnPoint* sp = new SpawnPoint;
272           sp->name = "main";
273           sp->pos = sp_pos;
274           spawnpoints.push_back(sp);
275           }
276       } else {
277         std::cerr << "Unknown token '" << iter.item() << "' in reset-points.\n";
278       }
279     }
280   }
281
282   // read objects
283   const lisp::Lisp* objects = reader.get_lisp("objects");
284   if(objects) {
285     lisp::ListIterator iter(objects);
286     while(iter.next()) {
287       GameObject* object = parse_object(iter.item(), *(iter.lisp()));
288       if(object) {
289         add_object(object);
290       } else {
291         std::cerr << "Unknown object '" << iter.item() << "' in level.\n";
292       }
293     }
294   }
295
296   // add a camera
297   Camera* camera = new Camera(this);
298   add_object(camera);
299
300   update_game_objects();
301   fix_old_tiles();
302   update_game_objects();
303   if(solids == 0)
304     throw std::runtime_error("sector does not contain a solid tile layer.");  
305 }
306
307 void
308 Sector::fix_old_tiles()
309 {
310   // hack for now...
311   for(size_t x=0; x < solids->get_width(); ++x) {
312     for(size_t y=0; y < solids->get_height(); ++y) {
313       const Tile* tile = solids->get_tile(x, y);
314       Vector pos(x*32, y*32);
315       
316       if(tile->getID() == 112) {
317         add_object(new InvisibleBlock(pos));
318         solids->change(x, y, 0);
319       } else if(tile->getID() == 295) {
320         add_object(new Spike(pos, Spike::NORTH));
321         solids->change(x, y, 0);
322       } else if(tile->getID() == 296) {
323         add_object(new Spike(pos, Spike::EAST));
324         solids->change(x, y, 0);
325       } else if(tile->getID() == 297) {
326         add_object(new Spike(pos, Spike::SOUTH));
327         solids->change(x, y, 0);
328       } else if(tile->getID() == 298) {
329         add_object(new Spike(pos, Spike::WEST));
330         solids->change(x, y, 0);
331       } else if(tile->getAttributes() & Tile::COIN) {
332         add_object(new Coin(pos));
333         solids->change(x, y, 0);
334       } else if(tile->getAttributes() & Tile::FULLBOX) {
335         add_object(new BonusBlock(pos, tile->getData()));
336         solids->change(x, y, 0);
337       } else if(tile->getAttributes() & Tile::BRICK) {
338         add_object(new Brick(pos, tile->getData()));
339         solids->change(x, y, 0);
340       } else if(tile->getAttributes() & Tile::GOAL) {
341         add_object(new SequenceTrigger(pos, "endsequence"));
342         solids->change(x, y, 0);
343       }
344     }                                                   
345   }
346 }
347
348 void
349 Sector::write(lisp::Writer& writer)
350 {
351   writer.write_string("name", name);
352   writer.write_float("gravity", gravity);
353   writer.write_string("music", song_title);
354
355   // write spawnpoints
356   for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
357       ++i) {
358     SpawnPoint* spawn = *i;
359     writer.start_list("spawn-points");
360     writer.write_string("name", spawn->name);
361     writer.write_float("x", spawn->pos.x);
362     writer.write_float("y", spawn->pos.y);
363     writer.end_list("spawn-points");
364   }
365
366   // write objects
367   for(GameObjects::iterator i = gameobjects.begin();
368       i != gameobjects.end(); ++i) {
369     Serializable* serializable = dynamic_cast<Serializable*> (*i);
370     if(serializable)
371       serializable->write(writer);
372   }
373 }
374
375 void
376 Sector::add_object(GameObject* object)
377 {
378   // make sure the object isn't already in the list
379 #ifdef DEBUG
380   for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
381       ++i) {
382     if(*i == object) {
383       assert("object already added to sector" == 0);
384     }
385   }
386   for(GameObjects::iterator i = gameobjects_new.begin();
387       i != gameobjects_new.end(); ++i) {
388     if(*i == object) {
389       assert("object already added to sector" == 0);
390     }
391   }
392 #endif
393
394   gameobjects_new.push_back(object);
395 }
396
397 void
398 Sector::activate(const std::string& spawnpoint)
399 {
400   _current = this;
401
402   // Apply bonuses from former levels
403   switch (player_status.bonus)
404     {
405     case PlayerStatus::NO_BONUS:
406       break;
407                                                                                 
408     case PlayerStatus::FLOWER_BONUS:
409       player->got_power = Player::FIRE_POWER;  // FIXME: add ice power to here
410       // fall through
411                                                                                 
412     case PlayerStatus::GROWUP_BONUS:
413       player->grow(false);
414       break;
415     }
416
417   SpawnPoint* sp = 0;
418   for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
419       ++i) {
420     if((*i)->name == spawnpoint) {
421       sp = *i;
422       break;
423     }
424   }
425   if(!sp) {
426     std::cerr << "Spawnpoint '" << spawnpoint << "' not found.\n";
427   } else {
428     player->move(sp->pos);
429   }
430
431   camera->reset(player->get_pos());
432 }
433
434 Vector
435 Sector::get_best_spawn_point(Vector pos)
436 {
437   Vector best_reset_point = Vector(-1,-1);
438
439   for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
440       ++i) {
441     if((*i)->name != "main")
442       continue;
443     if((*i)->pos.x > best_reset_point.x && (*i)->pos.x < pos.x)
444       best_reset_point = (*i)->pos;
445   }
446
447   return best_reset_point;
448 }
449
450 Rectangle
451 Sector::get_active_region()
452 {
453   return Rectangle(
454     camera->get_translation() - Vector(1600, 1200),
455     camera->get_translation() + Vector(1600, 1200));
456 }
457
458 void
459 Sector::action(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->action(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->action(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   Rectangle 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       if(!(tile->getAttributes() & Tile::SOLID))
627         continue;
628       if((tile->getAttributes() & Tile::UNISOLID) && object->movement.y < 0)
629         continue;
630
631       if(tile->getAttributes() & Tile::SLOPE) { // slope tile
632         AATriangle triangle;
633         Vector p1(x*32, y*32);
634         Vector p2((x+1)*32, (y+1)*32);
635         triangle = AATriangle(p1, p2, tile->getData());
636
637         if(Collision::rectangle_aatriangle(temphit, dest, object->movement,
638               triangle)) {
639           if(temphit.time > hit.time)
640             hit = temphit;
641         }
642       } else { // normal rectangular tile
643         Rectangle rect(x*32, y*32, (x+1)*32, (y+1)*32);
644         if(Collision::rectangle_rectangle(temphit, dest,
645               object->movement, rect)) {
646           if(temphit.time > hit.time)
647             hit = temphit;
648         }
649       }
650     }
651   }
652
653   // did we collide at all?
654   if(hit.time < 0)
655     return;
656  
657   // call collision function
658   HitResponse response = object->collision(*solids, hit);
659   if(response == ABORT_MOVE) {
660     object->movement = Vector(0, 0);
661     return;
662   }
663   if(response == FORCE_MOVE) {
664       return;
665   }
666   // move out of collision and try again
667   object->movement += hit.normal * (hit.depth + DELTA);
668   collision_tilemap(object, depth+1);
669 }
670
671 void
672 Sector::collision_object(MovingObject* object1, MovingObject* object2)
673 {
674   CollisionHit hit;
675   Rectangle dest1 = object1->get_bbox();
676   dest1.move(object1->get_movement());
677   Rectangle dest2 = object2->get_bbox();
678   dest2.move(object2->get_movement());
679
680   Vector movement = object1->get_movement() - object2->get_movement();
681   if(Collision::rectangle_rectangle(hit, dest1, movement, dest2)) {
682     HitResponse response1 = object1->collision(*object2, hit);
683     hit.normal *= -1;
684     HitResponse response2 = object2->collision(*object1, hit);
685
686     if(response1 != CONTINUE) {
687       if(response1 == ABORT_MOVE)
688         object1->movement = Vector(0, 0);
689       if(response2 == CONTINUE)
690         object2->movement += hit.normal * (hit.depth + DELTA);
691     } else if(response2 != CONTINUE) {
692       if(response2 == ABORT_MOVE)
693         object2->movement = Vector(0, 0);
694       if(response1 == CONTINUE)
695         object1->movement += -hit.normal * (hit.depth + DELTA);
696     } else {
697       object1->movement += -hit.normal * (hit.depth/2 + DELTA);
698       object2->movement += hit.normal * (hit.depth/2 + DELTA);
699     }
700   }
701 }
702
703 void
704 Sector::collision_handler()
705 {
706 #if 0
707   grid->check_collisions();
708 #else
709   for(std::vector<GameObject*>::iterator i = gameobjects.begin();
710       i != gameobjects.end(); ++i) {
711     GameObject* gameobject = *i;
712     if(!gameobject->is_valid())
713       continue;
714     MovingObject* movingobject = dynamic_cast<MovingObject*> (gameobject);
715     if(!movingobject)
716       continue;
717     if(movingobject->get_flags() & GameObject::FLAG_NO_COLLDET) {
718       movingobject->bbox.move(movingobject->movement);
719       movingobject->movement = Vector(0, 0);
720       continue;
721     }
722
723     // collision with tilemap
724     if(! (movingobject->movement == Vector(0, 0)))
725       collision_tilemap(movingobject, 0);
726
727     // collision with other objects
728     for(std::vector<GameObject*>::iterator i2 = i+1;
729         i2 != gameobjects.end(); ++i2) {
730       GameObject* other_object = *i2;
731       if(!other_object->is_valid() 
732           || other_object->get_flags() & GameObject::FLAG_NO_COLLDET)
733         continue;
734       MovingObject* movingobject2 = dynamic_cast<MovingObject*> (other_object);
735       if(!movingobject2)
736         continue;
737
738       collision_object(movingobject, movingobject2);
739     }
740
741     movingobject->bbox.move(movingobject->get_movement());
742     movingobject->movement = Vector(0, 0);
743   }
744 #endif
745 }
746
747 bool
748 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
749 {
750   if(player->got_power == Player::FIRE_POWER) {
751     if(bullets.size() > MAX_FIRE_BULLETS-1)
752       return false;
753   } else if(player->got_power == Player::ICE_POWER) {
754     if(bullets.size() > MAX_ICE_BULLETS-1)
755       return false;
756   }
757                                                                                 
758   Bullet* new_bullet = 0;
759   if(player->got_power == Player::FIRE_POWER)
760     new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET);
761   else if(player->got_power == Player::ICE_POWER)
762     new_bullet = new Bullet(pos, xm, dir, ICE_BULLET);
763   else
764     throw std::runtime_error("wrong bullet type.");
765   add_object(new_bullet);
766
767   SoundManager::get()->play_sound(IDToSound(SND_SHOOT));
768
769   return true;
770 }
771
772 bool
773 Sector::add_smoke_cloud(const Vector& pos)
774 {
775   add_object(new SmokeCloud(pos));
776   return true;
777 }
778
779 void
780 Sector::add_floating_text(const Vector& pos, const std::string& text)
781 {
782   add_object(new FloatingText(pos, text));
783 }
784
785 void
786 Sector::load_music()
787 {
788   char* song_path;
789   char* song_subtitle;
790                                                                                 
791   level_song = SoundManager::get()->load_music(datadir + "/music/" + song_title);
792                                                                                 
793   song_path = (char *) malloc(sizeof(char) * datadir.length() +
794                               strlen(song_title.c_str()) + 8 + 5);
795   song_subtitle = strdup(song_title.c_str());
796   strcpy(strstr(song_subtitle, "."), "\0");
797   sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(),
798           song_subtitle, strstr(song_title.c_str(), "."));
799   if(!SoundManager::get()->exists_music(song_path)) {
800     level_song_fast = level_song;
801   } else {
802     level_song_fast = SoundManager::get()->load_music(song_path);
803   }
804   free(song_subtitle);
805   free(song_path);
806 }
807
808 void
809 Sector::play_music(int type)
810 {
811   currentmusic = type;
812   switch(currentmusic) {
813     case HURRYUP_MUSIC:
814       SoundManager::get()->play_music(level_song_fast);
815       break;
816     case LEVEL_MUSIC:
817       SoundManager::get()->play_music(level_song);
818       break;
819     case HERRING_MUSIC:
820       SoundManager::get()->play_music(herring_song);
821       break;
822     default:
823       SoundManager::get()->halt_music();
824       break;
825   }
826 }
827
828 int
829 Sector::get_music_type()
830 {
831   return currentmusic;
832 }
833
834 int
835 Sector::get_total_badguys()
836 {
837   int total_badguys = 0;
838 #if 0
839   for(GameObjects::iterator i = gameobjects_new.begin(); i != gameobjects_new.end(); ++i)
840     {
841     BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
842     if(badguy)
843       total_badguys++;
844     }
845 #endif
846   return total_badguys;
847 }
848
849 bool
850 Sector::inside(const Rectangle& rect) const
851 {
852   if(rect.p1.x > solids->get_width() * 32 
853       || rect.p1.y > solids->get_height() * 32
854       || rect.p2.x < 0 || rect.p2.y < 0)
855     return false;
856
857   return true;
858 }