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