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