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