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