3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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.
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"
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"
70 Sector* Sector::_current = 0;
72 bool Sector::show_collrects = false;
73 bool Sector::draw_solids_only = false;
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)
79 add_object(new Player(player_status, "Tux"));
80 add_object(new DisplayEffect("Effect"));
81 add_object(new TextObject("Text"));
83 // create a new squirrel table for the sector
84 using namespace Scripting;
86 sq_collectgarbage(global_vm);
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");
93 sq_resetobject(§or_table);
94 if(SQ_FAILED(sq_getstackobj(global_vm, -1, §or_table)))
95 throw Scripting::SquirrelError(global_vm, "Couldn't get sector table");
96 sq_addref(global_vm, §or_table);
102 using namespace Scripting;
106 for(ScriptList::iterator i = scripts.begin();
107 i != scripts.end(); ++i) {
108 HSQOBJECT& object = *i;
109 sq_release(global_vm, &object);
111 sq_release(global_vm, §or_table);
112 sq_collectgarbage(global_vm);
114 update_game_objects();
115 assert(gameobjects_new.size() == 0);
117 for(GameObjects::iterator i = gameobjects.begin();
118 i != gameobjects.end(); ++i) {
119 GameObject* object = *i;
120 before_object_remove(object);
124 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
136 Sector::parse_object(const std::string& name, const lisp::Lisp& reader)
138 if(name == "camera") {
139 Camera* camera = new Camera(this, "Camera");
140 camera->parse(reader);
142 } else if(name == "particles-snow") {
143 SnowParticleSystem* partsys = new SnowParticleSystem();
144 partsys->parse(reader);
146 } else if(name == "particles-rain") {
147 RainParticleSystem* partsys = new RainParticleSystem();
148 partsys->parse(reader);
150 } else if(name == "particles-comets") {
151 CometParticleSystem* partsys = new CometParticleSystem();
152 partsys->parse(reader);
154 } else if(name == "particles-ghosts") {
155 GhostParticleSystem* partsys = new GhostParticleSystem();
156 partsys->parse(reader);
158 } else if(name == "particles-clouds") {
159 CloudParticleSystem* partsys = new CloudParticleSystem();
160 partsys->parse(reader);
162 } else if(name == "money") { // for compatibility with old maps
163 return new Jumpy(reader);
167 return create_object(name, reader);
168 } catch(std::exception& e) {
169 log_warning << e.what() << "" << std::endl;
176 Sector::parse(const lisp::Lisp& sector)
178 lisp::ListIterator iter(§or);
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;
198 ambient_light = Color( vColor );
201 GameObject* object = parse_object(token, *(iter.lisp()));
208 update_game_objects();
210 if(solid_tilemaps.size() < 1) log_warning << "sector '" << name << "' does not contain a solid tile layer." << std::endl;
214 log_warning << "sector '" << name << "' does not contain a camera." << std::endl;
215 update_game_objects();
216 add_object(new Camera(this, "Camera"));
219 update_game_objects();
223 Sector::parse_old_format(const lisp::Lisp& reader)
226 reader.get("gravity", gravity);
228 std::string backgroundimage;
229 reader.get("background", backgroundimage);
231 reader.get("bkgd_speed", bgspeed);
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;
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;
250 if(backgroundimage != "") {
251 Background* background = new Background();
252 background->set_image(
253 std::string("images/background/") + backgroundimage, bgspeed);
254 add_object(background);
256 Gradient* gradient = new Gradient();
257 gradient->set_gradient(bkgd_top, bkgd_bottom);
258 add_object(gradient);
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());
270 Vector startpos(100, 170);
271 reader.get("start_pos_x", startpos.x);
272 reader.get("start_pos_y", startpos.y);
274 SpawnPoint* spawn = new SpawnPoint;
275 spawn->pos = startpos;
276 spawn->name = "main";
277 spawnpoints.push_back(spawn);
279 music = "chipdisko.ogg";
280 reader.get("music", music);
281 music = "music/" + music;
283 int width = 30, height = 15;
284 reader.get("width", width);
285 reader.get("height", height);
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);
295 if(reader.get_vector("background-tm", tiles)) {
296 TileMap* tilemap = new TileMap();
297 tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
301 if(reader.get_vector("foreground-tm", tiles)) {
302 TileMap* tilemap = new TileMap();
303 tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
307 // read reset-points (now spawn-points)
308 const lisp::Lisp* resetpoints = reader.get_lisp("reset-points");
310 lisp::ListIterator iter(resetpoints);
312 if(iter.item() == "point") {
314 if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
316 SpawnPoint* sp = new SpawnPoint;
319 spawnpoints.push_back(sp);
322 log_warning << "Unknown token '" << iter.item() << "' in reset-points." << std::endl;
328 const lisp::Lisp* objects = reader.get_lisp("objects");
330 lisp::ListIterator iter(objects);
332 GameObject* object = parse_object(iter.item(), *(iter.lisp()));
336 log_warning << "Unknown object '" << iter.item() << "' in level." << std::endl;
342 Camera* camera = new Camera(this, "Camera");
345 update_game_objects();
347 if(solid_tilemaps.size() < 1) log_warning << "sector '" << name << "' does not contain a solid tile layer." << std::endl;
350 update_game_objects();
354 Sector::fix_old_tiles()
356 for(std::list<TileMap*>::iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
357 TileMap* solids = *i;
359 for(size_t x=0; x < solids->get_width(); ++x) {
360 for(size_t y=0; y < solids->get_height(); ++y) {
361 const Tile* tile = solids->get_tile(x, y);
362 Vector pos(solids->get_x_offset() + x*32, solids->get_y_offset() + y*32);
364 if(tile->getID() == 112) {
365 add_object(new InvisibleBlock(pos));
366 solids->change(x, y, 0);
367 } else if(tile->getAttributes() & Tile::COIN) {
368 add_object(new Coin(pos));
369 solids->change(x, y, 0);
370 } else if(tile->getAttributes() & Tile::FULLBOX) {
371 add_object(new BonusBlock(pos, tile->getData()));
372 solids->change(x, y, 0);
373 } else if(tile->getAttributes() & Tile::BRICK) {
374 add_object(new Brick(pos, tile->getData()));
375 solids->change(x, y, 0);
376 } else if(tile->getAttributes() & Tile::GOAL) {
377 std::string sequence = tile->getData() == 0 ? "endsequence" : "stoptux";
378 add_object(new SequenceTrigger(pos, sequence));
379 solids->change(x, y, 0);
385 // add lights for special tiles
386 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); i++) {
387 TileMap* tm = dynamic_cast<TileMap*>(*i);
389 for(size_t x=0; x < tm->get_width(); ++x) {
390 for(size_t y=0; y < tm->get_height(); ++y) {
391 const Tile* tile = tm->get_tile(x, y);
392 Vector pos(tm->get_x_offset() + x*32, tm->get_y_offset() + y*32);
393 Vector center(pos.x + 16, pos.y + 16);
396 if (tile->getID() == 1517) {
397 float pseudo_rnd = (float)((int)pos.x % 10) / 10;
398 add_object(new PulsingLight(center, 1.0 + pseudo_rnd, 0.9, 1.0, Color(1.0, 1.0, 0.6, 1.0)));
401 if ((tile->getID() == 173) || (tile->getID() == 1700) || (tile->getID() == 1705) || (tile->getID() == 1706)) {
402 // space lights a bit
403 if (((tm->get_tile(x-1, y)->getID() != tm->get_tile(x,y)->getID())
404 && (tm->get_tile(x, y-1)->getID() != tm->get_tile(x,y)->getID()))
405 || ((x % 3 == 0) && (y % 3 == 0))) {
406 float pseudo_rnd = (float)((int)pos.x % 10) / 10;
407 add_object(new PulsingLight(center, 1.0 + pseudo_rnd, 0.8, 1.0, Color(1.0, 0.3, 0.0, 1.0)));
419 Sector::write(lisp::Writer& writer)
421 writer.write_string("name", name);
422 writer.write_float("gravity", gravity);
423 writer.write_string("music", music);
426 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
428 SpawnPoint* spawn = *i;
429 writer.start_list("spawn-points");
430 writer.write_string("name", spawn->name);
431 writer.write_float("x", spawn->pos.x);
432 writer.write_float("y", spawn->pos.y);
433 writer.end_list("spawn-points");
437 for(GameObjects::iterator i = gameobjects.begin();
438 i != gameobjects.end(); ++i) {
439 Serializable* serializable = dynamic_cast<Serializable*> (*i);
441 serializable->write(writer);
446 Sector::run_script(std::istream& in, const std::string& sourcename)
448 using namespace Scripting;
450 // garbage collect thread list
451 for(ScriptList::iterator i = scripts.begin();
452 i != scripts.end(); ) {
453 HSQOBJECT& object = *i;
454 HSQUIRRELVM vm = object_to_vm(object);
456 if(sq_getvmstate(vm) != SQ_VMSTATE_SUSPENDED) {
457 sq_release(global_vm, &object);
458 i = scripts.erase(i);
465 HSQOBJECT object = create_thread(global_vm);
466 scripts.push_back(object);
468 HSQUIRRELVM vm = object_to_vm(object);
470 // set sector_table as roottable for the thread
471 sq_pushobject(vm, sector_table);
474 compile_and_run(vm, in, sourcename);
480 Sector::add_object(GameObject* object)
482 // make sure the object isn't already in the list
484 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
487 assert("object already added to sector" == 0);
490 for(GameObjects::iterator i = gameobjects_new.begin();
491 i != gameobjects_new.end(); ++i) {
493 assert("object already added to sector" == 0);
499 gameobjects_new.push_back(object);
503 Sector::activate(const std::string& spawnpoint)
506 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
508 if((*i)->name == spawnpoint) {
514 log_warning << "Spawnpoint '" << spawnpoint << "' not found." << std::endl;
515 if(spawnpoint != "main") {
518 activate(Vector(0, 0));
526 Sector::activate(const Vector& player_pos)
528 if(_current != this) {
530 _current->deactivate();
533 // register sectortable as sector in scripting
534 HSQUIRRELVM vm = Scripting::global_vm;
535 sq_pushroottable(vm);
536 sq_pushstring(vm, "sector", -1);
537 sq_pushobject(vm, sector_table);
538 if(SQ_FAILED(sq_createslot(vm, -3)))
539 throw Scripting::SquirrelError(vm, "Couldn't set sector in roottable");
542 for(GameObjects::iterator i = gameobjects.begin();
543 i != gameobjects.end(); ++i) {
544 GameObject* object = *i;
551 // spawn smalltux below spawnpoint
552 if (!player->is_big()) {
553 player->move(player_pos + Vector(0,32));
555 player->move(player_pos);
558 // spawning tux in the ground would kill him
559 if(!is_free_of_tiles(player->get_bbox())) {
560 log_warning << "Tried spawning Tux in solid matter. Compensating." << std::endl;
561 Vector npos = player->get_bbox().p1;
566 camera->reset(player->get_pos());
567 update_game_objects();
570 if(init_script != "") {
571 std::istringstream in(init_script);
572 run_script(in, std::string("Sector(") + name + ") - init");
582 // remove sector entry from global vm
583 HSQUIRRELVM vm = Scripting::global_vm;
584 sq_pushroottable(vm);
585 sq_pushstring(vm, "sector", -1);
586 if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
587 throw Scripting::SquirrelError(vm, "Couldn't unset sector in roottable");
590 for(GameObjects::iterator i = gameobjects.begin();
591 i != gameobjects.end(); ++i) {
592 GameObject* object = *i;
594 try_unexpose(object);
602 Sector::get_active_region()
605 camera->get_translation() - Vector(1600, 1200),
606 camera->get_translation() + Vector(1600, 1200));
610 bool is_tilemap_solid(const TileMap* tm) { return tm->is_solid(); }
611 bool is_tilemap_nonsolid(const TileMap* tm) { return !tm->is_solid(); }
615 Sector::update_solid_tilemap_list()
618 // remove non-solid tilemaps from list
619 solid_tilemaps.erase(std::remove_if(solid_tilemaps.begin(), solid_tilemaps.end(), is_tilemap_nonsolid), solid_tilemaps.end());
621 // if tilemaps are to be added, create new list of solid tilemaps
622 if (solid_tilemaps.size() != (unsigned int)std::count_if(tilemaps.begin(), tilemaps.end(), is_tilemap_solid)) {
623 log_debug << "Found new solid tilemaps - this is eeevil! Re-creating list of solid tilemaps." << std::endl;
624 solid_tilemaps.clear();
625 for(std::list<TileMap*>::iterator i = tilemaps.begin(); i != tilemaps.end(); i++) {
627 if (tm->is_solid()) solid_tilemaps.push_back(tm);
634 Sector::update(float elapsed_time)
636 player->check_bounds(camera);
638 // update solid_tilemaps list
639 update_solid_tilemap_list();
642 for(GameObjects::iterator i = gameobjects.begin();
643 i != gameobjects.end(); ++i) {
644 GameObject* object = *i;
645 if(!object->is_valid())
648 object->update(elapsed_time);
651 /* Handle all possible collisions. */
653 update_game_objects();
657 Sector::update_game_objects()
659 /** cleanup marked objects */
660 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
661 i != gameobjects.end(); /* nothing */) {
662 GameObject* object = *i;
664 if(object->is_valid()) {
669 before_object_remove(object);
672 i = gameobjects.erase(i);
675 /* add newly created objects */
676 for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
677 i != gameobjects_new.end(); ++i)
679 GameObject* object = *i;
681 before_object_add(object);
683 gameobjects.push_back(object);
685 gameobjects_new.clear();
689 Sector::before_object_add(GameObject* object)
691 Bullet* bullet = dynamic_cast<Bullet*> (object);
693 bullets.push_back(bullet);
696 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
697 if(movingobject != NULL) {
698 moving_objects.push_back(movingobject);
701 Portable* portable = dynamic_cast<Portable*> (object);
702 if(portable != NULL) {
703 portables.push_back(portable);
706 TileMap* tilemap = dynamic_cast<TileMap*> (object);
707 if(tilemap != NULL) {
708 tilemaps.push_back(tilemap);
709 if (tilemap->is_solid()) solid_tilemaps.push_back(tilemap);
712 Camera* camera = dynamic_cast<Camera*> (object);
714 if(this->camera != 0) {
715 log_warning << "Multiple cameras added. Ignoring" << std::endl;
718 this->camera = camera;
721 Player* player = dynamic_cast<Player*> (object);
723 if(this->player != 0) {
724 log_warning << "Multiple players added. Ignoring" << std::endl;
727 this->player = player;
730 if(_current == this) {
738 Sector::try_expose(GameObject* object)
740 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
741 if(interface != NULL) {
742 HSQUIRRELVM vm = Scripting::global_vm;
743 sq_pushobject(vm, sector_table);
744 interface->expose(vm, -1);
750 Sector::try_expose_me()
752 HSQUIRRELVM vm = Scripting::global_vm;
753 sq_pushobject(vm, sector_table);
754 Scripting::SSector* interface = static_cast<Scripting::SSector*> (this);
755 expose_object(vm, -1, interface, "settings", false);
760 Sector::before_object_remove(GameObject* object)
762 Portable* portable = dynamic_cast<Portable*> (object);
763 if(portable != NULL) {
764 portables.erase(std::find(portables.begin(), portables.end(), portable));
766 TileMap* tilemap = dynamic_cast<TileMap*> (object);
767 if(tilemap != NULL) {
768 tilemaps.erase(std::find(tilemaps.begin(), tilemaps.end(), tilemap));
769 if (tilemap->is_solid()) solid_tilemaps.erase(std::find(solid_tilemaps.begin(), solid_tilemaps.end(), tilemap));
771 Bullet* bullet = dynamic_cast<Bullet*> (object);
773 bullets.erase(std::find(bullets.begin(), bullets.end(), bullet));
775 MovingObject* moving_object = dynamic_cast<MovingObject*> (object);
776 if(moving_object != NULL) {
777 moving_objects.erase(
778 std::find(moving_objects.begin(), moving_objects.end(), moving_object));
782 try_unexpose(object);
786 Sector::try_unexpose(GameObject* object)
788 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
789 if(interface != NULL) {
790 HSQUIRRELVM vm = Scripting::global_vm;
791 SQInteger oldtop = sq_gettop(vm);
792 sq_pushobject(vm, sector_table);
794 interface->unexpose(vm, -1);
795 } catch(std::exception& e) {
796 log_warning << "Couldn't unregister object: " << e.what() << std::endl;
798 sq_settop(vm, oldtop);
803 Sector::try_unexpose_me()
805 HSQUIRRELVM vm = Scripting::global_vm;
806 SQInteger oldtop = sq_gettop(vm);
807 sq_pushobject(vm, sector_table);
809 Scripting::unexpose_object(vm, -1, "settings");
810 } catch(std::exception& e) {
811 log_warning << "Couldn't unregister object: " << e.what() << std::endl;
813 sq_settop(vm, oldtop);
816 Sector::draw(DrawingContext& context)
818 context.set_ambient_color( ambient_light );
819 context.push_transform();
820 context.set_translation(camera->get_translation());
822 for(GameObjects::iterator i = gameobjects.begin();
823 i != gameobjects.end(); ++i) {
824 GameObject* object = *i;
825 if(!object->is_valid())
828 if (draw_solids_only)
830 TileMap* tm = dynamic_cast<TileMap*>(object);
831 if (tm && !tm->is_solid())
835 object->draw(context);
839 Color col(0.2, 0.2, 0.2, 0.7);
840 for(MovingObjects::iterator i = moving_objects.begin();
841 i != moving_objects.end(); ++i) {
842 MovingObject* object = *i;
843 const Rect& rect = object->get_bbox();
845 context.draw_filled_rect(rect, col, LAYER_FOREGROUND1 + 10);
849 context.pop_transform();
852 /*-------------------------------------------------------------------------
853 * Collision Detection
854 *-------------------------------------------------------------------------*/
856 static const float SHIFT_DELTA = 7.0f;
858 /** r1 is supposed to be moving, r2 a solid object */
859 void check_collisions(collision::Constraints* constraints,
860 const Vector& movement, const Rect& r1, const Rect& r2,
861 GameObject* object = NULL, MovingObject* other = NULL)
863 if(!collision::intersects(r1, r2))
866 // calculate intersection
867 float itop = r1.get_bottom() - r2.get_top();
868 float ibottom = r2.get_bottom() - r1.get_top();
869 float ileft = r1.get_right() - r2.get_left();
870 float iright = r2.get_right() - r1.get_left();
872 if(fabsf(movement.y) > fabsf(movement.x)) {
873 if(ileft < SHIFT_DELTA) {
874 constraints->right = std::min(constraints->right, r2.get_left());
876 } else if(iright < SHIFT_DELTA) {
877 constraints->left = std::max(constraints->left, r2.get_right());
881 // shiftout bottom/top
882 if(itop < SHIFT_DELTA) {
883 constraints->bottom = std::min(constraints->bottom, r2.get_top());
885 } else if(ibottom < SHIFT_DELTA) {
886 constraints->top = std::max(constraints->top, r2.get_bottom());
893 HitResponse response = other->collision(*object, dummy);
894 if(response == PASSTHROUGH)
896 if(other->get_movement() != Vector(0, 0)) {
897 // TODO what todo when we collide with 2 moving objects?!?
898 constraints->ground_movement = other->get_movement();
902 float vert_penetration = std::min(itop, ibottom);
903 float horiz_penetration = std::min(ileft, iright);
904 if(vert_penetration < horiz_penetration) {
906 constraints->bottom = std::min(constraints->bottom, r2.get_top());
907 constraints->hit.bottom = true;
909 constraints->top = std::max(constraints->top, r2.get_bottom());
910 constraints->hit.top = true;
914 constraints->right = std::min(constraints->right, r2.get_left());
915 constraints->hit.right = true;
917 constraints->left = std::max(constraints->left, r2.get_right());
918 constraints->hit.left = true;
923 static const float DELTA = .001;
926 Sector::collision_tilemap(collision::Constraints* constraints,
927 const Vector& movement, const Rect& dest) const
929 // calculate rectangle where the object will move
930 float x1 = dest.get_left();
931 float x2 = dest.get_right();
932 float y1 = dest.get_top();
933 float y2 = dest.get_bottom();
935 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
936 TileMap* solids = *i;
938 // test with all tiles in this rectangle
939 int starttilex = int(x1 - solids->get_x_offset()) / 32;
940 int starttiley = int(y1 - solids->get_y_offset()) / 32;
941 int max_x = int(x2 - solids->get_x_offset());
942 int max_y = int(y2+1 - solids->get_y_offset());
944 for(int x = starttilex; x*32 < max_x; ++x) {
945 for(int y = starttiley; y*32 < max_y; ++y) {
946 const Tile* tile = solids->get_tile(x, y);
949 // skip non-solid tiles
950 if((tile->getAttributes() & Tile::SOLID) == 0)
952 // only handle unisolid when the player is falling down and when he was
953 // above the tile before
954 if(tile->getAttributes() & Tile::UNISOLID) {
955 if(movement.y <= 0 || dest.get_bottom() - movement.y - SHIFT_DELTA > y*32)
959 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
961 Vector p1(x*32 + solids->get_x_offset(), y*32 + solids->get_y_offset());
962 Vector p2((x+1)*32 + solids->get_x_offset(), (y+1)*32 + solids->get_y_offset());
963 triangle = AATriangle(p1, p2, tile->getData());
965 collision::rectangle_aatriangle(constraints, dest, triangle);
966 } else { // normal rectangular tile
967 Rect rect(x*32 + solids->get_x_offset(), y*32 + solids->get_y_offset(), (x+1)*32 + solids->get_x_offset(), (y+1)*32 + solids->get_y_offset());
968 check_collisions(constraints, movement, dest, rect);
976 Sector::collision_tile_attributes(const Rect& dest) const
978 float x1 = dest.p1.x;
979 float y1 = dest.p1.y;
980 float x2 = dest.p2.x;
981 float y2 = dest.p2.y;
984 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
985 TileMap* solids = *i;
987 // test with all tiles in this rectangle
988 int starttilex = int(x1 - solids->get_x_offset()) / 32;
989 int starttiley = int(y1 - solids->get_y_offset()) / 32;
990 int max_x = int(x2 - solids->get_x_offset());
991 int max_y = int(y2+1 - solids->get_y_offset());
993 for(int x = starttilex; x*32 < max_x; ++x) {
994 for(int y = starttiley; y*32 < max_y; ++y) {
995 const Tile* tile = solids->get_tile(x, y);
998 result |= tile->getAttributes();
1006 /** fills in CollisionHit and Normal vector of 2 intersecting rectangle */
1007 static void get_hit_normal(const Rect& r1, const Rect& r2, CollisionHit& hit,
1010 float itop = r1.get_bottom() - r2.get_top();
1011 float ibottom = r2.get_bottom() - r1.get_top();
1012 float ileft = r1.get_right() - r2.get_left();
1013 float iright = r2.get_right() - r1.get_left();
1015 float vert_penetration = std::min(itop, ibottom);
1016 float horiz_penetration = std::min(ileft, iright);
1017 if(vert_penetration < horiz_penetration) {
1018 if(itop < ibottom) {
1020 normal.y = vert_penetration;
1023 normal.y = -vert_penetration;
1026 if(ileft < iright) {
1028 normal.x = horiz_penetration;
1031 normal.x = -horiz_penetration;
1037 Sector::collision_object(MovingObject* object1, MovingObject* object2) const
1039 using namespace collision;
1041 const Rect& r1 = object1->dest;
1042 const Rect& r2 = object2->dest;
1045 if(intersects(object1->dest, object2->dest)) {
1047 get_hit_normal(r1, r2, hit, normal);
1049 HitResponse response1 = object1->collision(*object2, hit);
1050 std::swap(hit.left, hit.right);
1051 std::swap(hit.top, hit.bottom);
1052 HitResponse response2 = object2->collision(*object1, hit);
1053 assert( response1 != SOLID && response1 != PASSTHROUGH );
1054 assert( response2 != SOLID && response2 != PASSTHROUGH );
1055 if(response1 == CONTINUE && response2 == CONTINUE) {
1056 normal *= (0.5 + DELTA);
1057 object1->dest.move(-normal);
1058 object2->dest.move(normal);
1059 } else if (response1 == CONTINUE && response2 == FORCE_MOVE) {
1060 normal *= (1 + DELTA);
1061 object1->dest.move(-normal);
1062 } else if (response1 == FORCE_MOVE && response2 == CONTINUE) {
1063 normal *= (1 + DELTA);
1064 object2->dest.move(normal);
1070 Sector::collision_static(collision::Constraints* constraints,
1071 const Vector& movement, const Rect& dest,
1074 collision_tilemap(constraints, movement, dest);
1076 // collision with other (static) objects
1077 for(MovingObjects::iterator i = moving_objects.begin();
1078 i != moving_objects.end(); ++i) {
1079 MovingObject* moving_object = *i;
1080 if(moving_object->get_group() != COLGROUP_STATIC
1081 && moving_object->get_group() != COLGROUP_MOVING_STATIC)
1083 if(!moving_object->is_valid())
1086 if(moving_object != &object)
1087 check_collisions(constraints, movement, dest, moving_object->bbox,
1088 &object, moving_object);
1093 Sector::collision_static_constrains(MovingObject& object)
1095 using namespace collision;
1096 float infinity = (std::numeric_limits<float>::has_infinity ? std::numeric_limits<float>::infinity() : std::numeric_limits<float>::max());
1098 Constraints constraints;
1099 Vector movement = object.get_movement();
1100 Rect& dest = object.dest;
1101 float owidth = object.get_bbox().get_width();
1102 float oheight = object.get_bbox().get_height();
1104 for(int i = 0; i < 2; ++i) {
1105 collision_static(&constraints, Vector(0, movement.y), dest, object);
1106 if(!constraints.has_constraints())
1109 // apply calculated horizontal constraints
1110 if(constraints.bottom < infinity) {
1111 float height = constraints.bottom - constraints.top;
1112 if(height < oheight) {
1113 // we're crushed, but ignore this for now, we'll get this again
1114 // later if we're really crushed or things will solve itself when
1115 // looking at the vertical constraints
1117 dest.p2.y = constraints.bottom - DELTA;
1118 dest.p1.y = dest.p2.y - oheight;
1119 } else if(constraints.top > -infinity) {
1120 dest.p1.y = constraints.top + DELTA;
1121 dest.p2.y = dest.p1.y + oheight;
1124 if(constraints.has_constraints()) {
1125 if(constraints.hit.bottom) {
1126 dest.move(constraints.ground_movement);
1128 if(constraints.hit.top || constraints.hit.bottom) {
1129 constraints.hit.left = false;
1130 constraints.hit.right = false;
1131 object.collision_solid(constraints.hit);
1135 constraints = Constraints();
1136 for(int i = 0; i < 2; ++i) {
1137 collision_static(&constraints, movement, dest, object);
1138 if(!constraints.has_constraints())
1141 // apply calculated vertical constraints
1142 if(constraints.right < infinity) {
1143 float width = constraints.right - constraints.left;
1144 if(width + SHIFT_DELTA < owidth) {
1145 printf("Object %p crushed horizontally... L:%f R:%f\n", &object,
1146 constraints.left, constraints.right);
1151 object.collision_solid(h);
1153 dest.p2.x = constraints.right - DELTA;
1154 dest.p1.x = dest.p2.x - owidth;
1156 } else if(constraints.left > -infinity) {
1157 dest.p1.x = constraints.left + DELTA;
1158 dest.p2.x = dest.p1.x + owidth;
1162 if(constraints.has_constraints()) {
1163 if( constraints.hit.left || constraints.hit.right
1164 || constraints.hit.top || constraints.hit.bottom
1165 || constraints.hit.crush )
1166 object.collision_solid(constraints.hit);
1169 // an extra pass to make sure we're not crushed horizontally
1170 constraints = Constraints();
1171 collision_static(&constraints, movement, dest, object);
1172 if(constraints.bottom < infinity) {
1173 float height = constraints.bottom - constraints.top;
1174 if(height + SHIFT_DELTA < oheight) {
1175 printf("Object %p crushed vertically...\n", &object);
1180 object.collision_solid(h);
1186 Sector::handle_collisions()
1188 using namespace collision;
1190 // calculate destination positions of the objects
1191 for(MovingObjects::iterator i = moving_objects.begin();
1192 i != moving_objects.end(); ++i) {
1193 MovingObject* moving_object = *i;
1195 moving_object->dest = moving_object->get_bbox();
1196 moving_object->dest.move(moving_object->get_movement());
1199 // part1: COLGROUP_MOVING vs COLGROUP_STATIC and tilemap
1200 for(MovingObjects::iterator i = moving_objects.begin();
1201 i != moving_objects.end(); ++i) {
1202 MovingObject* moving_object = *i;
1203 if((moving_object->get_group() != COLGROUP_MOVING
1204 && moving_object->get_group() != COLGROUP_MOVING_STATIC
1205 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
1206 || !moving_object->is_valid())
1209 collision_static_constrains(*moving_object);
1213 // part2: COLGROUP_MOVING vs tile attributes
1214 for(MovingObjects::iterator i = moving_objects.begin();
1215 i != moving_objects.end(); ++i) {
1216 MovingObject* moving_object = *i;
1217 if((moving_object->get_group() != COLGROUP_MOVING
1218 && moving_object->get_group() != COLGROUP_MOVING_STATIC
1219 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
1220 || !moving_object->is_valid())
1223 uint32_t tile_attributes = collision_tile_attributes(moving_object->dest);
1224 if(tile_attributes > Tile::FIRST_INTERESTING_FLAG) {
1225 moving_object->collision_tile(tile_attributes);
1229 // part2.5: COLGROUP_MOVING vs COLGROUP_TOUCHABLE
1230 for(MovingObjects::iterator i = moving_objects.begin();
1231 i != moving_objects.end(); ++i) {
1232 MovingObject* moving_object = *i;
1233 if((moving_object->get_group() != COLGROUP_MOVING
1234 && moving_object->get_group() != COLGROUP_MOVING_STATIC)
1235 || !moving_object->is_valid())
1238 for(MovingObjects::iterator i2 = moving_objects.begin();
1239 i2 != moving_objects.end(); ++i2) {
1240 MovingObject* moving_object_2 = *i2;
1241 if(moving_object_2->get_group() != COLGROUP_TOUCHABLE
1242 || !moving_object_2->is_valid())
1245 if(intersects(moving_object->dest, moving_object_2->dest)) {
1248 get_hit_normal(moving_object->dest, moving_object_2->dest,
1250 moving_object->collision(*moving_object_2, hit);
1251 moving_object_2->collision(*moving_object, hit);
1256 // part3: COLGROUP_MOVING vs COLGROUP_MOVING
1257 for(MovingObjects::iterator i = moving_objects.begin();
1258 i != moving_objects.end(); ++i) {
1259 MovingObject* moving_object = *i;
1261 if((moving_object->get_group() != COLGROUP_MOVING
1262 && moving_object->get_group() != COLGROUP_MOVING_STATIC)
1263 || !moving_object->is_valid())
1266 for(MovingObjects::iterator i2 = i+1;
1267 i2 != moving_objects.end(); ++i2) {
1268 MovingObject* moving_object_2 = *i2;
1269 if((moving_object_2->get_group() != COLGROUP_MOVING
1270 && moving_object_2->get_group() != COLGROUP_MOVING_STATIC)
1271 || !moving_object_2->is_valid())
1274 collision_object(moving_object, moving_object_2);
1278 // apply object movement
1279 for(MovingObjects::iterator i = moving_objects.begin();
1280 i != moving_objects.end(); ++i) {
1281 MovingObject* moving_object = *i;
1283 moving_object->bbox = moving_object->dest;
1284 moving_object->movement = Vector(0, 0);
1289 Sector::is_free_of_tiles(const Rect& rect) const
1291 using namespace collision;
1293 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1294 TileMap* solids = *i;
1296 // test with all tiles in this rectangle
1297 int starttilex = int(rect.p1.x - solids->get_x_offset()) / 32;
1298 int starttiley = int(rect.p1.y - solids->get_y_offset()) / 32;
1299 int max_x = int(rect.p2.x - solids->get_x_offset());
1300 int max_y = int(rect.p2.y - solids->get_y_offset());
1302 for(int x = starttilex; x*32 <= max_x; ++x) {
1303 for(int y = starttiley; y*32 <= max_y; ++y) {
1304 const Tile* tile = solids->get_tile(x, y);
1306 if(tile->getAttributes() & Tile::SLOPE) {
1307 AATriangle triangle;
1308 Vector p1(x*32 + solids->get_x_offset(), y*32 + solids->get_y_offset());
1309 Vector p2((x+1)*32 + solids->get_x_offset(), (y+1)*32 + solids->get_y_offset());
1310 triangle = AATriangle(p1, p2, tile->getData());
1311 Constraints constraints;
1312 return collision::rectangle_aatriangle(&constraints, rect, triangle);
1314 if(tile->getAttributes() & Tile::SOLID) return false;
1323 Sector::is_free_of_statics(const Rect& rect, const MovingObject* ignore_object) const
1325 using namespace collision;
1327 if (!is_free_of_tiles(rect)) return false;
1329 for(MovingObjects::const_iterator i = moving_objects.begin();
1330 i != moving_objects.end(); ++i) {
1331 const MovingObject* moving_object = *i;
1332 if (moving_object == ignore_object) continue;
1333 if (!moving_object->is_valid()) continue;
1334 if (moving_object->get_group() == COLGROUP_STATIC) {
1335 if(intersects(rect, moving_object->get_bbox())) return false;
1343 Sector::is_free_of_movingstatics(const Rect& rect, const MovingObject* ignore_object) const
1345 using namespace collision;
1347 if (!is_free_of_tiles(rect)) return false;
1349 for(MovingObjects::const_iterator i = moving_objects.begin();
1350 i != moving_objects.end(); ++i) {
1351 const MovingObject* moving_object = *i;
1352 if (moving_object == ignore_object) continue;
1353 if (!moving_object->is_valid()) continue;
1354 if ((moving_object->get_group() == COLGROUP_MOVING)
1355 || (moving_object->get_group() == COLGROUP_MOVING_STATIC)
1356 || (moving_object->get_group() == COLGROUP_STATIC)) {
1357 if(intersects(rect, moving_object->get_bbox())) return false;
1365 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
1367 // TODO remove this function and move these checks elsewhere...
1369 Bullet* new_bullet = 0;
1370 if((player_status->bonus == FIRE_BONUS &&
1371 (int)bullets.size() >= player_status->max_fire_bullets) ||
1372 (player_status->bonus == ICE_BONUS &&
1373 (int)bullets.size() >= player_status->max_ice_bullets))
1375 new_bullet = new Bullet(pos, xm, dir, player_status->bonus);
1376 add_object(new_bullet);
1378 sound_manager->play("sounds/shoot.wav");
1384 Sector::add_smoke_cloud(const Vector& pos)
1386 add_object(new SmokeCloud(pos));
1391 Sector::play_music(MusicType type)
1393 currentmusic = type;
1394 switch(currentmusic) {
1396 sound_manager->play_music(music);
1399 sound_manager->play_music("music/salcon.ogg");
1401 case HERRING_WARNING_MUSIC:
1402 sound_manager->stop_music(TUX_INVINCIBLE_TIME_WARNING);
1405 sound_manager->play_music("");
1411 Sector::get_music_type()
1413 return currentmusic;
1417 Sector::get_total_badguys()
1419 int total_badguys = 0;
1420 for(GameObjects::iterator i = gameobjects.begin();
1421 i != gameobjects.end(); ++i) {
1422 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
1423 if (badguy && badguy->countMe)
1427 return total_badguys;
1431 Sector::inside(const Rect& rect) const
1433 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1434 TileMap* solids = *i;
1436 bool horizontally = ((rect.p2.x >= 0 + solids->get_x_offset()) && (rect.p1.x <= solids->get_width() * 32 + solids->get_x_offset()));
1437 bool vertically = (rect.p1.y <= solids->get_height() * 32 + solids->get_y_offset());
1438 if (horizontally && vertically) return true;
1444 Sector::get_width() const
1447 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1448 TileMap* solids = *i;
1450 if ((solids->get_width() * 32 + solids->get_x_offset()) > width) width = (solids->get_width() * 32 + solids->get_x_offset());
1456 Sector::get_height() const
1459 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1460 TileMap* solids = *i;
1462 if ((solids->get_height() * 32 + solids->get_y_offset()) > height) height = (solids->get_height() * 32 + solids->get_y_offset());
1468 Sector::change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id)
1470 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1471 TileMap* solids = *i;
1473 solids->change_all(old_tile_id, new_tile_id);
1479 Sector::set_ambient_light(float red, float green, float blue)
1481 ambient_light.red = red;
1482 ambient_light.green = green;
1483 ambient_light.blue = blue;
1487 Sector::get_ambient_red()
1489 return ambient_light.red;
1493 Sector::get_ambient_green()
1495 return ambient_light.green;
1499 Sector::get_ambient_blue()
1501 return ambient_light.blue;