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.
32 #include "object/player.hpp"
33 #include "object/gameobjs.hpp"
34 #include "object/camera.hpp"
35 #include "object/background.hpp"
36 #include "object/gradient.hpp"
37 #include "object/particlesystem.hpp"
38 #include "object/particlesystem_interactive.hpp"
39 #include "object/tilemap.hpp"
40 #include "lisp/parser.hpp"
41 #include "lisp/lisp.hpp"
42 #include "lisp/writer.hpp"
43 #include "lisp/list_iterator.hpp"
45 #include "audio/sound_manager.hpp"
46 #include "game_session.hpp"
47 #include "resources.hpp"
48 #include "statistics.hpp"
49 #include "object_factory.hpp"
50 #include "collision.hpp"
51 #include "spawn_point.hpp"
52 #include "math/rect.hpp"
53 #include "math/aatriangle.hpp"
54 #include "object/coin.hpp"
55 #include "object/block.hpp"
56 #include "object/invisible_block.hpp"
57 #include "object/bullet.hpp"
58 #include "object/text_object.hpp"
59 #include "badguy/jumpy.hpp"
60 #include "trigger/sequence_trigger.hpp"
61 #include "player_status.hpp"
62 #include "scripting/squirrel_util.hpp"
63 #include "script_interface.hpp"
66 Sector* Sector::_current = 0;
68 bool Sector::show_collrects = false;
69 bool Sector::draw_solids_only = false;
71 Sector::Sector(Level* parent)
72 : level(parent), currentmusic(LEVEL_MUSIC), gravity(10),
73 player(0), solids(0), camera(0)
75 add_object(new Player(player_status));
76 add_object(new DisplayEffect());
77 add_object(new TextObject());
79 // create a new squirrel table for the sector
80 using namespace Scripting;
82 sq_collectgarbage(global_vm);
84 sq_newtable(global_vm);
85 sq_pushroottable(global_vm);
86 if(SQ_FAILED(sq_setdelegate(global_vm, -2)))
87 throw Scripting::SquirrelError(global_vm, "Couldn't set sector_table delegate");
89 sq_resetobject(§or_table);
90 if(SQ_FAILED(sq_getstackobj(global_vm, -1, §or_table)))
91 throw Scripting::SquirrelError(global_vm, "Couldn't get sector table");
92 sq_addref(global_vm, §or_table);
98 using namespace Scripting;
102 for(ScriptList::iterator i = scripts.begin();
103 i != scripts.end(); ++i) {
104 HSQOBJECT& object = *i;
105 sq_release(global_vm, &object);
107 sq_release(global_vm, §or_table);
108 sq_collectgarbage(global_vm);
110 update_game_objects();
111 assert(gameobjects_new.size() == 0);
113 for(GameObjects::iterator i = gameobjects.begin();
114 i != gameobjects.end(); ++i) {
115 GameObject* object = *i;
116 before_object_remove(object);
120 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
132 Sector::parse_object(const std::string& name, const lisp::Lisp& reader)
134 if(name == "camera") {
135 Camera* camera = new Camera(this);
136 camera->parse(reader);
138 } else if(name == "particles-snow") {
139 SnowParticleSystem* partsys = new SnowParticleSystem();
140 partsys->parse(reader);
142 } else if(name == "particles-rain") {
143 RainParticleSystem* partsys = new RainParticleSystem();
144 partsys->parse(reader);
146 } else if(name == "particles-comets") {
147 CometParticleSystem* partsys = new CometParticleSystem();
148 partsys->parse(reader);
150 } else if(name == "particles-ghosts") {
151 GhostParticleSystem* partsys = new GhostParticleSystem();
152 partsys->parse(reader);
154 } else if(name == "particles-clouds") {
155 CloudParticleSystem* partsys = new CloudParticleSystem();
156 partsys->parse(reader);
158 } else if(name == "money") { // for compatibility with old maps
159 return new Jumpy(reader);
163 return create_object(name, reader);
164 } catch(std::exception& e) {
165 log_warning << e.what() << "" << std::endl;
172 Sector::parse(const lisp::Lisp& sector)
174 lisp::ListIterator iter(§or);
176 const std::string& token = iter.item();
177 if(token == "name") {
178 iter.value()->get(name);
179 } else if(token == "gravity") {
180 iter.value()->get(gravity);
181 } else if(token == "music") {
182 iter.value()->get(music);
183 } else if(token == "spawnpoint") {
184 SpawnPoint* sp = new SpawnPoint(iter.lisp());
185 spawnpoints.push_back(sp);
186 } else if(token == "init-script") {
187 iter.value()->get(init_script);
189 GameObject* object = parse_object(token, *(iter.lisp()));
196 update_game_objects();
199 throw std::runtime_error("sector does not contain a solid tile layer.");
203 log_warning << "sector '" << name << "' does not contain a camera." << std::endl;
204 update_game_objects();
205 add_object(new Camera(this));
208 update_game_objects();
212 Sector::parse_old_format(const lisp::Lisp& reader)
215 reader.get("gravity", gravity);
217 std::string backgroundimage;
218 reader.get("background", backgroundimage);
220 reader.get("bkgd_speed", bgspeed);
223 Color bkgd_top, bkgd_bottom;
224 int r = 0, g = 0, b = 128;
225 reader.get("bkgd_red_top", r);
226 reader.get("bkgd_green_top", g);
227 reader.get("bkgd_blue_top", b);
228 bkgd_top.red = static_cast<float> (r) / 255.0f;
229 bkgd_top.green = static_cast<float> (g) / 255.0f;
230 bkgd_top.blue = static_cast<float> (b) / 255.0f;
232 reader.get("bkgd_red_bottom", r);
233 reader.get("bkgd_green_bottom", g);
234 reader.get("bkgd_blue_bottom", b);
235 bkgd_bottom.red = static_cast<float> (r) / 255.0f;
236 bkgd_bottom.green = static_cast<float> (g) / 255.0f;
237 bkgd_bottom.blue = static_cast<float> (b) / 255.0f;
239 if(backgroundimage != "") {
240 Background* background = new Background();
241 background->set_image(
242 std::string("images/background/") + backgroundimage, bgspeed);
243 add_object(background);
245 Gradient* gradient = new Gradient();
246 gradient->set_gradient(bkgd_top, bkgd_bottom);
247 add_object(gradient);
250 std::string particlesystem;
251 reader.get("particle_system", particlesystem);
252 if(particlesystem == "clouds")
253 add_object(new CloudParticleSystem());
254 else if(particlesystem == "snow")
255 add_object(new SnowParticleSystem());
256 else if(particlesystem == "rain")
257 add_object(new RainParticleSystem());
259 Vector startpos(100, 170);
260 reader.get("start_pos_x", startpos.x);
261 reader.get("start_pos_y", startpos.y);
263 SpawnPoint* spawn = new SpawnPoint;
264 spawn->pos = startpos;
265 spawn->name = "main";
266 spawnpoints.push_back(spawn);
268 music = "chipdisko.ogg";
269 reader.get("music", music);
270 music = "music/" + music;
272 int width = 30, height = 15;
273 reader.get("width", width);
274 reader.get("height", height);
276 std::vector<unsigned int> tiles;
277 if(reader.get_vector("interactive-tm", tiles)
278 || reader.get_vector("tilemap", tiles)) {
279 TileMap* tilemap = new TileMap();
280 tilemap->set(width, height, tiles, LAYER_TILES, true);
284 if(reader.get_vector("background-tm", tiles)) {
285 TileMap* tilemap = new TileMap();
286 tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
290 if(reader.get_vector("foreground-tm", tiles)) {
291 TileMap* tilemap = new TileMap();
292 tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
296 // read reset-points (now spawn-points)
297 const lisp::Lisp* resetpoints = reader.get_lisp("reset-points");
299 lisp::ListIterator iter(resetpoints);
301 if(iter.item() == "point") {
303 if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
305 SpawnPoint* sp = new SpawnPoint;
308 spawnpoints.push_back(sp);
311 log_warning << "Unknown token '" << iter.item() << "' in reset-points." << std::endl;
317 const lisp::Lisp* objects = reader.get_lisp("objects");
319 lisp::ListIterator iter(objects);
321 GameObject* object = parse_object(iter.item(), *(iter.lisp()));
325 log_warning << "Unknown object '" << iter.item() << "' in level." << std::endl;
331 Camera* camera = new Camera(this);
334 update_game_objects();
337 throw std::runtime_error("sector does not contain a solid tile layer.");
340 update_game_objects();
344 Sector::fix_old_tiles()
347 for(size_t x=0; x < solids->get_width(); ++x) {
348 for(size_t y=0; y < solids->get_height(); ++y) {
349 const Tile* tile = solids->get_tile(x, y);
350 Vector pos(x*32, y*32);
352 if(tile->getID() == 112) {
353 add_object(new InvisibleBlock(pos));
354 solids->change(x, y, 0);
355 } else if(tile->getAttributes() & Tile::COIN) {
356 add_object(new Coin(pos));
357 solids->change(x, y, 0);
358 } else if(tile->getAttributes() & Tile::FULLBOX) {
359 add_object(new BonusBlock(pos, tile->getData()));
360 solids->change(x, y, 0);
361 } else if(tile->getAttributes() & Tile::BRICK) {
362 add_object(new Brick(pos, tile->getData()));
363 solids->change(x, y, 0);
364 } else if(tile->getAttributes() & Tile::GOAL) {
365 std::string sequence = tile->getData() == 0 ? "endsequence" : "stoptux";
366 add_object(new SequenceTrigger(pos, sequence));
367 solids->change(x, y, 0);
374 Sector::write(lisp::Writer& writer)
376 writer.write_string("name", name);
377 writer.write_float("gravity", gravity);
378 writer.write_string("music", music);
381 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
383 SpawnPoint* spawn = *i;
384 writer.start_list("spawn-points");
385 writer.write_string("name", spawn->name);
386 writer.write_float("x", spawn->pos.x);
387 writer.write_float("y", spawn->pos.y);
388 writer.end_list("spawn-points");
392 for(GameObjects::iterator i = gameobjects.begin();
393 i != gameobjects.end(); ++i) {
394 Serializable* serializable = dynamic_cast<Serializable*> (*i);
396 serializable->write(writer);
401 Sector::run_script(std::istream& in, const std::string& sourcename)
403 using namespace Scripting;
405 // garbage collect thread list
406 for(ScriptList::iterator i = scripts.begin();
407 i != scripts.end(); ) {
408 HSQOBJECT& object = *i;
409 HSQUIRRELVM vm = object_to_vm(object);
411 if(sq_getvmstate(vm) != SQ_VMSTATE_SUSPENDED) {
412 sq_release(global_vm, &object);
413 i = scripts.erase(i);
420 HSQOBJECT object = create_thread(global_vm);
421 scripts.push_back(object);
423 HSQUIRRELVM vm = object_to_vm(object);
425 // set sector_table as roottable for the thread
426 sq_pushobject(vm, sector_table);
429 compile_and_run(vm, in, sourcename);
435 Sector::add_object(GameObject* object)
437 // make sure the object isn't already in the list
439 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
442 assert("object already added to sector" == 0);
445 for(GameObjects::iterator i = gameobjects_new.begin();
446 i != gameobjects_new.end(); ++i) {
448 assert("object already added to sector" == 0);
454 gameobjects_new.push_back(object);
458 Sector::activate(const std::string& spawnpoint)
461 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
463 if((*i)->name == spawnpoint) {
469 log_warning << "Spawnpoint '" << spawnpoint << "' not found." << std::endl;
470 if(spawnpoint != "main") {
473 activate(Vector(0, 0));
481 Sector::activate(const Vector& player_pos)
483 if(_current != this) {
485 _current->deactivate();
488 // register sectortable as sector in scripting
489 HSQUIRRELVM vm = Scripting::global_vm;
490 sq_pushroottable(vm);
491 sq_pushstring(vm, "sector", -1);
492 sq_pushobject(vm, sector_table);
493 if(SQ_FAILED(sq_createslot(vm, -3)))
494 throw Scripting::SquirrelError(vm, "Couldn't set sector in roottable");
497 for(GameObjects::iterator i = gameobjects.begin();
498 i != gameobjects.end(); ++i) {
499 GameObject* object = *i;
505 player->move(player_pos);
506 camera->reset(player->get_pos());
507 update_game_objects();
510 if(init_script != "") {
511 std::istringstream in(init_script);
512 run_script(in, std::string("Sector(") + name + ") - init");
522 // remove sector entry from global vm
523 HSQUIRRELVM vm = Scripting::global_vm;
524 sq_pushroottable(vm);
525 sq_pushstring(vm, "sector", -1);
526 if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
527 throw Scripting::SquirrelError(vm, "Couldn't unset sector in roottable");
530 for(GameObjects::iterator i = gameobjects.begin();
531 i != gameobjects.end(); ++i) {
532 GameObject* object = *i;
534 try_unexpose(object);
541 Sector::get_active_region()
544 camera->get_translation() - Vector(1600, 1200),
545 camera->get_translation() + Vector(1600, 1200));
549 Sector::update(float elapsed_time)
551 player->check_bounds(camera);
554 for(GameObjects::iterator i = gameobjects.begin();
555 i != gameobjects.end(); ++i) {
556 GameObject* object = *i;
557 if(!object->is_valid())
560 object->update(elapsed_time);
563 /* Handle all possible collisions. */
565 update_game_objects();
569 Sector::update_game_objects()
571 /** cleanup marked objects */
572 for(std::vector<Bullet*>::iterator i = bullets.begin();
573 i != bullets.end(); /* nothing */) {
575 if(bullet->is_valid()) {
580 i = bullets.erase(i);
582 for(MovingObjects::iterator i = moving_objects.begin();
583 i != moving_objects.end(); /* nothing */) {
584 MovingObject* moving_object = *i;
585 if(moving_object->is_valid()) {
590 i = moving_objects.erase(i);
592 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
593 i != gameobjects.end(); /* nothing */) {
594 GameObject* object = *i;
596 if(object->is_valid()) {
601 before_object_remove(object);
604 i = gameobjects.erase(i);
607 /* add newly created objects */
608 for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
609 i != gameobjects_new.end(); ++i)
611 GameObject* object = *i;
613 before_object_add(object);
615 gameobjects.push_back(object);
617 gameobjects_new.clear();
621 Sector::before_object_add(GameObject* object)
623 Bullet* bullet = dynamic_cast<Bullet*> (object);
625 bullets.push_back(bullet);
627 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
629 moving_objects.push_back(movingobject);
632 TileMap* tilemap = dynamic_cast<TileMap*> (object);
633 if(tilemap && tilemap->is_solid()) {
637 log_warning << "Another solid tilemaps added. Ignoring" << std::endl;
641 Camera* camera = dynamic_cast<Camera*> (object);
643 if(this->camera != 0) {
644 log_warning << "Multiple cameras added. Ignoring" << std::endl;
647 this->camera = camera;
650 Player* player = dynamic_cast<Player*> (object);
652 if(this->player != 0) {
653 log_warning << "Multiple players added. Ignoring" << std::endl;
656 this->player = player;
659 if(_current == this) {
667 Sector::try_expose(GameObject* object)
669 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
670 if(interface != NULL) {
671 HSQUIRRELVM vm = Scripting::global_vm;
672 sq_pushobject(vm, sector_table);
673 interface->expose(vm, -1);
679 Sector::before_object_remove(GameObject* object)
682 try_unexpose(object);
686 Sector::try_unexpose(GameObject* object)
688 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
689 if(interface != NULL) {
690 HSQUIRRELVM vm = Scripting::global_vm;
691 SQInteger oldtop = sq_gettop(vm);
692 sq_pushobject(vm, sector_table);
694 interface->unexpose(vm, -1);
695 } catch(std::exception& e) {
696 log_warning << "Couldn't unregister object: " << e.what() << std::endl;
698 sq_settop(vm, oldtop);
703 Sector::draw(DrawingContext& context)
705 context.push_transform();
706 context.set_translation(camera->get_translation());
708 for(GameObjects::iterator i = gameobjects.begin();
709 i != gameobjects.end(); ++i) {
710 GameObject* object = *i;
711 if(!object->is_valid())
714 if (draw_solids_only)
716 TileMap* tm = dynamic_cast<TileMap*>(object);
717 if (tm && !tm->is_solid())
721 object->draw(context);
725 Color col(0.2, 0.2, 0.2, 0.7);
726 for(MovingObjects::iterator i = moving_objects.begin();
727 i != moving_objects.end(); ++i) {
728 MovingObject* object = *i;
729 const Rect& rect = object->get_bbox();
731 context.draw_filled_rect(rect, col, LAYER_FOREGROUND1 + 10);
735 context.pop_transform();
738 /*-------------------------------------------------------------------------
739 * Collision Detection
740 *-------------------------------------------------------------------------*/
742 static const float SHIFT_DELTA = 7.0f;
744 /** r1 is supposed to be moving, r2 a solid object */
745 void check_collisions(collision::Constraints* constraints,
746 const Vector& movement, const Rect& r1, const Rect& r2,
747 GameObject* object = NULL, MovingObject* other = NULL)
749 if(!collision::intersects(r1, r2))
752 // calculate intersection
753 float itop = r1.get_bottom() - r2.get_top();
754 float ibottom = r2.get_bottom() - r1.get_top();
755 float ileft = r1.get_right() - r2.get_left();
756 float iright = r2.get_right() - r1.get_left();
758 if(fabsf(movement.y) > fabsf(movement.x)) {
759 if(ileft < SHIFT_DELTA) {
760 constraints->right = std::min(constraints->right, r2.get_left());
762 } else if(iright < SHIFT_DELTA) {
763 constraints->left = std::max(constraints->left, r2.get_right());
767 // shiftout bottom/top
768 if(itop < SHIFT_DELTA) {
769 constraints->bottom = std::min(constraints->bottom, r2.get_top());
771 } else if(ibottom < SHIFT_DELTA) {
772 constraints->top = std::max(constraints->top, r2.get_bottom());
779 HitResponse response = other->collision(*object, dummy);
780 if(response == PASSTHROUGH)
782 if(other->get_movement() != Vector(0, 0)) {
783 // TODO what todo when we collide with 2 moving objects?!?
784 constraints->ground_movement = other->get_movement();
788 float vert_penetration = std::min(itop, ibottom);
789 float horiz_penetration = std::min(ileft, iright);
790 if(vert_penetration < horiz_penetration) {
792 constraints->bottom = std::min(constraints->bottom, r2.get_top());
793 constraints->hit.bottom = true;
795 constraints->top = std::max(constraints->top, r2.get_bottom());
796 constraints->hit.top = true;
800 constraints->right = std::min(constraints->right, r2.get_left());
801 constraints->hit.right = true;
803 constraints->left = std::max(constraints->left, r2.get_right());
804 constraints->hit.left = true;
809 static const float DELTA = .001;
812 Sector::collision_tilemap(collision::Constraints* constraints,
813 const Vector& movement, const Rect& dest) const
815 // calculate rectangle where the object will move
816 float x1 = dest.get_left();
817 float x2 = dest.get_right();
818 float y1 = dest.get_top();
819 float y2 = dest.get_bottom();
821 // test with all tiles in this rectangle
822 int starttilex = int(x1) / 32;
823 int starttiley = int(y1) / 32;
825 int max_y = int(y2+1);
827 for(int x = starttilex; x*32 < max_x; ++x) {
828 for(int y = starttiley; y*32 < max_y; ++y) {
829 const Tile* tile = solids->get_tile(x, y);
832 // skip non-solid tiles
833 if((tile->getAttributes() & Tile::SOLID) == 0)
835 // only handle unisolid when the player is falling down and when he was
836 // above the tile before
837 if(tile->getAttributes() & Tile::UNISOLID) {
838 if(movement.y <= 0 || dest.get_bottom() - movement.y - SHIFT_DELTA > y*32)
842 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
844 Vector p1(x*32, y*32);
845 Vector p2((x+1)*32, (y+1)*32);
846 triangle = AATriangle(p1, p2, tile->getData());
848 collision::rectangle_aatriangle(constraints, dest, triangle);
849 } else { // normal rectangular tile
850 Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
851 check_collisions(constraints, movement, dest, rect);
858 Sector::collision_tile_attributes(const Rect& dest) const
860 float x1 = dest.p1.x;
861 float y1 = dest.p1.y;
862 float x2 = dest.p2.x;
863 float y2 = dest.p2.y;
865 // test with all tiles in this rectangle
866 int starttilex = int(x1) / 32;
867 int starttiley = int(y1) / 32;
872 for(int x = starttilex; x*32 < max_x; ++x) {
873 for(int y = starttiley; y*32 < max_y; ++y) {
874 const Tile* tile = solids->get_tile(x, y);
877 result |= tile->getAttributes();
884 /** fills in CollisionHit and Normal vector of 2 intersecting rectangle */
885 static void get_hit_normal(const Rect& r1, const Rect& r2, CollisionHit& hit,
888 float itop = r1.get_bottom() - r2.get_top();
889 float ibottom = r2.get_bottom() - r1.get_top();
890 float ileft = r1.get_right() - r2.get_left();
891 float iright = r2.get_right() - r1.get_left();
893 float vert_penetration = std::min(itop, ibottom);
894 float horiz_penetration = std::min(ileft, iright);
895 if(vert_penetration < horiz_penetration) {
898 normal.y = vert_penetration;
901 normal.y = -vert_penetration;
906 normal.x = horiz_penetration;
909 normal.x = -horiz_penetration;
915 Sector::collision_object(MovingObject* object1, MovingObject* object2) const
917 using namespace collision;
919 const Rect& r1 = object1->dest;
920 const Rect& r2 = object2->dest;
923 if(intersects(object1->dest, object2->dest)) {
925 get_hit_normal(r1, r2, hit, normal);
927 HitResponse response1 = object1->collision(*object2, hit);
928 std::swap(hit.left, hit.right);
929 std::swap(hit.top, hit.bottom);
930 HitResponse response2 = object2->collision(*object1, hit);
931 assert( response1 != SOLID && response1 != PASSTHROUGH );
932 assert( response2 != SOLID && response2 != PASSTHROUGH );
933 if(response1 == CONTINUE && response2 == CONTINUE) {
934 normal *= (0.5 + DELTA);
935 object1->dest.move(-normal);
936 object2->dest.move(normal);
937 } else if (response1 == CONTINUE && response2 == FORCE_MOVE) {
938 normal *= (1 + DELTA);
939 object1->dest.move(-normal);
940 } else if (response1 == FORCE_MOVE && response2 == CONTINUE) {
941 normal *= (1 + DELTA);
942 object2->dest.move(normal);
948 Sector::collision_static(collision::Constraints* constraints,
949 const Vector& movement, const Rect& dest,
952 collision_tilemap(constraints, movement, dest);
954 // collision with other (static) objects
955 for(MovingObjects::iterator i = moving_objects.begin();
956 i != moving_objects.end(); ++i) {
957 MovingObject* moving_object = *i;
958 if(moving_object->get_group() != COLGROUP_STATIC
959 || !moving_object->is_valid())
962 check_collisions(constraints, movement, dest, moving_object->dest,
963 &object, moving_object);
968 Sector::collision_static_constrains(MovingObject& object)
970 using namespace collision;
972 Constraints constraints;
973 Vector movement = object.get_movement();
974 Rect& dest = object.dest;
975 float owidth = object.get_bbox().get_width();
976 float oheight = object.get_bbox().get_height();
978 for(int i = 0; i < 2; ++i) {
979 collision_static(&constraints, Vector(0, movement.y), dest, object);
980 if(!constraints.has_constraints())
983 // apply calculated horizontal constraints
984 if(constraints.bottom < INFINITY) {
985 float height = constraints.bottom - constraints.top;
986 if(height < oheight) {
987 // we're crushed, but ignore this for now, we'll get this again
988 // later if we're really crushed or things will solve itself when
989 // looking at the vertical constraints
991 dest.p2.y = constraints.bottom - DELTA;
992 dest.p1.y = dest.p2.y - oheight;
993 } else if(constraints.top > -INFINITY) {
994 dest.p1.y = constraints.top + DELTA;
995 dest.p2.y = dest.p1.y + oheight;
998 if(constraints.has_constraints()) {
999 if(constraints.hit.bottom) {
1000 dest.move(constraints.ground_movement);
1002 if(constraints.hit.top || constraints.hit.bottom) {
1003 constraints.hit.left = false;
1004 constraints.hit.right = false;
1005 object.collision_solid(constraints.hit);
1009 constraints = Constraints();
1010 for(int i = 0; i < 2; ++i) {
1011 collision_static(&constraints, movement, dest, object);
1012 if(!constraints.has_constraints())
1015 // apply calculated vertical constraints
1016 if(constraints.right < INFINITY) {
1017 float width = constraints.right - constraints.left;
1018 if(width + SHIFT_DELTA < owidth) {
1019 printf("Object %p crushed horizontally... L:%f R:%f\n", &object,
1020 constraints.left, constraints.right);
1025 object.collision_solid(h);
1027 dest.p2.x = constraints.right - DELTA;
1028 dest.p1.x = dest.p2.x - owidth;
1030 } else if(constraints.left > -INFINITY) {
1031 dest.p1.x = constraints.left + DELTA;
1032 dest.p2.x = dest.p1.x + owidth;
1036 if(constraints.has_constraints()) {
1037 if( constraints.hit.left || constraints.hit.right
1038 || constraints.hit.top || constraints.hit.bottom
1039 || constraints.hit.crush )
1040 object.collision_solid(constraints.hit);
1043 // an extra pass to make sure we're not crushed horizontally
1044 constraints = Constraints();
1045 collision_static(&constraints, movement, dest, object);
1046 if(constraints.bottom < INFINITY) {
1047 float height = constraints.bottom - constraints.top;
1048 if(height + SHIFT_DELTA < oheight) {
1049 printf("Object %p crushed vertically...\n", &object);
1054 object.collision_solid(h);
1060 Sector::handle_collisions()
1062 using namespace collision;
1064 // calculate destination positions of the objects
1065 for(MovingObjects::iterator i = moving_objects.begin();
1066 i != moving_objects.end(); ++i) {
1067 MovingObject* moving_object = *i;
1069 moving_object->dest = moving_object->get_bbox();
1070 moving_object->dest.move(moving_object->get_movement());
1073 // part1: COLGROUP_MOVING vs COLGROUP_STATIC and tilemap
1074 for(MovingObjects::iterator i = moving_objects.begin();
1075 i != moving_objects.end(); ++i) {
1076 MovingObject* moving_object = *i;
1077 if((moving_object->get_group() != COLGROUP_MOVING
1078 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
1079 || !moving_object->is_valid())
1082 collision_static_constrains(*moving_object);
1086 // part2: COLGROUP_MOVING vs tile attributes
1087 for(MovingObjects::iterator i = moving_objects.begin();
1088 i != moving_objects.end(); ++i) {
1089 MovingObject* moving_object = *i;
1090 if((moving_object->get_group() != COLGROUP_MOVING
1091 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
1092 || !moving_object->is_valid())
1095 uint32_t tile_attributes = collision_tile_attributes(moving_object->dest);
1096 if(tile_attributes > Tile::FIRST_INTERESTING_FLAG) {
1097 moving_object->collision_tile(tile_attributes);
1101 // part2.5: COLGROUP_MOVING vs COLGROUP_TOUCHABLE
1102 for(MovingObjects::iterator i = moving_objects.begin();
1103 i != moving_objects.end(); ++i) {
1104 MovingObject* moving_object = *i;
1105 if(moving_object->get_group() != COLGROUP_MOVING
1106 || !moving_object->is_valid())
1109 for(MovingObjects::iterator i2 = moving_objects.begin();
1110 i2 != moving_objects.end(); ++i2) {
1111 MovingObject* moving_object_2 = *i2;
1112 if(moving_object_2->get_group() != COLGROUP_TOUCHABLE
1113 || !moving_object_2->is_valid())
1116 if(intersects(moving_object->dest, moving_object_2->dest)) {
1119 get_hit_normal(moving_object->dest, moving_object_2->dest,
1121 moving_object->collision(*moving_object_2, hit);
1122 moving_object_2->collision(*moving_object, hit);
1127 // part3: COLGROUP_MOVING vs COLGROUP_MOVING
1128 for(MovingObjects::iterator i = moving_objects.begin();
1129 i != moving_objects.end(); ++i) {
1130 MovingObject* moving_object = *i;
1132 if(moving_object->get_group() != COLGROUP_MOVING
1133 || !moving_object->is_valid())
1136 for(MovingObjects::iterator i2 = i+1;
1137 i2 != moving_objects.end(); ++i2) {
1138 MovingObject* moving_object_2 = *i2;
1139 if(moving_object_2->get_group() != COLGROUP_MOVING
1140 || !moving_object_2->is_valid())
1143 collision_object(moving_object, moving_object_2);
1147 // apply object movement
1148 for(MovingObjects::iterator i = moving_objects.begin();
1149 i != moving_objects.end(); ++i) {
1150 MovingObject* moving_object = *i;
1152 moving_object->bbox = moving_object->dest;
1153 moving_object->movement = Vector(0, 0);
1158 Sector::is_free_space(const Rect& rect) const
1160 using namespace collision;
1162 // test with all tiles in this rectangle
1163 int starttilex = int(rect.p1.x) / 32;
1164 int starttiley = int(rect.p1.y) / 32;
1165 int max_x = int(rect.p2.x);
1166 int max_y = int(rect.p2.y);
1168 for(int x = starttilex; x*32 <= max_x; ++x) {
1169 for(int y = starttiley; y*32 <= max_y; ++y) {
1170 const Tile* tile = solids->get_tile(x, y);
1173 if(tile->getAttributes() & Tile::SOLID)
1178 for(MovingObjects::const_iterator i = moving_objects.begin();
1179 i != moving_objects.end(); ++i) {
1180 const MovingObject* moving_object = *i;
1181 if(moving_object->get_group() != COLGROUP_STATIC
1182 || !moving_object->is_valid())
1185 if(intersects(rect, moving_object->get_bbox()))
1193 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
1195 // TODO remove this function and move these checks elsewhere...
1197 Bullet* new_bullet = 0;
1198 if((int)bullets.size() >= player_status->max_fire_bullets)
1200 new_bullet = new Bullet(pos, xm, dir);
1201 add_object(new_bullet);
1203 sound_manager->play("sounds/shoot.wav");
1209 Sector::add_smoke_cloud(const Vector& pos)
1211 add_object(new SmokeCloud(pos));
1216 Sector::play_music(MusicType type)
1218 currentmusic = type;
1219 switch(currentmusic) {
1221 sound_manager->play_music(music);
1224 sound_manager->play_music("music/salcon.ogg");
1226 case HERRING_WARNING_MUSIC:
1227 sound_manager->stop_music(TUX_INVINCIBLE_TIME_WARNING);
1230 sound_manager->play_music("");
1236 Sector::get_music_type()
1238 return currentmusic;
1242 Sector::get_total_badguys()
1244 int total_badguys = 0;
1245 for(GameObjects::iterator i = gameobjects.begin();
1246 i != gameobjects.end(); ++i) {
1247 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
1248 if (badguy && badguy->countMe)
1252 return total_badguys;
1256 Sector::inside(const Rect& rect) const
1258 if(rect.p1.x > solids->get_width() * 32
1259 || rect.p1.y > solids->get_height() * 32