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