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, "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);
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, "Camera"));
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, "Camera");
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(solids->get_x_offset() + x*32, solids->get_y_offset() + 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 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
817 TileMap* solids = *i;
819 // test with all tiles in this rectangle
820 int starttilex = int(x1 - solids->get_x_offset()) / 32;
821 int starttiley = int(y1 - solids->get_y_offset()) / 32;
822 int max_x = int(x2 - solids->get_x_offset());
823 int max_y = int(y2+1 - solids->get_y_offset());
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 + solids->get_x_offset(), y*32 + solids->get_y_offset());
843 Vector p2((x+1)*32 + solids->get_x_offset(), (y+1)*32 + solids->get_y_offset());
844 triangle = AATriangle(p1, p2, tile->getData());
846 collision::rectangle_aatriangle(constraints, dest, triangle);
847 } else { // normal rectangular tile
848 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());
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;
865 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
866 TileMap* solids = *i;
868 // test with all tiles in this rectangle
869 int starttilex = int(x1 - solids->get_x_offset()) / 32;
870 int starttiley = int(y1 - solids->get_y_offset()) / 32;
871 int max_x = int(x2 - solids->get_x_offset());
872 int max_y = int(y2+1 - solids->get_y_offset());
874 for(int x = starttilex; x*32 < max_x; ++x) {
875 for(int y = starttiley; y*32 < max_y; ++y) {
876 const Tile* tile = solids->get_tile(x, y);
879 result |= tile->getAttributes();
887 /** fills in CollisionHit and Normal vector of 2 intersecting rectangle */
888 static void get_hit_normal(const Rect& r1, const Rect& r2, CollisionHit& hit,
891 float itop = r1.get_bottom() - r2.get_top();
892 float ibottom = r2.get_bottom() - r1.get_top();
893 float ileft = r1.get_right() - r2.get_left();
894 float iright = r2.get_right() - r1.get_left();
896 float vert_penetration = std::min(itop, ibottom);
897 float horiz_penetration = std::min(ileft, iright);
898 if(vert_penetration < horiz_penetration) {
901 normal.y = vert_penetration;
904 normal.y = -vert_penetration;
909 normal.x = horiz_penetration;
912 normal.x = -horiz_penetration;
918 Sector::collision_object(MovingObject* object1, MovingObject* object2) const
920 using namespace collision;
922 const Rect& r1 = object1->dest;
923 const Rect& r2 = object2->dest;
926 if(intersects(object1->dest, object2->dest)) {
928 get_hit_normal(r1, r2, hit, normal);
930 HitResponse response1 = object1->collision(*object2, hit);
931 std::swap(hit.left, hit.right);
932 std::swap(hit.top, hit.bottom);
933 HitResponse response2 = object2->collision(*object1, hit);
934 assert( response1 != SOLID && response1 != PASSTHROUGH );
935 assert( response2 != SOLID && response2 != PASSTHROUGH );
936 if(response1 == CONTINUE && response2 == CONTINUE) {
937 normal *= (0.5 + DELTA);
938 object1->dest.move(-normal);
939 object2->dest.move(normal);
940 } else if (response1 == CONTINUE && response2 == FORCE_MOVE) {
941 normal *= (1 + DELTA);
942 object1->dest.move(-normal);
943 } else if (response1 == FORCE_MOVE && response2 == CONTINUE) {
944 normal *= (1 + DELTA);
945 object2->dest.move(normal);
951 Sector::collision_static(collision::Constraints* constraints,
952 const Vector& movement, const Rect& dest,
955 collision_tilemap(constraints, movement, dest);
957 // collision with other (static) objects
958 for(MovingObjects::iterator i = moving_objects.begin();
959 i != moving_objects.end(); ++i) {
960 MovingObject* moving_object = *i;
961 if(moving_object->get_group() != COLGROUP_STATIC
962 || !moving_object->is_valid())
965 check_collisions(constraints, movement, dest, moving_object->dest,
966 &object, moving_object);
971 Sector::collision_static_constrains(MovingObject& object)
973 using namespace collision;
975 Constraints constraints;
976 Vector movement = object.get_movement();
977 Rect& dest = object.dest;
978 float owidth = object.get_bbox().get_width();
979 float oheight = object.get_bbox().get_height();
981 for(int i = 0; i < 2; ++i) {
982 collision_static(&constraints, Vector(0, movement.y), dest, object);
983 if(!constraints.has_constraints())
986 // apply calculated horizontal constraints
987 if(constraints.bottom < INFINITY) {
988 float height = constraints.bottom - constraints.top;
989 if(height < oheight) {
990 // we're crushed, but ignore this for now, we'll get this again
991 // later if we're really crushed or things will solve itself when
992 // looking at the vertical constraints
994 dest.p2.y = constraints.bottom - DELTA;
995 dest.p1.y = dest.p2.y - oheight;
996 } else if(constraints.top > -INFINITY) {
997 dest.p1.y = constraints.top + DELTA;
998 dest.p2.y = dest.p1.y + oheight;
1001 if(constraints.has_constraints()) {
1002 if(constraints.hit.bottom) {
1003 dest.move(constraints.ground_movement);
1005 if(constraints.hit.top || constraints.hit.bottom) {
1006 constraints.hit.left = false;
1007 constraints.hit.right = false;
1008 object.collision_solid(constraints.hit);
1012 constraints = Constraints();
1013 for(int i = 0; i < 2; ++i) {
1014 collision_static(&constraints, movement, dest, object);
1015 if(!constraints.has_constraints())
1018 // apply calculated vertical constraints
1019 if(constraints.right < INFINITY) {
1020 float width = constraints.right - constraints.left;
1021 if(width + SHIFT_DELTA < owidth) {
1022 printf("Object %p crushed horizontally... L:%f R:%f\n", &object,
1023 constraints.left, constraints.right);
1028 object.collision_solid(h);
1030 dest.p2.x = constraints.right - DELTA;
1031 dest.p1.x = dest.p2.x - owidth;
1033 } else if(constraints.left > -INFINITY) {
1034 dest.p1.x = constraints.left + DELTA;
1035 dest.p2.x = dest.p1.x + owidth;
1039 if(constraints.has_constraints()) {
1040 if( constraints.hit.left || constraints.hit.right
1041 || constraints.hit.top || constraints.hit.bottom
1042 || constraints.hit.crush )
1043 object.collision_solid(constraints.hit);
1046 // an extra pass to make sure we're not crushed horizontally
1047 constraints = Constraints();
1048 collision_static(&constraints, movement, dest, object);
1049 if(constraints.bottom < INFINITY) {
1050 float height = constraints.bottom - constraints.top;
1051 if(height + SHIFT_DELTA < oheight) {
1052 printf("Object %p crushed vertically...\n", &object);
1057 object.collision_solid(h);
1063 Sector::handle_collisions()
1065 using namespace collision;
1067 // calculate destination positions of the objects
1068 for(MovingObjects::iterator i = moving_objects.begin();
1069 i != moving_objects.end(); ++i) {
1070 MovingObject* moving_object = *i;
1072 moving_object->dest = moving_object->get_bbox();
1073 moving_object->dest.move(moving_object->get_movement());
1076 // part1: COLGROUP_MOVING vs COLGROUP_STATIC and tilemap
1077 for(MovingObjects::iterator i = moving_objects.begin();
1078 i != moving_objects.end(); ++i) {
1079 MovingObject* moving_object = *i;
1080 if((moving_object->get_group() != COLGROUP_MOVING
1081 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
1082 || !moving_object->is_valid())
1085 collision_static_constrains(*moving_object);
1089 // part2: COLGROUP_MOVING vs tile attributes
1090 for(MovingObjects::iterator i = moving_objects.begin();
1091 i != moving_objects.end(); ++i) {
1092 MovingObject* moving_object = *i;
1093 if((moving_object->get_group() != COLGROUP_MOVING
1094 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
1095 || !moving_object->is_valid())
1098 uint32_t tile_attributes = collision_tile_attributes(moving_object->dest);
1099 if(tile_attributes > Tile::FIRST_INTERESTING_FLAG) {
1100 moving_object->collision_tile(tile_attributes);
1104 // part2.5: COLGROUP_MOVING vs COLGROUP_TOUCHABLE
1105 for(MovingObjects::iterator i = moving_objects.begin();
1106 i != moving_objects.end(); ++i) {
1107 MovingObject* moving_object = *i;
1108 if(moving_object->get_group() != COLGROUP_MOVING
1109 || !moving_object->is_valid())
1112 for(MovingObjects::iterator i2 = moving_objects.begin();
1113 i2 != moving_objects.end(); ++i2) {
1114 MovingObject* moving_object_2 = *i2;
1115 if(moving_object_2->get_group() != COLGROUP_TOUCHABLE
1116 || !moving_object_2->is_valid())
1119 if(intersects(moving_object->dest, moving_object_2->dest)) {
1122 get_hit_normal(moving_object->dest, moving_object_2->dest,
1124 moving_object->collision(*moving_object_2, hit);
1125 moving_object_2->collision(*moving_object, hit);
1130 // part3: COLGROUP_MOVING vs COLGROUP_MOVING
1131 for(MovingObjects::iterator i = moving_objects.begin();
1132 i != moving_objects.end(); ++i) {
1133 MovingObject* moving_object = *i;
1135 if(moving_object->get_group() != COLGROUP_MOVING
1136 || !moving_object->is_valid())
1139 for(MovingObjects::iterator i2 = i+1;
1140 i2 != moving_objects.end(); ++i2) {
1141 MovingObject* moving_object_2 = *i2;
1142 if(moving_object_2->get_group() != COLGROUP_MOVING
1143 || !moving_object_2->is_valid())
1146 collision_object(moving_object, moving_object_2);
1150 // apply object movement
1151 for(MovingObjects::iterator i = moving_objects.begin();
1152 i != moving_objects.end(); ++i) {
1153 MovingObject* moving_object = *i;
1155 moving_object->bbox = moving_object->dest;
1156 moving_object->movement = Vector(0, 0);
1161 Sector::is_free_space(const Rect& rect) const
1163 using namespace collision;
1165 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1166 TileMap* solids = *i;
1168 // test with all tiles in this rectangle
1169 int starttilex = int(rect.p1.x - solids->get_x_offset()) / 32;
1170 int starttiley = int(rect.p1.y - solids->get_y_offset()) / 32;
1171 int max_x = int(rect.p2.x - solids->get_x_offset());
1172 int max_y = int(rect.p2.y - solids->get_y_offset());
1174 for(int x = starttilex; x*32 <= max_x; ++x) {
1175 for(int y = starttiley; y*32 <= max_y; ++y) {
1176 const Tile* tile = solids->get_tile(x, y);
1178 if(tile->getAttributes() & Tile::SLOPE) {
1179 AATriangle triangle;
1180 Vector p1(x*32 + solids->get_x_offset(), y*32 + solids->get_y_offset());
1181 Vector p2((x+1)*32 + solids->get_x_offset(), (y+1)*32 + solids->get_y_offset());
1182 triangle = AATriangle(p1, p2, tile->getData());
1183 Constraints constraints;
1184 return collision::rectangle_aatriangle(&constraints, rect, triangle);
1186 if(tile->getAttributes() & Tile::SOLID) return false;
1191 for(MovingObjects::const_iterator i = moving_objects.begin();
1192 i != moving_objects.end(); ++i) {
1193 const MovingObject* moving_object = *i;
1194 if(moving_object->get_group() != COLGROUP_STATIC
1195 || !moving_object->is_valid())
1198 if(intersects(rect, moving_object->get_bbox()))
1206 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
1208 // TODO remove this function and move these checks elsewhere...
1210 Bullet* new_bullet = 0;
1211 if((int)bullets.size() >= player_status->max_fire_bullets)
1213 new_bullet = new Bullet(pos, xm, dir);
1214 add_object(new_bullet);
1216 sound_manager->play("sounds/shoot.wav");
1222 Sector::add_smoke_cloud(const Vector& pos)
1224 add_object(new SmokeCloud(pos));
1229 Sector::play_music(MusicType type)
1231 currentmusic = type;
1232 switch(currentmusic) {
1234 sound_manager->play_music(music);
1237 sound_manager->play_music("music/salcon.ogg");
1239 case HERRING_WARNING_MUSIC:
1240 sound_manager->stop_music(TUX_INVINCIBLE_TIME_WARNING);
1243 sound_manager->play_music("");
1249 Sector::get_music_type()
1251 return currentmusic;
1255 Sector::get_total_badguys()
1257 int total_badguys = 0;
1258 for(GameObjects::iterator i = gameobjects.begin();
1259 i != gameobjects.end(); ++i) {
1260 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
1261 if (badguy && badguy->countMe)
1265 return total_badguys;
1269 Sector::inside(const Rect& rect) const
1271 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1272 TileMap* solids = *i;
1273 bool horizontally = ((rect.p2.x >= 0 + solids->get_x_offset()) && (rect.p1.x <= solids->get_width() * 32 + solids->get_x_offset()));
1274 bool vertically = (rect.p1.y <= solids->get_height() * 32 + solids->get_y_offset());
1275 if (horizontally && vertically) return true;
1281 Sector::get_width() const
1284 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1285 TileMap* solids = *i;
1286 if ((solids->get_width() * 32 + solids->get_x_offset()) > width) width = (solids->get_width() * 32 + solids->get_x_offset());
1292 Sector::get_height() const
1295 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1296 TileMap* solids = *i;
1297 if ((solids->get_height() * 32 + solids->get_y_offset()) > height) height = (solids->get_height() * 32 + solids->get_y_offset());
1303 Sector::change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id)
1305 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1306 TileMap* solids = *i;
1307 solids->change_all(old_tile_id, new_tile_id);