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),
73 ambient_light( 1.0f, 1.0f, 1.0f, 1.0f ), gravity(10), player(0), camera(0)
75 add_object(new Player(player_status, "Tux"));
76 add_object(new DisplayEffect("Effect"));
77 add_object(new TextObject("Text"));
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, "Camera");
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);
188 } else if(token == "ambient-light") {
189 std::vector<float> vColor;
190 sector.get_vector( "ambient-light", vColor );
191 ambient_light = Color( vColor );
193 GameObject* object = parse_object(token, *(iter.lisp()));
200 update_game_objects();
202 if(solid_tilemaps.size() < 1) log_warning << "sector '" << name << "' does not contain a solid tile layer." << std::endl;
206 log_warning << "sector '" << name << "' does not contain a camera." << std::endl;
207 update_game_objects();
208 add_object(new Camera(this, "Camera"));
211 update_game_objects();
215 Sector::parse_old_format(const lisp::Lisp& reader)
218 reader.get("gravity", gravity);
220 std::string backgroundimage;
221 reader.get("background", backgroundimage);
223 reader.get("bkgd_speed", bgspeed);
226 Color bkgd_top, bkgd_bottom;
227 int r = 0, g = 0, b = 128;
228 reader.get("bkgd_red_top", r);
229 reader.get("bkgd_green_top", g);
230 reader.get("bkgd_blue_top", b);
231 bkgd_top.red = static_cast<float> (r) / 255.0f;
232 bkgd_top.green = static_cast<float> (g) / 255.0f;
233 bkgd_top.blue = static_cast<float> (b) / 255.0f;
235 reader.get("bkgd_red_bottom", r);
236 reader.get("bkgd_green_bottom", g);
237 reader.get("bkgd_blue_bottom", b);
238 bkgd_bottom.red = static_cast<float> (r) / 255.0f;
239 bkgd_bottom.green = static_cast<float> (g) / 255.0f;
240 bkgd_bottom.blue = static_cast<float> (b) / 255.0f;
242 if(backgroundimage != "") {
243 Background* background = new Background();
244 background->set_image(
245 std::string("images/background/") + backgroundimage, bgspeed);
246 add_object(background);
248 Gradient* gradient = new Gradient();
249 gradient->set_gradient(bkgd_top, bkgd_bottom);
250 add_object(gradient);
253 std::string particlesystem;
254 reader.get("particle_system", particlesystem);
255 if(particlesystem == "clouds")
256 add_object(new CloudParticleSystem());
257 else if(particlesystem == "snow")
258 add_object(new SnowParticleSystem());
259 else if(particlesystem == "rain")
260 add_object(new RainParticleSystem());
262 Vector startpos(100, 170);
263 reader.get("start_pos_x", startpos.x);
264 reader.get("start_pos_y", startpos.y);
266 SpawnPoint* spawn = new SpawnPoint;
267 spawn->pos = startpos;
268 spawn->name = "main";
269 spawnpoints.push_back(spawn);
271 music = "chipdisko.ogg";
272 reader.get("music", music);
273 music = "music/" + music;
275 int width = 30, height = 15;
276 reader.get("width", width);
277 reader.get("height", height);
279 std::vector<unsigned int> tiles;
280 if(reader.get_vector("interactive-tm", tiles)
281 || reader.get_vector("tilemap", tiles)) {
282 TileMap* tilemap = new TileMap();
283 tilemap->set(width, height, tiles, LAYER_TILES, true);
287 if(reader.get_vector("background-tm", tiles)) {
288 TileMap* tilemap = new TileMap();
289 tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
293 if(reader.get_vector("foreground-tm", tiles)) {
294 TileMap* tilemap = new TileMap();
295 tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
299 // read reset-points (now spawn-points)
300 const lisp::Lisp* resetpoints = reader.get_lisp("reset-points");
302 lisp::ListIterator iter(resetpoints);
304 if(iter.item() == "point") {
306 if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
308 SpawnPoint* sp = new SpawnPoint;
311 spawnpoints.push_back(sp);
314 log_warning << "Unknown token '" << iter.item() << "' in reset-points." << std::endl;
320 const lisp::Lisp* objects = reader.get_lisp("objects");
322 lisp::ListIterator iter(objects);
324 GameObject* object = parse_object(iter.item(), *(iter.lisp()));
328 log_warning << "Unknown object '" << iter.item() << "' in level." << std::endl;
334 Camera* camera = new Camera(this, "Camera");
337 update_game_objects();
339 if(solid_tilemaps.size() < 1) log_warning << "sector '" << name << "' does not contain a solid tile layer." << std::endl;
342 update_game_objects();
346 Sector::fix_old_tiles()
349 for(std::list<TileMap*>::iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
350 TileMap* solids = *i;
351 for(size_t x=0; x < solids->get_width(); ++x) {
352 for(size_t y=0; y < solids->get_height(); ++y) {
353 const Tile* tile = solids->get_tile(x, y);
354 Vector pos(solids->get_x_offset() + x*32, solids->get_y_offset() + y*32);
356 if(tile->getID() == 112) {
357 add_object(new InvisibleBlock(pos));
358 solids->change(x, y, 0);
359 } else if(tile->getAttributes() & Tile::COIN) {
360 add_object(new Coin(pos));
361 solids->change(x, y, 0);
362 } else if(tile->getAttributes() & Tile::FULLBOX) {
363 add_object(new BonusBlock(pos, tile->getData()));
364 solids->change(x, y, 0);
365 } else if(tile->getAttributes() & Tile::BRICK) {
366 add_object(new Brick(pos, tile->getData()));
367 solids->change(x, y, 0);
368 } else if(tile->getAttributes() & Tile::GOAL) {
369 std::string sequence = tile->getData() == 0 ? "endsequence" : "stoptux";
370 add_object(new SequenceTrigger(pos, sequence));
371 solids->change(x, y, 0);
379 Sector::write(lisp::Writer& writer)
381 writer.write_string("name", name);
382 writer.write_float("gravity", gravity);
383 writer.write_string("music", music);
386 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
388 SpawnPoint* spawn = *i;
389 writer.start_list("spawn-points");
390 writer.write_string("name", spawn->name);
391 writer.write_float("x", spawn->pos.x);
392 writer.write_float("y", spawn->pos.y);
393 writer.end_list("spawn-points");
397 for(GameObjects::iterator i = gameobjects.begin();
398 i != gameobjects.end(); ++i) {
399 Serializable* serializable = dynamic_cast<Serializable*> (*i);
401 serializable->write(writer);
406 Sector::run_script(std::istream& in, const std::string& sourcename)
408 using namespace Scripting;
410 // garbage collect thread list
411 for(ScriptList::iterator i = scripts.begin();
412 i != scripts.end(); ) {
413 HSQOBJECT& object = *i;
414 HSQUIRRELVM vm = object_to_vm(object);
416 if(sq_getvmstate(vm) != SQ_VMSTATE_SUSPENDED) {
417 sq_release(global_vm, &object);
418 i = scripts.erase(i);
425 HSQOBJECT object = create_thread(global_vm);
426 scripts.push_back(object);
428 HSQUIRRELVM vm = object_to_vm(object);
430 // set sector_table as roottable for the thread
431 sq_pushobject(vm, sector_table);
434 compile_and_run(vm, in, sourcename);
440 Sector::add_object(GameObject* object)
442 // make sure the object isn't already in the list
444 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
447 assert("object already added to sector" == 0);
450 for(GameObjects::iterator i = gameobjects_new.begin();
451 i != gameobjects_new.end(); ++i) {
453 assert("object already added to sector" == 0);
459 gameobjects_new.push_back(object);
463 Sector::activate(const std::string& spawnpoint)
466 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
468 if((*i)->name == spawnpoint) {
474 log_warning << "Spawnpoint '" << spawnpoint << "' not found." << std::endl;
475 if(spawnpoint != "main") {
478 activate(Vector(0, 0));
486 Sector::activate(const Vector& player_pos)
488 if(_current != this) {
490 _current->deactivate();
493 // register sectortable as sector in scripting
494 HSQUIRRELVM vm = Scripting::global_vm;
495 sq_pushroottable(vm);
496 sq_pushstring(vm, "sector", -1);
497 sq_pushobject(vm, sector_table);
498 if(SQ_FAILED(sq_createslot(vm, -3)))
499 throw Scripting::SquirrelError(vm, "Couldn't set sector in roottable");
502 for(GameObjects::iterator i = gameobjects.begin();
503 i != gameobjects.end(); ++i) {
504 GameObject* object = *i;
510 player->move(player_pos);
511 camera->reset(player->get_pos());
512 update_game_objects();
515 if(init_script != "") {
516 std::istringstream in(init_script);
517 run_script(in, std::string("Sector(") + name + ") - init");
527 // remove sector entry from global vm
528 HSQUIRRELVM vm = Scripting::global_vm;
529 sq_pushroottable(vm);
530 sq_pushstring(vm, "sector", -1);
531 if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
532 throw Scripting::SquirrelError(vm, "Couldn't unset sector in roottable");
535 for(GameObjects::iterator i = gameobjects.begin();
536 i != gameobjects.end(); ++i) {
537 GameObject* object = *i;
539 try_unexpose(object);
546 Sector::get_active_region()
549 camera->get_translation() - Vector(1600, 1200),
550 camera->get_translation() + Vector(1600, 1200));
554 Sector::update(float elapsed_time)
556 player->check_bounds(camera);
559 for(GameObjects::iterator i = gameobjects.begin();
560 i != gameobjects.end(); ++i) {
561 GameObject* object = *i;
562 if(!object->is_valid())
565 object->update(elapsed_time);
568 /* Handle all possible collisions. */
570 update_game_objects();
574 Sector::update_game_objects()
576 /** cleanup marked objects */
577 for(std::vector<Bullet*>::iterator i = bullets.begin();
578 i != bullets.end(); /* nothing */) {
580 if(bullet->is_valid()) {
585 i = bullets.erase(i);
587 for(MovingObjects::iterator i = moving_objects.begin();
588 i != moving_objects.end(); /* nothing */) {
589 MovingObject* moving_object = *i;
590 if(moving_object->is_valid()) {
595 i = moving_objects.erase(i);
597 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
598 i != gameobjects.end(); /* nothing */) {
599 GameObject* object = *i;
601 if(object->is_valid()) {
606 before_object_remove(object);
609 i = gameobjects.erase(i);
612 /* add newly created objects */
613 for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
614 i != gameobjects_new.end(); ++i)
616 GameObject* object = *i;
618 before_object_add(object);
620 gameobjects.push_back(object);
622 gameobjects_new.clear();
626 Sector::before_object_add(GameObject* object)
628 Bullet* bullet = dynamic_cast<Bullet*> (object);
630 bullets.push_back(bullet);
632 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
634 moving_objects.push_back(movingobject);
637 TileMap* tilemap = dynamic_cast<TileMap*> (object);
638 if(tilemap && tilemap->is_solid()) solid_tilemaps.push_back(tilemap);
640 Camera* camera = dynamic_cast<Camera*> (object);
642 if(this->camera != 0) {
643 log_warning << "Multiple cameras added. Ignoring" << std::endl;
646 this->camera = camera;
649 Player* player = dynamic_cast<Player*> (object);
651 if(this->player != 0) {
652 log_warning << "Multiple players added. Ignoring" << std::endl;
655 this->player = player;
658 if(_current == this) {
666 Sector::try_expose(GameObject* object)
668 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
669 if(interface != NULL) {
670 HSQUIRRELVM vm = Scripting::global_vm;
671 sq_pushobject(vm, sector_table);
672 interface->expose(vm, -1);
678 Sector::before_object_remove(GameObject* object)
681 try_unexpose(object);
685 Sector::try_unexpose(GameObject* object)
687 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
688 if(interface != NULL) {
689 HSQUIRRELVM vm = Scripting::global_vm;
690 SQInteger oldtop = sq_gettop(vm);
691 sq_pushobject(vm, sector_table);
693 interface->unexpose(vm, -1);
694 } catch(std::exception& e) {
695 log_warning << "Couldn't unregister object: " << e.what() << std::endl;
697 sq_settop(vm, oldtop);
702 Sector::draw(DrawingContext& context)
704 context.set_ambient_color( ambient_light );
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 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
822 TileMap* solids = *i;
824 // test with all tiles in this rectangle
825 int starttilex = int(x1 - solids->get_x_offset()) / 32;
826 int starttiley = int(y1 - solids->get_y_offset()) / 32;
827 int max_x = int(x2 - solids->get_x_offset());
828 int max_y = int(y2+1 - solids->get_y_offset());
830 for(int x = starttilex; x*32 < max_x; ++x) {
831 for(int y = starttiley; y*32 < max_y; ++y) {
832 const Tile* tile = solids->get_tile(x, y);
835 // skip non-solid tiles
836 if((tile->getAttributes() & Tile::SOLID) == 0)
838 // only handle unisolid when the player is falling down and when he was
839 // above the tile before
840 if(tile->getAttributes() & Tile::UNISOLID) {
841 if(movement.y <= 0 || dest.get_bottom() - movement.y - SHIFT_DELTA > y*32)
845 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
847 Vector p1(x*32 + solids->get_x_offset(), y*32 + solids->get_y_offset());
848 Vector p2((x+1)*32 + solids->get_x_offset(), (y+1)*32 + solids->get_y_offset());
849 triangle = AATriangle(p1, p2, tile->getData());
851 collision::rectangle_aatriangle(constraints, dest, triangle);
852 } else { // normal rectangular tile
853 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());
854 check_collisions(constraints, movement, dest, rect);
862 Sector::collision_tile_attributes(const Rect& dest) const
864 float x1 = dest.p1.x;
865 float y1 = dest.p1.y;
866 float x2 = dest.p2.x;
867 float y2 = dest.p2.y;
870 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
871 TileMap* solids = *i;
873 // test with all tiles in this rectangle
874 int starttilex = int(x1 - solids->get_x_offset()) / 32;
875 int starttiley = int(y1 - solids->get_y_offset()) / 32;
876 int max_x = int(x2 - solids->get_x_offset());
877 int max_y = int(y2+1 - solids->get_y_offset());
879 for(int x = starttilex; x*32 < max_x; ++x) {
880 for(int y = starttiley; y*32 < max_y; ++y) {
881 const Tile* tile = solids->get_tile(x, y);
884 result |= tile->getAttributes();
892 /** fills in CollisionHit and Normal vector of 2 intersecting rectangle */
893 static void get_hit_normal(const Rect& r1, const Rect& r2, CollisionHit& hit,
896 float itop = r1.get_bottom() - r2.get_top();
897 float ibottom = r2.get_bottom() - r1.get_top();
898 float ileft = r1.get_right() - r2.get_left();
899 float iright = r2.get_right() - r1.get_left();
901 float vert_penetration = std::min(itop, ibottom);
902 float horiz_penetration = std::min(ileft, iright);
903 if(vert_penetration < horiz_penetration) {
906 normal.y = vert_penetration;
909 normal.y = -vert_penetration;
914 normal.x = horiz_penetration;
917 normal.x = -horiz_penetration;
923 Sector::collision_object(MovingObject* object1, MovingObject* object2) const
925 using namespace collision;
927 const Rect& r1 = object1->dest;
928 const Rect& r2 = object2->dest;
931 if(intersects(object1->dest, object2->dest)) {
933 get_hit_normal(r1, r2, hit, normal);
935 HitResponse response1 = object1->collision(*object2, hit);
936 std::swap(hit.left, hit.right);
937 std::swap(hit.top, hit.bottom);
938 HitResponse response2 = object2->collision(*object1, hit);
939 assert( response1 != SOLID && response1 != PASSTHROUGH );
940 assert( response2 != SOLID && response2 != PASSTHROUGH );
941 if(response1 == CONTINUE && response2 == CONTINUE) {
942 normal *= (0.5 + DELTA);
943 object1->dest.move(-normal);
944 object2->dest.move(normal);
945 } else if (response1 == CONTINUE && response2 == FORCE_MOVE) {
946 normal *= (1 + DELTA);
947 object1->dest.move(-normal);
948 } else if (response1 == FORCE_MOVE && response2 == CONTINUE) {
949 normal *= (1 + DELTA);
950 object2->dest.move(normal);
956 Sector::collision_static(collision::Constraints* constraints,
957 const Vector& movement, const Rect& dest,
960 collision_tilemap(constraints, movement, dest);
962 // collision with other (static) objects
963 for(MovingObjects::iterator i = moving_objects.begin();
964 i != moving_objects.end(); ++i) {
965 MovingObject* moving_object = *i;
966 if(moving_object->get_group() != COLGROUP_STATIC
967 || !moving_object->is_valid())
970 check_collisions(constraints, movement, dest, moving_object->dest,
971 &object, moving_object);
976 Sector::collision_static_constrains(MovingObject& object)
978 using namespace collision;
980 Constraints constraints;
981 Vector movement = object.get_movement();
982 Rect& dest = object.dest;
983 float owidth = object.get_bbox().get_width();
984 float oheight = object.get_bbox().get_height();
986 for(int i = 0; i < 2; ++i) {
987 collision_static(&constraints, Vector(0, movement.y), dest, object);
988 if(!constraints.has_constraints())
991 // apply calculated horizontal constraints
992 if(constraints.bottom < INFINITY) {
993 float height = constraints.bottom - constraints.top;
994 if(height < oheight) {
995 // we're crushed, but ignore this for now, we'll get this again
996 // later if we're really crushed or things will solve itself when
997 // looking at the vertical constraints
999 dest.p2.y = constraints.bottom - DELTA;
1000 dest.p1.y = dest.p2.y - oheight;
1001 } else if(constraints.top > -INFINITY) {
1002 dest.p1.y = constraints.top + DELTA;
1003 dest.p2.y = dest.p1.y + oheight;
1006 if(constraints.has_constraints()) {
1007 if(constraints.hit.bottom) {
1008 dest.move(constraints.ground_movement);
1010 if(constraints.hit.top || constraints.hit.bottom) {
1011 constraints.hit.left = false;
1012 constraints.hit.right = false;
1013 object.collision_solid(constraints.hit);
1017 constraints = Constraints();
1018 for(int i = 0; i < 2; ++i) {
1019 collision_static(&constraints, movement, dest, object);
1020 if(!constraints.has_constraints())
1023 // apply calculated vertical constraints
1024 if(constraints.right < INFINITY) {
1025 float width = constraints.right - constraints.left;
1026 if(width + SHIFT_DELTA < owidth) {
1027 printf("Object %p crushed horizontally... L:%f R:%f\n", &object,
1028 constraints.left, constraints.right);
1033 object.collision_solid(h);
1035 dest.p2.x = constraints.right - DELTA;
1036 dest.p1.x = dest.p2.x - owidth;
1038 } else if(constraints.left > -INFINITY) {
1039 dest.p1.x = constraints.left + DELTA;
1040 dest.p2.x = dest.p1.x + owidth;
1044 if(constraints.has_constraints()) {
1045 if( constraints.hit.left || constraints.hit.right
1046 || constraints.hit.top || constraints.hit.bottom
1047 || constraints.hit.crush )
1048 object.collision_solid(constraints.hit);
1051 // an extra pass to make sure we're not crushed horizontally
1052 constraints = Constraints();
1053 collision_static(&constraints, movement, dest, object);
1054 if(constraints.bottom < INFINITY) {
1055 float height = constraints.bottom - constraints.top;
1056 if(height + SHIFT_DELTA < oheight) {
1057 printf("Object %p crushed vertically...\n", &object);
1062 object.collision_solid(h);
1068 Sector::handle_collisions()
1070 using namespace collision;
1072 // calculate destination positions of the objects
1073 for(MovingObjects::iterator i = moving_objects.begin();
1074 i != moving_objects.end(); ++i) {
1075 MovingObject* moving_object = *i;
1077 moving_object->dest = moving_object->get_bbox();
1078 moving_object->dest.move(moving_object->get_movement());
1081 // part1: COLGROUP_MOVING vs COLGROUP_STATIC and tilemap
1082 for(MovingObjects::iterator i = moving_objects.begin();
1083 i != moving_objects.end(); ++i) {
1084 MovingObject* moving_object = *i;
1085 if((moving_object->get_group() != COLGROUP_MOVING
1086 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
1087 || !moving_object->is_valid())
1090 collision_static_constrains(*moving_object);
1094 // part2: COLGROUP_MOVING vs tile attributes
1095 for(MovingObjects::iterator i = moving_objects.begin();
1096 i != moving_objects.end(); ++i) {
1097 MovingObject* moving_object = *i;
1098 if((moving_object->get_group() != COLGROUP_MOVING
1099 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
1100 || !moving_object->is_valid())
1103 uint32_t tile_attributes = collision_tile_attributes(moving_object->dest);
1104 if(tile_attributes > Tile::FIRST_INTERESTING_FLAG) {
1105 moving_object->collision_tile(tile_attributes);
1109 // part2.5: COLGROUP_MOVING vs COLGROUP_TOUCHABLE
1110 for(MovingObjects::iterator i = moving_objects.begin();
1111 i != moving_objects.end(); ++i) {
1112 MovingObject* moving_object = *i;
1113 if(moving_object->get_group() != COLGROUP_MOVING
1114 || !moving_object->is_valid())
1117 for(MovingObjects::iterator i2 = moving_objects.begin();
1118 i2 != moving_objects.end(); ++i2) {
1119 MovingObject* moving_object_2 = *i2;
1120 if(moving_object_2->get_group() != COLGROUP_TOUCHABLE
1121 || !moving_object_2->is_valid())
1124 if(intersects(moving_object->dest, moving_object_2->dest)) {
1127 get_hit_normal(moving_object->dest, moving_object_2->dest,
1129 moving_object->collision(*moving_object_2, hit);
1130 moving_object_2->collision(*moving_object, hit);
1135 // part3: COLGROUP_MOVING vs COLGROUP_MOVING
1136 for(MovingObjects::iterator i = moving_objects.begin();
1137 i != moving_objects.end(); ++i) {
1138 MovingObject* moving_object = *i;
1140 if(moving_object->get_group() != COLGROUP_MOVING
1141 || !moving_object->is_valid())
1144 for(MovingObjects::iterator i2 = i+1;
1145 i2 != moving_objects.end(); ++i2) {
1146 MovingObject* moving_object_2 = *i2;
1147 if(moving_object_2->get_group() != COLGROUP_MOVING
1148 || !moving_object_2->is_valid())
1151 collision_object(moving_object, moving_object_2);
1155 // apply object movement
1156 for(MovingObjects::iterator i = moving_objects.begin();
1157 i != moving_objects.end(); ++i) {
1158 MovingObject* moving_object = *i;
1160 moving_object->bbox = moving_object->dest;
1161 moving_object->movement = Vector(0, 0);
1166 Sector::is_free_space(const Rect& rect) const
1168 using namespace collision;
1170 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1171 TileMap* solids = *i;
1173 // test with all tiles in this rectangle
1174 int starttilex = int(rect.p1.x - solids->get_x_offset()) / 32;
1175 int starttiley = int(rect.p1.y - solids->get_y_offset()) / 32;
1176 int max_x = int(rect.p2.x - solids->get_x_offset());
1177 int max_y = int(rect.p2.y - solids->get_y_offset());
1179 for(int x = starttilex; x*32 <= max_x; ++x) {
1180 for(int y = starttiley; y*32 <= max_y; ++y) {
1181 const Tile* tile = solids->get_tile(x, y);
1183 if(tile->getAttributes() & Tile::SLOPE) {
1184 AATriangle triangle;
1185 Vector p1(x*32 + solids->get_x_offset(), y*32 + solids->get_y_offset());
1186 Vector p2((x+1)*32 + solids->get_x_offset(), (y+1)*32 + solids->get_y_offset());
1187 triangle = AATriangle(p1, p2, tile->getData());
1188 Constraints constraints;
1189 return collision::rectangle_aatriangle(&constraints, rect, triangle);
1191 if(tile->getAttributes() & Tile::SOLID) return false;
1196 for(MovingObjects::const_iterator i = moving_objects.begin();
1197 i != moving_objects.end(); ++i) {
1198 const MovingObject* moving_object = *i;
1199 if(moving_object->get_group() != COLGROUP_STATIC
1200 || !moving_object->is_valid())
1203 if(intersects(rect, moving_object->get_bbox()))
1211 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
1213 // TODO remove this function and move these checks elsewhere...
1215 Bullet* new_bullet = 0;
1216 if((int)bullets.size() >= player_status->max_fire_bullets)
1218 new_bullet = new Bullet(pos, xm, dir);
1219 add_object(new_bullet);
1221 sound_manager->play("sounds/shoot.wav");
1227 Sector::add_smoke_cloud(const Vector& pos)
1229 add_object(new SmokeCloud(pos));
1234 Sector::play_music(MusicType type)
1236 currentmusic = type;
1237 switch(currentmusic) {
1239 sound_manager->play_music(music);
1242 sound_manager->play_music("music/salcon.ogg");
1244 case HERRING_WARNING_MUSIC:
1245 sound_manager->stop_music(TUX_INVINCIBLE_TIME_WARNING);
1248 sound_manager->play_music("");
1254 Sector::get_music_type()
1256 return currentmusic;
1260 Sector::get_total_badguys()
1262 int total_badguys = 0;
1263 for(GameObjects::iterator i = gameobjects.begin();
1264 i != gameobjects.end(); ++i) {
1265 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
1266 if (badguy && badguy->countMe)
1270 return total_badguys;
1274 Sector::inside(const Rect& rect) const
1276 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1277 TileMap* solids = *i;
1278 bool horizontally = ((rect.p2.x >= 0 + solids->get_x_offset()) && (rect.p1.x <= solids->get_width() * 32 + solids->get_x_offset()));
1279 bool vertically = (rect.p1.y <= solids->get_height() * 32 + solids->get_y_offset());
1280 if (horizontally && vertically) return true;
1286 Sector::get_width() const
1289 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1290 TileMap* solids = *i;
1291 if ((solids->get_width() * 32 + solids->get_x_offset()) > width) width = (solids->get_width() * 32 + solids->get_x_offset());
1297 Sector::get_height() const
1300 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1301 TileMap* solids = *i;
1302 if ((solids->get_height() * 32 + solids->get_y_offset()) > height) height = (solids->get_height() * 32 + solids->get_y_offset());
1308 Sector::change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id)
1310 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1311 TileMap* solids = *i;
1312 solids->change_all(old_tile_id, new_tile_id);