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