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/bullet.hpp"
59 #include "object/text_object.hpp"
60 #include "object/portable.hpp"
61 #include "badguy/jumpy.hpp"
62 #include "trigger/sequence_trigger.hpp"
63 #include "player_status.hpp"
64 #include "scripting/squirrel_util.hpp"
65 #include "script_interface.hpp"
68 Sector* Sector::_current = 0;
70 bool Sector::show_collrects = false;
71 bool Sector::draw_solids_only = false;
73 Sector::Sector(Level* parent)
74 : level(parent), currentmusic(LEVEL_MUSIC),
75 ambient_light( 1.0f, 1.0f, 1.0f, 1.0f ), gravity(10), player(0), camera(0)
77 add_object(new Player(player_status, "Tux"));
78 add_object(new DisplayEffect("Effect"));
79 add_object(new TextObject("Text"));
81 // create a new squirrel table for the sector
82 using namespace Scripting;
84 sq_collectgarbage(global_vm);
86 sq_newtable(global_vm);
87 sq_pushroottable(global_vm);
88 if(SQ_FAILED(sq_setdelegate(global_vm, -2)))
89 throw Scripting::SquirrelError(global_vm, "Couldn't set sector_table delegate");
91 sq_resetobject(§or_table);
92 if(SQ_FAILED(sq_getstackobj(global_vm, -1, §or_table)))
93 throw Scripting::SquirrelError(global_vm, "Couldn't get sector table");
94 sq_addref(global_vm, §or_table);
100 using namespace Scripting;
104 for(ScriptList::iterator i = scripts.begin();
105 i != scripts.end(); ++i) {
106 HSQOBJECT& object = *i;
107 sq_release(global_vm, &object);
109 sq_release(global_vm, §or_table);
110 sq_collectgarbage(global_vm);
112 update_game_objects();
113 assert(gameobjects_new.size() == 0);
115 for(GameObjects::iterator i = gameobjects.begin();
116 i != gameobjects.end(); ++i) {
117 GameObject* object = *i;
118 before_object_remove(object);
122 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
134 Sector::parse_object(const std::string& name, const lisp::Lisp& reader)
136 if(name == "camera") {
137 Camera* camera = new Camera(this, "Camera");
138 camera->parse(reader);
140 } else if(name == "particles-snow") {
141 SnowParticleSystem* partsys = new SnowParticleSystem();
142 partsys->parse(reader);
144 } else if(name == "particles-rain") {
145 RainParticleSystem* partsys = new RainParticleSystem();
146 partsys->parse(reader);
148 } else if(name == "particles-comets") {
149 CometParticleSystem* partsys = new CometParticleSystem();
150 partsys->parse(reader);
152 } else if(name == "particles-ghosts") {
153 GhostParticleSystem* partsys = new GhostParticleSystem();
154 partsys->parse(reader);
156 } else if(name == "particles-clouds") {
157 CloudParticleSystem* partsys = new CloudParticleSystem();
158 partsys->parse(reader);
160 } else if(name == "money") { // for compatibility with old maps
161 return new Jumpy(reader);
165 return create_object(name, reader);
166 } catch(std::exception& e) {
167 log_warning << e.what() << "" << std::endl;
174 Sector::parse(const lisp::Lisp& sector)
176 lisp::ListIterator iter(§or);
178 const std::string& token = iter.item();
179 if(token == "name") {
180 iter.value()->get(name);
181 } else if(token == "gravity") {
182 iter.value()->get(gravity);
183 } else if(token == "music") {
184 iter.value()->get(music);
185 } else if(token == "spawnpoint") {
186 SpawnPoint* sp = new SpawnPoint(iter.lisp());
187 spawnpoints.push_back(sp);
188 } else if(token == "init-script") {
189 iter.value()->get(init_script);
190 } else if(token == "ambient-light") {
191 std::vector<float> vColor;
192 sector.get_vector( "ambient-light", vColor );
193 if(vColor.size() < 3) {
194 log_warning << "(ambient-light) requires a color as argument" << std::endl;
196 ambient_light = Color( vColor );
199 GameObject* object = parse_object(token, *(iter.lisp()));
206 update_game_objects();
208 if(solid_tilemaps.size() < 1) log_warning << "sector '" << name << "' does not contain a solid tile layer." << std::endl;
212 log_warning << "sector '" << name << "' does not contain a camera." << std::endl;
213 update_game_objects();
214 add_object(new Camera(this, "Camera"));
217 update_game_objects();
221 Sector::parse_old_format(const lisp::Lisp& reader)
224 reader.get("gravity", gravity);
226 std::string backgroundimage;
227 reader.get("background", backgroundimage);
229 reader.get("bkgd_speed", bgspeed);
232 Color bkgd_top, bkgd_bottom;
233 int r = 0, g = 0, b = 128;
234 reader.get("bkgd_red_top", r);
235 reader.get("bkgd_green_top", g);
236 reader.get("bkgd_blue_top", b);
237 bkgd_top.red = static_cast<float> (r) / 255.0f;
238 bkgd_top.green = static_cast<float> (g) / 255.0f;
239 bkgd_top.blue = static_cast<float> (b) / 255.0f;
241 reader.get("bkgd_red_bottom", r);
242 reader.get("bkgd_green_bottom", g);
243 reader.get("bkgd_blue_bottom", b);
244 bkgd_bottom.red = static_cast<float> (r) / 255.0f;
245 bkgd_bottom.green = static_cast<float> (g) / 255.0f;
246 bkgd_bottom.blue = static_cast<float> (b) / 255.0f;
248 if(backgroundimage != "") {
249 Background* background = new Background();
250 background->set_image(
251 std::string("images/background/") + backgroundimage, bgspeed);
252 add_object(background);
254 Gradient* gradient = new Gradient();
255 gradient->set_gradient(bkgd_top, bkgd_bottom);
256 add_object(gradient);
259 std::string particlesystem;
260 reader.get("particle_system", particlesystem);
261 if(particlesystem == "clouds")
262 add_object(new CloudParticleSystem());
263 else if(particlesystem == "snow")
264 add_object(new SnowParticleSystem());
265 else if(particlesystem == "rain")
266 add_object(new RainParticleSystem());
268 Vector startpos(100, 170);
269 reader.get("start_pos_x", startpos.x);
270 reader.get("start_pos_y", startpos.y);
272 SpawnPoint* spawn = new SpawnPoint;
273 spawn->pos = startpos;
274 spawn->name = "main";
275 spawnpoints.push_back(spawn);
277 music = "chipdisko.ogg";
278 reader.get("music", music);
279 music = "music/" + music;
281 int width = 30, height = 15;
282 reader.get("width", width);
283 reader.get("height", height);
285 std::vector<unsigned int> tiles;
286 if(reader.get_vector("interactive-tm", tiles)
287 || reader.get_vector("tilemap", tiles)) {
288 TileMap* tilemap = new TileMap();
289 tilemap->set(width, height, tiles, LAYER_TILES, true);
293 if(reader.get_vector("background-tm", tiles)) {
294 TileMap* tilemap = new TileMap();
295 tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
299 if(reader.get_vector("foreground-tm", tiles)) {
300 TileMap* tilemap = new TileMap();
301 tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
305 // read reset-points (now spawn-points)
306 const lisp::Lisp* resetpoints = reader.get_lisp("reset-points");
308 lisp::ListIterator iter(resetpoints);
310 if(iter.item() == "point") {
312 if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
314 SpawnPoint* sp = new SpawnPoint;
317 spawnpoints.push_back(sp);
320 log_warning << "Unknown token '" << iter.item() << "' in reset-points." << std::endl;
326 const lisp::Lisp* objects = reader.get_lisp("objects");
328 lisp::ListIterator iter(objects);
330 GameObject* object = parse_object(iter.item(), *(iter.lisp()));
334 log_warning << "Unknown object '" << iter.item() << "' in level." << std::endl;
340 Camera* camera = new Camera(this, "Camera");
343 update_game_objects();
345 if(solid_tilemaps.size() < 1) log_warning << "sector '" << name << "' does not contain a solid tile layer." << std::endl;
348 update_game_objects();
352 Sector::fix_old_tiles()
354 for(std::list<TileMap*>::iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
355 TileMap* solids = *i;
356 for(size_t x=0; x < solids->get_width(); ++x) {
357 for(size_t y=0; y < solids->get_height(); ++y) {
358 const Tile* tile = solids->get_tile(x, y);
359 Vector pos(solids->get_x_offset() + x*32, solids->get_y_offset() + y*32);
361 if(tile->getID() == 112) {
362 add_object(new InvisibleBlock(pos));
363 solids->change(x, y, 0);
364 } else if(tile->getAttributes() & Tile::COIN) {
365 add_object(new Coin(pos));
366 solids->change(x, y, 0);
367 } else if(tile->getAttributes() & Tile::FULLBOX) {
368 add_object(new BonusBlock(pos, tile->getData()));
369 solids->change(x, y, 0);
370 } else if(tile->getAttributes() & Tile::BRICK) {
371 add_object(new Brick(pos, tile->getData()));
372 solids->change(x, y, 0);
373 } else if(tile->getAttributes() & Tile::GOAL) {
374 std::string sequence = tile->getData() == 0 ? "endsequence" : "stoptux";
375 add_object(new SequenceTrigger(pos, sequence));
376 solids->change(x, y, 0);
384 Sector::write(lisp::Writer& writer)
386 writer.write_string("name", name);
387 writer.write_float("gravity", gravity);
388 writer.write_string("music", music);
391 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
393 SpawnPoint* spawn = *i;
394 writer.start_list("spawn-points");
395 writer.write_string("name", spawn->name);
396 writer.write_float("x", spawn->pos.x);
397 writer.write_float("y", spawn->pos.y);
398 writer.end_list("spawn-points");
402 for(GameObjects::iterator i = gameobjects.begin();
403 i != gameobjects.end(); ++i) {
404 Serializable* serializable = dynamic_cast<Serializable*> (*i);
406 serializable->write(writer);
411 Sector::run_script(std::istream& in, const std::string& sourcename)
413 using namespace Scripting;
415 // garbage collect thread list
416 for(ScriptList::iterator i = scripts.begin();
417 i != scripts.end(); ) {
418 HSQOBJECT& object = *i;
419 HSQUIRRELVM vm = object_to_vm(object);
421 if(sq_getvmstate(vm) != SQ_VMSTATE_SUSPENDED) {
422 sq_release(global_vm, &object);
423 i = scripts.erase(i);
430 HSQOBJECT object = create_thread(global_vm);
431 scripts.push_back(object);
433 HSQUIRRELVM vm = object_to_vm(object);
435 // set sector_table as roottable for the thread
436 sq_pushobject(vm, sector_table);
439 compile_and_run(vm, in, sourcename);
445 Sector::add_object(GameObject* object)
447 // make sure the object isn't already in the list
449 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
452 assert("object already added to sector" == 0);
455 for(GameObjects::iterator i = gameobjects_new.begin();
456 i != gameobjects_new.end(); ++i) {
458 assert("object already added to sector" == 0);
464 gameobjects_new.push_back(object);
468 Sector::activate(const std::string& spawnpoint)
471 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
473 if((*i)->name == spawnpoint) {
479 log_warning << "Spawnpoint '" << spawnpoint << "' not found." << std::endl;
480 if(spawnpoint != "main") {
483 activate(Vector(0, 0));
491 Sector::activate(const Vector& player_pos)
493 if(_current != this) {
495 _current->deactivate();
498 // register sectortable as sector in scripting
499 HSQUIRRELVM vm = Scripting::global_vm;
500 sq_pushroottable(vm);
501 sq_pushstring(vm, "sector", -1);
502 sq_pushobject(vm, sector_table);
503 if(SQ_FAILED(sq_createslot(vm, -3)))
504 throw Scripting::SquirrelError(vm, "Couldn't set sector in roottable");
507 for(GameObjects::iterator i = gameobjects.begin();
508 i != gameobjects.end(); ++i) {
509 GameObject* object = *i;
516 // spawn smalltux below spawnpoint
517 if (!player->is_big()) {
518 player->move(player_pos + Vector(0,32));
520 player->move(player_pos);
523 // spawning tux in the ground would kill him
524 if(!is_free_of_tiles(player->get_bbox())) {
525 log_warning << "Tried spawning Tux in solid matter. Compensating." << std::endl;
526 Vector npos = player->get_bbox().p1;
531 camera->reset(player->get_pos());
532 update_game_objects();
535 if(init_script != "") {
536 std::istringstream in(init_script);
537 run_script(in, std::string("Sector(") + name + ") - init");
547 // remove sector entry from global vm
548 HSQUIRRELVM vm = Scripting::global_vm;
549 sq_pushroottable(vm);
550 sq_pushstring(vm, "sector", -1);
551 if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
552 throw Scripting::SquirrelError(vm, "Couldn't unset sector in roottable");
555 for(GameObjects::iterator i = gameobjects.begin();
556 i != gameobjects.end(); ++i) {
557 GameObject* object = *i;
559 try_unexpose(object);
567 Sector::get_active_region()
570 camera->get_translation() - Vector(1600, 1200),
571 camera->get_translation() + Vector(1600, 1200));
575 Sector::update(float elapsed_time)
577 player->check_bounds(camera);
580 for(GameObjects::iterator i = gameobjects.begin();
581 i != gameobjects.end(); ++i) {
582 GameObject* object = *i;
583 if(!object->is_valid())
586 object->update(elapsed_time);
589 /* Handle all possible collisions. */
591 update_game_objects();
595 Sector::update_game_objects()
597 /** cleanup marked objects */
598 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
599 i != gameobjects.end(); /* nothing */) {
600 GameObject* object = *i;
602 if(object->is_valid()) {
607 before_object_remove(object);
610 i = gameobjects.erase(i);
613 /* add newly created objects */
614 for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
615 i != gameobjects_new.end(); ++i)
617 GameObject* object = *i;
619 before_object_add(object);
621 gameobjects.push_back(object);
623 gameobjects_new.clear();
627 Sector::before_object_add(GameObject* object)
629 Bullet* bullet = dynamic_cast<Bullet*> (object);
631 bullets.push_back(bullet);
634 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
635 if(movingobject != NULL) {
636 moving_objects.push_back(movingobject);
639 Portable* portable = dynamic_cast<Portable*> (object);
640 if(portable != NULL) {
641 portables.push_back(portable);
644 TileMap* tilemap = dynamic_cast<TileMap*> (object);
645 if(tilemap != NULL && tilemap->is_solid()) {
646 solid_tilemaps.push_back(tilemap);
649 Camera* camera = dynamic_cast<Camera*> (object);
651 if(this->camera != 0) {
652 log_warning << "Multiple cameras added. Ignoring" << std::endl;
655 this->camera = camera;
658 Player* player = dynamic_cast<Player*> (object);
660 if(this->player != 0) {
661 log_warning << "Multiple players added. Ignoring" << std::endl;
664 this->player = player;
667 if(_current == this) {
675 Sector::try_expose(GameObject* object)
677 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
678 if(interface != NULL) {
679 HSQUIRRELVM vm = Scripting::global_vm;
680 sq_pushobject(vm, sector_table);
681 interface->expose(vm, -1);
687 Sector::try_expose_me()
689 HSQUIRRELVM vm = Scripting::global_vm;
690 sq_pushobject(vm, sector_table);
691 Scripting::SSector* interface = static_cast<Scripting::SSector*> (this);
692 expose_object(vm, -1, interface, "settings", false);
697 Sector::before_object_remove(GameObject* object)
699 Portable* portable = dynamic_cast<Portable*> (object);
700 if(portable != NULL) {
701 portables.erase(std::find(portables.begin(), portables.end(), portable));
703 Bullet* bullet = dynamic_cast<Bullet*> (object);
705 bullets.erase(std::find(bullets.begin(), bullets.end(), bullet));
707 MovingObject* moving_object = dynamic_cast<MovingObject*> (object);
708 if(moving_object != NULL) {
709 moving_objects.erase(
710 std::find(moving_objects.begin(), moving_objects.end(), moving_object));
714 try_unexpose(object);
718 Sector::try_unexpose(GameObject* object)
720 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
721 if(interface != NULL) {
722 HSQUIRRELVM vm = Scripting::global_vm;
723 SQInteger oldtop = sq_gettop(vm);
724 sq_pushobject(vm, sector_table);
726 interface->unexpose(vm, -1);
727 } catch(std::exception& e) {
728 log_warning << "Couldn't unregister object: " << e.what() << std::endl;
730 sq_settop(vm, oldtop);
735 Sector::try_unexpose_me()
737 HSQUIRRELVM vm = Scripting::global_vm;
738 SQInteger oldtop = sq_gettop(vm);
739 sq_pushobject(vm, sector_table);
741 Scripting::unexpose_object(vm, -1, "settings");
742 } catch(std::exception& e) {
743 log_warning << "Couldn't unregister object: " << e.what() << std::endl;
745 sq_settop(vm, oldtop);
748 Sector::draw(DrawingContext& context)
750 context.set_ambient_color( ambient_light );
751 context.push_transform();
752 context.set_translation(camera->get_translation());
754 for(GameObjects::iterator i = gameobjects.begin();
755 i != gameobjects.end(); ++i) {
756 GameObject* object = *i;
757 if(!object->is_valid())
760 if (draw_solids_only)
762 TileMap* tm = dynamic_cast<TileMap*>(object);
763 if (tm && !tm->is_solid())
767 object->draw(context);
771 Color col(0.2, 0.2, 0.2, 0.7);
772 for(MovingObjects::iterator i = moving_objects.begin();
773 i != moving_objects.end(); ++i) {
774 MovingObject* object = *i;
775 const Rect& rect = object->get_bbox();
777 context.draw_filled_rect(rect, col, LAYER_FOREGROUND1 + 10);
781 context.pop_transform();
784 /*-------------------------------------------------------------------------
785 * Collision Detection
786 *-------------------------------------------------------------------------*/
788 static const float SHIFT_DELTA = 7.0f;
790 /** r1 is supposed to be moving, r2 a solid object */
791 void check_collisions(collision::Constraints* constraints,
792 const Vector& movement, const Rect& r1, const Rect& r2,
793 GameObject* object = NULL, MovingObject* other = NULL)
795 if(!collision::intersects(r1, r2))
798 // calculate intersection
799 float itop = r1.get_bottom() - r2.get_top();
800 float ibottom = r2.get_bottom() - r1.get_top();
801 float ileft = r1.get_right() - r2.get_left();
802 float iright = r2.get_right() - r1.get_left();
804 if(fabsf(movement.y) > fabsf(movement.x)) {
805 if(ileft < SHIFT_DELTA) {
806 constraints->right = std::min(constraints->right, r2.get_left());
808 } else if(iright < SHIFT_DELTA) {
809 constraints->left = std::max(constraints->left, r2.get_right());
813 // shiftout bottom/top
814 if(itop < SHIFT_DELTA) {
815 constraints->bottom = std::min(constraints->bottom, r2.get_top());
817 } else if(ibottom < SHIFT_DELTA) {
818 constraints->top = std::max(constraints->top, r2.get_bottom());
825 HitResponse response = other->collision(*object, dummy);
826 if(response == PASSTHROUGH)
828 if(other->get_movement() != Vector(0, 0)) {
829 // TODO what todo when we collide with 2 moving objects?!?
830 constraints->ground_movement = other->get_movement();
834 float vert_penetration = std::min(itop, ibottom);
835 float horiz_penetration = std::min(ileft, iright);
836 if(vert_penetration < horiz_penetration) {
838 constraints->bottom = std::min(constraints->bottom, r2.get_top());
839 constraints->hit.bottom = true;
841 constraints->top = std::max(constraints->top, r2.get_bottom());
842 constraints->hit.top = true;
846 constraints->right = std::min(constraints->right, r2.get_left());
847 constraints->hit.right = true;
849 constraints->left = std::max(constraints->left, r2.get_right());
850 constraints->hit.left = true;
855 static const float DELTA = .001;
858 Sector::collision_tilemap(collision::Constraints* constraints,
859 const Vector& movement, const Rect& dest) const
861 // calculate rectangle where the object will move
862 float x1 = dest.get_left();
863 float x2 = dest.get_right();
864 float y1 = dest.get_top();
865 float y2 = dest.get_bottom();
867 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
868 TileMap* solids = *i;
870 // test with all tiles in this rectangle
871 int starttilex = int(x1 - solids->get_x_offset()) / 32;
872 int starttiley = int(y1 - solids->get_y_offset()) / 32;
873 int max_x = int(x2 - solids->get_x_offset());
874 int max_y = int(y2+1 - solids->get_y_offset());
876 for(int x = starttilex; x*32 < max_x; ++x) {
877 for(int y = starttiley; y*32 < max_y; ++y) {
878 const Tile* tile = solids->get_tile(x, y);
881 // skip non-solid tiles
882 if((tile->getAttributes() & Tile::SOLID) == 0)
884 // only handle unisolid when the player is falling down and when he was
885 // above the tile before
886 if(tile->getAttributes() & Tile::UNISOLID) {
887 if(movement.y <= 0 || dest.get_bottom() - movement.y - SHIFT_DELTA > y*32)
891 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
893 Vector p1(x*32 + solids->get_x_offset(), y*32 + solids->get_y_offset());
894 Vector p2((x+1)*32 + solids->get_x_offset(), (y+1)*32 + solids->get_y_offset());
895 triangle = AATriangle(p1, p2, tile->getData());
897 collision::rectangle_aatriangle(constraints, dest, triangle);
898 } else { // normal rectangular tile
899 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());
900 check_collisions(constraints, movement, dest, rect);
908 Sector::collision_tile_attributes(const Rect& dest) const
910 float x1 = dest.p1.x;
911 float y1 = dest.p1.y;
912 float x2 = dest.p2.x;
913 float y2 = dest.p2.y;
916 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
917 TileMap* solids = *i;
919 // test with all tiles in this rectangle
920 int starttilex = int(x1 - solids->get_x_offset()) / 32;
921 int starttiley = int(y1 - solids->get_y_offset()) / 32;
922 int max_x = int(x2 - solids->get_x_offset());
923 int max_y = int(y2+1 - solids->get_y_offset());
925 for(int x = starttilex; x*32 < max_x; ++x) {
926 for(int y = starttiley; y*32 < max_y; ++y) {
927 const Tile* tile = solids->get_tile(x, y);
930 result |= tile->getAttributes();
938 /** fills in CollisionHit and Normal vector of 2 intersecting rectangle */
939 static void get_hit_normal(const Rect& r1, const Rect& r2, CollisionHit& hit,
942 float itop = r1.get_bottom() - r2.get_top();
943 float ibottom = r2.get_bottom() - r1.get_top();
944 float ileft = r1.get_right() - r2.get_left();
945 float iright = r2.get_right() - r1.get_left();
947 float vert_penetration = std::min(itop, ibottom);
948 float horiz_penetration = std::min(ileft, iright);
949 if(vert_penetration < horiz_penetration) {
952 normal.y = vert_penetration;
955 normal.y = -vert_penetration;
960 normal.x = horiz_penetration;
963 normal.x = -horiz_penetration;
969 Sector::collision_object(MovingObject* object1, MovingObject* object2) const
971 using namespace collision;
973 const Rect& r1 = object1->dest;
974 const Rect& r2 = object2->dest;
977 if(intersects(object1->dest, object2->dest)) {
979 get_hit_normal(r1, r2, hit, normal);
981 HitResponse response1 = object1->collision(*object2, hit);
982 std::swap(hit.left, hit.right);
983 std::swap(hit.top, hit.bottom);
984 HitResponse response2 = object2->collision(*object1, hit);
985 assert( response1 != SOLID && response1 != PASSTHROUGH );
986 assert( response2 != SOLID && response2 != PASSTHROUGH );
987 if(response1 == CONTINUE && response2 == CONTINUE) {
988 normal *= (0.5 + DELTA);
989 object1->dest.move(-normal);
990 object2->dest.move(normal);
991 } else if (response1 == CONTINUE && response2 == FORCE_MOVE) {
992 normal *= (1 + DELTA);
993 object1->dest.move(-normal);
994 } else if (response1 == FORCE_MOVE && response2 == CONTINUE) {
995 normal *= (1 + DELTA);
996 object2->dest.move(normal);
1002 Sector::collision_static(collision::Constraints* constraints,
1003 const Vector& movement, const Rect& dest,
1006 collision_tilemap(constraints, movement, dest);
1008 // collision with other (static) objects
1009 for(MovingObjects::iterator i = moving_objects.begin();
1010 i != moving_objects.end(); ++i) {
1011 MovingObject* moving_object = *i;
1012 if(moving_object->get_group() != COLGROUP_STATIC
1013 && moving_object->get_group() != COLGROUP_MOVING_STATIC)
1015 if(!moving_object->is_valid())
1018 if(moving_object != &object)
1019 check_collisions(constraints, movement, dest, moving_object->bbox,
1020 &object, moving_object);
1025 Sector::collision_static_constrains(MovingObject& object)
1027 using namespace collision;
1028 float infinity = (std::numeric_limits<float>::has_infinity ? std::numeric_limits<float>::infinity() : std::numeric_limits<float>::max());
1030 Constraints constraints;
1031 Vector movement = object.get_movement();
1032 Rect& dest = object.dest;
1033 float owidth = object.get_bbox().get_width();
1034 float oheight = object.get_bbox().get_height();
1036 for(int i = 0; i < 2; ++i) {
1037 collision_static(&constraints, Vector(0, movement.y), dest, object);
1038 if(!constraints.has_constraints())
1041 // apply calculated horizontal constraints
1042 if(constraints.bottom < infinity) {
1043 float height = constraints.bottom - constraints.top;
1044 if(height < oheight) {
1045 // we're crushed, but ignore this for now, we'll get this again
1046 // later if we're really crushed or things will solve itself when
1047 // looking at the vertical constraints
1049 dest.p2.y = constraints.bottom - DELTA;
1050 dest.p1.y = dest.p2.y - oheight;
1051 } else if(constraints.top > -infinity) {
1052 dest.p1.y = constraints.top + DELTA;
1053 dest.p2.y = dest.p1.y + oheight;
1056 if(constraints.has_constraints()) {
1057 if(constraints.hit.bottom) {
1058 dest.move(constraints.ground_movement);
1060 if(constraints.hit.top || constraints.hit.bottom) {
1061 constraints.hit.left = false;
1062 constraints.hit.right = false;
1063 object.collision_solid(constraints.hit);
1067 constraints = Constraints();
1068 for(int i = 0; i < 2; ++i) {
1069 collision_static(&constraints, movement, dest, object);
1070 if(!constraints.has_constraints())
1073 // apply calculated vertical constraints
1074 if(constraints.right < infinity) {
1075 float width = constraints.right - constraints.left;
1076 if(width + SHIFT_DELTA < owidth) {
1077 printf("Object %p crushed horizontally... L:%f R:%f\n", &object,
1078 constraints.left, constraints.right);
1083 object.collision_solid(h);
1085 dest.p2.x = constraints.right - DELTA;
1086 dest.p1.x = dest.p2.x - owidth;
1088 } else if(constraints.left > -infinity) {
1089 dest.p1.x = constraints.left + DELTA;
1090 dest.p2.x = dest.p1.x + owidth;
1094 if(constraints.has_constraints()) {
1095 if( constraints.hit.left || constraints.hit.right
1096 || constraints.hit.top || constraints.hit.bottom
1097 || constraints.hit.crush )
1098 object.collision_solid(constraints.hit);
1101 // an extra pass to make sure we're not crushed horizontally
1102 constraints = Constraints();
1103 collision_static(&constraints, movement, dest, object);
1104 if(constraints.bottom < infinity) {
1105 float height = constraints.bottom - constraints.top;
1106 if(height + SHIFT_DELTA < oheight) {
1107 printf("Object %p crushed vertically...\n", &object);
1112 object.collision_solid(h);
1118 Sector::handle_collisions()
1120 using namespace collision;
1122 // calculate destination positions of the objects
1123 for(MovingObjects::iterator i = moving_objects.begin();
1124 i != moving_objects.end(); ++i) {
1125 MovingObject* moving_object = *i;
1127 moving_object->dest = moving_object->get_bbox();
1128 moving_object->dest.move(moving_object->get_movement());
1131 // part1: COLGROUP_MOVING vs COLGROUP_STATIC and tilemap
1132 for(MovingObjects::iterator i = moving_objects.begin();
1133 i != moving_objects.end(); ++i) {
1134 MovingObject* moving_object = *i;
1135 if((moving_object->get_group() != COLGROUP_MOVING
1136 && moving_object->get_group() != COLGROUP_MOVING_STATIC
1137 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
1138 || !moving_object->is_valid())
1141 collision_static_constrains(*moving_object);
1145 // part2: COLGROUP_MOVING vs tile attributes
1146 for(MovingObjects::iterator i = moving_objects.begin();
1147 i != moving_objects.end(); ++i) {
1148 MovingObject* moving_object = *i;
1149 if((moving_object->get_group() != COLGROUP_MOVING
1150 && moving_object->get_group() != COLGROUP_MOVING_STATIC
1151 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
1152 || !moving_object->is_valid())
1155 uint32_t tile_attributes = collision_tile_attributes(moving_object->dest);
1156 if(tile_attributes > Tile::FIRST_INTERESTING_FLAG) {
1157 moving_object->collision_tile(tile_attributes);
1161 // part2.5: COLGROUP_MOVING vs COLGROUP_TOUCHABLE
1162 for(MovingObjects::iterator i = moving_objects.begin();
1163 i != moving_objects.end(); ++i) {
1164 MovingObject* moving_object = *i;
1165 if((moving_object->get_group() != COLGROUP_MOVING
1166 && moving_object->get_group() != COLGROUP_MOVING_STATIC)
1167 || !moving_object->is_valid())
1170 for(MovingObjects::iterator i2 = moving_objects.begin();
1171 i2 != moving_objects.end(); ++i2) {
1172 MovingObject* moving_object_2 = *i2;
1173 if(moving_object_2->get_group() != COLGROUP_TOUCHABLE
1174 || !moving_object_2->is_valid())
1177 if(intersects(moving_object->dest, moving_object_2->dest)) {
1180 get_hit_normal(moving_object->dest, moving_object_2->dest,
1182 moving_object->collision(*moving_object_2, hit);
1183 moving_object_2->collision(*moving_object, hit);
1188 // part3: COLGROUP_MOVING vs COLGROUP_MOVING
1189 for(MovingObjects::iterator i = moving_objects.begin();
1190 i != moving_objects.end(); ++i) {
1191 MovingObject* moving_object = *i;
1193 if((moving_object->get_group() != COLGROUP_MOVING
1194 && moving_object->get_group() != COLGROUP_MOVING_STATIC)
1195 || !moving_object->is_valid())
1198 for(MovingObjects::iterator i2 = i+1;
1199 i2 != moving_objects.end(); ++i2) {
1200 MovingObject* moving_object_2 = *i2;
1201 if((moving_object_2->get_group() != COLGROUP_MOVING
1202 && moving_object_2->get_group() != COLGROUP_MOVING_STATIC)
1203 || !moving_object_2->is_valid())
1206 collision_object(moving_object, moving_object_2);
1210 // apply object movement
1211 for(MovingObjects::iterator i = moving_objects.begin();
1212 i != moving_objects.end(); ++i) {
1213 MovingObject* moving_object = *i;
1215 moving_object->bbox = moving_object->dest;
1216 moving_object->movement = Vector(0, 0);
1221 Sector::is_free_of_tiles(const Rect& rect) const
1223 using namespace collision;
1225 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1226 TileMap* solids = *i;
1228 // test with all tiles in this rectangle
1229 int starttilex = int(rect.p1.x - solids->get_x_offset()) / 32;
1230 int starttiley = int(rect.p1.y - solids->get_y_offset()) / 32;
1231 int max_x = int(rect.p2.x - solids->get_x_offset());
1232 int max_y = int(rect.p2.y - solids->get_y_offset());
1234 for(int x = starttilex; x*32 <= max_x; ++x) {
1235 for(int y = starttiley; y*32 <= max_y; ++y) {
1236 const Tile* tile = solids->get_tile(x, y);
1238 if(tile->getAttributes() & Tile::SLOPE) {
1239 AATriangle triangle;
1240 Vector p1(x*32 + solids->get_x_offset(), y*32 + solids->get_y_offset());
1241 Vector p2((x+1)*32 + solids->get_x_offset(), (y+1)*32 + solids->get_y_offset());
1242 triangle = AATriangle(p1, p2, tile->getData());
1243 Constraints constraints;
1244 return collision::rectangle_aatriangle(&constraints, rect, triangle);
1246 if(tile->getAttributes() & Tile::SOLID) return false;
1255 Sector::is_free_of_statics(const Rect& rect, const MovingObject* ignore_object) const
1257 using namespace collision;
1259 if (!is_free_of_tiles(rect)) return false;
1261 for(MovingObjects::const_iterator i = moving_objects.begin();
1262 i != moving_objects.end(); ++i) {
1263 const MovingObject* moving_object = *i;
1264 if (moving_object == ignore_object) continue;
1265 if (!moving_object->is_valid()) continue;
1266 if (moving_object->get_group() == COLGROUP_STATIC) {
1267 if(intersects(rect, moving_object->get_bbox())) return false;
1275 Sector::is_free_of_movingstatics(const Rect& rect, const MovingObject* ignore_object) const
1277 using namespace collision;
1279 if (!is_free_of_tiles(rect)) return false;
1281 for(MovingObjects::const_iterator i = moving_objects.begin();
1282 i != moving_objects.end(); ++i) {
1283 const MovingObject* moving_object = *i;
1284 if (moving_object == ignore_object) continue;
1285 if (!moving_object->is_valid()) continue;
1286 if ((moving_object->get_group() == COLGROUP_MOVING)
1287 || (moving_object->get_group() == COLGROUP_MOVING_STATIC)
1288 || (moving_object->get_group() == COLGROUP_STATIC)) {
1289 if(intersects(rect, moving_object->get_bbox())) return false;
1297 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
1299 // TODO remove this function and move these checks elsewhere...
1301 Bullet* new_bullet = 0;
1302 if((player_status->bonus == FIRE_BONUS &&
1303 (int)bullets.size() >= player_status->max_fire_bullets) ||
1304 (player_status->bonus == ICE_BONUS &&
1305 (int)bullets.size() >= player_status->max_ice_bullets))
1307 new_bullet = new Bullet(pos, xm, dir, player_status->bonus);
1308 add_object(new_bullet);
1310 sound_manager->play("sounds/shoot.wav");
1316 Sector::add_smoke_cloud(const Vector& pos)
1318 add_object(new SmokeCloud(pos));
1323 Sector::play_music(MusicType type)
1325 currentmusic = type;
1326 switch(currentmusic) {
1328 sound_manager->play_music(music);
1331 sound_manager->play_music("music/salcon.ogg");
1333 case HERRING_WARNING_MUSIC:
1334 sound_manager->stop_music(TUX_INVINCIBLE_TIME_WARNING);
1337 sound_manager->play_music("");
1343 Sector::get_music_type()
1345 return currentmusic;
1349 Sector::get_total_badguys()
1351 int total_badguys = 0;
1352 for(GameObjects::iterator i = gameobjects.begin();
1353 i != gameobjects.end(); ++i) {
1354 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
1355 if (badguy && badguy->countMe)
1359 return total_badguys;
1363 Sector::inside(const Rect& rect) const
1365 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1366 TileMap* solids = *i;
1367 bool horizontally = ((rect.p2.x >= 0 + solids->get_x_offset()) && (rect.p1.x <= solids->get_width() * 32 + solids->get_x_offset()));
1368 bool vertically = (rect.p1.y <= solids->get_height() * 32 + solids->get_y_offset());
1369 if (horizontally && vertically) return true;
1375 Sector::get_width() const
1378 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1379 TileMap* solids = *i;
1380 if ((solids->get_width() * 32 + solids->get_x_offset()) > width) width = (solids->get_width() * 32 + solids->get_x_offset());
1386 Sector::get_height() const
1389 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1390 TileMap* solids = *i;
1391 if ((solids->get_height() * 32 + solids->get_y_offset()) > height) height = (solids->get_height() * 32 + solids->get_y_offset());
1397 Sector::change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id)
1399 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1400 TileMap* solids = *i;
1401 solids->change_all(old_tile_id, new_tile_id);
1407 Sector::set_ambient_light(float red, float green, float blue)
1409 ambient_light.red = red;
1410 ambient_light.green = green;
1411 ambient_light.blue = blue;
1415 Sector::get_ambient_red()
1417 return ambient_light.red;
1421 Sector::get_ambient_green()
1423 return ambient_light.green;
1427 Sector::get_ambient_blue()
1429 return ambient_light.blue;