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