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