Fixed trailing whitespaces in all(?) source files of supertux, also fixed some svn...
[supertux.git] / src / sector.cpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2006 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 #include <config.h>
20
21 #include <memory>
22 #include <algorithm>
23 #include <stdexcept>
24 #include <iostream>
25 #include <fstream>
26 #include <sstream>
27 #include <stdexcept>
28 #include <float.h>
29 #include <math.h>
30 #include <limits>
31 #include <physfs.h>
32
33 #include "sector.hpp"
34 #include "object/player.hpp"
35 #include "object/gameobjs.hpp"
36 #include "object/camera.hpp"
37 #include "object/background.hpp"
38 #include "object/gradient.hpp"
39 #include "object/particlesystem.hpp"
40 #include "object/particlesystem_interactive.hpp"
41 #include "object/tilemap.hpp"
42 #include "lisp/parser.hpp"
43 #include "lisp/lisp.hpp"
44 #include "lisp/writer.hpp"
45 #include "lisp/list_iterator.hpp"
46 #include "tile.hpp"
47 #include "audio/sound_manager.hpp"
48 #include "game_session.hpp"
49 #include "resources.hpp"
50 #include "statistics.hpp"
51 #include "object_factory.hpp"
52 #include "collision.hpp"
53 #include "spawn_point.hpp"
54 #include "math/rect.hpp"
55 #include "math/aatriangle.hpp"
56 #include "object/coin.hpp"
57 #include "object/block.hpp"
58 #include "object/invisible_block.hpp"
59 #include "object/light.hpp"
60 #include "object/pulsing_light.hpp"
61 #include "object/bullet.hpp"
62 #include "object/text_object.hpp"
63 #include "object/portable.hpp"
64 #include "badguy/jumpy.hpp"
65 #include "trigger/sequence_trigger.hpp"
66 #include "player_status.hpp"
67 #include "scripting/squirrel_util.hpp"
68 #include "script_interface.hpp"
69 #include "log.hpp"
70 #include "main.hpp"
71
72 Sector* Sector::_current = 0;
73
74 bool Sector::show_collrects = false;
75 bool Sector::draw_solids_only = false;
76
77 Sector::Sector(Level* parent)
78   : level(parent), currentmusic(LEVEL_MUSIC),
79   ambient_light( 1.0f, 1.0f, 1.0f, 1.0f ), gravity(10.0), player(0), camera(0)
80 {
81   add_object(new Player(player_status, "Tux"));
82   add_object(new DisplayEffect("Effect"));
83   add_object(new TextObject("Text"));
84
85   // create a new squirrel table for the sector
86   using namespace Scripting;
87
88   sq_collectgarbage(global_vm);
89
90   sq_newtable(global_vm);
91   sq_pushroottable(global_vm);
92   if(SQ_FAILED(sq_setdelegate(global_vm, -2)))
93     throw Scripting::SquirrelError(global_vm, "Couldn't set sector_table delegate");
94
95   sq_resetobject(&sector_table);
96   if(SQ_FAILED(sq_getstackobj(global_vm, -1, &sector_table)))
97     throw Scripting::SquirrelError(global_vm, "Couldn't get sector table");
98   sq_addref(global_vm, &sector_table);
99   sq_pop(global_vm, 1);
100 }
101
102 Sector::~Sector()
103 {
104   using namespace Scripting;
105
106   deactivate();
107
108   for(ScriptList::iterator i = scripts.begin();
109       i != scripts.end(); ++i) {
110     HSQOBJECT& object = *i;
111     sq_release(global_vm, &object);
112   }
113   sq_release(global_vm, &sector_table);
114   sq_collectgarbage(global_vm);
115
116   update_game_objects();
117   assert(gameobjects_new.size() == 0);
118
119   for(GameObjects::iterator i = gameobjects.begin();
120       i != gameobjects.end(); ++i) {
121     GameObject* object = *i;
122     before_object_remove(object);
123     object->unref();
124   }
125
126   for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
127       ++i)
128     delete *i;
129 }
130
131 Level*
132 Sector::get_level()
133 {
134   return level;
135 }
136
137 GameObject*
138 Sector::parse_object(const std::string& name, const lisp::Lisp& reader)
139 {
140   if(name == "camera") {
141     Camera* camera = new Camera(this, "Camera");
142     camera->parse(reader);
143     return camera;
144   } else if(name == "particles-snow") {
145     SnowParticleSystem* partsys = new SnowParticleSystem();
146     partsys->parse(reader);
147     return partsys;
148   } else if(name == "particles-rain") {
149     RainParticleSystem* partsys = new RainParticleSystem();
150     partsys->parse(reader);
151     return partsys;
152   } else if(name == "particles-comets") {
153     CometParticleSystem* partsys = new CometParticleSystem();
154     partsys->parse(reader);
155     return partsys;
156   } else if(name == "particles-ghosts") {
157     GhostParticleSystem* partsys = new GhostParticleSystem();
158     partsys->parse(reader);
159     return partsys;
160   } else if(name == "particles-clouds") {
161     CloudParticleSystem* partsys = new CloudParticleSystem();
162     partsys->parse(reader);
163     return partsys;
164   } else if(name == "money") { // for compatibility with old maps
165     return new Jumpy(reader);
166   }
167
168   try {
169     return create_object(name, reader);
170   } catch(std::exception& e) {
171     log_warning << e.what() << "" << std::endl;
172   }
173
174   return 0;
175 }
176
177 void
178 Sector::parse(const lisp::Lisp& sector)
179 {
180   lisp::ListIterator iter(&sector);
181   while(iter.next()) {
182     const std::string& token = iter.item();
183     if(token == "name") {
184       iter.value()->get(name);
185     } else if(token == "gravity") {
186       iter.value()->get(gravity);
187     } else if(token == "music") {
188       iter.value()->get(music);
189     } else if(token == "spawnpoint") {
190       SpawnPoint* sp = new SpawnPoint(iter.lisp());
191       spawnpoints.push_back(sp);
192     } else if(token == "init-script") {
193       iter.value()->get(init_script);
194     } else if(token == "ambient-light") {
195       std::vector<float> vColor;
196       sector.get_vector( "ambient-light", vColor );
197       if(vColor.size() < 3) {
198         log_warning << "(ambient-light) requires a color as argument" << std::endl;
199       } else {
200         ambient_light = Color( vColor );
201       }
202     } else {
203       GameObject* object = parse_object(token, *(iter.lisp()));
204       if(object) {
205         add_object(object);
206       }
207     }
208   }
209
210   update_game_objects();
211
212   if(solid_tilemaps.size() < 1) log_warning << "sector '" << name << "' does not contain a solid tile layer." << std::endl;
213
214   fix_old_tiles();
215   if(!camera) {
216     log_warning << "sector '" << name << "' does not contain a camera." << std::endl;
217     update_game_objects();
218     add_object(new Camera(this, "Camera"));
219   }
220
221   update_game_objects();
222 }
223
224 void
225 Sector::parse_old_format(const lisp::Lisp& reader)
226 {
227   name = "main";
228   reader.get("gravity", gravity);
229
230   std::string backgroundimage;
231   if (reader.get("background", backgroundimage) && (backgroundimage != "")) {
232     if (backgroundimage == "arctis.png") backgroundimage = "arctis.jpg";
233     if (backgroundimage == "arctis2.jpg") backgroundimage = "arctis.jpg";
234     if (backgroundimage == "ocean.png") backgroundimage = "ocean.jpg";
235     backgroundimage = "images/background/" + backgroundimage;
236     if (!PHYSFS_exists(backgroundimage.c_str())) {
237       log_warning << "Background image \"" << backgroundimage << "\" not found. Ignoring." << std::endl;
238       backgroundimage = "";
239     }
240   }
241
242   float bgspeed = .5;
243   reader.get("bkgd_speed", bgspeed);
244   bgspeed /= 100;
245
246   Color bkgd_top, bkgd_bottom;
247   int r = 0, g = 0, b = 128;
248   reader.get("bkgd_red_top", r);
249   reader.get("bkgd_green_top",  g);
250   reader.get("bkgd_blue_top",  b);
251   bkgd_top.red = static_cast<float> (r) / 255.0f;
252   bkgd_top.green = static_cast<float> (g) / 255.0f;
253   bkgd_top.blue = static_cast<float> (b) / 255.0f;
254
255   reader.get("bkgd_red_bottom",  r);
256   reader.get("bkgd_green_bottom", g);
257   reader.get("bkgd_blue_bottom", b);
258   bkgd_bottom.red = static_cast<float> (r) / 255.0f;
259   bkgd_bottom.green = static_cast<float> (g) / 255.0f;
260   bkgd_bottom.blue = static_cast<float> (b) / 255.0f;
261
262   if(backgroundimage != "") {
263     Background* background = new Background();
264     background->set_image(backgroundimage, bgspeed);
265     add_object(background);
266   } else {
267     Gradient* gradient = new Gradient();
268     gradient->set_gradient(bkgd_top, bkgd_bottom);
269     add_object(gradient);
270   }
271
272   std::string particlesystem;
273   reader.get("particle_system", particlesystem);
274   if(particlesystem == "clouds")
275     add_object(new CloudParticleSystem());
276   else if(particlesystem == "snow")
277     add_object(new SnowParticleSystem());
278   else if(particlesystem == "rain")
279     add_object(new RainParticleSystem());
280
281   Vector startpos(100, 170);
282   reader.get("start_pos_x", startpos.x);
283   reader.get("start_pos_y", startpos.y);
284
285   SpawnPoint* spawn = new SpawnPoint;
286   spawn->pos = startpos;
287   spawn->name = "main";
288   spawnpoints.push_back(spawn);
289
290   music = "chipdisko.ogg";
291   // skip reading music filename. It's all .ogg now, anyway
292   /*
293   reader.get("music", music);
294   */
295   music = "music/" + music;
296
297   int width = 30, height = 15;
298   reader.get("width", width);
299   reader.get("height", height);
300
301   std::vector<unsigned int> tiles;
302   if(reader.get_vector("interactive-tm", tiles)
303       || reader.get_vector("tilemap", tiles)) {
304     TileMap* tilemap = new TileMap();
305     tilemap->set(width, height, tiles, LAYER_TILES, true);
306
307     // replace tile id 112 (old invisible tile) with 1311 (new invisible tile)
308     for(size_t x=0; x < tilemap->get_width(); ++x) {
309       for(size_t y=0; y < tilemap->get_height(); ++y) {
310         const Tile* tile = tilemap->get_tile(x, y);
311         if(tile->getID() == 112) tilemap->change(x, y, 1311);
312       }
313     }
314
315     if (height < 19) tilemap->resize(width, 19);
316     add_object(tilemap);
317   }
318
319   if(reader.get_vector("background-tm", tiles)) {
320     TileMap* tilemap = new TileMap();
321     tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
322     if (height < 19) tilemap->resize(width, 19);
323     add_object(tilemap);
324   }
325
326   if(reader.get_vector("foreground-tm", tiles)) {
327     TileMap* tilemap = new TileMap();
328     tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
329
330     // fill additional space in foreground with tiles of ID 2035 (lightmap/black)
331     if (height < 19) tilemap->resize(width, 19, 2035);
332
333     add_object(tilemap);
334   }
335
336   // read reset-points (now spawn-points)
337   const lisp::Lisp* resetpoints = reader.get_lisp("reset-points");
338   if(resetpoints) {
339     lisp::ListIterator iter(resetpoints);
340     while(iter.next()) {
341       if(iter.item() == "point") {
342         Vector sp_pos;
343         if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
344           {
345           SpawnPoint* sp = new SpawnPoint;
346           sp->name = "main";
347           sp->pos = sp_pos;
348           spawnpoints.push_back(sp);
349           }
350       } else {
351         log_warning << "Unknown token '" << iter.item() << "' in reset-points." << std::endl;
352       }
353     }
354   }
355
356   // read objects
357   const lisp::Lisp* objects = reader.get_lisp("objects");
358   if(objects) {
359     lisp::ListIterator iter(objects);
360     while(iter.next()) {
361       GameObject* object = parse_object(iter.item(), *(iter.lisp()));
362       if(object) {
363         add_object(object);
364       } else {
365         log_warning << "Unknown object '" << iter.item() << "' in level." << std::endl;
366       }
367     }
368   }
369
370   // add a camera
371   Camera* camera = new Camera(this, "Camera");
372   add_object(camera);
373
374   update_game_objects();
375
376   if(solid_tilemaps.size() < 1) log_warning << "sector '" << name << "' does not contain a solid tile layer." << std::endl;
377
378   fix_old_tiles();
379   update_game_objects();
380 }
381
382 void
383 Sector::fix_old_tiles()
384 {
385   for(std::list<TileMap*>::iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
386     TileMap* solids = *i;
387     for(size_t x=0; x < solids->get_width(); ++x) {
388       for(size_t y=0; y < solids->get_height(); ++y) {
389         const Tile* tile = solids->get_tile(x, y);
390         Vector pos(solids->get_x_offset() + x*32, solids->get_y_offset() + y*32);
391
392         if(tile->getID() == 112) {
393           add_object(new InvisibleBlock(pos));
394           solids->change(x, y, 0);
395         } else if(tile->getAttributes() & Tile::COIN) {
396           add_object(new Coin(pos));
397           solids->change(x, y, 0);
398         } else if(tile->getAttributes() & Tile::FULLBOX) {
399           add_object(new BonusBlock(pos, tile->getData()));
400           solids->change(x, y, 0);
401         } else if(tile->getAttributes() & Tile::BRICK) {
402           add_object(new Brick(pos, tile->getData()));
403           solids->change(x, y, 0);
404         } else if(tile->getAttributes() & Tile::GOAL) {
405           std::string sequence = tile->getData() == 0 ? "endsequence" : "stoptux";
406           add_object(new SequenceTrigger(pos, sequence));
407           solids->change(x, y, 0);
408         }
409       }
410     }
411   }
412
413   // add lights for special tiles
414   for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); i++) {
415     TileMap* tm = dynamic_cast<TileMap*>(*i);
416     if (!tm) continue;
417     for(size_t x=0; x < tm->get_width(); ++x) {
418       for(size_t y=0; y < tm->get_height(); ++y) {
419         const Tile* tile = tm->get_tile(x, y);
420         Vector pos(tm->get_x_offset() + x*32, tm->get_y_offset() + y*32);
421         Vector center(pos.x + 16, pos.y + 16);
422
423         // torch
424         if (tile->getID() == 1517) {
425           float pseudo_rnd = (float)((int)pos.x % 10) / 10;
426           add_object(new PulsingLight(center, 1.0f + pseudo_rnd, 0.9f, 1.0f, Color(1.0f, 1.0f, 0.6f, 1.0f)));
427         }
428         // lava or lavaflow
429         if ((tile->getID() == 173) || (tile->getID() == 1700) || (tile->getID() == 1705) || (tile->getID() == 1706)) {
430           // space lights a bit
431           if (((tm->get_tile(x-1, y)->getID() != tm->get_tile(x,y)->getID())
432               && (tm->get_tile(x, y-1)->getID() != tm->get_tile(x,y)->getID()))
433               || ((x % 3 == 0) && (y % 3 == 0))) {
434             float pseudo_rnd = (float)((int)pos.x % 10) / 10;
435             add_object(new PulsingLight(center, 1.0f + pseudo_rnd, 0.8f, 1.0f, Color(1.0f, 0.3f, 0.0f, 1.0f)));
436           }
437         }
438
439       }
440     }
441   }
442
443
444 }
445
446 void
447 Sector::write(lisp::Writer& writer)
448 {
449   writer.write_string("name", name);
450   writer.write_float("gravity", gravity);
451   writer.write_string("music", music);
452
453   // write spawnpoints
454   for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
455       ++i) {
456     SpawnPoint* spawn = *i;
457     writer.start_list("spawn-points");
458     writer.write_string("name", spawn->name);
459     writer.write_float("x", spawn->pos.x);
460     writer.write_float("y", spawn->pos.y);
461     writer.end_list("spawn-points");
462   }
463
464   // write objects
465   for(GameObjects::iterator i = gameobjects.begin();
466       i != gameobjects.end(); ++i) {
467     Serializable* serializable = dynamic_cast<Serializable*> (*i);
468     if(serializable)
469       serializable->write(writer);
470   }
471 }
472
473 HSQUIRRELVM
474 Sector::run_script(std::istream& in, const std::string& sourcename)
475 {
476   using namespace Scripting;
477
478   // garbage collect thread list
479   for(ScriptList::iterator i = scripts.begin();
480       i != scripts.end(); ) {
481     HSQOBJECT& object = *i;
482     HSQUIRRELVM vm = object_to_vm(object);
483
484     if(sq_getvmstate(vm) != SQ_VMSTATE_SUSPENDED) {
485       sq_release(global_vm, &object);
486       i = scripts.erase(i);
487       continue;
488     }
489
490     ++i;
491   }
492
493   HSQOBJECT object = create_thread(global_vm);
494   scripts.push_back(object);
495
496   HSQUIRRELVM vm = object_to_vm(object);
497
498   // set sector_table as roottable for the thread
499   sq_pushobject(vm, sector_table);
500   sq_setroottable(vm);
501
502   compile_and_run(vm, in, sourcename);
503
504   return vm;
505 }
506
507 void
508 Sector::add_object(GameObject* object)
509 {
510   // make sure the object isn't already in the list
511 #ifdef DEBUG
512   for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
513       ++i) {
514     if(*i == object) {
515       assert("object already added to sector" == 0);
516     }
517   }
518   for(GameObjects::iterator i = gameobjects_new.begin();
519       i != gameobjects_new.end(); ++i) {
520     if(*i == object) {
521       assert("object already added to sector" == 0);
522     }
523   }
524 #endif
525
526   object->ref();
527   gameobjects_new.push_back(object);
528 }
529
530 void
531 Sector::activate(const std::string& spawnpoint)
532 {
533   SpawnPoint* sp = 0;
534   for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
535       ++i) {
536     if((*i)->name == spawnpoint) {
537       sp = *i;
538       break;
539     }
540   }
541   if(!sp) {
542     log_warning << "Spawnpoint '" << spawnpoint << "' not found." << std::endl;
543     if(spawnpoint != "main") {
544       activate("main");
545     } else {
546       activate(Vector(0, 0));
547     }
548   } else {
549     activate(sp->pos);
550   }
551 }
552
553 void
554 Sector::activate(const Vector& player_pos)
555 {
556   if(_current != this) {
557     if(_current != NULL)
558       _current->deactivate();
559     _current = this;
560
561     // register sectortable as sector in scripting
562     HSQUIRRELVM vm = Scripting::global_vm;
563     sq_pushroottable(vm);
564     sq_pushstring(vm, "sector", -1);
565     sq_pushobject(vm, sector_table);
566     if(SQ_FAILED(sq_createslot(vm, -3)))
567       throw Scripting::SquirrelError(vm, "Couldn't set sector in roottable");
568     sq_pop(vm, 1);
569
570     for(GameObjects::iterator i = gameobjects.begin();
571         i != gameobjects.end(); ++i) {
572       GameObject* object = *i;
573
574       try_expose(object);
575     }
576   }
577   try_expose_me();
578
579   // spawn smalltux below spawnpoint
580   if (!player->is_big()) {
581     player->move(player_pos + Vector(0,32));
582   } else {
583     player->move(player_pos);
584   }
585
586   // spawning tux in the ground would kill him
587   if(!is_free_of_tiles(player->get_bbox())) {
588     log_warning << "Tried spawning Tux in solid matter. Compensating." << std::endl;
589     Vector npos = player->get_bbox().p1;
590     npos.y-=32;
591     player->move(npos);
592   }
593
594   camera->reset(player->get_pos());
595   update_game_objects();
596
597   // Run init script
598   if(init_script != "") {
599     std::istringstream in(init_script);
600     run_script(in, std::string("Sector(") + name + ") - init");
601   }
602 }
603
604 void
605 Sector::deactivate()
606 {
607   if(_current != this)
608     return;
609
610   // remove sector entry from global vm
611   HSQUIRRELVM vm = Scripting::global_vm;
612   sq_pushroottable(vm);
613   sq_pushstring(vm, "sector", -1);
614   if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
615     throw Scripting::SquirrelError(vm, "Couldn't unset sector in roottable");
616   sq_pop(vm, 1);
617
618   for(GameObjects::iterator i = gameobjects.begin();
619       i != gameobjects.end(); ++i) {
620     GameObject* object = *i;
621
622     try_unexpose(object);
623   }
624
625   try_unexpose_me();
626   _current = NULL;
627 }
628
629 Rect
630 Sector::get_active_region()
631 {
632   return Rect(
633     camera->get_translation() - Vector(1600, 1200),
634     camera->get_translation() + Vector(1600, 1200) + Vector(SCREEN_WIDTH,SCREEN_HEIGHT));
635 }
636
637 void
638 Sector::update(float elapsed_time)
639 {
640   player->check_bounds(camera);
641
642   /* update objects */
643   for(GameObjects::iterator i = gameobjects.begin();
644           i != gameobjects.end(); ++i) {
645     GameObject* object = *i;
646     if(!object->is_valid())
647       continue;
648
649     object->update(elapsed_time);
650   }
651
652   /* Handle all possible collisions. */
653   handle_collisions();
654   update_game_objects();
655 }
656
657 void
658 Sector::update_game_objects()
659 {
660   /** cleanup marked objects */
661   for(std::vector<GameObject*>::iterator i = gameobjects.begin();
662       i != gameobjects.end(); /* nothing */) {
663     GameObject* object = *i;
664
665     if(object->is_valid()) {
666       ++i;
667       continue;
668     }
669
670     before_object_remove(object);
671
672     object->unref();
673     i = gameobjects.erase(i);
674   }
675
676   /* add newly created objects */
677   for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
678       i != gameobjects_new.end(); ++i)
679   {
680     GameObject* object = *i;
681
682     before_object_add(object);
683
684     gameobjects.push_back(object);
685   }
686   gameobjects_new.clear();
687 }
688
689 bool
690 Sector::before_object_add(GameObject* object)
691 {
692   Bullet* bullet = dynamic_cast<Bullet*> (object);
693   if(bullet != NULL) {
694     bullets.push_back(bullet);
695   }
696
697   MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
698   if(movingobject != NULL) {
699     moving_objects.push_back(movingobject);
700   }
701
702   Portable* portable = dynamic_cast<Portable*> (object);
703   if(portable != NULL) {
704     portables.push_back(portable);
705   }
706
707   TileMap* tilemap = dynamic_cast<TileMap*> (object);
708   if(tilemap != NULL && tilemap->is_solid()) {
709     solid_tilemaps.push_back(tilemap);
710   }
711
712   Camera* camera = dynamic_cast<Camera*> (object);
713   if(camera != NULL) {
714     if(this->camera != 0) {
715       log_warning << "Multiple cameras added. Ignoring" << std::endl;
716       return false;
717     }
718     this->camera = camera;
719   }
720
721   Player* player = dynamic_cast<Player*> (object);
722   if(player != NULL) {
723     if(this->player != 0) {
724       log_warning << "Multiple players added. Ignoring" << std::endl;
725       return false;
726     }
727     this->player = player;
728   }
729
730   UsesPhysic *physic_object = dynamic_cast<UsesPhysic *>(object);
731   if(physic_object)
732   {
733     physic_object->physic.set_gravity(gravity);
734   }
735
736
737   if(_current == this) {
738     try_expose(object);
739   }
740
741   return true;
742 }
743
744 void
745 Sector::try_expose(GameObject* object)
746 {
747   ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
748   if(interface != NULL) {
749     HSQUIRRELVM vm = Scripting::global_vm;
750     sq_pushobject(vm, sector_table);
751     interface->expose(vm, -1);
752     sq_pop(vm, 1);
753   }
754 }
755
756 void
757 Sector::try_expose_me()
758 {
759   HSQUIRRELVM vm = Scripting::global_vm;
760   sq_pushobject(vm, sector_table);
761   Scripting::SSector* interface = static_cast<Scripting::SSector*> (this);
762   expose_object(vm, -1, interface, "settings", false);
763   sq_pop(vm, 1);
764 }
765
766 void
767 Sector::before_object_remove(GameObject* object)
768 {
769   Portable* portable = dynamic_cast<Portable*> (object);
770   if(portable != NULL) {
771     portables.erase(std::find(portables.begin(), portables.end(), portable));
772   }
773   Bullet* bullet = dynamic_cast<Bullet*> (object);
774   if(bullet != NULL) {
775     bullets.erase(std::find(bullets.begin(), bullets.end(), bullet));
776   }
777   MovingObject* moving_object = dynamic_cast<MovingObject*> (object);
778   if(moving_object != NULL) {
779     moving_objects.erase(
780         std::find(moving_objects.begin(), moving_objects.end(), moving_object));
781   }
782
783   if(_current == this)
784     try_unexpose(object);
785 }
786
787 void
788 Sector::try_unexpose(GameObject* object)
789 {
790   ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
791   if(interface != NULL) {
792     HSQUIRRELVM vm = Scripting::global_vm;
793     SQInteger oldtop = sq_gettop(vm);
794     sq_pushobject(vm, sector_table);
795     try {
796       interface->unexpose(vm, -1);
797     } catch(std::exception& e) {
798       log_warning << "Couldn't unregister object: " << e.what() << std::endl;
799     }
800     sq_settop(vm, oldtop);
801   }
802 }
803
804 void
805 Sector::try_unexpose_me()
806 {
807   HSQUIRRELVM vm = Scripting::global_vm;
808   SQInteger oldtop = sq_gettop(vm);
809   sq_pushobject(vm, sector_table);
810   try {
811     Scripting::unexpose_object(vm, -1, "settings");
812   } catch(std::exception& e) {
813     log_warning << "Couldn't unregister object: " << e.what() << std::endl;
814   }
815   sq_settop(vm, oldtop);
816 }
817 void
818 Sector::draw(DrawingContext& context)
819 {
820   context.set_ambient_color( ambient_light );
821   context.push_transform();
822   context.set_translation(camera->get_translation());
823
824   for(GameObjects::iterator i = gameobjects.begin();
825       i != gameobjects.end(); ++i) {
826     GameObject* object = *i;
827     if(!object->is_valid())
828       continue;
829
830     if (draw_solids_only)
831     {
832       TileMap* tm = dynamic_cast<TileMap*>(object);
833       if (tm && !tm->is_solid())
834         continue;
835     }
836
837     object->draw(context);
838   }
839
840   if(show_collrects) {
841     Color col(0.2f, 0.2f, 0.2f, 0.7f);
842     for(MovingObjects::iterator i = moving_objects.begin();
843             i != moving_objects.end(); ++i) {
844       MovingObject* object = *i;
845       const Rect& rect = object->get_bbox();
846
847       context.draw_filled_rect(rect, col, LAYER_FOREGROUND1 + 10);
848     }
849   }
850
851   context.pop_transform();
852 }
853
854 /*-------------------------------------------------------------------------
855  * Collision Detection
856  *-------------------------------------------------------------------------*/
857
858 static const float SHIFT_DELTA = 7.0f;
859
860 /** r1 is supposed to be moving, r2 a solid object */
861 void check_collisions(collision::Constraints* constraints,
862                       const Vector& movement, const Rect& r1, const Rect& r2,
863                       GameObject* object = NULL, MovingObject* other = NULL)
864 {
865   if(!collision::intersects(r1, r2))
866     return;
867
868   // calculate intersection
869   float itop = r1.get_bottom() - r2.get_top();
870   float ibottom = r2.get_bottom() - r1.get_top();
871   float ileft = r1.get_right() - r2.get_left();
872   float iright = r2.get_right() - r1.get_left();
873
874   if(fabsf(movement.y) > fabsf(movement.x)) {
875     if(ileft < SHIFT_DELTA) {
876       constraints->right = std::min(constraints->right, r2.get_left());
877       return;
878     } else if(iright < SHIFT_DELTA) {
879       constraints->left = std::max(constraints->left, r2.get_right());
880       return;
881     }
882   } else {
883     // shiftout bottom/top
884     if(itop < SHIFT_DELTA) {
885       constraints->bottom = std::min(constraints->bottom, r2.get_top());
886       return;
887     } else if(ibottom < SHIFT_DELTA) {
888       constraints->top = std::max(constraints->top, r2.get_bottom());
889       return;
890     }
891   }
892
893   if(other != NULL) {
894     CollisionHit dummy;
895     HitResponse response = other->collision(*object, dummy);
896     if(response == PASSTHROUGH)
897       return;
898     if(other->get_movement() != Vector(0, 0)) {
899       // TODO what todo when we collide with 2 moving objects?!?
900       constraints->ground_movement = other->get_movement();
901     }
902   }
903
904   float vert_penetration = std::min(itop, ibottom);
905   float horiz_penetration = std::min(ileft, iright);
906   if(vert_penetration < horiz_penetration) {
907     if(itop < ibottom) {
908       constraints->bottom = std::min(constraints->bottom, r2.get_top());
909       constraints->hit.bottom = true;
910     } else {
911       constraints->top = std::max(constraints->top, r2.get_bottom());
912       constraints->hit.top = true;
913     }
914   } else {
915     if(ileft < iright) {
916       constraints->right = std::min(constraints->right, r2.get_left());
917       constraints->hit.right = true;
918     } else {
919       constraints->left = std::max(constraints->left, r2.get_right());
920       constraints->hit.left = true;
921     }
922   }
923 }
924
925 static const float DELTA = .001f;
926
927 void
928 Sector::collision_tilemap(collision::Constraints* constraints,
929                           const Vector& movement, const Rect& dest) const
930 {
931   // calculate rectangle where the object will move
932   float x1 = dest.get_left();
933   float x2 = dest.get_right();
934   float y1 = dest.get_top();
935   float y2 = dest.get_bottom();
936
937   for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
938     TileMap* solids = *i;
939
940     // test with all tiles in this rectangle
941     int starttilex = int(x1 - solids->get_x_offset()) / 32;
942     int starttiley = int(y1 - solids->get_y_offset()) / 32;
943     int max_x = int(x2 - solids->get_x_offset());
944     int max_y = int(y2+1 - solids->get_y_offset());
945
946     for(int x = starttilex; x*32 < max_x; ++x) {
947       for(int y = starttiley; y*32 < max_y; ++y) {
948         const Tile* tile = solids->get_tile(x, y);
949         if(!tile)
950           continue;
951         // skip non-solid tiles
952         if((tile->getAttributes() & Tile::SOLID) == 0)
953           continue;
954         // only handle unisolid when the player is falling down and when he was
955         // above the tile before
956         if(tile->getAttributes() & Tile::UNISOLID) {
957           if(movement.y <= 0 || dest.get_bottom() - movement.y - SHIFT_DELTA > y*32)
958             continue;
959         }
960
961         if(tile->getAttributes() & Tile::SLOPE) { // slope tile
962           AATriangle triangle;
963           Vector p1(x*32 + solids->get_x_offset(), y*32 + solids->get_y_offset());
964           Vector p2((x+1)*32 + solids->get_x_offset(), (y+1)*32 + solids->get_y_offset());
965           triangle = AATriangle(p1, p2, tile->getData());
966
967           collision::rectangle_aatriangle(constraints, dest, triangle);
968         } else { // normal rectangular tile
969           Rect rect(x*32 + solids->get_x_offset(), y*32 + solids->get_y_offset(), (x+1)*32 + solids->get_x_offset(), (y+1)*32 + solids->get_y_offset());
970           check_collisions(constraints, movement, dest, rect);
971         }
972       }
973     }
974   }
975 }
976
977 uint32_t
978 Sector::collision_tile_attributes(const Rect& dest) const
979 {
980   float x1 = dest.p1.x;
981   float y1 = dest.p1.y;
982   float x2 = dest.p2.x;
983   float y2 = dest.p2.y;
984
985   uint32_t result = 0;
986   for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
987     TileMap* solids = *i;
988
989     // test with all tiles in this rectangle
990     int starttilex = int(x1 - solids->get_x_offset()) / 32;
991     int starttiley = int(y1 - solids->get_y_offset()) / 32;
992     int max_x = int(x2 - solids->get_x_offset());
993     int max_y = int(y2+1 - solids->get_y_offset());
994
995     for(int x = starttilex; x*32 < max_x; ++x) {
996       for(int y = starttiley; y*32 < max_y; ++y) {
997         const Tile* tile = solids->get_tile(x, y);
998         if(!tile)
999           continue;
1000         result |= tile->getAttributes();
1001       }
1002     }
1003   }
1004
1005   return result;
1006 }
1007
1008 /** fills in CollisionHit and Normal vector of 2 intersecting rectangle */
1009 static void get_hit_normal(const Rect& r1, const Rect& r2, CollisionHit& hit,
1010                            Vector& normal)
1011 {
1012   float itop = r1.get_bottom() - r2.get_top();
1013   float ibottom = r2.get_bottom() - r1.get_top();
1014   float ileft = r1.get_right() - r2.get_left();
1015   float iright = r2.get_right() - r1.get_left();
1016
1017   float vert_penetration = std::min(itop, ibottom);
1018   float horiz_penetration = std::min(ileft, iright);
1019   if(vert_penetration < horiz_penetration) {
1020     if(itop < ibottom) {
1021       hit.bottom = true;
1022       normal.y = vert_penetration;
1023     } else {
1024       hit.top = true;
1025       normal.y = -vert_penetration;
1026     }
1027   } else {
1028     if(ileft < iright) {
1029       hit.right = true;
1030       normal.x = horiz_penetration;
1031     } else {
1032       hit.left = true;
1033       normal.x = -horiz_penetration;
1034     }
1035   }
1036 }
1037
1038 void
1039 Sector::collision_object(MovingObject* object1, MovingObject* object2) const
1040 {
1041   using namespace collision;
1042
1043   const Rect& r1 = object1->dest;
1044   const Rect& r2 = object2->dest;
1045
1046   CollisionHit hit;
1047   if(intersects(object1->dest, object2->dest)) {
1048     Vector normal;
1049     get_hit_normal(r1, r2, hit, normal);
1050
1051     HitResponse response1 = object1->collision(*object2, hit);
1052     std::swap(hit.left, hit.right);
1053     std::swap(hit.top, hit.bottom);
1054     HitResponse response2 = object2->collision(*object1, hit);
1055     assert( response1 != SOLID && response1 != PASSTHROUGH );
1056     assert( response2 != SOLID && response2 != PASSTHROUGH );
1057     if(response1 == CONTINUE && response2 == CONTINUE) {
1058       normal *= (0.5 + DELTA);
1059       object1->dest.move(-normal);
1060       object2->dest.move(normal);
1061     } else if (response1 == CONTINUE && response2 == FORCE_MOVE) {
1062       normal *= (1 + DELTA);
1063       object1->dest.move(-normal);
1064     } else if (response1 == FORCE_MOVE && response2 == CONTINUE) {
1065       normal *= (1 + DELTA);
1066       object2->dest.move(normal);
1067     }
1068   }
1069 }
1070
1071 void
1072 Sector::collision_static(collision::Constraints* constraints,
1073                          const Vector& movement, const Rect& dest,
1074                          GameObject& object)
1075 {
1076   collision_tilemap(constraints, movement, dest);
1077
1078   // collision with other (static) objects
1079   for(MovingObjects::iterator i = moving_objects.begin();
1080       i != moving_objects.end(); ++i) {
1081     MovingObject* moving_object = *i;
1082     if(moving_object->get_group() != COLGROUP_STATIC
1083        && moving_object->get_group() != COLGROUP_MOVING_STATIC)
1084       continue;
1085     if(!moving_object->is_valid())
1086       continue;
1087
1088     if(moving_object != &object)
1089       check_collisions(constraints, movement, dest, moving_object->bbox,
1090           &object, moving_object);
1091   }
1092 }
1093
1094 void
1095 Sector::collision_static_constrains(MovingObject& object)
1096 {
1097   using namespace collision;
1098   float infinity = (std::numeric_limits<float>::has_infinity ? std::numeric_limits<float>::infinity() : std::numeric_limits<float>::max());
1099
1100   Constraints constraints;
1101   Vector movement = object.get_movement();
1102   Rect& dest = object.dest;
1103   float owidth = object.get_bbox().get_width();
1104   float oheight = object.get_bbox().get_height();
1105
1106   for(int i = 0; i < 2; ++i) {
1107     collision_static(&constraints, Vector(0, movement.y), dest, object);
1108     if(!constraints.has_constraints())
1109       break;
1110
1111     // apply calculated horizontal constraints
1112     if(constraints.bottom < infinity) {
1113       float height = constraints.bottom - constraints.top;
1114       if(height < oheight) {
1115         // we're crushed, but ignore this for now, we'll get this again
1116         // later if we're really crushed or things will solve itself when
1117         // looking at the vertical constraints
1118       }
1119       dest.p2.y = constraints.bottom - DELTA;
1120       dest.p1.y = dest.p2.y - oheight;
1121     } else if(constraints.top > -infinity) {
1122       dest.p1.y = constraints.top + DELTA;
1123       dest.p2.y = dest.p1.y + oheight;
1124     }
1125   }
1126   if(constraints.has_constraints()) {
1127     if(constraints.hit.bottom) {
1128       dest.move(constraints.ground_movement);
1129     }
1130     if(constraints.hit.top || constraints.hit.bottom) {
1131       constraints.hit.left = false;
1132       constraints.hit.right = false;
1133       object.collision_solid(constraints.hit);
1134     }
1135   }
1136
1137   constraints = Constraints();
1138   for(int i = 0; i < 2; ++i) {
1139     collision_static(&constraints, movement, dest, object);
1140     if(!constraints.has_constraints())
1141       break;
1142
1143     // apply calculated vertical constraints
1144     if(constraints.right < infinity) {
1145       float width = constraints.right - constraints.left;
1146       if(width + SHIFT_DELTA < owidth) {
1147         printf("Object %p crushed horizontally... L:%f R:%f\n", &object,
1148             constraints.left, constraints.right);
1149         CollisionHit h;
1150         h.left = true;
1151         h.right = true;
1152         h.crush = true;
1153         object.collision_solid(h);
1154       } else {
1155         dest.p2.x = constraints.right - DELTA;
1156         dest.p1.x = dest.p2.x - owidth;
1157       }
1158     } else if(constraints.left > -infinity) {
1159       dest.p1.x = constraints.left + DELTA;
1160       dest.p2.x = dest.p1.x + owidth;
1161     }
1162   }
1163
1164   if(constraints.has_constraints()) {
1165     if( constraints.hit.left || constraints.hit.right
1166         || constraints.hit.top || constraints.hit.bottom
1167         || constraints.hit.crush )
1168       object.collision_solid(constraints.hit);
1169   }
1170
1171   // an extra pass to make sure we're not crushed horizontally
1172   constraints = Constraints();
1173   collision_static(&constraints, movement, dest, object);
1174   if(constraints.bottom < infinity) {
1175     float height = constraints.bottom - constraints.top;
1176     if(height + SHIFT_DELTA < oheight) {
1177       printf("Object %p crushed vertically...\n", &object);
1178       CollisionHit h;
1179       h.top = true;
1180       h.bottom = true;
1181       h.crush = true;
1182       object.collision_solid(h);
1183     }
1184   }
1185 }
1186
1187 void
1188 Sector::handle_collisions()
1189 {
1190   using namespace collision;
1191
1192   // calculate destination positions of the objects
1193   for(MovingObjects::iterator i = moving_objects.begin();
1194       i != moving_objects.end(); ++i) {
1195     MovingObject* moving_object = *i;
1196
1197     moving_object->dest = moving_object->get_bbox();
1198     moving_object->dest.move(moving_object->get_movement());
1199   }
1200
1201   // part1: COLGROUP_MOVING vs COLGROUP_STATIC and tilemap
1202   for(MovingObjects::iterator i = moving_objects.begin();
1203       i != moving_objects.end(); ++i) {
1204     MovingObject* moving_object = *i;
1205     if((moving_object->get_group() != COLGROUP_MOVING
1206           && moving_object->get_group() != COLGROUP_MOVING_STATIC
1207           && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
1208         || !moving_object->is_valid())
1209       continue;
1210
1211     collision_static_constrains(*moving_object);
1212   }
1213
1214
1215   // part2: COLGROUP_MOVING vs tile attributes
1216   for(MovingObjects::iterator i = moving_objects.begin();
1217       i != moving_objects.end(); ++i) {
1218     MovingObject* moving_object = *i;
1219     if((moving_object->get_group() != COLGROUP_MOVING
1220           && moving_object->get_group() != COLGROUP_MOVING_STATIC
1221           && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
1222         || !moving_object->is_valid())
1223       continue;
1224
1225     uint32_t tile_attributes = collision_tile_attributes(moving_object->dest);
1226     if(tile_attributes > Tile::FIRST_INTERESTING_FLAG) {
1227       moving_object->collision_tile(tile_attributes);
1228     }
1229   }
1230
1231   // part2.5: COLGROUP_MOVING vs COLGROUP_TOUCHABLE
1232   for(MovingObjects::iterator i = moving_objects.begin();
1233       i != moving_objects.end(); ++i) {
1234     MovingObject* moving_object = *i;
1235     if((moving_object->get_group() != COLGROUP_MOVING
1236           && moving_object->get_group() != COLGROUP_MOVING_STATIC)
1237         || !moving_object->is_valid())
1238       continue;
1239
1240     for(MovingObjects::iterator i2 = moving_objects.begin();
1241         i2 != moving_objects.end(); ++i2) {
1242       MovingObject* moving_object_2 = *i2;
1243       if(moving_object_2->get_group() != COLGROUP_TOUCHABLE
1244          || !moving_object_2->is_valid())
1245         continue;
1246
1247       if(intersects(moving_object->dest, moving_object_2->dest)) {
1248         Vector normal;
1249         CollisionHit hit;
1250         get_hit_normal(moving_object->dest, moving_object_2->dest,
1251                        hit, normal);
1252         moving_object->collision(*moving_object_2, hit);
1253         moving_object_2->collision(*moving_object, hit);
1254       }
1255     }
1256   }
1257
1258   // part3: COLGROUP_MOVING vs COLGROUP_MOVING
1259   for(MovingObjects::iterator i = moving_objects.begin();
1260       i != moving_objects.end(); ++i) {
1261     MovingObject* moving_object = *i;
1262
1263     if((moving_object->get_group() != COLGROUP_MOVING
1264           && moving_object->get_group() != COLGROUP_MOVING_STATIC)
1265         || !moving_object->is_valid())
1266       continue;
1267
1268     for(MovingObjects::iterator i2 = i+1;
1269         i2 != moving_objects.end(); ++i2) {
1270       MovingObject* moving_object_2 = *i2;
1271       if((moving_object_2->get_group() != COLGROUP_MOVING
1272             && moving_object_2->get_group() != COLGROUP_MOVING_STATIC)
1273          || !moving_object_2->is_valid())
1274         continue;
1275
1276       collision_object(moving_object, moving_object_2);
1277     }
1278   }
1279
1280   // apply object movement
1281   for(MovingObjects::iterator i = moving_objects.begin();
1282       i != moving_objects.end(); ++i) {
1283     MovingObject* moving_object = *i;
1284
1285     moving_object->bbox = moving_object->dest;
1286     moving_object->movement = Vector(0, 0);
1287   }
1288 }
1289
1290 bool
1291 Sector::is_free_of_tiles(const Rect& rect) const
1292 {
1293   using namespace collision;
1294
1295   for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1296     TileMap* solids = *i;
1297
1298     // test with all tiles in this rectangle
1299     int starttilex = int(rect.p1.x - solids->get_x_offset()) / 32;
1300     int starttiley = int(rect.p1.y - solids->get_y_offset()) / 32;
1301     int max_x = int(rect.p2.x - solids->get_x_offset());
1302     int max_y = int(rect.p2.y - solids->get_y_offset());
1303
1304     for(int x = starttilex; x*32 <= max_x; ++x) {
1305       for(int y = starttiley; y*32 <= max_y; ++y) {
1306         const Tile* tile = solids->get_tile(x, y);
1307         if(!tile) continue;
1308         if(tile->getAttributes() & Tile::SLOPE) {
1309           AATriangle triangle;
1310           Vector p1(x*32 + solids->get_x_offset(), y*32 + solids->get_y_offset());
1311           Vector p2((x+1)*32 + solids->get_x_offset(), (y+1)*32 + solids->get_y_offset());
1312           triangle = AATriangle(p1, p2, tile->getData());
1313           Constraints constraints;
1314           return collision::rectangle_aatriangle(&constraints, rect, triangle);
1315         }
1316         if(tile->getAttributes() & Tile::SOLID) return false;
1317       }
1318     }
1319   }
1320
1321   return true;
1322 }
1323
1324 bool
1325 Sector::is_free_of_statics(const Rect& rect, const MovingObject* ignore_object) const
1326 {
1327   using namespace collision;
1328
1329   if (!is_free_of_tiles(rect)) return false;
1330
1331   for(MovingObjects::const_iterator i = moving_objects.begin();
1332       i != moving_objects.end(); ++i) {
1333     const MovingObject* moving_object = *i;
1334     if (moving_object == ignore_object) continue;
1335     if (!moving_object->is_valid()) continue;
1336     if (moving_object->get_group() == COLGROUP_STATIC) {
1337       if(intersects(rect, moving_object->get_bbox())) return false;
1338     }
1339   }
1340
1341   return true;
1342 }
1343
1344 bool
1345 Sector::is_free_of_movingstatics(const Rect& rect, const MovingObject* ignore_object) const
1346 {
1347   using namespace collision;
1348
1349   if (!is_free_of_tiles(rect)) return false;
1350
1351   for(MovingObjects::const_iterator i = moving_objects.begin();
1352       i != moving_objects.end(); ++i) {
1353     const MovingObject* moving_object = *i;
1354     if (moving_object == ignore_object) continue;
1355     if (!moving_object->is_valid()) continue;
1356     if ((moving_object->get_group() == COLGROUP_MOVING)
1357       || (moving_object->get_group() == COLGROUP_MOVING_STATIC)
1358       || (moving_object->get_group() == COLGROUP_STATIC)) {
1359       if(intersects(rect, moving_object->get_bbox())) return false;
1360     }
1361   }
1362
1363   return true;
1364 }
1365
1366 bool
1367 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
1368 {
1369   // TODO remove this function and move these checks elsewhere...
1370
1371   Bullet* new_bullet = 0;
1372   if((player_status->bonus == FIRE_BONUS &&
1373       (int)bullets.size() >= player_status->max_fire_bullets) ||
1374      (player_status->bonus == ICE_BONUS &&
1375       (int)bullets.size() >= player_status->max_ice_bullets))
1376     return false;
1377   new_bullet = new Bullet(pos, xm, dir, player_status->bonus);
1378   add_object(new_bullet);
1379
1380   sound_manager->play("sounds/shoot.wav");
1381
1382   return true;
1383 }
1384
1385 bool
1386 Sector::add_smoke_cloud(const Vector& pos)
1387 {
1388   add_object(new SmokeCloud(pos));
1389   return true;
1390 }
1391
1392 void
1393 Sector::play_music(MusicType type)
1394 {
1395   currentmusic = type;
1396   switch(currentmusic) {
1397     case LEVEL_MUSIC:
1398       sound_manager->play_music(music);
1399       break;
1400     case HERRING_MUSIC:
1401       sound_manager->play_music("music/salcon.ogg");
1402       break;
1403     case HERRING_WARNING_MUSIC:
1404       sound_manager->stop_music(TUX_INVINCIBLE_TIME_WARNING);
1405       break;
1406     default:
1407       sound_manager->play_music("");
1408       break;
1409   }
1410 }
1411
1412 MusicType
1413 Sector::get_music_type()
1414 {
1415   return currentmusic;
1416 }
1417
1418 int
1419 Sector::get_total_badguys()
1420 {
1421   int total_badguys = 0;
1422   for(GameObjects::iterator i = gameobjects.begin();
1423       i != gameobjects.end(); ++i) {
1424     BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
1425     if (badguy && badguy->countMe)
1426       total_badguys++;
1427   }
1428
1429   return total_badguys;
1430 }
1431
1432 bool
1433 Sector::inside(const Rect& rect) const
1434 {
1435   for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1436     TileMap* solids = *i;
1437     bool horizontally = ((rect.p2.x >= 0 + solids->get_x_offset()) && (rect.p1.x <= solids->get_width() * 32 + solids->get_x_offset()));
1438     bool vertically = (rect.p1.y <= solids->get_height() * 32 + solids->get_y_offset());
1439     if (horizontally && vertically) return true;
1440   }
1441   return false;
1442 }
1443
1444 float
1445 Sector::get_width() const
1446 {
1447   float width = 0;
1448   for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1449     TileMap* solids = *i;
1450     if ((solids->get_width() * 32 + solids->get_x_offset()) > width) width = (solids->get_width() * 32 + solids->get_x_offset());
1451   }
1452   return width;
1453 }
1454
1455 float
1456 Sector::get_height() const
1457 {
1458   float height = 0;
1459   for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1460     TileMap* solids = *i;
1461     if ((solids->get_height() * 32 + solids->get_y_offset()) > height) height = (solids->get_height() * 32 + solids->get_y_offset());
1462   }
1463   return height;
1464 }
1465
1466 void
1467 Sector::change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id)
1468 {
1469   for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1470     TileMap* solids = *i;
1471     solids->change_all(old_tile_id, new_tile_id);
1472   }
1473 }
1474
1475
1476 void
1477 Sector::set_ambient_light(float red, float green, float blue)
1478 {
1479   ambient_light.red = red;
1480   ambient_light.green = green;
1481   ambient_light.blue = blue;
1482 }
1483
1484 float
1485 Sector::get_ambient_red()
1486 {
1487   return ambient_light.red;
1488 }
1489
1490 float
1491 Sector::get_ambient_green()
1492 {
1493   return ambient_light.green;
1494 }
1495
1496 float
1497 Sector::get_ambient_blue()
1498 {
1499   return ambient_light.blue;
1500 }