fix secretarea and sequencetrigger loading bugs
[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 "special/collision.h"
47 #include "math/rectangle.h"
48 #include "math/aatriangle.h"
49 #include "object/coin.h"
50 #include "object/block.h"
51 #include "object/invisible_block.h"
52 #include "object/platform.h"
53 #include "object/bullet.h"
54 #include "badguy/jumpy.h"
55 #include "badguy/snowball.h"
56 #include "badguy/bouncing_snowball.h"
57 #include "badguy/flame.h"
58 #include "badguy/mriceblock.h"
59 #include "badguy/mrbomb.h"
60 #include "badguy/dispenser.h"
61 #include "badguy/spike.h"
62 #include "badguy/spiky.h"
63 #include "badguy/nolok_01.h"
64 #include "trigger/door.h"
65 #include "trigger/sequence_trigger.h"
66 #include "trigger/secretarea_trigger.h"
67
68 Sector* Sector::_current = 0;
69
70 Sector::Sector()
71   : gravity(10), player(0), solids(0), camera(0),
72     currentmusic(LEVEL_MUSIC)
73 {
74   song_title = "Mortimers_chipdisko.mod";
75   player = new Player();
76   add_object(player);
77
78   grid = new CollisionGrid(32000, 32000);
79 }
80
81 Sector::~Sector()
82 {
83   update_game_objects();
84   assert(gameobjects_new.size() == 0);
85
86   delete grid;
87
88   for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
89       ++i) {
90     delete *i;
91   }
92
93   for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
94       ++i)
95     delete *i;
96     
97   if(_current == this)
98     _current = 0;
99 }
100
101 GameObject*
102 Sector::parse_object(const std::string& name, const lisp::Lisp& reader)
103 {
104   if(name == "background") {
105     return new Background(reader);
106   } else if(name == "camera") {
107     Camera* camera = new Camera(this);
108     camera->parse(reader);
109     return camera;
110   } else if(name == "tilemap") {
111     return  new TileMap(reader);
112   } else if(name == "particles-snow") {
113     SnowParticleSystem* partsys = new SnowParticleSystem();
114     partsys->parse(reader);
115     return partsys;
116   } else if(name == "particles-clouds") {
117     CloudParticleSystem* partsys = new CloudParticleSystem();
118     partsys->parse(reader);
119     return partsys;
120   } else if(name == "door") {
121     return new Door(reader);
122   } else if(name == "secretarea") {
123     return new SecretAreaTrigger(reader);
124   } else if(name == "sequencetrigger") {
125     return new SequenceTrigger(reader);
126   } else if(name == "platform") {
127     return new Platform(reader);
128   } else if(name == "jumpy" || name == "money") {
129     return new Jumpy(reader);
130   } else if(name == "snowball") {
131     return new SnowBall(reader);
132   } else if(name == "bouncingsnowball") {
133     return new BouncingSnowball(reader);
134   } else if(name == "flame") {
135     return new Flame(reader);
136   } else if(name == "mriceblock") {
137     return new MrIceBlock(reader);
138   } else if(name == "mrbomb") {
139     return new MrBomb(reader);
140   } else if(name == "dispenser") {
141     return new Dispenser(reader);
142   } else if(name == "spike") {
143     return new Spike(reader);
144   } else if(name == "spiky") {
145     return new Spiky(reader);
146   } else if(name == "nolok_01") {
147     return new Nolok_01(reader);
148   }
149
150   std::cerr << "Unknown object type '" << name << "'.\n";
151   return 0;
152 }
153
154 void
155 Sector::parse(const lisp::Lisp& sector)
156 {
157   _current = this;
158   
159   lisp::ListIterator iter(&sector);
160   while(iter.next()) {
161     const std::string& token = iter.item();
162     if(token == "name") {
163       iter.value()->get(name);
164     } else if(token == "gravity") {
165       iter.value()->get(gravity);
166     } else if(token == "music") {
167       iter.value()->get(song_title);
168       load_music();
169     } else if(token == "spawnpoint") {
170       const lisp::Lisp* spawnpoint_lisp = iter.lisp();
171       
172       SpawnPoint* sp = new SpawnPoint;
173       spawnpoint_lisp->get("name", sp->name);
174       spawnpoint_lisp->get("x", sp->pos.x);
175       spawnpoint_lisp->get("y", sp->pos.y);
176       spawnpoints.push_back(sp);
177     } else {
178       GameObject* object = parse_object(token, *(iter.lisp()));
179       if(object) {
180         add_object(object);
181       }
182     }
183   }
184
185   update_game_objects();
186   fix_old_tiles();
187   update_game_objects();
188   if(!camera) {
189     std::cerr << "sector '" << name << "' does not contain a camera.\n";
190     camera = new Camera(this);
191     add_object(camera);
192   }
193   if(!solids)
194     throw std::runtime_error("sector does not contain a solid tile layer.");
195 }
196
197 void
198 Sector::parse_old_format(const lisp::Lisp& reader)
199 {
200   _current = this;
201   
202   name = "main";
203   reader.get("gravity", gravity);
204
205   std::string backgroundimage;
206   reader.get("background", backgroundimage);
207   float bgspeed = .5;
208   reader.get("bkgd_speed", bgspeed);
209   bgspeed /= 100;
210
211   Color bkgd_top, bkgd_bottom;
212   int r = 0, g = 0, b = 128;
213   reader.get("bkgd_red_top", r);
214   reader.get("bkgd_green_top",  g);
215   reader.get("bkgd_blue_top",  b);
216   bkgd_top.red = r;
217   bkgd_top.green = g;
218   bkgd_top.blue = b;
219   
220   reader.get("bkgd_red_bottom",  r);
221   reader.get("bkgd_green_bottom", g);
222   reader.get("bkgd_blue_bottom", b);
223   bkgd_bottom.red = r;
224   bkgd_bottom.green = g;
225   bkgd_bottom.blue = b;
226   
227   if(backgroundimage != "") {
228     Background* background = new Background;
229     background->set_image(backgroundimage, bgspeed);
230     add_object(background);
231   } else {
232     Background* background = new Background;
233     background->set_gradient(bkgd_top, bkgd_bottom);
234     add_object(background);
235   }
236
237   std::string particlesystem;
238   reader.get("particle_system", particlesystem);
239   if(particlesystem == "clouds")
240     add_object(new CloudParticleSystem());
241   else if(particlesystem == "snow")
242     add_object(new SnowParticleSystem());
243
244   Vector startpos(100, 170);
245   reader.get("start_pos_x", startpos.x);
246   reader.get("start_pos_y", startpos.y);
247
248   SpawnPoint* spawn = new SpawnPoint;
249   spawn->pos = startpos;
250   spawn->name = "main";
251   spawnpoints.push_back(spawn);
252
253   song_title = "Mortimers_chipdisko.mod";
254   reader.get("music", song_title);
255   load_music();
256
257   int width, height = 15;
258   reader.get("width", width);
259   reader.get("height", height);
260   
261   std::vector<unsigned int> tiles;
262   if(reader.get_vector("interactive-tm", tiles)
263       || reader.get_vector("tilemap", tiles)) {
264     TileMap* tilemap = new TileMap();
265     tilemap->set(width, height, tiles, LAYER_TILES, true);
266     add_object(tilemap);
267   }
268
269   if(reader.get_vector("background-tm", tiles)) {
270     TileMap* tilemap = new TileMap();
271     tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
272     add_object(tilemap);
273   }
274
275   if(reader.get_vector("foreground-tm", tiles)) {
276     TileMap* tilemap = new TileMap();
277     tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
278     add_object(tilemap);
279   }
280
281   // read reset-points (now spawn-points)
282   const lisp::Lisp* resetpoints = reader.get_lisp("reset-points");
283   if(resetpoints) {
284     lisp::ListIterator iter(resetpoints);
285     while(iter.next()) {
286       if(iter.item() == "point") {
287         Vector sp_pos;
288         if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
289           {
290           SpawnPoint* sp = new SpawnPoint;
291           sp->name = "main";
292           sp->pos = sp_pos;
293           spawnpoints.push_back(sp);
294           }
295       } else {
296         std::cerr << "Unknown token '" << iter.item() << "' in reset-points.\n";
297       }
298     }
299   }
300
301   // read objects
302   const lisp::Lisp* objects = reader.get_lisp("objects");
303   if(objects) {
304     lisp::ListIterator iter(objects);
305     while(iter.next()) {
306       GameObject* object = parse_object(iter.item(), *(iter.lisp()));
307       if(object) {
308         add_object(object);
309       } else {
310         std::cerr << "Unknown object '" << iter.item() << "' in level.\n";
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->getID() == 112) {
336         add_object(new InvisibleBlock(pos));
337         solids->change(x, y, 0);
338       } else if(tile->getID() == 295) {
339         add_object(new Spike(pos, Spike::NORTH));
340         solids->change(x, y, 0);
341       } else if(tile->getID() == 296) {
342         add_object(new Spike(pos, Spike::EAST));
343         solids->change(x, y, 0);
344       } else if(tile->getID() == 297) {
345         add_object(new Spike(pos, Spike::SOUTH));
346         solids->change(x, y, 0);
347       } else if(tile->getID() == 298) {
348         add_object(new Spike(pos, Spike::WEST));
349         solids->change(x, y, 0);
350       } else if(tile->getAttributes() & Tile::COIN) {
351         add_object(new Coin(pos));
352         solids->change(x, y, 0);
353       } else if(tile->getAttributes() & Tile::FULLBOX) {
354         add_object(new BonusBlock(pos, tile->getData()));
355         solids->change(x, y, 0);
356       } else if(tile->getAttributes() & Tile::BRICK) {
357         add_object(new Brick(pos, tile->getData()));
358         solids->change(x, y, 0);
359       } else if(tile->getAttributes() & 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(lisp::Writer& 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     GameObject* object = *i;
496     
497     if(object->is_valid()) {
498       ++i;
499       continue;
500     }
501     
502     Bullet* bullet = dynamic_cast<Bullet*> (object);
503     if(bullet) {
504       bullets.erase(
505           std::remove(bullets.begin(), bullets.end(), bullet),
506           bullets.end());
507     }
508     MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
509     if(movingobject) {
510       grid->remove_object(movingobject);
511     }
512     delete *i;
513     i = gameobjects.erase(i);
514   }
515
516   /* add newly created objects */
517   for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
518       i != gameobjects_new.end(); ++i)
519   {
520     GameObject* object = *i;
521     
522     Bullet* bullet = dynamic_cast<Bullet*> (object);
523     if(bullet)
524       bullets.push_back(bullet);
525
526     MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
527     if(movingobject)
528       grid->add_object(movingobject);
529     
530     TileMap* tilemap = dynamic_cast<TileMap*> (object);
531     if(tilemap && tilemap->is_solid()) {
532       if(solids == 0) {
533         solids = tilemap;
534       } else {
535         std::cerr << "Another solid tilemaps added. Ignoring.";
536       }
537     }
538
539     Camera* camera = dynamic_cast<Camera*> (object);
540     if(camera) {
541       if(this->camera != 0) {
542         std::cerr << "Warning: Multiple cameras added. Ignoring.";
543         continue;
544       }
545       this->camera = camera;
546     }
547
548     gameobjects.push_back(object);
549   }
550   gameobjects_new.clear();
551 }
552
553 void
554 Sector::draw(DrawingContext& context)
555 {
556   context.push_transform();
557   context.set_translation(camera->get_translation());
558   
559   for(GameObjects::iterator i = gameobjects.begin();
560       i != gameobjects.end(); ++i) {
561     GameObject* object = *i; 
562     if(!object->is_valid())
563       continue;
564     
565     object->draw(context);
566   }
567
568   context.pop_transform();
569 }
570
571 static const float DELTA = .001;
572
573 void
574 Sector::collision_tilemap(MovingObject* object, int depth)
575 {
576   if(depth >= 4) {
577 #ifdef DEBUG
578     std::cout << "Max collision depth reached.\n";
579 #endif
580     object->movement = Vector(0, 0);
581     return;
582   }
583
584   // calculate rectangle where the object will move
585   float x1, x2;
586   if(object->get_movement().x >= 0) {
587     x1 = object->get_pos().x;
588     x2 = object->get_bbox().p2.x + object->get_movement().x;
589   } else {
590     x1 = object->get_pos().x + object->get_movement().x;
591     x2 = object->get_bbox().p2.x;
592   }
593   float y1, y2;
594   if(object->get_movement().y >= 0) {
595     y1 = object->get_pos().y;
596     y2 = object->get_bbox().p2.y + object->get_movement().y;
597   } else {
598     y1 = object->get_pos().y + object->get_movement().y;
599     y2 = object->get_bbox().p2.y;
600   }
601
602   // test with all tiles in this rectangle
603   int starttilex = int(x1-1) / 32;
604   int starttiley = int(y1-1) / 32;
605   int max_x = int(x2+1);
606   int max_y = int(y2+1);
607
608   CollisionHit temphit, hit;
609   Rectangle dest = object->get_bbox();
610   dest.move(object->movement);
611   hit.time = -1; // represents an invalid value
612   for(int x = starttilex; x*32 < max_x; ++x) {
613     for(int y = starttiley; y*32 < max_y; ++y) {
614       const Tile* tile = solids->get_tile(x, y);
615       if(!tile)
616         continue;
617       if(!(tile->getAttributes() & Tile::SOLID))
618         continue;
619       if((tile->getAttributes() & Tile::UNISOLID) && object->movement.y < 0)
620         continue;
621
622       if(tile->getAttributes() & Tile::SLOPE) { // slope tile
623         AATriangle triangle;
624         Vector p1(x*32, y*32);
625         Vector p2((x+1)*32, (y+1)*32);
626         triangle = AATriangle(p1, p2, tile->getData());
627
628         if(Collision::rectangle_aatriangle(temphit, dest, object->movement,
629               triangle)) {
630           if(temphit.time > hit.time)
631             hit = temphit;
632         }
633       } else { // normal rectangular tile
634         Rectangle rect(x*32, y*32, (x+1)*32, (y+1)*32);
635         if(Collision::rectangle_rectangle(temphit, dest,
636               object->movement, rect)) {
637           if(temphit.time > hit.time)
638             hit = temphit;
639         }
640       }
641     }
642   }
643
644   // did we collide at all?
645   if(hit.time < 0)
646     return;
647  
648   // call collision function
649   HitResponse response = object->collision(*solids, hit);
650   if(response == ABORT_MOVE) {
651     object->movement = Vector(0, 0);
652     return;
653   }
654   if(response == FORCE_MOVE) {
655       return;
656   }
657   // move out of collision and try again
658   object->movement += hit.normal * (hit.depth + DELTA);
659   collision_tilemap(object, depth+1);
660 }
661
662 void
663 Sector::collision_object(MovingObject* object1, MovingObject* object2)
664 {
665   CollisionHit hit;
666   Rectangle dest1 = object1->get_bbox();
667   dest1.move(object1->get_movement());
668   Rectangle dest2 = object2->get_bbox();
669   dest2.move(object2->get_movement());
670
671   Vector movement = object1->get_movement() - object2->get_movement();
672   if(Collision::rectangle_rectangle(hit, dest1, movement, dest2)) {
673     HitResponse response1 = object1->collision(*object2, hit);
674     hit.normal *= -1;
675     HitResponse response2 = object2->collision(*object1, hit);
676
677     if(response1 != CONTINUE) {
678       if(response1 == ABORT_MOVE)
679         object1->movement = Vector(0, 0);
680       if(response2 == CONTINUE)
681         object2->movement += hit.normal * (hit.depth + DELTA);
682     } else if(response2 != CONTINUE) {
683       if(response2 == ABORT_MOVE)
684         object2->movement = Vector(0, 0);
685       if(response1 == CONTINUE)
686         object1->movement += -hit.normal * (hit.depth + DELTA);
687     } else {
688       object1->movement += -hit.normal * (hit.depth/2 + DELTA);
689       object2->movement += hit.normal * (hit.depth/2 + DELTA);
690     }
691   }
692 }
693
694 void
695 Sector::collision_handler()
696 {
697 #if 0
698   grid->check_collisions();
699 #else
700   for(std::vector<GameObject*>::iterator i = gameobjects.begin();
701       i != gameobjects.end(); ++i) {
702     GameObject* gameobject = *i;
703     if(!gameobject->is_valid())
704       continue;
705     MovingObject* movingobject = dynamic_cast<MovingObject*> (gameobject);
706     if(!movingobject)
707       continue;
708     if(movingobject->get_flags() & GameObject::FLAG_NO_COLLDET) {
709       movingobject->bbox.move(movingobject->movement);
710       movingobject->movement = Vector(0, 0);
711       continue;
712     }
713
714     // collision with tilemap
715     if(! (movingobject->movement == Vector(0, 0)))
716       collision_tilemap(movingobject, 0);
717
718     // collision with other objects
719     for(std::vector<GameObject*>::iterator i2 = i+1;
720         i2 != gameobjects.end(); ++i2) {
721       GameObject* other_object = *i2;
722       if(!other_object->is_valid() 
723           || other_object->get_flags() & GameObject::FLAG_NO_COLLDET)
724         continue;
725       MovingObject* movingobject2 = dynamic_cast<MovingObject*> (other_object);
726       if(!movingobject2)
727         continue;
728
729       collision_object(movingobject, movingobject2);
730     }
731
732     movingobject->bbox.move(movingobject->get_movement());
733     movingobject->movement = Vector(0, 0);
734   }
735 #endif
736 }
737
738 bool
739 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
740 {
741   if(player->got_power == Player::FIRE_POWER) {
742     if(bullets.size() > MAX_FIRE_BULLETS-1)
743       return false;
744   } else if(player->got_power == Player::ICE_POWER) {
745     if(bullets.size() > MAX_ICE_BULLETS-1)
746       return false;
747   }
748                                                                                 
749   Bullet* new_bullet = 0;
750   if(player->got_power == Player::FIRE_POWER)
751     new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET);
752   else if(player->got_power == Player::ICE_POWER)
753     new_bullet = new Bullet(pos, xm, dir, ICE_BULLET);
754   else
755     throw std::runtime_error("wrong bullet type.");
756   add_object(new_bullet);
757
758   SoundManager::get()->play_sound(IDToSound(SND_SHOOT));
759
760   return true;
761 }
762
763 bool
764 Sector::add_smoke_cloud(const Vector& pos)
765 {
766   add_object(new SmokeCloud(pos));
767   return true;
768 }
769
770 void
771 Sector::add_floating_text(const Vector& pos, const std::string& text)
772 {
773   add_object(new FloatingText(pos, text));
774 }
775
776 void
777 Sector::load_music()
778 {
779   char* song_path;
780   char* song_subtitle;
781                                                                                 
782   level_song = SoundManager::get()->load_music(datadir + "/music/" + song_title);
783                                                                                 
784   song_path = (char *) malloc(sizeof(char) * datadir.length() +
785                               strlen(song_title.c_str()) + 8 + 5);
786   song_subtitle = strdup(song_title.c_str());
787   strcpy(strstr(song_subtitle, "."), "\0");
788   sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(),
789           song_subtitle, strstr(song_title.c_str(), "."));
790   if(!SoundManager::get()->exists_music(song_path)) {
791     level_song_fast = level_song;
792   } else {
793     level_song_fast = SoundManager::get()->load_music(song_path);
794   }
795   free(song_subtitle);
796   free(song_path);
797 }
798
799 void
800 Sector::play_music(int type)
801 {
802   currentmusic = type;
803   switch(currentmusic) {
804     case HURRYUP_MUSIC:
805       SoundManager::get()->play_music(level_song_fast);
806       break;
807     case LEVEL_MUSIC:
808       SoundManager::get()->play_music(level_song);
809       break;
810     case HERRING_MUSIC:
811       SoundManager::get()->play_music(herring_song);
812       break;
813     default:
814       SoundManager::get()->halt_music();
815       break;
816   }
817 }
818
819 int
820 Sector::get_music_type()
821 {
822   return currentmusic;
823 }
824
825 int
826 Sector::get_total_badguys()
827 {
828   int total_badguys = 0;
829 #if 0
830   for(GameObjects::iterator i = gameobjects_new.begin(); i != gameobjects_new.end(); ++i)
831     {
832     BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
833     if(badguy)
834       total_badguys++;
835     }
836 #endif
837   return total_badguys;
838 }
839
840 bool
841 Sector::inside(const Rectangle& rect) const
842 {
843   if(rect.p1.x > solids->get_width() * 32 
844       || rect.p1.y > solids->get_height() * 32
845       || rect.p2.x < 0 || rect.p2.y < 0)
846     return false;
847
848   return true;
849 }