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