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