added simple enemy dispenser (can only dispense bouncing snowballs so far and looks...
[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 void
604 Sector::collision_tilemap(MovingObject* object, int depth)
605 {
606   if(depth >= 4) {
607 #ifdef DEBUG
608     std::cout << "Max collision depth reached.\n";
609 #endif
610     object->movement = Vector(0, 0);
611     return;
612   }
613
614   // calculate rectangle where the object will move
615   float x1, x2;
616   if(object->get_movement().x >= 0) {
617     x1 = object->get_pos().x;
618     x2 = object->get_bbox().p2.x + object->get_movement().x;
619   } else {
620     x1 = object->get_pos().x + object->get_movement().x;
621     x2 = object->get_bbox().p2.x;
622   }
623   float y1, y2;
624   if(object->get_movement().y >= 0) {
625     y1 = object->get_pos().y;
626     y2 = object->get_bbox().p2.y + object->get_movement().y;
627   } else {
628     y1 = object->get_pos().y + object->get_movement().y;
629     y2 = object->get_bbox().p2.y;
630   }
631
632   // test with all tiles in this rectangle
633   int starttilex = int(x1-1) / 32;
634   int starttiley = int(y1-1) / 32;
635   int max_x = int(x2+1);
636   int max_y = int(y2+1);
637
638   CollisionHit temphit, hit;
639   Rectangle dest = object->get_bbox();
640   dest.move(object->movement);
641   hit.time = -1; // represents an invalid value
642   for(int x = starttilex; x*32 < max_x; ++x) {
643     for(int y = starttiley; y*32 < max_y; ++y) {
644       const Tile* tile = solids->get_tile(x, y);
645       if(!tile)
646         continue;
647       if(!(tile->attributes & Tile::SOLID))
648         continue;
649       if((tile->attributes & Tile::UNISOLID) && object->movement.y < 0)
650         continue;
651
652       if(tile->attributes & Tile::SLOPE) { // slope tile
653         AATriangle triangle;
654         Vector p1(x*32, y*32);
655         Vector p2((x+1)*32, (y+1)*32);
656         switch(tile->data) {
657           case 0:
658             triangle = AATriangle(p1, p2, AATriangle::SOUTHWEST);
659             break;
660           case 1:
661             triangle = AATriangle(p1, p2, AATriangle::NORTHEAST);
662             break;
663           case 2:
664             triangle = AATriangle(p1, p2, AATriangle::SOUTHEAST);
665             break;
666           case 3:
667             triangle = AATriangle(p1, p2, AATriangle::NORTHWEST);
668             break;
669           default:
670             printf("Invalid slope angle in tile %d !\n", tile->id);
671             break;
672         }
673
674         if(Collision::rectangle_aatriangle(temphit, dest, object->movement,
675               triangle)) {
676           if(temphit.time > hit.time)
677             hit = temphit;
678         }
679       } else { // normal rectangular tile
680         Rectangle rect(x*32, y*32, (x+1)*32, (y+1)*32);
681         if(Collision::rectangle_rectangle(temphit, dest,
682               object->movement, rect)) {
683           if(temphit.time > hit.time)
684             hit = temphit;
685         }
686       }
687     }
688   }
689
690   // did we collide at all?
691   if(hit.time < 0)
692     return;
693  
694   // call collision function
695   HitResponse response = object->collision(*solids, hit);
696   if(response == ABORT_MOVE) {
697     object->movement = Vector(0, 0);
698     return;
699   }
700   if(response == FORCE_MOVE) {
701       return;
702   }
703   // move out of collision and try again
704   object->movement += hit.normal * (hit.depth + .05);
705   collision_tilemap(object, depth+1);
706 }
707
708 void
709 Sector::collision_object(MovingObject* object1, MovingObject* object2)
710 {
711   CollisionHit hit;
712   Rectangle dest1 = object1->get_bbox();
713   dest1.move(object1->get_movement());
714   Rectangle dest2 = object2->get_bbox();
715   dest2.move(object2->get_movement());
716
717   Vector movement = object1->get_movement() - object2->get_movement();
718   if(Collision::rectangle_rectangle(hit, dest1, movement, dest2)) {
719     HitResponse response1 = object1->collision(*object2, hit);
720     hit.normal *= -1;
721     HitResponse response2 = object2->collision(*object1, hit);
722
723     if(response1 != CONTINUE) {
724       if(response1 == ABORT_MOVE)
725         object1->movement = Vector(0, 0);
726       if(response2 == CONTINUE)
727         object2->movement += hit.normal * (hit.depth + .05);
728     } else if(response2 != CONTINUE) {
729       if(response2 == ABORT_MOVE)
730         object2->movement = Vector(0, 0);
731       if(response1 == CONTINUE)
732         object1->movement += -hit.normal * (hit.depth + .05);
733     } else {
734       object1->movement += -hit.normal * (hit.depth/2 + .05);
735       object2->movement += hit.normal * (hit.depth/2 + .05);
736     }
737   }
738 }
739
740 void
741 Sector::collision_handler()
742 {
743   for(std::vector<GameObject*>::iterator i = gameobjects.begin();
744       i != gameobjects.end(); ++i) {
745     GameObject* gameobject = *i;
746     if(!gameobject->is_valid() 
747         || gameobject->get_flags() & GameObject::FLAG_NO_COLLDET)
748       continue;
749     MovingObject* movingobject = dynamic_cast<MovingObject*> (gameobject);
750     if(!movingobject)
751       continue;
752
753     // collision with tilemap
754     if(! (movingobject->movement == Vector(0, 0)))
755       collision_tilemap(movingobject, 0);
756
757     // collision with other objects
758     for(std::vector<GameObject*>::iterator i2 = i+1;
759         i2 != gameobjects.end(); ++i2) {
760       GameObject* other_object = *i2;
761       if(!other_object->is_valid() 
762           || other_object->get_flags() & GameObject::FLAG_NO_COLLDET)
763         continue;
764       MovingObject* movingobject2 = dynamic_cast<MovingObject*> (other_object);
765       if(!movingobject2)
766         continue;
767
768       collision_object(movingobject, movingobject2);
769     }
770
771     movingobject->bbox.move(movingobject->get_movement());
772     movingobject->movement = Vector(0, 0);
773   }
774 }
775
776 void
777 Sector::add_score(const Vector& pos, int s)
778 {
779   global_stats.add_points(SCORE_STAT, s);
780                                                                                 
781   add_object(new FloatingText(pos, s));
782 }
783                                                                                 
784 bool
785 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
786 {
787   if(player->got_power == Player::FIRE_POWER) {
788     if(bullets.size() > MAX_FIRE_BULLETS-1)
789       return false;
790   } else if(player->got_power == Player::ICE_POWER) {
791     if(bullets.size() > MAX_ICE_BULLETS-1)
792       return false;
793   }
794                                                                                 
795   Bullet* new_bullet = 0;
796   if(player->got_power == Player::FIRE_POWER)
797     new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET);
798   else if(player->got_power == Player::ICE_POWER)
799     new_bullet = new Bullet(pos, xm, dir, ICE_BULLET);
800   else
801     throw std::runtime_error("wrong bullet type.");
802   add_object(new_bullet);
803
804   SoundManager::get()->play_sound(IDToSound(SND_SHOOT));
805
806   return true;
807 }
808
809 bool
810 Sector::add_smoke_cloud(const Vector& pos)
811 {
812   add_object(new SmokeCloud(pos));
813   return true;
814 }
815
816 void
817 Sector::add_floating_text(const Vector& pos, const std::string& text)
818 {
819   add_object(new FloatingText(pos, text));
820 }
821
822 void
823 Sector::load_music()
824 {
825   char* song_path;
826   char* song_subtitle;
827                                                                                 
828   level_song = SoundManager::get()->load_music(datadir + "/music/" + song_title);
829                                                                                 
830   song_path = (char *) malloc(sizeof(char) * datadir.length() +
831                               strlen(song_title.c_str()) + 8 + 5);
832   song_subtitle = strdup(song_title.c_str());
833   strcpy(strstr(song_subtitle, "."), "\0");
834   sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(),
835           song_subtitle, strstr(song_title.c_str(), "."));
836   if(!SoundManager::get()->exists_music(song_path)) {
837     level_song_fast = level_song;
838   } else {
839     level_song_fast = SoundManager::get()->load_music(song_path);
840   }
841   free(song_subtitle);
842   free(song_path);
843 }
844
845 void
846 Sector::play_music(int type)
847 {
848   currentmusic = type;
849   switch(currentmusic) {
850     case HURRYUP_MUSIC:
851       SoundManager::get()->play_music(level_song_fast);
852       break;
853     case LEVEL_MUSIC:
854       SoundManager::get()->play_music(level_song);
855       break;
856     case HERRING_MUSIC:
857       SoundManager::get()->play_music(herring_song);
858       break;
859     default:
860       SoundManager::get()->halt_music();
861       break;
862   }
863 }
864
865 int
866 Sector::get_music_type()
867 {
868   return currentmusic;
869 }
870
871 int
872 Sector::get_total_badguys()
873 {
874   int total_badguys = 0;
875 #if 0
876   for(GameObjects::iterator i = gameobjects_new.begin(); i != gameobjects_new.end(); ++i)
877     {
878     BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
879     if(badguy)
880       total_badguys++;
881     }
882 #endif
883   return total_badguys;
884 }
885
886 bool
887 Sector::inside(const Rectangle& rect) const
888 {
889   if(rect.p1.x > solids->get_width() * 32 
890       || rect.p1.y > solids->get_height() * 32
891       || rect.p2.x < 0 || rect.p2.y < 0)
892     return false;
893
894   return true;
895 }