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