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),
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();
198 if(solid_tilemaps.size() < 1) log_warning << "sector '" << name << "' does not contain a solid tile layer." << std::endl;
202 log_warning << "sector '" << name << "' does not contain a camera." << std::endl;
203 update_game_objects();
204 add_object(new Camera(this));
207 update_game_objects();
211 Sector::parse_old_format(const lisp::Lisp& reader)
214 reader.get("gravity", gravity);
216 std::string backgroundimage;
217 reader.get("background", backgroundimage);
219 reader.get("bkgd_speed", bgspeed);
222 Color bkgd_top, bkgd_bottom;
223 int r = 0, g = 0, b = 128;
224 reader.get("bkgd_red_top", r);
225 reader.get("bkgd_green_top", g);
226 reader.get("bkgd_blue_top", b);
227 bkgd_top.red = static_cast<float> (r) / 255.0f;
228 bkgd_top.green = static_cast<float> (g) / 255.0f;
229 bkgd_top.blue = static_cast<float> (b) / 255.0f;
231 reader.get("bkgd_red_bottom", r);
232 reader.get("bkgd_green_bottom", g);
233 reader.get("bkgd_blue_bottom", b);
234 bkgd_bottom.red = static_cast<float> (r) / 255.0f;
235 bkgd_bottom.green = static_cast<float> (g) / 255.0f;
236 bkgd_bottom.blue = static_cast<float> (b) / 255.0f;
238 if(backgroundimage != "") {
239 Background* background = new Background();
240 background->set_image(
241 std::string("images/background/") + backgroundimage, bgspeed);
242 add_object(background);
244 Gradient* gradient = new Gradient();
245 gradient->set_gradient(bkgd_top, bkgd_bottom);
246 add_object(gradient);
249 std::string particlesystem;
250 reader.get("particle_system", particlesystem);
251 if(particlesystem == "clouds")
252 add_object(new CloudParticleSystem());
253 else if(particlesystem == "snow")
254 add_object(new SnowParticleSystem());
255 else if(particlesystem == "rain")
256 add_object(new RainParticleSystem());
258 Vector startpos(100, 170);
259 reader.get("start_pos_x", startpos.x);
260 reader.get("start_pos_y", startpos.y);
262 SpawnPoint* spawn = new SpawnPoint;
263 spawn->pos = startpos;
264 spawn->name = "main";
265 spawnpoints.push_back(spawn);
267 music = "chipdisko.ogg";
268 reader.get("music", music);
269 music = "music/" + music;
271 int width = 30, height = 15;
272 reader.get("width", width);
273 reader.get("height", height);
275 std::vector<unsigned int> tiles;
276 if(reader.get_vector("interactive-tm", tiles)
277 || reader.get_vector("tilemap", tiles)) {
278 TileMap* tilemap = new TileMap();
279 tilemap->set(width, height, tiles, LAYER_TILES, true);
283 if(reader.get_vector("background-tm", tiles)) {
284 TileMap* tilemap = new TileMap();
285 tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
289 if(reader.get_vector("foreground-tm", tiles)) {
290 TileMap* tilemap = new TileMap();
291 tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
295 // read reset-points (now spawn-points)
296 const lisp::Lisp* resetpoints = reader.get_lisp("reset-points");
298 lisp::ListIterator iter(resetpoints);
300 if(iter.item() == "point") {
302 if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
304 SpawnPoint* sp = new SpawnPoint;
307 spawnpoints.push_back(sp);
310 log_warning << "Unknown token '" << iter.item() << "' in reset-points." << std::endl;
316 const lisp::Lisp* objects = reader.get_lisp("objects");
318 lisp::ListIterator iter(objects);
320 GameObject* object = parse_object(iter.item(), *(iter.lisp()));
324 log_warning << "Unknown object '" << iter.item() << "' in level." << std::endl;
330 Camera* camera = new Camera(this);
333 update_game_objects();
335 if(solid_tilemaps.size() < 1) log_warning << "sector '" << name << "' does not contain a solid tile layer." << std::endl;
338 update_game_objects();
342 Sector::fix_old_tiles()
345 for(std::list<TileMap*>::iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
346 TileMap* solids = *i;
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);
375 Sector::write(lisp::Writer& writer)
377 writer.write_string("name", name);
378 writer.write_float("gravity", gravity);
379 writer.write_string("music", music);
382 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
384 SpawnPoint* spawn = *i;
385 writer.start_list("spawn-points");
386 writer.write_string("name", spawn->name);
387 writer.write_float("x", spawn->pos.x);
388 writer.write_float("y", spawn->pos.y);
389 writer.end_list("spawn-points");
393 for(GameObjects::iterator i = gameobjects.begin();
394 i != gameobjects.end(); ++i) {
395 Serializable* serializable = dynamic_cast<Serializable*> (*i);
397 serializable->write(writer);
402 Sector::run_script(std::istream& in, const std::string& sourcename)
404 using namespace Scripting;
406 // garbage collect thread list
407 for(ScriptList::iterator i = scripts.begin();
408 i != scripts.end(); ) {
409 HSQOBJECT& object = *i;
410 HSQUIRRELVM vm = object_to_vm(object);
412 if(sq_getvmstate(vm) != SQ_VMSTATE_SUSPENDED) {
413 sq_release(global_vm, &object);
414 i = scripts.erase(i);
421 HSQOBJECT object = create_thread(global_vm);
422 scripts.push_back(object);
424 HSQUIRRELVM vm = object_to_vm(object);
426 // set sector_table as roottable for the thread
427 sq_pushobject(vm, sector_table);
430 compile_and_run(vm, in, sourcename);
436 Sector::add_object(GameObject* object)
438 // make sure the object isn't already in the list
440 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
443 assert("object already added to sector" == 0);
446 for(GameObjects::iterator i = gameobjects_new.begin();
447 i != gameobjects_new.end(); ++i) {
449 assert("object already added to sector" == 0);
455 gameobjects_new.push_back(object);
459 Sector::activate(const std::string& spawnpoint)
462 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
464 if((*i)->name == spawnpoint) {
470 log_warning << "Spawnpoint '" << spawnpoint << "' not found." << std::endl;
471 if(spawnpoint != "main") {
474 activate(Vector(0, 0));
482 Sector::activate(const Vector& player_pos)
484 if(_current != this) {
486 _current->deactivate();
489 // register sectortable as sector in scripting
490 HSQUIRRELVM vm = Scripting::global_vm;
491 sq_pushroottable(vm);
492 sq_pushstring(vm, "sector", -1);
493 sq_pushobject(vm, sector_table);
494 if(SQ_FAILED(sq_createslot(vm, -3)))
495 throw Scripting::SquirrelError(vm, "Couldn't set sector in roottable");
498 for(GameObjects::iterator i = gameobjects.begin();
499 i != gameobjects.end(); ++i) {
500 GameObject* object = *i;
506 player->move(player_pos);
507 camera->reset(player->get_pos());
508 update_game_objects();
511 if(init_script != "") {
512 std::istringstream in(init_script);
513 run_script(in, std::string("Sector(") + name + ") - init");
523 // remove sector entry from global vm
524 HSQUIRRELVM vm = Scripting::global_vm;
525 sq_pushroottable(vm);
526 sq_pushstring(vm, "sector", -1);
527 if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
528 throw Scripting::SquirrelError(vm, "Couldn't unset sector in roottable");
531 for(GameObjects::iterator i = gameobjects.begin();
532 i != gameobjects.end(); ++i) {
533 GameObject* object = *i;
535 try_unexpose(object);
542 Sector::get_active_region()
545 camera->get_translation() - Vector(1600, 1200),
546 camera->get_translation() + Vector(1600, 1200));
550 Sector::update(float elapsed_time)
552 player->check_bounds(camera);
555 for(GameObjects::iterator i = gameobjects.begin();
556 i != gameobjects.end(); ++i) {
557 GameObject* object = *i;
558 if(!object->is_valid())
561 object->update(elapsed_time);
564 /* Handle all possible collisions. */
566 update_game_objects();
570 Sector::update_game_objects()
572 /** cleanup marked objects */
573 for(std::vector<Bullet*>::iterator i = bullets.begin();
574 i != bullets.end(); /* nothing */) {
576 if(bullet->is_valid()) {
581 i = bullets.erase(i);
583 for(MovingObjects::iterator i = moving_objects.begin();
584 i != moving_objects.end(); /* nothing */) {
585 MovingObject* moving_object = *i;
586 if(moving_object->is_valid()) {
591 i = moving_objects.erase(i);
593 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
594 i != gameobjects.end(); /* nothing */) {
595 GameObject* object = *i;
597 if(object->is_valid()) {
602 before_object_remove(object);
605 i = gameobjects.erase(i);
608 /* add newly created objects */
609 for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
610 i != gameobjects_new.end(); ++i)
612 GameObject* object = *i;
614 before_object_add(object);
616 gameobjects.push_back(object);
618 gameobjects_new.clear();
622 Sector::before_object_add(GameObject* object)
624 Bullet* bullet = dynamic_cast<Bullet*> (object);
626 bullets.push_back(bullet);
628 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
630 moving_objects.push_back(movingobject);
633 TileMap* tilemap = dynamic_cast<TileMap*> (object);
634 if(tilemap && tilemap->is_solid()) solid_tilemaps.push_back(tilemap);
636 Camera* camera = dynamic_cast<Camera*> (object);
638 if(this->camera != 0) {
639 log_warning << "Multiple cameras added. Ignoring" << std::endl;
642 this->camera = camera;
645 Player* player = dynamic_cast<Player*> (object);
647 if(this->player != 0) {
648 log_warning << "Multiple players added. Ignoring" << std::endl;
651 this->player = player;
654 if(_current == this) {
662 Sector::try_expose(GameObject* object)
664 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
665 if(interface != NULL) {
666 HSQUIRRELVM vm = Scripting::global_vm;
667 sq_pushobject(vm, sector_table);
668 interface->expose(vm, -1);
674 Sector::before_object_remove(GameObject* object)
677 try_unexpose(object);
681 Sector::try_unexpose(GameObject* object)
683 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
684 if(interface != NULL) {
685 HSQUIRRELVM vm = Scripting::global_vm;
686 SQInteger oldtop = sq_gettop(vm);
687 sq_pushobject(vm, sector_table);
689 interface->unexpose(vm, -1);
690 } catch(std::exception& e) {
691 log_warning << "Couldn't unregister object: " << e.what() << std::endl;
693 sq_settop(vm, oldtop);
698 Sector::draw(DrawingContext& context)
700 context.push_transform();
701 context.set_translation(camera->get_translation());
703 for(GameObjects::iterator i = gameobjects.begin();
704 i != gameobjects.end(); ++i) {
705 GameObject* object = *i;
706 if(!object->is_valid())
709 if (draw_solids_only)
711 TileMap* tm = dynamic_cast<TileMap*>(object);
712 if (tm && !tm->is_solid())
716 object->draw(context);
720 Color col(0.2, 0.2, 0.2, 0.7);
721 for(MovingObjects::iterator i = moving_objects.begin();
722 i != moving_objects.end(); ++i) {
723 MovingObject* object = *i;
724 const Rect& rect = object->get_bbox();
726 context.draw_filled_rect(rect, col, LAYER_FOREGROUND1 + 10);
730 context.pop_transform();
733 /*-------------------------------------------------------------------------
734 * Collision Detection
735 *-------------------------------------------------------------------------*/
737 static const float SHIFT_DELTA = 7.0f;
739 /** r1 is supposed to be moving, r2 a solid object */
740 void check_collisions(collision::Constraints* constraints,
741 const Vector& movement, const Rect& r1, const Rect& r2,
742 GameObject* object = NULL, MovingObject* other = NULL)
744 if(!collision::intersects(r1, r2))
747 // calculate intersection
748 float itop = r1.get_bottom() - r2.get_top();
749 float ibottom = r2.get_bottom() - r1.get_top();
750 float ileft = r1.get_right() - r2.get_left();
751 float iright = r2.get_right() - r1.get_left();
753 if(fabsf(movement.y) > fabsf(movement.x)) {
754 if(ileft < SHIFT_DELTA) {
755 constraints->right = std::min(constraints->right, r2.get_left());
757 } else if(iright < SHIFT_DELTA) {
758 constraints->left = std::max(constraints->left, r2.get_right());
762 // shiftout bottom/top
763 if(itop < SHIFT_DELTA) {
764 constraints->bottom = std::min(constraints->bottom, r2.get_top());
766 } else if(ibottom < SHIFT_DELTA) {
767 constraints->top = std::max(constraints->top, r2.get_bottom());
774 HitResponse response = other->collision(*object, dummy);
775 if(response == PASSTHROUGH)
777 if(other->get_movement() != Vector(0, 0)) {
778 // TODO what todo when we collide with 2 moving objects?!?
779 constraints->ground_movement = other->get_movement();
783 float vert_penetration = std::min(itop, ibottom);
784 float horiz_penetration = std::min(ileft, iright);
785 if(vert_penetration < horiz_penetration) {
787 constraints->bottom = std::min(constraints->bottom, r2.get_top());
788 constraints->hit.bottom = true;
790 constraints->top = std::max(constraints->top, r2.get_bottom());
791 constraints->hit.top = true;
795 constraints->right = std::min(constraints->right, r2.get_left());
796 constraints->hit.right = true;
798 constraints->left = std::max(constraints->left, r2.get_right());
799 constraints->hit.left = true;
804 static const float DELTA = .001;
807 Sector::collision_tilemap(collision::Constraints* constraints,
808 const Vector& movement, const Rect& dest) const
810 // calculate rectangle where the object will move
811 float x1 = dest.get_left();
812 float x2 = dest.get_right();
813 float y1 = dest.get_top();
814 float y2 = dest.get_bottom();
816 // test with all tiles in this rectangle
817 int starttilex = int(x1) / 32;
818 int starttiley = int(y1) / 32;
820 int max_y = int(y2+1);
823 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
824 TileMap* solids = *i;
825 for(int x = starttilex; x*32 < max_x; ++x) {
826 for(int y = starttiley; y*32 < max_y; ++y) {
827 const Tile* tile = solids->get_tile(x, y);
830 // skip non-solid tiles
831 if((tile->getAttributes() & Tile::SOLID) == 0)
833 // only handle unisolid when the player is falling down and when he was
834 // above the tile before
835 if(tile->getAttributes() & Tile::UNISOLID) {
836 if(movement.y <= 0 || dest.get_bottom() - movement.y - SHIFT_DELTA > y*32)
840 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
842 Vector p1(x*32, y*32);
843 Vector p2((x+1)*32, (y+1)*32);
844 triangle = AATriangle(p1, p2, tile->getData());
846 collision::rectangle_aatriangle(constraints, dest, triangle);
847 } else { // normal rectangular tile
848 Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
849 check_collisions(constraints, movement, dest, rect);
857 Sector::collision_tile_attributes(const Rect& dest) const
859 float x1 = dest.p1.x;
860 float y1 = dest.p1.y;
861 float x2 = dest.p2.x;
862 float y2 = dest.p2.y;
864 // test with all tiles in this rectangle
865 int starttilex = int(x1) / 32;
866 int starttiley = int(y1) / 32;
871 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
872 TileMap* solids = *i;
873 for(int x = starttilex; x*32 < max_x; ++x) {
874 for(int y = starttiley; y*32 < max_y; ++y) {
875 const Tile* tile = solids->get_tile(x, y);
878 result |= tile->getAttributes();
886 /** fills in CollisionHit and Normal vector of 2 intersecting rectangle */
887 static void get_hit_normal(const Rect& r1, const Rect& r2, CollisionHit& hit,
890 float itop = r1.get_bottom() - r2.get_top();
891 float ibottom = r2.get_bottom() - r1.get_top();
892 float ileft = r1.get_right() - r2.get_left();
893 float iright = r2.get_right() - r1.get_left();
895 float vert_penetration = std::min(itop, ibottom);
896 float horiz_penetration = std::min(ileft, iright);
897 if(vert_penetration < horiz_penetration) {
900 normal.y = vert_penetration;
903 normal.y = -vert_penetration;
908 normal.x = horiz_penetration;
911 normal.x = -horiz_penetration;
917 Sector::collision_object(MovingObject* object1, MovingObject* object2) const
919 using namespace collision;
921 const Rect& r1 = object1->dest;
922 const Rect& r2 = object2->dest;
925 if(intersects(object1->dest, object2->dest)) {
927 get_hit_normal(r1, r2, hit, normal);
929 HitResponse response1 = object1->collision(*object2, hit);
930 std::swap(hit.left, hit.right);
931 std::swap(hit.top, hit.bottom);
932 HitResponse response2 = object2->collision(*object1, hit);
933 assert( response1 != SOLID && response1 != PASSTHROUGH );
934 assert( response2 != SOLID && response2 != PASSTHROUGH );
935 if(response1 == CONTINUE && response2 == CONTINUE) {
936 normal *= (0.5 + DELTA);
937 object1->dest.move(-normal);
938 object2->dest.move(normal);
939 } else if (response1 == CONTINUE && response2 == FORCE_MOVE) {
940 normal *= (1 + DELTA);
941 object1->dest.move(-normal);
942 } else if (response1 == FORCE_MOVE && response2 == CONTINUE) {
943 normal *= (1 + DELTA);
944 object2->dest.move(normal);
950 Sector::collision_static(collision::Constraints* constraints,
951 const Vector& movement, const Rect& dest,
954 collision_tilemap(constraints, movement, dest);
956 // collision with other (static) objects
957 for(MovingObjects::iterator i = moving_objects.begin();
958 i != moving_objects.end(); ++i) {
959 MovingObject* moving_object = *i;
960 if(moving_object->get_group() != COLGROUP_STATIC
961 || !moving_object->is_valid())
964 check_collisions(constraints, movement, dest, moving_object->dest,
965 &object, moving_object);
970 Sector::collision_static_constrains(MovingObject& object)
972 using namespace collision;
974 Constraints constraints;
975 Vector movement = object.get_movement();
976 Rect& dest = object.dest;
977 float owidth = object.get_bbox().get_width();
978 float oheight = object.get_bbox().get_height();
980 for(int i = 0; i < 2; ++i) {
981 collision_static(&constraints, Vector(0, movement.y), dest, object);
982 if(!constraints.has_constraints())
985 // apply calculated horizontal constraints
986 if(constraints.bottom < INFINITY) {
987 float height = constraints.bottom - constraints.top;
988 if(height < oheight) {
989 // we're crushed, but ignore this for now, we'll get this again
990 // later if we're really crushed or things will solve itself when
991 // looking at the vertical constraints
993 dest.p2.y = constraints.bottom - DELTA;
994 dest.p1.y = dest.p2.y - oheight;
995 } else if(constraints.top > -INFINITY) {
996 dest.p1.y = constraints.top + DELTA;
997 dest.p2.y = dest.p1.y + oheight;
1000 if(constraints.has_constraints()) {
1001 if(constraints.hit.bottom) {
1002 dest.move(constraints.ground_movement);
1004 if(constraints.hit.top || constraints.hit.bottom) {
1005 constraints.hit.left = false;
1006 constraints.hit.right = false;
1007 object.collision_solid(constraints.hit);
1011 constraints = Constraints();
1012 for(int i = 0; i < 2; ++i) {
1013 collision_static(&constraints, movement, dest, object);
1014 if(!constraints.has_constraints())
1017 // apply calculated vertical constraints
1018 if(constraints.right < INFINITY) {
1019 float width = constraints.right - constraints.left;
1020 if(width + SHIFT_DELTA < owidth) {
1021 printf("Object %p crushed horizontally... L:%f R:%f\n", &object,
1022 constraints.left, constraints.right);
1027 object.collision_solid(h);
1029 dest.p2.x = constraints.right - DELTA;
1030 dest.p1.x = dest.p2.x - owidth;
1032 } else if(constraints.left > -INFINITY) {
1033 dest.p1.x = constraints.left + DELTA;
1034 dest.p2.x = dest.p1.x + owidth;
1038 if(constraints.has_constraints()) {
1039 if( constraints.hit.left || constraints.hit.right
1040 || constraints.hit.top || constraints.hit.bottom
1041 || constraints.hit.crush )
1042 object.collision_solid(constraints.hit);
1045 // an extra pass to make sure we're not crushed horizontally
1046 constraints = Constraints();
1047 collision_static(&constraints, movement, dest, object);
1048 if(constraints.bottom < INFINITY) {
1049 float height = constraints.bottom - constraints.top;
1050 if(height + SHIFT_DELTA < oheight) {
1051 printf("Object %p crushed vertically...\n", &object);
1056 object.collision_solid(h);
1062 Sector::handle_collisions()
1064 using namespace collision;
1066 // calculate destination positions of the objects
1067 for(MovingObjects::iterator i = moving_objects.begin();
1068 i != moving_objects.end(); ++i) {
1069 MovingObject* moving_object = *i;
1071 moving_object->dest = moving_object->get_bbox();
1072 moving_object->dest.move(moving_object->get_movement());
1075 // part1: COLGROUP_MOVING vs COLGROUP_STATIC and tilemap
1076 for(MovingObjects::iterator i = moving_objects.begin();
1077 i != moving_objects.end(); ++i) {
1078 MovingObject* moving_object = *i;
1079 if((moving_object->get_group() != COLGROUP_MOVING
1080 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
1081 || !moving_object->is_valid())
1084 collision_static_constrains(*moving_object);
1088 // part2: COLGROUP_MOVING vs tile attributes
1089 for(MovingObjects::iterator i = moving_objects.begin();
1090 i != moving_objects.end(); ++i) {
1091 MovingObject* moving_object = *i;
1092 if((moving_object->get_group() != COLGROUP_MOVING
1093 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
1094 || !moving_object->is_valid())
1097 uint32_t tile_attributes = collision_tile_attributes(moving_object->dest);
1098 if(tile_attributes > Tile::FIRST_INTERESTING_FLAG) {
1099 moving_object->collision_tile(tile_attributes);
1103 // part2.5: COLGROUP_MOVING vs COLGROUP_TOUCHABLE
1104 for(MovingObjects::iterator i = moving_objects.begin();
1105 i != moving_objects.end(); ++i) {
1106 MovingObject* moving_object = *i;
1107 if(moving_object->get_group() != COLGROUP_MOVING
1108 || !moving_object->is_valid())
1111 for(MovingObjects::iterator i2 = moving_objects.begin();
1112 i2 != moving_objects.end(); ++i2) {
1113 MovingObject* moving_object_2 = *i2;
1114 if(moving_object_2->get_group() != COLGROUP_TOUCHABLE
1115 || !moving_object_2->is_valid())
1118 if(intersects(moving_object->dest, moving_object_2->dest)) {
1121 get_hit_normal(moving_object->dest, moving_object_2->dest,
1123 moving_object->collision(*moving_object_2, hit);
1124 moving_object_2->collision(*moving_object, hit);
1129 // part3: COLGROUP_MOVING vs COLGROUP_MOVING
1130 for(MovingObjects::iterator i = moving_objects.begin();
1131 i != moving_objects.end(); ++i) {
1132 MovingObject* moving_object = *i;
1134 if(moving_object->get_group() != COLGROUP_MOVING
1135 || !moving_object->is_valid())
1138 for(MovingObjects::iterator i2 = i+1;
1139 i2 != moving_objects.end(); ++i2) {
1140 MovingObject* moving_object_2 = *i2;
1141 if(moving_object_2->get_group() != COLGROUP_MOVING
1142 || !moving_object_2->is_valid())
1145 collision_object(moving_object, moving_object_2);
1149 // apply object movement
1150 for(MovingObjects::iterator i = moving_objects.begin();
1151 i != moving_objects.end(); ++i) {
1152 MovingObject* moving_object = *i;
1154 moving_object->bbox = moving_object->dest;
1155 moving_object->movement = Vector(0, 0);
1160 Sector::is_free_space(const Rect& rect) const
1162 using namespace collision;
1164 // test with all tiles in this rectangle
1165 int starttilex = int(rect.p1.x) / 32;
1166 int starttiley = int(rect.p1.y) / 32;
1167 int max_x = int(rect.p2.x);
1168 int max_y = int(rect.p2.y);
1170 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1171 TileMap* solids = *i;
1172 for(int x = starttilex; x*32 <= max_x; ++x) {
1173 for(int y = starttiley; y*32 <= max_y; ++y) {
1174 const Tile* tile = solids->get_tile(x, y);
1176 if(tile->getAttributes() & Tile::SLOPE) {
1177 AATriangle triangle;
1178 Vector p1(x*32, y*32);
1179 Vector p2((x+1)*32, (y+1)*32);
1180 triangle = AATriangle(p1, p2, tile->getData());
1181 Constraints constraints;
1182 return collision::rectangle_aatriangle(&constraints, rect, triangle);
1184 if(tile->getAttributes() & Tile::SOLID) return false;
1189 for(MovingObjects::const_iterator i = moving_objects.begin();
1190 i != moving_objects.end(); ++i) {
1191 const MovingObject* moving_object = *i;
1192 if(moving_object->get_group() != COLGROUP_STATIC
1193 || !moving_object->is_valid())
1196 if(intersects(rect, moving_object->get_bbox()))
1204 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
1206 // TODO remove this function and move these checks elsewhere...
1208 Bullet* new_bullet = 0;
1209 if((int)bullets.size() >= player_status->max_fire_bullets)
1211 new_bullet = new Bullet(pos, xm, dir);
1212 add_object(new_bullet);
1214 sound_manager->play("sounds/shoot.wav");
1220 Sector::add_smoke_cloud(const Vector& pos)
1222 add_object(new SmokeCloud(pos));
1227 Sector::play_music(MusicType type)
1229 currentmusic = type;
1230 switch(currentmusic) {
1232 sound_manager->play_music(music);
1235 sound_manager->play_music("music/salcon.ogg");
1237 case HERRING_WARNING_MUSIC:
1238 sound_manager->stop_music(TUX_INVINCIBLE_TIME_WARNING);
1241 sound_manager->play_music("");
1247 Sector::get_music_type()
1249 return currentmusic;
1253 Sector::get_total_badguys()
1255 int total_badguys = 0;
1256 for(GameObjects::iterator i = gameobjects.begin();
1257 i != gameobjects.end(); ++i) {
1258 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
1259 if (badguy && badguy->countMe)
1263 return total_badguys;
1267 Sector::inside(const Rect& rect) const
1269 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1270 TileMap* solids = *i;
1271 bool horizontally = ((rect.p2.x >= 0) && (rect.p1.x <= solids->get_width() * 32));
1272 bool vertically = (rect.p1.y <= solids->get_height() * 32);
1273 if (horizontally && vertically) return true;
1279 Sector::get_width() const
1282 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1283 TileMap* solids = *i;
1284 if (solids->get_width() > width) width = solids->get_width();
1290 Sector::get_height() const
1293 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1294 TileMap* solids = *i;
1295 if (solids->get_height() > height) height = solids->get_height();
1301 Sector::change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id)
1303 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1304 TileMap* solids = *i;
1305 solids->change_all(old_tile_id, new_tile_id);