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