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