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